| 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 |  *      Reto Grieder | 
|---|
| 25 |  *   Co-authors: | 
|---|
| 26 |  *      ... | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | /** | 
|---|
| 30 |     @file | 
|---|
| 31 |     @brief | 
|---|
| 32 |         Math conversion functions. Definitions are in Math.cc | 
|---|
| 33 | */ | 
|---|
| 34 |  | 
|---|
| 35 | #ifndef _MathConvert_H__ | 
|---|
| 36 | #define _MathConvert_H__ | 
|---|
| 37 |  | 
|---|
| 38 | #include "UtilPrereqs.h" | 
|---|
| 39 | #include "Math.h" | 
|---|
| 40 | #include "Convert.h" | 
|---|
| 41 |  | 
|---|
| 42 | namespace orxonox | 
|---|
| 43 | { | 
|---|
| 44 |     //////////////////// | 
|---|
| 45 |     // Math to string // | 
|---|
| 46 |     //////////////////// | 
|---|
| 47 |  | 
|---|
| 48 |     // Vector2 to std::string | 
|---|
| 49 |     template <> | 
|---|
| 50 |     struct ConverterExplicit<orxonox::Vector2, std::string> | 
|---|
| 51 |     { | 
|---|
| 52 |         FORCEINLINE static bool convert(std::string* output, const orxonox::Vector2& input) | 
|---|
| 53 |         { | 
|---|
| 54 |             std::ostringstream ostream; | 
|---|
| 55 |             if (ostream << input.x << ',' << input.y) | 
|---|
| 56 |             { | 
|---|
| 57 |                 (*output) = ostream.str(); | 
|---|
| 58 |                 return true; | 
|---|
| 59 |             } | 
|---|
| 60 |             return false; | 
|---|
| 61 |         } | 
|---|
| 62 |     }; | 
|---|
| 63 |  | 
|---|
| 64 |     // Vector3 to std::string | 
|---|
| 65 |     template <> | 
|---|
| 66 |     struct ConverterExplicit<orxonox::Vector3, std::string> | 
|---|
| 67 |     { | 
|---|
| 68 |         FORCEINLINE static bool convert(std::string* output, const orxonox::Vector3& input) | 
|---|
| 69 |         { | 
|---|
| 70 |             std::ostringstream ostream; | 
|---|
| 71 |             if (ostream << input.x << ',' << input.y << ',' << input.z) | 
|---|
| 72 |             { | 
|---|
| 73 |                 (*output) = ostream.str(); | 
|---|
| 74 |                 return true; | 
|---|
| 75 |             } | 
|---|
| 76 |             return false; | 
|---|
| 77 |         } | 
|---|
| 78 |     }; | 
|---|
| 79 |  | 
|---|
| 80 |     // Vector4 to std::string | 
|---|
| 81 |     template <> | 
|---|
| 82 |     struct ConverterExplicit<orxonox::Vector4, std::string> | 
|---|
| 83 |     { | 
|---|
| 84 |         FORCEINLINE static bool convert(std::string* output, const orxonox::Vector4& input) | 
|---|
| 85 |         { | 
|---|
| 86 |             std::ostringstream ostream; | 
|---|
| 87 |             if (ostream << input.x << ',' << input.y << ',' << input.z << ',' << input.w) | 
|---|
| 88 |             { | 
|---|
| 89 |                 (*output) = ostream.str(); | 
|---|
| 90 |                 return true; | 
|---|
| 91 |             } | 
|---|
| 92 |             return false; | 
|---|
| 93 |         } | 
|---|
| 94 |     }; | 
|---|
| 95 |  | 
|---|
| 96 |     // Quaternion to std::string | 
|---|
| 97 |     template <> | 
|---|
| 98 |     struct ConverterExplicit<orxonox::Quaternion, std::string> | 
|---|
| 99 |     { | 
|---|
| 100 |         FORCEINLINE static bool convert(std::string* output, const orxonox::Quaternion& input) | 
|---|
| 101 |         { | 
|---|
| 102 |             std::ostringstream ostream; | 
|---|
| 103 |             if (ostream << input.w << ',' << input.x << ',' << input.y << ',' << input.z) | 
|---|
| 104 |             { | 
|---|
| 105 |                 (*output) = ostream.str(); | 
|---|
| 106 |                 return true; | 
|---|
| 107 |             } | 
|---|
| 108 |             return false; | 
|---|
| 109 |         } | 
|---|
| 110 |     }; | 
|---|
| 111 |  | 
|---|
| 112 |     // ColourValue to std::string | 
|---|
| 113 |     template <> | 
|---|
| 114 |     struct ConverterExplicit<orxonox::ColourValue, std::string> | 
|---|
| 115 |     { | 
|---|
| 116 |         FORCEINLINE static bool convert(std::string* output, const orxonox::ColourValue& input) | 
|---|
| 117 |         { | 
|---|
| 118 |             std::ostringstream ostream; | 
|---|
| 119 |             if (ostream << input.r << ',' << input.g << ',' << input.b << ',' << input.a) | 
|---|
| 120 |             { | 
|---|
| 121 |                 (*output) = ostream.str(); | 
|---|
| 122 |                 return true; | 
|---|
| 123 |             } | 
|---|
| 124 |             return false; | 
|---|
| 125 |         } | 
|---|
| 126 |     }; | 
|---|
| 127 |  | 
|---|
| 128 |  | 
|---|
| 129 |     //////////////////// | 
|---|
| 130 |     // string to Math // | 
|---|
| 131 |     //////////////////// | 
|---|
| 132 |  | 
|---|
| 133 |     // std::string to Vector2 | 
|---|
| 134 |     template <> struct _UtilExport ConverterFallback<std::string, orxonox::Vector2> | 
|---|
| 135 |     { static bool convert(orxonox::Vector2*     output, const std::string& input); }; | 
|---|
| 136 |     // std::string to Vector3 | 
|---|
| 137 |     template <> struct _UtilExport ConverterFallback<std::string, orxonox::Vector3> | 
|---|
| 138 |     { static bool convert(orxonox::Vector3*     output, const std::string& input); }; | 
|---|
| 139 |     // std::string to Vector4 | 
|---|
| 140 |     template <> struct _UtilExport ConverterFallback<std::string, orxonox::Vector4> | 
|---|
| 141 |     { static bool convert(orxonox::Vector4*     output, const std::string& input); }; | 
|---|
| 142 |     // std::string to Quaternion | 
|---|
| 143 |     template <> struct _UtilExport ConverterFallback<std::string, orxonox::Quaternion> | 
|---|
| 144 |     { static bool convert(orxonox::Quaternion*  output, const std::string& input); }; | 
|---|
| 145 |     // std::string to ColourValue | 
|---|
| 146 |     template <> struct _UtilExport ConverterFallback<std::string, orxonox::ColourValue> | 
|---|
| 147 |     { static bool convert(orxonox::ColourValue* output, const std::string& input); }; | 
|---|
| 148 |  | 
|---|
| 149 |  | 
|---|
| 150 |     /////////////////////////////// | 
|---|
| 151 |     // From and to Radian/Degree // | 
|---|
| 152 |     /////////////////////////////// | 
|---|
| 153 |  | 
|---|
| 154 |     // From Radian | 
|---|
| 155 |     template <class ToType> | 
|---|
| 156 |     struct ConverterFallback<orxonox::Radian, ToType> | 
|---|
| 157 |     { | 
|---|
| 158 |         FORCEINLINE static bool convert(ToType* output, const orxonox::Radian& input) | 
|---|
| 159 |         { | 
|---|
| 160 |             return convertValue<Ogre::Real, ToType>(output, input.valueRadians()); | 
|---|
| 161 |         } | 
|---|
| 162 |     }; | 
|---|
| 163 |  | 
|---|
| 164 |     // From Degree | 
|---|
| 165 |     template <class ToType> | 
|---|
| 166 |     struct ConverterFallback<orxonox::Degree, ToType> | 
|---|
| 167 |     { | 
|---|
| 168 |         FORCEINLINE static bool convert(ToType* output, const orxonox::Degree& input) | 
|---|
| 169 |         { | 
|---|
| 170 |             return convertValue<Ogre::Real, ToType>(output, input.valueDegrees()); | 
|---|
| 171 |         } | 
|---|
| 172 |     }; | 
|---|
| 173 |  | 
|---|
| 174 |     // To Radian | 
|---|
| 175 |     template <class FromType> | 
|---|
| 176 |     struct ConverterFallback<FromType, orxonox::Radian> | 
|---|
| 177 |     { | 
|---|
| 178 |         FORCEINLINE static bool convert(orxonox::Radian* output, const FromType& input) | 
|---|
| 179 |         { | 
|---|
| 180 |             float temp; | 
|---|
| 181 |             if (convertValue(&temp, input)) | 
|---|
| 182 |             { | 
|---|
| 183 |                 *output = temp; | 
|---|
| 184 |                 return true; | 
|---|
| 185 |             } | 
|---|
| 186 |             else | 
|---|
| 187 |                 return false; | 
|---|
| 188 |         } | 
|---|
| 189 |     }; | 
|---|
| 190 |  | 
|---|
| 191 |     // To Degree | 
|---|
| 192 |     template <class FromType> | 
|---|
| 193 |     struct ConverterFallback<FromType, orxonox::Degree> | 
|---|
| 194 |     { | 
|---|
| 195 |         FORCEINLINE static bool convert(orxonox::Degree* output, const FromType& input) | 
|---|
| 196 |         { | 
|---|
| 197 |             float temp; | 
|---|
| 198 |             if (convertValue(&temp, input)) | 
|---|
| 199 |             { | 
|---|
| 200 |                 *output = temp; | 
|---|
| 201 |                 return true; | 
|---|
| 202 |             } | 
|---|
| 203 |             else | 
|---|
| 204 |                 return false; | 
|---|
| 205 |         } | 
|---|
| 206 |     }; | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | #endif /* _MathConvert_H__ */ | 
|---|