UFO: Alien Invasion
Loading...
Searching...
No Matches
mathlib.h
Go to the documentation of this file.
1
4
5/*
6Copyright (C) 1997-2001 Id Software, Inc.
7
8This program is free software; you can redistribute it and/or
9modify it under the terms of the GNU General Public License
10as published by the Free Software Foundation; either version 2
11of the License, or (at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17See the GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program; if not, write to the Free Software
21Foundation, 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
42bool 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
92extern const float dvecsn[CORE_DIRECTIONS][2];
93extern const float directionAngles[CORE_DIRECTIONS];
94
95extern const byte dvright[CORE_DIRECTIONS];
96extern 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
120class GridBox {
121public:
122 static const GridBox EMPTY;
123
124 /*==================
125 * ctors
126 *==================*/
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 {
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
236typedef 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
242
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
256int AngleToDir(int angle);
257#define AngleToDV(x) (AngleToDir(x) << DV_DIR_BIT_SHIFT)
258
259void VectorMA(const vec3_t veca, const float scale, const vec3_t vecb, vec3_t outVector);
260void VectorClampMA(vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc);
261void VectorMix(const vec3_t v1, const vec3_t v2, const float mix, vec3_t out);
262
263void MatrixMultiply(const vec3_t a[3], const vec3_t b[3], vec3_t c[3]);
264void GLMatrixAssemble(const vec3_t origin, const vec3_t angles, float* matrix);
265void GLMatrixMultiply(const float a[16], const float b[16], float c[16]);
266void GLVectorTransform(const float m[16], const vec4_t in, vec4_t out);
267void GLPositionTransform(const float m[16], const vec3_t in, vec3_t out);
268void VectorRotate(vec3_t m[3], const vec3_t va, vec3_t vb);
269
270void ClearBounds(vec3_t mins, vec3_t maxs);
271void AddPointToBounds(const vec3_t v, vec3_t mins, vec3_t maxs);
272int VectorCompareEps(const vec3_t v1, const vec3_t v2, float epsilon);
273bool VectorNearer(const vec3_t v1, const vec3_t v2, const vec3_t comp);
275void CrossProduct(const vec3_t v1, const vec3_t v2, vec3_t cross);
276vec_t VectorNormalize(vec3_t v); /* returns vector length */
280void VectorMidpoint(const vec3_t point1, const vec3_t point2, vec3_t midpoint);
281int Q_log2(int val);
282
283double GetDistanceOnGlobe(const vec2_t pos1, const vec2_t pos2);
284
285void VectorCalcMinsMaxs(const vec3_t center, const vec3_t size, vec3_t mins, vec3_t maxs);
286float VectorAngleBetween(const vec3_t vec1, const vec3_t vec2);
287
288void VecToAngles(const vec3_t vec, vec3_t angles);
289
290void VecToPolar(const vec3_t v, vec2_t a);
291void PolarToVec(const vec2_t a, vec3_t v);
292
293void CalculateMinsMaxs(const vec3_t angles, const AABB& relBox, const vec3_t origin, AABB& absBox);
294
295void VectorCreateRotationMatrix(const vec3_t angles, vec3_t matrix[3]);
296void VectorRotatePoint(vec3_t point, vec3_t matrix[3]);
297
298void AngleVectors(const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up);
299float AngleNormalize360(float angle);
300float AngleNormalize180(float angle);
301
302float LerpAngle(float a1, float a2, float frac);
303
304bool FrustumVis(const vec3_t origin, int dir, const vec3_t point);
305
306void PerpendicularVector(vec3_t dst, const vec3_t src);
307void RotatePointAroundVector(vec3_t dst, const vec3_t dir, const vec3_t point, float degrees);
308
309float frand(void); /* 0 to 1 */
310float crand(void); /* -1 to 1 */
311void gaussrand(float* gauss1, float* gauss2); /* -inf to +inf, median 0, stdev 1 */
312
313vec_t Q_rint(const vec_t in);
314vec_t ColorNormalize(const vec3_t in, vec3_t out);
315
316void TangentVectors(const vec3_t normal, const vec3_t sdir, const vec3_t tdir, vec4_t tangent, vec3_t binormal);
317
318void Orthogonalize(vec3_t v1, const vec3_t v2);
319void MatrixTranspose(const vec3_t m[3], vec3_t t[3]);
320
321bool RayIntersectAABB(const vec3_t start, const vec3_t end, const AABB& aabb);
Definition aabb.h:42
const vec3_t & getMaxs() const
Definition aabb.h:128
const vec3_t & getMins() const
Definition aabb.h:116
pos_t getMinY() const
Definition mathlib.h:177
void set(const pos3_t mini, const pos3_t maxi)
Definition mathlib.h:148
static const GridBox EMPTY
Definition mathlib.h:122
void clipToMaxBoundaries()
Definition mathlib.h:215
GridBox(const ipos3_t mini, const ipos3_t maxi)
Definition mathlib.h:131
void addOneZ()
Definition mathlib.h:212
pos_t getMinZ() const
Definition mathlib.h:180
pos3_t maxs
Definition mathlib.h:223
void set(const AABB &aabb)
Definition mathlib.h:152
pos3_t mins
Definition mathlib.h:222
pos_t getMaxY() const
Definition mathlib.h:186
bool isZero() const
Definition mathlib.h:196
void expandXY(const int byVal)
expand the box in four directions, but clip them to the maximum boundaries
Definition mathlib.h:206
pos_t getMinX() const
Definition mathlib.h:174
GridBox(const AABB &aabb)
Definition mathlib.h:140
GridBox(const pos3_t mini, const pos3_t maxi)
Definition mathlib.h:135
pos_t getMaxX() const
Definition mathlib.h:183
pos_t getMaxZ() const
Definition mathlib.h:189
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
GridBox()
Definition mathlib.h:127
Defined CONSTANTS (Macros are elsewhere).
#define PATHFINDING_WIDTH
absolute max
Definition defines.h:292
#define PATHFINDING_HEIGHT
15 max, adjusting above 8 will require a rewrite to the DV code
Definition defines.h:294
voidpf void uLong size
Definition ioapi.h:42
voidpf uLong int origin
Definition ioapi.h:45
A simple line between two points.
const vec4_t dvecs[PATHFINDING_DIRECTIONS]
Definition mathlib.cpp:58
const byte dvleft[CORE_DIRECTIONS]
Definition mathlib.cpp:119
const vec3_t vec3_origin
Definition mathlib.cpp:35
const float directionAngles[CORE_DIRECTIONS]
Definition mathlib.cpp:105
const byte dvright[CORE_DIRECTIONS]
Definition mathlib.cpp:116
const float dvecsn[CORE_DIRECTIONS][2]
Definition mathlib.cpp:102
float LerpAngle(float a1, float a2, float frac)
Returns the angle resulting from turning fraction * angle from angle1 to angle2.
Definition mathlib.cpp:981
void VectorMidpoint(const vec3_t point1, const vec3_t point2, vec3_t midpoint)
Calculates the midpoint between two vectors.
Definition mathlib.cpp:473
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
void ClearBounds(vec3_t mins, vec3_t maxs)
Sets mins and maxs to their starting points before using AddPointToBounds.
Definition mathlib.cpp:1032
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 gaussrand(float *gauss1, float *gauss2)
generate two gaussian distributed random numbers with median at 0 and stdev of 1
Definition mathlib.cpp:529
float AngleNormalize360(float angle)
returns angle normalized to the range [0 <= angle < 360]
Definition mathlib.cpp:995
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
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
void VecToAngles(const vec3_t vec, vec3_t angles)
Converts a vector to an angle vector.
Definition mathlib.cpp:934
vec_t VectorNormalize(vec3_t v)
Calculate unit vector for a given vec3_t.
Definition mathlib.cpp:745
void GLMatrixAssemble(const vec3_t origin, const vec3_t angles, float *matrix)
Builds an opengl translation and rotation matrix.
Definition mathlib.cpp:325
vec_t VectorLength(const vec3_t v)
Calculate the length of a vector.
Definition mathlib.cpp:434
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
#define CORE_DIRECTIONS
Definition mathlib.h:88
vec_t Q_rint(const vec_t in)
Round to nearest integer.
Definition mathlib.cpp:156
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
void MatrixTranspose(const vec3_t m[3], vec3_t t[3])
Transposes m and stores the result in t.
Definition mathlib.cpp:1101
int Q_log2(int val)
Definition mathlib.cpp:492
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
vec_t VectorNormalize2(const vec3_t v, vec3_t out)
Calculated the normal vector for a given vec3_t.
Definition mathlib.cpp:237
void GLPositionTransform(const float m[16], const vec3_t in, vec3_t out)
Transform position (xyz) vector by OpenGL rules.
Definition mathlib.cpp:380
void VectorCreateRotationMatrix(const vec3_t angles, vec3_t matrix[3])
Definition mathlib.cpp:592
void VectorInverse(vec3_t v)
Inverse a vector.
Definition mathlib.cpp:460
float crand(void)
Return random values between -1 and 1.
Definition mathlib.cpp:517
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
#define PATHFINDING_DIRECTIONS
Definition mathlib.h:87
void PolarToVec(const vec2_t a, vec3_t v)
Converts longitude and latitude to a 3D vector in Euclidean coordinates.
Definition mathlib.cpp:910
bool RayIntersectAABB(const vec3_t start, const vec3_t end, const AABB &aabb)
Definition mathlib.cpp:1110
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
vec_t ColorNormalize(const vec3_t in, vec3_t out)
Definition mathlib.cpp:190
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
void VectorRotate(vec3_t m[3], const vec3_t va, vec3_t vb)
Rotate a vector with a rotation matrix.
Definition mathlib.cpp:395
bool Q_IsPowerOfTwo(int i)
Checks whether i is power of two value.
Definition mathlib.cpp:972
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
float AngleNormalize180(float angle)
returns angle normalized to the range [-180 < angle <= 180]
Definition mathlib.cpp:1004
void PerpendicularVector(vec3_t dst, const vec3_t src)
Finds a vector perpendicular to the source vector.
Definition mathlib.cpp:780
int AngleToDir(int angle)
Returns the index of array directionAngles[DIRECTIONS] whose value is the closest to angle.
Definition mathlib.cpp:130
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
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
#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
double GetDistanceOnGlobe(const vec2_t pos1, const vec2_t pos2)
Calculate distance on the geoscape.
Definition mathlib.cpp:171
void VectorClampMA(vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc)
Definition mathlib.cpp:268
void VecToPolar(const vec3_t v, vec2_t a)
Converts vector coordinates into polar coordinates.
Definition mathlib.cpp:922
void VectorRotatePoint(vec3_t point, vec3_t matrix[3])
Definition mathlib.cpp:603
void Orthogonalize(vec3_t v1, const vec3_t v2)
Grahm-Schmidt orthogonalization.
Definition mathlib.cpp:1088
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
float frand(void)
Return random values between 0 and 1.
Definition mathlib.cpp:506
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 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
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
void GLVectorTransform(const float m[16], const vec4_t in, vec4_t out)
Multiply 4*4 matrix by 4d vector.
Definition mathlib.cpp:366
static struct mdfour * m
Definition md4.cpp:35
QGL_EXTERN int GLboolean GLfloat * v
Definition r_gl.h:120
QGL_EXTERN GLint i
Definition r_gl.h:113
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
Cross-platform type definitions.
pos_t pos3_t[3]
Definition ufotypes.h:58
byte pos_t
Definition ufotypes.h:57
float vec_t
Definition ufotypes.h:37
vec_t vec3_t[3]
Definition ufotypes.h:39
ipos_t ipos3_t[3]
Definition ufotypes.h:70
vec_t vec4_t[4]
Definition ufotypes.h:40
vec_t vec2_t[2]
Definition ufotypes.h:38
static const vec3_t scale
#define VectorIntZero(a)
Definition vector.h:77
#define VectorCopy(src, dest)
Definition vector.h:51