Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core/src/util/MultiTypeMath.cc @ 854

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

hopefully fixed the bug in XMLPort and Converter

File size: 6.7 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 *   Inspiration: MultiType by Benjamin Grauer
27 */
28
29#include "MultiTypeMath.h"
30#include "Convert.h"
31
32MultiTypeMath::MultiTypeMath(MultiType type) : MultiTypeString(type)
33{
34    if (type == MT_vector2)
35        this->vector2_ = orxonox::Vector2(0, 0);
36    else if (type == MT_vector3)
37        this->vector3_ = orxonox::Vector3(0, 0, 0);
38    else if (type == MT_colourvalue)
39        this->colourvalue_ = orxonox::ColourValue(0, 0, 0, 0);
40    else if (type == MT_quaternion)
41        this->quaternion_ = orxonox::Quaternion(1, 0, 0, 0);
42    else if (type == MT_radian)
43        this->radian_ = orxonox::Radian(0);
44    else if (type == MT_degree)
45        this->degree_ = orxonox::Degree(0);
46}
47
48bool MultiTypeMath::operator==(const MultiTypeMath& mtm) const
49{
50    if (!MultiTypeString::operator==(mtm) && this->type_ == mtm.type_)
51    {
52        if (this->type_ == MT_vector2)
53            return (this->vector2_ == mtm.vector2_);
54        else if (this->type_ == MT_vector3)
55            return (this->vector3_ == mtm.vector3_);
56        else if (this->type_ == MT_colourvalue)
57            return (this->colourvalue_ == mtm.colourvalue_);
58        else if (this->type_ == MT_quaternion)
59            return (this->quaternion_ == mtm.quaternion_);
60        else if (this->type_ == MT_radian)
61            return (this->radian_ == mtm.radian_);
62        else if (this->type_ == MT_degree)
63            return (this->degree_ == mtm.degree_);
64    }
65
66    return false;
67}
68
69bool MultiTypeMath::operator!=(const MultiTypeMath& mtm) const
70{
71    if (MultiTypeString::operator==(mtm) && this->type_ == mtm.type_)
72    {
73        if (this->type_ == MT_vector2)
74            return (this->vector2_ != mtm.vector2_);
75        else if (this->type_ == MT_vector3)
76            return (this->vector3_ != mtm.vector3_);
77        else if (this->type_ == MT_colourvalue)
78            return (this->colourvalue_ != mtm.colourvalue_);
79        else if (this->type_ == MT_quaternion)
80            return (this->quaternion_ != mtm.quaternion_);
81        else if (this->type_ == MT_radian)
82            return (this->radian_ != mtm.radian_);
83        else if (this->type_ == MT_degree)
84            return (this->degree_ != mtm.degree_);
85    }
86
87    return true;
88}
89
90MultiTypeMath::operator orxonox::Vector2() const
91{
92    return (this->type_ == MT_vector2) ? this->vector2_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Vector2>(*this);
93}
94
95MultiTypeMath::operator orxonox::Vector3() const
96{
97    return (this->type_ == MT_vector3) ? this->vector3_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Vector3>(*this);
98}
99
100MultiTypeMath::operator orxonox::Quaternion() const
101{
102    return (this->type_ == MT_quaternion) ? this->quaternion_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Quaternion>(*this);
103}
104
105MultiTypeMath::operator orxonox::ColourValue() const
106{
107    return (this->type_ == MT_colourvalue) ? this->colourvalue_ : ConvertValueAndReturn<MultiTypeMath, orxonox::ColourValue>(*this);
108}
109
110MultiTypeMath::operator orxonox::Radian() const
111{
112    return (this->type_ == MT_radian) ? this->radian_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Radian>(*this);
113}
114
115MultiTypeMath::operator orxonox::Degree() const
116{
117    return (this->type_ == MT_degree) ? this->degree_ : ConvertValueAndReturn<MultiTypeMath, orxonox::Degree>(*this);
118}
119
120void MultiTypeMath::setValue(const MultiTypeMath& mtm)
121{
122    std::cout << "4_3: setValue in MultiTypeMath with other Multitype as parameter: " << mtm << std::endl;
123
124    MultiTypeString::setValue(mtm);
125    this->vector2_ = mtm.vector2_;
126    this->vector3_ = mtm.vector3_;
127    this->quaternion_ = mtm.quaternion_;
128    this->colourvalue_ = mtm.colourvalue_;
129    this->radian_ = mtm.radian_;
130    this->degree_ = mtm.degree_;
131}
132
133std::string MultiTypeMath::toString() const
134{
135    std::string output;
136
137    if (this->type_ == MT_vector2)
138        ConvertValue(&output, this->vector2_);
139    else if (this->type_ == MT_vector3)
140        ConvertValue(&output, this->vector3_);
141    else if (this->type_ == MT_colourvalue)
142        ConvertValue(&output, this->colourvalue_);
143    else if (this->type_ == MT_quaternion)
144        ConvertValue(&output, this->quaternion_);
145    else if (this->type_ == MT_radian)
146        ConvertValue(&output, this->radian_);
147    else if (this->type_ == MT_degree)
148        ConvertValue(&output, this->degree_);
149    else
150        return MultiTypeString::toString();
151
152    return output;
153}
154
155bool MultiTypeMath::fromString(const std::string value)
156{
157    if (this->type_ == MT_vector2)
158        return ConvertValue(&this->vector2_, value, orxonox::Vector2(0, 0));
159    else if (this->type_ == MT_vector3)
160        return ConvertValue(&this->vector3_, value, orxonox::Vector3(0, 0, 0));
161    else if (this->type_ == MT_colourvalue)
162        return ConvertValue(&this->colourvalue_, value, orxonox::ColourValue(0, 0, 0, 0));
163    else if (this->type_ == MT_quaternion)
164        return ConvertValue(&this->quaternion_, value, orxonox::Quaternion(1, 0, 0, 0));
165    else if (this->type_ == MT_radian)
166        return ConvertValue(&this->radian_, value, orxonox::Radian(0));
167    else if (this->type_ == MT_degree)
168        return ConvertValue(&this->degree_, value, orxonox::Degree(0));
169    else
170        return MultiTypeString::fromString(value);
171}
172
173std::ostream& operator<<(std::ostream& out, MultiTypeMath& mtm)
174{
175    if (mtm.isA(MT_vector2))
176        out << mtm.getVector2();
177    else if (mtm.isA(MT_vector3))
178        out << mtm.getVector3();
179    else if (mtm.isA(MT_colourvalue))
180        out << mtm.getColourValue();
181    else if (mtm.isA(MT_quaternion))
182        out << mtm.getQuaternion();
183    else if (mtm.isA(MT_radian))
184        out << mtm.getRadian();
185    else if (mtm.isA(MT_degree))
186        out << mtm.getDegree();
187    else
188        out << ((MultiTypeString)mtm);
189
190    return out;
191}
Note: See TracBrowser for help on using the repository browser.