PageRenderTime 26ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/source/gameshared/q_math.h

https://github.com/Racenet/racesow
C Header | 279 lines | 192 code | 57 blank | 30 comment | 6 complexity | d44246cec9ae50a8066ee5ebbc4b7379 MD5 | raw file
  1. /*
  2. Copyright (C) 1997-2001 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #ifndef GAME_QMATH_H
  16. #define GAME_QMATH_H
  17. #include "q_arch.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. //==============================================================
  22. //
  23. //MATHLIB
  24. //
  25. //==============================================================
  26. #define PITCH 0 // up / down
  27. #define YAW 1 // left / right
  28. #define ROLL 2 // fall over
  29. #define FORWARD 0
  30. #define RIGHT 1
  31. #define UP 2
  32. typedef float vec_t;
  33. typedef vec_t vec2_t[2];
  34. typedef vec_t vec3_t[3];
  35. typedef vec_t vec4_t[4];
  36. typedef vec_t vec5_t[5];
  37. typedef vec_t quat_t[4];
  38. typedef qbyte byte_vec4_t[4];
  39. // 0-2 are axial planes
  40. #define PLANE_X 0
  41. #define PLANE_Y 1
  42. #define PLANE_Z 2
  43. #define PLANE_NONAXIAL 3
  44. // cplane_t structure
  45. typedef struct cplane_s
  46. {
  47. vec3_t normal;
  48. float dist;
  49. short type; // for fast side tests
  50. short signbits; // signx + (signy<<1) + (signz<<1)
  51. } cplane_t;
  52. extern vec3_t vec3_origin;
  53. extern vec3_t axis_identity[3];
  54. extern quat_t quat_identity;
  55. extern vec4_t colorBlack;
  56. extern vec4_t colorRed;
  57. extern vec4_t colorGreen;
  58. extern vec4_t colorBlue;
  59. extern vec4_t colorYellow;
  60. extern vec4_t colorMagenta;
  61. extern vec4_t colorCyan;
  62. extern vec4_t colorWhite;
  63. extern vec4_t colorLtGrey;
  64. extern vec4_t colorMdGrey;
  65. extern vec4_t colorDkGrey;
  66. extern vec4_t colorOrange;
  67. #define MAX_S_COLORS 10
  68. extern vec4_t color_table[MAX_S_COLORS];
  69. #define nanmask ( 255<<23 )
  70. #define IS_NAN( x ) ( ( ( *(int *)&x )&nanmask ) == nanmask )
  71. #ifndef M_PI
  72. #define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h
  73. #endif
  74. #ifndef M_TWOPI
  75. #define M_TWOPI 6.28318530717958647692
  76. #endif
  77. #define DEG2RAD( a ) ( a * M_PI ) / 180.0F
  78. #define RAD2DEG( a ) ( a * 180.0F ) / M_PI
  79. // returns b clamped to [a..c] range
  80. //#define bound(a,b,c) (max((a), min((b), (c))))
  81. #ifndef max
  82. #define max( a, b ) ( ( a ) > ( b ) ? ( a ) : ( b ) )
  83. #endif
  84. #ifndef min
  85. #define min( a, b ) ( ( a ) < ( b ) ? ( a ) : ( b ) )
  86. #endif
  87. #define bound( a, b, c ) ( ( a ) >= ( c ) ? ( a ) : ( b ) < ( a ) ? ( a ) : ( b ) > ( c ) ? ( c ) : ( b ) )
  88. // clamps a (must be lvalue) to [b..c] range
  89. #define clamp( a, b, c ) ( ( b ) >= ( c ) ? ( a ) = ( b ) : ( a ) < ( b ) ? ( a ) = ( b ) : ( a ) > ( c ) ? ( a ) = ( c ) : ( a ) )
  90. #define clamp_low( a, low ) ( ( a ) = ( a ) < ( low ) ? ( low ) : ( a ) )
  91. #define clamp_high( a, high ) ( ( a ) = ( a ) > ( high ) ? ( high ) : ( a ) )
  92. #define random() ( ( rand() & 0x7fff ) / ( (float)0x7fff ) ) // 0..1
  93. #define brandom( a, b ) ( ( a )+random()*( ( b )-( a ) ) ) // a..b
  94. #define crandom() brandom( -1, 1 ) // -1..1
  95. int Q_rand( int *seed );
  96. #define Q_random( seed ) ( ( Q_rand( seed ) & 0x7fff ) / ( (float)0x7fff ) ) // 0..1
  97. #define Q_brandom( seed, a, b ) ( ( a )+Q_random( seed )*( ( b )-( a ) ) ) // a..b
  98. #define Q_crandom( seed ) Q_brandom( seed, -1, 1 )
  99. float Q_RSqrt( float number );
  100. int Q_log2( int val );
  101. #define NEARESTEXPOF2(x) ((int)floor( ( log( max( (x), 1 ) ) - log( 1.5 ) ) / log( 2 ) + 1 ))
  102. #define SQRTFAST( x ) ( ( x ) * Q_RSqrt( x ) ) // jal : //The expression a * rsqrt(b) is intended as a higher performance alternative to a / sqrt(b). The two expressions are comparably accurate, but do not compute exactly the same value in every case. For example, a * rsqrt(a*a + b*b) can be just slightly greater than 1, in rare cases.
  103. #define DotProduct( x, y ) ( ( x )[0]*( y )[0]+( x )[1]*( y )[1]+( x )[2]*( y )[2] )
  104. #define CrossProduct( v1, v2, cross ) ( ( cross )[0] = ( v1 )[1]*( v2 )[2]-( v1 )[2]*( v2 )[1], ( cross )[1] = ( v1 )[2]*( v2 )[0]-( v1 )[0]*( v2 )[2], ( cross )[2] = ( v1 )[0]*( v2 )[1]-( v1 )[1]*( v2 )[0] )
  105. #define PlaneDiff( point, plane ) ( ( ( plane )->type < 3 ? ( point )[( plane )->type] : DotProduct( ( point ), ( plane )->normal ) ) - ( plane )->dist )
  106. #define VectorSubtract( a, b, c ) ( ( c )[0] = ( a )[0]-( b )[0], ( c )[1] = ( a )[1]-( b )[1], ( c )[2] = ( a )[2]-( b )[2] )
  107. #define VectorAdd( a, b, c ) ( ( c )[0] = ( a )[0]+( b )[0], ( c )[1] = ( a )[1]+( b )[1], ( c )[2] = ( a )[2]+( b )[2] )
  108. #define VectorCopy( a, b ) ( ( b )[0] = ( a )[0], ( b )[1] = ( a )[1], ( b )[2] = ( a )[2] )
  109. #define VectorClear( a ) ( ( a )[0] = ( a )[1] = ( a )[2] = 0 )
  110. #define VectorNegate( a, b ) ( ( b )[0] = -( a )[0], ( b )[1] = -( a )[1], ( b )[2] = -( a )[2] )
  111. #define VectorSet( v, x, y, z ) ( ( v )[0] = ( x ), ( v )[1] = ( y ), ( v )[2] = ( z ) )
  112. #define VectorAvg( a, b, c ) ( ( c )[0] = ( ( a )[0]+( b )[0] )*0.5f, ( c )[1] = ( ( a )[1]+( b )[1] )*0.5f, ( c )[2] = ( ( a )[2]+( b )[2] )*0.5f )
  113. #define VectorMA( a, b, c, d ) ( ( d )[0] = ( a )[0]+( b )*( c )[0], ( d )[1] = ( a )[1]+( b )*( c )[1], ( d )[2] = ( a )[2]+( b )*( c )[2] )
  114. #define VectorCompare( v1, v2 ) ( ( v1 )[0] == ( v2 )[0] && ( v1 )[1] == ( v2 )[1] && ( v1 )[2] == ( v2 )[2] )
  115. #define VectorLength( v ) ( sqrt( DotProduct( ( v ), ( v ) ) ) )
  116. #define VectorInverse( v ) ( ( v )[0] = -( v )[0], ( v )[1] = -( v )[1], ( v )[2] = -( v )[2] )
  117. #define VectorLerp( a, c, b, v ) ( ( v )[0] = ( a )[0]+( c )*( ( b )[0]-( a )[0] ), ( v )[1] = ( a )[1]+( c )*( ( b )[1]-( a )[1] ), ( v )[2] = ( a )[2]+( c )*( ( b )[2]-( a )[2] ) )
  118. #define VectorScale( in, scale, out ) ( ( out )[0] = ( in )[0]*( scale ), ( out )[1] = ( in )[1]*( scale ), ( out )[2] = ( in )[2]*( scale ) )
  119. #define DistanceSquared( v1, v2 ) ( ( ( v1 )[0]-( v2 )[0] )*( ( v1 )[0]-( v2 )[0] )+( ( v1 )[1]-( v2 )[1] )*( ( v1 )[1]-( v2 )[1] )+( ( v1 )[2]-( v2 )[2] )*( ( v1 )[2]-( v2 )[2] ) )
  120. #define Distance( v1, v2 ) ( sqrt( DistanceSquared( v1, v2 ) ) )
  121. #define VectorLengthFast( v ) ( SQRTFAST( DotProduct( ( v ), ( v ) ) ) ) // jal : //The expression a * rsqrt(b) is intended as a higher performance alternative to a / sqrt(b). The two expressions are comparably accurate, but do not compute exactly the same value in every case. For example, a * rsqrt(a*a + b*b) can be just slightly greater than 1, in rare cases.
  122. #define DistanceFast( v1, v2 ) ( SQRTFAST( DistanceSquared( v1, v2 ) ) ) // jal : //The expression a * rsqrt(b) is intended as a higher performance alternative to a / sqrt(b). The two expressions are comparably accurate, but do not compute exactly the same value in every case. For example, a * rsqrt(a*a + b*b) can be just slightly greater than 1, in rare cases.
  123. #define Vector2Set( v, x, y ) ( ( v )[0] = ( x ), ( v )[1] = ( y ) )
  124. #define Vector2Copy( a, b ) ( ( b )[0] = ( a )[0], ( b )[1] = ( a )[1] )
  125. #define Vector2Avg( a, b, c ) ( ( c )[0] = ( ( ( a[0] )+( b[0] ) )*0.5f ), ( c )[1] = ( ( ( a[1] )+( b[1] ) )*0.5f ) )
  126. #define Vector4Set( v, a, b, c, d ) ( ( v )[0] = ( a ), ( v )[1] = ( b ), ( v )[2] = ( c ), ( v )[3] = ( d ) )
  127. #define Vector4Copy( a, b ) ( ( b )[0] = ( a )[0], ( b )[1] = ( a )[1], ( b )[2] = ( a )[2], ( b )[3] = ( a )[3] )
  128. #define Vector4Scale( in, scale, out ) ( ( out )[0] = ( in )[0]*scale, ( out )[1] = ( in )[1]*scale, ( out )[2] = ( in )[2]*scale, ( out )[3] = ( in )[3]*scale )
  129. #define Vector4Add( a, b, c ) ( ( c )[0] = ( ( ( a[0] )+( b[0] ) ) ), ( c )[1] = ( ( ( a[1] )+( b[1] ) ) ), ( c )[2] = ( ( ( a[2] )+( b[2] ) ) ), ( c )[3] = ( ( ( a[3] )+( b[3] ) ) ) )
  130. #define Vector4Avg( a, b, c ) ( ( c )[0] = ( ( ( a[0] )+( b[0] ) )*0.5f ), ( c )[1] = ( ( ( a[1] )+( b[1] ) )*0.5f ), ( c )[2] = ( ( ( a[2] )+( b[2] ) )*0.5f ), ( c )[3] = ( ( ( a[3] )+( b[3] ) )*0.5f ) )
  131. vec_t VectorNormalize( vec3_t v ); // returns vector length
  132. vec_t VectorNormalize2( const vec3_t v, vec3_t out );
  133. void VectorNormalizeFast( vec3_t v );
  134. // just in case you do't want to use the macros
  135. void _VectorMA( const vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc );
  136. vec_t _DotProduct( const vec3_t v1, const vec3_t v2 );
  137. void _VectorSubtract( const vec3_t veca, const vec3_t vecb, vec3_t out );
  138. void _VectorAdd( const vec3_t veca, const vec3_t vecb, vec3_t out );
  139. void _VectorCopy( const vec3_t in, vec3_t out );
  140. void ClearBounds( vec3_t mins, vec3_t maxs );
  141. void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs );
  142. float RadiusFromBounds( const vec3_t mins, const vec3_t maxs );
  143. qboolean BoundsIntersect( const vec3_t mins1, const vec3_t maxs1, const vec3_t mins2, const vec3_t maxs2 );
  144. qboolean BoundsAndSphereIntersect( const vec3_t mins, const vec3_t maxs, const vec3_t centre, float radius );
  145. #define NUMVERTEXNORMALS 162
  146. int DirToByte( vec3_t dir );
  147. void ByteToDir( int b, vec3_t dir );
  148. void NormToLatLong( const vec3_t normal, qbyte latlong[2] );
  149. void MakeNormalVectors( const vec3_t forward, vec3_t right, vec3_t up );
  150. void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up );
  151. int BoxOnPlaneSide( const vec3_t emins, const vec3_t emaxs, const struct cplane_s *plane );
  152. float anglemod( float a );
  153. float LerpAngle( float a1, float a2, const float frac );
  154. float AngleSubtract( float a1, float a2 );
  155. void AnglesSubtract( vec3_t v1, vec3_t v2, vec3_t v3 );
  156. float AngleNormalize360( float angle );
  157. float AngleNormalize180( float angle );
  158. float AngleDelta( float angle1, float angle2 );
  159. void VecToAngles( const vec3_t vec, vec3_t angles );
  160. void AnglesToAxis( const vec3_t angles, vec3_t axis[3] );
  161. void NormalVectorToAxis( const vec3_t forward, vec3_t axis[3] );
  162. void BuildBoxPoints( vec3_t p[8], const vec3_t org, const vec3_t mins, const vec3_t maxs );
  163. vec_t ColorNormalize( const vec_t *in, vec_t *out );
  164. float CalcFov( float fov_x, float width, float height );
  165. void AdjustFov( float *fov_x, float *fov_y, float width, float height, qboolean lock_x );
  166. #define Q_rint( x ) ( ( x ) < 0 ? ( (int)( ( x )-0.5f ) ) : ( (int)( ( x )+0.5f ) ) )
  167. int SignbitsForPlane( const cplane_t *out );
  168. int PlaneTypeForNormal( const vec3_t normal );
  169. void CategorizePlane( cplane_t *plane );
  170. void PlaneFromPoints( vec3_t verts[3], cplane_t *plane );
  171. qboolean ComparePlanes( const vec3_t p1normal, vec_t p1dist, const vec3_t p2normal, vec_t p2dist );
  172. void SnapVector( vec3_t normal );
  173. void SnapPlane( vec3_t normal, vec_t *dist );
  174. #define BOX_ON_PLANE_SIDE(emins, emaxs, p) \
  175. (((p)->type < 3)? \
  176. ( \
  177. ((p)->dist <= (emins)[(p)->type])? \
  178. 1 \
  179. : \
  180. ( \
  181. ((p)->dist >= (emaxs)[(p)->type])?\
  182. 2 \
  183. : \
  184. 3 \
  185. ) \
  186. ) \
  187. : \
  188. BoxOnPlaneSide( (emins), (emaxs), (p)))
  189. void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal );
  190. void PerpendicularVector( vec3_t dst, const vec3_t src );
  191. void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
  192. void Matrix_Identity( vec3_t m[3] );
  193. void Matrix_Copy( vec3_t m1[3], vec3_t m2[3] );
  194. qboolean Matrix_Compare( vec3_t m1[3], vec3_t m2[3] );
  195. void Matrix_Multiply( vec3_t m1[3], vec3_t m2[3], vec3_t out[3] );
  196. void Matrix_TransformVector( vec3_t m[3], vec3_t v, vec3_t out );
  197. void Matrix_Transpose( vec3_t in[3], vec3_t out[3] );
  198. void Matrix_EulerAngles( vec3_t m[3], vec3_t angles );
  199. void Matrix_Rotate( vec3_t m[3], vec_t angle, vec_t x, vec_t y, vec_t z );
  200. void Matrix_FromPoints( vec3_t v1, vec3_t v2, vec3_t v3, vec3_t m[3] );
  201. void Quat_Identity( quat_t q );
  202. void Quat_Copy( const quat_t q1, quat_t q2 );
  203. qboolean Quat_Compare( const quat_t q1, const quat_t q2 );
  204. void Quat_Conjugate( const quat_t q1, quat_t q2 );
  205. vec_t Quat_Normalize( quat_t q );
  206. vec_t Quat_Inverse( const quat_t q1, quat_t q2 );
  207. void Quat_Multiply( const quat_t q1, const quat_t q2, quat_t out );
  208. void Quat_Lerp( const quat_t q1, const quat_t q2, vec_t t, quat_t out );
  209. void Quat_Vectors( const quat_t q, vec3_t f, vec3_t r, vec3_t u );
  210. void Quat_Matrix( const quat_t q, vec3_t m[3] );
  211. void Matrix_Quat( vec3_t m[3], quat_t q );
  212. void Quat_TransformVector( const quat_t q, const vec3_t v, vec3_t out );
  213. void Quat_ConcatTransforms( const quat_t q1, const vec3_t v1, const quat_t q2, const vec3_t v2, quat_t q, vec3_t v );
  214. #ifdef __cplusplus
  215. };
  216. #endif
  217. #endif // GAME_QMATH_H