Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/util/Math.h @ 3049

Last change on this file since 3049 was 2872, checked in by landauf, 15 years ago

some small adjustments in PongAI and related classes

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