Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: sandbox_qt/src/libraries/util/Math.h @ 7421

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

Basic stuff up and running for the Qt sandbox.
No GUI support yet.

  • 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
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
57
58namespace orxonox
59{
60    // C++ doesn't define any constants for pi, e, etc.
61    namespace math
62    {
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
69
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)
76    }
77
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    {
86        return (x >= 0) ? (T)1 : (T)-1;
87    }
88
89    /**
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.
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;
100
101        if (x > max)
102            return max;
103
104        return x;
105    }
106
107    /**
108        @brief Returns the squared value (x^2).
109    */
110    template <typename T>
111    inline T square(T x)
112    {
113        return x*x;
114    }
115
116    /**
117        @brief Returns the cubed value (x^3).
118    */
119    template <typename T>
120    inline T cube(T x)
121    {
122        return x*x*x;
123    }
124
125    /**
126        @brief Rounds the value to the nearest integer.
127    */
128    template <typename T>
129    inline int round(T x)
130    {
131        return static_cast<int>(x + 0.5);
132    }
133
134    /**
135        @brief The modulo operation, enhanced to work properly with negative values.
136        @param x The value
137        @param max The operand
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
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    }
160
161    /**
162        @brief Interpolates between two values for a time between 0 and 1.
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
167    */
168    template <typename T>
169    inline T interpolate(float time, const T& start, const T& end)
170    {
171        return time * (end - start) + start;
172    }
173
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.
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
180    */
181    template <typename T>
182    inline T interpolateSmooth(float time, const T& start, const T& end)
183    {
184        return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start;
185    }
186
187    /**
188        @brief Returns a random number between 0 and almost 1: <tt>0 <= rnd < 1</tt>.
189    */
190    inline float rnd()
191    {
192        return rand() / (RAND_MAX + 1.0f);
193    }
194
195    /**
196        @brief Returns a random number between 0 and almost @a max: <tt>0 <= rnd < max</tt>.
197        @param max The maximum
198    */
199    inline float rnd(float max)
200    {
201        return rnd() * max;
202    }
203
204    /**
205        @brief Returns a random number between @a min and almost @a max: <tt>min <= rnd < max</tt>.
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    }
213
214    /**
215        @brief Returns randomly 1 or -1 with equal probability.
216    */
217    inline float rndsgn()
218    {
219        return static_cast<float>((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0
220    }
221
222    _UtilExport unsigned long getUniqueNumber();
223}
224
225#endif /* _Util_Math_H__ */
Note: See TracBrowser for help on using the repository browser.