| 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 |  | 
|---|
| 33 | MultiTypeMath::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 |  | 
|---|
| 51 | bool 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 |  | 
|---|
| 74 | bool MultiTypeMath::operator==(const MultiTypeString& mts) const | 
|---|
| 75 | { | 
|---|
| 76 | return MultiTypeString::operator==(mts); | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | bool MultiTypeMath::operator==(const MultiTypePrimitive& mtp) const | 
|---|
| 80 | { | 
|---|
| 81 | return MultiTypePrimitive::operator==(mtp); | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | bool MultiTypeMath::operator!=(const MultiTypeMath& mtm) const | 
|---|
| 85 | { | 
|---|
| 86 | if (MultiTypeString::operator==(mtm) && this->type_ == mtm.type_) | 
|---|
| 87 | { | 
|---|
| 88 | if (this->type_ == MT_vector2) | 
|---|
| 89 | return (this->vector2_ != mtm.vector2_); | 
|---|
| 90 | else if (this->type_ == MT_vector3) | 
|---|
| 91 | return (this->vector3_ != mtm.vector3_); | 
|---|
| 92 | else if (this->type_ == MT_vector4) | 
|---|
| 93 | return (this->vector4_ != mtm.vector4_); | 
|---|
| 94 | else if (this->type_ == MT_colourvalue) | 
|---|
| 95 | return (this->colourvalue_ != mtm.colourvalue_); | 
|---|
| 96 | else if (this->type_ == MT_quaternion) | 
|---|
| 97 | return (this->quaternion_ != mtm.quaternion_); | 
|---|
| 98 | else if (this->type_ == MT_radian) | 
|---|
| 99 | return (this->radian_ != mtm.radian_); | 
|---|
| 100 | else if (this->type_ == MT_degree) | 
|---|
| 101 | return (this->degree_ != mtm.degree_); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | return true; | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | bool MultiTypeMath::operator!=(const MultiTypeString& mts) const | 
|---|
| 108 | { | 
|---|
| 109 | return MultiTypeString::operator!=(mts); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | bool MultiTypeMath::operator!=(const MultiTypePrimitive& mtp) const | 
|---|
| 113 | { | 
|---|
| 114 | return MultiTypePrimitive::operator!=(mtp); | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | MultiTypeMath::operator void*() const | 
|---|
| 118 | { return (this->type_ == MT_void) ? this->value_.void_ : getConvertedValue<MultiTypeMath, void*>(*this, 0); } | 
|---|
| 119 | MultiTypeMath::operator int() const | 
|---|
| 120 | { return (this->type_ == MT_int) ? this->value_.int_ : getConvertedValue<MultiTypeMath, int>(*this, 0); } | 
|---|
| 121 | MultiTypeMath::operator unsigned int() const | 
|---|
| 122 | { return (this->type_ == MT_uint) ? this->value_.uint_ : getConvertedValue<MultiTypeMath, unsigned int>(*this, 0); } | 
|---|
| 123 | MultiTypeMath::operator char() const | 
|---|
| 124 | { return (this->type_ == MT_char) ? this->value_.char_ : getConvertedValue<MultiTypeMath, char>(*this, 0); } | 
|---|
| 125 | MultiTypeMath::operator unsigned char() const | 
|---|
| 126 | { return (this->type_ == MT_uchar) ? this->value_.uchar_ : getConvertedValue<MultiTypeMath, unsigned char>(*this, 0); } | 
|---|
| 127 | MultiTypeMath::operator short() const | 
|---|
| 128 | { return (this->type_ == MT_short) ? this->value_.short_ : getConvertedValue<MultiTypeMath, short>(*this, 0); } | 
|---|
| 129 | MultiTypeMath::operator unsigned short() const | 
|---|
| 130 | { return (this->type_ == MT_ushort) ? this->value_.ushort_ : getConvertedValue<MultiTypeMath, unsigned short>(*this, 0); } | 
|---|
| 131 | MultiTypeMath::operator long() const | 
|---|
| 132 | { return (this->type_ == MT_long) ? this->value_.long_ : getConvertedValue<MultiTypeMath, long>(*this, 0); } | 
|---|
| 133 | MultiTypeMath::operator unsigned long() const | 
|---|
| 134 | { return (this->type_ == MT_ulong) ? this->value_.ulong_ : getConvertedValue<MultiTypeMath, unsigned long>(*this, 0); } | 
|---|
| 135 | MultiTypeMath::operator float() const | 
|---|
| 136 | { return (this->type_ == MT_float) ? this->value_.float_ : getConvertedValue<MultiTypeMath, float>(*this, 0); } | 
|---|
| 137 | MultiTypeMath::operator double() const | 
|---|
| 138 | { return (this->type_ == MT_double) ? this->value_.double_ : getConvertedValue<MultiTypeMath, double>(*this, 0); } | 
|---|
| 139 | MultiTypeMath::operator long double() const | 
|---|
| 140 | { return (this->type_ == MT_longdouble) ? this->value_.longdouble_ : getConvertedValue<MultiTypeMath, long double>(*this, 0); } | 
|---|
| 141 | MultiTypeMath::operator bool() const | 
|---|
| 142 | { return (this->type_ == MT_bool) ? this->value_.bool_ : getConvertedValue<MultiTypeMath, bool>(*this, 0); } | 
|---|
| 143 | MultiTypeMath::operator std::string() const | 
|---|
| 144 | { return (this->type_ == MT_string) ? this->string_ : getConvertedValue<MultiTypeMath, std::string>(*this); } | 
|---|
| 145 | MultiTypeMath::operator const char*() const | 
|---|
| 146 | { return ((this->type_ == MT_constchar) ? this->string_ : getConvertedValue<MultiTypeMath, std::string>(*this)).c_str(); } | 
|---|
| 147 | MultiTypeMath::operator orxonox::Vector2() const | 
|---|
| 148 | { return (this->type_ == MT_vector2) ? this->vector2_ : getConvertedValue<MultiTypeMath, orxonox::Vector2>(*this); } | 
|---|
| 149 | MultiTypeMath::operator orxonox::Vector3() const | 
|---|
| 150 | { return (this->type_ == MT_vector3) ? this->vector3_ : getConvertedValue<MultiTypeMath, orxonox::Vector3>(*this); } | 
|---|
| 151 | MultiTypeMath::operator orxonox::Vector4() const | 
|---|
| 152 | { return (this->type_ == MT_vector4) ? this->vector4_ : getConvertedValue<MultiTypeMath, orxonox::Vector4>(*this); } | 
|---|
| 153 | MultiTypeMath::operator orxonox::Quaternion() const | 
|---|
| 154 | { return (this->type_ == MT_quaternion) ? this->quaternion_ : getConvertedValue<MultiTypeMath, orxonox::Quaternion>(*this); } | 
|---|
| 155 | MultiTypeMath::operator orxonox::ColourValue() const | 
|---|
| 156 | { return (this->type_ == MT_colourvalue) ? this->colourvalue_ : getConvertedValue<MultiTypeMath, orxonox::ColourValue>(*this); } | 
|---|
| 157 | MultiTypeMath::operator orxonox::Radian() const | 
|---|
| 158 | { return (this->type_ == MT_radian) ? this->radian_ : getConvertedValue<MultiTypeMath, orxonox::Radian>(*this); } | 
|---|
| 159 | MultiTypeMath::operator orxonox::Degree() const | 
|---|
| 160 | { return (this->type_ == MT_degree) ? this->degree_ : getConvertedValue<MultiTypeMath, orxonox::Degree>(*this); } | 
|---|
| 161 |  | 
|---|
| 162 | void MultiTypeMath::setValue(const MultiTypeMath& mtm) | 
|---|
| 163 | { | 
|---|
| 164 | MultiTypeString::setValue(mtm); | 
|---|
| 165 | this->vector2_ = mtm.vector2_; | 
|---|
| 166 | this->vector3_ = mtm.vector3_; | 
|---|
| 167 | this->vector4_ = mtm.vector4_; | 
|---|
| 168 | this->quaternion_ = mtm.quaternion_; | 
|---|
| 169 | this->colourvalue_ = mtm.colourvalue_; | 
|---|
| 170 | this->radian_ = mtm.radian_; | 
|---|
| 171 | this->degree_ = mtm.degree_; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | void MultiTypeMath::setValue(const MultiTypeString& mts) | 
|---|
| 175 | { | 
|---|
| 176 | MultiTypeString::setValue(mts); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | void MultiTypeMath::setValue(const MultiTypePrimitive& mtp) | 
|---|
| 180 | { | 
|---|
| 181 | MultiTypePrimitive::setValue(mtp); | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | std::string MultiTypeMath::getTypename() const | 
|---|
| 185 | { | 
|---|
| 186 | if (this->type_ == MT_vector2) | 
|---|
| 187 | return "Vector2"; | 
|---|
| 188 | else if (this->type_ == MT_vector3) | 
|---|
| 189 | return "Vector3"; | 
|---|
| 190 | else if (this->type_ == MT_vector4) | 
|---|
| 191 | return "Vector4"; | 
|---|
| 192 | else if (this->type_ == MT_colourvalue) | 
|---|
| 193 | return "ColourValue"; | 
|---|
| 194 | else if (this->type_ == MT_quaternion) | 
|---|
| 195 | return "Quaternion"; | 
|---|
| 196 | else if (this->type_ == MT_radian) | 
|---|
| 197 | return "Radian"; | 
|---|
| 198 | else if (this->type_ == MT_degree) | 
|---|
| 199 | return "Degree"; | 
|---|
| 200 | else | 
|---|
| 201 | return MultiTypeString::getTypename(); | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 | std::string MultiTypeMath::toString() const | 
|---|
| 205 | { | 
|---|
| 206 | std::string output; | 
|---|
| 207 |  | 
|---|
| 208 | if (this->type_ == MT_vector2) | 
|---|
| 209 | ConvertValue(&output, this->vector2_); | 
|---|
| 210 | else if (this->type_ == MT_vector3) | 
|---|
| 211 | ConvertValue(&output, this->vector3_); | 
|---|
| 212 | else if (this->type_ == MT_vector4) | 
|---|
| 213 | ConvertValue(&output, this->vector4_); | 
|---|
| 214 | else if (this->type_ == MT_colourvalue) | 
|---|
| 215 | ConvertValue(&output, this->colourvalue_); | 
|---|
| 216 | else if (this->type_ == MT_quaternion) | 
|---|
| 217 | ConvertValue(&output, this->quaternion_); | 
|---|
| 218 | else if (this->type_ == MT_radian) | 
|---|
| 219 | ConvertValue(&output, this->radian_); | 
|---|
| 220 | else if (this->type_ == MT_degree) | 
|---|
| 221 | ConvertValue(&output, this->degree_); | 
|---|
| 222 | else | 
|---|
| 223 | return MultiTypeString::toString(); | 
|---|
| 224 |  | 
|---|
| 225 | return output; | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 | bool MultiTypeMath::fromString(const std::string value) | 
|---|
| 229 | { | 
|---|
| 230 | if (this->type_ == MT_vector2) | 
|---|
| 231 | return ConvertValue(&this->vector2_, value, orxonox::Vector2(0, 0)); | 
|---|
| 232 | else if (this->type_ == MT_vector3) | 
|---|
| 233 | return ConvertValue(&this->vector3_, value, orxonox::Vector3(0, 0, 0)); | 
|---|
| 234 | else if (this->type_ == MT_vector4) | 
|---|
| 235 | return ConvertValue(&this->vector4_, value, orxonox::Vector4(0, 0, 0, 0)); | 
|---|
| 236 | else if (this->type_ == MT_colourvalue) | 
|---|
| 237 | return ConvertValue(&this->colourvalue_, value, orxonox::ColourValue(0, 0, 0, 0)); | 
|---|
| 238 | else if (this->type_ == MT_quaternion) | 
|---|
| 239 | return ConvertValue(&this->quaternion_, value, orxonox::Quaternion(1, 0, 0, 0)); | 
|---|
| 240 | else if (this->type_ == MT_radian) | 
|---|
| 241 | return ConvertValue(&this->radian_, value, orxonox::Radian(0)); | 
|---|
| 242 | else if (this->type_ == MT_degree) | 
|---|
| 243 | return ConvertValue(&this->degree_, value, orxonox::Degree(0)); | 
|---|
| 244 | else | 
|---|
| 245 | return MultiTypeString::fromString(value); | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | bool MultiTypeMath::assimilate(const MultiTypeMath& mtm, const MultiTypeMath& defvalue) | 
|---|
| 249 | { | 
|---|
| 250 | if (this->type_ == MT_void) | 
|---|
| 251 | return ConvertValue(&this->value_.void_, mtm, defvalue.value_.void_); | 
|---|
| 252 | else if (this->type_ == MT_int) | 
|---|
| 253 | return ConvertValue(&this->value_.int_, mtm, defvalue.value_.int_); | 
|---|
| 254 | else if (this->type_ == MT_uint) | 
|---|
| 255 | return ConvertValue(&this->value_.uint_, mtm, defvalue.value_.uint_); | 
|---|
| 256 | else if (this->type_ == MT_char) | 
|---|
| 257 | return ConvertValue(&this->value_.char_, mtm, defvalue.value_.char_); | 
|---|
| 258 | else if (this->type_ == MT_uchar) | 
|---|
| 259 | return ConvertValue(&this->value_.uchar_, mtm, defvalue.value_.uchar_); | 
|---|
| 260 | else if (this->type_ == MT_short) | 
|---|
| 261 | return ConvertValue(&this->value_.short_, mtm, defvalue.value_.short_); | 
|---|
| 262 | else if (this->type_ == MT_ushort) | 
|---|
| 263 | return ConvertValue(&this->value_.ushort_, mtm, defvalue.value_.ushort_); | 
|---|
| 264 | else if (this->type_ == MT_long) | 
|---|
| 265 | return ConvertValue(&this->value_.long_, mtm, defvalue.value_.long_); | 
|---|
| 266 | else if (this->type_ == MT_ulong) | 
|---|
| 267 | return ConvertValue(&this->value_.ulong_, mtm, defvalue.value_.ulong_); | 
|---|
| 268 | else if (this->type_ == MT_float) | 
|---|
| 269 | return ConvertValue(&this->value_.float_, mtm, defvalue.value_.float_); | 
|---|
| 270 | else if (this->type_ == MT_double) | 
|---|
| 271 | return ConvertValue(&this->value_.double_, mtm, defvalue.value_.double_); | 
|---|
| 272 | else if (this->type_ == MT_longdouble) | 
|---|
| 273 | return ConvertValue(&this->value_.longdouble_, mtm, defvalue.value_.longdouble_); | 
|---|
| 274 | else if (this->type_ == MT_bool) | 
|---|
| 275 | return ConvertValue(&this->value_.bool_, mtm, defvalue.value_.bool_); | 
|---|
| 276 | else if (this->type_ == MT_constchar) | 
|---|
| 277 | return ConvertValue(&this->string_, mtm, defvalue.string_); | 
|---|
| 278 | else if (this->type_ == MT_string) | 
|---|
| 279 | return ConvertValue(&this->string_, mtm, defvalue.string_); | 
|---|
| 280 | else if (this->type_ == MT_vector2) | 
|---|
| 281 | return ConvertValue(&this->vector2_, mtm, defvalue.vector2_); | 
|---|
| 282 | else if (this->type_ == MT_vector3) | 
|---|
| 283 | return ConvertValue(&this->vector3_, mtm, defvalue.vector3_); | 
|---|
| 284 | else if (this->type_ == MT_vector4) | 
|---|
| 285 | return ConvertValue(&this->vector4_, mtm, defvalue.vector4_); | 
|---|
| 286 | else if (this->type_ == MT_colourvalue) | 
|---|
| 287 | return ConvertValue(&this->colourvalue_, mtm, defvalue.colourvalue_); | 
|---|
| 288 | else if (this->type_ == MT_quaternion) | 
|---|
| 289 | return ConvertValue(&this->quaternion_, mtm, defvalue.quaternion_); | 
|---|
| 290 | else if (this->type_ == MT_radian) | 
|---|
| 291 | return ConvertValue(&this->radian_, mtm, defvalue.radian_); | 
|---|
| 292 | else if (this->type_ == MT_degree) | 
|---|
| 293 | return ConvertValue(&this->degree_, mtm, defvalue.degree_); | 
|---|
| 294 | else | 
|---|
| 295 | return false; | 
|---|
| 296 | } | 
|---|
| 297 |  | 
|---|
| 298 | std::ostream& operator<<(std::ostream& out, MultiTypeMath& mtm) | 
|---|
| 299 | { | 
|---|
| 300 | out << mtm.toString(); | 
|---|
| 301 | return out; | 
|---|
| 302 | } | 
|---|