ScummVM API documentation
Math.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_MATH_H
29#define HPL_MATH_H
30
31#include "hpl1/engine/math/BoundingVolume.h"
32#include "hpl1/engine/math/MathTypes.h"
33#include "hpl1/engine/math/MeshTypes.h"
34
35#include "hpl1/engine/graphics/Color.h"
36
37namespace hpl {
38
39class cMath {
40public:
44
50 static int RandRectl(int alMin, int alMax);
51
58 static float RandRectf(float alMin, float alMax);
59
63 static cVector2f RandRectVector2f(const cVector3f &avMin, const cVector3f &avMax);
64
68 static cVector3f RandRectVector3f(const cVector3f &avMin, const cVector3f &avMax);
69
73 static cColor RandRectColor(const cColor &aMin, const cColor &aMax);
74
79 static void Randomize(int alSeed = -1);
80
84
85 static bool BoxCollision(cRect2l aRect1, cRect2l aRect2);
86 static bool BoxCollision(cRect2f aRect1, cRect2f aRect2);
87
88 static bool PointBoxCollision(cVector2f avPoint, cRect2f aRect);
89
90 static bool BoxFit(cRect2l aRectSrc, cRect2l aRectDest);
91 static bool BoxFit(cRect2f aRectSrc, cRect2f aRectDest);
92
93 static float Dist2D(const cVector2f &avPosA, const cVector2f &avPosB);
94 static float Dist2D(const cVector3f &avPosA, const cVector3f &avPosB);
95
96 static float SqrDist2D(const cVector2f &avPosA, const cVector2f &avPosB);
97 static float SqrDist2D(const cVector3f &avPosA, const cVector3f &avPosB);
98
99 static cRect2f &ClipRect(cRect2f &aRectSrc, const cRect2f &aRectDest);
100
106
107 static bool PointBVCollision(const cVector3f &avPoint, cBoundingVolume &aBV2);
108
113 static bool GetClipRectFromBV(cRect2l &aDestRect, cBoundingVolume &aBV,
114 const cMatrixf &a_mtxView, const cMatrixf &a_mtxProj,
115 float afNearClipPlane, const cVector2l &avScreenSize);
116
117 static bool CheckSphereInPlanes(const cVector3f &avCenter, float afRadius,
118 const cPlanef *apPlanes, int alPlaneCount);
119
123
129 static float GetFraction(float afVal);
136 static float Modulus(float afDividend, float afDivisor);
137
138 static float ToRad(float afAngle);
139 static float ToDeg(float afAngle);
140
144 static int Log2ToInt(int alX);
145
149 static bool IsPow2(int alX);
150
156 static float Wrap(float afX, float afMin, float afMax);
157
161 static float Clamp(float afX, float afMin, float afMax);
162
163 inline static float Max(float afX, float afY) {
164 if (afX > afY)
165 return afX;
166 return afY;
167 }
168 inline static float Min(float afX, float afY) {
169 if (afX < afY)
170 return afX;
171 return afY;
172 }
173
174 inline static int Max(int alX, int alY) {
175 if (alX > alY)
176 return alX;
177 return alY;
178 }
179 inline static int Min(int alX, int alY) {
180 if (alX < alY)
181 return alX;
182 return alY;
183 }
184
185 inline static float Abs(float afX) { return afX < 0 ? -afX : afX; }
186 inline static int Abs(int alX) { return alX < 0 ? -alX : alX; }
187
188 static float GetAngleDistance(float afAngle1, float afAngle2, float afMaxAngle);
189 static float GetAngleDistanceRad(float afAngle1, float afAngle2);
190 static float GetAngleDistanceDeg(float afAngle1, float afAngle2);
191
192 static float TurnAngle(float afAngle, float afFinalAngle, float afSpeed, float afMaxAngle);
193 static float TurnAngleRad(float afAngle, float afFinalAngle, float afSpeed);
194 static float TurnAngleDeg(float afAngle, float afFinalAngle, float afSpeed);
195
196 static float InterpolateFloat(float afA, float afB, float afT);
197
198 template<typename Vector>
199 static decltype(Vector::x) & GetVectorX(Vector &v) {
200 return v.x;
201 }
202
203 template<typename Vector>
204 static decltype(Vector::y) & GetVectorY(Vector &v) {
205 return v.y;
206 }
207 template<typename Vector>
208 static decltype(Vector::z) & GetVectorZ(Vector &v) {
209 return v.z;
210 }
211
215
221 static float GetAngleFromPoints2D(const cVector2f &aStartPos, const cVector2f &avGoalPos);
228 static cVector2f GetVectorFromAngle2D(float afAngle, float afLength);
235 static void GetAngleFromVector(const cVector2f &avVec, float *apAngle, float *apLength);
236
243 static cVector2f ProjectVector2D(const cVector2f &avSrcVec, const cVector2f &avDestVec);
244
248
249 static inline cVector3f Vector3ToRad(const cVector3f &avVec) {
250 return cVector3f(ToRad(avVec.x), ToRad(avVec.y), ToRad(avVec.z));
251 }
252
253 static inline cVector3f Vector3ToDeg(const cVector3f &avVec) {
254 return cVector3f(ToDeg(avVec.x), ToDeg(avVec.y), ToDeg(avVec.z));
255 }
256
257 static inline cVector3f Vector3Normalize(const cVector3f &avVec) {
258 cVector3f vNorm = avVec;
259 vNorm.Normalise();
260 return vNorm;
261 }
262
263 static inline float Vector3DistSqr(const cVector3f &avStartPos, const cVector3f &avEndPos) {
264 float fDX = avEndPos.x - avStartPos.x;
265 float fDY = avEndPos.y - avStartPos.y;
266 float fDZ = avEndPos.z - avStartPos.z;
267
268 return fDX * fDX + fDY * fDY + fDZ * fDZ;
269 }
270
271 static inline float Vector3Dist(const cVector3f &avStartPos, const cVector3f &avEndPos) {
272 return sqrt(Vector3DistSqr(avStartPos, avEndPos));
273 }
274
275 static cVector3f GetAngleFromPoints3D(const cVector3f &avStartPos, const cVector3f &avGoalPos);
276
283 static cVector3f Vector3Cross(const cVector3f &avVecA, const cVector3f &avVecB);
284
291 static float Vector3Dot(const cVector3f &avVecA, const cVector3f &avVecB);
292
299 static cVector3f ProjectVector3D(const cVector3f &avSrcVec, const cVector3f &avDestVec);
300
307 static float Vector3Angle(const cVector3f &avVecA, const cVector3f &avVecB);
308
312 static cVector3f Vector3UnProject(const cVector3f &avVec, const cRect2f &aScreenRect,
313 cMatrixf a_mtxViewProj);
314
321 static float PlaneToPointDist(const cPlanef &aPlane, const cVector3f &avVec);
322
330 static void PlaneIntersectionLine(const cPlanef &aPA, const cPlanef &aPB,
331 cVector3f &avDir, cVector3f &avPoint);
332
336 static bool CheckFrustumLineIntersection(const cPlanef *apPlanePairs, const cVector3f &avPoint1,
337 const cVector3f &avPoint2, int alPairNum);
338
343 static bool CheckFrustumQuadMeshIntersection(const cPlanef *apPlanePairs, tVector3fVec *apPoints,
344 int alPairNum);
345
349
356 static cQuaternion QuaternionSlerp(float afT, const cQuaternion &aqA, const cQuaternion &aqB,
357 bool abShortestPath);
358
359 static float QuaternionDot(const cQuaternion &aqA, const cQuaternion &aqB);
360
364
371 static cMatrixf MatrixSlerp(float afT, const cMatrixf &a_mtxA, const cMatrixf &a_mtxB,
372 bool abShortestPath);
373
377 static cMatrixf MatrixMul(const cMatrixf &a_mtxA, const cMatrixf &a_mtxB);
381 static cVector3f MatrixMul(const cMatrixf &a_mtxA, const cVector3f &avB);
385 static cVector3f MatrixMulDivideW(const cMatrixf &a_mtxA, const cVector3f &avB);
386
390 static cMatrixf MatrixMulScalar(const cMatrixf &a_mtxA, float afB);
391
395 static cMatrixf MatrixRotate(cVector3f avRot, eEulerRotationOrder aOrder);
399 static cMatrixf MatrixRotateX(float afAngle);
403 static cMatrixf MatrixRotateY(float afAngle);
407 static cMatrixf MatrixRotateZ(float afAngle);
408 static cMatrixf MatrixScale(cVector3f avScale);
409 static cMatrixf MatrixTranslate(cVector3f avTrans);
410
416
420 static inline float MatrixMinor(const cMatrixf &a_mtxA,
421 const size_t r0, const size_t r1, const size_t r2,
422 const size_t c0, const size_t c1, const size_t c2);
426 static inline cMatrixf MatrixAdjoint(const cMatrixf &a_mtxA);
427
431 static inline float MatrixDeterminant(const cMatrixf &a_mtxA);
432
436 static cMatrixf MatrixInverse(const cMatrixf &a_mtxA);
437
443 static cVector3f MatrixToEulerAngles(const cMatrixf &a_mtxA, eEulerRotationOrder aOrder);
444
448 static const char *MatrixToChar(const cMatrixf &a_mtxA);
449
453
465 static bool CreateTriTangentVectors(float *apDestArray,
466 const unsigned int *apIndexArray, int alIndexNum,
467 const float *apVertexArray, int alVtxStride,
468 const float *apTexArray,
469 const float *apNormalArray, int alVertexNum);
470
480 static bool CreateTriangleData(tTriangleDataVec &avTriangles,
481 const unsigned int *apIndexArray, int alIndexNum,
482 const float *apVertexArray, int alVtxStride, int alVertexNum);
483
493 static bool CreateEdges(tTriEdgeVec &avEdges,
494 const unsigned int *apIndexArray, int alIndexNum,
495 const float *apVertexArray, int alVtxStride, int alVertexNum,
496 bool *apIsDoubleSided);
497};
498
499} // namespace hpl
500
501#endif // HPL_MATH_H
Definition: BoundingVolume.h:71
Definition: Color.h:37
Definition: Math.h:39
static float Modulus(float afDividend, float afDivisor)
static void PlaneIntersectionLine(const cPlanef &aPA, const cPlanef &aPB, cVector3f &avDir, cVector3f &avPoint)
static bool CreateTriangleData(tTriangleDataVec &avTriangles, const unsigned int *apIndexArray, int alIndexNum, const float *apVertexArray, int alVtxStride, int alVertexNum)
static cQuaternion QuaternionSlerp(float afT, const cQuaternion &aqA, const cQuaternion &aqB, bool abShortestPath)
static const char * MatrixToChar(const cMatrixf &a_mtxA)
static bool CheckCollisionBV(cBoundingVolume &aBV1, cBoundingVolume &aBV2)
static cVector3f Vector3Cross(const cVector3f &avVecA, const cVector3f &avVecB)
static float Vector3Angle(const cVector3f &avVecA, const cVector3f &avVecB)
static cVector2f RandRectVector2f(const cVector3f &avMin, const cVector3f &avMax)
static void Randomize(int alSeed=-1)
static float PlaneToPointDist(const cPlanef &aPlane, const cVector3f &avVec)
static cMatrixf MatrixRotateZ(float afAngle)
static cColor RandRectColor(const cColor &aMin, const cColor &aMax)
static cVector3f MatrixToEulerAngles(const cMatrixf &a_mtxA, eEulerRotationOrder aOrder)
static float Vector3Dot(const cVector3f &avVecA, const cVector3f &avVecB)
static bool CreateTriTangentVectors(float *apDestArray, const unsigned int *apIndexArray, int alIndexNum, const float *apVertexArray, int alVtxStride, const float *apTexArray, const float *apNormalArray, int alVertexNum)
static bool CheckFrustumQuadMeshIntersection(const cPlanef *apPlanePairs, tVector3fVec *apPoints, int alPairNum)
static bool CreateEdges(tTriEdgeVec &avEdges, const unsigned int *apIndexArray, int alIndexNum, const float *apVertexArray, int alVtxStride, int alVertexNum, bool *apIsDoubleSided)
static cVector3f MatrixMul(const cMatrixf &a_mtxA, const cVector3f &avB)
static float RandRectf(float alMin, float alMax)
static cMatrixf MatrixInverse(const cMatrixf &a_mtxA)
static cMatrixf MatrixRotateY(float afAngle)
static bool CheckFrustumLineIntersection(const cPlanef *apPlanePairs, const cVector3f &avPoint1, const cVector3f &avPoint2, int alPairNum)
static bool GetClipRectFromBV(cRect2l &aDestRect, cBoundingVolume &aBV, const cMatrixf &a_mtxView, const cMatrixf &a_mtxProj, float afNearClipPlane, const cVector2l &avScreenSize)
static cVector3f ProjectVector3D(const cVector3f &avSrcVec, const cVector3f &avDestVec)
static int RandRectl(int alMin, int alMax)
static cVector2f ProjectVector2D(const cVector2f &avSrcVec, const cVector2f &avDestVec)
static float MatrixDeterminant(const cMatrixf &a_mtxA)
static int Log2ToInt(int alX)
static float Wrap(float afX, float afMin, float afMax)
static cVector3f Vector3UnProject(const cVector3f &avVec, const cRect2f &aScreenRect, cMatrixf a_mtxViewProj)
static cMatrixf MatrixMulScalar(const cMatrixf &a_mtxA, float afB)
static cVector3f MatrixMulDivideW(const cMatrixf &a_mtxA, const cVector3f &avB)
static cMatrixf MatrixRotateX(float afAngle)
static cMatrixf MatrixMul(const cMatrixf &a_mtxA, const cMatrixf &a_mtxB)
static cMatrixf MatrixAdjoint(const cMatrixf &a_mtxA)
static float GetFraction(float afVal)
static float GetAngleFromPoints2D(const cVector2f &aStartPos, const cVector2f &avGoalPos)
static cMatrixf MatrixSlerp(float afT, const cMatrixf &a_mtxA, const cMatrixf &a_mtxB, bool abShortestPath)
static bool IsPow2(int alX)
static cMatrixf MatrixRotate(cVector3f avRot, eEulerRotationOrder aOrder)
static float MatrixMinor(const cMatrixf &a_mtxA, const size_t r0, const size_t r1, const size_t r2, const size_t c0, const size_t c1, const size_t c2)
static cVector2f GetVectorFromAngle2D(float afAngle, float afLength)
static cMatrixf MatrixQuaternion(const cQuaternion &aqRot)
static cVector3f RandRectVector3f(const cVector3f &avMin, const cVector3f &avMax)
static float Clamp(float afX, float afMin, float afMax)
static void GetAngleFromVector(const cVector2f &avVec, float *apAngle, float *apLength)
Definition: Quaternion.h:36
T Normalise()
Definition: Vector3.h:299