Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1788 was 1781, checked in by rgrieder, 16 years ago
  • hack-fixed the colour of the radar dots.
  • dealt with some M$ windows hackery (who would ever macro-define min/max???)
  • Property svn:eol-style set to native
File size: 4.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#ifndef _Util_Math_H__
30#define _Util_Math_H__
31
32#include "UtilPrereqs.h"
33
34#include <ostream>
35#include <string>
36
37#include <OgreMath.h>
38#include <OgreVector2.h>
39#include <OgreVector3.h>
40#include <OgreVector4.h>
41#include <OgreMatrix3.h>
42#include <OgreMatrix4.h>
43#include <OgreQuaternion.h>
44#include <OgreColourValue.h>
45
46namespace orxonox
47{
48  typedef Ogre::Radian Radian;
49  typedef Ogre::Degree Degree;
50  typedef Ogre::Vector2 Vector2;
51  typedef Ogre::Vector3 Vector3;
52  typedef Ogre::Vector4 Vector4;
53  typedef Ogre::Matrix3 Matrix3;
54  typedef Ogre::Matrix4 Matrix4;
55  typedef Ogre::Quaternion Quaternion;
56  typedef Ogre::ColourValue ColourValue;
57}
58
59_UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian);
60_UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian);
61_UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree);
62_UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree);
63
64_UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition);
65_UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
66_UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition);
67_UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity);
68
69//Get around Windows hackery
70#if ORXONOX_PLATFORM == ORXONOX_PLATFORM_WIN32
71#  ifdef max
72#    undef max
73#  endif
74#  ifdef min
75#    undef min
76#  endif
77#endif
78
79template <typename T>
80inline T sgn(T x)
81{
82    return (x >= 0) ? 1 : -1;
83}
84
85template <typename T>
86inline T min(T a, T b)
87{
88    return (a <= b) ? a : b;
89}
90
91template <typename T>
92inline T max(T a, T b)
93{
94    return (a >= b) ? a : b;
95}
96
97template <typename T>
98inline T clamp(T x, T min, T max)
99{
100    if (x < min)
101        return min;
102
103    if (x > max)
104        return max;
105
106    return x;
107}
108
109template <typename T>
110inline T square(T x)
111{
112    return x*x;
113}
114
115template <typename T>
116inline T cube(T x)
117{
118    return x*x*x;
119}
120
121template <typename T>
122inline int floor(T x)
123{
124    return (int)(x);
125}
126
127template <typename T>
128inline int ceil(T x)
129{
130    int temp = floor(x);
131    return (temp != x) ? (temp + 1) : temp;
132}
133
134template <typename T>
135inline int round(T x)
136{
137    return (int)(x + 0.5);
138}
139
140template <typename T>
141inline int mod(T x, int max)
142{
143    if (x >= 0)
144        return (x % max);
145    else
146        return ((x % max) + max);
147}
148
149template <typename T>
150T interpolate(float time, const T& start, const T& end)
151{
152    return time * (end - start) + start;
153}
154
155template <typename T>
156T interpolateSmooth(float time, const T& start, const T& end)
157{
158    return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start;
159}
160
161inline _UtilExport float rnd()
162{
163    return ((float)rand() / RAND_MAX);
164}
165
166inline _UtilExport float rnd(float max)
167{
168    return rnd() * max;
169}
170
171inline _UtilExport float rnd(float min, float max)
172{
173    return rnd(max - min) + min;
174}
175
176_UtilExport unsigned long getUniqueNumber();
177_UtilExport std::string getUniqueNumberStr();
178
179class _UtilExport IntVector2
180{
181public:
182  IntVector2() : x(0), y(0) { }
183  IntVector2(int _x, int _y) : x(_x), y(_y) { }
184  int x;
185  int y;
186};
187
188class _UtilExport IntVector3
189{
190public:
191  IntVector3() : x(0), y(0), z(0) { }
192  IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { }
193  int x;
194  int y;
195  int z;
196};
197
198#endif /* _Util_Math_H__ */
Note: See TracBrowser for help on using the repository browser.