Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/console/src/util/MultiTypeMath.cc @ 1319

Last change on this file since 1319 was 1319, checked in by landauf, 16 years ago

added support for Vector4 to MultiTypeMath and added a new function called 'assimilate' that converts the stored value of a given MultiType to it's own type and assigns it.

File size: 10.5 KB
Line 
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 *   Inspiration: MultiType by Benjamin Grauer
28 */
29
30#include "MultiTypeMath.h"
31#include "Convert.h"
32
33MultiTypeMath::MultiTypeMath(MultiType type) : MultiTypeString(type)
34{
35    if (type == MT_vector2)
36        this->vector2_ = orxonox::Vector2(0, 0);
37    else if (type == MT_vector3)
38        this->vector3_ = orxonox::Vector3(0, 0, 0);
39    else if (type == MT_vector4)
40        this->vector4_ = orxonox::Vector4(0, 0, 0, 0);
41    else if (type == MT_colourvalue)
42        this->colourvalue_ = orxonox::ColourValue(0, 0, 0, 0);
43    else if (type == MT_quaternion)
44        this->quaternion_ = orxonox::Quaternion(1, 0, 0, 0);
45    else if (type == MT_radian)
46        this->radian_ = orxonox::Radian(0);
47    else if (type == MT_degree)
48        this->degree_ = orxonox::Degree(0);
49}
50
51bool MultiTypeMath::operator==(const MultiTypeMath& mtm) const
52{
53    if (!MultiTypeString::operator==(mtm) && this->type_ == mtm.type_)
54    {
55        if (this->type_ == MT_vector2)
56            return (this->vector2_ == mtm.vector2_);
57        else if (this->type_ == MT_vector3)
58            return (this->vector3_ == mtm.vector3_);
59        else if (this->type_ == MT_vector4)
60            return (this->vector4_ == mtm.vector4_);
61        else if (this->type_ == MT_colourvalue)
62            return (this->colourvalue_ == mtm.colourvalue_);
63        else if (this->type_ == MT_quaternion)
64            return (this->quaternion_ == mtm.quaternion_);
65        else if (this->type_ == MT_radian)
66            return (this->radian_ == mtm.radian_);
67        else if (this->type_ == MT_degree)
68            return (this->degree_ == mtm.degree_);
69    }
70
71    return false;
72}
73
74bool MultiTypeMath::operator!=(const MultiTypeMath& mtm) const
75{
76    if (MultiTypeString::operator==(mtm) && this->type_ == mtm.type_)
77    {
78        if (this->type_ == MT_vector2)
79            return (this->vector2_ != mtm.vector2_);
80        else if (this->type_ == MT_vector3)
81            return (this->vector3_ != mtm.vector3_);
82        else if (this->type_ == MT_vector4)
83            return (this->vector4_ != mtm.vector4_);
84        else if (this->type_ == MT_colourvalue)
85            return (this->colourvalue_ != mtm.colourvalue_);
86        else if (this->type_ == MT_quaternion)
87            return (this->quaternion_ != mtm.quaternion_);
88        else if (this->type_ == MT_radian)
89            return (this->radian_ != mtm.radian_);
90        else if (this->type_ == MT_degree)
91            return (this->degree_ != mtm.degree_);
92    }
93
94    return true;
95}
96
97MultiTypeMath::operator void*() const
98{ return (this->type_ == MT_void) ? this->value_.void_ : getConvertedValue<MultiTypeMath, void*>(*this, 0); }
99MultiTypeMath::operator int() const
100{ return (this->type_ == MT_int) ? this->value_.int_ : getConvertedValue<MultiTypeMath, int>(*this, 0); }
101MultiTypeMath::operator unsigned int() const
102{ return (this->type_ == MT_uint) ? this->value_.uint_ : getConvertedValue<MultiTypeMath, unsigned int>(*this, 0); }
103MultiTypeMath::operator char() const
104{ return (this->type_ == MT_char) ? this->value_.char_ : getConvertedValue<MultiTypeMath, char>(*this, 0); }
105MultiTypeMath::operator unsigned char() const
106{ return (this->type_ == MT_uchar) ? this->value_.uchar_ : getConvertedValue<MultiTypeMath, unsigned char>(*this, 0); }
107MultiTypeMath::operator short() const
108{ return (this->type_ == MT_short) ? this->value_.short_ : getConvertedValue<MultiTypeMath, short>(*this, 0); }
109MultiTypeMath::operator unsigned short() const
110{ return (this->type_ == MT_ushort) ? this->value_.ushort_ : getConvertedValue<MultiTypeMath, unsigned short>(*this, 0); }
111MultiTypeMath::operator long() const
112{ return (this->type_ == MT_long) ? this->value_.long_ : getConvertedValue<MultiTypeMath, long>(*this, 0); }
113MultiTypeMath::operator unsigned long() const
114{ return (this->type_ == MT_ulong) ? this->value_.ulong_ : getConvertedValue<MultiTypeMath, unsigned long>(*this, 0); }
115MultiTypeMath::operator float() const
116{ return (this->type_ == MT_float) ? this->value_.float_ : getConvertedValue<MultiTypeMath, float>(*this, 0); }
117MultiTypeMath::operator double() const
118{ return (this->type_ == MT_double) ? this->value_.double_ : getConvertedValue<MultiTypeMath, double>(*this, 0); }
119MultiTypeMath::operator long double() const
120{ return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : getConvertedValue<MultiTypeMath, long double>(*this, 0); }
121MultiTypeMath::operator bool() const
122{ return (this->type_ == MT_bool) ? this->value_.bool_ : getConvertedValue<MultiTypeMath, bool>(*this, 0); }
123MultiTypeMath::operator std::string() const
124{ return (this->type_ == MT_string) ? this->string_ : getConvertedValue<MultiTypeMath, std::string>(*this); }
125MultiTypeMath::operator const char*() const
126{ return ((this->type_ == MT_constchar) ? this->string_ : getConvertedValue<MultiTypeMath, std::string>(*this)).c_str(); }
127MultiTypeMath::operator orxonox::Vector2() const
128{ return (this->type_ == MT_vector2) ? this->vector2_ : getConvertedValue<MultiTypeMath, orxonox::Vector2>(*this); }
129MultiTypeMath::operator orxonox::Vector3() const
130{ return (this->type_ == MT_vector3) ? this->vector3_ : getConvertedValue<MultiTypeMath, orxonox::Vector3>(*this); }
131MultiTypeMath::operator orxonox::Vector4() const
132{ return (this->type_ == MT_vector4) ? this->vector4_ : getConvertedValue<MultiTypeMath, orxonox::Vector4>(*this); }
133MultiTypeMath::operator orxonox::Quaternion() const
134{ return (this->type_ == MT_quaternion) ? this->quaternion_ : getConvertedValue<MultiTypeMath, orxonox::Quaternion>(*this); }
135MultiTypeMath::operator orxonox::ColourValue() const
136{ return (this->type_ == MT_colourvalue) ? this->colourvalue_ : getConvertedValue<MultiTypeMath, orxonox::ColourValue>(*this); }
137MultiTypeMath::operator orxonox::Radian() const
138{ return (this->type_ == MT_radian) ? this->radian_ : getConvertedValue<MultiTypeMath, orxonox::Radian>(*this); }
139MultiTypeMath::operator orxonox::Degree() const
140{ return (this->type_ == MT_degree) ? this->degree_ : getConvertedValue<MultiTypeMath, orxonox::Degree>(*this); }
141
142void MultiTypeMath::setValue(const MultiTypeMath& mtm)
143{
144    MultiTypeString::setValue(mtm);
145    this->vector2_ = mtm.vector2_;
146    this->vector3_ = mtm.vector3_;
147    this->vector4_ = mtm.vector4_;
148    this->quaternion_ = mtm.quaternion_;
149    this->colourvalue_ = mtm.colourvalue_;
150    this->radian_ = mtm.radian_;
151    this->degree_ = mtm.degree_;
152}
153
154std::string MultiTypeMath::getTypename() const
155{
156    if (this->type_ == MT_vector2)
157        return "Vector2";
158    else if (this->type_ == MT_vector3)
159        return "Vector3";
160    else if (this->type_ == MT_vector4)
161        return "Vector4";
162    else if (this->type_ == MT_colourvalue)
163        return "ColourValue";
164    else if (this->type_ == MT_quaternion)
165        return "Quaternion";
166    else if (this->type_ == MT_radian)
167        return "Radian";
168    else if (this->type_ == MT_degree)
169        return "Degree";
170    else
171        return MultiTypeString::getTypename();
172}
173
174std::string MultiTypeMath::toString() const
175{
176    std::string output;
177
178    if (this->type_ == MT_vector2)
179        ConvertValue(&output, this->vector2_);
180    else if (this->type_ == MT_vector3)
181        ConvertValue(&output, this->vector3_);
182    else if (this->type_ == MT_vector4)
183        ConvertValue(&output, this->vector4_);
184    else if (this->type_ == MT_colourvalue)
185        ConvertValue(&output, this->colourvalue_);
186    else if (this->type_ == MT_quaternion)
187        ConvertValue(&output, this->quaternion_);
188    else if (this->type_ == MT_radian)
189        ConvertValue(&output, this->radian_);
190    else if (this->type_ == MT_degree)
191        ConvertValue(&output, this->degree_);
192    else
193        return MultiTypeString::toString();
194
195    return output;
196}
197
198bool MultiTypeMath::fromString(const std::string value)
199{
200    if (this->type_ == MT_vector2)
201        return ConvertValue(&this->vector2_, value, orxonox::Vector2(0, 0));
202    else if (this->type_ == MT_vector3)
203        return ConvertValue(&this->vector3_, value, orxonox::Vector3(0, 0, 0));
204    else if (this->type_ == MT_vector4)
205        return ConvertValue(&this->vector4_, value, orxonox::Vector4(0, 0, 0, 0));
206    else if (this->type_ == MT_colourvalue)
207        return ConvertValue(&this->colourvalue_, value, orxonox::ColourValue(0, 0, 0, 0));
208    else if (this->type_ == MT_quaternion)
209        return ConvertValue(&this->quaternion_, value, orxonox::Quaternion(1, 0, 0, 0));
210    else if (this->type_ == MT_radian)
211        return ConvertValue(&this->radian_, value, orxonox::Radian(0));
212    else if (this->type_ == MT_degree)
213        return ConvertValue(&this->degree_, value, orxonox::Degree(0));
214    else
215        return MultiTypeString::fromString(value);
216}
217
218void MultiTypeMath::assimilate(const MultiTypeMath& mtm)
219{
220    if (this->type_ == MT_vector2)
221        this->vector2_ = mtm.operator orxonox::Vector2();
222    else if (this->type_ == MT_vector3)
223        this->vector3_ = mtm.operator orxonox::Vector3();
224    else if (this->type_ == MT_vector4)
225        this->vector4_ = mtm.operator orxonox::Vector4();
226    else if (this->type_ == MT_colourvalue)
227        this->colourvalue_ = mtm;
228    else if (this->type_ == MT_quaternion)
229        this->quaternion_ = mtm;
230    else if (this->type_ == MT_radian)
231        this->radian_ = mtm.operator orxonox::Radian();
232    else if (this->type_ == MT_degree)
233        this->degree_ = mtm.operator orxonox::Degree();
234    else
235        MultiTypeString::assimilate(mtm);
236}
237
238std::ostream& operator<<(std::ostream& out, MultiTypeMath& mtm)
239{
240    out << mtm.toString();
241    return out;
242}
Note: See TracBrowser for help on using the repository browser.