[1505] | 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 | */ |
---|
| 28 | |
---|
[1791] | 29 | /** |
---|
[2171] | 30 | @file |
---|
[1791] | 31 | @brief Declaration and implementation of several math-functions, typedefs of some Ogre::Math classes to the orxonox namespace. |
---|
| 32 | */ |
---|
| 33 | |
---|
[1505] | 34 | #ifndef _Util_Math_H__ |
---|
| 35 | #define _Util_Math_H__ |
---|
| 36 | |
---|
| 37 | #include "UtilPrereqs.h" |
---|
| 38 | |
---|
| 39 | #include <ostream> |
---|
[1625] | 40 | #include <string> |
---|
[2171] | 41 | #include <cmath> |
---|
[2087] | 42 | #include <boost/static_assert.hpp> |
---|
[1505] | 43 | |
---|
| 44 | #include <OgreMath.h> |
---|
| 45 | #include <OgreVector2.h> |
---|
| 46 | #include <OgreVector3.h> |
---|
| 47 | #include <OgreVector4.h> |
---|
| 48 | #include <OgreMatrix3.h> |
---|
[1625] | 49 | #include <OgreMatrix4.h> |
---|
[1505] | 50 | #include <OgreQuaternion.h> |
---|
| 51 | #include <OgreColourValue.h> |
---|
| 52 | |
---|
[1781] | 53 | //Get around Windows hackery |
---|
[1791] | 54 | #if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32 |
---|
[1781] | 55 | # ifdef max |
---|
| 56 | # undef max |
---|
| 57 | # endif |
---|
| 58 | # ifdef min |
---|
| 59 | # undef min |
---|
| 60 | # endif |
---|
| 61 | #endif |
---|
| 62 | |
---|
[2171] | 63 | namespace orxonox |
---|
[1505] | 64 | { |
---|
[2442] | 65 | using Ogre::Radian; |
---|
| 66 | using Ogre::Degree; |
---|
| 67 | using Ogre::Vector2; |
---|
| 68 | using Ogre::Vector3; |
---|
| 69 | using Ogre::Vector4; |
---|
| 70 | using Ogre::Matrix3; |
---|
| 71 | using Ogre::Matrix4; |
---|
| 72 | using Ogre::Quaternion; |
---|
| 73 | using Ogre::ColourValue; |
---|
[1505] | 74 | |
---|
[2442] | 75 | // Also define our own transform space enum |
---|
| 76 | namespace TransformSpace |
---|
| 77 | { |
---|
| 78 | /** |
---|
| 79 | @brief |
---|
| 80 | Enumeration denoting the spaces which a transform can be relative to. |
---|
| 81 | */ |
---|
| 82 | enum Space |
---|
| 83 | { |
---|
| 84 | /// Transform is relative to the local space |
---|
| 85 | Local, |
---|
| 86 | /// Transform is relative to the space of the parent node |
---|
| 87 | Parent, |
---|
| 88 | /// Transform is relative to world space |
---|
| 89 | World |
---|
| 90 | }; |
---|
| 91 | } |
---|
| 92 | |
---|
[2171] | 93 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian); |
---|
| 94 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian); |
---|
| 95 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree); |
---|
| 96 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree); |
---|
[1505] | 97 | |
---|
[2171] | 98 | _UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition); |
---|
| 99 | _UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
| 100 | _UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
| 101 | _UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity); |
---|
[1505] | 102 | |
---|
[2171] | 103 | /** |
---|
| 104 | @brief Returns the sign of the given value. |
---|
| 105 | @param x The value |
---|
| 106 | @return 1 if the value is positive or zero, -1 if the value is negative |
---|
| 107 | */ |
---|
| 108 | template <typename T> |
---|
| 109 | inline T sgn(T x) |
---|
| 110 | { |
---|
| 111 | return (x >= 0) ? 1 : -1; |
---|
| 112 | } |
---|
[1505] | 113 | |
---|
[2171] | 114 | /** |
---|
| 115 | @brief Returns the smaller of two values. |
---|
| 116 | */ |
---|
| 117 | template <typename T> |
---|
| 118 | inline T min(T a, T b) |
---|
| 119 | { |
---|
| 120 | return (a <= b) ? a : b; |
---|
| 121 | } |
---|
[1505] | 122 | |
---|
[2171] | 123 | /** |
---|
| 124 | @brief Returns the greater of two values. |
---|
| 125 | */ |
---|
| 126 | template <typename T> |
---|
| 127 | inline T max(T a, T b) |
---|
| 128 | { |
---|
| 129 | return (a >= b) ? a : b; |
---|
| 130 | } |
---|
[1505] | 131 | |
---|
[2171] | 132 | /** |
---|
| 133 | @brief Keeps a value between a lower and an upper limit. |
---|
| 134 | @param x The value |
---|
| 135 | @param min The lower limit |
---|
| 136 | @param max The upper limit |
---|
| 137 | */ |
---|
| 138 | template <typename T> |
---|
| 139 | inline T clamp(T x, T min, T max) |
---|
| 140 | { |
---|
| 141 | if (x < min) |
---|
| 142 | return min; |
---|
[1505] | 143 | |
---|
[2171] | 144 | if (x > max) |
---|
| 145 | return max; |
---|
[1505] | 146 | |
---|
[2171] | 147 | return x; |
---|
| 148 | } |
---|
[1505] | 149 | |
---|
[2171] | 150 | /** |
---|
| 151 | @brief Returns the square value (x^2). |
---|
| 152 | */ |
---|
| 153 | template <typename T> |
---|
| 154 | inline T square(T x) |
---|
| 155 | { |
---|
| 156 | return x*x; |
---|
| 157 | } |
---|
[1505] | 158 | |
---|
[2171] | 159 | /** |
---|
| 160 | @brief Returns the cube value (x^3). |
---|
| 161 | */ |
---|
| 162 | template <typename T> |
---|
| 163 | inline T cube(T x) |
---|
| 164 | { |
---|
| 165 | return x*x*x; |
---|
| 166 | } |
---|
[1505] | 167 | |
---|
[2171] | 168 | /** |
---|
| 169 | @brief Rounds the value. |
---|
| 170 | */ |
---|
| 171 | template <typename T> |
---|
| 172 | inline int round(T x) |
---|
| 173 | { |
---|
| 174 | return (int)(x + 0.5); |
---|
| 175 | } |
---|
[1505] | 176 | |
---|
[2171] | 177 | /** |
---|
| 178 | @brief The modulo operation, enhanced to work properly with negative values. |
---|
| 179 | @param x The value |
---|
| 180 | @param max The operand |
---|
| 181 | */ |
---|
| 182 | template <typename T> |
---|
| 183 | inline int mod(T x, int max) |
---|
| 184 | { |
---|
| 185 | if (x >= 0) |
---|
| 186 | return (x % max); |
---|
| 187 | else |
---|
| 188 | return ((x % max) + max); |
---|
| 189 | } |
---|
[2087] | 190 | |
---|
[2171] | 191 | template <typename T> |
---|
| 192 | inline T zeroise() |
---|
| 193 | { |
---|
[2442] | 194 | // Default, raise a compiler error without including large boost header cascade. |
---|
| 195 | T temp(); |
---|
| 196 | *********temp; // If you reach this code, you abused zeroise()! |
---|
| 197 | return temp; |
---|
[2171] | 198 | } |
---|
[2087] | 199 | |
---|
[2171] | 200 | template <> inline char zeroise<char>() { return 0; } |
---|
| 201 | template <> inline unsigned char zeroise<unsigned char>() { return 0; } |
---|
| 202 | template <> inline short zeroise<short>() { return 0; } |
---|
| 203 | template <> inline unsigned short zeroise<unsigned short>() { return 0; } |
---|
| 204 | template <> inline int zeroise<int>() { return 0; } |
---|
| 205 | template <> inline unsigned int zeroise<unsigned int>() { return 0; } |
---|
| 206 | template <> inline long zeroise<long>() { return 0; } |
---|
| 207 | template <> inline unsigned long zeroise<unsigned long>() { return 0; } |
---|
| 208 | template <> inline long long zeroise<long long>() { return 0; } |
---|
| 209 | template <> inline unsigned long long zeroise<unsigned long long>() { return 0; } |
---|
| 210 | template <> inline float zeroise<float>() { return 0; } |
---|
| 211 | template <> inline double zeroise<double>() { return 0; } |
---|
| 212 | template <> inline long double zeroise<long double>() { return 0; } |
---|
| 213 | template <> inline bool zeroise<bool>() { return 0; } |
---|
| 214 | template <> inline void* zeroise<void*>() { return 0; } |
---|
| 215 | template <> inline std::string zeroise<std::string>() { return ""; } |
---|
| 216 | template <> inline orxonox::Radian zeroise<orxonox::Radian>() { return orxonox::Radian(0.0f); } |
---|
| 217 | template <> inline orxonox::Degree zeroise<orxonox::Degree>() { return orxonox::Degree(0.0f); } |
---|
| 218 | template <> inline orxonox::Vector2 zeroise<orxonox::Vector2>() { return orxonox::Vector2 (0, 0) ; } |
---|
| 219 | template <> inline orxonox::Vector3 zeroise<orxonox::Vector3>() { return orxonox::Vector3 (0, 0, 0) ; } |
---|
| 220 | template <> inline orxonox::Vector4 zeroise<orxonox::Vector4>() { return orxonox::Vector4 (0, 0, 0, 0); } |
---|
| 221 | template <> inline orxonox::ColourValue zeroise<orxonox::ColourValue>() { return orxonox::ColourValue(0, 0, 0, 0); } |
---|
| 222 | template <> inline orxonox::Quaternion zeroise<orxonox::Quaternion>() { return orxonox::Quaternion (0, 0, 0, 0); } |
---|
[1505] | 223 | |
---|
[2171] | 224 | /** |
---|
| 225 | @brief Interpolates between two values for a time between 0 and 1. |
---|
| 226 | @param time The time is a value between 0 and 1 - the function returns start if time is 0 and end if time is 1 and interpolates if time is between 0 and 1. |
---|
| 227 | @param start The value at time = 0 |
---|
| 228 | @param end The value at time = 1 |
---|
| 229 | @return The interpolation at a given time |
---|
| 230 | */ |
---|
| 231 | template <typename T> |
---|
| 232 | T interpolate(float time, const T& start, const T& end) |
---|
| 233 | { |
---|
| 234 | return time * (end - start) + start; |
---|
| 235 | } |
---|
[1505] | 236 | |
---|
[2171] | 237 | /** |
---|
| 238 | @brief Interpolates smoothly between two values for a time between 0 and 1. The function starts slowly, increases faster and stops slowly again. |
---|
| 239 | @param time The time is a value between 0 and 1 - the function returns start if time is 0 and end if time is 1 and interpolates if time is between 0 and 1. |
---|
| 240 | @param start The value at time = 0 |
---|
| 241 | @param end The value at time = 1 |
---|
| 242 | @return The smoothed interpolation at a given time |
---|
| 243 | */ |
---|
| 244 | template <typename T> |
---|
| 245 | T interpolateSmooth(float time, const T& start, const T& end) |
---|
| 246 | { |
---|
| 247 | return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start; |
---|
| 248 | } |
---|
[1505] | 249 | |
---|
[2171] | 250 | /** |
---|
| 251 | @brief Returns a random number between 0 and almost 1: 0 <= rnd < 1. |
---|
| 252 | */ |
---|
| 253 | inline float rnd() |
---|
| 254 | { |
---|
| 255 | return rand() / (RAND_MAX + 1.0); |
---|
| 256 | } |
---|
[1505] | 257 | |
---|
[2171] | 258 | /** |
---|
| 259 | @brief Returns a random number between 0 and almost max: 0 <= rnd < max. |
---|
| 260 | @param max The maximum |
---|
| 261 | */ |
---|
| 262 | inline float rnd(float max) |
---|
| 263 | { |
---|
| 264 | return rnd() * max; |
---|
| 265 | } |
---|
[1505] | 266 | |
---|
[2171] | 267 | /** |
---|
| 268 | @brief Returns a random number between min and almost max: min <= rnd < max. |
---|
| 269 | @param min The minimum |
---|
| 270 | @param max The maximum |
---|
| 271 | */ |
---|
| 272 | inline float rnd(float min, float max) |
---|
| 273 | { |
---|
| 274 | return rnd(max - min) + min; |
---|
| 275 | } |
---|
[1625] | 276 | |
---|
[2171] | 277 | _UtilExport unsigned long getUniqueNumber(); |
---|
[1505] | 278 | |
---|
[2171] | 279 | class IntVector2 |
---|
| 280 | { |
---|
| 281 | public: |
---|
| 282 | IntVector2() : x(0), y(0) { } |
---|
| 283 | IntVector2(int _x, int _y) : x(_x), y(_y) { } |
---|
| 284 | int x; |
---|
| 285 | int y; |
---|
| 286 | }; |
---|
[1505] | 287 | |
---|
[2171] | 288 | class IntVector3 |
---|
| 289 | { |
---|
| 290 | public: |
---|
| 291 | IntVector3() : x(0), y(0), z(0) { } |
---|
| 292 | IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { } |
---|
| 293 | int x; |
---|
| 294 | int y; |
---|
| 295 | int z; |
---|
| 296 | }; |
---|
| 297 | } |
---|
| 298 | |
---|
[1505] | 299 | #endif /* _Util_Math_H__ */ |
---|