Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 892 was 892, checked in by rgrieder, 16 years ago
  • commit test
File size: 3.2 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#ifndef _Math_H__
29#define _Math_H__
30
31#include <ostream>
32
33#include "UtilPrereqs.h"
34
35#include <OgreMath.h>
36#include <OgreVector2.h>
37#include <OgreVector3.h>
38#include <OgreVector4.h>
39#include <OgreMatrix3.h>
40#include <OgreQuaternion.h>
41#include <OgreColourValue.h>
42
43namespace orxonox
44{
45  typedef Ogre::Radian Radian;
46  typedef Ogre::Degree Degree;
47  typedef Ogre::Vector2 Vector2;
48  typedef Ogre::Vector3 Vector3;
49  typedef Ogre::Vector4 Vector4;
50  typedef Ogre::Matrix3 Matrix3;
51  typedef Ogre::Quaternion Quaternion;
52  typedef Ogre::ColourValue ColourValue;
53}
54
55_UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian);
56_UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian);
57_UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree);
58_UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree);
59
60template <typename T>
61inline T sgn(T x)
62{
63    return (x >= 0) ? 1 : -1;
64}
65
66template <typename T>
67inline T min(T a, T b)
68{
69    return (a <= b) ? a : b;
70}
71
72template <typename T>
73inline T max(T a, T b)
74{
75    return (a >= b) ? a : b;
76}
77
78template <typename T>
79inline T clamp(T x, T min, T max)
80{
81    if (x < min)
82        return min;
83
84    if (x > max)
85        return max;
86
87    return x;
88}
89
90template <typename T>
91inline T square(T x)
92{
93    return x*x;
94}
95
96template <typename T>
97inline T cube(T x)
98{
99    return x*x*x;
100}
101
102template <typename T>
103inline int floor(T x)
104{
105    return (int)(x);
106}
107
108template <typename T>
109inline int ceil(T x)
110{
111    int temp = floor(x);
112    return (temp != x) ? (temp + 1) : temp;
113}
114
115template <typename T>
116inline int round(T x)
117{
118    return (int)(x + 0.5);
119}
120
121template <typename T>
122T interpolate(float time, const T& start, const T& end)
123{
124    return time * (end - start) + start;
125}
126
127template <typename T>
128T interpolateSmooth(float time, const T& start, const T& end)
129{
130    return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start;
131}
132
133inline _UtilExport float rnd()
134{
135    return ((float)rand() / RAND_MAX);
136}
137
138inline _UtilExport float rnd(float max)
139{
140    return rnd() * max;
141}
142
143inline _UtilExport float rnd(float min, float max)
144{
145    return rnd(max - min) + min;
146}
147
148#endif /* _Math_H__ */
149
Note: See TracBrowser for help on using the repository browser.