[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 | /** |
---|
[2114] | 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> |
---|
[2002] | 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 | |
---|
[1781] | 52 | //Get around Windows hackery |
---|
[1791] | 53 | #if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32 |
---|
[1781] | 54 | # ifdef max |
---|
| 55 | # undef max |
---|
| 56 | # endif |
---|
| 57 | # ifdef min |
---|
| 58 | # undef min |
---|
| 59 | # endif |
---|
| 60 | #endif |
---|
| 61 | |
---|
[2111] | 62 | namespace orxonox |
---|
[1505] | 63 | { |
---|
[2111] | 64 | typedef Ogre::Radian Radian; |
---|
| 65 | typedef Ogre::Degree Degree; |
---|
| 66 | typedef Ogre::Vector2 Vector2; |
---|
| 67 | typedef Ogre::Vector3 Vector3; |
---|
| 68 | typedef Ogre::Vector4 Vector4; |
---|
| 69 | typedef Ogre::Matrix3 Matrix3; |
---|
| 70 | typedef Ogre::Matrix4 Matrix4; |
---|
| 71 | typedef Ogre::Quaternion Quaternion; |
---|
| 72 | typedef Ogre::ColourValue ColourValue; |
---|
[1505] | 73 | |
---|
[2111] | 74 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian); |
---|
| 75 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian); |
---|
| 76 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree); |
---|
| 77 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree); |
---|
[1505] | 78 | |
---|
[2111] | 79 | _UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition); |
---|
| 80 | _UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
| 81 | _UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
| 82 | _UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity); |
---|
[1505] | 83 | |
---|
[2111] | 84 | /** |
---|
| 85 | @brief Returns the sign of the given value. |
---|
| 86 | @param x The value |
---|
| 87 | @return 1 if the value is positive or zero, -1 if the value is negative |
---|
| 88 | */ |
---|
| 89 | template <typename T> |
---|
| 90 | inline T sgn(T x) |
---|
| 91 | { |
---|
| 92 | return (x >= 0) ? 1 : -1; |
---|
| 93 | } |
---|
[1505] | 94 | |
---|
[2111] | 95 | /** |
---|
| 96 | @brief Returns the smaller of two values. |
---|
| 97 | */ |
---|
| 98 | template <typename T> |
---|
| 99 | inline T min(T a, T b) |
---|
| 100 | { |
---|
| 101 | return (a <= b) ? a : b; |
---|
| 102 | } |
---|
[1505] | 103 | |
---|
[2111] | 104 | /** |
---|
| 105 | @brief Returns the greater of two values. |
---|
| 106 | */ |
---|
| 107 | template <typename T> |
---|
| 108 | inline T max(T a, T b) |
---|
| 109 | { |
---|
| 110 | return (a >= b) ? a : b; |
---|
| 111 | } |
---|
[1505] | 112 | |
---|
[2111] | 113 | /** |
---|
| 114 | @brief Keeps a value between a lower and an upper limit. |
---|
| 115 | @param x The value |
---|
| 116 | @param min The lower limit |
---|
| 117 | @param max The upper limit |
---|
| 118 | */ |
---|
| 119 | template <typename T> |
---|
| 120 | inline T clamp(T x, T min, T max) |
---|
| 121 | { |
---|
| 122 | if (x < min) |
---|
| 123 | return min; |
---|
[1505] | 124 | |
---|
[2111] | 125 | if (x > max) |
---|
| 126 | return max; |
---|
[1505] | 127 | |
---|
[2111] | 128 | return x; |
---|
| 129 | } |
---|
[1505] | 130 | |
---|
[2111] | 131 | /** |
---|
| 132 | @brief Returns the square value (x^2). |
---|
| 133 | */ |
---|
| 134 | template <typename T> |
---|
| 135 | inline T square(T x) |
---|
| 136 | { |
---|
| 137 | return x*x; |
---|
| 138 | } |
---|
[1505] | 139 | |
---|
[2111] | 140 | /** |
---|
| 141 | @brief Returns the cube value (x^3). |
---|
| 142 | */ |
---|
| 143 | template <typename T> |
---|
| 144 | inline T cube(T x) |
---|
| 145 | { |
---|
| 146 | return x*x*x; |
---|
| 147 | } |
---|
[1505] | 148 | |
---|
[2111] | 149 | /** |
---|
| 150 | @brief Rounds the value down. |
---|
| 151 | */ |
---|
| 152 | template <typename T> |
---|
| 153 | inline int floor(T x) |
---|
| 154 | { |
---|
| 155 | return (int)(x); |
---|
| 156 | } |
---|
[1505] | 157 | |
---|
[2111] | 158 | /** |
---|
| 159 | @brief Rounds the value up. |
---|
| 160 | */ |
---|
| 161 | template <typename T> |
---|
| 162 | inline int ceil(T x) |
---|
| 163 | { |
---|
| 164 | int temp = floor(x); |
---|
| 165 | return (temp != x) ? (temp + 1) : temp; |
---|
| 166 | } |
---|
[2002] | 167 | |
---|
[2111] | 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 | } |
---|
[2002] | 176 | |
---|
[2111] | 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 | } |
---|
[1505] | 190 | |
---|
[2111] | 191 | template <typename T> |
---|
| 192 | inline T zeroise() |
---|
| 193 | { |
---|
| 194 | BOOST_STATIC_ASSERT(sizeof(T) == 0); |
---|
| 195 | return T(); |
---|
| 196 | } |
---|
[1505] | 197 | |
---|
[2111] | 198 | template <> inline char zeroise<char>() { return 0; } |
---|
| 199 | template <> inline unsigned char zeroise<unsigned char>() { return 0; } |
---|
| 200 | template <> inline short zeroise<short>() { return 0; } |
---|
| 201 | template <> inline unsigned short zeroise<unsigned short>() { return 0; } |
---|
| 202 | template <> inline int zeroise<int>() { return 0; } |
---|
| 203 | template <> inline unsigned int zeroise<unsigned int>() { return 0; } |
---|
| 204 | template <> inline long zeroise<long>() { return 0; } |
---|
| 205 | template <> inline unsigned long zeroise<unsigned long>() { return 0; } |
---|
| 206 | template <> inline long long zeroise<long long>() { return 0; } |
---|
| 207 | template <> inline unsigned long long zeroise<unsigned long long>() { return 0; } |
---|
| 208 | template <> inline float zeroise<float>() { return 0; } |
---|
| 209 | template <> inline double zeroise<double>() { return 0; } |
---|
| 210 | template <> inline long double zeroise<long double>() { return 0; } |
---|
| 211 | template <> inline bool zeroise<bool>() { return 0; } |
---|
| 212 | template <> inline void* zeroise<void*>() { return 0; } |
---|
| 213 | template <> inline std::string zeroise<std::string>() { return ""; } |
---|
| 214 | template <> inline orxonox::Radian zeroise<orxonox::Radian>() { return orxonox::Radian(0.0f); } |
---|
| 215 | template <> inline orxonox::Degree zeroise<orxonox::Degree>() { return orxonox::Degree(0.0f); } |
---|
| 216 | template <> inline orxonox::Vector2 zeroise<orxonox::Vector2>() { return orxonox::Vector2 (0, 0) ; } |
---|
| 217 | template <> inline orxonox::Vector3 zeroise<orxonox::Vector3>() { return orxonox::Vector3 (0, 0, 0) ; } |
---|
| 218 | template <> inline orxonox::Vector4 zeroise<orxonox::Vector4>() { return orxonox::Vector4 (0, 0, 0, 0); } |
---|
| 219 | template <> inline orxonox::ColourValue zeroise<orxonox::ColourValue>() { return orxonox::ColourValue(0, 0, 0, 0); } |
---|
| 220 | template <> inline orxonox::Quaternion zeroise<orxonox::Quaternion>() { return orxonox::Quaternion (0, 0, 0, 0); } |
---|
[1505] | 221 | |
---|
[2111] | 222 | /** |
---|
| 223 | @brief Interpolates between two values for a time between 0 and 1. |
---|
| 224 | @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. |
---|
| 225 | @param start The value at time = 0 |
---|
| 226 | @param end The value at time = 1 |
---|
| 227 | @return The interpolation at a given time |
---|
| 228 | */ |
---|
| 229 | template <typename T> |
---|
| 230 | T interpolate(float time, const T& start, const T& end) |
---|
| 231 | { |
---|
| 232 | return time * (end - start) + start; |
---|
| 233 | } |
---|
[1505] | 234 | |
---|
[2111] | 235 | /** |
---|
| 236 | @brief Interpolates smoothly between two values for a time between 0 and 1. The function starts slowly, increases faster and stops slowly again. |
---|
| 237 | @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. |
---|
| 238 | @param start The value at time = 0 |
---|
| 239 | @param end The value at time = 1 |
---|
| 240 | @return The smoothed interpolation at a given time |
---|
| 241 | */ |
---|
| 242 | template <typename T> |
---|
| 243 | T interpolateSmooth(float time, const T& start, const T& end) |
---|
| 244 | { |
---|
| 245 | return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start; |
---|
| 246 | } |
---|
[1505] | 247 | |
---|
[2111] | 248 | /** |
---|
| 249 | @brief Returns a random number between 0 and almost 1: 0 <= rnd < 1. |
---|
| 250 | */ |
---|
| 251 | inline float rnd() |
---|
| 252 | { |
---|
| 253 | return rand() / (RAND_MAX + 1.0); |
---|
| 254 | } |
---|
[1625] | 255 | |
---|
[2111] | 256 | /** |
---|
| 257 | @brief Returns a random number between 0 and almost max: 0 <= rnd < max. |
---|
| 258 | @param max The maximum |
---|
| 259 | */ |
---|
| 260 | inline float rnd(float max) |
---|
| 261 | { |
---|
| 262 | return rnd() * max; |
---|
| 263 | } |
---|
[1505] | 264 | |
---|
[2111] | 265 | /** |
---|
| 266 | @brief Returns a random number between min and almost max: min <= rnd < max. |
---|
| 267 | @param min The minimum |
---|
| 268 | @param max The maximum |
---|
| 269 | */ |
---|
| 270 | inline float rnd(float min, float max) |
---|
| 271 | { |
---|
| 272 | return rnd(max - min) + min; |
---|
| 273 | } |
---|
[1505] | 274 | |
---|
[2111] | 275 | _UtilExport unsigned long getUniqueNumber(); |
---|
| 276 | |
---|
| 277 | class IntVector2 |
---|
| 278 | { |
---|
| 279 | public: |
---|
| 280 | IntVector2() : x(0), y(0) { } |
---|
| 281 | IntVector2(int _x, int _y) : x(_x), y(_y) { } |
---|
| 282 | int x; |
---|
| 283 | int y; |
---|
| 284 | }; |
---|
| 285 | |
---|
| 286 | class IntVector3 |
---|
| 287 | { |
---|
| 288 | public: |
---|
| 289 | IntVector3() : x(0), y(0), z(0) { } |
---|
| 290 | IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { } |
---|
| 291 | int x; |
---|
| 292 | int y; |
---|
| 293 | int z; |
---|
| 294 | }; |
---|
| 295 | } |
---|
| 296 | |
---|
[1505] | 297 | #endif /* _Util_Math_H__ */ |
---|