[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 | /** |
---|
[7401] | 30 | @defgroup Math Mathematical functions |
---|
| 31 | @ingroup Util |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | /** |
---|
[2171] | 35 | @file |
---|
[7401] | 36 | @ingroup Math |
---|
[1791] | 37 | @brief Declaration and implementation of several math-functions, typedefs of some Ogre::Math classes to the orxonox namespace. |
---|
| 38 | */ |
---|
| 39 | |
---|
[1505] | 40 | #ifndef _Util_Math_H__ |
---|
| 41 | #define _Util_Math_H__ |
---|
| 42 | |
---|
| 43 | #include "UtilPrereqs.h" |
---|
| 44 | |
---|
[1625] | 45 | #include <string> |
---|
[2171] | 46 | #include <cmath> |
---|
[7427] | 47 | #include <cstdlib> |
---|
[1505] | 48 | |
---|
| 49 | #include <OgreMath.h> |
---|
| 50 | #include <OgreVector2.h> |
---|
| 51 | #include <OgreVector3.h> |
---|
| 52 | #include <OgreVector4.h> |
---|
| 53 | #include <OgreQuaternion.h> |
---|
| 54 | #include <OgreColourValue.h> |
---|
| 55 | |
---|
[3214] | 56 | // Certain headers might define unwanted macros... |
---|
| 57 | #undef max |
---|
| 58 | #undef min |
---|
| 59 | #undef sgn |
---|
| 60 | #undef clamp |
---|
| 61 | #undef sqrt |
---|
| 62 | #undef square |
---|
| 63 | #undef mod |
---|
| 64 | #undef rnd |
---|
[1781] | 65 | |
---|
[2171] | 66 | namespace orxonox |
---|
[1505] | 67 | { |
---|
[8351] | 68 | /** Often used numerical constants because C++ doesn't define any. |
---|
| 69 | @note |
---|
| 70 | The values here are decimal representations of the approximate floating |
---|
| 71 | point value as it is stored according to the IEEE 754 standard. |
---|
| 72 | */ |
---|
[7184] | 73 | namespace math |
---|
| 74 | { |
---|
[8351] | 75 | const float twoPi = 6.283185482025146484375f; ///< PI * 2 |
---|
| 76 | const float pi = 3.1415927410125732421875f; ///< PI |
---|
| 77 | const float pi_2 = 1.57079637050628662109375f; ///< PI / 2 |
---|
| 78 | const float pi_4 = 0.785398185253143310546875f; ///< PI / 4 |
---|
| 79 | const float e = 2.718281269073486328125f; ///< e |
---|
| 80 | const float sqrt2 = 1.41421353816986083984375f; ///< sqrt(2) |
---|
| 81 | const float sqrt2_2 = 0.707106769084930419921875f; ///< sqrt(2) / 2 |
---|
[7184] | 82 | } |
---|
| 83 | |
---|
[6417] | 84 | #if OGRE_VERSION < 0x010603 |
---|
[2171] | 85 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian); |
---|
[6417] | 86 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree); |
---|
| 87 | #endif |
---|
[2171] | 88 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian); |
---|
| 89 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree); |
---|
[1505] | 90 | |
---|
[2171] | 91 | _UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition); |
---|
| 92 | _UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
| 93 | _UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
| 94 | _UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity); |
---|
[1505] | 95 | |
---|
[2171] | 96 | /** |
---|
| 97 | @brief Returns the sign of the given value. |
---|
| 98 | @param x The value |
---|
| 99 | @return 1 if the value is positive or zero, -1 if the value is negative |
---|
| 100 | */ |
---|
| 101 | template <typename T> |
---|
| 102 | inline T sgn(T x) |
---|
| 103 | { |
---|
[6502] | 104 | return (x >= 0) ? (T)1 : (T)-1; |
---|
[2171] | 105 | } |
---|
[1505] | 106 | |
---|
[2171] | 107 | /** |
---|
[7401] | 108 | @brief Keeps a value between a lower and an upper limit. Values beyond these limits are limited to either @a min or @a max. |
---|
[2171] | 109 | @param x The value |
---|
| 110 | @param min The lower limit |
---|
| 111 | @param max The upper limit |
---|
| 112 | */ |
---|
| 113 | template <typename T> |
---|
| 114 | inline T clamp(T x, T min, T max) |
---|
| 115 | { |
---|
| 116 | if (x < min) |
---|
| 117 | return min; |
---|
[1505] | 118 | |
---|
[2171] | 119 | if (x > max) |
---|
| 120 | return max; |
---|
[1505] | 121 | |
---|
[2171] | 122 | return x; |
---|
| 123 | } |
---|
[1505] | 124 | |
---|
[2171] | 125 | /** |
---|
[7401] | 126 | @brief Returns the squared value (x^2). |
---|
[2171] | 127 | */ |
---|
| 128 | template <typename T> |
---|
| 129 | inline T square(T x) |
---|
| 130 | { |
---|
| 131 | return x*x; |
---|
| 132 | } |
---|
[1505] | 133 | |
---|
[2171] | 134 | /** |
---|
[7401] | 135 | @brief Returns the cubed value (x^3). |
---|
[2171] | 136 | */ |
---|
| 137 | template <typename T> |
---|
| 138 | inline T cube(T x) |
---|
| 139 | { |
---|
| 140 | return x*x*x; |
---|
| 141 | } |
---|
[1505] | 142 | |
---|
[2171] | 143 | /** |
---|
[7401] | 144 | @brief Rounds the value to the nearest integer. |
---|
[2171] | 145 | */ |
---|
| 146 | template <typename T> |
---|
| 147 | inline int round(T x) |
---|
| 148 | { |
---|
[3300] | 149 | return static_cast<int>(x + 0.5); |
---|
[2171] | 150 | } |
---|
[1505] | 151 | |
---|
[2171] | 152 | /** |
---|
| 153 | @brief The modulo operation, enhanced to work properly with negative values. |
---|
| 154 | @param x The value |
---|
| 155 | @param max The operand |
---|
[7401] | 156 | |
---|
| 157 | The built in modulo operator % yields a strange behavior with negative values. |
---|
| 158 | This function corrects this - the result is guaranteed to lie always between |
---|
| 159 | zero and (max-1). |
---|
| 160 | |
---|
| 161 | Example: |
---|
| 162 | @code |
---|
| 163 | int var = 11 % 10; // 1 |
---|
| 164 | int var = -1 % 10; // -1 |
---|
| 165 | |
---|
| 166 | int var = mod(11, 10); // 1 |
---|
| 167 | int var = mod(-1, 10); // 9 |
---|
| 168 | @endcode |
---|
[2171] | 169 | */ |
---|
| 170 | template <typename T> |
---|
| 171 | inline int mod(T x, int max) |
---|
| 172 | { |
---|
| 173 | if (x >= 0) |
---|
| 174 | return (x % max); |
---|
| 175 | else |
---|
| 176 | return ((x % max) + max); |
---|
| 177 | } |
---|
[2087] | 178 | |
---|
[7401] | 179 | /** |
---|
| 180 | @brief Returns a "zero" value for the given type. |
---|
| 181 | @note This is the default template of the zeroise() function. The template is spezialized for each supported type. |
---|
| 182 | |
---|
| 183 | The exact return value of the function depends on the type. For @c int this is 0, |
---|
| 184 | for @c float it's 0.0f. For a @c std::string the function returns "" and for |
---|
| 185 | @c Vector3 you get <tt>Vector3(0, 0, 0)</tt>. |
---|
| 186 | */ |
---|
[2171] | 187 | template <typename T> |
---|
| 188 | inline T zeroise() |
---|
| 189 | { |
---|
[2662] | 190 | // Default, raise a compiler error without including large boost header cascade. |
---|
| 191 | T temp(); |
---|
| 192 | *********temp; // If you reach this code, you abused zeroise()! |
---|
| 193 | return temp; |
---|
[2171] | 194 | } |
---|
[2087] | 195 | |
---|
[2171] | 196 | template <> inline char zeroise<char>() { return 0; } |
---|
| 197 | template <> inline unsigned char zeroise<unsigned char>() { return 0; } |
---|
| 198 | template <> inline short zeroise<short>() { return 0; } |
---|
| 199 | template <> inline unsigned short zeroise<unsigned short>() { return 0; } |
---|
| 200 | template <> inline int zeroise<int>() { return 0; } |
---|
| 201 | template <> inline unsigned int zeroise<unsigned int>() { return 0; } |
---|
| 202 | template <> inline long zeroise<long>() { return 0; } |
---|
| 203 | template <> inline unsigned long zeroise<unsigned long>() { return 0; } |
---|
| 204 | template <> inline long long zeroise<long long>() { return 0; } |
---|
| 205 | template <> inline unsigned long long zeroise<unsigned long long>() { return 0; } |
---|
| 206 | template <> inline float zeroise<float>() { return 0; } |
---|
| 207 | template <> inline double zeroise<double>() { return 0; } |
---|
| 208 | template <> inline long double zeroise<long double>() { return 0; } |
---|
| 209 | template <> inline bool zeroise<bool>() { return 0; } |
---|
| 210 | template <> inline void* zeroise<void*>() { return 0; } |
---|
[6417] | 211 | template <> inline std::string zeroise<std::string>() { return std::string(); } |
---|
[2171] | 212 | template <> inline orxonox::Radian zeroise<orxonox::Radian>() { return orxonox::Radian(0.0f); } |
---|
| 213 | template <> inline orxonox::Degree zeroise<orxonox::Degree>() { return orxonox::Degree(0.0f); } |
---|
| 214 | template <> inline orxonox::Vector2 zeroise<orxonox::Vector2>() { return orxonox::Vector2 (0, 0) ; } |
---|
| 215 | template <> inline orxonox::Vector3 zeroise<orxonox::Vector3>() { return orxonox::Vector3 (0, 0, 0) ; } |
---|
| 216 | template <> inline orxonox::Vector4 zeroise<orxonox::Vector4>() { return orxonox::Vector4 (0, 0, 0, 0); } |
---|
| 217 | template <> inline orxonox::ColourValue zeroise<orxonox::ColourValue>() { return orxonox::ColourValue(0, 0, 0, 0); } |
---|
| 218 | template <> inline orxonox::Quaternion zeroise<orxonox::Quaternion>() { return orxonox::Quaternion (0, 0, 0, 0); } |
---|
[1505] | 219 | |
---|
[7401] | 220 | /** |
---|
| 221 | @brief Provides zero value symbols that can be returned as reference |
---|
| 222 | @see zeroise() |
---|
| 223 | */ |
---|
[3196] | 224 | template <typename T> |
---|
| 225 | struct NilValue |
---|
| 226 | { |
---|
| 227 | inline operator const T&() const |
---|
| 228 | { |
---|
| 229 | return value; |
---|
| 230 | } |
---|
| 231 | static T value; |
---|
| 232 | }; |
---|
| 233 | template <typename T> |
---|
| 234 | T NilValue<T>::value = zeroise<T>(); |
---|
| 235 | |
---|
[2171] | 236 | /** |
---|
| 237 | @brief Interpolates between two values for a time between 0 and 1. |
---|
[7401] | 238 | @param time The time is a value between 0 and 1 - the function returns @a start if @a time is 0, @a end if @a time is 1, and interpolates if @a time is between 0 and 1. |
---|
| 239 | @param start The value at @a time = 0 |
---|
| 240 | @param end The value at @a time = 1 |
---|
| 241 | @return The interpolated value at a given time |
---|
[2171] | 242 | */ |
---|
| 243 | template <typename T> |
---|
[3196] | 244 | inline T interpolate(float time, const T& start, const T& end) |
---|
[2171] | 245 | { |
---|
| 246 | return time * (end - start) + start; |
---|
| 247 | } |
---|
[1505] | 248 | |
---|
[2171] | 249 | /** |
---|
| 250 | @brief Interpolates smoothly between two values for a time between 0 and 1. The function starts slowly, increases faster and stops slowly again. |
---|
[7401] | 251 | @param time The time is a value between 0 and 1 - the function returns @a start if @a time is 0, @a end if @a time is 1, and interpolates if @a time is between 0 and 1. |
---|
| 252 | @param start The value at @a time = 0 |
---|
| 253 | @param end The value at @a time = 1 |
---|
| 254 | @return The interpolated value at a given time |
---|
[2171] | 255 | */ |
---|
| 256 | template <typename T> |
---|
[3196] | 257 | inline T interpolateSmooth(float time, const T& start, const T& end) |
---|
[2171] | 258 | { |
---|
| 259 | return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start; |
---|
| 260 | } |
---|
[1505] | 261 | |
---|
[2171] | 262 | /** |
---|
[7401] | 263 | @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>. |
---|
[2171] | 264 | */ |
---|
| 265 | inline float rnd() |
---|
| 266 | { |
---|
[3196] | 267 | return rand() / (RAND_MAX + 1.0f); |
---|
[2171] | 268 | } |
---|
[1505] | 269 | |
---|
[2171] | 270 | /** |
---|
[7401] | 271 | @brief Returns a random number between 0 and almost @a max: <tt>0 <= rnd < max</tt>. |
---|
[2171] | 272 | @param max The maximum |
---|
| 273 | */ |
---|
| 274 | inline float rnd(float max) |
---|
| 275 | { |
---|
| 276 | return rnd() * max; |
---|
| 277 | } |
---|
[1505] | 278 | |
---|
[2171] | 279 | /** |
---|
[7401] | 280 | @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>. |
---|
[2171] | 281 | @param min The minimum |
---|
| 282 | @param max The maximum |
---|
| 283 | */ |
---|
| 284 | inline float rnd(float min, float max) |
---|
| 285 | { |
---|
| 286 | return rnd(max - min) + min; |
---|
| 287 | } |
---|
[1625] | 288 | |
---|
[2872] | 289 | /** |
---|
| 290 | @brief Returns randomly 1 or -1 with equal probability. |
---|
| 291 | */ |
---|
| 292 | inline float rndsgn() |
---|
| 293 | { |
---|
[3196] | 294 | return static_cast<float>((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0 |
---|
[2872] | 295 | } |
---|
| 296 | |
---|
[2171] | 297 | _UtilExport unsigned long getUniqueNumber(); |
---|
| 298 | } |
---|
| 299 | |
---|
[1505] | 300 | #endif /* _Util_Math_H__ */ |
---|