PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/shared/qcommon/q_math.h

http://github.com/mrwonko/Jedi-Academy--Renaissance
C Header | 296 lines | 166 code | 49 blank | 81 comment | 0 complexity | 861146b577efb1c6672d283ee49d286e MD5 | raw file
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999 - 2005, Id Software, Inc.
  4. Copyright (C) 2000 - 2013, Raven Software, Inc.
  5. Copyright (C) 2001 - 2013, Activision, Inc.
  6. Copyright (C) 2013 - 2015, OpenJK contributors
  7. This file is part of the OpenJK source code.
  8. OpenJK is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License version 2 as
  10. published by the Free Software Foundation.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. ===========================================================================
  18. */
  19. #pragma once
  20. #include "q_platform.h"
  21. typedef float vec_t;
  22. typedef float vec2_t[2], vec3_t[3], vec4_t[4], vec5_t[5];
  23. typedef int ivec2_t[2], ivec3_t[3], ivec4_t[4], ivec5_t[5];
  24. typedef vec3_t vec3pair_t[2], matrix3_t[3];
  25. typedef int fixed4_t, fixed8_t, fixed16_t;
  26. #ifndef M_PI
  27. #define M_PI 3.14159265358979323846f // matches value in gcc v2 math.h
  28. #endif
  29. #define MAX_QINT 0x7fffffff
  30. #define MIN_QINT (-MAX_QINT-1)
  31. ///////////////////////////////////////////////////////////////////////////
  32. //
  33. // DIRECTION ENCODING
  34. //
  35. ///////////////////////////////////////////////////////////////////////////
  36. int DirToByte( vec3_t dir );
  37. void ByteToDir( int b, vec3_t dir );
  38. void NormalToLatLong( const vec3_t normal, byte bytes[2] );
  39. ///////////////////////////////////////////////////////////////////////////
  40. //
  41. // RANDOM NUMBER GENERATION
  42. //
  43. ///////////////////////////////////////////////////////////////////////////
  44. #define QRAND_MAX 32768
  45. int Q_rand( int *seed );
  46. float Q_random( int *seed );
  47. float Q_crandom( int *seed );
  48. void Rand_Init( int seed );
  49. float Q_flrand( float min, float max );
  50. int Q_irand( int value1, int value2 );
  51. float flrand( float min, float max );
  52. int irand( int min, int max );
  53. float erandom( float mean );
  54. ///////////////////////////////////////////////////////////////////////////
  55. //
  56. // MATH UTILITIES
  57. //
  58. ///////////////////////////////////////////////////////////////////////////
  59. #define minimum( x, y ) ((x) < (y) ? (x) : (y))
  60. #define maximum( x, y ) ((x) > (y) ? (x) : (y))
  61. #define PI_DIV_180 0.017453292519943295769236907684886f
  62. #define INV_PI_DIV_180 57.295779513082320876798154814105f
  63. #define DEG2RAD( a ) ( ( (a) * PI_DIV_180 ) )
  64. #define RAD2DEG( a ) ( ( (a) * INV_PI_DIV_180 ) )
  65. #define SQRTFAST( x ) ( (x) * Q_rsqrt( x ) )
  66. #define Q_min(x,y) ((x)<(y)?(x):(y))
  67. #define Q_max(x,y) ((x)>(y)?(x):(y))
  68. #if defined(_MSC_VER)
  69. static __inline long Q_ftol( float f )
  70. {
  71. return (long)f;
  72. }
  73. #else
  74. static inline long Q_ftol( float f )
  75. {
  76. return (long)f;
  77. }
  78. #endif
  79. signed char ClampChar( int i );
  80. signed short ClampShort( int i );
  81. int Com_Clampi( int min, int max, int value );
  82. float Com_Clamp( float min, float max, float value );
  83. int Com_AbsClampi( int min, int max, int value );
  84. float Com_AbsClamp( float min, float max, float value );
  85. float Q_rsqrt( float number );
  86. float Q_fabs( float f );
  87. float Q_acos(float c);
  88. float Q_asin(float c);
  89. float Q_powf ( float x, int y );
  90. qboolean Q_isnan (float f);
  91. int Q_log2( int val );
  92. float LerpAngle(float from, float to, float frac);
  93. float AngleSubtract( float a1, float a2 );
  94. void AnglesSubtract( vec3_t v1, vec3_t v2, vec3_t v3 );
  95. float AngleMod(float a);
  96. float AngleNormalize360 ( float angle );
  97. float AngleNormalize180 ( float angle );
  98. float AngleDelta( float angle1, float angle2 );
  99. ///////////////////////////////////////////////////////////////////////////
  100. //
  101. // GEOMETRIC UTILITIES
  102. //
  103. ///////////////////////////////////////////////////////////////////////////
  104. // angle indexes
  105. #define PITCH 0 // up / down
  106. #define YAW 1 // left / right
  107. #define ROLL 2 // fall over
  108. qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c );
  109. void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point, float degrees );
  110. void RotateAroundDirection( matrix3_t axis, float yaw );
  111. void vectoangles( const vec3_t value1, vec3_t angles );
  112. vec_t GetYawForDirection( const vec3_t p1, const vec3_t p2 );
  113. void GetAnglesForDirection( const vec3_t p1, const vec3_t p2, vec3_t out );
  114. void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal );
  115. qboolean G_FindClosestPointOnLineSegment( const vec3_t start, const vec3_t end, const vec3_t from, vec3_t result );
  116. float G_PointDistFromLineSegment( const vec3_t start, const vec3_t end, const vec3_t from );
  117. void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]);
  118. ///////////////////////////////////////////////////////////////////////////
  119. //
  120. // BOUNDING BOX
  121. //
  122. ///////////////////////////////////////////////////////////////////////////
  123. float RadiusFromBounds( const vec3_t mins, const vec3_t maxs );
  124. void ClearBounds( vec3_t mins, vec3_t maxs );
  125. void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs );
  126. ///////////////////////////////////////////////////////////////////////////
  127. //
  128. // PLANE
  129. //
  130. ///////////////////////////////////////////////////////////////////////////
  131. // plane types are used to speed some tests
  132. // 0-2 are axial planes
  133. #define PLANE_X 0
  134. #define PLANE_Y 1
  135. #define PLANE_Z 2
  136. #define PLANE_NON_AXIAL 3
  137. // plane_t structure
  138. typedef struct cplane_s {
  139. vec3_t normal;
  140. float dist;
  141. byte type; // for fast side tests: 0,1,2 = axial, 3 = nonaxial
  142. byte signbits; // signx + (signy<<1) + (signz<<2), used as lookup during collision
  143. byte pad[2];
  144. } cplane_t;
  145. void SetPlaneSignbits( cplane_t *out );
  146. int PlaneTypeForNormal( vec3_t normal );
  147. int BoxOnPlaneSide(vec3_t emins, vec3_t emaxs, cplane_t *p);
  148. ///////////////////////////////////////////////////////////////////////////
  149. //
  150. // AXIS
  151. //
  152. ///////////////////////////////////////////////////////////////////////////
  153. extern matrix3_t axisDefault;
  154. void AxisClear( matrix3_t axis );
  155. void AxisCopy( matrix3_t in, matrix3_t out );
  156. void AnglesToAxis( const vec3_t angles, matrix3_t axis );
  157. ///////////////////////////////////////////////////////////////////////////
  158. //
  159. // VEC2
  160. //
  161. ///////////////////////////////////////////////////////////////////////////
  162. extern vec2_t vec3_zero;
  163. #define VectorScale2M(v, factor, dst) \
  164. (dst)[0] = (v[0]) * (factor), \
  165. (dst)[1] = (v[1]) * (factor)
  166. #define VectorCopy2M(src, dst) \
  167. (dst)[0] = (src[0]), \
  168. (dst)[1] = (src[1])
  169. #define VectorClear2M(dst) \
  170. memset((dst), 0, sizeof((dst)[0]) * 2)
  171. void VectorAdd2( const vec2_t vec1, const vec2_t vec2, vec2_t vecOut );
  172. void VectorSubtract2( const vec2_t vec1, const vec2_t vec2, vec2_t vec2_t );
  173. void VectorScale2( const vec2_t vecIn, float scale, vec2_t vecOut );
  174. void VectorMA2( const vec2_t vec1, float scale, const vec2_t vec2, vec2_t vecOut );
  175. void VectorSet2( vec2_t vec, float x, float y );
  176. void VectorClear2( vec2_t vec );
  177. void VectorCopy2( const vec2_t vecIn, vec2_t vecOut );
  178. ///////////////////////////////////////////////////////////////////////////
  179. //
  180. // VEC3
  181. //
  182. ///////////////////////////////////////////////////////////////////////////
  183. extern vec3_t vec3_origin;
  184. #define VectorScaleM(v, factor, dst) \
  185. (dst)[0] = (v[0]) * (factor), \
  186. (dst)[1] = (v[1]) * (factor), \
  187. (dst)[2] = (v[2]) * (factor)
  188. #define VectorCopyM(src, dst) \
  189. (dst)[0] = (src[0]), \
  190. (dst)[1] = (src[1]), \
  191. (dst)[2] = (src[2])
  192. #define VectorClearM(dst) \
  193. memset((dst), 0, sizeof((dst)[0]) * 3)
  194. void VectorAdd( const vec3_t vec1, const vec3_t vec2, vec3_t vecOut );
  195. void VectorSubtract( const vec3_t vec1, const vec3_t vec2, vec3_t vecOut );
  196. void VectorScale( const vec3_t vecIn, float scale, vec3_t vecOut );
  197. void VectorMA( const vec3_t vec1, float scale, const vec3_t vec2, vec3_t vecOut );
  198. void VectorSet( vec3_t vec, float x, float y, float z );
  199. void VectorClear( vec3_t vec );
  200. void VectorCopy( const vec3_t vecIn, vec3_t vecOut );
  201. float VectorLength( const vec3_t vec );
  202. float VectorLengthSquared( const vec3_t vec );
  203. void VectorNormalizeFast( vec3_t vec );
  204. float VectorNormalize( vec3_t vec );
  205. float VectorNormalize2( const vec3_t vec, vec3_t vecOut );
  206. void VectorAdvance( const vec3_t veca, const float scale, const vec3_t vecb, vec3_t vecc);
  207. void VectorInc( vec3_t vec );
  208. void VectorDec( vec3_t vec );
  209. void VectorInverse( vec3_t vec );
  210. void CrossProduct( const vec3_t vec1, const vec3_t vec2, vec3_t vecOut );
  211. float DotProduct( const vec3_t vec1, const vec3_t vec2 );
  212. qboolean VectorCompare( const vec3_t vec1, const vec3_t vec2 );
  213. qboolean VectorCompare2( const vec3_t v1, const vec3_t v2 );
  214. void SnapVector( float *v );
  215. float Distance( const vec3_t p1, const vec3_t p2 );
  216. float DistanceSquared( const vec3_t p1, const vec3_t p2 );
  217. float DistanceHorizontal( const vec3_t p1, const vec3_t p2 );
  218. float DistanceHorizontalSquared( const vec3_t p1, const vec3_t p2 );
  219. void MakeNormalVectors( const vec3_t forward, vec3_t right, vec3_t up);
  220. void VectorRotate( const vec3_t in, matrix3_t matrix, vec3_t out );
  221. void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up);
  222. void PerpendicularVector( vec3_t dst, const vec3_t src );
  223. float DotProductNormalize( const vec3_t inVec1, const vec3_t inVec2 );
  224. #define VectorScaleVector(a,b,c) (((c)[0]=(a)[0]*(b)[0]),((c)[1]=(a)[1]*(b)[1]),((c)[2]=(a)[2]*(b)[2]))
  225. #define VectorInverseScaleVector(a,b,c) ((c)[0]=(a)[0]/(b)[0],(c)[1]=(a)[1]/(b)[1],(c)[2]=(a)[2]/(b)[2])
  226. #define VectorScaleVectorAdd(c,a,b,o) ((o)[0]=(c)[0]+((a)[0]*(b)[0]),(o)[1]=(c)[1]+((a)[1]*(b)[1]),(o)[2]=(c)[2]+((a)[2]*(b)[2]))
  227. #define VectorAverage(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))
  228. #define VectorNegate(a,b) ((b)[0]=-(a)[0],(b)[1]=-(a)[1],(b)[2]=-(a)[2])
  229. ///////////////////////////////////////////////////////////////////////////
  230. //
  231. // VEC4
  232. //
  233. ///////////////////////////////////////////////////////////////////////////
  234. void VectorScale4( const vec4_t vecIn, float scale, vec4_t vecOut );
  235. void VectorCopy4( const vec4_t vecIn, vec4_t vecOut );
  236. void VectorSet4( vec4_t vec, float x, float y, float z, float w );
  237. void VectorClear4( vec4_t vec );
  238. ///////////////////////////////////////////////////////////////////////////
  239. //
  240. // VEC5
  241. //
  242. ///////////////////////////////////////////////////////////////////////////
  243. void VectorSet5( vec5_t vec, float x, float y, float z, float w, float u );