[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 | |
---|
[1625] | 39 | #include <string> |
---|
[2171] | 40 | #include <cmath> |
---|
[1505] | 41 | |
---|
[5789] | 42 | #include <ogremath/OgreMath.h> |
---|
| 43 | #include <ogremath/OgreVector2.h> |
---|
| 44 | #include <ogremath/OgreVector3.h> |
---|
| 45 | #include <ogremath/OgreVector4.h> |
---|
| 46 | #include <ogremath/OgreQuaternion.h> |
---|
| 47 | #include <ogremath/OgreColourValue.h> |
---|
[1505] | 48 | |
---|
[3214] | 49 | // Certain headers might define unwanted macros... |
---|
| 50 | #undef max |
---|
| 51 | #undef min |
---|
| 52 | #undef sgn |
---|
| 53 | #undef clamp |
---|
| 54 | #undef sqrt |
---|
| 55 | #undef square |
---|
| 56 | #undef mod |
---|
| 57 | #undef rnd |
---|
[1781] | 58 | |
---|
[2171] | 59 | namespace orxonox |
---|
[1505] | 60 | { |
---|
[2171] | 61 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian); |
---|
| 62 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian); |
---|
| 63 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree); |
---|
| 64 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree); |
---|
[1505] | 65 | |
---|
[2171] | 66 | /** |
---|
| 67 | @brief Returns the sign of the given value. |
---|
| 68 | @param x The value |
---|
| 69 | @return 1 if the value is positive or zero, -1 if the value is negative |
---|
| 70 | */ |
---|
| 71 | template <typename T> |
---|
| 72 | inline T sgn(T x) |
---|
| 73 | { |
---|
| 74 | return (x >= 0) ? 1 : -1; |
---|
| 75 | } |
---|
[1505] | 76 | |
---|
[2171] | 77 | /** |
---|
| 78 | @brief Keeps a value between a lower and an upper limit. |
---|
| 79 | @param x The value |
---|
| 80 | @param min The lower limit |
---|
| 81 | @param max The upper limit |
---|
| 82 | */ |
---|
| 83 | template <typename T> |
---|
| 84 | inline T clamp(T x, T min, T max) |
---|
| 85 | { |
---|
| 86 | if (x < min) |
---|
| 87 | return min; |
---|
[1505] | 88 | |
---|
[2171] | 89 | if (x > max) |
---|
| 90 | return max; |
---|
[1505] | 91 | |
---|
[2171] | 92 | return x; |
---|
| 93 | } |
---|
[1505] | 94 | |
---|
[2171] | 95 | /** |
---|
| 96 | @brief Returns the square value (x^2). |
---|
| 97 | */ |
---|
| 98 | template <typename T> |
---|
| 99 | inline T square(T x) |
---|
| 100 | { |
---|
| 101 | return x*x; |
---|
| 102 | } |
---|
[1505] | 103 | |
---|
[2171] | 104 | /** |
---|
| 105 | @brief Returns the cube value (x^3). |
---|
| 106 | */ |
---|
| 107 | template <typename T> |
---|
| 108 | inline T cube(T x) |
---|
| 109 | { |
---|
| 110 | return x*x*x; |
---|
| 111 | } |
---|
[1505] | 112 | |
---|
[2171] | 113 | /** |
---|
| 114 | @brief Rounds the value. |
---|
| 115 | */ |
---|
| 116 | template <typename T> |
---|
| 117 | inline int round(T x) |
---|
| 118 | { |
---|
[3300] | 119 | return static_cast<int>(x + 0.5); |
---|
[2171] | 120 | } |
---|
[1505] | 121 | |
---|
[2171] | 122 | /** |
---|
| 123 | @brief The modulo operation, enhanced to work properly with negative values. |
---|
| 124 | @param x The value |
---|
| 125 | @param max The operand |
---|
| 126 | */ |
---|
| 127 | template <typename T> |
---|
| 128 | inline int mod(T x, int max) |
---|
| 129 | { |
---|
| 130 | if (x >= 0) |
---|
| 131 | return (x % max); |
---|
| 132 | else |
---|
| 133 | return ((x % max) + max); |
---|
| 134 | } |
---|
[2087] | 135 | |
---|
[2171] | 136 | template <typename T> |
---|
| 137 | inline T zeroise() |
---|
| 138 | { |
---|
[2662] | 139 | // Default, raise a compiler error without including large boost header cascade. |
---|
| 140 | T temp(); |
---|
| 141 | *********temp; // If you reach this code, you abused zeroise()! |
---|
| 142 | return temp; |
---|
[2171] | 143 | } |
---|
[2087] | 144 | |
---|
[2171] | 145 | template <> inline char zeroise<char>() { return 0; } |
---|
| 146 | template <> inline unsigned char zeroise<unsigned char>() { return 0; } |
---|
| 147 | template <> inline short zeroise<short>() { return 0; } |
---|
| 148 | template <> inline unsigned short zeroise<unsigned short>() { return 0; } |
---|
| 149 | template <> inline int zeroise<int>() { return 0; } |
---|
| 150 | template <> inline unsigned int zeroise<unsigned int>() { return 0; } |
---|
| 151 | template <> inline long zeroise<long>() { return 0; } |
---|
| 152 | template <> inline unsigned long zeroise<unsigned long>() { return 0; } |
---|
| 153 | template <> inline long long zeroise<long long>() { return 0; } |
---|
| 154 | template <> inline unsigned long long zeroise<unsigned long long>() { return 0; } |
---|
| 155 | template <> inline float zeroise<float>() { return 0; } |
---|
| 156 | template <> inline double zeroise<double>() { return 0; } |
---|
| 157 | template <> inline long double zeroise<long double>() { return 0; } |
---|
| 158 | template <> inline bool zeroise<bool>() { return 0; } |
---|
| 159 | template <> inline void* zeroise<void*>() { return 0; } |
---|
| 160 | template <> inline std::string zeroise<std::string>() { return ""; } |
---|
| 161 | template <> inline orxonox::Radian zeroise<orxonox::Radian>() { return orxonox::Radian(0.0f); } |
---|
| 162 | template <> inline orxonox::Degree zeroise<orxonox::Degree>() { return orxonox::Degree(0.0f); } |
---|
| 163 | template <> inline orxonox::Vector2 zeroise<orxonox::Vector2>() { return orxonox::Vector2 (0, 0) ; } |
---|
| 164 | template <> inline orxonox::Vector3 zeroise<orxonox::Vector3>() { return orxonox::Vector3 (0, 0, 0) ; } |
---|
| 165 | template <> inline orxonox::Vector4 zeroise<orxonox::Vector4>() { return orxonox::Vector4 (0, 0, 0, 0); } |
---|
| 166 | template <> inline orxonox::ColourValue zeroise<orxonox::ColourValue>() { return orxonox::ColourValue(0, 0, 0, 0); } |
---|
| 167 | template <> inline orxonox::Quaternion zeroise<orxonox::Quaternion>() { return orxonox::Quaternion (0, 0, 0, 0); } |
---|
[1505] | 168 | |
---|
[3196] | 169 | //! Provides zero value symbols that can be returned as reference |
---|
| 170 | template <typename T> |
---|
| 171 | struct NilValue |
---|
| 172 | { |
---|
| 173 | inline operator const T&() const |
---|
| 174 | { |
---|
| 175 | return value; |
---|
| 176 | } |
---|
| 177 | static T value; |
---|
| 178 | }; |
---|
| 179 | template <typename T> |
---|
| 180 | T NilValue<T>::value = zeroise<T>(); |
---|
| 181 | |
---|
[2171] | 182 | /** |
---|
| 183 | @brief Interpolates between two values for a time between 0 and 1. |
---|
| 184 | @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. |
---|
| 185 | @param start The value at time = 0 |
---|
| 186 | @param end The value at time = 1 |
---|
| 187 | @return The interpolation at a given time |
---|
| 188 | */ |
---|
| 189 | template <typename T> |
---|
[3196] | 190 | inline T interpolate(float time, const T& start, const T& end) |
---|
[2171] | 191 | { |
---|
| 192 | return time * (end - start) + start; |
---|
| 193 | } |
---|
[1505] | 194 | |
---|
[2171] | 195 | /** |
---|
| 196 | @brief Interpolates smoothly between two values for a time between 0 and 1. The function starts slowly, increases faster and stops slowly again. |
---|
| 197 | @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. |
---|
| 198 | @param start The value at time = 0 |
---|
| 199 | @param end The value at time = 1 |
---|
| 200 | @return The smoothed interpolation at a given time |
---|
| 201 | */ |
---|
| 202 | template <typename T> |
---|
[3196] | 203 | inline T interpolateSmooth(float time, const T& start, const T& end) |
---|
[2171] | 204 | { |
---|
| 205 | return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start; |
---|
| 206 | } |
---|
[1505] | 207 | |
---|
[2171] | 208 | /** |
---|
| 209 | @brief Returns a random number between 0 and almost 1: 0 <= rnd < 1. |
---|
| 210 | */ |
---|
| 211 | inline float rnd() |
---|
| 212 | { |
---|
[3196] | 213 | return rand() / (RAND_MAX + 1.0f); |
---|
[2171] | 214 | } |
---|
[1505] | 215 | |
---|
[2171] | 216 | /** |
---|
| 217 | @brief Returns a random number between 0 and almost max: 0 <= rnd < max. |
---|
| 218 | @param max The maximum |
---|
| 219 | */ |
---|
| 220 | inline float rnd(float max) |
---|
| 221 | { |
---|
| 222 | return rnd() * max; |
---|
| 223 | } |
---|
[1505] | 224 | |
---|
[2171] | 225 | /** |
---|
| 226 | @brief Returns a random number between min and almost max: min <= rnd < max. |
---|
| 227 | @param min The minimum |
---|
| 228 | @param max The maximum |
---|
| 229 | */ |
---|
| 230 | inline float rnd(float min, float max) |
---|
| 231 | { |
---|
| 232 | return rnd(max - min) + min; |
---|
| 233 | } |
---|
[1625] | 234 | |
---|
[2872] | 235 | /** |
---|
| 236 | @brief Returns randomly 1 or -1 with equal probability. |
---|
| 237 | */ |
---|
| 238 | inline float rndsgn() |
---|
| 239 | { |
---|
[3196] | 240 | return static_cast<float>((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0 |
---|
[2872] | 241 | } |
---|
| 242 | |
---|
[2171] | 243 | _UtilExport unsigned long getUniqueNumber(); |
---|
[1505] | 244 | |
---|
[2171] | 245 | class IntVector2 |
---|
| 246 | { |
---|
| 247 | public: |
---|
[3196] | 248 | IntVector2() : x(0), y(0) { } |
---|
| 249 | IntVector2(int _x, int _y) : x(_x), y(_y) { } |
---|
| 250 | int x; |
---|
| 251 | int y; |
---|
[2171] | 252 | }; |
---|
[1505] | 253 | |
---|
[2171] | 254 | class IntVector3 |
---|
| 255 | { |
---|
| 256 | public: |
---|
[3196] | 257 | IntVector3() : x(0), y(0), z(0) { } |
---|
| 258 | IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { } |
---|
| 259 | int x; |
---|
| 260 | int y; |
---|
| 261 | int z; |
---|
[2171] | 262 | }; |
---|
| 263 | } |
---|
| 264 | |
---|
[1505] | 265 | #endif /* _Util_Math_H__ */ |
---|