ScummVM API documentation
MathTypes.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (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, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Copyright (C) 2006-2010 - Frictional Games
24  *
25  * This file is part of HPL1 Engine.
26  */
27 
28 #ifndef HPL_MATHTYPES_H
29 #define HPL_MATHTYPES_H
30 
31 #include "common/array.h"
32 #include "common/list.h"
33 #include "hpl1/engine/math/Vector2.h"
34 #include "hpl1/engine/math/Vector3.h"
35 #include "hpl1/engine/math/hplMatrix.h"
36 #include "hpl1/engine/system/SystemTypes.h"
37 
38 namespace hpl {
39 
40 #define kPif (3.141592654f)
41 #define kPi2f (1.570796327f)
42 #define kPi4f (0.7853981634f)
43 #define k2Pif (6.283185307f)
44 
45 #define kEpsilonf (0.0001f)
46 
47 #define kSqrt2f (1.414213562f)
48 
49 enum eEulerRotationOrder {
50  eEulerRotationOrder_XYZ,
51  eEulerRotationOrder_XZY,
52  eEulerRotationOrder_YXZ,
53  eEulerRotationOrder_YZX,
54  eEulerRotationOrder_ZXY,
55  eEulerRotationOrder_ZYX,
56  eEulerRotationOrder_LastEnum,
57 };
58 
59 //-------------------------------------------
60 
62 public:
63  float max, min, val, up_speed, down_speed;
64 
66  cLinearOscillation(float afMin, float afMax, float afVal, float afUpSpeed, float afDownSpeed) {
67  SetUp(afMin, afMax, afVal, afUpSpeed, afDownSpeed);
68  }
69 
70  void SetUp(float afMin, float afMax, float afVal, float afUpSpeed, float afDownSpeed) {
71  min = afMin;
72  max = afMax;
73  val = afVal;
74 
75  up_speed = ABS(afUpSpeed);
76  down_speed = -ABS(afDownSpeed);
77 
78  add = up_speed;
79  }
80 
81  void Update(float afTimeStep) {
82  val += add * afTimeStep;
83  if (add > 0) {
84  if (val >= max) {
85  val = max;
86  add = down_speed;
87  }
88  } else {
89  if (val <= min) {
90  val = min;
91  add = up_speed;
92  }
93  }
94  }
95 
96 private:
97  float add;
98 };
99 
100 //-------------------------------------------
101 
102 template<class T>
103 class cPlane {
104 public:
105  T a, b, c, d;
106  cVector3<T> normal;
107 
109 
110  cPlane(T aA, T aB, T aC, T aD) {
111  a = aA;
112  b = aB;
113  c = aC;
114  d = aD;
115  }
116 
117  cPlane() {
118  a = 0;
119  b = 0;
120  c = 0;
121  d = 0;
122  }
123 
125 
126  cPlane(const cVector3<T> &avNormal, const cVector3<T> &avPoint) {
127  FromNormalPoint(avNormal, avPoint);
128  }
129 
131 
132  cPlane(const cVector3<T> &avPoint0, const cVector3<T> &avPoint1,
133  const cVector3<T> &avPoint2) {
134  FromPoints(avPoint0, avPoint1, avPoint2);
135  }
136 
138 
139  inline void FromNormalPoint(const cVector3<T> &avNormal, const cVector3<T> &avPoint) {
140  a = avNormal.x;
141  b = avNormal.y;
142  c = avNormal.z;
143 
144  // Dot product
145  d = -(avNormal.x * avPoint.x + avNormal.y * avPoint.y + avNormal.z * avPoint.z);
146  }
147 
149 
150  inline void FromPoints(const cVector3<T> &avPoint0, const cVector3<T> &avPoint1,
151  const cVector3<T> &avPoint2) {
152  cVector3<T> vEdge1 = avPoint1 - avPoint0;
153  cVector3<T> vEdge2 = avPoint2 - avPoint0;
154 
155  // Cross product
156  normal.x = vEdge1.y * vEdge2.z - vEdge1.z * vEdge2.y;
157  normal.y = vEdge1.z * vEdge2.x - vEdge1.x * vEdge2.z;
158  normal.z = vEdge1.x * vEdge2.y - vEdge1.y * vEdge2.x;
159 
160  normal.Normalise();
161 
162  a = normal.x;
163  b = normal.y;
164  c = normal.z;
165 
166  // Dot product
167  d = -(normal.x * avPoint0.x + normal.y * avPoint0.y + normal.z * avPoint0.z);
168  }
169 
171 
172  inline void Normalise() {
173  T fMag = sqrt(a * a + b * b + c * c);
174  a = a / fMag;
175  b = b / fMag;
176  c = c / fMag;
177  d = d / fMag;
178  }
179 
181 
182  inline void CalcNormal() {
183  normal = cVector3<T>(a, b, c);
184  normal.Normalise();
185  }
186 
188 
189  void FromVec(T *apV) {
190  a = apV[0];
191  b = apV[1];
192  c = apV[2];
193  d = apV[3];
194 
195  CalcNormal();
196  }
197 };
198 
199 typedef cPlane<float> cPlanef;
201 typedef tPlanefVec tPlanefVecIt;
202 
203 //-------------------------------------------
204 
205 template<class T>
206 class cSphere {
207 public:
208  T r;
209  cVector3<T> center;
210 
211  cSphere() {
212  }
213 
214  cSphere(cVector3<T> aV, T aR) {
215  center = aV;
216  r = aR;
217  }
218 };
219 
220 typedef cSphere<float> cSpheref;
221 
222 //-------------------------------------------
223 
224 template<class T>
225 class cRect2 {
226 public:
227  T x, y, w, h;
228  cRect2(T aX, T aY, T aW, T aH) {
229  x = aX;
230  y = aY;
231  w = aW;
232  h = aH;
233  }
234  cRect2(T aX, T aY) {
235  x = aX;
236  y = aY;
237  w = 0;
238  h = 0;
239  }
240  cRect2() {
241  x = 0;
242  y = 0;
243  w = 0;
244  h = 0;
245  }
246  cRect2(cVector2<T> aPos, cVector2<T> aSize) {
247  x = aPos.x;
248  y = aPos.y;
249  w = aSize.x;
250  h = aSize.y;
251  }
252 
253  void FromVec(T *apV) {
254  x = apV[0];
255  y = apV[1];
256  w = apV[2];
257  h = apV[3];
258  }
259 };
260 
261 typedef cRect2<float> cRect2f;
262 typedef cRect2<int> cRect2l;
264 typedef tRect2lList tRect2lListIt;
265 
266 //-------------------------------------------
267 
268 typedef cVector2<float> cVector2f;
269 typedef cVector2<int> cVector2l;
270 
271 typedef cMatrix<float> cMatrixf;
272 
274 typedef tMatrixfVec::iterator tMatrixfVecIt;
275 
278 
281 
282 typedef cVector3<float> cVector3f;
283 typedef cVector3<int> cVector3l;
284 
287 
289 typedef tVector2fVec::iterator tVector2fVecIt;
290 
293 
294 //-------------------------------------------
295 } // namespace hpl
296 
297 #include "hpl1/engine/math/Quaternion.h"
298 
299 #endif // HPL_MATHTYPES_H
Definition: MathTypes.h:225
Definition: AI.h:36
Definition: array.h:52
Definition: Vector2.h:36
Definition: list.h:44
cMatrixf * iterator
Definition: array.h:54
Definition: Vector3.h:38
Definition: MathTypes.h:103
T Normalise()
Definition: Vector3.h:299
Definition: MathTypes.h:206
Definition: list_intern.h:51
Definition: MathTypes.h:61
T ABS(T x)
Definition: util.h:56