Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/forks/pch_analyser/src/libraries/util/Math.h @ 11930

Last change on this file since 11930 was 7425, checked in by rgrieder, 14 years ago

Fixed various problems revealed on tardis

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
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
29/**
30    @defgroup Math Mathematical functions
31    @ingroup Util
32*/
33
34/**
35    @file
36    @ingroup Math
37    @brief Declaration and implementation of several math-functions.
38*/
39
40#ifndef _Util_Math_H__
41#define _Util_Math_H__
42
43#include "UtilPrereqs.h"
44
45#include <string>
46#include <cmath>
47#include <cstdlib>
48
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
58
59namespace orxonox
60{
61    // C++ doesn't define any constants for pi, e, etc.
62    namespace math
63    {
64        const float pi      = 3.14159265f;      ///< PI
65        const float pi_2    = 1.57079633f;      ///< PI / 2
66        const float pi_4    = 7.85398163e-1f;   ///< PI / 4
67        const float e       = 2.71828183f;      ///< e
68        const float sqrt2   = 1.41421356f;      ///< sqrt(2)
69        const float sqrt2_2 = 7.07106781e-1f;   ///< sqrt(2) / 2
70
71        const double pi_d      = 3.14159265358979324;       ///< PI (double)
72        const double pi_2_d    = 1.57079632679489662;       ///< PI / 2 (double)
73        const double pi_4_d    = 7.85398163397448310e-1;    ///< PI / 4 (double)
74        const double e_d       = 2.71828182845904524;       ///< e (double)
75        const double sqrt2_d   = 1.41421356237309505;       ///< sqrt(2) (double)
76        const double sqrt2_2_d = 7.07106781186547524e-1;    ///< sqrt(2) / 2 (double)
77    }
78
79    /**
80        @brief Returns the sign of the given value.
81        @param x The value
82        @return 1 if the value is positive or zero, -1 if the value is negative
83    */
84    template <typename T>
85    inline T sgn(T x)
86    {
87        return (x >= 0) ? (T)1 : (T)-1;
88    }
89
90    /**
91        @brief Keeps a value between a lower and an upper limit. Values beyond these limits are limited to either @a min or @a max.
92        @param x The value
93        @param min The lower limit
94        @param max The upper limit
95    */
96    template <typename T>
97    inline T clamp(T x, T min, T max)
98    {
99        if (x < min)
100            return min;
101
102        if (x > max)
103            return max;
104
105        return x;
106    }
107
108    /**
109        @brief Returns the squared value (x^2).
110    */
111    template <typename T>
112    inline T square(T x)
113    {
114        return x*x;
115    }
116
117    /**
118        @brief Returns the cubed value (x^3).
119    */
120    template <typename T>
121    inline T cube(T x)
122    {
123        return x*x*x;
124    }
125
126    /**
127        @brief Rounds the value to the nearest integer.
128    */
129    template <typename T>
130    inline int round(T x)
131    {
132        return static_cast<int>(x + 0.5);
133    }
134
135    /**
136        @brief The modulo operation, enhanced to work properly with negative values.
137        @param x The value
138        @param max The operand
139
140        The built in modulo operator % yields a strange behavior with negative values.
141        This function corrects this - the result is guaranteed to lie always between
142        zero and (max-1).
143
144        Example:
145        @code
146        int var = 11 % 10;      //  1
147        int var = -1 % 10;      // -1
148
149        int var = mod(11, 10);  //  1
150        int var = mod(-1, 10);  //  9
151        @endcode
152    */
153    template <typename T>
154    inline int mod(T x, int max)
155    {
156        if (x >= 0)
157            return (x % max);
158        else
159            return ((x % max) + max);
160    }
161
162    /**
163        @brief Interpolates between two values for a time between 0 and 1.
164        @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.
165        @param start The value at @a time = 0
166        @param end The value at @a time = 1
167        @return The interpolated value at a given time
168    */
169    template <typename T>
170    inline T interpolate(float time, const T& start, const T& end)
171    {
172        return time * (end - start) + start;
173    }
174
175    /**
176        @brief Interpolates smoothly between two values for a time between 0 and 1. The function starts slowly, increases faster and stops slowly again.
177        @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.
178        @param start The value at @a time = 0
179        @param end The value at @a time = 1
180        @return The interpolated value at a given time
181    */
182    template <typename T>
183    inline T interpolateSmooth(float time, const T& start, const T& end)
184    {
185        return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start;
186    }
187
188    /**
189        @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>.
190    */
191    inline float rnd()
192    {
193        return rand() / (RAND_MAX + 1.0f);
194    }
195
196    /**
197        @brief Returns a random number between 0 and almost @a max: <tt>0 <= rnd < max</tt>.
198        @param max The maximum
199    */
200    inline float rnd(float max)
201    {
202        return rnd() * max;
203    }
204
205    /**
206        @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>.
207        @param min The minimum
208        @param max The maximum
209    */
210    inline float rnd(float min, float max)
211    {
212        return rnd(max - min) + min;
213    }
214
215    /**
216        @brief Returns randomly 1 or -1 with equal probability.
217    */
218    inline float rndsgn()
219    {
220        return static_cast<float>((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0
221    }
222
223    _UtilExport unsigned long getUniqueNumber();
224}
225
226#endif /* _Util_Math_H__ */
Note: See TracBrowser for help on using the repository browser.