Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2119 was 2087, checked in by landauf, 16 years ago

merged objecthierarchy branch back to trunk

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