CmMatrix3.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef __Matrix3_H__
  25. #define __Matrix3_H__
  26. #include "CmPrerequisitesUtil.h"
  27. #include "CmVector3.h"
  28. // NB All code adapted from Wild Magic 0.2 Matrix math (free source code)
  29. // http://www.geometrictools.com/
  30. // NOTE. The (x,y,z) coordinate system is assumed to be right-handed.
  31. // Coordinate axis rotation matrices are of the form
  32. // RX = 1 0 0
  33. // 0 cos(t) -sin(t)
  34. // 0 sin(t) cos(t)
  35. // where t > 0 indicates a counterclockwise rotation in the yz-plane
  36. // RY = cos(t) 0 sin(t)
  37. // 0 1 0
  38. // -sin(t) 0 cos(t)
  39. // where t > 0 indicates a counterclockwise rotation in the zx-plane
  40. // RZ = cos(t) -sin(t) 0
  41. // sin(t) cos(t) 0
  42. // 0 0 1
  43. // where t > 0 indicates a counterclockwise rotation in the xy-plane.
  44. namespace CamelotFramework
  45. {
  46. /** \addtogroup Core
  47. * @{
  48. */
  49. /** \addtogroup Math
  50. * @{
  51. */
  52. /** A 3x3 matrix which can represent rotations around axes.
  53. @note
  54. <b>All the code is adapted from the Wild Magic 0.2 Matrix
  55. library (http://www.geometrictools.com/).</b>
  56. @par
  57. The coordinate system is assumed to be <b>right-handed</b>.
  58. */
  59. class CM_UTILITY_EXPORT Matrix3
  60. {
  61. public:
  62. /** Default constructor.
  63. @note
  64. It does <b>NOT</b> initialize the matrix for efficiency.
  65. */
  66. inline Matrix3 () {}
  67. inline explicit Matrix3 (const float arr[3][3])
  68. {
  69. memcpy(m,arr,9*sizeof(float));
  70. }
  71. inline Matrix3 (const Matrix3& rkMatrix)
  72. {
  73. memcpy(m,rkMatrix.m,9*sizeof(float));
  74. }
  75. Matrix3 (float fEntry00, float fEntry01, float fEntry02,
  76. float fEntry10, float fEntry11, float fEntry12,
  77. float fEntry20, float fEntry21, float fEntry22)
  78. {
  79. m[0][0] = fEntry00;
  80. m[0][1] = fEntry01;
  81. m[0][2] = fEntry02;
  82. m[1][0] = fEntry10;
  83. m[1][1] = fEntry11;
  84. m[1][2] = fEntry12;
  85. m[2][0] = fEntry20;
  86. m[2][1] = fEntry21;
  87. m[2][2] = fEntry22;
  88. }
  89. /** Exchange the contents of this matrix with another.
  90. */
  91. inline void swap(Matrix3& other)
  92. {
  93. std::swap(m[0][0], other.m[0][0]);
  94. std::swap(m[0][1], other.m[0][1]);
  95. std::swap(m[0][2], other.m[0][2]);
  96. std::swap(m[1][0], other.m[1][0]);
  97. std::swap(m[1][1], other.m[1][1]);
  98. std::swap(m[1][2], other.m[1][2]);
  99. std::swap(m[2][0], other.m[2][0]);
  100. std::swap(m[2][1], other.m[2][1]);
  101. std::swap(m[2][2], other.m[2][2]);
  102. }
  103. // member access, allows use of construct mat[r][c]
  104. inline float* operator[] (size_t iRow) const
  105. {
  106. return (float*)m[iRow];
  107. }
  108. /*inline operator float* ()
  109. {
  110. return (float*)m[0];
  111. }*/
  112. Vector3 GetColumn (size_t iCol) const;
  113. void SetColumn(size_t iCol, const Vector3& vec);
  114. void FromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
  115. // assignment and comparison
  116. inline Matrix3& operator= (const Matrix3& rkMatrix)
  117. {
  118. memcpy(m,rkMatrix.m,9*sizeof(float));
  119. return *this;
  120. }
  121. bool operator== (const Matrix3& rkMatrix) const;
  122. inline bool operator!= (const Matrix3& rkMatrix) const
  123. {
  124. return !operator==(rkMatrix);
  125. }
  126. // arithmetic operations
  127. Matrix3 operator+ (const Matrix3& rkMatrix) const;
  128. Matrix3 operator- (const Matrix3& rkMatrix) const;
  129. Matrix3 operator* (const Matrix3& rkMatrix) const;
  130. Matrix3 operator- () const;
  131. // matrix * vector [3x3 * 3x1 = 3x1]
  132. Vector3 operator* (const Vector3& rkVector) const;
  133. // vector * matrix [1x3 * 3x3 = 1x3]
  134. CM_UTILITY_EXPORT friend Vector3 operator* (const Vector3& rkVector,
  135. const Matrix3& rkMatrix);
  136. // matrix * scalar
  137. Matrix3 operator* (float fScalar) const;
  138. // scalar * matrix
  139. CM_UTILITY_EXPORT friend Matrix3 operator* (float fScalar, const Matrix3& rkMatrix);
  140. // utilities
  141. Matrix3 transpose () const;
  142. bool Inverse (Matrix3& rkInverse, float fTolerance = 1e-06) const;
  143. Matrix3 Inverse (float fTolerance = 1e-06) const;
  144. float Determinant () const;
  145. // singular value decomposition
  146. void SingularValueDecomposition (Matrix3& rkL, Vector3& rkS,
  147. Matrix3& rkR) const;
  148. void SingularValueComposition (const Matrix3& rkL,
  149. const Vector3& rkS, const Matrix3& rkR);
  150. // Gram-Schmidt orthonormalization (applied to columns of rotation matrix)
  151. void Orthonormalize ();
  152. // orthogonal Q, diagonal D, upper triangular U stored as (u01,u02,u12)
  153. void QDUDecomposition (Matrix3& rkQ, Vector3& rkD,
  154. Vector3& rkU) const;
  155. float SpectralNorm () const;
  156. // matrix must be orthonormal
  157. void ToAxisAngle (Vector3& rkAxis, Radian& rfAngle) const;
  158. inline void ToAxisAngle (Vector3& rkAxis, Degree& rfAngle) const {
  159. Radian r;
  160. ToAxisAngle ( rkAxis, r );
  161. rfAngle = r;
  162. }
  163. void FromAxisAngle (const Vector3& rkAxis, const Radian& fRadians);
  164. // The matrix must be orthonormal. The decomposition is yaw*pitch*roll
  165. // where yaw is rotation about the Up vector, pitch is rotation about the
  166. // Right axis, and roll is rotation about the Direction axis.
  167. bool ToEulerAnglesXYZ (Radian& rfYAngle, Radian& rfPAngle,
  168. Radian& rfRAngle) const;
  169. bool ToEulerAnglesXZY (Radian& rfYAngle, Radian& rfPAngle,
  170. Radian& rfRAngle) const;
  171. bool ToEulerAnglesYXZ (Radian& rfYAngle, Radian& rfPAngle,
  172. Radian& rfRAngle) const;
  173. bool ToEulerAnglesYZX (Radian& rfYAngle, Radian& rfPAngle,
  174. Radian& rfRAngle) const;
  175. bool ToEulerAnglesZXY (Radian& rfYAngle, Radian& rfPAngle,
  176. Radian& rfRAngle) const;
  177. bool ToEulerAnglesZYX (Radian& rfYAngle, Radian& rfPAngle,
  178. Radian& rfRAngle) const;
  179. void FromEulerAnglesXYZ (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
  180. void FromEulerAnglesXZY (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
  181. void FromEulerAnglesYXZ (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
  182. void FromEulerAnglesYZX (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
  183. void FromEulerAnglesZXY (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
  184. void FromEulerAnglesZYX (const Radian& fYAngle, const Radian& fPAngle, const Radian& fRAngle);
  185. // eigensolver, matrix must be symmetric
  186. void EigenSolveSymmetric (float afEigenvalue[3],
  187. Vector3 akEigenvector[3]) const;
  188. static void TensorProduct (const Vector3& rkU, const Vector3& rkV,
  189. Matrix3& rkProduct);
  190. /** Determines if this matrix involves a scaling. */
  191. inline bool hasScale() const
  192. {
  193. // check magnitude of column vectors (==local axes)
  194. float t = m[0][0] * m[0][0] + m[1][0] * m[1][0] + m[2][0] * m[2][0];
  195. if (!Math::RealEqual(t, 1.0, (float)1e-04))
  196. return true;
  197. t = m[0][1] * m[0][1] + m[1][1] * m[1][1] + m[2][1] * m[2][1];
  198. if (!Math::RealEqual(t, 1.0, (float)1e-04))
  199. return true;
  200. t = m[0][2] * m[0][2] + m[1][2] * m[1][2] + m[2][2] * m[2][2];
  201. if (!Math::RealEqual(t, 1.0, (float)1e-04))
  202. return true;
  203. return false;
  204. }
  205. /** Function for writing to a stream.
  206. */
  207. inline CM_UTILITY_EXPORT friend std::ostream& operator <<
  208. ( std::ostream& o, const Matrix3& mat )
  209. {
  210. o << "Matrix3(" << mat[0][0] << ", " << mat[0][1] << ", " << mat[0][2] << ", "
  211. << mat[1][0] << ", " << mat[1][1] << ", " << mat[1][2] << ", "
  212. << mat[2][0] << ", " << mat[2][1] << ", " << mat[2][2] << ")";
  213. return o;
  214. }
  215. static const float EPSILON;
  216. static const Matrix3 ZERO;
  217. static const Matrix3 IDENTITY;
  218. protected:
  219. // support for eigensolver
  220. void Tridiagonal (float afDiag[3], float afSubDiag[3]);
  221. bool QLAlgorithm (float afDiag[3], float afSubDiag[3]);
  222. // support for singular value decomposition
  223. static const float ms_fSvdEpsilon;
  224. static const unsigned int ms_iSvdMaxIterations;
  225. static void Bidiagonalize (Matrix3& kA, Matrix3& kL,
  226. Matrix3& kR);
  227. static void GolubKahanStep (Matrix3& kA, Matrix3& kL,
  228. Matrix3& kR);
  229. // support for spectral norm
  230. static float MaxCubicRoot (float afCoeff[3]);
  231. float m[3][3];
  232. // for faster access
  233. friend class Matrix4;
  234. };
  235. /** @} */
  236. /** @} */
  237. CM_ALLOW_MEMCPY_SERIALIZATION(Matrix3);
  238. }
  239. #endif