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