Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: sandbox_light/src/libraries/util/Math.cc @ 5789

Last change on this file since 5789 was 5789, checked in by rgrieder, 15 years ago

Stripped sandbox further down to get a light version that excludes almost all core features.
Also, the Ogre dependency has been removed and a little ogremath library been added.

  • Property svn:eol-style set to native
File size: 7.1 KB
RevLine 
[1505]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
[1791]29/**
[2087]30    @file
[1791]31    @brief Implementation of several math-functions.
32*/
33
[2087]34#include "Math.h"
35
36#include "MathConvert.h"
37#include "SubString.h"
[1505]38
[2171]39namespace orxonox
[1505]40{
[2171]41    /**
42        @brief Function for writing a Radian to a stream.
43    */
44    std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian)
45    {
46        out << radian.valueRadians();
47        return out;
48    }
[1505]49
[2171]50    /**
51        @brief Function for reading a Radian from a stream.
52    */
53    std::istream& operator>>(std::istream& in, orxonox::Radian& radian)
54    {
55        float temp;
56        in >> temp;
57        radian = temp;
58        return in;
59    }
[1505]60
[2171]61    /**
62        @brief Function for writing a Degree to a stream.
63    */
64    std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree)
65    {
66        out << degree.valueDegrees();
67        return out;
68    }
[1564]69
[2171]70    /**
71        @brief Function for reading a Degree from a stream.
72    */
73    std::istream& operator>>(std::istream& in, orxonox::Degree& degree)
74    {
75        float temp;
76        in >> temp;
77        degree = temp;
78        return in;
79    }
[1564]80
[2171]81    unsigned long getUniqueNumber()
82    {
83        static unsigned long aNumber = 135;
84        return aNumber++;
85    }
[2087]86
87
[2171]88    //////////////////////////
89    // Conversion functions //
90    //////////////////////////
[2087]91
[2171]92    // std::string to Vector2
93    bool ConverterFallback<std::string, orxonox::Vector2>::convert(orxonox::Vector2* output, const std::string& input)
[2087]94    {
[2171]95        size_t opening_parenthesis, closing_parenthesis = input.find(')');
96        if ((opening_parenthesis = input.find('(')) == std::string::npos)
97            opening_parenthesis = 0;
98        else
99            opening_parenthesis++;
[2087]100
[2171]101        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
102                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
103        if (tokens.size() >= 2)
104        {
[3196]105            if (!convertValue(&(output->x), tokens[0]))
[2171]106                return false;
[3196]107            if (!convertValue(&(output->y), tokens[1]))
[2171]108                return false;
109
110            return true;
111        }
112        return false;
[2087]113    }
114
[2171]115    // std::string to Vector3
116    bool ConverterFallback<std::string, orxonox::Vector3>::convert(orxonox::Vector3* output, const std::string& input)
[2087]117    {
[2171]118        size_t opening_parenthesis, closing_parenthesis = input.find(')');
119        if ((opening_parenthesis = input.find('(')) == std::string::npos)
120            opening_parenthesis = 0;
121        else
122            opening_parenthesis++;
[2087]123
[2171]124        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
125                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
126        if (tokens.size() >= 3)
127        {
[3196]128            if (!convertValue(&(output->x), tokens[0]))
[2171]129                return false;
[3196]130            if (!convertValue(&(output->y), tokens[1]))
[2171]131                return false;
[3196]132            if (!convertValue(&(output->z), tokens[2]))
[2171]133                return false;
134
135            return true;
136        }
137        return false;
[2087]138    }
139
[2171]140    // std::string to Vector4
141    bool ConverterFallback<std::string, orxonox::Vector4>::convert(orxonox::Vector4* output, const std::string& input)
[2087]142    {
[2171]143        size_t opening_parenthesis, closing_parenthesis = input.find(')');
144        if ((opening_parenthesis = input.find('(')) == std::string::npos)
145            opening_parenthesis = 0;
146        else
147            opening_parenthesis++;
[2087]148
[2171]149        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis),
150                         ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
151        if (tokens.size() >= 4)
152        {
[3196]153            if (!convertValue(&(output->x), tokens[0]))
[2171]154                return false;
[3196]155            if (!convertValue(&(output->y), tokens[1]))
[2171]156                return false;
[3196]157            if (!convertValue(&(output->z), tokens[2]))
[2171]158                return false;
[3196]159            if (!convertValue(&(output->w), tokens[3]))
[2171]160                return false;
161
162            return true;
163        }
164        return false;
[2087]165    }
166
[2171]167    // std::string to Quaternion
168    bool ConverterFallback<std::string, orxonox::Quaternion>::convert(orxonox::Quaternion* output, const std::string& input)
[2087]169    {
[2171]170        size_t opening_parenthesis, closing_parenthesis = input.find(')');
171        if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
[2087]172
[2171]173        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
174        if (tokens.size() >= 4)
175        {
[3196]176            if (!convertValue(&(output->w), tokens[0]))
[2171]177                return false;
[3196]178            if (!convertValue(&(output->x), tokens[1]))
[2171]179                return false;
[3196]180            if (!convertValue(&(output->y), tokens[2]))
[2171]181                return false;
[3196]182            if (!convertValue(&(output->z), tokens[3]))
[2171]183                return false;
184
185            return true;
186        }
187        return false;
[2087]188    }
189
[2171]190    // std::string to ColourValue
191    bool ConverterFallback<std::string, orxonox::ColourValue>::convert(orxonox::ColourValue* output, const std::string& input)
192    {
193        size_t opening_parenthesis, closing_parenthesis = input.find(')');
194        if ((opening_parenthesis = input.find('(')) == std::string::npos) { opening_parenthesis = 0; } else { opening_parenthesis++; }
[2087]195
[2171]196        SubString tokens(input.substr(opening_parenthesis, closing_parenthesis - opening_parenthesis), ",", SubString::WhiteSpaces, false, '\\', true, '"', true, '\0', '\0', true, '\0');
197        if (tokens.size() >= 3)
[2087]198        {
[3196]199            if (!convertValue(&(output->r), tokens[0]))
[2087]200                return false;
[3196]201            if (!convertValue(&(output->g), tokens[1]))
[2171]202                return false;
[3196]203            if (!convertValue(&(output->b), tokens[2]))
[2171]204                return false;
205            if (tokens.size() >= 4)
206            {
[3196]207                if (!convertValue(&(output->a), tokens[3]))
[2171]208                    return false;
209            }
210            else
211                output->a = 1.0;
212
213            return true;
[2087]214        }
[2171]215        return false;
[2087]216    }
217}
Note: See TracBrowser for help on using the repository browser.