UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
mathlib.h
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 1997-2001 Id Software, Inc.
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.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23 */
24 
25 #pragma once
26 
27 #include "ufotypes.h"
28 #include "defines.h"
29 #include <algorithm>
30 #include <cstdlib>
31 #include <cmath>
32 
33 #ifndef M_PI
34 #define M_PI 3.14159265358979323846 /* matches value in gcc v2 math.h */
35 #endif
36 #ifndef M_PI_2
37 #define M_PI_2 1.57079632679489661923 /* pi/2 */
38 #endif
39 
40 #define EQUAL_EPSILON 0.001
41 
42 bool Q_IsPowerOfTwo(int i);
43 
44 /* Compare floats with custom epsilon error */
45 #define EQUAL2(a,b,epsilon) (fabs((a)-(b))<epsilon)
46 
47 /* microsoft's fabs seems to be ungodly slow... */
48 #define Q_ftol(f) (long) (f)
49 
50 #define torad (M_PI/180.0f)
51 #define todeg (180.0f/M_PI)
52 
53 /* angle indexes */
54 #define PITCH 0 /* rotation around y axis - up / down (-90 up to 90 degree) */
55 #define YAW 1 /* rotation around z axis - left / right (0 up to 360 degree) */
56 #define ROLL 2 /* rotation around x axis - fall over */
57 
58 /* axis */
59 #define AXIS_FORWARD 0
60 #define AXIS_RIGHT 1
61 #define AXIS_UP 2
62 
63 /* earth map data */
64 /* values of sinus and cosinus of earth inclination (23,5 degrees) for faster day and night calculations */
65 #define SIN_ALPHA 0.39875
66 #define COS_ALPHA 0.91706
67 #define HIGH_LAT +1.0
68 #define LOW_LAT -1.0
69 #define CENTER_LAT 0.0
70 #define SIZE_LAT 2.0
71 
78 #define DIRECTIONS 8
79 
84 #define BASE_DIRECTIONS 4 /* Only the standard N,S,E,W directions */
85 
86 /* game/g_ai.c, game/g_spawn.c, common/routing.c, ufo2map/routing.c, client/cl_actor.c, common/cmodel.c, shared/typedefs.h */
87 #define PATHFINDING_DIRECTIONS 40 /* total number of directions */
88 #define CORE_DIRECTIONS 8 /* The standard N,S,E,W directions plus diagonals. */
89 #define FLYING_DIRECTIONS 16 /* starting number of directions available only to fliers */
90 
91 extern const vec4_t dvecs[PATHFINDING_DIRECTIONS];
92 extern const float dvecsn[CORE_DIRECTIONS][2];
93 extern const float directionAngles[CORE_DIRECTIONS];
94 
95 extern const byte dvright[CORE_DIRECTIONS];
96 extern const byte dvleft[CORE_DIRECTIONS];
97 
100 #define VecToPos(v, p) ( \
101  (p)[0] = ((int)(v)[0] + MAX_WORLD_WIDTH) / UNIT_SIZE, \
102  (p)[1] = ((int)(v)[1] + MAX_WORLD_WIDTH) / UNIT_SIZE, \
103  (p)[2] = std::min((PATHFINDING_HEIGHT - 1), ((int)(v)[2] / UNIT_HEIGHT)) \
104 )
105 
110 #define PosToVec(p, v) ( \
111  (v)[0] = ((int)(p)[0] - GRID_WIDTH) * UNIT_SIZE + UNIT_SIZE / 2, \
112  (v)[1] = ((int)(p)[1] - GRID_WIDTH) * UNIT_SIZE + UNIT_SIZE / 2, \
113  (v)[2] = (int)(p)[2] * UNIT_HEIGHT + UNIT_HEIGHT / 2 \
114 )
115 
116 #include "vector.h"
117 #include "line.h"
118 #include "aabb.h"
119 
120 class GridBox {
121 public:
122  static const GridBox EMPTY;
123 
124  /*==================
125  * ctors
126  *==================*/
130  }
131  GridBox(const ipos3_t mini, const ipos3_t maxi) {
132  VectorCopy(mini, mins);
133  VectorCopy(maxi, maxs);
134  }
135  GridBox(const pos3_t mini, const pos3_t maxi) {
136  VectorCopy(mini, mins);
137  VectorCopy(maxi, maxs);
138  }
139 #if 1
140  GridBox(const AABB& aabb) {
141  VecToPos(aabb.getMins(), mins);
142  VecToPos(aabb.getMaxs(), maxs);
143  }
144 #endif
145  /*==================
146  * setters
147  *==================*/
148  inline void set(const pos3_t mini, const pos3_t maxi) {
149  VectorCopy(mini, mins);
150  VectorCopy(maxi, maxs);
151  }
152  inline void set(const AABB& aabb) {
153  VecToPos(aabb.getMins(), mins);
154  VecToPos(aabb.getMaxs(), maxs);
155  }
156 
163  inline void setFromMapBounds(const vec3_t mini, const vec3_t maxi) {
164  VecToPos(mini, mins);
165  VecToPos(maxi, maxs);
166  maxs[0]--;
167  maxs[1]--;
168  maxs[2]--;
169  }
170 
171  /*==================
172  * getters
173  *==================*/
174  inline pos_t getMinX () const {
175  return mins[0];
176  }
177  inline pos_t getMinY () const {
178  return mins[1];
179  }
180  inline pos_t getMinZ () const {
181  return mins[2];
182  }
183  inline pos_t getMaxX () const {
184  return maxs[0];
185  }
186  inline pos_t getMaxY () const {
187  return maxs[1];
188  }
189  inline pos_t getMaxZ () const {
190  return maxs[2];
191  }
192 
193  /*==================
194  * checkers
195  *==================*/
196  inline bool isZero() const {
197  return VectorIntZero(mins) && VectorIntZero(maxs);
198  }
199 
200  /*==================
201  * manipulators
202  *==================*/
206  inline void expandXY(const int byVal) {
207  mins[0] = std::max(mins[0] - byVal, 0);
208  mins[1] = std::max(mins[1] - byVal, 0);
209  maxs[0] = std::min(maxs[0] + byVal, PATHFINDING_WIDTH - 1);
210  maxs[1] = std::min(maxs[1] + byVal, PATHFINDING_WIDTH - 1);
211  }
212  inline void addOneZ () {
213  maxs[2] = std::min(maxs[2] + 1, PATHFINDING_HEIGHT - 1);
214  }
215  inline void clipToMaxBoundaries() {
216  return; /* do nothing, see above */
217  }
218 
219  /*==================
220  * data
221  *==================*/
224 };
225 
236 typedef short dvec_t;
237 #define DV_FLAGS_BIT_SHIFT 4
238 #define DV_DIR_BIT_SHIFT 8
239 #define DV_Z_BIT_MASK 0x0007
240 #define DV_FLAGS_BIT_MASK 0x00F0
241 #define DV_DIR_BIT_MASK 0xFF00
243 #define DV_FLAG_AUTOCROUCH 0x01
244 #define DV_FLAG_AUTOCROUCHED 0x02
245 #define DV_FLAG_AUTODIVE 0x04
246 
247 #define makeDV(dir, z) (((dir) << DV_DIR_BIT_SHIFT) | ((z) & DV_Z_BIT_MASK))
248 #define setDVz(dv, z) (((dv) & (~DV_Z_BIT_MASK)) | ((z) & DV_Z_BIT_MASK))
249 #define getDVdir(dv) ((dv) >> DV_DIR_BIT_SHIFT)
250 #define getDVflags(dv) (((dv) & DV_FLAGS_BIT_MASK) >> DV_FLAGS_BIT_SHIFT)
251 #define getDVz(dv) ((dv) & DV_Z_BIT_MASK)
252 
253 #define PosAddDV(p, crouch, dv) ((p)[0]+=dvecs[getDVdir(dv)][0], (p)[1]+=dvecs[getDVdir(dv)][1], (p)[2]=getDVz(dv), (crouch)+=dvecs[getDVdir(dv)][3])
254 #define PosSubDV(p, crouch, dv) ((p)[0]-=dvecs[getDVdir(dv)][0], (p)[1]-=dvecs[getDVdir(dv)][1], (p)[2]=getDVz(dv), (crouch)-=dvecs[getDVdir(dv)][3])
255 
256 int AngleToDir(int angle);
257 #define AngleToDV(x) (AngleToDir(x) << DV_DIR_BIT_SHIFT)
258 
259 void VectorMA(const vec3_t veca, const float scale, const vec3_t vecb, vec3_t outVector);
260 void VectorClampMA(vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc);
261 void VectorMix(const vec3_t v1, const vec3_t v2, const float mix, vec3_t out);
262 
263 void MatrixMultiply(const vec3_t a[3], const vec3_t b[3], vec3_t c[3]);
264 void GLMatrixAssemble(const vec3_t origin, const vec3_t angles, float* matrix);
265 void GLMatrixMultiply(const float a[16], const float b[16], float c[16]);
266 void GLVectorTransform(const float m[16], const vec4_t in, vec4_t out);
267 void GLPositionTransform(const float m[16], const vec3_t in, vec3_t out);
268 void VectorRotate(vec3_t m[3], const vec3_t va, vec3_t vb);
269 
270 void ClearBounds(vec3_t mins, vec3_t maxs);
271 void AddPointToBounds(const vec3_t v, vec3_t mins, vec3_t maxs);
272 int VectorCompareEps(const vec3_t v1, const vec3_t v2, float epsilon);
273 bool VectorNearer(const vec3_t v1, const vec3_t v2, const vec3_t comp);
274 vec_t VectorLength(const vec3_t v);
275 void CrossProduct(const vec3_t v1, const vec3_t v2, vec3_t cross);
276 vec_t VectorNormalize(vec3_t v); /* returns vector length */
278 vec_t VectorNormalize2(const vec3_t v, vec3_t out);
279 void VectorInverse(vec3_t v);
280 void VectorMidpoint(const vec3_t point1, const vec3_t point2, vec3_t midpoint);
281 int Q_log2(int val);
282 
283 double GetDistanceOnGlobe(const vec2_t pos1, const vec2_t pos2);
284 
285 void VectorCalcMinsMaxs(const vec3_t center, const vec3_t size, vec3_t mins, vec3_t maxs);
286 float VectorAngleBetween(const vec3_t vec1, const vec3_t vec2);
287 
288 void VecToAngles(const vec3_t vec, vec3_t angles);
289 
290 void VecToPolar(const vec3_t v, vec2_t a);
291 void PolarToVec(const vec2_t a, vec3_t v);
292 
293 void CalculateMinsMaxs(const vec3_t angles, const AABB& relBox, const vec3_t origin, AABB& absBox);
294 
295 void VectorCreateRotationMatrix(const vec3_t angles, vec3_t matrix[3]);
296 void VectorRotatePoint(vec3_t point, vec3_t matrix[3]);
297 
298 void AngleVectors(const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up);
299 float AngleNormalize360(float angle);
300 float AngleNormalize180(float angle);
301 
302 float LerpAngle(float a1, float a2, float frac);
303 
304 bool FrustumVis(const vec3_t origin, int dir, const vec3_t point);
305 
306 void PerpendicularVector(vec3_t dst, const vec3_t src);
307 void RotatePointAroundVector(vec3_t dst, const vec3_t dir, const vec3_t point, float degrees);
308 
309 float frand(void); /* 0 to 1 */
310 float crand(void); /* -1 to 1 */
311 void gaussrand(float* gauss1, float* gauss2); /* -inf to +inf, median 0, stdev 1 */
312 
313 vec_t Q_rint(const vec_t in);
314 vec_t ColorNormalize(const vec3_t in, vec3_t out);
315 
316 void TangentVectors(const vec3_t normal, const vec3_t sdir, const vec3_t tdir, vec4_t tangent, vec3_t binormal);
317 
318 void Orthogonalize(vec3_t v1, const vec3_t v2);
319 void MatrixTranspose(const vec3_t m[3], vec3_t t[3]);
320 
321 bool RayIntersectAABB(const vec3_t start, const vec3_t end, const AABB& aabb);
void set(const AABB &aabb)
Definition: mathlib.h:152
void set(const pos3_t mini, const pos3_t maxi)
Definition: mathlib.h:148
#define VectorCopy(src, dest)
Definition: vector.h:51
double GetDistanceOnGlobe(const vec2_t pos1, const vec2_t pos2)
Calculate distance on the geoscape.
Definition: mathlib.cpp:171
void VectorMidpoint(const vec3_t point1, const vec3_t point2, vec3_t midpoint)
Calculates the midpoint between two vectors.
Definition: mathlib.cpp:473
pos_t getMinZ() const
Definition: mathlib.h:180
void VectorRotate(vec3_t m[3], const vec3_t va, vec3_t vb)
Rotate a vector with a rotation matrix.
Definition: mathlib.cpp:395
const byte dvleft[CORE_DIRECTIONS]
Definition: mathlib.cpp:119
void VectorNormalizeFast(vec3_t v)
fast vector normalize routine that does not check to make sure that length != 0, nor does it return l...
Definition: mathlib.cpp:762
void GLMatrixAssemble(const vec3_t origin, const vec3_t angles, float *matrix)
Builds an opengl translation and rotation matrix.
Definition: mathlib.cpp:325
GridBox(const ipos3_t mini, const ipos3_t maxi)
Definition: mathlib.h:131
void expandXY(const int byVal)
expand the box in four directions, but clip them to the maximum boundaries
Definition: mathlib.h:206
pos_t getMaxY() const
Definition: mathlib.h:186
void GLPositionTransform(const float m[16], const vec3_t in, vec3_t out)
Transform position (xyz) vector by OpenGL rules.
Definition: mathlib.cpp:380
const char * va(const char *format,...)
does a varargs printf into a temp buffer, so I don't need to have varargs versions of all text functi...
Definition: shared.cpp:410
pos_t getMaxX() const
Definition: mathlib.h:183
void TangentVectors(const vec3_t normal, const vec3_t sdir, const vec3_t tdir, vec4_t tangent, vec3_t binormal)
Projects the normalized directional vectors on to the normal's plane. The fourth component of the res...
Definition: mathlib.cpp:1057
bool FrustumVis(const vec3_t origin, int dir, const vec3_t point)
Checks whether a point is visible from a given position.
Definition: mathlib.cpp:666
void CalculateMinsMaxs(const vec3_t angles, const AABB &relBox, const vec3_t origin, AABB &absBox)
Calculates the bounding box in absolute coordinates, also for rotating objects. WARNING: do not use t...
Definition: mathlib.cpp:546
static const vec3_t scale
int VectorCompareEps(const vec3_t v1, const vec3_t v2, float epsilon)
Compare two vectors that may have an epsilon difference but still be the same vectors.
Definition: mathlib.cpp:413
voidpf uLong int origin
Definition: ioapi.h:45
const vec4_t dvecs[PATHFINDING_DIRECTIONS]
Definition: mathlib.cpp:58
Definition: aabb.h:42
void VectorMix(const vec3_t v1, const vec3_t v2, const float mix, vec3_t out)
Calculate a position on v1 v2 line.
Definition: mathlib.cpp:447
const vec3_t vec3_origin
Definition: mathlib.cpp:35
float vec_t
Definition: ufotypes.h:37
vec_t VectorLength(const vec3_t v)
Calculate the length of a vector.
Definition: mathlib.cpp:434
void Orthogonalize(vec3_t v1, const vec3_t v2)
Grahm-Schmidt orthogonalization.
Definition: mathlib.cpp:1088
void VecToPolar(const vec3_t v, vec2_t a)
Converts vector coordinates into polar coordinates.
Definition: mathlib.cpp:922
bool Q_IsPowerOfTwo(int i)
Checks whether i is power of two value.
Definition: mathlib.cpp:972
float VectorAngleBetween(const vec3_t vec1, const vec3_t vec2)
Calculates the angle (in radians) between the two given vectors.
Definition: mathlib.cpp:484
void VectorClampMA(vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc)
Definition: mathlib.cpp:268
void VectorCalcMinsMaxs(const vec3_t center, const vec3_t size, vec3_t mins, vec3_t maxs)
Calculates a bounding box from a center and a size.
Definition: mathlib.cpp:1019
float AngleNormalize180(float angle)
returns angle normalized to the range [-180 < angle <= 180]
Definition: mathlib.cpp:1004
vec_t VectorNormalize(vec3_t v)
Calculate unit vector for a given vec3_t.
Definition: mathlib.cpp:745
pos3_t mins
Definition: mathlib.h:222
const byte dvright[CORE_DIRECTIONS]
Definition: mathlib.cpp:116
GridBox(const pos3_t mini, const pos3_t maxi)
Definition: mathlib.h:135
const float dvecsn[CORE_DIRECTIONS][2]
Definition: mathlib.cpp:102
vec_t ColorNormalize(const vec3_t in, vec3_t out)
Definition: mathlib.cpp:190
void VectorCreateRotationMatrix(const vec3_t angles, vec3_t matrix[3])
Definition: mathlib.cpp:592
float frand(void)
Return random values between 0 and 1.
Definition: mathlib.cpp:506
ipos_t ipos3_t[3]
Definition: ufotypes.h:70
vec_t Q_rint(const vec_t in)
Round to nearest integer.
Definition: mathlib.cpp:156
void MatrixMultiply(const vec3_t a[3], const vec3_t b[3], vec3_t c[3])
Multiply 3*3 matrix by 3*3 matrix.
Definition: mathlib.cpp:304
#define PATHFINDING_WIDTH
absolute max
Definition: defines.h:292
int AngleToDir(int angle)
Returns the index of array directionAngles[DIRECTIONS] whose value is the closest to angle...
Definition: mathlib.cpp:130
bool VectorNearer(const vec3_t v1, const vec3_t v2, const vec3_t comp)
Checks whether the given vector v1 is closer to comp as the vector v2.
Definition: mathlib.cpp:219
float LerpAngle(float a1, float a2, float frac)
Returns the angle resulting from turning fraction * angle from angle1 to angle2.
Definition: mathlib.cpp:981
GridBox()
Definition: mathlib.h:127
int Q_log2(int val)
Definition: mathlib.cpp:492
GLsizei size
Definition: r_gl.h:152
void PerpendicularVector(vec3_t dst, const vec3_t src)
Finds a vector perpendicular to the source vector.
Definition: mathlib.cpp:780
#define PATHFINDING_DIRECTIONS
Definition: mathlib.h:87
void VectorMA(const vec3_t veca, const float scale, const vec3_t vecb, vec3_t outVector)
Sets vector_out (vc) to vevtor1 (va) + scale * vector2 (vb)
Definition: mathlib.cpp:261
void GLVectorTransform(const float m[16], const vec4_t in, vec4_t out)
Multiply 4*4 matrix by 4d vector.
Definition: mathlib.cpp:366
void MatrixTranspose(const vec3_t m[3], vec3_t t[3])
Transposes m and stores the result in t.
Definition: mathlib.cpp:1101
pos_t getMinY() const
Definition: mathlib.h:177
void gaussrand(float *gauss1, float *gauss2)
generate two gaussian distributed random numbers with median at 0 and stdev of 1
Definition: mathlib.cpp:529
pos_t getMinX() const
Definition: mathlib.h:174
Defined CONSTANTS (Macros are elsewhere)
#define VecToPos(v, p)
Map boundary is +/- MAX_WORLD_WIDTH - to get into the positive area we add the possible max negative ...
Definition: mathlib.h:100
float AngleNormalize360(float angle)
returns angle normalized to the range [0 <= angle < 360]
Definition: mathlib.cpp:995
GridBox(const AABB &aabb)
Definition: mathlib.h:140
bool RayIntersectAABB(const vec3_t start, const vec3_t end, const AABB &aabb)
Definition: mathlib.cpp:1110
void VecToAngles(const vec3_t vec, vec3_t angles)
Converts a vector to an angle vector.
Definition: mathlib.cpp:934
pos_t pos3_t[3]
Definition: ufotypes.h:58
void clipToMaxBoundaries()
Definition: mathlib.h:215
void AddPointToBounds(const vec3_t v, vec3_t mins, vec3_t maxs)
If the point is outside the box defined by mins and maxs, expand the box to accommodate it...
Definition: mathlib.cpp:1042
float crand(void)
Return random values between -1 and 1.
Definition: mathlib.cpp:517
void setFromMapBounds(const vec3_t mini, const vec3_t maxi)
Set the box correctly if the maxs value is the upper corner of a cell. VecToPos considers the upper b...
Definition: mathlib.h:163
#define PATHFINDING_HEIGHT
15 max, adjusting above 8 will require a rewrite to the DV code
Definition: defines.h:294
void ClearBounds(vec3_t mins, vec3_t maxs)
Sets mins and maxs to their starting points before using AddPointToBounds.
Definition: mathlib.cpp:1032
void PolarToVec(const vec2_t a, vec3_t v)
Converts longitude and latitude to a 3D vector in Euclidean coordinates.
Definition: mathlib.cpp:910
void addOneZ()
Definition: mathlib.h:212
const vec3_t & getMins() const
Definition: aabb.h:116
QGL_EXTERN GLint i
Definition: r_gl.h:113
pos_t getMaxZ() const
Definition: mathlib.h:189
#define CORE_DIRECTIONS
Definition: mathlib.h:88
void RotatePointAroundVector(vec3_t dst, const vec3_t dir, const vec3_t point, float degrees)
Rotate a point around a given vector.
Definition: mathlib.cpp:849
vec_t VectorNormalize2(const vec3_t v, vec3_t out)
Calculated the normal vector for a given vec3_t.
Definition: mathlib.cpp:237
void GLMatrixMultiply(const float a[16], const float b[16], float c[16])
Multiply 4*4 matrix by 4*4 matrix.
Definition: mathlib.cpp:350
const float directionAngles[CORE_DIRECTIONS]
Definition: mathlib.cpp:105
void AngleVectors(const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up)
Create the rotation matrix in order to rotate something.
Definition: mathlib.cpp:631
void VectorRotatePoint(vec3_t point, vec3_t matrix[3])
Definition: mathlib.cpp:603
vec_t vec3_t[3]
Definition: ufotypes.h:39
vec_t vec2_t[2]
Definition: ufotypes.h:38
static const GridBox EMPTY
Definition: mathlib.h:122
short dvec_t
The direction vector tells us where the actor came from (in his previous step). The pathing table hol...
Definition: mathlib.h:236
bool isZero() const
Definition: mathlib.h:196
#define VectorIntZero(a)
Definition: vector.h:77
Cross-platform type definitions.
pos3_t maxs
Definition: mathlib.h:223
void CrossProduct(const vec3_t v1, const vec3_t v2, vec3_t cross)
binary operation on vectors in a three-dimensional space
Definition: mathlib.cpp:820
uint8_t byte
Definition: ufotypes.h:34
QGL_EXTERN int GLboolean GLfloat * v
Definition: r_gl.h:120
const vec3_t & getMaxs() const
Definition: aabb.h:128
static struct mdfour * m
Definition: md4.cpp:35
byte pos_t
Definition: ufotypes.h:57
void VectorInverse(vec3_t v)
Inverse a vector.
Definition: mathlib.cpp:460
vec_t vec4_t[4]
Definition: ufotypes.h:40
A simple line between two points.