BsMatrix3.h 11 KB

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