Counter Strike : Global Offensive Source Code
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.

178 lines
5.4 KiB

  1. //==== Copyright (c) 1996-2011, Valve Corporation, All rights reserved. =====//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #if !defined(_STATIC_LINKED) || defined(_SHARED_LIB)
  9. #include "mathlib/transform.h"
  10. #include "mathlib/mathlib.h"
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include "tier0/memdbgon.h"
  13. const CTransform g_TransformIdentity( Vector( 0.0f, 0.0f, 0.0f ), Quaternion( 0.0f, 0.0f, 0.0f, 1.0f ) );
  14. void SetIdentityTransform( CTransform &out )
  15. {
  16. out.m_vPosition = vec3_origin;
  17. out.m_orientation = quat_identity;
  18. }
  19. void ConcatTransforms( const CTransform &in1, const CTransform &in2, CTransform &out )
  20. {
  21. // Store in temp to avoid problems if out == in1 or out == in2
  22. CTransform result;
  23. QuaternionMult( in1.m_orientation, in2.m_orientation, result.m_orientation );
  24. QuaternionMultiply( in1.m_orientation, in2.m_vPosition, result.m_vPosition );
  25. result.m_vPosition += in1.m_vPosition;
  26. out = result;
  27. }
  28. void VectorIRotate( const Vector &v, const CTransform &t, Vector &out )
  29. {
  30. // FIXME: Make work directly with the transform
  31. matrix3x4_t m;
  32. TransformMatrix( t, m );
  33. VectorIRotate( v, m, out );
  34. }
  35. void VectorITransform( const Vector &v, const CTransform &t, Vector &out )
  36. {
  37. // FIXME: Make work directly with the transform
  38. matrix3x4_t m;
  39. TransformMatrix( t, m );
  40. VectorITransform( v, m, out );
  41. }
  42. void TransformSlerp( const CTransform &p, const CTransform &q, float t, CTransform &qt )
  43. {
  44. QuaternionSlerp( p.m_orientation, q.m_orientation, t, qt.m_orientation );
  45. VectorLerp( p.m_vPosition, q.m_vPosition, t, qt.m_vPosition );
  46. }
  47. void TransformLerp( const CTransform &p, const CTransform &q, float t, CTransform &qt )
  48. {
  49. QuaternionBlend( p.m_orientation, q.m_orientation, t, qt.m_orientation );
  50. VectorLerp( p.m_vPosition, q.m_vPosition, t, qt.m_vPosition );
  51. }
  52. void TransformMatrix( const CTransform &in, matrix3x4_t &out )
  53. {
  54. QuaternionMatrix( in.m_orientation, in.m_vPosition, out );
  55. }
  56. void TransformMatrix( const CTransformUnaligned &in, matrix3x4_t &out )
  57. {
  58. QuaternionMatrix( in.m_orientation, in.m_vPosition, out );
  59. }
  60. void TransformMatrix( const CTransform &in, const Vector &vScaleIn, matrix3x4_t &out )
  61. {
  62. QuaternionMatrix( in.m_orientation, in.m_vPosition, vScaleIn, out );
  63. }
  64. void MatrixTransform( const matrix3x4_t &in, CTransformUnaligned &out )
  65. {
  66. MatrixQuaternion( in, out.m_orientation );
  67. MatrixGetColumn( in, ORIGIN, out.m_vPosition );
  68. }
  69. void MatrixTransform( const matrix3x4_t &in, CTransform &out )
  70. {
  71. MatrixQuaternion( in, out.m_orientation );
  72. MatrixGetColumn( in, ORIGIN, out.m_vPosition );
  73. }
  74. void MatrixTransform( const matrix3x4_t &in, CTransform &out, Vector &vScaleOut )
  75. {
  76. matrix3x4_t norm;
  77. vScaleOut = MatrixNormalize( in, norm );
  78. MatrixTransform( norm, out );
  79. }
  80. void AngleTransform( const QAngle &angles, const Vector &origin, CTransform &out )
  81. {
  82. AngleQuaternion( angles, out.m_orientation );
  83. out.m_vPosition = origin;
  84. }
  85. void TransformInvert( const CTransform &in, CTransform &out )
  86. {
  87. QuaternionInvert( in.m_orientation, out.m_orientation );
  88. QuaternionMultiply( out.m_orientation, in.m_vPosition, out.m_vPosition );
  89. out.m_vPosition *= -1.0f;
  90. }
  91. void AxisAngleTransform( const Vector &vecAxis, float flAngleDegrees, CTransform &out )
  92. {
  93. AxisAngleQuaternion( vecAxis, flAngleDegrees, out.m_orientation );
  94. out.m_vPosition = vec3_origin;
  95. }
  96. void TransformVectorsFLU( const CTransform &in, Vector* pForward, Vector *pLeft, Vector *pUp )
  97. {
  98. QuaternionVectorsFLU( in.m_orientation, pForward, pLeft, pUp );
  99. }
  100. void TransformVectorsForward( const CTransform &in, Vector* pForward )
  101. {
  102. QuaternionVectorsForward( in.m_orientation, pForward );
  103. }
  104. bool TransformsAreEqual( const CTransform &src1, const CTransform &src2, float flPosTolerance, float flRotTolerance )
  105. {
  106. if ( !VectorsAreEqual( src1.m_vPosition, src2.m_vPosition, flPosTolerance ) )
  107. return false;
  108. return QuaternionsAreEqual( src1.m_orientation, src2.m_orientation, flRotTolerance );
  109. }
  110. // FIXME: optimize this with simd goodness
  111. void TransformToWorldSpace( int nRootTransformCount, int nTransformCount, const int *pParentIndices, CTransform *pTransforms )
  112. {
  113. #ifdef _DEBUG
  114. for ( int i = 0; i < nRootTransformCount; ++i )
  115. {
  116. Assert( pParentIndices[i] < 0 );
  117. }
  118. #endif
  119. for ( int i = nRootTransformCount; i < nTransformCount; ++i )
  120. {
  121. int nParentBone = pParentIndices[i];
  122. Assert( nParentBone >= 0 && nParentBone < i );
  123. ConcatTransforms( pTransforms[ nParentBone ], pTransforms[ i ], pTransforms[ i ] );
  124. }
  125. }
  126. // FIXME: optimize this with simd goodness
  127. void TransformToParentSpace( int nRootTransformCount, int nTransformCount, const int *pParentIndices, CTransform *pTransforms )
  128. {
  129. #ifdef _DEBUG
  130. for ( int i = 0; i < nRootTransformCount; ++i )
  131. {
  132. Assert( pParentIndices[i] < 0 );
  133. }
  134. #endif
  135. bool *pComputedParentTransform = (bool*)stackalloc( nTransformCount * sizeof(bool) );
  136. memset( pComputedParentTransform, 0, nTransformCount * sizeof(bool) );
  137. CTransform *pWorldToParentTransforms = (CTransform*)stackalloc( nTransformCount * sizeof(CTransform) );
  138. for ( int b = nTransformCount; --b >= nRootTransformCount; )
  139. {
  140. int nParentBone = pParentIndices[ b ];
  141. if ( !pComputedParentTransform[ nParentBone ] )
  142. {
  143. TransformInvert( pTransforms[ nParentBone ], pWorldToParentTransforms[ nParentBone ] );
  144. pComputedParentTransform[ nParentBone ] = true;
  145. }
  146. ConcatTransforms( pWorldToParentTransforms[ nParentBone ], pTransforms[ b ], pTransforms[ b ] );
  147. }
  148. }
  149. #endif // !_STATIC_LINKED || _SHARED_LIB