[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 |
---|
[7421] | 37 | @brief Declaration and implementation of several math-functions. |
---|
[1791] | 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> |
---|
[1505] | 47 | |
---|
[3214] | 48 | // Certain headers might define unwanted macros... |
---|
| 49 | #undef max |
---|
| 50 | #undef min |
---|
| 51 | #undef sgn |
---|
| 52 | #undef clamp |
---|
| 53 | #undef sqrt |
---|
| 54 | #undef square |
---|
| 55 | #undef mod |
---|
| 56 | #undef rnd |
---|
[1781] | 57 | |
---|
[2171] | 58 | namespace orxonox |
---|
[1505] | 59 | { |
---|
[7184] | 60 | // C++ doesn't define any constants for pi, e, etc. |
---|
| 61 | namespace math |
---|
| 62 | { |
---|
[7401] | 63 | const float pi = 3.14159265f; ///< PI |
---|
| 64 | const float pi_2 = 1.57079633f; ///< PI / 2 |
---|
| 65 | const float pi_4 = 7.85398163e-1f; ///< PI / 4 |
---|
| 66 | const float e = 2.71828183f; ///< e |
---|
| 67 | const float sqrt2 = 1.41421356f; ///< sqrt(2) |
---|
| 68 | const float sqrt2_2 = 7.07106781e-1f; ///< sqrt(2) / 2 |
---|
[7184] | 69 | |
---|
[7401] | 70 | const double pi_d = 3.14159265358979324; ///< PI (double) |
---|
| 71 | const double pi_2_d = 1.57079632679489662; ///< PI / 2 (double) |
---|
| 72 | const double pi_4_d = 7.85398163397448310e-1; ///< PI / 4 (double) |
---|
| 73 | const double e_d = 2.71828182845904524; ///< e (double) |
---|
| 74 | const double sqrt2_d = 1.41421356237309505; ///< sqrt(2) (double) |
---|
| 75 | const double sqrt2_2_d = 7.07106781186547524e-1; ///< sqrt(2) / 2 (double) |
---|
[7184] | 76 | } |
---|
| 77 | |
---|
[2171] | 78 | /** |
---|
| 79 | @brief Returns the sign of the given value. |
---|
| 80 | @param x The value |
---|
| 81 | @return 1 if the value is positive or zero, -1 if the value is negative |
---|
| 82 | */ |
---|
| 83 | template <typename T> |
---|
| 84 | inline T sgn(T x) |
---|
| 85 | { |
---|
[6502] | 86 | return (x >= 0) ? (T)1 : (T)-1; |
---|
[2171] | 87 | } |
---|
[1505] | 88 | |
---|
[2171] | 89 | /** |
---|
[7401] | 90 | @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] | 91 | @param x The value |
---|
| 92 | @param min The lower limit |
---|
| 93 | @param max The upper limit |
---|
| 94 | */ |
---|
| 95 | template <typename T> |
---|
| 96 | inline T clamp(T x, T min, T max) |
---|
| 97 | { |
---|
| 98 | if (x < min) |
---|
| 99 | return min; |
---|
[1505] | 100 | |
---|
[2171] | 101 | if (x > max) |
---|
| 102 | return max; |
---|
[1505] | 103 | |
---|
[2171] | 104 | return x; |
---|
| 105 | } |
---|
[1505] | 106 | |
---|
[2171] | 107 | /** |
---|
[7401] | 108 | @brief Returns the squared value (x^2). |
---|
[2171] | 109 | */ |
---|
| 110 | template <typename T> |
---|
| 111 | inline T square(T x) |
---|
| 112 | { |
---|
| 113 | return x*x; |
---|
| 114 | } |
---|
[1505] | 115 | |
---|
[2171] | 116 | /** |
---|
[7401] | 117 | @brief Returns the cubed value (x^3). |
---|
[2171] | 118 | */ |
---|
| 119 | template <typename T> |
---|
| 120 | inline T cube(T x) |
---|
| 121 | { |
---|
| 122 | return x*x*x; |
---|
| 123 | } |
---|
[1505] | 124 | |
---|
[2171] | 125 | /** |
---|
[7401] | 126 | @brief Rounds the value to the nearest integer. |
---|
[2171] | 127 | */ |
---|
| 128 | template <typename T> |
---|
| 129 | inline int round(T x) |
---|
| 130 | { |
---|
[3300] | 131 | return static_cast<int>(x + 0.5); |
---|
[2171] | 132 | } |
---|
[1505] | 133 | |
---|
[2171] | 134 | /** |
---|
| 135 | @brief The modulo operation, enhanced to work properly with negative values. |
---|
| 136 | @param x The value |
---|
| 137 | @param max The operand |
---|
[7401] | 138 | |
---|
| 139 | The built in modulo operator % yields a strange behavior with negative values. |
---|
| 140 | This function corrects this - the result is guaranteed to lie always between |
---|
| 141 | zero and (max-1). |
---|
| 142 | |
---|
| 143 | Example: |
---|
| 144 | @code |
---|
| 145 | int var = 11 % 10; // 1 |
---|
| 146 | int var = -1 % 10; // -1 |
---|
| 147 | |
---|
| 148 | int var = mod(11, 10); // 1 |
---|
| 149 | int var = mod(-1, 10); // 9 |
---|
| 150 | @endcode |
---|
[2171] | 151 | */ |
---|
| 152 | template <typename T> |
---|
| 153 | inline int mod(T x, int max) |
---|
| 154 | { |
---|
| 155 | if (x >= 0) |
---|
| 156 | return (x % max); |
---|
| 157 | else |
---|
| 158 | return ((x % max) + max); |
---|
| 159 | } |
---|
[2087] | 160 | |
---|
[7401] | 161 | /** |
---|
[2171] | 162 | @brief Interpolates between two values for a time between 0 and 1. |
---|
[7401] | 163 | @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. |
---|
| 164 | @param start The value at @a time = 0 |
---|
| 165 | @param end The value at @a time = 1 |
---|
| 166 | @return The interpolated value at a given time |
---|
[2171] | 167 | */ |
---|
| 168 | template <typename T> |
---|
[3196] | 169 | inline T interpolate(float time, const T& start, const T& end) |
---|
[2171] | 170 | { |
---|
| 171 | return time * (end - start) + start; |
---|
| 172 | } |
---|
[1505] | 173 | |
---|
[2171] | 174 | /** |
---|
| 175 | @brief Interpolates smoothly between two values for a time between 0 and 1. The function starts slowly, increases faster and stops slowly again. |
---|
[7401] | 176 | @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. |
---|
| 177 | @param start The value at @a time = 0 |
---|
| 178 | @param end The value at @a time = 1 |
---|
| 179 | @return The interpolated value at a given time |
---|
[2171] | 180 | */ |
---|
| 181 | template <typename T> |
---|
[3196] | 182 | inline T interpolateSmooth(float time, const T& start, const T& end) |
---|
[2171] | 183 | { |
---|
| 184 | return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start; |
---|
| 185 | } |
---|
[1505] | 186 | |
---|
[2171] | 187 | /** |
---|
[7401] | 188 | @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>. |
---|
[2171] | 189 | */ |
---|
| 190 | inline float rnd() |
---|
| 191 | { |
---|
[3196] | 192 | return rand() / (RAND_MAX + 1.0f); |
---|
[2171] | 193 | } |
---|
[1505] | 194 | |
---|
[2171] | 195 | /** |
---|
[7401] | 196 | @brief Returns a random number between 0 and almost @a max: <tt>0 <= rnd < max</tt>. |
---|
[2171] | 197 | @param max The maximum |
---|
| 198 | */ |
---|
| 199 | inline float rnd(float max) |
---|
| 200 | { |
---|
| 201 | return rnd() * max; |
---|
| 202 | } |
---|
[1505] | 203 | |
---|
[2171] | 204 | /** |
---|
[7401] | 205 | @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>. |
---|
[2171] | 206 | @param min The minimum |
---|
| 207 | @param max The maximum |
---|
| 208 | */ |
---|
| 209 | inline float rnd(float min, float max) |
---|
| 210 | { |
---|
| 211 | return rnd(max - min) + min; |
---|
| 212 | } |
---|
[1625] | 213 | |
---|
[2872] | 214 | /** |
---|
| 215 | @brief Returns randomly 1 or -1 with equal probability. |
---|
| 216 | */ |
---|
| 217 | inline float rndsgn() |
---|
| 218 | { |
---|
[3196] | 219 | return static_cast<float>((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0 |
---|
[2872] | 220 | } |
---|
| 221 | |
---|
[2171] | 222 | _UtilExport unsigned long getUniqueNumber(); |
---|
| 223 | } |
---|
| 224 | |
---|
[1505] | 225 | #endif /* _Util_Math_H__ */ |
---|