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 | |
---|
36 | #include <OgreMath.h> |
---|
37 | #include <OgreVector2.h> |
---|
38 | #include <OgreVector3.h> |
---|
39 | #include <OgreVector4.h> |
---|
40 | #include <OgreMatrix3.h> |
---|
41 | #include <OgreQuaternion.h> |
---|
42 | #include <OgreColourValue.h> |
---|
43 | #include "SubString.h" |
---|
44 | |
---|
45 | namespace orxonox |
---|
46 | { |
---|
47 | typedef Ogre::Radian Radian; |
---|
48 | typedef Ogre::Degree Degree; |
---|
49 | typedef Ogre::Vector2 Vector2; |
---|
50 | typedef Ogre::Vector3 Vector3; |
---|
51 | typedef Ogre::Vector4 Vector4; |
---|
52 | typedef Ogre::Matrix3 Matrix3; |
---|
53 | typedef Ogre::Quaternion Quaternion; |
---|
54 | typedef Ogre::ColourValue ColourValue; |
---|
55 | } |
---|
56 | |
---|
57 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Radian& radian); |
---|
58 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Radian& radian); |
---|
59 | _UtilExport std::ostream& operator<<(std::ostream& out, const orxonox::Degree& degree); |
---|
60 | _UtilExport std::istream& operator>>(std::istream& in, orxonox::Degree& degree); |
---|
61 | |
---|
62 | _UtilExport float getAngle(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& otherposition); |
---|
63 | _UtilExport orxonox::Vector2 get2DViewdirection(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
64 | _UtilExport orxonox::Vector2 get2DViewcoordinates(const orxonox::Vector3& myposition, const orxonox::Vector3& mydirection, const orxonox::Vector3& myorthonormal, const orxonox::Vector3& otherposition); |
---|
65 | _UtilExport orxonox::Vector3 getPredictedPosition(const orxonox::Vector3& myposition, float projectilespeed, const orxonox::Vector3& targetposition, const orxonox::Vector3& targetvelocity); |
---|
66 | |
---|
67 | ///////////////////////////////////// |
---|
68 | // Conversion Functions of our own // |
---|
69 | ///////////////////////////////////// |
---|
70 | |
---|
71 | // Vector2 to std::string |
---|
72 | inline bool explicitConversion(std::string* output, const orxonox::Vector2& input) |
---|
73 | { |
---|
74 | std::ostringstream ostream; |
---|
75 | if (ostream << input.x << "," << input.y) |
---|
76 | { |
---|
77 | (*output) = ostream.str(); |
---|
78 | return true; |
---|
79 | } |
---|
80 | return false; |
---|
81 | } |
---|
82 | |
---|
83 | // Vector3 to std::string |
---|
84 | inline bool explicitConversion(std::string* output, const orxonox::Vector3& input) |
---|
85 | { |
---|
86 | std::ostringstream ostream; |
---|
87 | if (ostream << input.x << "," << input.y << "," << input.z) |
---|
88 | { |
---|
89 | (*output) = ostream.str(); |
---|
90 | return true; |
---|
91 | } |
---|
92 | return false; |
---|
93 | } |
---|
94 | |
---|
95 | // Vector4 to std::string |
---|
96 | inline bool explicitConversion(std::string* output, const orxonox::Vector4& input) |
---|
97 | { |
---|
98 | std::ostringstream ostream; |
---|
99 | if (ostream << input.x << "," << input.y << "," << input.z << "," << input.w) |
---|
100 | { |
---|
101 | (*output) = ostream.str(); |
---|
102 | return true; |
---|
103 | } |
---|
104 | return false; |
---|
105 | } |
---|
106 | |
---|
107 | // Quaternion to std::string |
---|
108 | inline bool explicitConversion(std::string* output, const orxonox::Quaternion& input) |
---|
109 | { |
---|
110 | std::ostringstream ostream; |
---|
111 | if (ostream << input.w << "," << input.x << "," << input.y << "," << input.z) |
---|
112 | { |
---|
113 | (*output) = ostream.str(); |
---|
114 | return true; |
---|
115 | } |
---|
116 | return false; |
---|
117 | } |
---|
118 | |
---|
119 | // ColourValue to std::string |
---|
120 | inline bool explicitConversion(std::string* output, const orxonox::ColourValue& input) |
---|
121 | { |
---|
122 | std::ostringstream ostream; |
---|
123 | if (ostream << input.r << "," << input.g << "," << input.b << "," << input.a) |
---|
124 | { |
---|
125 | (*output) = ostream.str(); |
---|
126 | return true; |
---|
127 | } |
---|
128 | return false; |
---|
129 | } |
---|
130 | |
---|
131 | // std::string to Vector2 |
---|
132 | _UtilExport bool explicitConversion(orxonox::Vector2* output, const std::string& input); |
---|
133 | // std::string to Vector3 |
---|
134 | _UtilExport bool explicitConversion(orxonox::Vector3* output, const std::string& input); |
---|
135 | // std::string to Vector4 |
---|
136 | _UtilExport bool explicitConversion(orxonox::Vector4* output, const std::string& input); |
---|
137 | // std::string to Quaternion |
---|
138 | _UtilExport bool explicitConversion(orxonox::Quaternion* output, const std::string& input); |
---|
139 | // std::string to ColourValue |
---|
140 | _UtilExport bool explicitConversion(orxonox::ColourValue* output, const std::string& input); |
---|
141 | |
---|
142 | template <typename T> |
---|
143 | inline T sgn(T x) |
---|
144 | { |
---|
145 | return (x >= 0) ? 1 : -1; |
---|
146 | } |
---|
147 | |
---|
148 | template <typename T> |
---|
149 | inline T min(T a, T b) |
---|
150 | { |
---|
151 | return (a <= b) ? a : b; |
---|
152 | } |
---|
153 | |
---|
154 | template <typename T> |
---|
155 | inline T max(T a, T b) |
---|
156 | { |
---|
157 | return (a >= b) ? a : b; |
---|
158 | } |
---|
159 | |
---|
160 | template <typename T> |
---|
161 | inline T clamp(T x, T min, T max) |
---|
162 | { |
---|
163 | if (x < min) |
---|
164 | return min; |
---|
165 | |
---|
166 | if (x > max) |
---|
167 | return max; |
---|
168 | |
---|
169 | return x; |
---|
170 | } |
---|
171 | |
---|
172 | template <typename T> |
---|
173 | inline T square(T x) |
---|
174 | { |
---|
175 | return x*x; |
---|
176 | } |
---|
177 | |
---|
178 | template <typename T> |
---|
179 | inline T cube(T x) |
---|
180 | { |
---|
181 | return x*x*x; |
---|
182 | } |
---|
183 | |
---|
184 | template <typename T> |
---|
185 | inline int floor(T x) |
---|
186 | { |
---|
187 | return (int)(x); |
---|
188 | } |
---|
189 | |
---|
190 | template <typename T> |
---|
191 | inline int ceil(T x) |
---|
192 | { |
---|
193 | int temp = floor(x); |
---|
194 | return (temp != x) ? (temp + 1) : temp; |
---|
195 | } |
---|
196 | |
---|
197 | template <typename T> |
---|
198 | inline int round(T x) |
---|
199 | { |
---|
200 | return (int)(x + 0.5); |
---|
201 | } |
---|
202 | |
---|
203 | template <typename T> |
---|
204 | inline int mod(T x, int max) |
---|
205 | { |
---|
206 | if (x >= 0) |
---|
207 | return (x % max); |
---|
208 | else |
---|
209 | return ((x % max) + max); |
---|
210 | } |
---|
211 | |
---|
212 | template <typename T> |
---|
213 | T interpolate(float time, const T& start, const T& end) |
---|
214 | { |
---|
215 | return time * (end - start) + start; |
---|
216 | } |
---|
217 | |
---|
218 | template <typename T> |
---|
219 | T interpolateSmooth(float time, const T& start, const T& end) |
---|
220 | { |
---|
221 | return (-2 * (end - start) * cube(time)) + (3 * (end - start) * square(time)) + start; |
---|
222 | } |
---|
223 | |
---|
224 | inline _UtilExport float rnd() |
---|
225 | { |
---|
226 | return ((float)rand() / RAND_MAX); |
---|
227 | } |
---|
228 | |
---|
229 | inline _UtilExport float rnd(float max) |
---|
230 | { |
---|
231 | return rnd() * max; |
---|
232 | } |
---|
233 | |
---|
234 | inline _UtilExport float rnd(float min, float max) |
---|
235 | { |
---|
236 | return rnd(max - min) + min; |
---|
237 | } |
---|
238 | |
---|
239 | class _UtilExport IntVector2 |
---|
240 | { |
---|
241 | public: |
---|
242 | IntVector2() : x(0), y(0) { } |
---|
243 | IntVector2(int _x, int _y) : x(_x), y(_y) { } |
---|
244 | int x; |
---|
245 | int y; |
---|
246 | }; |
---|
247 | |
---|
248 | class _UtilExport IntVector3 |
---|
249 | { |
---|
250 | public: |
---|
251 | IntVector3() : x(0), y(0), z(0) { } |
---|
252 | IntVector3(int _x, int _y, int _z) : x(_x), y(_y), z(_z) { } |
---|
253 | int x; |
---|
254 | int y; |
---|
255 | int z; |
---|
256 | }; |
---|
257 | |
---|
258 | #endif /* _Util_Math_H__ */ |
---|