hkMathUtil.h
上传用户:yisoukefu
上传日期:2020-08-09
资源大小:39506k
文件大小:4k
源码类别:

其他游戏

开发平台:

Visual C++

  1. /* 
  2.  * 
  3.  * Confidential Information of Telekinesys Research Limited (t/a Havok). Not for disclosure or distribution without Havok's
  4.  * prior written consent. This software contains code, techniques and know-how which is confidential and proprietary to Havok.
  5.  * Level 2 and Level 3 source code contains trade secrets of Havok. Havok Software (C) Copyright 1999-2009 Telekinesys Research Limited t/a Havok. All Rights Reserved. Use of this software is subject to the terms of an end user license agreement.
  6.  * 
  7.  */
  8. #ifndef HK_MATH_MATH_UTIL_H
  9. #define HK_MATH_MATH_UTIL_H
  10. #include <Common/Base/hkBase.h>
  11. namespace hkMathUtil
  12. {
  13. // [row0col0, row0col1, row1col0, row1col1]
  14. inline hkBool HK_CALL invert2x2Matrix(const hkReal* m, hkReal* out, hkReal tolerance);
  15. /// Structure returned by decompose4x4ColTransform
  16. struct Decomposition
  17. {
  18. HK_DECLARE_NONVIRTUAL_CLASS_ALLOCATOR( HK_MEMORY_CLASS_MATH, Decomposition );
  19. hkVector4 m_translation; // T
  20. hkQuaternion m_rotation; // R
  21. hkVector4 m_scale; // S
  22. hkBool m_hasScale; // True if S !=(1,1,1)
  23. hkBool m_hasSkew; // True if T*R*S != M (original Matrix)
  24. hkBool m_flips; // True if Det(M)<0.0f
  25. hkMatrix3 m_scaleAndSkew; // M = T*R * scaleAndSkew
  26. hkMatrix3 m_skew; // M = T*R*S * skew
  27. hkRotation m_basis; // Rotation matrix, may involve flipping
  28. // If m_flips=false, then m_basis == m_rotation
  29. };
  30. /// Decomposes M (4x4 matrix) as M == T * R * Scale * Skew == T * R * ScaleAndSkew
  31. /// Useful for conversion both to orthogonal (no skew) and orthonormal (no scale or skew) transforms
  32. void HK_CALL decomposeMatrix (const class hkMatrix4& matrixIn, Decomposition& decomposition);
  33. /// Decomposes M (3x3 matrix). Same as decomposeMatrix for Matrix4, but translation component is Zero
  34. void HK_CALL decomposeMatrix (const class hkMatrix3& matrixIn, Decomposition& decomposition);
  35. /// Decomposes M (4x4 matrix, column-major) as M == T * R * Scale * Skew == T * R * ScaleAndSkew
  36. /// Useful for conversion both to orthogonal (no skew) and orthonormal (no scale or skew) transforms
  37. void HK_CALL decompose4x4ColTransform (const hkReal* matrixIn, Decomposition& decomposition);
  38. /// Calculates the greatest common divisor of A and B (using the Euclidean algorithm)
  39. inline int HK_CALL calculateGcd (int a, int b);
  40. /// Calculates the least common multiple of A and B (using the Euclidean algorithm)
  41. inline int HK_CALL calculateLcm (int a, int b);
  42. }
  43. hkBool HK_CALL hkMathUtil::invert2x2Matrix(const hkReal* m, hkReal* out, hkReal tolerance)
  44. {
  45.     const hkReal *co0 = m;
  46.     const hkReal *co1 = co0 + 2;
  47.     hkReal D =  (co0[0] * co1[1]) - (co0[1] * co1[0]);
  48.     if(D*D < tolerance * tolerance){
  49.         return false;  // may be an ordinary result
  50.     }
  51.     hkReal DI = 1.0f/D;
  52.     hkReal a1 =  co1[1] * DI;
  53.     hkReal b1 = -co0[1] * DI;
  54.     hkReal a2 = -co1[0] * DI;
  55.     hkReal b2 =  co0[0] * DI;
  56.     hkReal *o_co0 = out;
  57.     hkReal *o_co1 = o_co0 + 2;
  58.     o_co0[0] = a1;
  59.     o_co1[0] = b1;
  60.     o_co0[1] = a2;
  61.     o_co1[1] = b2;
  62.     return true;
  63. }
  64. /// Calculates the greatest common divisor of A and B (using the Euclidean algorithm)
  65. inline int HK_CALL hkMathUtil::calculateGcd (int a, int b)
  66. {
  67. int t;
  68. while (b!=0)
  69. {
  70. t=b;
  71. b = a % b;
  72. a=t;
  73. }
  74. return a;
  75. }
  76. /// Calculates the least common multiple of A and B (using the Euclidean algorithm)
  77. inline int HK_CALL hkMathUtil::calculateLcm (int a, int b)
  78. {
  79. return (a*b) / calculateGcd (a,b);
  80. }
  81. #endif
  82. /*
  83. * Havok SDK - NO SOURCE PC DOWNLOAD, BUILD(#20090216)
  84. * Confidential Information of Havok.  (C) Copyright 1999-2009
  85. * Telekinesys Research Limited t/a Havok. All Rights Reserved. The Havok
  86. * Logo, and the Havok buzzsaw logo are trademarks of Havok.  Title, ownership
  87. * rights, and intellectual property rights in the Havok software remain in
  88. * Havok and/or its suppliers.
  89. * Use of this software for evaluation purposes is subject to and indicates
  90. * acceptance of the End User licence Agreement for this product. A copy of
  91. * the license is included with this software and is also available at www.havok.com/tryhavok.
  92. */