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