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