Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/util/Math.h @ 3133

Last change on this file since 3133 was 3128, checked in by rgrieder, 15 years ago

Allow use of Vector2, etc. as pointers or references when only including UtilPrereqs.h

  • Property svn:eol-style set to native
File size: 9.8 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 <ostream>
40#include <string>
41#include <cmath>
42#include <boost/static_assert.hpp>
43
44#include <OgreMath.h>
45#include <OgreVector2.h>
46#include <OgreVector3.h>
47#include <OgreVector4.h>
48#include <OgreMatrix3.h>
49#include <OgreMatrix4.h>
50#include <OgreQuaternion.h>
51#include <OgreColourValue.h>
52
53// Certain headers might define min and max macros
54#if defined(max) || defined(min) || defined(sgn) || defined(clamp) || defined(square) || defined(mod)
55#  error An inline math function was overridden by a macro
56#endif
57
58namespace orxonox
59{
60    // Also define our own transform space enum
61    namespace TransformSpace
62    {
63        /**
64        @brief
65            Enumeration denoting the spaces which a transform can be relative to.
66        */
67        enum Enum
68        {
69            //! Transform is relative to the local space
70            Local,
71            //! Transform is relative to the space of the parent node
72            Parent,
73            //! Transform is relative to world space
74            World
75        };
76    }
77
78    _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian);
79    _UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian);
80    _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree);
81    _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree);
82
83    _UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition);
84    _UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
85    _UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
86    _UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity);
87
88    /**
89        @brief Returns the sign of the given value.
90        @param x The value
91        @return 1 if the value is positive or zero, -1 if the value is negative
92    */
93    template <typename T>
94    inline T sgn(T x)
95    {
96        return (x >= 0) ? 1 : -1;
97    }
98
99    /**
100        @brief Returns the smaller of two values.
101    */
102    template <typename T>
103    inline T min(T a, T b)
104    {
105        return (a <= b) ? a : b;
106    }
107
108    /**
109        @brief Returns the greater of two values.
110    */
111    template <typename T>
112    inline T max(T a, T b)
113    {
114        return (a >= b) ? a : b;
115    }
116
117    /**
118        @brief Keeps a value between a lower and an upper limit.
119        @param x The value
120        @param min The lower limit
121        @param max The upper limit
122    */
123    template <typename T>
124    inline T clamp(T x, T min, T max)
125    {
126        if (x < min)
127            return min;
128
129        if (x > max)
130            return max;
131
132        return x;
133    }
134
135    /**
136        @brief Returns the square value (x^2).
137    */
138    template <typename T>
139    inline T square(T x)
140    {
141        return x*x;
142    }
143
144    /**
145        @brief Returns the cube value (x^3).
146    */
147    template <typename T>
148    inline T cube(T x)
149    {
150        return x*x*x;
151    }
152
153    /**
154        @brief Rounds the value.
155    */
156    template <typename T>
157    inline int round(T x)
158    {
159        return (int)(x + 0.5);
160    }
161
162    /**
163        @brief The modulo operation, enhanced to work properly with negative values.
164        @param x The value
165        @param max The operand
166    */
167    template <typename T>
168    inline int mod(T x, int max)
169    {
170        if (x >= 0)
171            return (x % max);
172        else
173            return ((x % max) + max);
174    }
175
176    template <typename T>
177    inline T zeroise()
178    {
179        // Default, raise a compiler error without including large boost header cascade.
180        T temp();
181        *********temp; // If you reach this code, you abused zeroise()!
182        return temp;
183    }
184
185    template <> inline char                 zeroise<char>()                 { return 0; }
186    template <> inline unsigned char        zeroise<unsigned char>()        { return 0; }
187    template <> inline short                zeroise<short>()                { return 0; }
188    template <> inline unsigned short       zeroise<unsigned short>()       { return 0; }
189    template <> inline int                  zeroise<int>()                  { return 0; }
190    template <> inline unsigned int         zeroise<unsigned int>()         { return 0; }
191    template <> inline long                 zeroise<long>()                 { return 0; }
192    template <> inline unsigned long        zeroise<unsigned long>()        { return 0; }
193    template <> inline long long            zeroise<long long>()            { return 0; }
194    template <> inline unsigned long long   zeroise<unsigned long long>()   { return 0; }
195    template <> inline float                zeroise<float>()                { return 0; }
196    template <> inline double               zeroise<double>()               { return 0; }
197    template <> inline long double          zeroise<long double>()          { return 0; }
198    template <> inline bool                 zeroise<bool>()                 { return 0; }
199    template <> inline void*                zeroise<void*>()                { return 0; }
200    template <> inline std::string          zeroise<std::string>()          { return ""; }
201    template <> inline orxonox::Radian      zeroise<orxonox::Radian>()      { return orxonox::Radian(0.0f); }
202    template <> inline orxonox::Degree      zeroise<orxonox::Degree>()      { return orxonox::Degree(0.0f); }
203    template <> inline orxonox::Vector2     zeroise<orxonox::Vector2>()     { return orxonox::Vector2    (0, 0)      ; }
204    template <> inline orxonox::Vector3     zeroise<orxonox::Vector3>()     { return orxonox::Vector3    (0, 0, 0)   ; }
205    template <> inline orxonox::Vector4     zeroise<orxonox::Vector4>()     { return orxonox::Vector4    (0, 0, 0, 0); }
206    template <> inline orxonox::ColourValue zeroise<orxonox::ColourValue>() { return orxonox::ColourValue(0, 0, 0, 0); }
207    template <> inline orxonox::Quaternion  zeroise<orxonox::Quaternion>()  { return orxonox::Quaternion (0, 0, 0, 0); }
208
209    /**
210        @brief Interpolates between two values for a time between 0 and 1.
211        @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.
212        @param start The value at time = 0
213        @param end The value at time = 1
214        @return The interpolation at a given time
215    */
216    template <typename T>
217    T interpolate(float time, const T& start, const T& end)
218    {
219        return time * (end - start) + start;
220    }
221
222    /**
223        @brief Interpolates smoothly between two values for a time between 0 and 1. The function starts slowly, increases faster and stops slowly again.
224        @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.
225        @param start The value at time = 0
226        @param end The value at time = 1
227        @return The smoothed interpolation at a given time
228    */
229    template <typename T>
230    T interpolateSmooth(float time, const T& start, const T& end)
231    {
232        return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start;
233    }
234
235    /**
236        @brief Returns a random number between 0 and almost 1: 0 <= rnd < 1.
237    */
238    inline float rnd()
239    {
240        return rand() / (RAND_MAX + 1.0);
241    }
242
243    /**
244        @brief Returns a random number between 0 and almost max: 0 <= rnd < max.
245        @param max The maximum
246    */
247    inline float rnd(float max)
248    {
249        return rnd() * max;
250    }
251
252    /**
253        @brief Returns a random number between min and almost max: min <= rnd < max.
254        @param min The minimum
255        @param max The maximum
256    */
257    inline float rnd(float min, float max)
258    {
259        return rnd(max - min) + min;
260    }
261
262    /**
263        @brief Returns randomly 1 or -1 with equal probability.
264    */
265    inline float rndsgn()
266    {
267        return ((rand() & 0x2) - 1); // rand() & 0x2 is either 2 or 0
268    }
269
270    _UtilExport unsigned long getUniqueNumber();
271
272    class IntVector2
273    {
274    public:
275      IntVector2() : x(0), y(0) { }
276      IntVector2(int _x, int _y) : x(_x), y(_y) { }
277      int x;
278      int y;
279    };
280
281    class IntVector3
282    {
283    public:
284      IntVector3() : x(0), y(0), z(0) { }
285      IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { }
286      int x;
287      int y;
288      int z;
289    };
290}
291
292#endif /* _Util_Math_H__ */
Note: See TracBrowser for help on using the repository browser.