Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ceguilua/src/util/Math.h @ 2144

Last change on this file since 2144 was 1791, checked in by landauf, 16 years ago

added comments to all my classes in util

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