Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4303 lines
111 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Math primitives.
  4. //
  5. //===========================================================================//
  6. /// FIXME: As soon as all references to mathlib.c are gone, include it in here
  7. #include <math.h>
  8. #include <float.h> // Needed for FLT_EPSILON
  9. #include "tier0/basetypes.h"
  10. #include <memory.h>
  11. #include "tier0/dbg.h"
  12. #include "tier0/vprof.h"
  13. //#define _VPROF_MATHLIB
  14. #pragma warning(disable:4244) // "conversion from 'const int' to 'float', possible loss of data"
  15. #pragma warning(disable:4730) // "mixing _m64 and floating point expressions may result in incorrect code"
  16. #include "mathlib/mathlib.h"
  17. #include "mathlib/vector.h"
  18. #if !defined( _X360 )
  19. #include "mathlib/amd3dx.h"
  20. #ifndef OSX
  21. #include "3dnow.h"
  22. #endif
  23. #include "sse.h"
  24. #endif
  25. #include "mathlib/ssemath.h"
  26. #include "mathlib/ssequaternion.h"
  27. // memdbgon must be the last include file in a .cpp file!!!
  28. #include "tier0/memdbgon.h"
  29. bool s_bMathlibInitialized = false;
  30. #ifdef PARANOID
  31. // User must provide an implementation of Sys_Error()
  32. void Sys_Error (char *error, ...);
  33. #endif
  34. const Vector vec3_origin(0,0,0);
  35. const QAngle vec3_angle(0,0,0);
  36. const Vector vec3_invalid( FLT_MAX, FLT_MAX, FLT_MAX );
  37. const int nanmask = 255<<23;
  38. //-----------------------------------------------------------------------------
  39. // Standard C implementations of optimized routines:
  40. //-----------------------------------------------------------------------------
  41. float _sqrtf(float _X)
  42. {
  43. Assert( s_bMathlibInitialized );
  44. return sqrtf(_X);
  45. }
  46. float _rsqrtf(float x)
  47. {
  48. Assert( s_bMathlibInitialized );
  49. return 1.f / _sqrtf( x );
  50. }
  51. float FASTCALL _VectorNormalize (Vector& vec)
  52. {
  53. #ifdef _VPROF_MATHLIB
  54. VPROF_BUDGET( "_VectorNormalize", "Mathlib" );
  55. #endif
  56. Assert( s_bMathlibInitialized );
  57. float radius = sqrtf(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z);
  58. // FLT_EPSILON is added to the radius to eliminate the possibility of divide by zero.
  59. float iradius = 1.f / ( radius + FLT_EPSILON );
  60. vec.x *= iradius;
  61. vec.y *= iradius;
  62. vec.z *= iradius;
  63. return radius;
  64. }
  65. // TODO: Add fast C VectorNormalizeFast.
  66. // Perhaps use approximate rsqrt trick, if the accuracy isn't too bad.
  67. void FASTCALL _VectorNormalizeFast (Vector& vec)
  68. {
  69. Assert( s_bMathlibInitialized );
  70. // FLT_EPSILON is added to the radius to eliminate the possibility of divide by zero.
  71. float iradius = 1.f / ( sqrtf(vec.x*vec.x + vec.y*vec.y + vec.z*vec.z) + FLT_EPSILON );
  72. vec.x *= iradius;
  73. vec.y *= iradius;
  74. vec.z *= iradius;
  75. }
  76. float _InvRSquared(const float* v)
  77. {
  78. Assert( s_bMathlibInitialized );
  79. float r2 = DotProduct(v, v);
  80. return r2 < 1.f ? 1.f : 1/r2;
  81. }
  82. //-----------------------------------------------------------------------------
  83. // Function pointers selecting the appropriate implementation
  84. //-----------------------------------------------------------------------------
  85. float (*pfSqrt)(float x) = _sqrtf;
  86. float (*pfRSqrt)(float x) = _rsqrtf;
  87. float (*pfRSqrtFast)(float x) = _rsqrtf;
  88. float (FASTCALL *pfVectorNormalize)(Vector& v) = _VectorNormalize;
  89. void (FASTCALL *pfVectorNormalizeFast)(Vector& v) = _VectorNormalizeFast;
  90. float (*pfInvRSquared)(const float* v) = _InvRSquared;
  91. void (*pfFastSinCos)(float x, float* s, float* c) = SinCos;
  92. float (*pfFastCos)(float x) = cosf;
  93. float SinCosTable[SIN_TABLE_SIZE];
  94. void InitSinCosTable()
  95. {
  96. for( int i = 0; i < SIN_TABLE_SIZE; i++ )
  97. {
  98. SinCosTable[i] = sin(i * 2.0 * M_PI / SIN_TABLE_SIZE);
  99. }
  100. }
  101. qboolean VectorsEqual( const float *v1, const float *v2 )
  102. {
  103. Assert( s_bMathlibInitialized );
  104. return ( ( v1[0] == v2[0] ) &&
  105. ( v1[1] == v2[1] ) &&
  106. ( v1[2] == v2[2] ) );
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Purpose: Generates Euler angles given a left-handed orientation matrix. The
  110. // columns of the matrix contain the forward, left, and up vectors.
  111. // Input : matrix - Left-handed orientation matrix.
  112. // angles[PITCH, YAW, ROLL]. Receives right-handed counterclockwise
  113. // rotations in degrees around Y, Z, and X respectively.
  114. //-----------------------------------------------------------------------------
  115. void MatrixAngles( const matrix3x4_t& matrix, RadianEuler &angles, Vector &position )
  116. {
  117. MatrixGetColumn( matrix, 3, position );
  118. MatrixAngles( matrix, angles );
  119. }
  120. void MatrixAngles( const matrix3x4_t &matrix, Quaternion &q, Vector &pos )
  121. {
  122. #ifdef _VPROF_MATHLIB
  123. VPROF_BUDGET( "MatrixQuaternion", "Mathlib" );
  124. #endif
  125. float trace;
  126. trace = matrix[0][0] + matrix[1][1] + matrix[2][2] + 1.0f;
  127. if( trace > 1.0f + FLT_EPSILON )
  128. {
  129. // VPROF_INCREMENT_COUNTER("MatrixQuaternion A",1);
  130. q.x = ( matrix[2][1] - matrix[1][2] );
  131. q.y = ( matrix[0][2] - matrix[2][0] );
  132. q.z = ( matrix[1][0] - matrix[0][1] );
  133. q.w = trace;
  134. }
  135. else if ( matrix[0][0] > matrix[1][1] && matrix[0][0] > matrix[2][2] )
  136. {
  137. // VPROF_INCREMENT_COUNTER("MatrixQuaternion B",1);
  138. trace = 1.0f + matrix[0][0] - matrix[1][1] - matrix[2][2];
  139. q.x = trace;
  140. q.y = (matrix[1][0] + matrix[0][1] );
  141. q.z = (matrix[0][2] + matrix[2][0] );
  142. q.w = (matrix[2][1] - matrix[1][2] );
  143. }
  144. else if (matrix[1][1] > matrix[2][2])
  145. {
  146. // VPROF_INCREMENT_COUNTER("MatrixQuaternion C",1);
  147. trace = 1.0f + matrix[1][1] - matrix[0][0] - matrix[2][2];
  148. q.x = (matrix[0][1] + matrix[1][0] );
  149. q.y = trace;
  150. q.z = (matrix[2][1] + matrix[1][2] );
  151. q.w = (matrix[0][2] - matrix[2][0] );
  152. }
  153. else
  154. {
  155. // VPROF_INCREMENT_COUNTER("MatrixQuaternion D",1);
  156. trace = 1.0f + matrix[2][2] - matrix[0][0] - matrix[1][1];
  157. q.x = (matrix[0][2] + matrix[2][0] );
  158. q.y = (matrix[2][1] + matrix[1][2] );
  159. q.z = trace;
  160. q.w = (matrix[1][0] - matrix[0][1] );
  161. }
  162. QuaternionNormalize( q );
  163. #if 0
  164. // check against the angle version
  165. RadianEuler ang;
  166. MatrixAngles( matrix, ang );
  167. Quaternion test;
  168. AngleQuaternion( ang, test );
  169. float d = QuaternionDotProduct( q, test );
  170. Assert( fabs(d) > 0.99 && fabs(d) < 1.01 );
  171. #endif
  172. MatrixGetColumn( matrix, 3, pos );
  173. }
  174. void MatrixAngles( const matrix3x4_t& matrix, float *angles )
  175. {
  176. #ifdef _VPROF_MATHLIB
  177. VPROF_BUDGET( "MatrixAngles", "Mathlib" );
  178. #endif
  179. Assert( s_bMathlibInitialized );
  180. float forward[3];
  181. float left[3];
  182. float up[3];
  183. //
  184. // Extract the basis vectors from the matrix. Since we only need the Z
  185. // component of the up vector, we don't get X and Y.
  186. //
  187. forward[0] = matrix[0][0];
  188. forward[1] = matrix[1][0];
  189. forward[2] = matrix[2][0];
  190. left[0] = matrix[0][1];
  191. left[1] = matrix[1][1];
  192. left[2] = matrix[2][1];
  193. up[2] = matrix[2][2];
  194. float xyDist = sqrtf( forward[0] * forward[0] + forward[1] * forward[1] );
  195. // enough here to get angles?
  196. if ( xyDist > 0.001f )
  197. {
  198. // (yaw) y = ATAN( forward.y, forward.x ); -- in our space, forward is the X axis
  199. angles[1] = RAD2DEG( atan2f( forward[1], forward[0] ) );
  200. // (pitch) x = ATAN( -forward.z, sqrt(forward.x*forward.x+forward.y*forward.y) );
  201. angles[0] = RAD2DEG( atan2f( -forward[2], xyDist ) );
  202. // (roll) z = ATAN( left.z, up.z );
  203. angles[2] = RAD2DEG( atan2f( left[2], up[2] ) );
  204. }
  205. else // forward is mostly Z, gimbal lock-
  206. {
  207. // (yaw) y = ATAN( -left.x, left.y ); -- forward is mostly z, so use right for yaw
  208. angles[1] = RAD2DEG( atan2f( -left[0], left[1] ) );
  209. // (pitch) x = ATAN( -forward.z, sqrt(forward.x*forward.x+forward.y*forward.y) );
  210. angles[0] = RAD2DEG( atan2f( -forward[2], xyDist ) );
  211. // Assume no roll in this case as one degree of freedom has been lost (i.e. yaw == roll)
  212. angles[2] = 0;
  213. }
  214. }
  215. // transform in1 by the matrix in2
  216. void VectorTransform (const float *in1, const matrix3x4_t& in2, float *out)
  217. {
  218. Assert( s_bMathlibInitialized );
  219. Assert( in1 != out );
  220. out[0] = DotProduct(in1, in2[0]) + in2[0][3];
  221. out[1] = DotProduct(in1, in2[1]) + in2[1][3];
  222. out[2] = DotProduct(in1, in2[2]) + in2[2][3];
  223. }
  224. // assuming the matrix is orthonormal, transform in1 by the transpose (also the inverse in this case) of in2.
  225. void VectorITransform (const float *in1, const matrix3x4_t& in2, float *out)
  226. {
  227. Assert( s_bMathlibInitialized );
  228. float in1t[3];
  229. in1t[0] = in1[0] - in2[0][3];
  230. in1t[1] = in1[1] - in2[1][3];
  231. in1t[2] = in1[2] - in2[2][3];
  232. out[0] = in1t[0] * in2[0][0] + in1t[1] * in2[1][0] + in1t[2] * in2[2][0];
  233. out[1] = in1t[0] * in2[0][1] + in1t[1] * in2[1][1] + in1t[2] * in2[2][1];
  234. out[2] = in1t[0] * in2[0][2] + in1t[1] * in2[1][2] + in1t[2] * in2[2][2];
  235. }
  236. // assume in2 is a rotation and rotate the input vector
  237. void VectorRotate( const float *in1, const matrix3x4_t& in2, float *out )
  238. {
  239. Assert( s_bMathlibInitialized );
  240. Assert( in1 != out );
  241. out[0] = DotProduct( in1, in2[0] );
  242. out[1] = DotProduct( in1, in2[1] );
  243. out[2] = DotProduct( in1, in2[2] );
  244. }
  245. // assume in2 is a rotation and rotate the input vector
  246. void VectorRotate( const Vector &in1, const QAngle &in2, Vector &out )
  247. {
  248. matrix3x4_t matRotate;
  249. AngleMatrix( in2, matRotate );
  250. VectorRotate( in1, matRotate, out );
  251. }
  252. // assume in2 is a rotation and rotate the input vector
  253. void VectorRotate( const Vector &in1, const Quaternion &in2, Vector &out )
  254. {
  255. matrix3x4_t matRotate;
  256. QuaternionMatrix( in2, matRotate );
  257. VectorRotate( in1, matRotate, out );
  258. }
  259. // rotate by the inverse of the matrix
  260. void VectorIRotate( const float *in1, const matrix3x4_t& in2, float *out )
  261. {
  262. Assert( s_bMathlibInitialized );
  263. Assert( in1 != out );
  264. out[0] = in1[0]*in2[0][0] + in1[1]*in2[1][0] + in1[2]*in2[2][0];
  265. out[1] = in1[0]*in2[0][1] + in1[1]*in2[1][1] + in1[2]*in2[2][1];
  266. out[2] = in1[0]*in2[0][2] + in1[1]*in2[1][2] + in1[2]*in2[2][2];
  267. }
  268. #ifndef VECTOR_NO_SLOW_OPERATIONS
  269. // transform a set of angles in the output space of parentMatrix to the input space
  270. QAngle TransformAnglesToLocalSpace( const QAngle &angles, const matrix3x4_t &parentMatrix )
  271. {
  272. matrix3x4_t angToWorld, worldToParent, localMatrix;
  273. MatrixInvert( parentMatrix, worldToParent );
  274. AngleMatrix( angles, angToWorld );
  275. ConcatTransforms( worldToParent, angToWorld, localMatrix );
  276. QAngle out;
  277. MatrixAngles( localMatrix, out );
  278. return out;
  279. }
  280. // transform a set of angles in the input space of parentMatrix to the output space
  281. QAngle TransformAnglesToWorldSpace( const QAngle &angles, const matrix3x4_t &parentMatrix )
  282. {
  283. matrix3x4_t angToParent, angToWorld;
  284. AngleMatrix( angles, angToParent );
  285. ConcatTransforms( parentMatrix, angToParent, angToWorld );
  286. QAngle out;
  287. MatrixAngles( angToWorld, out );
  288. return out;
  289. }
  290. #endif // VECTOR_NO_SLOW_OPERATIONS
  291. void MatrixInitialize( matrix3x4_t &mat, const Vector &vecOrigin, const Vector &vecXAxis, const Vector &vecYAxis, const Vector &vecZAxis )
  292. {
  293. MatrixSetColumn( vecXAxis, 0, mat );
  294. MatrixSetColumn( vecYAxis, 1, mat );
  295. MatrixSetColumn( vecZAxis, 2, mat );
  296. MatrixSetColumn( vecOrigin, 3, mat );
  297. }
  298. void MatrixCopy( const matrix3x4_t& in, matrix3x4_t& out )
  299. {
  300. Assert( s_bMathlibInitialized );
  301. memcpy( out.Base(), in.Base(), sizeof( float ) * 3 * 4 );
  302. }
  303. //-----------------------------------------------------------------------------
  304. // Matrix equality test
  305. //-----------------------------------------------------------------------------
  306. bool MatricesAreEqual( const matrix3x4_t &src1, const matrix3x4_t &src2, float flTolerance )
  307. {
  308. for ( int i = 0; i < 3; ++i )
  309. {
  310. for ( int j = 0; j < 4; ++j )
  311. {
  312. if ( fabs( src1[i][j] - src2[i][j] ) > flTolerance )
  313. return false;
  314. }
  315. }
  316. return true;
  317. }
  318. // NOTE: This is just the transpose not a general inverse
  319. void MatrixInvert( const matrix3x4_t& in, matrix3x4_t& out )
  320. {
  321. Assert( s_bMathlibInitialized );
  322. if ( &in == &out )
  323. {
  324. V_swap(out[0][1],out[1][0]);
  325. V_swap(out[0][2],out[2][0]);
  326. V_swap(out[1][2],out[2][1]);
  327. }
  328. else
  329. {
  330. // transpose the matrix
  331. out[0][0] = in[0][0];
  332. out[0][1] = in[1][0];
  333. out[0][2] = in[2][0];
  334. out[1][0] = in[0][1];
  335. out[1][1] = in[1][1];
  336. out[1][2] = in[2][1];
  337. out[2][0] = in[0][2];
  338. out[2][1] = in[1][2];
  339. out[2][2] = in[2][2];
  340. }
  341. // now fix up the translation to be in the other space
  342. float tmp[3];
  343. tmp[0] = in[0][3];
  344. tmp[1] = in[1][3];
  345. tmp[2] = in[2][3];
  346. out[0][3] = -DotProduct( tmp, out[0] );
  347. out[1][3] = -DotProduct( tmp, out[1] );
  348. out[2][3] = -DotProduct( tmp, out[2] );
  349. }
  350. void MatrixGetColumn( const matrix3x4_t& in, int column, Vector &out )
  351. {
  352. out.x = in[0][column];
  353. out.y = in[1][column];
  354. out.z = in[2][column];
  355. }
  356. void MatrixSetColumn( const Vector &in, int column, matrix3x4_t& out )
  357. {
  358. out[0][column] = in.x;
  359. out[1][column] = in.y;
  360. out[2][column] = in.z;
  361. }
  362. void MatrixScaleBy ( const float flScale, matrix3x4_t &out )
  363. {
  364. out[0][0] *= flScale;
  365. out[1][0] *= flScale;
  366. out[2][0] *= flScale;
  367. out[0][1] *= flScale;
  368. out[1][1] *= flScale;
  369. out[2][1] *= flScale;
  370. out[0][2] *= flScale;
  371. out[1][2] *= flScale;
  372. out[2][2] *= flScale;
  373. }
  374. void MatrixScaleByZero ( matrix3x4_t &out )
  375. {
  376. out[0][0] = 0.0f;
  377. out[1][0] = 0.0f;
  378. out[2][0] = 0.0f;
  379. out[0][1] = 0.0f;
  380. out[1][1] = 0.0f;
  381. out[2][1] = 0.0f;
  382. out[0][2] = 0.0f;
  383. out[1][2] = 0.0f;
  384. out[2][2] = 0.0f;
  385. }
  386. int VectorCompare (const float *v1, const float *v2)
  387. {
  388. Assert( s_bMathlibInitialized );
  389. int i;
  390. for (i=0 ; i<3 ; i++)
  391. if (v1[i] != v2[i])
  392. return 0;
  393. return 1;
  394. }
  395. void CrossProduct (const float* v1, const float* v2, float* cross)
  396. {
  397. Assert( s_bMathlibInitialized );
  398. Assert( v1 != cross );
  399. Assert( v2 != cross );
  400. cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  401. cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  402. cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  403. }
  404. int Q_log2(int val)
  405. {
  406. int answer=0;
  407. while (val>>=1)
  408. answer++;
  409. return answer;
  410. }
  411. // Matrix is right-handed x=forward, y=left, z=up. We a left-handed convention for vectors in the game code (forward, right, up)
  412. void MatrixVectors( const matrix3x4_t &matrix, Vector* pForward, Vector *pRight, Vector *pUp )
  413. {
  414. MatrixGetColumn( matrix, 0, *pForward );
  415. MatrixGetColumn( matrix, 1, *pRight );
  416. MatrixGetColumn( matrix, 2, *pUp );
  417. *pRight *= -1.0f;
  418. }
  419. void VectorVectors( const Vector &forward, Vector &right, Vector &up )
  420. {
  421. Assert( s_bMathlibInitialized );
  422. Vector tmp;
  423. if (forward[0] == 0 && forward[1] == 0)
  424. {
  425. // pitch 90 degrees up/down from identity
  426. right[0] = 0;
  427. right[1] = -1;
  428. right[2] = 0;
  429. up[0] = -forward[2];
  430. up[1] = 0;
  431. up[2] = 0;
  432. }
  433. else
  434. {
  435. tmp[0] = 0; tmp[1] = 0; tmp[2] = 1.0;
  436. CrossProduct( forward, tmp, right );
  437. VectorNormalize( right );
  438. CrossProduct( right, forward, up );
  439. VectorNormalize( up );
  440. }
  441. }
  442. void VectorMatrix( const Vector &forward, matrix3x4_t& matrix)
  443. {
  444. Assert( s_bMathlibInitialized );
  445. Vector right, up;
  446. VectorVectors(forward, right, up);
  447. MatrixSetColumn( forward, 0, matrix );
  448. MatrixSetColumn( -right, 1, matrix );
  449. MatrixSetColumn( up, 2, matrix );
  450. }
  451. void VectorAngles( const float *forward, float *angles )
  452. {
  453. Assert( s_bMathlibInitialized );
  454. float tmp, yaw, pitch;
  455. if (forward[1] == 0 && forward[0] == 0)
  456. {
  457. yaw = 0;
  458. if (forward[2] > 0)
  459. pitch = 270;
  460. else
  461. pitch = 90;
  462. }
  463. else
  464. {
  465. yaw = (atan2(forward[1], forward[0]) * 180 / M_PI);
  466. if (yaw < 0)
  467. yaw += 360;
  468. tmp = sqrt (forward[0]*forward[0] + forward[1]*forward[1]);
  469. pitch = (atan2(-forward[2], tmp) * 180 / M_PI);
  470. if (pitch < 0)
  471. pitch += 360;
  472. }
  473. angles[0] = pitch;
  474. angles[1] = yaw;
  475. angles[2] = 0;
  476. }
  477. /*
  478. ================
  479. R_ConcatRotations
  480. ================
  481. */
  482. void ConcatRotations (const float in1[3][3], const float in2[3][3], float out[3][3])
  483. {
  484. Assert( s_bMathlibInitialized );
  485. Assert( in1 != out );
  486. Assert( in2 != out );
  487. out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
  488. in1[0][2] * in2[2][0];
  489. out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
  490. in1[0][2] * in2[2][1];
  491. out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
  492. in1[0][2] * in2[2][2];
  493. out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
  494. in1[1][2] * in2[2][0];
  495. out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
  496. in1[1][2] * in2[2][1];
  497. out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
  498. in1[1][2] * in2[2][2];
  499. out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
  500. in1[2][2] * in2[2][0];
  501. out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
  502. in1[2][2] * in2[2][1];
  503. out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
  504. in1[2][2] * in2[2][2];
  505. }
  506. void ConcatTransforms_Aligned( const matrix3x4_t &m0, const matrix3x4_t &m1, matrix3x4_t &out )
  507. {
  508. Assert( (((size_t)&m0) % 16) == 0 );
  509. Assert( (((size_t)&m1) % 16) == 0 );
  510. Assert( (((size_t)&out) % 16) == 0 );
  511. fltx4 lastMask = *(fltx4 *)(&g_SIMD_ComponentMask[3]);
  512. fltx4 rowA0 = LoadAlignedSIMD( m0.m_flMatVal[0] );
  513. fltx4 rowA1 = LoadAlignedSIMD( m0.m_flMatVal[1] );
  514. fltx4 rowA2 = LoadAlignedSIMD( m0.m_flMatVal[2] );
  515. fltx4 rowB0 = LoadAlignedSIMD( m1.m_flMatVal[0] );
  516. fltx4 rowB1 = LoadAlignedSIMD( m1.m_flMatVal[1] );
  517. fltx4 rowB2 = LoadAlignedSIMD( m1.m_flMatVal[2] );
  518. // now we have the rows of m0 and the columns of m1
  519. // first output row
  520. fltx4 A0 = SplatXSIMD(rowA0);
  521. fltx4 A1 = SplatYSIMD(rowA0);
  522. fltx4 A2 = SplatZSIMD(rowA0);
  523. fltx4 mul00 = MulSIMD( A0, rowB0 );
  524. fltx4 mul01 = MulSIMD( A1, rowB1 );
  525. fltx4 mul02 = MulSIMD( A2, rowB2 );
  526. fltx4 out0 = AddSIMD( mul00, AddSIMD(mul01,mul02) );
  527. // second output row
  528. A0 = SplatXSIMD(rowA1);
  529. A1 = SplatYSIMD(rowA1);
  530. A2 = SplatZSIMD(rowA1);
  531. fltx4 mul10 = MulSIMD( A0, rowB0 );
  532. fltx4 mul11 = MulSIMD( A1, rowB1 );
  533. fltx4 mul12 = MulSIMD( A2, rowB2 );
  534. fltx4 out1 = AddSIMD( mul10, AddSIMD(mul11,mul12) );
  535. // third output row
  536. A0 = SplatXSIMD(rowA2);
  537. A1 = SplatYSIMD(rowA2);
  538. A2 = SplatZSIMD(rowA2);
  539. fltx4 mul20 = MulSIMD( A0, rowB0 );
  540. fltx4 mul21 = MulSIMD( A1, rowB1 );
  541. fltx4 mul22 = MulSIMD( A2, rowB2 );
  542. fltx4 out2 = AddSIMD( mul20, AddSIMD(mul21,mul22) );
  543. // add in translation vector
  544. A0 = AndSIMD(rowA0,lastMask);
  545. A1 = AndSIMD(rowA1,lastMask);
  546. A2 = AndSIMD(rowA2,lastMask);
  547. out0 = AddSIMD(out0, A0);
  548. out1 = AddSIMD(out1, A1);
  549. out2 = AddSIMD(out2, A2);
  550. StoreAlignedSIMD( out.m_flMatVal[0], out0 );
  551. StoreAlignedSIMD( out.m_flMatVal[1], out1 );
  552. StoreAlignedSIMD( out.m_flMatVal[2], out2 );
  553. }
  554. /*
  555. ================
  556. R_ConcatTransforms
  557. ================
  558. */
  559. void ConcatTransforms (const matrix3x4_t& in1, const matrix3x4_t& in2, matrix3x4_t& out)
  560. {
  561. #if 0
  562. // test for ones that'll be 2x faster
  563. if ( (((size_t)&in1) % 16) == 0 && (((size_t)&in2) % 16) == 0 && (((size_t)&out) % 16) == 0 )
  564. {
  565. ConcatTransforms_Aligned( in1, in2, out );
  566. return;
  567. }
  568. #endif
  569. fltx4 lastMask = *(fltx4 *)(&g_SIMD_ComponentMask[3]);
  570. fltx4 rowA0 = LoadUnalignedSIMD( in1.m_flMatVal[0] );
  571. fltx4 rowA1 = LoadUnalignedSIMD( in1.m_flMatVal[1] );
  572. fltx4 rowA2 = LoadUnalignedSIMD( in1.m_flMatVal[2] );
  573. fltx4 rowB0 = LoadUnalignedSIMD( in2.m_flMatVal[0] );
  574. fltx4 rowB1 = LoadUnalignedSIMD( in2.m_flMatVal[1] );
  575. fltx4 rowB2 = LoadUnalignedSIMD( in2.m_flMatVal[2] );
  576. // now we have the rows of m0 and the columns of m1
  577. // first output row
  578. fltx4 A0 = SplatXSIMD(rowA0);
  579. fltx4 A1 = SplatYSIMD(rowA0);
  580. fltx4 A2 = SplatZSIMD(rowA0);
  581. fltx4 mul00 = MulSIMD( A0, rowB0 );
  582. fltx4 mul01 = MulSIMD( A1, rowB1 );
  583. fltx4 mul02 = MulSIMD( A2, rowB2 );
  584. fltx4 out0 = AddSIMD( mul00, AddSIMD(mul01,mul02) );
  585. // second output row
  586. A0 = SplatXSIMD(rowA1);
  587. A1 = SplatYSIMD(rowA1);
  588. A2 = SplatZSIMD(rowA1);
  589. fltx4 mul10 = MulSIMD( A0, rowB0 );
  590. fltx4 mul11 = MulSIMD( A1, rowB1 );
  591. fltx4 mul12 = MulSIMD( A2, rowB2 );
  592. fltx4 out1 = AddSIMD( mul10, AddSIMD(mul11,mul12) );
  593. // third output row
  594. A0 = SplatXSIMD(rowA2);
  595. A1 = SplatYSIMD(rowA2);
  596. A2 = SplatZSIMD(rowA2);
  597. fltx4 mul20 = MulSIMD( A0, rowB0 );
  598. fltx4 mul21 = MulSIMD( A1, rowB1 );
  599. fltx4 mul22 = MulSIMD( A2, rowB2 );
  600. fltx4 out2 = AddSIMD( mul20, AddSIMD(mul21,mul22) );
  601. // add in translation vector
  602. A0 = AndSIMD(rowA0,lastMask);
  603. A1 = AndSIMD(rowA1,lastMask);
  604. A2 = AndSIMD(rowA2,lastMask);
  605. out0 = AddSIMD(out0, A0);
  606. out1 = AddSIMD(out1, A1);
  607. out2 = AddSIMD(out2, A2);
  608. // write to output
  609. StoreUnalignedSIMD( out.m_flMatVal[0], out0 );
  610. StoreUnalignedSIMD( out.m_flMatVal[1], out1 );
  611. StoreUnalignedSIMD( out.m_flMatVal[2], out2 );
  612. }
  613. /*
  614. ===================
  615. FloorDivMod
  616. Returns mathematically correct (floor-based) quotient and remainder for
  617. numer and denom, both of which should contain no fractional part. The
  618. quotient must fit in 32 bits.
  619. ====================
  620. */
  621. void FloorDivMod (double numer, double denom, int *quotient,
  622. int *rem)
  623. {
  624. Assert( s_bMathlibInitialized );
  625. int q, r;
  626. double x;
  627. #ifdef PARANOID
  628. if (denom <= 0.0)
  629. Sys_Error ("FloorDivMod: bad denominator %d\n", denom);
  630. // if ((floor(numer) != numer) || (floor(denom) != denom))
  631. // Sys_Error ("FloorDivMod: non-integer numer or denom %f %f\n",
  632. // numer, denom);
  633. #endif
  634. if (numer >= 0.0)
  635. {
  636. x = floor(numer / denom);
  637. q = (int)x;
  638. r = Floor2Int(numer - (x * denom));
  639. }
  640. else
  641. {
  642. //
  643. // perform operations with positive values, and fix mod to make floor-based
  644. //
  645. x = floor(-numer / denom);
  646. q = -(int)x;
  647. r = Floor2Int(-numer - (x * denom));
  648. if (r != 0)
  649. {
  650. q--;
  651. r = (int)denom - r;
  652. }
  653. }
  654. *quotient = q;
  655. *rem = r;
  656. }
  657. /*
  658. ===================
  659. GreatestCommonDivisor
  660. ====================
  661. */
  662. int GreatestCommonDivisor (int i1, int i2)
  663. {
  664. Assert( s_bMathlibInitialized );
  665. if (i1 > i2)
  666. {
  667. if (i2 == 0)
  668. return (i1);
  669. return GreatestCommonDivisor (i2, i1 % i2);
  670. }
  671. else
  672. {
  673. if (i1 == 0)
  674. return (i2);
  675. return GreatestCommonDivisor (i1, i2 % i1);
  676. }
  677. }
  678. bool IsDenormal( const float &val )
  679. {
  680. const int x = *reinterpret_cast <const int *> (&val); // needs 32-bit int
  681. const int abs_mantissa = x & 0x007FFFFF;
  682. const int biased_exponent = x & 0x7F800000;
  683. return ( biased_exponent == 0 && abs_mantissa != 0 );
  684. }
  685. int SignbitsForPlane (cplane_t *out)
  686. {
  687. Assert( s_bMathlibInitialized );
  688. int bits, j;
  689. // for fast box on planeside test
  690. bits = 0;
  691. for (j=0 ; j<3 ; j++)
  692. {
  693. if (out->normal[j] < 0)
  694. bits |= 1<<j;
  695. }
  696. return bits;
  697. }
  698. /*
  699. ==================
  700. BoxOnPlaneSide
  701. Returns 1, 2, or 1 + 2
  702. ==================
  703. */
  704. int __cdecl BoxOnPlaneSide (const float *emins, const float *emaxs, const cplane_t *p)
  705. {
  706. Assert( s_bMathlibInitialized );
  707. float dist1, dist2;
  708. int sides;
  709. // fast axial cases
  710. if (p->type < 3)
  711. {
  712. if (p->dist <= emins[p->type])
  713. return 1;
  714. if (p->dist >= emaxs[p->type])
  715. return 2;
  716. return 3;
  717. }
  718. // general case
  719. switch (p->signbits)
  720. {
  721. case 0:
  722. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  723. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  724. break;
  725. case 1:
  726. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  727. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  728. break;
  729. case 2:
  730. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  731. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  732. break;
  733. case 3:
  734. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  735. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  736. break;
  737. case 4:
  738. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  739. dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  740. break;
  741. case 5:
  742. dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  743. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  744. break;
  745. case 6:
  746. dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  747. dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  748. break;
  749. case 7:
  750. dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  751. dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  752. break;
  753. default:
  754. dist1 = dist2 = 0; // shut up compiler
  755. Assert( 0 );
  756. break;
  757. }
  758. sides = 0;
  759. if (dist1 >= p->dist)
  760. sides = 1;
  761. if (dist2 < p->dist)
  762. sides |= 2;
  763. Assert( sides != 0 );
  764. return sides;
  765. }
  766. //-----------------------------------------------------------------------------
  767. // Euler QAngle -> Basis Vectors
  768. //-----------------------------------------------------------------------------
  769. void AngleVectors (const QAngle &angles, Vector *forward)
  770. {
  771. Assert( s_bMathlibInitialized );
  772. Assert( forward );
  773. float sp, sy, cp, cy;
  774. SinCos( DEG2RAD( angles[YAW] ), &sy, &cy );
  775. SinCos( DEG2RAD( angles[PITCH] ), &sp, &cp );
  776. forward->x = cp*cy;
  777. forward->y = cp*sy;
  778. forward->z = -sp;
  779. }
  780. //-----------------------------------------------------------------------------
  781. // Euler QAngle -> Basis Vectors. Each vector is optional
  782. //-----------------------------------------------------------------------------
  783. void AngleVectors( const QAngle &angles, Vector *forward, Vector *right, Vector *up )
  784. {
  785. Assert( s_bMathlibInitialized );
  786. float sr, sp, sy, cr, cp, cy;
  787. #ifdef _X360
  788. fltx4 radians, scale, sine, cosine;
  789. radians = LoadUnaligned3SIMD( angles.Base() );
  790. scale = ReplicateX4( M_PI_F / 180.f );
  791. radians = MulSIMD( radians, scale );
  792. SinCos3SIMD( sine, cosine, radians );
  793. sp = SubFloat( sine, 0 ); sy = SubFloat( sine, 1 ); sr = SubFloat( sine, 2 );
  794. cp = SubFloat( cosine, 0 ); cy = SubFloat( cosine, 1 ); cr = SubFloat( cosine, 2 );
  795. #else
  796. SinCos( DEG2RAD( angles[YAW] ), &sy, &cy );
  797. SinCos( DEG2RAD( angles[PITCH] ), &sp, &cp );
  798. SinCos( DEG2RAD( angles[ROLL] ), &sr, &cr );
  799. #endif
  800. if (forward)
  801. {
  802. forward->x = cp*cy;
  803. forward->y = cp*sy;
  804. forward->z = -sp;
  805. }
  806. if (right)
  807. {
  808. right->x = (-1*sr*sp*cy+-1*cr*-sy);
  809. right->y = (-1*sr*sp*sy+-1*cr*cy);
  810. right->z = -1*sr*cp;
  811. }
  812. if (up)
  813. {
  814. up->x = (cr*sp*cy+-sr*-sy);
  815. up->y = (cr*sp*sy+-sr*cy);
  816. up->z = cr*cp;
  817. }
  818. }
  819. //-----------------------------------------------------------------------------
  820. // Euler QAngle -> Basis Vectors transposed
  821. //-----------------------------------------------------------------------------
  822. void AngleVectorsTranspose (const QAngle &angles, Vector *forward, Vector *right, Vector *up)
  823. {
  824. Assert( s_bMathlibInitialized );
  825. float sr, sp, sy, cr, cp, cy;
  826. SinCos( DEG2RAD( angles[YAW] ), &sy, &cy );
  827. SinCos( DEG2RAD( angles[PITCH] ), &sp, &cp );
  828. SinCos( DEG2RAD( angles[ROLL] ), &sr, &cr );
  829. if (forward)
  830. {
  831. forward->x = cp*cy;
  832. forward->y = (sr*sp*cy+cr*-sy);
  833. forward->z = (cr*sp*cy+-sr*-sy);
  834. }
  835. if (right)
  836. {
  837. right->x = cp*sy;
  838. right->y = (sr*sp*sy+cr*cy);
  839. right->z = (cr*sp*sy+-sr*cy);
  840. }
  841. if (up)
  842. {
  843. up->x = -sp;
  844. up->y = sr*cp;
  845. up->z = cr*cp;
  846. }
  847. }
  848. //-----------------------------------------------------------------------------
  849. // Forward direction vector -> Euler angles
  850. //-----------------------------------------------------------------------------
  851. void VectorAngles( const Vector& forward, QAngle &angles )
  852. {
  853. Assert( s_bMathlibInitialized );
  854. float tmp, yaw, pitch;
  855. if (forward[1] == 0 && forward[0] == 0)
  856. {
  857. yaw = 0;
  858. if (forward[2] > 0)
  859. pitch = 270;
  860. else
  861. pitch = 90;
  862. }
  863. else
  864. {
  865. yaw = (atan2(forward[1], forward[0]) * 180 / M_PI);
  866. if (yaw < 0)
  867. yaw += 360;
  868. tmp = FastSqrt (forward[0]*forward[0] + forward[1]*forward[1]);
  869. pitch = (atan2(-forward[2], tmp) * 180 / M_PI);
  870. if (pitch < 0)
  871. pitch += 360;
  872. }
  873. angles[0] = pitch;
  874. angles[1] = yaw;
  875. angles[2] = 0;
  876. }
  877. //-----------------------------------------------------------------------------
  878. // Forward direction vector with a reference up vector -> Euler angles
  879. //-----------------------------------------------------------------------------
  880. void VectorAngles( const Vector &forward, const Vector &pseudoup, QAngle &angles )
  881. {
  882. Assert( s_bMathlibInitialized );
  883. Vector left;
  884. CrossProduct( pseudoup, forward, left );
  885. VectorNormalizeFast( left );
  886. float xyDist = sqrtf( forward[0] * forward[0] + forward[1] * forward[1] );
  887. // enough here to get angles?
  888. if ( xyDist > 0.001f )
  889. {
  890. // (yaw) y = ATAN( forward.y, forward.x ); -- in our space, forward is the X axis
  891. angles[1] = RAD2DEG( atan2f( forward[1], forward[0] ) );
  892. // The engine does pitch inverted from this, but we always end up negating it in the DLL
  893. // UNDONE: Fix the engine to make it consistent
  894. // (pitch) x = ATAN( -forward.z, sqrt(forward.x*forward.x+forward.y*forward.y) );
  895. angles[0] = RAD2DEG( atan2f( -forward[2], xyDist ) );
  896. float up_z = (left[1] * forward[0]) - (left[0] * forward[1]);
  897. // (roll) z = ATAN( left.z, up.z );
  898. angles[2] = RAD2DEG( atan2f( left[2], up_z ) );
  899. }
  900. else // forward is mostly Z, gimbal lock-
  901. {
  902. // (yaw) y = ATAN( -left.x, left.y ); -- forward is mostly z, so use right for yaw
  903. angles[1] = RAD2DEG( atan2f( -left[0], left[1] ) ); //This was originally copied from the "void MatrixAngles( const matrix3x4_t& matrix, float *angles )" code, and it's 180 degrees off, negated the values and it all works now (Dave Kircher)
  904. // The engine does pitch inverted from this, but we always end up negating it in the DLL
  905. // UNDONE: Fix the engine to make it consistent
  906. // (pitch) x = ATAN( -forward.z, sqrt(forward.x*forward.x+forward.y*forward.y) );
  907. angles[0] = RAD2DEG( atan2f( -forward[2], xyDist ) );
  908. // Assume no roll in this case as one degree of freedom has been lost (i.e. yaw == roll)
  909. angles[2] = 0;
  910. }
  911. }
  912. void SetIdentityMatrix( matrix3x4_t& matrix )
  913. {
  914. memset( matrix.Base(), 0, sizeof(float)*3*4 );
  915. matrix[0][0] = 1.0;
  916. matrix[1][1] = 1.0;
  917. matrix[2][2] = 1.0;
  918. }
  919. //-----------------------------------------------------------------------------
  920. // Builds a scale matrix
  921. //-----------------------------------------------------------------------------
  922. void SetScaleMatrix( float x, float y, float z, matrix3x4_t &dst )
  923. {
  924. dst[0][0] = x; dst[0][1] = 0.0f; dst[0][2] = 0.0f; dst[0][3] = 0.0f;
  925. dst[1][0] = 0.0f; dst[1][1] = y; dst[1][2] = 0.0f; dst[1][3] = 0.0f;
  926. dst[2][0] = 0.0f; dst[2][1] = 0.0f; dst[2][2] = z; dst[2][3] = 0.0f;
  927. }
  928. //-----------------------------------------------------------------------------
  929. // Purpose: Builds the matrix for a counterclockwise rotation about an arbitrary axis.
  930. //
  931. // | ax2 + (1 - ax2)cosQ axay(1 - cosQ) - azsinQ azax(1 - cosQ) + aysinQ |
  932. // Ra(Q) = | axay(1 - cosQ) + azsinQ ay2 + (1 - ay2)cosQ ayaz(1 - cosQ) - axsinQ |
  933. // | azax(1 - cosQ) - aysinQ ayaz(1 - cosQ) + axsinQ az2 + (1 - az2)cosQ |
  934. //
  935. // Input : mat -
  936. // vAxisOrRot -
  937. // angle -
  938. //-----------------------------------------------------------------------------
  939. void MatrixBuildRotationAboutAxis( const Vector &vAxisOfRot, float angleDegrees, matrix3x4_t &dst )
  940. {
  941. float radians;
  942. float axisXSquared;
  943. float axisYSquared;
  944. float axisZSquared;
  945. float fSin;
  946. float fCos;
  947. radians = angleDegrees * ( M_PI / 180.0 );
  948. fSin = sin( radians );
  949. fCos = cos( radians );
  950. axisXSquared = vAxisOfRot[0] * vAxisOfRot[0];
  951. axisYSquared = vAxisOfRot[1] * vAxisOfRot[1];
  952. axisZSquared = vAxisOfRot[2] * vAxisOfRot[2];
  953. // Column 0:
  954. dst[0][0] = axisXSquared + (1 - axisXSquared) * fCos;
  955. dst[1][0] = vAxisOfRot[0] * vAxisOfRot[1] * (1 - fCos) + vAxisOfRot[2] * fSin;
  956. dst[2][0] = vAxisOfRot[2] * vAxisOfRot[0] * (1 - fCos) - vAxisOfRot[1] * fSin;
  957. // Column 1:
  958. dst[0][1] = vAxisOfRot[0] * vAxisOfRot[1] * (1 - fCos) - vAxisOfRot[2] * fSin;
  959. dst[1][1] = axisYSquared + (1 - axisYSquared) * fCos;
  960. dst[2][1] = vAxisOfRot[1] * vAxisOfRot[2] * (1 - fCos) + vAxisOfRot[0] * fSin;
  961. // Column 2:
  962. dst[0][2] = vAxisOfRot[2] * vAxisOfRot[0] * (1 - fCos) + vAxisOfRot[1] * fSin;
  963. dst[1][2] = vAxisOfRot[1] * vAxisOfRot[2] * (1 - fCos) - vAxisOfRot[0] * fSin;
  964. dst[2][2] = axisZSquared + (1 - axisZSquared) * fCos;
  965. // Column 3:
  966. dst[0][3] = 0;
  967. dst[1][3] = 0;
  968. dst[2][3] = 0;
  969. }
  970. //-----------------------------------------------------------------------------
  971. // Computes the transpose
  972. //-----------------------------------------------------------------------------
  973. void MatrixTranspose( matrix3x4_t& mat )
  974. {
  975. vec_t tmp;
  976. tmp = mat[0][1]; mat[0][1] = mat[1][0]; mat[1][0] = tmp;
  977. tmp = mat[0][2]; mat[0][2] = mat[2][0]; mat[2][0] = tmp;
  978. tmp = mat[1][2]; mat[1][2] = mat[2][1]; mat[2][1] = tmp;
  979. }
  980. void MatrixTranspose( const matrix3x4_t& src, matrix3x4_t& dst )
  981. {
  982. dst[0][0] = src[0][0]; dst[0][1] = src[1][0]; dst[0][2] = src[2][0]; dst[0][3] = 0.0f;
  983. dst[1][0] = src[0][1]; dst[1][1] = src[1][1]; dst[1][2] = src[2][1]; dst[1][3] = 0.0f;
  984. dst[2][0] = src[0][2]; dst[2][1] = src[1][2]; dst[2][2] = src[2][2]; dst[2][3] = 0.0f;
  985. }
  986. //-----------------------------------------------------------------------------
  987. // Purpose: converts engine euler angles into a matrix
  988. // Input : vec3_t angles - PITCH, YAW, ROLL
  989. // Output : *matrix - left-handed column matrix
  990. // the basis vectors for the rotations will be in the columns as follows:
  991. // matrix[][0] is forward
  992. // matrix[][1] is left
  993. // matrix[][2] is up
  994. //-----------------------------------------------------------------------------
  995. void AngleMatrix( RadianEuler const &angles, const Vector &position, matrix3x4_t& matrix )
  996. {
  997. AngleMatrix( angles, matrix );
  998. MatrixSetColumn( position, 3, matrix );
  999. }
  1000. void AngleMatrix( const RadianEuler& angles, matrix3x4_t& matrix )
  1001. {
  1002. QAngle quakeEuler( RAD2DEG( angles.y ), RAD2DEG( angles.z ), RAD2DEG( angles.x ) );
  1003. AngleMatrix( quakeEuler, matrix );
  1004. }
  1005. void AngleMatrix( const QAngle &angles, const Vector &position, matrix3x4_t& matrix )
  1006. {
  1007. AngleMatrix( angles, matrix );
  1008. MatrixSetColumn( position, 3, matrix );
  1009. }
  1010. void AngleMatrix( const QAngle &angles, matrix3x4_t& matrix )
  1011. {
  1012. #ifdef _VPROF_MATHLIB
  1013. VPROF_BUDGET( "AngleMatrix", "Mathlib" );
  1014. #endif
  1015. Assert( s_bMathlibInitialized );
  1016. float sr, sp, sy, cr, cp, cy;
  1017. #ifdef _X360
  1018. fltx4 radians, scale, sine, cosine;
  1019. radians = LoadUnaligned3SIMD( angles.Base() );
  1020. scale = ReplicateX4( M_PI_F / 180.f );
  1021. radians = MulSIMD( radians, scale );
  1022. SinCos3SIMD( sine, cosine, radians );
  1023. sp = SubFloat( sine, 0 ); sy = SubFloat( sine, 1 ); sr = SubFloat( sine, 2 );
  1024. cp = SubFloat( cosine, 0 ); cy = SubFloat( cosine, 1 ); cr = SubFloat( cosine, 2 );
  1025. #else
  1026. SinCos( DEG2RAD( angles[YAW] ), &sy, &cy );
  1027. SinCos( DEG2RAD( angles[PITCH] ), &sp, &cp );
  1028. SinCos( DEG2RAD( angles[ROLL] ), &sr, &cr );
  1029. #endif
  1030. // matrix = (YAW * PITCH) * ROLL
  1031. matrix[0][0] = cp*cy;
  1032. matrix[1][0] = cp*sy;
  1033. matrix[2][0] = -sp;
  1034. float crcy = cr*cy;
  1035. float crsy = cr*sy;
  1036. float srcy = sr*cy;
  1037. float srsy = sr*sy;
  1038. matrix[0][1] = sp*srcy-crsy;
  1039. matrix[1][1] = sp*srsy+crcy;
  1040. matrix[2][1] = sr*cp;
  1041. matrix[0][2] = (sp*crcy+srsy);
  1042. matrix[1][2] = (sp*crsy-srcy);
  1043. matrix[2][2] = cr*cp;
  1044. matrix[0][3] = 0.0f;
  1045. matrix[1][3] = 0.0f;
  1046. matrix[2][3] = 0.0f;
  1047. }
  1048. void AngleIMatrix( const RadianEuler& angles, matrix3x4_t& matrix )
  1049. {
  1050. QAngle quakeEuler( RAD2DEG( angles.y ), RAD2DEG( angles.z ), RAD2DEG( angles.x ) );
  1051. AngleIMatrix( quakeEuler, matrix );
  1052. }
  1053. void AngleIMatrix (const QAngle& angles, matrix3x4_t& matrix )
  1054. {
  1055. Assert( s_bMathlibInitialized );
  1056. float sr, sp, sy, cr, cp, cy;
  1057. SinCos( DEG2RAD( angles[YAW] ), &sy, &cy );
  1058. SinCos( DEG2RAD( angles[PITCH] ), &sp, &cp );
  1059. SinCos( DEG2RAD( angles[ROLL] ), &sr, &cr );
  1060. // matrix = (YAW * PITCH) * ROLL
  1061. matrix[0][0] = cp*cy;
  1062. matrix[0][1] = cp*sy;
  1063. matrix[0][2] = -sp;
  1064. matrix[1][0] = sr*sp*cy+cr*-sy;
  1065. matrix[1][1] = sr*sp*sy+cr*cy;
  1066. matrix[1][2] = sr*cp;
  1067. matrix[2][0] = (cr*sp*cy+-sr*-sy);
  1068. matrix[2][1] = (cr*sp*sy+-sr*cy);
  1069. matrix[2][2] = cr*cp;
  1070. matrix[0][3] = 0.f;
  1071. matrix[1][3] = 0.f;
  1072. matrix[2][3] = 0.f;
  1073. }
  1074. void AngleIMatrix (const QAngle &angles, const Vector &position, matrix3x4_t &mat )
  1075. {
  1076. AngleIMatrix( angles, mat );
  1077. Vector vecTranslation;
  1078. VectorRotate( position, mat, vecTranslation );
  1079. vecTranslation *= -1.0f;
  1080. MatrixSetColumn( vecTranslation, 3, mat );
  1081. }
  1082. //-----------------------------------------------------------------------------
  1083. // Bounding box construction methods
  1084. //-----------------------------------------------------------------------------
  1085. void ClearBounds (Vector& mins, Vector& maxs)
  1086. {
  1087. Assert( s_bMathlibInitialized );
  1088. mins[0] = mins[1] = mins[2] = 99999;
  1089. maxs[0] = maxs[1] = maxs[2] = -99999;
  1090. }
  1091. void AddPointToBounds (const Vector& v, Vector& mins, Vector& maxs)
  1092. {
  1093. Assert( s_bMathlibInitialized );
  1094. int i;
  1095. vec_t val;
  1096. for (i=0 ; i<3 ; i++)
  1097. {
  1098. val = v[i];
  1099. if (val < mins[i])
  1100. mins[i] = val;
  1101. if (val > maxs[i])
  1102. maxs[i] = val;
  1103. }
  1104. }
  1105. // solve a x^2 + b x + c = 0
  1106. bool SolveQuadratic( float a, float b, float c, float &root1, float &root2 )
  1107. {
  1108. Assert( s_bMathlibInitialized );
  1109. if (a == 0)
  1110. {
  1111. if (b != 0)
  1112. {
  1113. // no x^2 component, it's a linear system
  1114. root1 = root2 = -c / b;
  1115. return true;
  1116. }
  1117. if (c == 0)
  1118. {
  1119. // all zero's
  1120. root1 = root2 = 0;
  1121. return true;
  1122. }
  1123. return false;
  1124. }
  1125. float tmp = b * b - 4.0f * a * c;
  1126. if (tmp < 0)
  1127. {
  1128. // imaginary number, bah, no solution.
  1129. return false;
  1130. }
  1131. tmp = sqrt( tmp );
  1132. root1 = (-b + tmp) / (2.0f * a);
  1133. root2 = (-b - tmp) / (2.0f * a);
  1134. return true;
  1135. }
  1136. // solves for "a, b, c" where "a x^2 + b x + c = y", return true if solution exists
  1137. bool SolveInverseQuadratic( float x1, float y1, float x2, float y2, float x3, float y3, float &a, float &b, float &c )
  1138. {
  1139. float det = (x1 - x2)*(x1 - x3)*(x2 - x3);
  1140. // FIXME: check with some sort of epsilon
  1141. if (det == 0.0)
  1142. return false;
  1143. a = (x3*(-y1 + y2) + x2*(y1 - y3) + x1*(-y2 + y3)) / det;
  1144. b = (x3*x3*(y1 - y2) + x1*x1*(y2 - y3) + x2*x2*(-y1 + y3)) / det;
  1145. c = (x1*x3*(-x1 + x3)*y2 + x2*x2*(x3*y1 - x1*y3) + x2*(-(x3*x3*y1) + x1*x1*y3)) / det;
  1146. return true;
  1147. }
  1148. bool SolveInverseQuadraticMonotonic( float x1, float y1, float x2, float y2, float x3, float y3,
  1149. float &a, float &b, float &c )
  1150. {
  1151. // use SolveInverseQuadratic, but if the sigm of the derivative at the start point is the wrong
  1152. // sign, displace the mid point
  1153. // first, sort parameters
  1154. if (x1>x2)
  1155. {
  1156. V_swap(x1,x2);
  1157. V_swap(y1,y2);
  1158. }
  1159. if (x2>x3)
  1160. {
  1161. V_swap(x2,x3);
  1162. V_swap(y2,y3);
  1163. }
  1164. if (x1>x2)
  1165. {
  1166. V_swap(x1,x2);
  1167. V_swap(y1,y2);
  1168. }
  1169. // this code is not fast. what it does is when the curve would be non-monotonic, slowly shifts
  1170. // the center point closer to the linear line between the endpoints. Should anyone need htis
  1171. // function to be actually fast, it would be fairly easy to change it to be so.
  1172. for(float blend_to_linear_factor=0.0;blend_to_linear_factor<=1.0;blend_to_linear_factor+=0.05)
  1173. {
  1174. float tempy2=(1-blend_to_linear_factor)*y2+blend_to_linear_factor*FLerp(y1,y3,x1,x3,x2);
  1175. if (!SolveInverseQuadratic(x1,y1,x2,tempy2,x3,y3,a,b,c))
  1176. return false;
  1177. float derivative=2.0*a+b;
  1178. if ( (y1<y2) && (y2<y3)) // monotonically increasing
  1179. {
  1180. if (derivative>=0.0)
  1181. return true;
  1182. }
  1183. else
  1184. {
  1185. if ( (y1>y2) && (y2>y3)) // monotonically decreasing
  1186. {
  1187. if (derivative<=0.0)
  1188. return true;
  1189. }
  1190. else
  1191. return true;
  1192. }
  1193. }
  1194. return true;
  1195. }
  1196. // solves for "a, b, c" where "1/(a x^2 + b x + c ) = y", return true if solution exists
  1197. bool SolveInverseReciprocalQuadratic( float x1, float y1, float x2, float y2, float x3, float y3, float &a, float &b, float &c )
  1198. {
  1199. float det = (x1 - x2)*(x1 - x3)*(x2 - x3)*y1*y2*y3;
  1200. // FIXME: check with some sort of epsilon
  1201. if (det == 0.0)
  1202. return false;
  1203. a = (x1*y1*(y2 - y3) + x3*(y1 - y2)*y3 + x2*y2*(-y1 + y3)) / det;
  1204. b = (x2*x2*y2*(y1 - y3) + x3*x3*(-y1 + y2)*y3 + x1*x1*y1*(-y2 + y3)) / det;
  1205. c = (x2*(x2 - x3)*x3*y2*y3 + x1*x1*y1*(x2*y2 - x3*y3) + x1*(-(x2*x2*y1*y2) + x3*x3*y1*y3)) / det;
  1206. return true;
  1207. }
  1208. // Rotate a vector around the Z axis (YAW)
  1209. void VectorYawRotate( const Vector &in, float flYaw, Vector &out)
  1210. {
  1211. Assert( s_bMathlibInitialized );
  1212. if (&in == &out )
  1213. {
  1214. Vector tmp;
  1215. tmp = in;
  1216. VectorYawRotate( tmp, flYaw, out );
  1217. return;
  1218. }
  1219. float sy, cy;
  1220. SinCos( DEG2RAD(flYaw), &sy, &cy );
  1221. out.x = in.x * cy - in.y * sy;
  1222. out.y = in.x * sy + in.y * cy;
  1223. out.z = in.z;
  1224. }
  1225. float Bias( float x, float biasAmt )
  1226. {
  1227. // WARNING: not thread safe
  1228. static float lastAmt = -1;
  1229. static float lastExponent = 0;
  1230. if( lastAmt != biasAmt )
  1231. {
  1232. lastExponent = log( biasAmt ) * -1.4427f; // (-1.4427 = 1 / log(0.5))
  1233. }
  1234. float fRet = pow( x, lastExponent );
  1235. Assert ( !IS_NAN( fRet ) );
  1236. return fRet;
  1237. }
  1238. float Gain( float x, float biasAmt )
  1239. {
  1240. // WARNING: not thread safe
  1241. if( x < 0.5 )
  1242. return 0.5f * Bias( 2*x, 1-biasAmt );
  1243. else
  1244. return 1 - 0.5f * Bias( 2 - 2*x, 1-biasAmt );
  1245. }
  1246. float SmoothCurve( float x )
  1247. {
  1248. // Actual smooth curve. Visualization:
  1249. // http://www.wolframalpha.com/input/?i=plot%5B+0.5+*+%281+-+cos%5B2+*+pi+*+x%5D%29+for+x+%3D+%280%2C+1%29+%5D
  1250. return 0.5f * (1 - cos( 2.0f * M_PI * x ) );
  1251. }
  1252. inline float MovePeak( float x, float flPeakPos )
  1253. {
  1254. // Todo: make this higher-order?
  1255. if( x < flPeakPos )
  1256. return x * 0.5f / flPeakPos;
  1257. else
  1258. return 0.5 + 0.5 * (x - flPeakPos) / (1 - flPeakPos);
  1259. }
  1260. float SmoothCurve_Tweak( float x, float flPeakPos, float flPeakSharpness )
  1261. {
  1262. float flMovedPeak = MovePeak( x, flPeakPos );
  1263. float flSharpened = Gain( flMovedPeak, flPeakSharpness );
  1264. return SmoothCurve( flSharpened );
  1265. }
  1266. //-----------------------------------------------------------------------------
  1267. // make sure quaternions are within 180 degrees of one another, if not, reverse q
  1268. //-----------------------------------------------------------------------------
  1269. void QuaternionAlign( const Quaternion &p, const Quaternion &q, Quaternion &qt )
  1270. {
  1271. Assert( s_bMathlibInitialized );
  1272. // FIXME: can this be done with a quat dot product?
  1273. int i;
  1274. // decide if one of the quaternions is backwards
  1275. float a = 0;
  1276. float b = 0;
  1277. for (i = 0; i < 4; i++)
  1278. {
  1279. a += (p[i]-q[i])*(p[i]-q[i]);
  1280. b += (p[i]+q[i])*(p[i]+q[i]);
  1281. }
  1282. if (a > b)
  1283. {
  1284. for (i = 0; i < 4; i++)
  1285. {
  1286. qt[i] = -q[i];
  1287. }
  1288. }
  1289. else if (&qt != &q)
  1290. {
  1291. for (i = 0; i < 4; i++)
  1292. {
  1293. qt[i] = q[i];
  1294. }
  1295. }
  1296. }
  1297. //-----------------------------------------------------------------------------
  1298. // Do a piecewise addition of the quaternion elements. This actually makes little
  1299. // mathematical sense, but it's a cheap way to simulate a slerp.
  1300. //-----------------------------------------------------------------------------
  1301. void QuaternionBlend( const Quaternion &p, const Quaternion &q, float t, Quaternion &qt )
  1302. {
  1303. Assert( s_bMathlibInitialized );
  1304. #if ALLOW_SIMD_QUATERNION_MATH
  1305. fltx4 psimd, qsimd, qtsimd;
  1306. psimd = LoadUnalignedSIMD( p.Base() );
  1307. qsimd = LoadUnalignedSIMD( q.Base() );
  1308. qtsimd = QuaternionBlendSIMD( psimd, qsimd, t );
  1309. StoreUnalignedSIMD( qt.Base(), qtsimd );
  1310. #else
  1311. // decide if one of the quaternions is backwards
  1312. Quaternion q2;
  1313. QuaternionAlign( p, q, q2 );
  1314. QuaternionBlendNoAlign( p, q2, t, qt );
  1315. #endif
  1316. }
  1317. void QuaternionBlendNoAlign( const Quaternion &p, const Quaternion &q, float t, Quaternion &qt )
  1318. {
  1319. Assert( s_bMathlibInitialized );
  1320. float sclp, sclq;
  1321. int i;
  1322. // 0.0 returns p, 1.0 return q.
  1323. sclp = 1.0f - t;
  1324. sclq = t;
  1325. for (i = 0; i < 4; i++) {
  1326. qt[i] = sclp * p[i] + sclq * q[i];
  1327. }
  1328. QuaternionNormalize( qt );
  1329. }
  1330. void QuaternionIdentityBlend( const Quaternion &p, float t, Quaternion &qt )
  1331. {
  1332. Assert( s_bMathlibInitialized );
  1333. float sclp;
  1334. sclp = 1.0f - t;
  1335. qt.x = p.x * sclp;
  1336. qt.y = p.y * sclp;
  1337. qt.z = p.z * sclp;
  1338. if (qt.w < 0.0)
  1339. {
  1340. qt.w = p.w * sclp - t;
  1341. }
  1342. else
  1343. {
  1344. qt.w = p.w * sclp + t;
  1345. }
  1346. QuaternionNormalize( qt );
  1347. }
  1348. //-----------------------------------------------------------------------------
  1349. // Quaternion sphereical linear interpolation
  1350. //-----------------------------------------------------------------------------
  1351. void QuaternionSlerp( const Quaternion &p, const Quaternion &q, float t, Quaternion &qt )
  1352. {
  1353. Quaternion q2;
  1354. // 0.0 returns p, 1.0 return q.
  1355. // decide if one of the quaternions is backwards
  1356. QuaternionAlign( p, q, q2 );
  1357. QuaternionSlerpNoAlign( p, q2, t, qt );
  1358. }
  1359. void QuaternionSlerpNoAlign( const Quaternion &p, const Quaternion &q, float t, Quaternion &qt )
  1360. {
  1361. Assert( s_bMathlibInitialized );
  1362. float omega, cosom, sinom, sclp, sclq;
  1363. int i;
  1364. // 0.0 returns p, 1.0 return q.
  1365. cosom = p[0]*q[0] + p[1]*q[1] + p[2]*q[2] + p[3]*q[3];
  1366. if ((1.0f + cosom) > 0.000001f) {
  1367. if ((1.0f - cosom) > 0.000001f) {
  1368. omega = acos( cosom );
  1369. sinom = sin( omega );
  1370. sclp = sin( (1.0f - t)*omega) / sinom;
  1371. sclq = sin( t*omega ) / sinom;
  1372. }
  1373. else {
  1374. // TODO: add short circuit for cosom == 1.0f?
  1375. sclp = 1.0f - t;
  1376. sclq = t;
  1377. }
  1378. for (i = 0; i < 4; i++) {
  1379. qt[i] = sclp * p[i] + sclq * q[i];
  1380. }
  1381. }
  1382. else {
  1383. Assert( &qt != &q );
  1384. qt[0] = -q[1];
  1385. qt[1] = q[0];
  1386. qt[2] = -q[3];
  1387. qt[3] = q[2];
  1388. sclp = sin( (1.0f - t) * (0.5f * M_PI));
  1389. sclq = sin( t * (0.5f * M_PI));
  1390. for (i = 0; i < 3; i++) {
  1391. qt[i] = sclp * p[i] + sclq * qt[i];
  1392. }
  1393. }
  1394. Assert( qt.IsValid() );
  1395. }
  1396. //-----------------------------------------------------------------------------
  1397. // Purpose: Returns the angular delta between the two normalized quaternions in degrees.
  1398. //-----------------------------------------------------------------------------
  1399. float QuaternionAngleDiff( const Quaternion &p, const Quaternion &q )
  1400. {
  1401. #if 1
  1402. // this code path is here for 2 reasons:
  1403. // 1 - acos maps 1-epsilon to values much larger than epsilon (vs asin, which maps epsilon to itself)
  1404. // this means that in floats, anything below ~0.05 degrees truncates to 0
  1405. // 2 - normalized quaternions are frequently slightly non-normalized due to float precision issues,
  1406. // and the epsilon off of normalized can be several percents of a degree
  1407. Quaternion qInv, diff;
  1408. QuaternionConjugate( q, qInv );
  1409. QuaternionMult( p, qInv, diff );
  1410. // Note if the quaternion is slightly non-normalized the square root below may be more than 1,
  1411. // the value is clamped to one otherwise it may result in asin() returning an undefined result.
  1412. float sinang = MIN( 1.0f, sqrt( diff.x * diff.x + diff.y * diff.y + diff.z * diff.z ) );
  1413. float angle = RAD2DEG( 2 * asin( sinang ) );
  1414. return angle;
  1415. #else
  1416. Quaternion q2;
  1417. QuaternionAlign( p, q, q2 );
  1418. Assert( s_bMathlibInitialized );
  1419. float cosom = p.x * q2.x + p.y * q2.y + p.z * q2.z + p.w * q2.w;
  1420. if ( cosom > -1.0f )
  1421. {
  1422. if ( cosom < 1.0f )
  1423. {
  1424. float omega = 2 * fabs( acos( cosom ) );
  1425. return RAD2DEG( omega );
  1426. }
  1427. return 0.0f;
  1428. }
  1429. return 180.0f;
  1430. #endif
  1431. }
  1432. void QuaternionConjugate( const Quaternion &p, Quaternion &q )
  1433. {
  1434. Assert( s_bMathlibInitialized );
  1435. Assert( q.IsValid() );
  1436. q.x = -p.x;
  1437. q.y = -p.y;
  1438. q.z = -p.z;
  1439. q.w = p.w;
  1440. }
  1441. void QuaternionInvert( const Quaternion &p, Quaternion &q )
  1442. {
  1443. Assert( s_bMathlibInitialized );
  1444. Assert( q.IsValid() );
  1445. QuaternionConjugate( p, q );
  1446. float magnitudeSqr = QuaternionDotProduct( p, p );
  1447. Assert( magnitudeSqr );
  1448. if ( magnitudeSqr )
  1449. {
  1450. float inv = 1.0f / magnitudeSqr;
  1451. q.x *= inv;
  1452. q.y *= inv;
  1453. q.z *= inv;
  1454. q.w *= inv;
  1455. }
  1456. }
  1457. //-----------------------------------------------------------------------------
  1458. // Make sure the quaternion is of unit length
  1459. //-----------------------------------------------------------------------------
  1460. float QuaternionNormalize( Quaternion &q )
  1461. {
  1462. Assert( s_bMathlibInitialized );
  1463. float radius, iradius;
  1464. Assert( q.IsValid() );
  1465. radius = q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];
  1466. if ( radius ) // > FLT_EPSILON && ((radius < 1.0f - 4*FLT_EPSILON) || (radius > 1.0f + 4*FLT_EPSILON))
  1467. {
  1468. radius = sqrt(radius);
  1469. iradius = 1.0f/radius;
  1470. q[3] *= iradius;
  1471. q[2] *= iradius;
  1472. q[1] *= iradius;
  1473. q[0] *= iradius;
  1474. }
  1475. return radius;
  1476. }
  1477. void QuaternionScale( const Quaternion &p, float t, Quaternion &q )
  1478. {
  1479. Assert( s_bMathlibInitialized );
  1480. #if 0
  1481. Quaternion p0;
  1482. Quaternion q;
  1483. p0.Init( 0.0, 0.0, 0.0, 1.0 );
  1484. // slerp in "reverse order" so that p doesn't get realigned
  1485. QuaternionSlerp( p, p0, 1.0 - fabs( t ), q );
  1486. if (t < 0.0)
  1487. {
  1488. q.w = -q.w;
  1489. }
  1490. #else
  1491. float r;
  1492. // FIXME: nick, this isn't overly sensitive to accuracy, and it may be faster to
  1493. // use the cos part (w) of the quaternion (sin(omega)*N,cos(omega)) to figure the new scale.
  1494. float sinom = sqrt( DotProduct( &p.x, &p.x ) );
  1495. sinom = min( sinom, 1.f );
  1496. float sinsom = sin( asin( sinom ) * t );
  1497. t = sinsom / (sinom + FLT_EPSILON);
  1498. VectorScale( &p.x, t, &q.x );
  1499. // rescale rotation
  1500. r = 1.0f - sinsom * sinsom;
  1501. // Assert( r >= 0 );
  1502. if (r < 0.0f)
  1503. r = 0.0f;
  1504. r = sqrt( r );
  1505. // keep sign of rotation
  1506. if (p.w < 0)
  1507. q.w = -r;
  1508. else
  1509. q.w = r;
  1510. #endif
  1511. Assert( q.IsValid() );
  1512. return;
  1513. }
  1514. void QuaternionAdd( const Quaternion &p, const Quaternion &q, Quaternion &qt )
  1515. {
  1516. Assert( s_bMathlibInitialized );
  1517. Assert( p.IsValid() );
  1518. Assert( q.IsValid() );
  1519. // decide if one of the quaternions is backwards
  1520. Quaternion q2;
  1521. QuaternionAlign( p, q, q2 );
  1522. // is this right???
  1523. qt[0] = p[0] + q2[0];
  1524. qt[1] = p[1] + q2[1];
  1525. qt[2] = p[2] + q2[2];
  1526. qt[3] = p[3] + q2[3];
  1527. return;
  1528. }
  1529. float QuaternionDotProduct( const Quaternion &p, const Quaternion &q )
  1530. {
  1531. Assert( s_bMathlibInitialized );
  1532. Assert( p.IsValid() );
  1533. Assert( q.IsValid() );
  1534. return p.x * q.x + p.y * q.y + p.z * q.z + p.w * q.w;
  1535. }
  1536. // qt = p * q
  1537. void QuaternionMult( const Quaternion &p, const Quaternion &q, Quaternion &qt )
  1538. {
  1539. Assert( s_bMathlibInitialized );
  1540. Assert( p.IsValid() );
  1541. Assert( q.IsValid() );
  1542. if (&p == &qt)
  1543. {
  1544. Quaternion p2 = p;
  1545. QuaternionMult( p2, q, qt );
  1546. return;
  1547. }
  1548. // decide if one of the quaternions is backwards
  1549. Quaternion q2;
  1550. QuaternionAlign( p, q, q2 );
  1551. qt.x = p.x * q2.w + p.y * q2.z - p.z * q2.y + p.w * q2.x;
  1552. qt.y = -p.x * q2.z + p.y * q2.w + p.z * q2.x + p.w * q2.y;
  1553. qt.z = p.x * q2.y - p.y * q2.x + p.z * q2.w + p.w * q2.z;
  1554. qt.w = -p.x * q2.x - p.y * q2.y - p.z * q2.z + p.w * q2.w;
  1555. }
  1556. void QuaternionMatrix( const Quaternion &q, const Vector &pos, matrix3x4_t& matrix )
  1557. {
  1558. if ( !HushAsserts() )
  1559. {
  1560. Assert( pos.IsValid() );
  1561. }
  1562. QuaternionMatrix( q, matrix );
  1563. matrix[0][3] = pos.x;
  1564. matrix[1][3] = pos.y;
  1565. matrix[2][3] = pos.z;
  1566. }
  1567. void QuaternionMatrix( const Quaternion &q, matrix3x4_t& matrix )
  1568. {
  1569. Assert( s_bMathlibInitialized );
  1570. if ( !HushAsserts() )
  1571. {
  1572. Assert( q.IsValid() );
  1573. }
  1574. #ifdef _VPROF_MATHLIB
  1575. VPROF_BUDGET( "QuaternionMatrix", "Mathlib" );
  1576. #endif
  1577. // Original code
  1578. // This should produce the same code as below with optimization, but looking at the assmebly,
  1579. // it doesn't. There are 7 extra multiplies in the release build of this, go figure.
  1580. #if 1
  1581. matrix[0][0] = 1.0 - 2.0 * q.y * q.y - 2.0 * q.z * q.z;
  1582. matrix[1][0] = 2.0 * q.x * q.y + 2.0 * q.w * q.z;
  1583. matrix[2][0] = 2.0 * q.x * q.z - 2.0 * q.w * q.y;
  1584. matrix[0][1] = 2.0f * q.x * q.y - 2.0f * q.w * q.z;
  1585. matrix[1][1] = 1.0f - 2.0f * q.x * q.x - 2.0f * q.z * q.z;
  1586. matrix[2][1] = 2.0f * q.y * q.z + 2.0f * q.w * q.x;
  1587. matrix[0][2] = 2.0f * q.x * q.z + 2.0f * q.w * q.y;
  1588. matrix[1][2] = 2.0f * q.y * q.z - 2.0f * q.w * q.x;
  1589. matrix[2][2] = 1.0f - 2.0f * q.x * q.x - 2.0f * q.y * q.y;
  1590. matrix[0][3] = 0.0f;
  1591. matrix[1][3] = 0.0f;
  1592. matrix[2][3] = 0.0f;
  1593. #else
  1594. float wx, wy, wz, xx, yy, yz, xy, xz, zz, x2, y2, z2;
  1595. // precalculate common multiplitcations
  1596. x2 = q.x + q.x;
  1597. y2 = q.y + q.y;
  1598. z2 = q.z + q.z;
  1599. xx = q.x * x2;
  1600. xy = q.x * y2;
  1601. xz = q.x * z2;
  1602. yy = q.y * y2;
  1603. yz = q.y * z2;
  1604. zz = q.z * z2;
  1605. wx = q.w * x2;
  1606. wy = q.w * y2;
  1607. wz = q.w * z2;
  1608. matrix[0][0] = 1.0 - (yy + zz);
  1609. matrix[0][1] = xy - wz;
  1610. matrix[0][2] = xz + wy;
  1611. matrix[0][3] = 0.0f;
  1612. matrix[1][0] = xy + wz;
  1613. matrix[1][1] = 1.0 - (xx + zz);
  1614. matrix[1][2] = yz - wx;
  1615. matrix[1][3] = 0.0f;
  1616. matrix[2][0] = xz - wy;
  1617. matrix[2][1] = yz + wx;
  1618. matrix[2][2] = 1.0 - (xx + yy);
  1619. matrix[2][3] = 0.0f;
  1620. #endif
  1621. }
  1622. //-----------------------------------------------------------------------------
  1623. // Purpose: Converts a quaternion into engine angles
  1624. // Input : *quaternion - q3 + q0.i + q1.j + q2.k
  1625. // *outAngles - PITCH, YAW, ROLL
  1626. //-----------------------------------------------------------------------------
  1627. void QuaternionAngles( const Quaternion &q, QAngle &angles )
  1628. {
  1629. Assert( s_bMathlibInitialized );
  1630. Assert( q.IsValid() );
  1631. #ifdef _VPROF_MATHLIB
  1632. VPROF_BUDGET( "QuaternionAngles", "Mathlib" );
  1633. #endif
  1634. #if 1
  1635. // FIXME: doing it this way calculates too much data, needs to do an optimized version...
  1636. matrix3x4_t matrix;
  1637. QuaternionMatrix( q, matrix );
  1638. MatrixAngles( matrix, angles );
  1639. #else
  1640. float m11, m12, m13, m23, m33;
  1641. m11 = ( 2.0f * q.w * q.w ) + ( 2.0f * q.x * q.x ) - 1.0f;
  1642. m12 = ( 2.0f * q.x * q.y ) + ( 2.0f * q.w * q.z );
  1643. m13 = ( 2.0f * q.x * q.z ) - ( 2.0f * q.w * q.y );
  1644. m23 = ( 2.0f * q.y * q.z ) + ( 2.0f * q.w * q.x );
  1645. m33 = ( 2.0f * q.w * q.w ) + ( 2.0f * q.z * q.z ) - 1.0f;
  1646. // FIXME: this code has a singularity near PITCH +-90
  1647. angles[YAW] = RAD2DEG( atan2(m12, m11) );
  1648. angles[PITCH] = RAD2DEG( asin(-m13) );
  1649. angles[ROLL] = RAD2DEG( atan2(m23, m33) );
  1650. #endif
  1651. Assert( angles.IsValid() );
  1652. }
  1653. //-----------------------------------------------------------------------------
  1654. // Purpose: Converts a quaternion to an axis / angle in degrees
  1655. // (exponential map)
  1656. //-----------------------------------------------------------------------------
  1657. void QuaternionAxisAngle( const Quaternion &q, Vector &axis, float &angle )
  1658. {
  1659. angle = RAD2DEG(2 * acos(q.w));
  1660. if ( angle > 180 )
  1661. {
  1662. angle -= 360;
  1663. }
  1664. axis.x = q.x;
  1665. axis.y = q.y;
  1666. axis.z = q.z;
  1667. VectorNormalize( axis );
  1668. }
  1669. //-----------------------------------------------------------------------------
  1670. // Purpose: Converts an exponential map (ang/axis) to a quaternion
  1671. //-----------------------------------------------------------------------------
  1672. void AxisAngleQuaternion( const Vector &axis, float angle, Quaternion &q )
  1673. {
  1674. float sa, ca;
  1675. SinCos( DEG2RAD(angle) * 0.5f, &sa, &ca );
  1676. q.x = axis.x * sa;
  1677. q.y = axis.y * sa;
  1678. q.z = axis.z * sa;
  1679. q.w = ca;
  1680. }
  1681. //-----------------------------------------------------------------------------
  1682. // Purpose: Converts radian-euler axis aligned angles to a quaternion
  1683. // Input : *pfAngles - Right-handed Euler angles in radians
  1684. // *outQuat - quaternion of form (i,j,k,real)
  1685. //-----------------------------------------------------------------------------
  1686. void AngleQuaternion( const RadianEuler &angles, Quaternion &outQuat )
  1687. {
  1688. Assert( s_bMathlibInitialized );
  1689. // Assert( angles.IsValid() );
  1690. #ifdef _VPROF_MATHLIB
  1691. VPROF_BUDGET( "AngleQuaternion", "Mathlib" );
  1692. #endif
  1693. float sr, sp, sy, cr, cp, cy;
  1694. #ifdef _X360
  1695. fltx4 radians, scale, sine, cosine;
  1696. radians = LoadUnaligned3SIMD( &angles.x );
  1697. scale = ReplicateX4( 0.5f );
  1698. radians = MulSIMD( radians, scale );
  1699. SinCos3SIMD( sine, cosine, radians );
  1700. // NOTE: The ordering here is *different* from the AngleQuaternion below
  1701. // because p, y, r are not in the same locations in QAngle + RadianEuler. Yay!
  1702. sr = SubFloat( sine, 0 ); sp = SubFloat( sine, 1 ); sy = SubFloat( sine, 2 );
  1703. cr = SubFloat( cosine, 0 ); cp = SubFloat( cosine, 1 ); cy = SubFloat( cosine, 2 );
  1704. #else
  1705. SinCos( angles.z * 0.5f, &sy, &cy );
  1706. SinCos( angles.y * 0.5f, &sp, &cp );
  1707. SinCos( angles.x * 0.5f, &sr, &cr );
  1708. #endif
  1709. // NJS: for some reason VC6 wasn't recognizing the common subexpressions:
  1710. float srXcp = sr * cp, crXsp = cr * sp;
  1711. outQuat.x = srXcp*cy-crXsp*sy; // X
  1712. outQuat.y = crXsp*cy+srXcp*sy; // Y
  1713. float crXcp = cr * cp, srXsp = sr * sp;
  1714. outQuat.z = crXcp*sy-srXsp*cy; // Z
  1715. outQuat.w = crXcp*cy+srXsp*sy; // W (real component)
  1716. }
  1717. //-----------------------------------------------------------------------------
  1718. // Purpose: Converts engine-format euler angles to a quaternion
  1719. // Input : angles - Right-handed Euler angles in degrees as follows:
  1720. // [0]: PITCH: Clockwise rotation around the Y axis.
  1721. // [1]: YAW: Counterclockwise rotation around the Z axis.
  1722. // [2]: ROLL: Counterclockwise rotation around the X axis.
  1723. // *outQuat - quaternion of form (i,j,k,real)
  1724. //-----------------------------------------------------------------------------
  1725. void AngleQuaternion( const QAngle &angles, Quaternion &outQuat )
  1726. {
  1727. #ifdef _VPROF_MATHLIB
  1728. VPROF_BUDGET( "AngleQuaternion", "Mathlib" );
  1729. #endif
  1730. float sr, sp, sy, cr, cp, cy;
  1731. #ifdef _X360
  1732. fltx4 radians, scale, sine, cosine;
  1733. radians = LoadUnaligned3SIMD( angles.Base() );
  1734. scale = ReplicateX4( 0.5f * M_PI_F / 180.f );
  1735. radians = MulSIMD( radians, scale );
  1736. SinCos3SIMD( sine, cosine, radians );
  1737. // NOTE: The ordering here is *different* from the AngleQuaternion above
  1738. // because p, y, r are not in the same locations in QAngle + RadianEuler. Yay!
  1739. sp = SubFloat( sine, 0 ); sy = SubFloat( sine, 1 ); sr = SubFloat( sine, 2 );
  1740. cp = SubFloat( cosine, 0 ); cy = SubFloat( cosine, 1 ); cr = SubFloat( cosine, 2 );
  1741. #else
  1742. SinCos( DEG2RAD( angles.y ) * 0.5f, &sy, &cy );
  1743. SinCos( DEG2RAD( angles.x ) * 0.5f, &sp, &cp );
  1744. SinCos( DEG2RAD( angles.z ) * 0.5f, &sr, &cr );
  1745. #endif
  1746. // NJS: for some reason VC6 wasn't recognizing the common subexpressions:
  1747. float srXcp = sr * cp, crXsp = cr * sp;
  1748. outQuat.x = srXcp*cy-crXsp*sy; // X
  1749. outQuat.y = crXsp*cy+srXcp*sy; // Y
  1750. float crXcp = cr * cp, srXsp = sr * sp;
  1751. outQuat.z = crXcp*sy-srXsp*cy; // Z
  1752. outQuat.w = crXcp*cy+srXsp*sy; // W (real component)
  1753. }
  1754. //-----------------------------------------------------------------------------
  1755. // Purpose: Converts a basis to a quaternion
  1756. //-----------------------------------------------------------------------------
  1757. void BasisToQuaternion( const Vector &vecForward, const Vector &vecRight, const Vector &vecUp, Quaternion &q )
  1758. {
  1759. Assert( fabs( vecForward.LengthSqr() - 1.0f ) < 1e-3 );
  1760. Assert( fabs( vecRight.LengthSqr() - 1.0f ) < 1e-3 );
  1761. Assert( fabs( vecUp.LengthSqr() - 1.0f ) < 1e-3 );
  1762. Vector vecLeft;
  1763. VectorMultiply( vecRight, -1.0f, vecLeft );
  1764. // FIXME: Don't know why, but this doesn't match at all with other result
  1765. // so we can't use this super-fast way.
  1766. /*
  1767. // Find the trace of the matrix:
  1768. float flTrace = vecForward.x + vecLeft.y + vecUp.z + 1.0f;
  1769. if ( flTrace > 1e-6 )
  1770. {
  1771. float flSqrtTrace = FastSqrt( flTrace );
  1772. float s = 0.5f / flSqrtTrace;
  1773. q.x = ( vecUp.y - vecLeft.z ) * s;
  1774. q.y = ( vecForward.z - vecUp.x ) * s;
  1775. q.z = ( vecLeft.x - vecForward.y ) * s;
  1776. q.w = 0.5f * flSqrtTrace;
  1777. }
  1778. else
  1779. {
  1780. if (( vecForward.x > vecLeft.y ) && ( vecForward.x > vecUp.z ) )
  1781. {
  1782. float flSqrtTrace = FastSqrt( 1.0f + vecForward.x - vecLeft.y - vecUp.z );
  1783. float s = 0.5f / flSqrtTrace;
  1784. q.x = 0.5f * flSqrtTrace;
  1785. q.y = ( vecForward.y + vecLeft.x ) * s;
  1786. q.z = ( vecUp.x + vecForward.z ) * s;
  1787. q.w = ( vecUp.y - vecLeft.z ) * s;
  1788. }
  1789. else if ( vecLeft.y > vecUp.z )
  1790. {
  1791. float flSqrtTrace = FastSqrt( 1.0f + vecLeft.y - vecForward.x - vecUp.z );
  1792. float s = 0.5f / flSqrtTrace;
  1793. q.x = ( vecForward.y + vecLeft.x ) * s;
  1794. q.y = 0.5f * flSqrtTrace;
  1795. q.z = ( vecUp.y + vecLeft.z ) * s;
  1796. q.w = ( vecForward.z - vecUp.x ) * s;
  1797. }
  1798. else
  1799. {
  1800. float flSqrtTrace = FastSqrt( 1.0 + vecUp.z - vecForward.x - vecLeft.y );
  1801. float s = 0.5f / flSqrtTrace;
  1802. q.x = ( vecUp.x + vecForward.z ) * s;
  1803. q.y = ( vecUp.y + vecLeft.z ) * s;
  1804. q.z = 0.5f * flSqrtTrace;
  1805. q.w = ( vecLeft.x - vecForward.y ) * s;
  1806. }
  1807. }
  1808. QuaternionNormalize( q );
  1809. */
  1810. // Version 2: Go through angles
  1811. matrix3x4_t mat;
  1812. MatrixSetColumn( vecForward, 0, mat );
  1813. MatrixSetColumn( vecLeft, 1, mat );
  1814. MatrixSetColumn( vecUp, 2, mat );
  1815. QAngle angles;
  1816. MatrixAngles( mat, angles );
  1817. // Quaternion q2;
  1818. AngleQuaternion( angles, q );
  1819. // Assert( fabs(q.x - q2.x) < 1e-3 );
  1820. // Assert( fabs(q.y - q2.y) < 1e-3 );
  1821. // Assert( fabs(q.z - q2.z) < 1e-3 );
  1822. // Assert( fabs(q.w - q2.w) < 1e-3 );
  1823. }
  1824. // FIXME: Optimize!
  1825. void MatrixQuaternion( const matrix3x4_t &mat, Quaternion &q )
  1826. {
  1827. QAngle angles;
  1828. MatrixAngles( mat, angles );
  1829. AngleQuaternion( angles, q );
  1830. }
  1831. //-----------------------------------------------------------------------------
  1832. // Purpose: Converts a quaternion into engine angles
  1833. // Input : *quaternion - q3 + q0.i + q1.j + q2.k
  1834. // *outAngles - PITCH, YAW, ROLL
  1835. //-----------------------------------------------------------------------------
  1836. void QuaternionAngles( const Quaternion &q, RadianEuler &angles )
  1837. {
  1838. Assert( s_bMathlibInitialized );
  1839. Assert( q.IsValid() );
  1840. // FIXME: doing it this way calculates too much data, needs to do an optimized version...
  1841. matrix3x4_t matrix;
  1842. QuaternionMatrix( q, matrix );
  1843. MatrixAngles( matrix, angles );
  1844. Assert( angles.IsValid() );
  1845. }
  1846. //-----------------------------------------------------------------------------
  1847. // Purpose: A helper function to normalize p2.x->p1.x and p3.x->p4.x to
  1848. // be the same length as p2.x->p3.x
  1849. // Input : &p2 -
  1850. // &p4 -
  1851. // p4n -
  1852. //-----------------------------------------------------------------------------
  1853. void Spline_Normalize(
  1854. const Vector &p1,
  1855. const Vector &p2,
  1856. const Vector &p3,
  1857. const Vector &p4,
  1858. Vector& p1n,
  1859. Vector& p4n )
  1860. {
  1861. float dt = p3.x - p2.x;
  1862. p1n = p1;
  1863. p4n = p4;
  1864. if ( dt != 0.0 )
  1865. {
  1866. if (p1.x != p2.x)
  1867. {
  1868. // Equivalent to p1n = p2 - (p2 - p1) * (dt / (p2.x - p1.x));
  1869. VectorLerp( p2, p1, dt / (p2.x - p1.x), p1n );
  1870. }
  1871. if (p4.x != p3.x)
  1872. {
  1873. // Equivalent to p4n = p3 + (p4 - p3) * (dt / (p4.x - p3.x));
  1874. VectorLerp( p3, p4, dt / (p4.x - p3.x), p4n );
  1875. }
  1876. }
  1877. }
  1878. //-----------------------------------------------------------------------------
  1879. // Purpose:
  1880. // Input :
  1881. //-----------------------------------------------------------------------------
  1882. void Catmull_Rom_Spline(
  1883. const Vector &p1,
  1884. const Vector &p2,
  1885. const Vector &p3,
  1886. const Vector &p4,
  1887. float t,
  1888. Vector& output )
  1889. {
  1890. Assert( s_bMathlibInitialized );
  1891. float tSqr = t*t*0.5f;
  1892. float tSqrSqr = t*tSqr;
  1893. t *= 0.5f;
  1894. Assert( &output != &p1 );
  1895. Assert( &output != &p2 );
  1896. Assert( &output != &p3 );
  1897. Assert( &output != &p4 );
  1898. output.Init();
  1899. Vector a, b, c, d;
  1900. // matrix row 1
  1901. VectorScale( p1, -tSqrSqr, a ); // 0.5 t^3 * [ (-1*p1) + ( 3*p2) + (-3*p3) + p4 ]
  1902. VectorScale( p2, tSqrSqr*3, b );
  1903. VectorScale( p3, tSqrSqr*-3, c );
  1904. VectorScale( p4, tSqrSqr, d );
  1905. VectorAdd( a, output, output );
  1906. VectorAdd( b, output, output );
  1907. VectorAdd( c, output, output );
  1908. VectorAdd( d, output, output );
  1909. // matrix row 2
  1910. VectorScale( p1, tSqr*2, a ); // 0.5 t^2 * [ ( 2*p1) + (-5*p2) + ( 4*p3) - p4 ]
  1911. VectorScale( p2, tSqr*-5, b );
  1912. VectorScale( p3, tSqr*4, c );
  1913. VectorScale( p4, -tSqr, d );
  1914. VectorAdd( a, output, output );
  1915. VectorAdd( b, output, output );
  1916. VectorAdd( c, output, output );
  1917. VectorAdd( d, output, output );
  1918. // matrix row 3
  1919. VectorScale( p1, -t, a ); // 0.5 t * [ (-1*p1) + p3 ]
  1920. VectorScale( p3, t, b );
  1921. VectorAdd( a, output, output );
  1922. VectorAdd( b, output, output );
  1923. // matrix row 4
  1924. VectorAdd( p2, output, output ); // p2
  1925. }
  1926. void Catmull_Rom_Spline_Tangent(
  1927. const Vector &p1,
  1928. const Vector &p2,
  1929. const Vector &p3,
  1930. const Vector &p4,
  1931. float t,
  1932. Vector& output )
  1933. {
  1934. Assert( s_bMathlibInitialized );
  1935. float tOne = 3*t*t*0.5f;
  1936. float tTwo = 2*t*0.5f;
  1937. float tThree = 0.5;
  1938. Assert( &output != &p1 );
  1939. Assert( &output != &p2 );
  1940. Assert( &output != &p3 );
  1941. Assert( &output != &p4 );
  1942. output.Init();
  1943. Vector a, b, c, d;
  1944. // matrix row 1
  1945. VectorScale( p1, -tOne, a ); // 0.5 t^3 * [ (-1*p1) + ( 3*p2) + (-3*p3) + p4 ]
  1946. VectorScale( p2, tOne*3, b );
  1947. VectorScale( p3, tOne*-3, c );
  1948. VectorScale( p4, tOne, d );
  1949. VectorAdd( a, output, output );
  1950. VectorAdd( b, output, output );
  1951. VectorAdd( c, output, output );
  1952. VectorAdd( d, output, output );
  1953. // matrix row 2
  1954. VectorScale( p1, tTwo*2, a ); // 0.5 t^2 * [ ( 2*p1) + (-5*p2) + ( 4*p3) - p4 ]
  1955. VectorScale( p2, tTwo*-5, b );
  1956. VectorScale( p3, tTwo*4, c );
  1957. VectorScale( p4, -tTwo, d );
  1958. VectorAdd( a, output, output );
  1959. VectorAdd( b, output, output );
  1960. VectorAdd( c, output, output );
  1961. VectorAdd( d, output, output );
  1962. // matrix row 3
  1963. VectorScale( p1, -tThree, a ); // 0.5 t * [ (-1*p1) + p3 ]
  1964. VectorScale( p3, tThree, b );
  1965. VectorAdd( a, output, output );
  1966. VectorAdd( b, output, output );
  1967. }
  1968. // area under the curve [0..t]
  1969. void Catmull_Rom_Spline_Integral(
  1970. const Vector &p1,
  1971. const Vector &p2,
  1972. const Vector &p3,
  1973. const Vector &p4,
  1974. float t,
  1975. Vector& output )
  1976. {
  1977. output = p2*t
  1978. -0.25f*(p1 - p3)*t*t
  1979. + (1.0f/6.0f)*(2.0f*p1 - 5.0f*p2 + 4.0f*p3 - p4)*t*t*t
  1980. - 0.125f*(p1 - 3.0f*p2 + 3.0f*p3 - p4)*t*t*t*t;
  1981. }
  1982. // area under the curve [0..1]
  1983. void Catmull_Rom_Spline_Integral(
  1984. const Vector &p1,
  1985. const Vector &p2,
  1986. const Vector &p3,
  1987. const Vector &p4,
  1988. Vector& output )
  1989. {
  1990. output = (-0.25f * p1 + 3.25f * p2 + 3.25f * p3 - 0.25f * p4) * (1.0f / 6.0f);
  1991. }
  1992. void Catmull_Rom_Spline_Normalize(
  1993. const Vector &p1,
  1994. const Vector &p2,
  1995. const Vector &p3,
  1996. const Vector &p4,
  1997. float t,
  1998. Vector& output )
  1999. {
  2000. // Normalize p2->p1 and p3->p4 to be the same length as p2->p3
  2001. float dt = p3.DistTo(p2);
  2002. Vector p1n, p4n;
  2003. VectorSubtract( p1, p2, p1n );
  2004. VectorSubtract( p4, p3, p4n );
  2005. VectorNormalize( p1n );
  2006. VectorNormalize( p4n );
  2007. VectorMA( p2, dt, p1n, p1n );
  2008. VectorMA( p3, dt, p4n, p4n );
  2009. Catmull_Rom_Spline( p1n, p2, p3, p4n, t, output );
  2010. }
  2011. void Catmull_Rom_Spline_Integral_Normalize(
  2012. const Vector &p1,
  2013. const Vector &p2,
  2014. const Vector &p3,
  2015. const Vector &p4,
  2016. float t,
  2017. Vector& output )
  2018. {
  2019. // Normalize p2->p1 and p3->p4 to be the same length as p2->p3
  2020. float dt = p3.DistTo(p2);
  2021. Vector p1n, p4n;
  2022. VectorSubtract( p1, p2, p1n );
  2023. VectorSubtract( p4, p3, p4n );
  2024. VectorNormalize( p1n );
  2025. VectorNormalize( p4n );
  2026. VectorMA( p2, dt, p1n, p1n );
  2027. VectorMA( p3, dt, p4n, p4n );
  2028. Catmull_Rom_Spline_Integral( p1n, p2, p3, p4n, t, output );
  2029. }
  2030. void Catmull_Rom_Spline_NormalizeX(
  2031. const Vector &p1,
  2032. const Vector &p2,
  2033. const Vector &p3,
  2034. const Vector &p4,
  2035. float t,
  2036. Vector& output )
  2037. {
  2038. Vector p1n, p4n;
  2039. Spline_Normalize( p1, p2, p3, p4, p1n, p4n );
  2040. Catmull_Rom_Spline( p1n, p2, p3, p4n, t, output );
  2041. }
  2042. //-----------------------------------------------------------------------------
  2043. // Purpose: basic hermite spline. t = 0 returns p1, t = 1 returns p2,
  2044. // d1 and d2 are used to entry and exit slope of curve
  2045. // Input :
  2046. //-----------------------------------------------------------------------------
  2047. void Hermite_Spline(
  2048. const Vector &p1,
  2049. const Vector &p2,
  2050. const Vector &d1,
  2051. const Vector &d2,
  2052. float t,
  2053. Vector& output )
  2054. {
  2055. Assert( s_bMathlibInitialized );
  2056. float tSqr = t*t;
  2057. float tCube = t*tSqr;
  2058. Assert( &output != &p1 );
  2059. Assert( &output != &p2 );
  2060. Assert( &output != &d1 );
  2061. Assert( &output != &d2 );
  2062. float b1 = 2.0f*tCube-3.0f*tSqr+1.0f;
  2063. float b2 = 1.0f - b1; // -2*tCube+3*tSqr;
  2064. float b3 = tCube-2*tSqr+t;
  2065. float b4 = tCube-tSqr;
  2066. VectorScale( p1, b1, output );
  2067. VectorMA( output, b2, p2, output );
  2068. VectorMA( output, b3, d1, output );
  2069. VectorMA( output, b4, d2, output );
  2070. }
  2071. float Hermite_Spline(
  2072. float p1,
  2073. float p2,
  2074. float d1,
  2075. float d2,
  2076. float t )
  2077. {
  2078. Assert( s_bMathlibInitialized );
  2079. float output;
  2080. float tSqr = t*t;
  2081. float tCube = t*tSqr;
  2082. float b1 = 2.0f*tCube-3.0f*tSqr+1.0f;
  2083. float b2 = 1.0f - b1; // -2*tCube+3*tSqr;
  2084. float b3 = tCube-2*tSqr+t;
  2085. float b4 = tCube-tSqr;
  2086. output = p1 * b1;
  2087. output += p2 * b2;
  2088. output += d1 * b3;
  2089. output += d2 * b4;
  2090. return output;
  2091. }
  2092. void Hermite_SplineBasis( float t, float basis[4] )
  2093. {
  2094. float tSqr = t*t;
  2095. float tCube = t*tSqr;
  2096. basis[0] = 2.0f*tCube-3.0f*tSqr+1.0f;
  2097. basis[1] = 1.0f - basis[0]; // -2*tCube+3*tSqr;
  2098. basis[2] = tCube-2*tSqr+t;
  2099. basis[3] = tCube-tSqr;
  2100. }
  2101. //-----------------------------------------------------------------------------
  2102. // Purpose: simple three data point hermite spline.
  2103. // t = 0 returns p1, t = 1 returns p2,
  2104. // slopes are generated from the p0->p1 and p1->p2 segments
  2105. // this is reasonable C1 method when there's no "p3" data yet.
  2106. // Input :
  2107. //-----------------------------------------------------------------------------
  2108. // BUG: the VectorSubtract()'s calls go away if the global optimizer is enabled
  2109. #pragma optimize( "g", off )
  2110. void Hermite_Spline( const Vector &p0, const Vector &p1, const Vector &p2, float t, Vector& output )
  2111. {
  2112. Vector e10, e21;
  2113. VectorSubtract( p1, p0, e10 );
  2114. VectorSubtract( p2, p1, e21 );
  2115. Hermite_Spline( p1, p2, e10, e21, t, output );
  2116. }
  2117. #pragma optimize( "", on )
  2118. float Hermite_Spline( float p0, float p1, float p2, float t )
  2119. {
  2120. return Hermite_Spline( p1, p2, p1 - p0, p2 - p1, t );
  2121. }
  2122. void Hermite_Spline( const Quaternion &q0, const Quaternion &q1, const Quaternion &q2, float t, Quaternion &output )
  2123. {
  2124. // cheap, hacked version of quaternions
  2125. Quaternion q0a;
  2126. Quaternion q1a;
  2127. QuaternionAlign( q2, q0, q0a );
  2128. QuaternionAlign( q2, q1, q1a );
  2129. output.x = Hermite_Spline( q0a.x, q1a.x, q2.x, t );
  2130. output.y = Hermite_Spline( q0a.y, q1a.y, q2.y, t );
  2131. output.z = Hermite_Spline( q0a.z, q1a.z, q2.z, t );
  2132. output.w = Hermite_Spline( q0a.w, q1a.w, q2.w, t );
  2133. QuaternionNormalize( output );
  2134. }
  2135. // See http://en.wikipedia.org/wiki/Kochanek-Bartels_curves
  2136. //
  2137. // Tension: -1 = Round -> 1 = Tight
  2138. // Bias: -1 = Pre-shoot (bias left) -> 1 = Post-shoot (bias right)
  2139. // Continuity: -1 = Box corners -> 1 = Inverted corners
  2140. //
  2141. // If T=B=C=0 it's the same matrix as Catmull-Rom.
  2142. // If T=1 & B=C=0 it's the same as Cubic.
  2143. // If T=B=0 & C=-1 it's just linear interpolation
  2144. //
  2145. // See http://news.povray.org/povray.binaries.tutorials/attachment/%[email protected]%3E/Splines.bas.txt
  2146. // for example code and descriptions of various spline types...
  2147. //
  2148. void Kochanek_Bartels_Spline(
  2149. float tension,
  2150. float bias,
  2151. float continuity,
  2152. const Vector &p1,
  2153. const Vector &p2,
  2154. const Vector &p3,
  2155. const Vector &p4,
  2156. float t,
  2157. Vector& output )
  2158. {
  2159. Assert( s_bMathlibInitialized );
  2160. float ffa, ffb, ffc, ffd;
  2161. ffa = ( 1.0f - tension ) * ( 1.0f + continuity ) * ( 1.0f + bias );
  2162. ffb = ( 1.0f - tension ) * ( 1.0f - continuity ) * ( 1.0f - bias );
  2163. ffc = ( 1.0f - tension ) * ( 1.0f - continuity ) * ( 1.0f + bias );
  2164. ffd = ( 1.0f - tension ) * ( 1.0f + continuity ) * ( 1.0f - bias );
  2165. float tSqr = t*t*0.5f;
  2166. float tSqrSqr = t*tSqr;
  2167. t *= 0.5f;
  2168. Assert( &output != &p1 );
  2169. Assert( &output != &p2 );
  2170. Assert( &output != &p3 );
  2171. Assert( &output != &p4 );
  2172. output.Init();
  2173. Vector a, b, c, d;
  2174. // matrix row 1
  2175. VectorScale( p1, tSqrSqr * -ffa, a );
  2176. VectorScale( p2, tSqrSqr * ( 4.0f + ffa - ffb - ffc ), b );
  2177. VectorScale( p3, tSqrSqr * ( -4.0f + ffb + ffc - ffd ), c );
  2178. VectorScale( p4, tSqrSqr * ffd, d );
  2179. VectorAdd( a, output, output );
  2180. VectorAdd( b, output, output );
  2181. VectorAdd( c, output, output );
  2182. VectorAdd( d, output, output );
  2183. // matrix row 2
  2184. VectorScale( p1, tSqr* 2 * ffa, a );
  2185. VectorScale( p2, tSqr * ( -6 - 2 * ffa + 2 * ffb + ffc ), b );
  2186. VectorScale( p3, tSqr * ( 6 - 2 * ffb - ffc + ffd ), c );
  2187. VectorScale( p4, tSqr * -ffd, d );
  2188. VectorAdd( a, output, output );
  2189. VectorAdd( b, output, output );
  2190. VectorAdd( c, output, output );
  2191. VectorAdd( d, output, output );
  2192. // matrix row 3
  2193. VectorScale( p1, t * -ffa, a );
  2194. VectorScale( p2, t * ( ffa - ffb ), b );
  2195. VectorScale( p3, t * ffb, c );
  2196. // p4 unchanged
  2197. VectorAdd( a, output, output );
  2198. VectorAdd( b, output, output );
  2199. VectorAdd( c, output, output );
  2200. // matrix row 4
  2201. // p1, p3, p4 unchanged
  2202. // p2 is multiplied by 1 and added, so just added it directly
  2203. VectorAdd( p2, output, output );
  2204. }
  2205. void Kochanek_Bartels_Spline_NormalizeX(
  2206. float tension,
  2207. float bias,
  2208. float continuity,
  2209. const Vector &p1,
  2210. const Vector &p2,
  2211. const Vector &p3,
  2212. const Vector &p4,
  2213. float t,
  2214. Vector& output )
  2215. {
  2216. Vector p1n, p4n;
  2217. Spline_Normalize( p1, p2, p3, p4, p1n, p4n );
  2218. Kochanek_Bartels_Spline( tension, bias, continuity, p1n, p2, p3, p4n, t, output );
  2219. }
  2220. void Cubic_Spline(
  2221. const Vector &p1,
  2222. const Vector &p2,
  2223. const Vector &p3,
  2224. const Vector &p4,
  2225. float t,
  2226. Vector& output )
  2227. {
  2228. Assert( s_bMathlibInitialized );
  2229. float tSqr = t*t;
  2230. float tSqrSqr = t*tSqr;
  2231. Assert( &output != &p1 );
  2232. Assert( &output != &p2 );
  2233. Assert( &output != &p3 );
  2234. Assert( &output != &p4 );
  2235. output.Init();
  2236. Vector a, b, c, d;
  2237. // matrix row 1
  2238. VectorScale( p2, tSqrSqr * 2, b );
  2239. VectorScale( p3, tSqrSqr * -2, c );
  2240. VectorAdd( b, output, output );
  2241. VectorAdd( c, output, output );
  2242. // matrix row 2
  2243. VectorScale( p2, tSqr * -3, b );
  2244. VectorScale( p3, tSqr * 3, c );
  2245. VectorAdd( b, output, output );
  2246. VectorAdd( c, output, output );
  2247. // matrix row 3
  2248. // no influence
  2249. // p4 unchanged
  2250. // matrix row 4
  2251. // p1, p3, p4 unchanged
  2252. VectorAdd( p2, output, output );
  2253. }
  2254. void Cubic_Spline_NormalizeX(
  2255. const Vector &p1,
  2256. const Vector &p2,
  2257. const Vector &p3,
  2258. const Vector &p4,
  2259. float t,
  2260. Vector& output )
  2261. {
  2262. Vector p1n, p4n;
  2263. Spline_Normalize( p1, p2, p3, p4, p1n, p4n );
  2264. Cubic_Spline( p1n, p2, p3, p4n, t, output );
  2265. }
  2266. void BSpline(
  2267. const Vector &p1,
  2268. const Vector &p2,
  2269. const Vector &p3,
  2270. const Vector &p4,
  2271. float t,
  2272. Vector& output )
  2273. {
  2274. Assert( s_bMathlibInitialized );
  2275. float oneOver6 = 1.0f / 6.0f;
  2276. float tSqr = t * t * oneOver6;
  2277. float tSqrSqr = t*tSqr;
  2278. t *= oneOver6;
  2279. Assert( &output != &p1 );
  2280. Assert( &output != &p2 );
  2281. Assert( &output != &p3 );
  2282. Assert( &output != &p4 );
  2283. output.Init();
  2284. Vector a, b, c, d;
  2285. // matrix row 1
  2286. VectorScale( p1, -tSqrSqr, a );
  2287. VectorScale( p2, tSqrSqr * 3.0f, b );
  2288. VectorScale( p3, tSqrSqr * -3.0f, c );
  2289. VectorScale( p4, tSqrSqr, d );
  2290. VectorAdd( a, output, output );
  2291. VectorAdd( b, output, output );
  2292. VectorAdd( c, output, output );
  2293. VectorAdd( d, output, output );
  2294. // matrix row 2
  2295. VectorScale( p1, tSqr * 3.0f, a );
  2296. VectorScale( p2, tSqr * -6.0f, b );
  2297. VectorScale( p3, tSqr * 3.0f, c );
  2298. VectorAdd( a, output, output );
  2299. VectorAdd( b, output, output );
  2300. VectorAdd( c, output, output );
  2301. // matrix row 3
  2302. VectorScale( p1, t * -3.0f, a );
  2303. VectorScale( p3, t * 3.0f, c );
  2304. // p4 unchanged
  2305. VectorAdd( a, output, output );
  2306. VectorAdd( c, output, output );
  2307. // matrix row 4
  2308. // p1 and p3 scaled by 1.0f, so done below
  2309. VectorScale( p1, oneOver6, a );
  2310. VectorScale( p2, 4.0f * oneOver6, b );
  2311. VectorScale( p3, oneOver6, c );
  2312. VectorAdd( a, output, output );
  2313. VectorAdd( b, output, output );
  2314. VectorAdd( c, output, output );
  2315. }
  2316. void BSpline_NormalizeX(
  2317. const Vector &p1,
  2318. const Vector &p2,
  2319. const Vector &p3,
  2320. const Vector &p4,
  2321. float t,
  2322. Vector& output )
  2323. {
  2324. Vector p1n, p4n;
  2325. Spline_Normalize( p1, p2, p3, p4, p1n, p4n );
  2326. BSpline( p1n, p2, p3, p4n, t, output );
  2327. }
  2328. void Parabolic_Spline(
  2329. const Vector &p1,
  2330. const Vector &p2,
  2331. const Vector &p3,
  2332. const Vector &p4,
  2333. float t,
  2334. Vector& output )
  2335. {
  2336. Assert( s_bMathlibInitialized );
  2337. float tSqr = t*t*0.5f;
  2338. t *= 0.5f;
  2339. Assert( &output != &p1 );
  2340. Assert( &output != &p2 );
  2341. Assert( &output != &p3 );
  2342. Assert( &output != &p4 );
  2343. output.Init();
  2344. Vector a, b, c, d;
  2345. // matrix row 1
  2346. // no influence from t cubed
  2347. // matrix row 2
  2348. VectorScale( p1, tSqr, a );
  2349. VectorScale( p2, tSqr * -2.0f, b );
  2350. VectorScale( p3, tSqr, c );
  2351. VectorAdd( a, output, output );
  2352. VectorAdd( b, output, output );
  2353. VectorAdd( c, output, output );
  2354. // matrix row 3
  2355. VectorScale( p1, t * -2.0f, a );
  2356. VectorScale( p2, t * 2.0f, b );
  2357. // p4 unchanged
  2358. VectorAdd( a, output, output );
  2359. VectorAdd( b, output, output );
  2360. // matrix row 4
  2361. VectorScale( p1, 0.5f, a );
  2362. VectorScale( p2, 0.5f, b );
  2363. VectorAdd( a, output, output );
  2364. VectorAdd( b, output, output );
  2365. }
  2366. void Parabolic_Spline_NormalizeX(
  2367. const Vector &p1,
  2368. const Vector &p2,
  2369. const Vector &p3,
  2370. const Vector &p4,
  2371. float t,
  2372. Vector& output )
  2373. {
  2374. Vector p1n, p4n;
  2375. Spline_Normalize( p1, p2, p3, p4, p1n, p4n );
  2376. Parabolic_Spline( p1n, p2, p3, p4n, t, output );
  2377. }
  2378. //-----------------------------------------------------------------------------
  2379. // Purpose: Compress the input values for a ranged result such that from 75% to 200% smoothly of the range maps
  2380. //-----------------------------------------------------------------------------
  2381. float RangeCompressor( float flValue, float flMin, float flMax, float flBase )
  2382. {
  2383. // clamp base
  2384. if (flBase < flMin)
  2385. flBase = flMin;
  2386. if (flBase > flMax)
  2387. flBase = flMax;
  2388. flValue += flBase;
  2389. // convert to 0 to 1 value
  2390. float flMid = (flValue - flMin) / (flMax - flMin);
  2391. // convert to -1 to 1 value
  2392. float flTarget = flMid * 2 - 1;
  2393. if (fabs(flTarget) > 0.75)
  2394. {
  2395. float t = (fabs(flTarget) - 0.75) / (1.25);
  2396. if (t < 1.0)
  2397. {
  2398. if (flTarget > 0)
  2399. {
  2400. flTarget = Hermite_Spline( 0.75, 1, 0.75, 0, t );
  2401. }
  2402. else
  2403. {
  2404. flTarget = -Hermite_Spline( 0.75, 1, 0.75, 0, t );
  2405. }
  2406. }
  2407. else
  2408. {
  2409. flTarget = (flTarget > 0) ? 1.0f : -1.0f;
  2410. }
  2411. }
  2412. flMid = (flTarget + 1 ) / 2.0;
  2413. flValue = flMin * (1 - flMid) + flMax * flMid;
  2414. flValue -= flBase;
  2415. return flValue;
  2416. }
  2417. //#pragma optimize( "", on )
  2418. //-----------------------------------------------------------------------------
  2419. // Transforms a AABB into another space; which will inherently grow the box.
  2420. //-----------------------------------------------------------------------------
  2421. void TransformAABB( const matrix3x4_t& transform, const Vector &vecMinsIn, const Vector &vecMaxsIn, Vector &vecMinsOut, Vector &vecMaxsOut )
  2422. {
  2423. Vector localCenter;
  2424. VectorAdd( vecMinsIn, vecMaxsIn, localCenter );
  2425. localCenter *= 0.5f;
  2426. Vector localExtents;
  2427. VectorSubtract( vecMaxsIn, localCenter, localExtents );
  2428. Vector worldCenter;
  2429. VectorTransform( localCenter, transform, worldCenter );
  2430. Vector worldExtents;
  2431. worldExtents.x = DotProductAbs( localExtents, transform[0] );
  2432. worldExtents.y = DotProductAbs( localExtents, transform[1] );
  2433. worldExtents.z = DotProductAbs( localExtents, transform[2] );
  2434. VectorSubtract( worldCenter, worldExtents, vecMinsOut );
  2435. VectorAdd( worldCenter, worldExtents, vecMaxsOut );
  2436. }
  2437. //-----------------------------------------------------------------------------
  2438. // Uses the inverse transform of in1
  2439. //-----------------------------------------------------------------------------
  2440. void ITransformAABB( const matrix3x4_t& transform, const Vector &vecMinsIn, const Vector &vecMaxsIn, Vector &vecMinsOut, Vector &vecMaxsOut )
  2441. {
  2442. Vector worldCenter;
  2443. VectorAdd( vecMinsIn, vecMaxsIn, worldCenter );
  2444. worldCenter *= 0.5f;
  2445. Vector worldExtents;
  2446. VectorSubtract( vecMaxsIn, worldCenter, worldExtents );
  2447. Vector localCenter;
  2448. VectorITransform( worldCenter, transform, localCenter );
  2449. Vector localExtents;
  2450. localExtents.x = FloatMakePositive( worldExtents.x * transform[0][0] ) +
  2451. FloatMakePositive( worldExtents.y * transform[1][0] ) +
  2452. FloatMakePositive( worldExtents.z * transform[2][0] );
  2453. localExtents.y = FloatMakePositive( worldExtents.x * transform[0][1] ) +
  2454. FloatMakePositive( worldExtents.y * transform[1][1] ) +
  2455. FloatMakePositive( worldExtents.z * transform[2][1] );
  2456. localExtents.z = FloatMakePositive( worldExtents.x * transform[0][2] ) +
  2457. FloatMakePositive( worldExtents.y * transform[1][2] ) +
  2458. FloatMakePositive( worldExtents.z * transform[2][2] );
  2459. VectorSubtract( localCenter, localExtents, vecMinsOut );
  2460. VectorAdd( localCenter, localExtents, vecMaxsOut );
  2461. }
  2462. //-----------------------------------------------------------------------------
  2463. // Rotates a AABB into another space; which will inherently grow the box.
  2464. // (same as TransformAABB, but doesn't take the translation into account)
  2465. //-----------------------------------------------------------------------------
  2466. void RotateAABB( const matrix3x4_t &transform, const Vector &vecMinsIn, const Vector &vecMaxsIn, Vector &vecMinsOut, Vector &vecMaxsOut )
  2467. {
  2468. Vector localCenter;
  2469. VectorAdd( vecMinsIn, vecMaxsIn, localCenter );
  2470. localCenter *= 0.5f;
  2471. Vector localExtents;
  2472. VectorSubtract( vecMaxsIn, localCenter, localExtents );
  2473. Vector newCenter;
  2474. VectorRotate( localCenter, transform, newCenter );
  2475. Vector newExtents;
  2476. newExtents.x = DotProductAbs( localExtents, transform[0] );
  2477. newExtents.y = DotProductAbs( localExtents, transform[1] );
  2478. newExtents.z = DotProductAbs( localExtents, transform[2] );
  2479. VectorSubtract( newCenter, newExtents, vecMinsOut );
  2480. VectorAdd( newCenter, newExtents, vecMaxsOut );
  2481. }
  2482. //-----------------------------------------------------------------------------
  2483. // Uses the inverse transform of in1
  2484. //-----------------------------------------------------------------------------
  2485. void IRotateAABB( const matrix3x4_t &transform, const Vector &vecMinsIn, const Vector &vecMaxsIn, Vector &vecMinsOut, Vector &vecMaxsOut )
  2486. {
  2487. Vector oldCenter;
  2488. VectorAdd( vecMinsIn, vecMaxsIn, oldCenter );
  2489. oldCenter *= 0.5f;
  2490. Vector oldExtents;
  2491. VectorSubtract( vecMaxsIn, oldCenter, oldExtents );
  2492. Vector newCenter;
  2493. VectorIRotate( oldCenter, transform, newCenter );
  2494. Vector newExtents;
  2495. newExtents.x = FloatMakePositive( oldExtents.x * transform[0][0] ) +
  2496. FloatMakePositive( oldExtents.y * transform[1][0] ) +
  2497. FloatMakePositive( oldExtents.z * transform[2][0] );
  2498. newExtents.y = FloatMakePositive( oldExtents.x * transform[0][1] ) +
  2499. FloatMakePositive( oldExtents.y * transform[1][1] ) +
  2500. FloatMakePositive( oldExtents.z * transform[2][1] );
  2501. newExtents.z = FloatMakePositive( oldExtents.x * transform[0][2] ) +
  2502. FloatMakePositive( oldExtents.y * transform[1][2] ) +
  2503. FloatMakePositive( oldExtents.z * transform[2][2] );
  2504. VectorSubtract( newCenter, newExtents, vecMinsOut );
  2505. VectorAdd( newCenter, newExtents, vecMaxsOut );
  2506. }
  2507. float CalcSqrDistanceToAABB( const Vector &mins, const Vector &maxs, const Vector &point )
  2508. {
  2509. float flDelta;
  2510. float flDistSqr = 0.0f;
  2511. if ( point.x < mins.x )
  2512. {
  2513. flDelta = (mins.x - point.x);
  2514. flDistSqr += flDelta * flDelta;
  2515. }
  2516. else if ( point.x > maxs.x )
  2517. {
  2518. flDelta = (point.x - maxs.x);
  2519. flDistSqr += flDelta * flDelta;
  2520. }
  2521. if ( point.y < mins.y )
  2522. {
  2523. flDelta = (mins.y - point.y);
  2524. flDistSqr += flDelta * flDelta;
  2525. }
  2526. else if ( point.y > maxs.y )
  2527. {
  2528. flDelta = (point.y - maxs.y);
  2529. flDistSqr += flDelta * flDelta;
  2530. }
  2531. if ( point.z < mins.z )
  2532. {
  2533. flDelta = (mins.z - point.z);
  2534. flDistSqr += flDelta * flDelta;
  2535. }
  2536. else if ( point.z > maxs.z )
  2537. {
  2538. flDelta = (point.z - maxs.z);
  2539. flDistSqr += flDelta * flDelta;
  2540. }
  2541. return flDistSqr;
  2542. }
  2543. void CalcClosestPointOnAABB( const Vector &mins, const Vector &maxs, const Vector &point, Vector &closestOut )
  2544. {
  2545. closestOut.x = clamp( point.x, mins.x, maxs.x );
  2546. closestOut.y = clamp( point.y, mins.y, maxs.y );
  2547. closestOut.z = clamp( point.z, mins.z, maxs.z );
  2548. }
  2549. void CalcSqrDistAndClosestPointOnAABB( const Vector &mins, const Vector &maxs, const Vector &point, Vector &closestOut, float &distSqrOut )
  2550. {
  2551. distSqrOut = 0.0f;
  2552. for ( int i = 0; i < 3; i++ )
  2553. {
  2554. if ( point[i] < mins[i] )
  2555. {
  2556. closestOut[i] = mins[i];
  2557. float flDelta = closestOut[i] - mins[i];
  2558. distSqrOut += flDelta * flDelta;
  2559. }
  2560. else if ( point[i] > maxs[i] )
  2561. {
  2562. closestOut[i] = maxs[i];
  2563. float flDelta = closestOut[i] - maxs[i];
  2564. distSqrOut += flDelta * flDelta;
  2565. }
  2566. else
  2567. {
  2568. closestOut[i] = point[i];
  2569. }
  2570. }
  2571. }
  2572. float CalcClosestPointToLineT( const Vector &P, const Vector &vLineA, const Vector &vLineB, Vector &vDir )
  2573. {
  2574. Assert( s_bMathlibInitialized );
  2575. VectorSubtract( vLineB, vLineA, vDir );
  2576. // D dot [P - (A + D*t)] = 0
  2577. // t = ( DP - DA) / DD
  2578. float div = vDir.Dot( vDir );
  2579. if( div < 0.00001f )
  2580. {
  2581. return 0;
  2582. }
  2583. else
  2584. {
  2585. return (vDir.Dot( P ) - vDir.Dot( vLineA )) / div;
  2586. }
  2587. }
  2588. void CalcClosestPointOnLine( const Vector &P, const Vector &vLineA, const Vector &vLineB, Vector &vClosest, float *outT )
  2589. {
  2590. Assert( s_bMathlibInitialized );
  2591. Vector vDir;
  2592. float t = CalcClosestPointToLineT( P, vLineA, vLineB, vDir );
  2593. if ( outT ) *outT = t;
  2594. vClosest.MulAdd( vLineA, vDir, t );
  2595. }
  2596. float CalcDistanceToLine( const Vector &P, const Vector &vLineA, const Vector &vLineB, float *outT )
  2597. {
  2598. Assert( s_bMathlibInitialized );
  2599. Vector vClosest;
  2600. CalcClosestPointOnLine( P, vLineA, vLineB, vClosest, outT );
  2601. return P.DistTo(vClosest);
  2602. }
  2603. float CalcDistanceSqrToLine( const Vector &P, const Vector &vLineA, const Vector &vLineB, float *outT )
  2604. {
  2605. Assert( s_bMathlibInitialized );
  2606. Vector vClosest;
  2607. CalcClosestPointOnLine( P, vLineA, vLineB, vClosest, outT );
  2608. return P.DistToSqr(vClosest);
  2609. }
  2610. void CalcClosestPointOnLineSegment( const Vector &P, const Vector &vLineA, const Vector &vLineB, Vector &vClosest, float *outT )
  2611. {
  2612. Vector vDir;
  2613. float t = CalcClosestPointToLineT( P, vLineA, vLineB, vDir );
  2614. t = clamp( t, 0.f, 1.f );
  2615. if ( outT )
  2616. {
  2617. *outT = t;
  2618. }
  2619. vClosest.MulAdd( vLineA, vDir, t );
  2620. }
  2621. float CalcDistanceToLineSegment( const Vector &P, const Vector &vLineA, const Vector &vLineB, float *outT )
  2622. {
  2623. Assert( s_bMathlibInitialized );
  2624. Vector vClosest;
  2625. CalcClosestPointOnLineSegment( P, vLineA, vLineB, vClosest, outT );
  2626. return P.DistTo( vClosest );
  2627. }
  2628. float CalcDistanceSqrToLineSegment( const Vector &P, const Vector &vLineA, const Vector &vLineB, float *outT )
  2629. {
  2630. Assert( s_bMathlibInitialized );
  2631. Vector vClosest;
  2632. CalcClosestPointOnLineSegment( P, vLineA, vLineB, vClosest, outT );
  2633. return P.DistToSqr(vClosest);
  2634. }
  2635. float CalcClosestPointToLineT2D( const Vector2D &P, const Vector2D &vLineA, const Vector2D &vLineB, Vector2D &vDir )
  2636. {
  2637. Assert( s_bMathlibInitialized );
  2638. Vector2DSubtract( vLineB, vLineA, vDir );
  2639. // D dot [P - (A + D*t)] = 0
  2640. // t = (DP - DA) / DD
  2641. float div = vDir.Dot( vDir );
  2642. if( div < 0.00001f )
  2643. {
  2644. return 0;
  2645. }
  2646. else
  2647. {
  2648. return (vDir.Dot( P ) - vDir.Dot( vLineA )) / div;
  2649. }
  2650. }
  2651. void CalcClosestPointOnLine2D( const Vector2D &P, const Vector2D &vLineA, const Vector2D &vLineB, Vector2D &vClosest, float *outT )
  2652. {
  2653. Assert( s_bMathlibInitialized );
  2654. Vector2D vDir;
  2655. float t = CalcClosestPointToLineT2D( P, vLineA, vLineB, vDir );
  2656. if ( outT ) *outT = t;
  2657. vClosest.MulAdd( vLineA, vDir, t );
  2658. }
  2659. float CalcDistanceToLine2D( const Vector2D &P, const Vector2D &vLineA, const Vector2D &vLineB, float *outT )
  2660. {
  2661. Assert( s_bMathlibInitialized );
  2662. Vector2D vClosest;
  2663. CalcClosestPointOnLine2D( P, vLineA, vLineB, vClosest, outT );
  2664. return P.DistTo( vClosest );
  2665. }
  2666. float CalcDistanceSqrToLine2D( const Vector2D &P, const Vector2D &vLineA, const Vector2D &vLineB, float *outT )
  2667. {
  2668. Assert( s_bMathlibInitialized );
  2669. Vector2D vClosest;
  2670. CalcClosestPointOnLine2D( P, vLineA, vLineB, vClosest, outT );
  2671. return P.DistToSqr(vClosest);
  2672. }
  2673. void CalcClosestPointOnLineSegment2D( const Vector2D &P, const Vector2D &vLineA, const Vector2D &vLineB, Vector2D &vClosest, float *outT )
  2674. {
  2675. Vector2D vDir;
  2676. float t = CalcClosestPointToLineT2D( P, vLineA, vLineB, vDir );
  2677. t = clamp( t, 0.f, 1.f );
  2678. if ( outT )
  2679. {
  2680. *outT = t;
  2681. }
  2682. vClosest.MulAdd( vLineA, vDir, t );
  2683. }
  2684. float CalcDistanceToLineSegment2D( const Vector2D &P, const Vector2D &vLineA, const Vector2D &vLineB, float *outT )
  2685. {
  2686. Assert( s_bMathlibInitialized );
  2687. Vector2D vClosest;
  2688. CalcClosestPointOnLineSegment2D( P, vLineA, vLineB, vClosest, outT );
  2689. return P.DistTo( vClosest );
  2690. }
  2691. float CalcDistanceSqrToLineSegment2D( const Vector2D &P, const Vector2D &vLineA, const Vector2D &vLineB, float *outT )
  2692. {
  2693. Assert( s_bMathlibInitialized );
  2694. Vector2D vClosest;
  2695. CalcClosestPointOnLineSegment2D( P, vLineA, vLineB, vClosest, outT );
  2696. return P.DistToSqr( vClosest );
  2697. }
  2698. // Do we have another epsilon we could use
  2699. #define LINE_EPS ( 0.000001f )
  2700. //-----------------------------------------------------------------------------
  2701. // Purpose: Given lines p1->p2 and p3->p4, computes a line segment (pa->pb) and returns the parameters 0->1 multipliers
  2702. // along each segment for the returned points
  2703. // Input : p1 -
  2704. // p2 -
  2705. // p3 -
  2706. // p4 -
  2707. // *s1 -
  2708. // *s2 -
  2709. // Output : Returns true on success, false on failure.
  2710. //-----------------------------------------------------------------------------
  2711. bool CalcLineToLineIntersectionSegment(
  2712. const Vector& p1,const Vector& p2,const Vector& p3,const Vector& p4,Vector *s1,Vector *s2,
  2713. float *t1, float *t2)
  2714. {
  2715. Vector p13,p43,p21;
  2716. float d1343,d4321,d1321,d4343,d2121;
  2717. float numer,denom;
  2718. p13.x = p1.x - p3.x;
  2719. p13.y = p1.y - p3.y;
  2720. p13.z = p1.z - p3.z;
  2721. p43.x = p4.x - p3.x;
  2722. p43.y = p4.y - p3.y;
  2723. p43.z = p4.z - p3.z;
  2724. if (fabs(p43.x) < LINE_EPS && fabs(p43.y) < LINE_EPS && fabs(p43.z) < LINE_EPS)
  2725. return false;
  2726. p21.x = p2.x - p1.x;
  2727. p21.y = p2.y - p1.y;
  2728. p21.z = p2.z - p1.z;
  2729. if (fabs(p21.x) < LINE_EPS && fabs(p21.y) < LINE_EPS && fabs(p21.z) < LINE_EPS)
  2730. return false;
  2731. d1343 = p13.x * p43.x + p13.y * p43.y + p13.z * p43.z;
  2732. d4321 = p43.x * p21.x + p43.y * p21.y + p43.z * p21.z;
  2733. d1321 = p13.x * p21.x + p13.y * p21.y + p13.z * p21.z;
  2734. d4343 = p43.x * p43.x + p43.y * p43.y + p43.z * p43.z;
  2735. d2121 = p21.x * p21.x + p21.y * p21.y + p21.z * p21.z;
  2736. denom = d2121 * d4343 - d4321 * d4321;
  2737. if (fabs(denom) < LINE_EPS)
  2738. return false;
  2739. numer = d1343 * d4321 - d1321 * d4343;
  2740. *t1 = numer / denom;
  2741. *t2 = (d1343 + d4321 * (*t1)) / d4343;
  2742. s1->x = p1.x + *t1 * p21.x;
  2743. s1->y = p1.y + *t1 * p21.y;
  2744. s1->z = p1.z + *t1 * p21.z;
  2745. s2->x = p3.x + *t2 * p43.x;
  2746. s2->y = p3.y + *t2 * p43.y;
  2747. s2->z = p3.z + *t2 * p43.z;
  2748. return true;
  2749. }
  2750. #pragma optimize( "", off )
  2751. #ifndef EXCEPTION_EXECUTE_HANDLER
  2752. #define EXCEPTION_EXECUTE_HANDLER 1
  2753. #endif
  2754. #pragma optimize( "", on )
  2755. static bool s_b3DNowEnabled = false;
  2756. static bool s_bMMXEnabled = false;
  2757. static bool s_bSSEEnabled = false;
  2758. static bool s_bSSE2Enabled = false;
  2759. void MathLib_Init( float gamma, float texGamma, float brightness, int overbright, bool bAllow3DNow, bool bAllowSSE, bool bAllowSSE2, bool bAllowMMX )
  2760. {
  2761. if ( s_bMathlibInitialized )
  2762. return;
  2763. // FIXME: Hook SSE into VectorAligned + Vector4DAligned
  2764. #if !defined( _X360 )
  2765. // Grab the processor information:
  2766. const CPUInformation& pi = *GetCPUInformation();
  2767. // Select the default generic routines.
  2768. pfSqrt = _sqrtf;
  2769. pfRSqrt = _rsqrtf;
  2770. pfRSqrtFast = _rsqrtf;
  2771. pfVectorNormalize = _VectorNormalize;
  2772. pfVectorNormalizeFast = _VectorNormalizeFast;
  2773. pfInvRSquared = _InvRSquared;
  2774. pfFastSinCos = SinCos;
  2775. pfFastCos = cosf;
  2776. if ( bAllowMMX && pi.m_bMMX )
  2777. {
  2778. // Select the MMX specific routines if available
  2779. // (MMX routines were used by SW span fillers - not currently used for HW)
  2780. s_bMMXEnabled = true;
  2781. }
  2782. else
  2783. {
  2784. s_bMMXEnabled = false;
  2785. }
  2786. // SSE Generally performs better than 3DNow when present, so this is placed
  2787. // first to allow SSE to override these settings.
  2788. #if !defined( OSX ) && !defined( PLATFORM_WINDOWS_PC64 ) && !defined(LINUX)
  2789. if ( bAllow3DNow && pi.m_b3DNow )
  2790. {
  2791. s_b3DNowEnabled = true;
  2792. // Select the 3DNow specific routines if available;
  2793. pfVectorNormalize = _3DNow_VectorNormalize;
  2794. pfVectorNormalizeFast = _3DNow_VectorNormalizeFast;
  2795. pfInvRSquared = _3DNow_InvRSquared;
  2796. pfSqrt = _3DNow_Sqrt;
  2797. pfRSqrt = _3DNow_RSqrt;
  2798. pfRSqrtFast = _3DNow_RSqrt;
  2799. }
  2800. else
  2801. #endif
  2802. {
  2803. s_b3DNowEnabled = false;
  2804. }
  2805. if ( bAllowSSE && pi.m_bSSE )
  2806. {
  2807. s_bSSEEnabled = true;
  2808. #ifndef PLATFORM_WINDOWS_PC64
  2809. // These are not yet available.
  2810. // Select the SSE specific routines if available
  2811. pfVectorNormalize = _VectorNormalize;
  2812. pfVectorNormalizeFast = _SSE_VectorNormalizeFast;
  2813. pfInvRSquared = _SSE_InvRSquared;
  2814. pfSqrt = _SSE_Sqrt;
  2815. pfRSqrt = _SSE_RSqrtAccurate;
  2816. pfRSqrtFast = _SSE_RSqrtFast;
  2817. #endif
  2818. #ifdef PLATFORM_WINDOWS_PC32
  2819. pfFastSinCos = _SSE_SinCos;
  2820. pfFastCos = _SSE_cos;
  2821. #endif
  2822. }
  2823. else
  2824. {
  2825. s_bSSEEnabled = false;
  2826. }
  2827. if ( bAllowSSE2 && pi.m_bSSE2 )
  2828. {
  2829. s_bSSE2Enabled = true;
  2830. #ifdef PLATFORM_WINDOWS_PC32
  2831. pfFastSinCos = _SSE2_SinCos;
  2832. pfFastCos = _SSE2_cos;
  2833. #endif
  2834. }
  2835. else
  2836. {
  2837. s_bSSE2Enabled = false;
  2838. }
  2839. #endif // !_X360
  2840. s_bMathlibInitialized = true;
  2841. InitSinCosTable();
  2842. BuildGammaTable( gamma, texGamma, brightness, overbright );
  2843. }
  2844. bool MathLib_3DNowEnabled( void )
  2845. {
  2846. Assert( s_bMathlibInitialized );
  2847. return s_b3DNowEnabled;
  2848. }
  2849. bool MathLib_MMXEnabled( void )
  2850. {
  2851. Assert( s_bMathlibInitialized );
  2852. return s_bMMXEnabled;
  2853. }
  2854. bool MathLib_SSEEnabled( void )
  2855. {
  2856. Assert( s_bMathlibInitialized );
  2857. return s_bSSEEnabled;
  2858. }
  2859. bool MathLib_SSE2Enabled( void )
  2860. {
  2861. Assert( s_bMathlibInitialized );
  2862. return s_bSSE2Enabled;
  2863. }
  2864. float Approach( float target, float value, float speed )
  2865. {
  2866. float delta = target - value;
  2867. if ( delta > speed )
  2868. value += speed;
  2869. else if ( delta < -speed )
  2870. value -= speed;
  2871. else
  2872. value = target;
  2873. return value;
  2874. }
  2875. // BUGBUG: Why doesn't this call angle diff?!?!?
  2876. float ApproachAngle( float target, float value, float speed )
  2877. {
  2878. target = anglemod( target );
  2879. value = anglemod( value );
  2880. float delta = target - value;
  2881. // Speed is assumed to be positive
  2882. if ( speed < 0 )
  2883. speed = -speed;
  2884. if ( delta < -180 )
  2885. delta += 360;
  2886. else if ( delta > 180 )
  2887. delta -= 360;
  2888. if ( delta > speed )
  2889. value += speed;
  2890. else if ( delta < -speed )
  2891. value -= speed;
  2892. else
  2893. value = target;
  2894. return value;
  2895. }
  2896. // BUGBUG: Why do we need both of these?
  2897. float AngleDiff( float destAngle, float srcAngle )
  2898. {
  2899. float delta;
  2900. delta = fmodf(destAngle - srcAngle, 360.0f);
  2901. if ( destAngle > srcAngle )
  2902. {
  2903. if ( delta >= 180 )
  2904. delta -= 360;
  2905. }
  2906. else
  2907. {
  2908. if ( delta <= -180 )
  2909. delta += 360;
  2910. }
  2911. return delta;
  2912. }
  2913. float AngleDistance( float next, float cur )
  2914. {
  2915. float delta = next - cur;
  2916. if ( delta < -180 )
  2917. delta += 360;
  2918. else if ( delta > 180 )
  2919. delta -= 360;
  2920. return delta;
  2921. }
  2922. float AngleNormalize( float angle )
  2923. {
  2924. angle = fmodf(angle, 360.0f);
  2925. if (angle > 180)
  2926. {
  2927. angle -= 360;
  2928. }
  2929. if (angle < -180)
  2930. {
  2931. angle += 360;
  2932. }
  2933. return angle;
  2934. }
  2935. //--------------------------------------------------------------------------------------------------------------
  2936. // ensure that 0 <= angle <= 360
  2937. float AngleNormalizePositive( float angle )
  2938. {
  2939. angle = fmodf( angle, 360.0f );
  2940. if (angle < 0.0f)
  2941. {
  2942. angle += 360.0f;
  2943. }
  2944. return angle;
  2945. }
  2946. //--------------------------------------------------------------------------------------------------------------
  2947. bool AnglesAreEqual( float a, float b, float tolerance )
  2948. {
  2949. return (fabs( AngleDiff( a, b ) ) < tolerance);
  2950. }
  2951. void RotationDeltaAxisAngle( const QAngle &srcAngles, const QAngle &destAngles, Vector &deltaAxis, float &deltaAngle )
  2952. {
  2953. Quaternion srcQuat, destQuat, srcQuatInv, out;
  2954. AngleQuaternion( srcAngles, srcQuat );
  2955. AngleQuaternion( destAngles, destQuat );
  2956. QuaternionScale( srcQuat, -1, srcQuatInv );
  2957. QuaternionMult( destQuat, srcQuatInv, out );
  2958. QuaternionNormalize( out );
  2959. QuaternionAxisAngle( out, deltaAxis, deltaAngle );
  2960. }
  2961. void RotationDelta( const QAngle &srcAngles, const QAngle &destAngles, QAngle *out )
  2962. {
  2963. matrix3x4_t src, srcInv;
  2964. matrix3x4_t dest;
  2965. AngleMatrix( srcAngles, src );
  2966. AngleMatrix( destAngles, dest );
  2967. // xform = src(-1) * dest
  2968. MatrixInvert( src, srcInv );
  2969. matrix3x4_t xform;
  2970. ConcatTransforms( dest, srcInv, xform );
  2971. QAngle xformAngles;
  2972. MatrixAngles( xform, xformAngles );
  2973. if ( out )
  2974. {
  2975. *out = xformAngles;
  2976. }
  2977. }
  2978. //-----------------------------------------------------------------------------
  2979. // Purpose: Computes a triangle normal
  2980. //-----------------------------------------------------------------------------
  2981. void ComputeTrianglePlane( const Vector& v1, const Vector& v2, const Vector& v3, Vector& normal, float& intercept )
  2982. {
  2983. Vector e1, e2;
  2984. VectorSubtract( v2, v1, e1 );
  2985. VectorSubtract( v3, v1, e2 );
  2986. CrossProduct( e1, e2, normal );
  2987. VectorNormalize( normal );
  2988. intercept = DotProduct( normal, v1 );
  2989. }
  2990. //-----------------------------------------------------------------------------
  2991. // Purpose: This is a clone of BaseWindingForPlane()
  2992. // Input : *outVerts - an array of preallocated verts to build the polygon in
  2993. // normal - the plane normal
  2994. // dist - the plane constant
  2995. // Output : int - vert count (always 4)
  2996. //-----------------------------------------------------------------------------
  2997. int PolyFromPlane( Vector *outVerts, const Vector& normal, float dist, float fHalfScale )
  2998. {
  2999. int i, x;
  3000. vec_t max, v;
  3001. Vector org, vright, vup;
  3002. // find the major axis
  3003. max = -16384; //MAX_COORD_INTEGER
  3004. x = -1;
  3005. for (i=0 ; i<3; i++)
  3006. {
  3007. v = fabs(normal[i]);
  3008. if (v > max)
  3009. {
  3010. x = i;
  3011. max = v;
  3012. }
  3013. }
  3014. if (x==-1)
  3015. return 0;
  3016. // Build a unit vector along something other than the major axis
  3017. VectorCopy (vec3_origin, vup);
  3018. switch (x)
  3019. {
  3020. case 0:
  3021. case 1:
  3022. vup[2] = 1;
  3023. break;
  3024. case 2:
  3025. vup[0] = 1;
  3026. break;
  3027. }
  3028. // Remove the component of this vector along the normal
  3029. v = DotProduct (vup, normal);
  3030. VectorMA (vup, -v, normal, vup);
  3031. // Make it a unit (perpendicular)
  3032. VectorNormalize (vup);
  3033. // Center of the poly is at normal * dist
  3034. VectorScale (normal, dist, org);
  3035. // Calculate the third orthonormal basis vector for our plane space (this one and vup are in the plane)
  3036. CrossProduct (vup, normal, vright);
  3037. // Make the plane's basis vectors big (these are the half-sides of the polygon we're making)
  3038. VectorScale (vup, fHalfScale, vup);
  3039. VectorScale (vright, fHalfScale, vright);
  3040. // Move diagonally away from org to create the corner verts
  3041. VectorSubtract (org, vright, outVerts[0]); // left
  3042. VectorAdd (outVerts[0], vup, outVerts[0]); // up
  3043. VectorAdd (org, vright, outVerts[1]); // right
  3044. VectorAdd (outVerts[1], vup, outVerts[1]); // up
  3045. VectorAdd (org, vright, outVerts[2]); // right
  3046. VectorSubtract (outVerts[2], vup, outVerts[2]); // down
  3047. VectorSubtract (org, vright, outVerts[3]); // left
  3048. VectorSubtract (outVerts[3], vup, outVerts[3]); // down
  3049. // The four corners form a planar quadrilateral normal to "normal"
  3050. return 4;
  3051. }
  3052. //-----------------------------------------------------------------------------
  3053. // Purpose: clip a poly to the plane and return the poly on the front side of the plane
  3054. // Input : *inVerts - input polygon
  3055. // vertCount - # verts in input poly
  3056. // *outVerts - destination poly
  3057. // normal - plane normal
  3058. // dist - plane constant
  3059. // Output : int - # verts in output poly
  3060. //-----------------------------------------------------------------------------
  3061. int ClipPolyToPlane( Vector *inVerts, int vertCount, Vector *outVerts, const Vector& normal, float dist, float fOnPlaneEpsilon )
  3062. {
  3063. vec_t *dists = (vec_t *)stackalloc( sizeof(vec_t) * vertCount * 4 ); //4x vertcount should cover all cases
  3064. int *sides = (int *)stackalloc( sizeof(vec_t) * vertCount * 4 );
  3065. int counts[3];
  3066. vec_t dot;
  3067. int i, j;
  3068. Vector mid = vec3_origin;
  3069. int outCount;
  3070. counts[0] = counts[1] = counts[2] = 0;
  3071. // determine sides for each point
  3072. for ( i = 0; i < vertCount; i++ )
  3073. {
  3074. dot = DotProduct( inVerts[i], normal) - dist;
  3075. dists[i] = dot;
  3076. if ( dot > fOnPlaneEpsilon )
  3077. {
  3078. sides[i] = SIDE_FRONT;
  3079. }
  3080. else if ( dot < -fOnPlaneEpsilon )
  3081. {
  3082. sides[i] = SIDE_BACK;
  3083. }
  3084. else
  3085. {
  3086. sides[i] = SIDE_ON;
  3087. }
  3088. counts[sides[i]]++;
  3089. }
  3090. sides[i] = sides[0];
  3091. dists[i] = dists[0];
  3092. if (!counts[0])
  3093. return 0;
  3094. if (!counts[1])
  3095. {
  3096. // Copy to output verts
  3097. for ( i = 0; i < vertCount; i++ )
  3098. {
  3099. VectorCopy( inVerts[i], outVerts[i] );
  3100. }
  3101. return vertCount;
  3102. }
  3103. outCount = 0;
  3104. for ( i = 0; i < vertCount; i++ )
  3105. {
  3106. Vector& p1 = inVerts[i];
  3107. if (sides[i] == SIDE_ON)
  3108. {
  3109. VectorCopy( p1, outVerts[outCount]);
  3110. outCount++;
  3111. continue;
  3112. }
  3113. if (sides[i] == SIDE_FRONT)
  3114. {
  3115. VectorCopy( p1, outVerts[outCount]);
  3116. outCount++;
  3117. }
  3118. if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
  3119. continue;
  3120. // generate a split point
  3121. Vector& p2 = inVerts[(i+1)%vertCount];
  3122. dot = dists[i] / (dists[i]-dists[i+1]);
  3123. for (j=0 ; j<3 ; j++)
  3124. { // avoid round off error when possible
  3125. if (normal[j] == 1)
  3126. mid[j] = dist;
  3127. else if (normal[j] == -1)
  3128. mid[j] = -dist;
  3129. else
  3130. mid[j] = p1[j] + dot*(p2[j]-p1[j]);
  3131. }
  3132. VectorCopy (mid, outVerts[outCount]);
  3133. outCount++;
  3134. }
  3135. return outCount;
  3136. }
  3137. int ClipPolyToPlane_Precise( double *inVerts, int vertCount, double *outVerts, const double *normal, double dist, double fOnPlaneEpsilon )
  3138. {
  3139. double *dists = (double *)stackalloc( sizeof(double) * vertCount * 4 ); //4x vertcount should cover all cases
  3140. int *sides = (int *)stackalloc( sizeof(double) * vertCount * 4 );
  3141. int counts[3];
  3142. double dot;
  3143. int i, j;
  3144. //Vector mid = vec3_origin;
  3145. double mid[3];
  3146. mid[0] = 0.0;
  3147. mid[1] = 0.0;
  3148. mid[2] = 0.0;
  3149. int outCount;
  3150. counts[0] = counts[1] = counts[2] = 0;
  3151. // determine sides for each point
  3152. for ( i = 0; i < vertCount; i++ )
  3153. {
  3154. //dot = DotProduct( inVerts[i], normal) - dist;
  3155. dot = ((inVerts[i*3 + 0] * normal[0]) + (inVerts[i*3 + 1] * normal[1]) + (inVerts[i*3 + 2] * normal[2])) - dist;
  3156. dists[i] = dot;
  3157. if ( dot > fOnPlaneEpsilon )
  3158. {
  3159. sides[i] = SIDE_FRONT;
  3160. }
  3161. else if ( dot < -fOnPlaneEpsilon )
  3162. {
  3163. sides[i] = SIDE_BACK;
  3164. }
  3165. else
  3166. {
  3167. sides[i] = SIDE_ON;
  3168. }
  3169. counts[sides[i]]++;
  3170. }
  3171. sides[i] = sides[0];
  3172. dists[i] = dists[0];
  3173. if (!counts[0])
  3174. return 0;
  3175. if (!counts[1])
  3176. {
  3177. // Copy to output verts
  3178. //for ( i = 0; i < vertCount; i++ )
  3179. for ( i = 0; i < vertCount * 3; i++ )
  3180. {
  3181. //VectorCopy( inVerts[i], outVerts[i] );
  3182. outVerts[i] = inVerts[i];
  3183. }
  3184. return vertCount;
  3185. }
  3186. outCount = 0;
  3187. for ( i = 0; i < vertCount; i++ )
  3188. {
  3189. //Vector& p1 = inVerts[i];
  3190. double *p1 = &inVerts[i*3];
  3191. //p1[0] = inVerts[i*3 + 0];
  3192. //p1[1] = inVerts[i*3 + 1];
  3193. //p1[2] = inVerts[i*3 + 2];
  3194. if (sides[i] == SIDE_ON)
  3195. {
  3196. //VectorCopy( p1, outVerts[outCount]);
  3197. outVerts[outCount*3 + 0] = p1[0];
  3198. outVerts[outCount*3 + 1] = p1[1];
  3199. outVerts[outCount*3 + 2] = p1[2];
  3200. outCount++;
  3201. continue;
  3202. }
  3203. if (sides[i] == SIDE_FRONT)
  3204. {
  3205. //VectorCopy( p1, outVerts[outCount]);
  3206. outVerts[outCount*3 + 0] = p1[0];
  3207. outVerts[outCount*3 + 1] = p1[1];
  3208. outVerts[outCount*3 + 2] = p1[2];
  3209. outCount++;
  3210. }
  3211. if (sides[i+1] == SIDE_ON || sides[i+1] == sides[i])
  3212. continue;
  3213. // generate a split point
  3214. //Vector& p2 = inVerts[(i+1)%vertCount];
  3215. int wrappedindex = (i+1)%vertCount;
  3216. double *p2 = &inVerts[wrappedindex*3];
  3217. //p2[0] = inVerts[wrappedindex*3 + 0];
  3218. //p2[1] = inVerts[wrappedindex*3 + 1];
  3219. //p2[2] = inVerts[wrappedindex*3 + 2];
  3220. dot = dists[i] / (dists[i]-dists[i+1]);
  3221. for (j=0 ; j<3 ; j++)
  3222. {
  3223. mid[j] = (double)p1[j] + dot*((double)p2[j]-(double)p1[j]);
  3224. }
  3225. //VectorCopy (mid, outVerts[outCount]);
  3226. outVerts[outCount*3 + 0] = mid[0];
  3227. outVerts[outCount*3 + 1] = mid[1];
  3228. outVerts[outCount*3 + 2] = mid[2];
  3229. outCount++;
  3230. }
  3231. return outCount;
  3232. }
  3233. int CeilPow2( int in )
  3234. {
  3235. int retval;
  3236. retval = 1;
  3237. while( retval < in )
  3238. retval <<= 1;
  3239. return retval;
  3240. }
  3241. int FloorPow2( int in )
  3242. {
  3243. int retval;
  3244. retval = 1;
  3245. while( retval < in )
  3246. retval <<= 1;
  3247. return retval >> 1;
  3248. }
  3249. //-----------------------------------------------------------------------------
  3250. // Computes Y fov from an X fov and a screen aspect ratio
  3251. //-----------------------------------------------------------------------------
  3252. float CalcFovY( float flFovX, float flAspect )
  3253. {
  3254. if ( flFovX < 1 || flFovX > 179)
  3255. {
  3256. flFovX = 90; // error, set to 90
  3257. }
  3258. // The long, but illustrative version (more closely matches CShaderAPIDX8::PerspectiveX, which
  3259. // is what it's based on).
  3260. //
  3261. //float width = 2 * zNear * tan( DEG2RAD( fov_x / 2.0 ) );
  3262. //float height = width / screenaspect;
  3263. //float yRadians = atan( (height/2.0) / zNear );
  3264. //return RAD2DEG( yRadians ) * 2;
  3265. // The short and sweet version.
  3266. float val = atan( tan( DEG2RAD( flFovX ) * 0.5f ) / flAspect );
  3267. val = RAD2DEG( val ) * 2.0f;
  3268. return val;
  3269. }
  3270. float CalcFovX( float flFovY, float flAspect )
  3271. {
  3272. return RAD2DEG( atan( tan( DEG2RAD( flFovY ) * 0.5f ) * flAspect ) ) * 2.0f;
  3273. }
  3274. //-----------------------------------------------------------------------------
  3275. // Generate a frustum based on perspective view parameters
  3276. //-----------------------------------------------------------------------------
  3277. void GeneratePerspectiveFrustum( const Vector& origin, const Vector &forward,
  3278. const Vector &right, const Vector &up, float flZNear, float flZFar,
  3279. float flFovX, float flFovY, Frustum_t &frustum )
  3280. {
  3281. float flIntercept = DotProduct( origin, forward );
  3282. // Setup the near and far planes.
  3283. frustum.SetPlane( FRUSTUM_FARZ, PLANE_ANYZ, -forward, -flZFar - flIntercept );
  3284. frustum.SetPlane( FRUSTUM_NEARZ, PLANE_ANYZ, forward, flZNear + flIntercept );
  3285. flFovX *= 0.5f;
  3286. flFovY *= 0.5f;
  3287. float flTanX = tan( DEG2RAD( flFovX ) );
  3288. float flTanY = tan( DEG2RAD( flFovY ) );
  3289. // OPTIMIZE: Normalizing these planes is not necessary for culling
  3290. Vector normalPos, normalNeg;
  3291. VectorMA( right, flTanX, forward, normalPos );
  3292. VectorMA( normalPos, -2.0f, right, normalNeg );
  3293. VectorNormalize( normalPos );
  3294. VectorNormalize( normalNeg );
  3295. frustum.SetPlane( FRUSTUM_LEFT, PLANE_ANYZ, normalPos, normalPos.Dot( origin ) );
  3296. frustum.SetPlane( FRUSTUM_RIGHT, PLANE_ANYZ, normalNeg, normalNeg.Dot( origin ) );
  3297. VectorMA( up, flTanY, forward, normalPos );
  3298. VectorMA( normalPos, -2.0f, up, normalNeg );
  3299. VectorNormalize( normalPos );
  3300. VectorNormalize( normalNeg );
  3301. frustum.SetPlane( FRUSTUM_BOTTOM, PLANE_ANYZ, normalPos, normalPos.Dot( origin ) );
  3302. frustum.SetPlane( FRUSTUM_TOP, PLANE_ANYZ, normalNeg, normalNeg.Dot( origin ) );
  3303. }
  3304. //-----------------------------------------------------------------------------
  3305. // Version that accepts angles instead of vectors
  3306. //-----------------------------------------------------------------------------
  3307. void GeneratePerspectiveFrustum( const Vector& origin, const QAngle &angles, float flZNear, float flZFar, float flFovX, float flAspectRatio, Frustum_t &frustum )
  3308. {
  3309. Vector vecForward, vecRight, vecUp;
  3310. AngleVectors( angles, &vecForward, &vecRight, &vecUp );
  3311. float flFovY = CalcFovY( flFovX, flAspectRatio );
  3312. GeneratePerspectiveFrustum( origin, vecForward, vecRight, vecUp, flZNear, flZFar, flFovX, flFovY, frustum );
  3313. }
  3314. bool R_CullBox( const Vector& mins, const Vector& maxs, const Frustum_t &frustum )
  3315. {
  3316. return (( BoxOnPlaneSide( mins, maxs, frustum.GetPlane(FRUSTUM_RIGHT) ) == 2 ) ||
  3317. ( BoxOnPlaneSide( mins, maxs, frustum.GetPlane(FRUSTUM_LEFT) ) == 2 ) ||
  3318. ( BoxOnPlaneSide( mins, maxs, frustum.GetPlane(FRUSTUM_TOP) ) == 2 ) ||
  3319. ( BoxOnPlaneSide( mins, maxs, frustum.GetPlane(FRUSTUM_BOTTOM) ) == 2 ) ||
  3320. ( BoxOnPlaneSide( mins, maxs, frustum.GetPlane(FRUSTUM_NEARZ) ) == 2 ) ||
  3321. ( BoxOnPlaneSide( mins, maxs, frustum.GetPlane(FRUSTUM_FARZ) ) == 2 ) );
  3322. }
  3323. bool R_CullBoxSkipNear( const Vector& mins, const Vector& maxs, const Frustum_t &frustum )
  3324. {
  3325. return (( BoxOnPlaneSide( mins, maxs, frustum.GetPlane(FRUSTUM_RIGHT) ) == 2 ) ||
  3326. ( BoxOnPlaneSide( mins, maxs, frustum.GetPlane(FRUSTUM_LEFT) ) == 2 ) ||
  3327. ( BoxOnPlaneSide( mins, maxs, frustum.GetPlane(FRUSTUM_TOP) ) == 2 ) ||
  3328. ( BoxOnPlaneSide( mins, maxs, frustum.GetPlane(FRUSTUM_BOTTOM) ) == 2 ) ||
  3329. ( BoxOnPlaneSide( mins, maxs, frustum.GetPlane(FRUSTUM_FARZ) ) == 2 ) );
  3330. }
  3331. // NOTE: This routine was taken (and modified) from NVidia's BlinnReflection demo
  3332. // Creates basis vectors, based on a vertex and index list.
  3333. // See the NVidia white paper 'GDC2K PerPixel Lighting' for a description
  3334. // of how this computation works
  3335. #define SMALL_FLOAT 1e-12
  3336. void CalcTriangleTangentSpace( const Vector &p0, const Vector &p1, const Vector &p2,
  3337. const Vector2D &t0, const Vector2D &t1, const Vector2D& t2,
  3338. Vector &sVect, Vector &tVect )
  3339. {
  3340. /* Compute the partial derivatives of X, Y, and Z with respect to S and T. */
  3341. sVect.Init( 0.0f, 0.0f, 0.0f );
  3342. tVect.Init( 0.0f, 0.0f, 0.0f );
  3343. // x, s, t
  3344. Vector edge01( p1.x - p0.x, t1.x - t0.x, t1.y - t0.y );
  3345. Vector edge02( p2.x - p0.x, t2.x - t0.x, t2.y - t0.y );
  3346. Vector cross;
  3347. CrossProduct( edge01, edge02, cross );
  3348. if ( fabs( cross.x ) > SMALL_FLOAT )
  3349. {
  3350. sVect.x += -cross.y / cross.x;
  3351. tVect.x += -cross.z / cross.x;
  3352. }
  3353. // y, s, t
  3354. edge01.Init( p1.y - p0.y, t1.x - t0.x, t1.y - t0.y );
  3355. edge02.Init( p2.y - p0.y, t2.x - t0.x, t2.y - t0.y );
  3356. CrossProduct( edge01, edge02, cross );
  3357. if ( fabs( cross.x ) > SMALL_FLOAT )
  3358. {
  3359. sVect.y += -cross.y / cross.x;
  3360. tVect.y += -cross.z / cross.x;
  3361. }
  3362. // z, s, t
  3363. edge01.Init( p1.z - p0.z, t1.x - t0.x, t1.y - t0.y );
  3364. edge02.Init( p2.z - p0.z, t2.x - t0.x, t2.y - t0.y );
  3365. CrossProduct( edge01, edge02, cross );
  3366. if( fabs( cross.x ) > SMALL_FLOAT )
  3367. {
  3368. sVect.z += -cross.y / cross.x;
  3369. tVect.z += -cross.z / cross.x;
  3370. }
  3371. // Normalize sVect and tVect
  3372. VectorNormalize( sVect );
  3373. VectorNormalize( tVect );
  3374. }
  3375. //-----------------------------------------------------------------------------
  3376. // Convert RGB to HSV
  3377. //-----------------------------------------------------------------------------
  3378. void RGBtoHSV( const Vector &rgb, Vector &hsv )
  3379. {
  3380. float flMax = max( rgb.x, rgb.y );
  3381. flMax = max( flMax, rgb.z );
  3382. float flMin = min( rgb.x, rgb.y );
  3383. flMin = min( flMin, rgb.z );
  3384. // hsv.z is the value
  3385. hsv.z = flMax;
  3386. // hsv.y is the saturation
  3387. if (flMax != 0.0F)
  3388. {
  3389. hsv.y = (flMax - flMin) / flMax;
  3390. }
  3391. else
  3392. {
  3393. hsv.y = 0.0F;
  3394. }
  3395. // hsv.x is the hue
  3396. if (hsv.y == 0.0F)
  3397. {
  3398. hsv.x = -1.0f;
  3399. }
  3400. else
  3401. {
  3402. float32 d = flMax - flMin;
  3403. if (rgb.x == flMax)
  3404. {
  3405. hsv.x = (rgb.y - rgb.z) / d;
  3406. }
  3407. else if (rgb.y == flMax)
  3408. {
  3409. hsv.x = 2.0F + (rgb.z - rgb.x) / d;
  3410. }
  3411. else
  3412. {
  3413. hsv.x = 4.0F + (rgb.x - rgb.y) / d;
  3414. }
  3415. hsv.x *= 60.0F;
  3416. if ( hsv.x < 0.0F )
  3417. {
  3418. hsv.x += 360.0F;
  3419. }
  3420. }
  3421. }
  3422. //-----------------------------------------------------------------------------
  3423. // Convert HSV to RGB
  3424. //-----------------------------------------------------------------------------
  3425. void HSVtoRGB( const Vector &hsv, Vector &rgb )
  3426. {
  3427. if ( hsv.y == 0.0F )
  3428. {
  3429. rgb.Init( hsv.z, hsv.z, hsv.z );
  3430. return;
  3431. }
  3432. float32 hue = hsv.x;
  3433. if (hue == 360.0F)
  3434. {
  3435. hue = 0.0F;
  3436. }
  3437. hue /= 60.0F;
  3438. int i = hue; // integer part
  3439. float32 f = hue - i; // fractional part
  3440. float32 p = hsv.z * (1.0F - hsv.y);
  3441. float32 q = hsv.z * (1.0F - hsv.y * f);
  3442. float32 t = hsv.z * (1.0F - hsv.y * (1.0F - f));
  3443. switch(i)
  3444. {
  3445. case 0: rgb.Init( hsv.z, t, p ); break;
  3446. case 1: rgb.Init( q, hsv.z, p ); break;
  3447. case 2: rgb.Init( p, hsv.z, t ); break;
  3448. case 3: rgb.Init( p, q, hsv.z ); break;
  3449. case 4: rgb.Init( t, p, hsv.z ); break;
  3450. case 5: rgb.Init( hsv.z, p, q ); break;
  3451. }
  3452. }
  3453. void GetInterpolationData( float const *pKnotPositions,
  3454. float const *pKnotValues,
  3455. int nNumValuesinList,
  3456. int nInterpolationRange,
  3457. float flPositionToInterpolateAt,
  3458. bool bWrap,
  3459. float *pValueA,
  3460. float *pValueB,
  3461. float *pInterpolationValue)
  3462. {
  3463. // first, find the bracketting knots by looking for the first knot >= our index
  3464. int idx;
  3465. for(idx = 0; idx < nNumValuesinList; idx++ )
  3466. {
  3467. if ( pKnotPositions[idx] >= flPositionToInterpolateAt )
  3468. break;
  3469. }
  3470. int nKnot1, nKnot2;
  3471. float flOffsetFromStartOfGap, flSizeOfGap;
  3472. if ( idx == 0)
  3473. {
  3474. if ( bWrap )
  3475. {
  3476. nKnot1 = nNumValuesinList-1;
  3477. nKnot2 = 0;
  3478. flSizeOfGap =
  3479. ( pKnotPositions[nKnot2] + ( nInterpolationRange-pKnotPositions[nKnot1] ) );
  3480. flOffsetFromStartOfGap =
  3481. flPositionToInterpolateAt + ( nInterpolationRange-pKnotPositions[nKnot1] );
  3482. }
  3483. else
  3484. {
  3485. *pValueA = *pValueB = pKnotValues[0];
  3486. *pInterpolationValue = 1.0;
  3487. return;
  3488. }
  3489. }
  3490. else if ( idx == nNumValuesinList ) // ran out of values
  3491. {
  3492. if ( bWrap )
  3493. {
  3494. nKnot1 = nNumValuesinList -1;
  3495. nKnot2 = 0;
  3496. flSizeOfGap = ( pKnotPositions[nKnot2] +
  3497. ( nInterpolationRange-pKnotPositions[nKnot1] ) );
  3498. flOffsetFromStartOfGap = flPositionToInterpolateAt - pKnotPositions[nKnot1];
  3499. }
  3500. else
  3501. {
  3502. *pValueA = *pValueB = pKnotValues[nNumValuesinList-1];
  3503. *pInterpolationValue = 1.0;
  3504. return;
  3505. }
  3506. }
  3507. else
  3508. {
  3509. nKnot1 = idx-1;
  3510. nKnot2 = idx;
  3511. flSizeOfGap = pKnotPositions[nKnot2]-pKnotPositions[nKnot1];
  3512. flOffsetFromStartOfGap = flPositionToInterpolateAt-pKnotPositions[nKnot1];
  3513. }
  3514. *pValueA = pKnotValues[nKnot1];
  3515. *pValueB = pKnotValues[nKnot2];
  3516. *pInterpolationValue = FLerp( 0, 1, 0, flSizeOfGap, flOffsetFromStartOfGap );
  3517. return;
  3518. }
  3519. float RandomVectorInUnitSphere( Vector *pVector )
  3520. {
  3521. // Guarantee uniform random distribution within a sphere
  3522. // Graphics gems III contains this algorithm ("Nonuniform random point sets via warping")
  3523. float u = ((float)rand() / VALVE_RAND_MAX);
  3524. float v = ((float)rand() / VALVE_RAND_MAX);
  3525. float w = ((float)rand() / VALVE_RAND_MAX);
  3526. float flPhi = acos( 1 - 2 * u );
  3527. float flTheta = 2 * M_PI * v;
  3528. float flRadius = powf( w, 1.0f / 3.0f );
  3529. float flSinPhi, flCosPhi;
  3530. float flSinTheta, flCosTheta;
  3531. SinCos( flPhi, &flSinPhi, &flCosPhi );
  3532. SinCos( flTheta, &flSinTheta, &flCosTheta );
  3533. pVector->x = flRadius * flSinPhi * flCosTheta;
  3534. pVector->y = flRadius * flSinPhi * flSinTheta;
  3535. pVector->z = flRadius * flCosPhi;
  3536. return flRadius;
  3537. }
  3538. float RandomVectorInUnitCircle( Vector2D *pVector )
  3539. {
  3540. // Guarantee uniform random distribution within a sphere
  3541. // Graphics gems III contains this algorithm ("Nonuniform random point sets via warping")
  3542. float u = ((float)rand() / VALVE_RAND_MAX);
  3543. float v = ((float)rand() / VALVE_RAND_MAX);
  3544. float flTheta = 2 * M_PI * v;
  3545. float flRadius = powf( u, 1.0f / 2.0f );
  3546. float flSinTheta, flCosTheta;
  3547. SinCos( flTheta, &flSinTheta, &flCosTheta );
  3548. pVector->x = flRadius * flCosTheta;
  3549. pVector->y = flRadius * flSinTheta;
  3550. return flRadius;
  3551. }
  3552. #ifdef FP_EXCEPTIONS_ENABLED
  3553. #include <float.h> // For _clearfp and _controlfp_s
  3554. #endif
  3555. // FPExceptionDisable and FPExceptionEnabler taken from my blog post
  3556. // at http://www.altdevblogaday.com/2012/04/20/exceptional-floating-point/
  3557. #ifdef FP_EXCEPTIONS_ENABLED
  3558. // These functions are all inlined NOPs if FP_EXCEPTIONS_ENABLED is not defined.
  3559. FPExceptionDisabler::FPExceptionDisabler()
  3560. {
  3561. // Retrieve the current state of the exception flags. This
  3562. // must be done before changing them. _MCW_EM is a bit
  3563. // mask representing all available exception masks.
  3564. _controlfp_s(&mOldValues, 0, 0);
  3565. // Set all of the exception flags, which suppresses FP
  3566. // exceptions on the x87 and SSE units.
  3567. _controlfp_s(0, _MCW_EM, _MCW_EM);
  3568. }
  3569. FPExceptionDisabler::~FPExceptionDisabler()
  3570. {
  3571. // Clear any pending FP exceptions. This must be done
  3572. // prior to enabling FP exceptions since otherwise there
  3573. // may be a 'deferred crash' as soon the exceptions are
  3574. // enabled.
  3575. _clearfp();
  3576. // Reset (possibly enabling) the exception status.
  3577. _controlfp_s(0, mOldValues, _MCW_EM);
  3578. }
  3579. // Overflow, divide-by-zero, and invalid-operation are the FP
  3580. // exceptions most frequently associated with bugs.
  3581. FPExceptionEnabler::FPExceptionEnabler(unsigned int enableBits /*= _EM_OVERFLOW | _EM_ZERODIVIDE | _EM_INVALID*/)
  3582. {
  3583. // Retrieve the current state of the exception flags. This
  3584. // must be done before changing them. _MCW_EM is a bit
  3585. // mask representing all available exception masks.
  3586. _controlfp_s(&mOldValues, 0, 0);
  3587. // Make sure no non-exception flags have been specified,
  3588. // to avoid accidental changing of rounding modes, etc.
  3589. enableBits &= _MCW_EM;
  3590. // Clear any pending FP exceptions. This must be done
  3591. // prior to enabling FP exceptions since otherwise there
  3592. // may be a 'deferred crash' as soon the exceptions are
  3593. // enabled.
  3594. _clearfp();
  3595. // Zero out the specified bits, leaving other bits alone.
  3596. _controlfp_s(0, ~enableBits, enableBits);
  3597. }
  3598. FPExceptionEnabler::~FPExceptionEnabler()
  3599. {
  3600. // Reset the exception state.
  3601. _controlfp_s(0, mOldValues, _MCW_EM);
  3602. }
  3603. #endif