BsMatrix3.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. #include "BsVector3.h"
  4. namespace BansheeEngine
  5. {
  6. /** @addtogroup Math
  7. * @{
  8. */
  9. /** A 3x3 matrix. Can be used for non-homogenous transformations of three dimensional vectors and points. */
  10. class BS_UTILITY_EXPORT Matrix3
  11. {
  12. private:
  13. struct EulerAngleOrderData
  14. {
  15. int a, b, c;
  16. float sign;
  17. };
  18. public:
  19. Matrix3() {}
  20. Matrix3(const Matrix3& mat)
  21. {
  22. memcpy(m, mat.m, 9*sizeof(float));
  23. }
  24. Matrix3(float m00, float m01, float m02,
  25. float m10, float m11, float m12,
  26. float m20, float m21, float m22)
  27. {
  28. m[0][0] = m00;
  29. m[0][1] = m01;
  30. m[0][2] = m02;
  31. m[1][0] = m10;
  32. m[1][1] = m11;
  33. m[1][2] = m12;
  34. m[2][0] = m20;
  35. m[2][1] = m21;
  36. m[2][2] = m22;
  37. }
  38. /** Construct a matrix from a quaternion. */
  39. explicit Matrix3(const Quaternion& rotation)
  40. {
  41. fromQuaternion(rotation);
  42. }
  43. /** Construct a matrix that performs rotation and scale. */
  44. explicit Matrix3(const Quaternion& rotation, const Vector3& scale)
  45. {
  46. fromQuaternion(rotation);
  47. for (int row = 0; row < 3; row++)
  48. {
  49. for (int col = 0; col < 3; col++)
  50. m[row][col] = scale[row]*m[row][col];
  51. }
  52. }
  53. /** Construct a matrix from an angle/axis pair. */
  54. explicit Matrix3(const Vector3& axis, const Radian& angle)
  55. {
  56. fromAxisAngle(axis, angle);
  57. }
  58. /** Construct a matrix from 3 orthonormal local axes. */
  59. explicit Matrix3(const Vector3& xaxis, const Vector3& yaxis, const Vector3& zaxis)
  60. {
  61. fromAxes(xaxis, yaxis, zaxis);
  62. }
  63. /**
  64. * Construct a matrix from euler angles, YXZ ordering.
  65. *
  66. * @see Matrix3::fromEulerAngles
  67. */
  68. explicit Matrix3(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle)
  69. {
  70. fromEulerAngles(xAngle, yAngle, zAngle);
  71. }
  72. /**
  73. * Construct a matrix from euler angles, custom ordering.
  74. *
  75. * @see Matrix3::fromEulerAngles
  76. */
  77. explicit Matrix3(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order)
  78. {
  79. fromEulerAngles(xAngle, yAngle, zAngle, order);
  80. }
  81. /** Swaps the contents of this matrix with another. */
  82. void swap(Matrix3& other)
  83. {
  84. std::swap(m[0][0], other.m[0][0]);
  85. std::swap(m[0][1], other.m[0][1]);
  86. std::swap(m[0][2], other.m[0][2]);
  87. std::swap(m[1][0], other.m[1][0]);
  88. std::swap(m[1][1], other.m[1][1]);
  89. std::swap(m[1][2], other.m[1][2]);
  90. std::swap(m[2][0], other.m[2][0]);
  91. std::swap(m[2][1], other.m[2][1]);
  92. std::swap(m[2][2], other.m[2][2]);
  93. }
  94. /** Returns a row of the matrix. */
  95. float* operator[] (UINT32 row) const
  96. {
  97. assert(row < 3);
  98. return (float*)m[row];
  99. }
  100. Vector3 getColumn(UINT32 col) const;
  101. void setColumn(UINT32 col, const Vector3& vec);
  102. Matrix3& operator= (const Matrix3& rhs)
  103. {
  104. memcpy(m, rhs.m, 9*sizeof(float));
  105. return *this;
  106. }
  107. bool operator== (const Matrix3& rhs) const;
  108. bool operator!= (const Matrix3& rhs) const;
  109. Matrix3 operator+ (const Matrix3& rhs) const;
  110. Matrix3 operator- (const Matrix3& rhs) const;
  111. Matrix3 operator* (const Matrix3& rhs) const;
  112. Matrix3 operator- () const;
  113. Matrix3 operator* (float rhs) const;
  114. friend Matrix3 operator* (float lhs, const Matrix3& rhs);
  115. /** Transforms the given vector by this matrix and returns the newly transformed vector. */
  116. Vector3 transform(const Vector3& vec) const;
  117. /** Returns a transpose of the matrix (switched columns and rows). */
  118. Matrix3 transpose () const;
  119. /**
  120. * Calculates an inverse of the matrix if it exists.
  121. *
  122. * @param[out] mat Resulting matrix inverse.
  123. * @param[in] fTolerance (optional) Tolerance to use when checking if determinant is zero (or near zero in this case).
  124. * Zero determinant means inverse doesn't exist.
  125. * @return True if inverse exists, false otherwise.
  126. */
  127. bool inverse(Matrix3& mat, float fTolerance = 1e-06f) const;
  128. /**
  129. * Calculates an inverse of the matrix if it exists.
  130. *
  131. * @param[in] fTolerance (optional) Tolerance to use when checking if determinant is zero (or near zero in this case).
  132. * Zero determinant means inverse doesn't exist.
  133. *
  134. * @return Resulting matrix inverse if it exists, otherwise a zero matrix.
  135. */
  136. Matrix3 inverse(float fTolerance = 1e-06f) const;
  137. /** Calculates the matrix determinant. */
  138. float determinant() const;
  139. /**
  140. * Decompose a Matrix3 to rotation and scale.
  141. *
  142. * @note
  143. * Matrix must consist only of rotation and uniform scale transformations, otherwise accurate results are not
  144. * guaranteed. Applying non-uniform scale guarantees rotation portion will not be accurate.
  145. */
  146. void decomposition(Quaternion& rotation, Vector3& scale) const;
  147. /**
  148. * Decomposes the matrix into various useful values.
  149. *
  150. * @param[out] matL Unitary matrix. Columns form orthonormal bases. If your matrix is affine and
  151. * doesn't use non-uniform scaling this matrix will be a conjugate transpose of the rotation part of the matrix.
  152. * @param[out] matS Singular values of the matrix. If your matrix is affine these will be scaling factors of the matrix.
  153. * @param[out] matR Unitary matrix. Columns form orthonormal bases. If your matrix is affine and
  154. * doesn't use non-uniform scaling this matrix will be the rotation part of the matrix.
  155. */
  156. void singularValueDecomposition(Matrix3& matL, Vector3& matS, Matrix3& matR) const;
  157. /**
  158. * Decomposes the matrix into a set of values.
  159. *
  160. * @param[out] matQ Columns form orthonormal bases. If your matrix is affine and
  161. * doesn't use non-uniform scaling this matrix will be the rotation part of the matrix.
  162. * @param[out] vecD If the matrix is affine these will be scaling factors of the matrix.
  163. * @param[out] vecU If the matrix is affine these will be shear factors of the matrix.
  164. */
  165. void QDUDecomposition(Matrix3& matQ, Vector3& vecD, Vector3& vecU) const;
  166. /** Gram-Schmidt orthonormalization (applied to columns of rotation matrix) */
  167. void orthonormalize();
  168. /**
  169. * Converts an orthonormal matrix to axis angle representation.
  170. *
  171. * @note Matrix must be orthonormal.
  172. */
  173. void toAxisAngle(Vector3& axis, Radian& angle) const;
  174. /** Creates a rotation matrix from an axis angle representation. */
  175. void fromAxisAngle(const Vector3& axis, const Radian& angle);
  176. /**
  177. * Converts an orthonormal matrix to quaternion representation.
  178. *
  179. * @note Matrix must be orthonormal.
  180. */
  181. void toQuaternion(Quaternion& quat) const;
  182. /** Creates a rotation matrix from a quaternion representation. */
  183. void fromQuaternion(const Quaternion& quat);
  184. /** Creates a matrix from a three axes. */
  185. void fromAxes(const Vector3& xAxis, const Vector3& yAxis, const Vector3& zAxis);
  186. /**
  187. * Converts an orthonormal matrix to euler angle (pitch/yaw/roll) representation.
  188. *
  189. * @param[in,out] xAngle Rotation about x axis. (AKA Pitch)
  190. * @param[in,out] yAngle Rotation about y axis. (AKA Yaw)
  191. * @param[in,out] zAngle Rotation about z axis. (AKA Roll)
  192. * @return True if unique solution was found, false otherwise.
  193. *
  194. * @note Matrix must be orthonormal.
  195. */
  196. bool toEulerAngles(Radian& xAngle, Radian& yAngle, Radian& zAngle) const;
  197. /**
  198. * Creates a rotation matrix from the provided Pitch/Yaw/Roll angles.
  199. *
  200. * @param[in] xAngle Rotation about x axis. (AKA Pitch)
  201. * @param[in] yAngle Rotation about y axis. (AKA Yaw)
  202. * @param[in] zAngle Rotation about z axis. (AKA Roll)
  203. *
  204. * @note Matrix must be orthonormal.
  205. * Since different values will be produced depending in which order are the rotations applied, this method assumes
  206. * they are applied in YXZ order. If you need a specific order, use the overloaded "fromEulerAngles" method instead.
  207. */
  208. void fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle);
  209. /**
  210. * Creates a rotation matrix from the provided Pitch/Yaw/Roll angles.
  211. *
  212. * @param[in] xAngle Rotation about x axis. (AKA Pitch)
  213. * @param[in] yAngle Rotation about y axis. (AKA Yaw)
  214. * @param[in] zAngle Rotation about z axis. (AKA Roll)
  215. * @param[in] order The order in which rotations will be applied.
  216. * Different rotations can be created depending on the order.
  217. *
  218. * @note Matrix must be orthonormal.
  219. */
  220. void fromEulerAngles(const Radian& xAngle, const Radian& yAngle, const Radian& zAngle, EulerAngleOrder order);
  221. /**
  222. * Eigensolver, matrix must be symmetric.
  223. *
  224. * @note
  225. * Eigenvectors are vectors which when transformed by the matrix, only change in magnitude, but not in direction.
  226. * Eigenvalue is that magnitude. In other words you will get the same result whether you multiply the vector by the
  227. * matrix or by its eigenvalue.
  228. */
  229. void eigenSolveSymmetric(float eigenValues[3], Vector3 eigenVectors[3]) const;
  230. static const float EPSILON;
  231. static const Matrix3 ZERO;
  232. static const Matrix3 IDENTITY;
  233. protected:
  234. friend class Matrix4;
  235. // Support for eigensolver
  236. void tridiagonal (float diag[3], float subDiag[3]);
  237. bool QLAlgorithm (float diag[3], float subDiag[3]);
  238. // Support for singular value decomposition
  239. static const float SVD_EPSILON;
  240. static const unsigned int SVD_MAX_ITERS;
  241. static void bidiagonalize (Matrix3& matA, Matrix3& matL, Matrix3& matR);
  242. static void golubKahanStep (Matrix3& matA, Matrix3& matL, Matrix3& matR);
  243. // Euler angle conversions
  244. static const EulerAngleOrderData EA_LOOKUP[6];
  245. float m[3][3];
  246. };
  247. /** @} */
  248. /** @cond SPECIALIZATIONS */
  249. BS_ALLOW_MEMCPY_SERIALIZATION(Matrix3);
  250. /** @endcond */
  251. }