Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/util/Math.h @ 7184

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

Replaced mathematical constants with a common definition in Math.h.
Use math::pi, math::pi_d (double), math::e, etc. from now on.

  • Property svn:eol-style set to native
File size: 9.9 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    @file
31    @brief Declaration and implementation of several math-functions, typedefs of some Ogre::Math classes to the orxonox namespace.
32*/
33
34#ifndef _Util_Math_H__
35#define _Util_Math_H__
36
37#include "UtilPrereqs.h"
38
39#include <string>
40#include <cmath>
41
42#include <OgreMath.h>
43#include <OgreVector2.h>
44#include <OgreVector3.h>
45#include <OgreVector4.h>
46#include <OgreQuaternion.h>
47#include <OgreColourValue.h>
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;
65        const float pi_2    = 1.57079633f;
66        const float pi_4    = 7.85398163e-1f;
67        const float e       = 2.71828183f;
68        const float sqrt2   = 1.41421356f;
69        const float sqrt2_2 = 7.07106781e-1f;
70
71        const double pi_d      = 3.14159265358979324;
72        const double pi_2_d    = 1.57079632679489662;
73        const double pi_4_d    = 7.85398163397448310e-1;
74        const double e_d       = 2.71828182845904524;
75        const double sqrt2_d   = 1.41421356237309505;
76        const double sqrt2_2_d = 7.07106781186547524e-1;
77    }
78
79#if OGRE_VERSION < 0x010603
80    _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian);
81    _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree);
82#endif
83    _UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian);
84    _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree);
85
86    _UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition);
87    _UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
88    _UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
89    _UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity);
90
91    /**
92        @brief Returns the sign of the given value.
93        @param x The value
94        @return 1 if the value is positive or zero, -1 if the value is negative
95    */
96    template <typename T>
97    inline T sgn(T x)
98    {
99        return (x >= 0) ? (T)1 : (T)-1;
100    }
101
102    /**
103        @brief Keeps a value between a lower and an upper limit.
104        @param x The value
105        @param min The lower limit
106        @param max The upper limit
107    */
108    template <typename T>
109    inline T clamp(T x, T min, T max)
110    {
111        if (x < min)
112            return min;
113
114        if (x > max)
115            return max;
116
117        return x;
118    }
119
120    /**
121        @brief Returns the square value (x^2).
122    */
123    template <typename T>
124    inline T square(T x)
125    {
126        return x*x;
127    }
128
129    /**
130        @brief Returns the cube value (x^3).
131    */
132    template <typename T>
133    inline T cube(T x)
134    {
135        return x*x*x;
136    }
137
138    /**
139        @brief Rounds the value.
140    */
141    template <typename T>
142    inline int round(T x)
143    {
144        return static_cast<int>(x + 0.5);
145    }
146
147    /**
148        @brief The modulo operation, enhanced to work properly with negative values.
149        @param x The value
150        @param max The operand
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    template <typename T>
162    inline T zeroise()
163    {
164        // Default, raise a compiler error without including large boost header cascade.
165        T temp();
166        *********temp; // If you reach this code, you abused zeroise()!
167        return temp;
168    }
169
170    template <> inline char                 zeroise<char>()                 { return 0; }
171    template <> inline unsigned char        zeroise<unsigned char>()        { return 0; }
172    template <> inline short                zeroise<short>()                { return 0; }
173    template <> inline unsigned short       zeroise<unsigned short>()       { return 0; }
174    template <> inline int                  zeroise<int>()                  { return 0; }
175    template <> inline unsigned int         zeroise<unsigned int>()         { return 0; }
176    template <> inline long                 zeroise<long>()                 { return 0; }
177    template <> inline unsigned long        zeroise<unsigned long>()        { return 0; }
178    template <> inline long long            zeroise<long long>()            { return 0; }
179    template <> inline unsigned long long   zeroise<unsigned long long>()   { return 0; }
180    template <> inline float                zeroise<float>()                { return 0; }
181    template <> inline double               zeroise<double>()               { return 0; }
182    template <> inline long double          zeroise<long double>()          { return 0; }
183    template <> inline bool                 zeroise<bool>()                 { return 0; }
184    template <> inline void*                zeroise<void*>()                { return 0; }
185    template <> inline std::string          zeroise<std::string>()          { return std::string(); }
186    template <> inline orxonox::Radian      zeroise<orxonox::Radian>()      { return orxonox::Radian(0.0f); }
187    template <> inline orxonox::Degree      zeroise<orxonox::Degree>()      { return orxonox::Degree(0.0f); }
188    template <> inline orxonox::Vector2     zeroise<orxonox::Vector2>()     { return orxonox::Vector2    (0, 0)      ; }
189    template <> inline orxonox::Vector3     zeroise<orxonox::Vector3>()     { return orxonox::Vector3    (0, 0, 0)   ; }
190    template <> inline orxonox::Vector4     zeroise<orxonox::Vector4>()     { return orxonox::Vector4    (0, 0, 0, 0); }
191    template <> inline orxonox::ColourValue zeroise<orxonox::ColourValue>() { return orxonox::ColourValue(0, 0, 0, 0); }
192    template <> inline orxonox::Quaternion  zeroise<orxonox::Quaternion>()  { return orxonox::Quaternion (0, 0, 0, 0); }
193
194    //! Provides zero value symbols that can be returned as reference
195    template <typename T>
196    struct NilValue
197    {
198        inline operator const T&() const
199        {
200            return value;
201        }
202        static T value;
203    };
204    template <typename T>
205    T NilValue<T>::value = zeroise<T>();
206
207    /**
208        @brief Interpolates between two values for a time between 0 and 1.
209        @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.
210        @param start The value at time = 0
211        @param end The value at time = 1
212        @return The interpolation at a given time
213    */
214    template <typename T>
215    inline T interpolate(float time, const T& start, const T& end)
216    {
217        return time * (end - start) + start;
218    }
219
220    /**
221        @brief Interpolates smoothly between two values for a time between 0 and 1. The function starts slowly, increases faster and stops slowly again.
222        @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.
223        @param start The value at time = 0
224        @param end The value at time = 1
225        @return The smoothed interpolation at a given time
226    */
227    template <typename T>
228    inline T interpolateSmooth(float time, const T& start, const T& end)
229    {
230        return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start;
231    }
232
233    /**
234        @brief Returns a random number between 0 and almost 1: 0 <= rnd < 1.
235    */
236    inline float rnd()
237    {
238        return rand() / (RAND_MAX + 1.0f);
239    }
240
241    /**
242        @brief Returns a random number between 0 and almost max: 0 <= rnd < max.
243        @param max The maximum
244    */
245    inline float rnd(float max)
246    {
247        return rnd() * max;
248    }
249
250    /**
251        @brief Returns a random number between min and almost max: min <= rnd < max.
252        @param min The minimum
253        @param max The maximum
254    */
255    inline float rnd(float min, float max)
256    {
257        return rnd(max - min) + min;
258    }
259
260    /**
261        @brief Returns randomly 1 or -1 with equal probability.
262    */
263    inline float rndsgn()
264    {
265        return static_cast<float>((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0
266    }
267
268    _UtilExport unsigned long getUniqueNumber();
269
270    class IntVector2
271    {
272    public:
273        IntVector2() : x(0), y(0) { }
274        IntVector2(int _x, int _y) : x(_x), y(_y) { }
275        int x;
276        int y;
277    };
278
279    class IntVector3
280    {
281    public:
282        IntVector3() : x(0), y(0), z(0) { }
283        IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { }
284        int x;
285        int y;
286        int z;
287    };
288}
289
290#endif /* _Util_Math_H__ */
Note: See TracBrowser for help on using the repository browser.