BsMatrix3.h 11 KB

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