mathlib.h

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 1997-2001 Id Software, Inc.
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016 
00017 See the GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00023 */
00024 
00025 #ifndef __MATHLIB__
00026 #define __MATHLIB__
00027 
00028 #include "ufotypes.h"
00029 
00030 #ifndef M_PI
00031 #define M_PI 3.14159265358979323846  /* matches value in gcc v2 math.h */
00032 #endif
00033 
00034 #define EQUAL_EPSILON   0.001
00035 
00036 typedef float vec_t;
00037 typedef vec_t vec2_t[2];
00038 typedef vec_t vec3_t[3];
00039 typedef vec_t vec4_t[4];
00040 typedef vec_t vec5_t[5];
00041 
00042 typedef byte pos_t;
00043 typedef pos_t pos3_t[3];
00044 typedef pos_t pos4_t[4];
00045 
00046 extern const vec2_t vec2_origin;
00047 extern const vec3_t vec3_origin;
00048 extern const vec4_t vec4_origin;
00049 extern const vec4_t color_white;
00050 
00051 qboolean Q_IsPowerOfTwo(int i);
00052 
00053 /* Used to compare floats when rounding errors could occur  */
00054 #ifndef equal
00055 #define equal(a,b) (fabs((a)-(b))<0.0000000001)
00056 #endif
00057 
00058 #ifndef max
00059 #define max(a,b) ((a)>(b)?(a):(b))
00060 #endif
00061 
00062 #ifndef min
00063 #define min(a,b) ((a)<(b)?(a):(b))
00064 #endif
00065 
00066 /* microsoft's fabs seems to be ungodly slow... */
00067 #define Q_ftol(f) (long) (f)
00068 
00069 #define torad (M_PI/180.0f)
00070 #define todeg (180.0f/M_PI)
00071 
00072 /* angle indexes */
00073 #define PITCH  0   /* rotation around y axis - up / down (-90 up to 90 degree) */
00074 #define YAW    1   /* rotation around z axis - left / right (0 up to 360 degree) */
00075 #define ROLL   2   /* rotation around x axis - fall over */
00076 
00077 /* earth map data */
00078 /* values of sinus and cosinus of earth inclination (23,5 degrees) for faster day and night calculations */
00079 #define SIN_ALPHA   0.39875
00080 #define COS_ALPHA   0.91706
00081 #define HIGH_LAT    +1.0
00082 #define LOW_LAT     -1.0
00083 #define CENTER_LAT  0.0
00084 #define SIZE_LAT    2.0
00085 
00092 #define DIRECTIONS 8
00093 
00098 #define BASE_DIRECTIONS         4           /* Only the standard N,S,E,W directions */
00099 
00100 /* game/g_ai.c, game/g_spawn.c, common/routing.c, ufo2map/routing.c, client/cl_actor.c, common/cmodel.c, shared/typedefs.h */
00101 #define PATHFINDING_DIRECTIONS  40          /* total number of directions */
00102 #define CORE_DIRECTIONS         8           /* The standard N,S,E,W directions plus diagonals. */
00103 #define FLYING_DIRECTIONS       16          /* starting number of directions available only to fliers */
00104 
00105 extern const vec4_t dvecs[PATHFINDING_DIRECTIONS];
00106 extern const float dvecsn[CORE_DIRECTIONS][2];
00107 extern const float directionAngles[CORE_DIRECTIONS];
00108 
00109 extern const byte dvright[CORE_DIRECTIONS];
00110 extern const byte dvleft[CORE_DIRECTIONS];
00111 
00114 #define VecToPos(v, p) (                  \
00115     p[0] = ((int)v[0] + MAX_WORLD_WIDTH) / UNIT_SIZE,  \
00116     p[1] = ((int)v[1] + MAX_WORLD_WIDTH) / UNIT_SIZE,  \
00117     p[2] =  min((PATHFINDING_HEIGHT - 1), ((int)v[2] / UNIT_HEIGHT)) \
00118 )
00119 
00124 #define PosToVec(p, v) ( \
00125     v[0] = ((int)p[0] - GRID_WIDTH) * UNIT_SIZE   + UNIT_SIZE   / 2, \
00126     v[1] = ((int)p[1] - GRID_WIDTH) * UNIT_SIZE   + UNIT_SIZE   / 2, \
00127     v[2] =  (int)p[2]               * UNIT_HEIGHT + UNIT_HEIGHT / 2  \
00128 )
00129 
00131 #define DotProduct(x,y)         (x[0]*y[0]+x[1]*y[1]+x[2]*y[2])
00132 #define VectorSubtract(a,b,dest)   (dest[0]=a[0]-b[0],dest[1]=a[1]-b[1],dest[2]=a[2]-b[2])
00133 #define Vector2Subtract(a,b,dest)   (dest[0]=a[0]-b[0],dest[1]=a[1]-b[1])
00134 #define VectorAdd(a,b,dest)        (dest[0]=a[0]+b[0],dest[1]=a[1]+b[1],dest[2]=a[2]+b[2])
00135 #define VectorMul(scalar,b,dest)       (dest[0]=(scalar)*b[0],dest[1]=(scalar)*b[1],dest[2]=(scalar)*b[2])
00136 #define Vector2Mul(scalar,b,dest)      (c[0]=(scalar)*b[0],dest[1]=(scalar)*b[1])
00137 #define VectorDiv(in,scalar,out)    VectorScale(in,(1.0f/(scalar)),out)
00138 #define VectorCopy(src,dest)        (dest[0]=src[0],dest[1]=src[1],dest[2]=src[2])
00139 #define Vector2Copy(src,dest)       (dest[0]=src[0],dest[1]=src[1])
00140 #define Vector4Copy(src,dest)       (dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=src[3])
00141 #define Vector2Clear(a)            ((a)[0]=(a)[1]=0)
00142 #define VectorClear(a)          ((a)[0]=(a)[1]=(a)[2]=0)
00143 #define Vector4Clear(a)          ((a)[0]=(a)[1]=(a)[2]=(a)[3]=0)
00144 #define VectorNegate(src,dest)       ((dest)[0]=-(src)[0],(dest)[1]=-(src)[1],(dest)[2]=-(src)[2])
00145 #define VectorSet(v, x, y, z)   ((v)[0]=(x), (v)[1]=(y), (v)[2]=(z))
00146 #define VectorSum(a)            ((a)[0]+(a)[1]+(a)[2])
00147 #define Vector2Set(v, x, y)     ((v)[0]=(x), (v)[1]=(y))
00148 #define Vector4Set(v, r, g, b, a)   (v[0]=(r), v[1]=(g), v[2]=(b), v[3]=(a))
00149 #define VectorCompare(a,b)      (a[0]==b[0]?a[1]==b[1]?a[2]==b[2]?1:0:0:0)
00150 #define VectorEqual(a,b)      (equal(a[0],b[0])?equal(a[1],b[1])?equal(a[2],b[2])?1:0:0:0)
00151 #define Vector2Compare(a,b)     (a[0]==b[0]?a[1]==b[1]?1:0:0)
00152 #define Vector2Equal(a,b)      (equal(a[0],b[0])?equal(a[1],b[1])?1:0:0)
00153 #define VectorDistSqr(a,b)      ((b[0]-a[0])*(b[0]-a[0])+(b[1]-a[1])*(b[1]-a[1])+(b[2]-a[2])*(b[2]-a[2]))
00154 #define VectorDist(a,b)         (sqrt((b[0]-a[0])*(b[0]-a[0])+(b[1]-a[1])*(b[1]-a[1])+(b[2]-a[2])*(b[2]-a[2])))
00155 #define Vector2Dist(a,b)         (sqrt((b[0]-a[0])*(b[0]-a[0])+(b[1]-a[1])*(b[1]-a[1])))
00156 #define VectorLengthSqr(a)      (a[0]*a[0]+a[1]*a[1]+a[2]*a[2])
00157 #define VectorNotEmpty(a)           (a[0]||a[1]||a[2])
00158 #define Vector4NotEmpty(a)          (a[0]||a[1]||a[2]||a[3])
00159 #define LinearInterpolation(a, b, x, y)   (y=a[1] + (((x - a[0]) * (b[1] - a[1])) / (b[0] - a[0])))
00160 #define VectorScale(in,scale,out) ((out)[0] = (in)[0] * (scale),(out)[1] = (in)[1] * (scale),(out)[2] = (in)[2] * (scale))
00161 #define VectorInterpolation(p1,p2,frac,mid) (mid[0]=p1[0]+frac*(p2[0]-p1[0]),mid[1]=p1[1]+frac*(p2[1]-p1[1]),mid[2]=p1[2]+frac*(p2[2]-p1[2]))
00162 
00168 #define DV_Z_BIT_SHIFT  3   
00169 #define DV_Z_BIT_MASK   ((1 << DV_Z_BIT_SHIFT) - 1)  
00171 #define makeDV(dir, z)              ((dir << DV_Z_BIT_SHIFT) | (z & DV_Z_BIT_MASK))
00172 #define NewDVZ(dv, z)               ((dv & (~DV_Z_BIT_MASK)) | (z & DV_Z_BIT_MASK))
00173 #define getDVdir(dv)                (dv >> DV_Z_BIT_SHIFT)
00174 #define getDVz(dv)                  (dv & DV_Z_BIT_MASK)
00175 
00176 #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])
00177 #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])
00178 
00179 int AngleToDir(int angle);
00180 #define AngleToDV(x)    (AngleToDir(x) << DV_Z_BIT_SHIFT)
00181 
00182 void VectorMA(const vec3_t veca, const float scale, const vec3_t vecb, vec3_t outVector);
00183 void VectorClampMA(vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc);
00184 void VectorMix(const vec3_t v1, const vec3_t v2, const float mix, vec3_t out);
00185 
00186 void MatrixMultiply(const vec3_t a[3], const vec3_t b[3], vec3_t c[3]);
00187 void GLMatrixAssemble(const vec3_t origin, const vec3_t angles, float* matrix);
00188 void GLMatrixMultiply(const float a[16], const float b[16], float c[16]);
00189 void GLVectorTransform(const float m[16], const vec4_t in, vec4_t out);
00190 void VectorRotate(vec3_t m[3], const vec3_t va, vec3_t vb);
00191 
00192 void ClearBounds(vec3_t mins, vec3_t maxs);
00193 void AddPointToBounds(const vec3_t v, vec3_t mins, vec3_t maxs);
00194 int VectorCompareEps(const vec3_t v1, const vec3_t v2, float epsilon);
00195 qboolean VectorNearer(const vec3_t v1, const vec3_t v2, const vec3_t comp);
00196 vec_t VectorLength(const vec3_t v);
00197 void CrossProduct(const vec3_t v1, const vec3_t v2, vec3_t cross);
00198 vec_t VectorNormalize(vec3_t v);    /* returns vector length */
00199 vec_t VectorNormalize2(const vec3_t v, vec3_t out);
00200 void VectorInverse(vec3_t v);
00201 void VectorMidpoint(const vec3_t point1, const vec3_t point2, vec3_t midpoint);
00202 int Q_log2(int val);
00203 
00204 float GetDistanceOnGlobe(const vec2_t pos1, const vec2_t pos2);
00205 
00206 void VectorCenterFromMinsMaxs(const vec3_t mins, const vec3_t maxs, vec3_t center);
00207 float VectorAngleBetween(const vec3_t vec1, const vec3_t vec2);
00208 
00209 void VecToAngles(const vec3_t vec, vec3_t angles);
00210 
00211 void Print2Vector(const vec2_t v);
00212 void Print3Vector(const vec3_t v);
00213 
00214 void VecToPolar(const vec3_t v, vec2_t a);
00215 void PolarToVec(const vec2_t a, vec3_t v);
00216 
00217 void VectorCreateRotationMatrix(const vec3_t angles, vec3_t matrix[3]);
00218 void VectorRotatePoint(vec3_t point, vec3_t matrix[3]);
00219 
00220 void AngleVectors(const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up);
00221 float AngleNormalize360(float angle);
00222 float AngleNormalize180(float angle);
00223 
00224 float LerpAngle(float a1, float a2, float frac);
00225 
00226 qboolean FrustumVis(const vec3_t origin, int dir, const vec3_t point);
00227 
00228 void PerpendicularVector(vec3_t dst, const vec3_t src);
00229 void RotatePointAroundVector(vec3_t dst, const vec3_t dir, const vec3_t point, float degrees);
00230 
00231 float frand(void);              /* 0 to 1 */
00232 float crand(void);              /* -1 to 1 */
00233 void gaussrand(float *gauss1, float *gauss2);   /* -inf to +inf, median 0, stdev 1 */
00234 
00235 vec_t Q_rint(const vec_t in);
00236 vec_t ColorNormalize(const vec3_t in, vec3_t out);
00237 
00238 void TangentVectors(const vec3_t normal, const vec3_t sdir, const vec3_t tdir, vec4_t tangent, vec3_t binormal);
00239 
00240 void Orthogonalize(vec3_t v1, const vec3_t v2);
00241 void MatrixTranspose(const vec3_t m[3], vec3_t t[3]);
00242 
00243 #endif

Generated by  doxygen 1.6.2