CmMatrix4.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. #pragma once
  25. #include "CmPrerequisitesUtil.h"
  26. #include "CmVector3.h"
  27. #include "CmMatrix3.h"
  28. #include "CmVector4.h"
  29. #include "CmPlane.h"
  30. namespace CamelotFramework
  31. {
  32. class CM_UTILITY_EXPORT Matrix4
  33. {
  34. private:
  35. union
  36. {
  37. float m[4][4];
  38. float _m[16];
  39. };
  40. public:
  41. Matrix4()
  42. { }
  43. Matrix4(
  44. float m00, float m01, float m02, float m03,
  45. float m10, float m11, float m12, float m13,
  46. float m20, float m21, float m22, float m23,
  47. float m30, float m31, float m32, float m33)
  48. {
  49. m[0][0] = m00;
  50. m[0][1] = m01;
  51. m[0][2] = m02;
  52. m[0][3] = m03;
  53. m[1][0] = m10;
  54. m[1][1] = m11;
  55. m[1][2] = m12;
  56. m[1][3] = m13;
  57. m[2][0] = m20;
  58. m[2][1] = m21;
  59. m[2][2] = m22;
  60. m[2][3] = m23;
  61. m[3][0] = m30;
  62. m[3][1] = m31;
  63. m[3][2] = m32;
  64. m[3][3] = m33;
  65. }
  66. Matrix4(const Matrix4& mat)
  67. {
  68. memcpy(_m, mat._m, 16*sizeof(float));
  69. }
  70. /**
  71. * @brief Creates a 4x4 transformation matrix with a zero translation part from a rotation/scaling 3x3 matrix.
  72. */
  73. explicit Matrix4(const Matrix3& mat3)
  74. {
  75. m[0][0] = mat3.m[0][0]; m[0][1] = mat3.m[0][1]; m[0][2] = mat3.m[0][2]; m[0][3] = 0.0f;
  76. m[1][0] = mat3.m[1][0]; m[1][1] = mat3.m[1][1]; m[1][2] = mat3.m[1][2]; m[1][3] = 0.0f;
  77. m[2][0] = mat3.m[2][0]; m[2][1] = mat3.m[2][1]; m[2][2] = mat3.m[2][2]; m[2][3] = 0.0f;
  78. m[3][0] = 0.0f; m[3][1] = 0.0f; m[3][2] = 0.0f; m[3][3] = 1.0f;
  79. }
  80. /**
  81. * @brief Creates a 4x4 transformation matrix with translation, rotation and scale.
  82. */
  83. Matrix4(const Vector3& translation, const Quaternion& rot, const Vector3& scale)
  84. {
  85. setTRS(translation, rot, scale);
  86. }
  87. void swap(Matrix4& other)
  88. {
  89. std::swap(m[0][0], other.m[0][0]);
  90. std::swap(m[0][1], other.m[0][1]);
  91. std::swap(m[0][2], other.m[0][2]);
  92. std::swap(m[0][3], other.m[0][3]);
  93. std::swap(m[1][0], other.m[1][0]);
  94. std::swap(m[1][1], other.m[1][1]);
  95. std::swap(m[1][2], other.m[1][2]);
  96. std::swap(m[1][3], other.m[1][3]);
  97. std::swap(m[2][0], other.m[2][0]);
  98. std::swap(m[2][1], other.m[2][1]);
  99. std::swap(m[2][2], other.m[2][2]);
  100. std::swap(m[2][3], other.m[2][3]);
  101. std::swap(m[3][0], other.m[3][0]);
  102. std::swap(m[3][1], other.m[3][1]);
  103. std::swap(m[3][2], other.m[3][2]);
  104. std::swap(m[3][3], other.m[3][3]);
  105. }
  106. float* operator[] (size_t row)
  107. {
  108. assert(row < 4);
  109. return m[row];
  110. }
  111. const float *operator[] (size_t row) const
  112. {
  113. assert(row < 4);
  114. return m[row];
  115. }
  116. Matrix4 operator* (const Matrix4 &rhs) const
  117. {
  118. Matrix4 r;
  119. r.m[0][0] = m[0][0] * rhs.m[0][0] + m[0][1] * rhs.m[1][0] + m[0][2] * rhs.m[2][0] + m[0][3] * rhs.m[3][0];
  120. r.m[0][1] = m[0][0] * rhs.m[0][1] + m[0][1] * rhs.m[1][1] + m[0][2] * rhs.m[2][1] + m[0][3] * rhs.m[3][1];
  121. r.m[0][2] = m[0][0] * rhs.m[0][2] + m[0][1] * rhs.m[1][2] + m[0][2] * rhs.m[2][2] + m[0][3] * rhs.m[3][2];
  122. r.m[0][3] = m[0][0] * rhs.m[0][3] + m[0][1] * rhs.m[1][3] + m[0][2] * rhs.m[2][3] + m[0][3] * rhs.m[3][3];
  123. r.m[1][0] = m[1][0] * rhs.m[0][0] + m[1][1] * rhs.m[1][0] + m[1][2] * rhs.m[2][0] + m[1][3] * rhs.m[3][0];
  124. r.m[1][1] = m[1][0] * rhs.m[0][1] + m[1][1] * rhs.m[1][1] + m[1][2] * rhs.m[2][1] + m[1][3] * rhs.m[3][1];
  125. r.m[1][2] = m[1][0] * rhs.m[0][2] + m[1][1] * rhs.m[1][2] + m[1][2] * rhs.m[2][2] + m[1][3] * rhs.m[3][2];
  126. r.m[1][3] = m[1][0] * rhs.m[0][3] + m[1][1] * rhs.m[1][3] + m[1][2] * rhs.m[2][3] + m[1][3] * rhs.m[3][3];
  127. r.m[2][0] = m[2][0] * rhs.m[0][0] + m[2][1] * rhs.m[1][0] + m[2][2] * rhs.m[2][0] + m[2][3] * rhs.m[3][0];
  128. r.m[2][1] = m[2][0] * rhs.m[0][1] + m[2][1] * rhs.m[1][1] + m[2][2] * rhs.m[2][1] + m[2][3] * rhs.m[3][1];
  129. r.m[2][2] = m[2][0] * rhs.m[0][2] + m[2][1] * rhs.m[1][2] + m[2][2] * rhs.m[2][2] + m[2][3] * rhs.m[3][2];
  130. r.m[2][3] = m[2][0] * rhs.m[0][3] + m[2][1] * rhs.m[1][3] + m[2][2] * rhs.m[2][3] + m[2][3] * rhs.m[3][3];
  131. r.m[3][0] = m[3][0] * rhs.m[0][0] + m[3][1] * rhs.m[1][0] + m[3][2] * rhs.m[2][0] + m[3][3] * rhs.m[3][0];
  132. r.m[3][1] = m[3][0] * rhs.m[0][1] + m[3][1] * rhs.m[1][1] + m[3][2] * rhs.m[2][1] + m[3][3] * rhs.m[3][1];
  133. r.m[3][2] = m[3][0] * rhs.m[0][2] + m[3][1] * rhs.m[1][2] + m[3][2] * rhs.m[2][2] + m[3][3] * rhs.m[3][2];
  134. r.m[3][3] = m[3][0] * rhs.m[0][3] + m[3][1] * rhs.m[1][3] + m[3][2] * rhs.m[2][3] + m[3][3] * rhs.m[3][3];
  135. return r;
  136. }
  137. Matrix4 operator+ (const Matrix4 &rhs) const
  138. {
  139. Matrix4 r;
  140. r.m[0][0] = m[0][0] + rhs.m[0][0];
  141. r.m[0][1] = m[0][1] + rhs.m[0][1];
  142. r.m[0][2] = m[0][2] + rhs.m[0][2];
  143. r.m[0][3] = m[0][3] + rhs.m[0][3];
  144. r.m[1][0] = m[1][0] + rhs.m[1][0];
  145. r.m[1][1] = m[1][1] + rhs.m[1][1];
  146. r.m[1][2] = m[1][2] + rhs.m[1][2];
  147. r.m[1][3] = m[1][3] + rhs.m[1][3];
  148. r.m[2][0] = m[2][0] + rhs.m[2][0];
  149. r.m[2][1] = m[2][1] + rhs.m[2][1];
  150. r.m[2][2] = m[2][2] + rhs.m[2][2];
  151. r.m[2][3] = m[2][3] + rhs.m[2][3];
  152. r.m[3][0] = m[3][0] + rhs.m[3][0];
  153. r.m[3][1] = m[3][1] + rhs.m[3][1];
  154. r.m[3][2] = m[3][2] + rhs.m[3][2];
  155. r.m[3][3] = m[3][3] + rhs.m[3][3];
  156. return r;
  157. }
  158. Matrix4 operator- (const Matrix4 &rhs) const
  159. {
  160. Matrix4 r;
  161. r.m[0][0] = m[0][0] - rhs.m[0][0];
  162. r.m[0][1] = m[0][1] - rhs.m[0][1];
  163. r.m[0][2] = m[0][2] - rhs.m[0][2];
  164. r.m[0][3] = m[0][3] - rhs.m[0][3];
  165. r.m[1][0] = m[1][0] - rhs.m[1][0];
  166. r.m[1][1] = m[1][1] - rhs.m[1][1];
  167. r.m[1][2] = m[1][2] - rhs.m[1][2];
  168. r.m[1][3] = m[1][3] - rhs.m[1][3];
  169. r.m[2][0] = m[2][0] - rhs.m[2][0];
  170. r.m[2][1] = m[2][1] - rhs.m[2][1];
  171. r.m[2][2] = m[2][2] - rhs.m[2][2];
  172. r.m[2][3] = m[2][3] - rhs.m[2][3];
  173. r.m[3][0] = m[3][0] - rhs.m[3][0];
  174. r.m[3][1] = m[3][1] - rhs.m[3][1];
  175. r.m[3][2] = m[3][2] - rhs.m[3][2];
  176. r.m[3][3] = m[3][3] - rhs.m[3][3];
  177. return r;
  178. }
  179. inline bool operator== (const Matrix4& rhs ) const
  180. {
  181. if(
  182. m[0][0] != rhs.m[0][0] || m[0][1] != rhs.m[0][1] || m[0][2] != rhs.m[0][2] || m[0][3] != rhs.m[0][3] ||
  183. m[1][0] != rhs.m[1][0] || m[1][1] != rhs.m[1][1] || m[1][2] != rhs.m[1][2] || m[1][3] != rhs.m[1][3] ||
  184. m[2][0] != rhs.m[2][0] || m[2][1] != rhs.m[2][1] || m[2][2] != rhs.m[2][2] || m[2][3] != rhs.m[2][3] ||
  185. m[3][0] != rhs.m[3][0] || m[3][1] != rhs.m[3][1] || m[3][2] != rhs.m[3][2] || m[3][3] != rhs.m[3][3] )
  186. return false;
  187. return true;
  188. }
  189. inline bool operator!= (const Matrix4& rhs) const
  190. {
  191. return !operator==(rhs);
  192. }
  193. Matrix4 operator*(float rhs) const
  194. {
  195. return Matrix4(
  196. rhs*m[0][0], rhs*m[0][1], rhs*m[0][2], rhs*m[0][3],
  197. rhs*m[1][0], rhs*m[1][1], rhs*m[1][2], rhs*m[1][3],
  198. rhs*m[2][0], rhs*m[2][1], rhs*m[2][2], rhs*m[2][3],
  199. rhs*m[3][0], rhs*m[3][1], rhs*m[3][2], rhs*m[3][3]);
  200. }
  201. Matrix4 transpose() const
  202. {
  203. return Matrix4(m[0][0], m[1][0], m[2][0], m[3][0],
  204. m[0][1], m[1][1], m[2][1], m[3][1],
  205. m[0][2], m[1][2], m[2][2], m[3][2],
  206. m[0][3], m[1][3], m[2][3], m[3][3]);
  207. }
  208. /**
  209. * @brief Extracts the rotation/scaling part of the Matrix as a 3x3 matrix.
  210. */
  211. void extract3x3Matrix(Matrix3& m3x3) const
  212. {
  213. m3x3.m[0][0] = m[0][0];
  214. m3x3.m[0][1] = m[0][1];
  215. m3x3.m[0][2] = m[0][2];
  216. m3x3.m[1][0] = m[1][0];
  217. m3x3.m[1][1] = m[1][1];
  218. m3x3.m[1][2] = m[1][2];
  219. m3x3.m[2][0] = m[2][0];
  220. m3x3.m[2][1] = m[2][1];
  221. m3x3.m[2][2] = m[2][2];
  222. }
  223. Matrix4 adjoint() const;
  224. float determinant() const;
  225. Matrix4 inverse() const;
  226. /**
  227. * @brief Creates a matrix from translation, rotation and scale.
  228. *
  229. * @note The transformation are applied in scale->rotation->translation order.
  230. */
  231. void setTRS(const Vector3& translation, const Quaternion& rotation, const Vector3& scale);
  232. /**
  233. * @brief Creates a matrix from inverse translation, rotation and scale.
  234. *
  235. * @note This is cheaper than "setTRS" and then performing "inverse".
  236. */
  237. void setInverseTRS(const Vector3& translation, const Quaternion& rotation, const Vector3& scale);
  238. /**
  239. * @brief Decompose a Matrix4 to translation, rotation and scale.
  240. *
  241. * @note Matrix must consist only of translation, rotation and uniform scale transformations,
  242. * otherwise accurate results are not guaranteed. Applying non-uniform scale guarantees
  243. * results will not be accurate.
  244. */
  245. void decomposition(Vector3& position, Quaternion& rotation, Vector3& scale) const;
  246. /**
  247. * @brief Check whether or not the matrix is affine matrix.
  248. *
  249. * @note An affine matrix is a 4x4 matrix with row 3 equal to (0, 0, 0, 1),
  250. * i.e. no projective coefficients.
  251. */
  252. bool isAffine() const
  253. {
  254. return m[3][0] == 0 && m[3][1] == 0 && m[3][2] == 0 && m[3][3] == 1;
  255. }
  256. /**
  257. * @brief Returns the inverse of the affine matrix.
  258. *
  259. * @note Matrix must be affine.
  260. */
  261. Matrix4 inverseAffine(void) const;
  262. /**
  263. * @brief Concatenate two affine matrices.
  264. *
  265. * @note Both matrices must be affine.
  266. */
  267. Matrix4 concatenateAffine(const Matrix4 &other) const
  268. {
  269. assert(isAffine() && other.isAffine());
  270. return Matrix4(
  271. m[0][0] * other.m[0][0] + m[0][1] * other.m[1][0] + m[0][2] * other.m[2][0],
  272. m[0][0] * other.m[0][1] + m[0][1] * other.m[1][1] + m[0][2] * other.m[2][1],
  273. m[0][0] * other.m[0][2] + m[0][1] * other.m[1][2] + m[0][2] * other.m[2][2],
  274. m[0][0] * other.m[0][3] + m[0][1] * other.m[1][3] + m[0][2] * other.m[2][3] + m[0][3],
  275. m[1][0] * other.m[0][0] + m[1][1] * other.m[1][0] + m[1][2] * other.m[2][0],
  276. m[1][0] * other.m[0][1] + m[1][1] * other.m[1][1] + m[1][2] * other.m[2][1],
  277. m[1][0] * other.m[0][2] + m[1][1] * other.m[1][2] + m[1][2] * other.m[2][2],
  278. m[1][0] * other.m[0][3] + m[1][1] * other.m[1][3] + m[1][2] * other.m[2][3] + m[1][3],
  279. m[2][0] * other.m[0][0] + m[2][1] * other.m[1][0] + m[2][2] * other.m[2][0],
  280. m[2][0] * other.m[0][1] + m[2][1] * other.m[1][1] + m[2][2] * other.m[2][1],
  281. m[2][0] * other.m[0][2] + m[2][1] * other.m[1][2] + m[2][2] * other.m[2][2],
  282. m[2][0] * other.m[0][3] + m[2][1] * other.m[1][3] + m[2][2] * other.m[2][3] + m[2][3],
  283. 0, 0, 0, 1);
  284. }
  285. /**
  286. * @brief Transform a 3D vector by this matrix.
  287. *
  288. * @note Matrix must be affine, if it is not use "multiply" method.
  289. */
  290. Vector3 multiply3x4(const Vector3& v) const
  291. {
  292. return Vector3(
  293. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3],
  294. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3],
  295. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3]);
  296. }
  297. /**
  298. * @brief Transform a 4D vector by this matrix.
  299. *
  300. * @note Matrix must be affine, if it is not use "multiply" method.
  301. */
  302. Vector4 multiply3x4(const Vector4& v) const
  303. {
  304. return Vector4(
  305. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w,
  306. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w,
  307. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w,
  308. v.w);
  309. }
  310. /**
  311. * @brief Transform a 3D vector by this matrix.
  312. *
  313. * @note w component of the vector is assumed to be 1. After transformation all components
  314. * are projected back so that w remains 1.
  315. *
  316. * If your matrix doesn't contain projection components use "multiply3x4" method as it is faster.
  317. */
  318. Vector3 multiply(const Vector3 &v) const
  319. {
  320. Vector3 r;
  321. float fInvW = 1.0f / (m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3]);
  322. r.x = (m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3]) * fInvW;
  323. r.y = (m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3]) * fInvW;
  324. r.z = (m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3]) * fInvW;
  325. return r;
  326. }
  327. /**
  328. * @brief Transform a 3D vector by this matrix.
  329. *
  330. * @note After transformation all components are projected back so that w remains 1.
  331. *
  332. * If your matrix doesn't contain projection components use "multiply3x4" method as it is faster.
  333. */
  334. Vector4 multiply(const Vector4& v) const
  335. {
  336. return Vector4(
  337. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w,
  338. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w,
  339. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w,
  340. m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w
  341. );
  342. }
  343. /**
  344. * @brief Creates a view matrix and applies optional reflection.
  345. */
  346. void makeView(const Vector3& position, const Quaternion& orientation, const Matrix4* reflectMatrix = nullptr);
  347. static const Matrix4 ZERO;
  348. static const Matrix4 IDENTITY;
  349. };
  350. CM_ALLOW_MEMCPY_SERIALIZATION(Matrix4);
  351. }