CmMatrix4.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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 __Matrix4__
  25. #define __Matrix4__
  26. // Precompiler options
  27. #include "CmPrerequisitesUtil.h"
  28. #include "CmVector3.h"
  29. #include "CmMatrix3.h"
  30. #include "CmVector4.h"
  31. #include "CmPlane.h"
  32. namespace CamelotFramework
  33. {
  34. /** \addtogroup Core
  35. * @{
  36. */
  37. /** \addtogroup Math
  38. * @{
  39. */
  40. /** Class encapsulating a standard 4x4 homogeneous matrix.
  41. @remarks
  42. We use column vectors when applying matrix multiplications,
  43. This means a vector is represented as a single column, 4-row
  44. matrix. This has the effect that the transformations implemented
  45. by the matrices happens right-to-left e.g. if vector V is to be
  46. transformed by M1 then M2 then M3, the calculation would be
  47. M3 * M2 * M1 * V. The order that matrices are concatenated is
  48. vital since matrix multiplication is not commutative, i.e. you
  49. can get a different result if you concatenate in the wrong order.
  50. @par
  51. The use of column vectors and right-to-left ordering is the
  52. standard in most mathematical texts, and is the same as used in
  53. OpenGL. It is, however, the opposite of Direct3D, which has
  54. inexplicably chosen to differ from the accepted standard and uses
  55. row vectors and left-to-right matrix multiplication.
  56. @par
  57. We deal with the differences between D3D and OpenGL etc.
  58. internally when operating through different render systems.
  59. Users only need to conform to standard maths conventions, i.e.
  60. right-to-left matrix multiplication, (Engine transposes matrices it
  61. passes to D3D to compensate).
  62. @par
  63. The generic form M * V which shows the layout of the matrix
  64. entries is shown below:
  65. <pre>
  66. [ m[0][0] m[0][1] m[0][2] m[0][3] ] {x}
  67. | m[1][0] m[1][1] m[1][2] m[1][3] | * {y}
  68. | m[2][0] m[2][1] m[2][2] m[2][3] | {z}
  69. [ m[3][0] m[3][1] m[3][2] m[3][3] ] {1}
  70. </pre>
  71. */
  72. class CM_UTILITY_EXPORT Matrix4
  73. {
  74. protected:
  75. /// The matrix entries, indexed by [row][col].
  76. union {
  77. float m[4][4];
  78. float _m[16];
  79. };
  80. public:
  81. /** Default constructor.
  82. @note
  83. It does <b>NOT</b> initialize the matrix for efficiency.
  84. */
  85. inline Matrix4()
  86. {
  87. }
  88. inline Matrix4(
  89. float m00, float m01, float m02, float m03,
  90. float m10, float m11, float m12, float m13,
  91. float m20, float m21, float m22, float m23,
  92. float m30, float m31, float m32, float m33 )
  93. {
  94. m[0][0] = m00;
  95. m[0][1] = m01;
  96. m[0][2] = m02;
  97. m[0][3] = m03;
  98. m[1][0] = m10;
  99. m[1][1] = m11;
  100. m[1][2] = m12;
  101. m[1][3] = m13;
  102. m[2][0] = m20;
  103. m[2][1] = m21;
  104. m[2][2] = m22;
  105. m[2][3] = m23;
  106. m[3][0] = m30;
  107. m[3][1] = m31;
  108. m[3][2] = m32;
  109. m[3][3] = m33;
  110. }
  111. /** Creates a standard 4x4 transformation matrix with a zero translation part from a rotation/scaling 3x3 matrix.
  112. */
  113. inline Matrix4(const Matrix3& m3x3)
  114. {
  115. operator=(IDENTITY);
  116. operator=(m3x3);
  117. }
  118. /** Creates a standard 4x4 transformation matrix with a zero translation part from a rotation/scaling Quaternion.
  119. */
  120. inline Matrix4(const Quaternion& rot)
  121. {
  122. Matrix3 m3x3;
  123. rot.ToRotationMatrix(m3x3);
  124. operator=(IDENTITY);
  125. operator=(m3x3);
  126. }
  127. /** Exchange the contents of this matrix with another.
  128. */
  129. inline void swap(Matrix4& other)
  130. {
  131. std::swap(m[0][0], other.m[0][0]);
  132. std::swap(m[0][1], other.m[0][1]);
  133. std::swap(m[0][2], other.m[0][2]);
  134. std::swap(m[0][3], other.m[0][3]);
  135. std::swap(m[1][0], other.m[1][0]);
  136. std::swap(m[1][1], other.m[1][1]);
  137. std::swap(m[1][2], other.m[1][2]);
  138. std::swap(m[1][3], other.m[1][3]);
  139. std::swap(m[2][0], other.m[2][0]);
  140. std::swap(m[2][1], other.m[2][1]);
  141. std::swap(m[2][2], other.m[2][2]);
  142. std::swap(m[2][3], other.m[2][3]);
  143. std::swap(m[3][0], other.m[3][0]);
  144. std::swap(m[3][1], other.m[3][1]);
  145. std::swap(m[3][2], other.m[3][2]);
  146. std::swap(m[3][3], other.m[3][3]);
  147. }
  148. inline float* operator [] ( size_t iRow )
  149. {
  150. assert( iRow < 4 );
  151. return m[iRow];
  152. }
  153. inline const float *operator [] ( size_t iRow ) const
  154. {
  155. assert( iRow < 4 );
  156. return m[iRow];
  157. }
  158. inline Matrix4 concatenate(const Matrix4 &m2) const
  159. {
  160. Matrix4 r;
  161. r.m[0][0] = m[0][0] * m2.m[0][0] + m[0][1] * m2.m[1][0] + m[0][2] * m2.m[2][0] + m[0][3] * m2.m[3][0];
  162. r.m[0][1] = m[0][0] * m2.m[0][1] + m[0][1] * m2.m[1][1] + m[0][2] * m2.m[2][1] + m[0][3] * m2.m[3][1];
  163. r.m[0][2] = m[0][0] * m2.m[0][2] + m[0][1] * m2.m[1][2] + m[0][2] * m2.m[2][2] + m[0][3] * m2.m[3][2];
  164. r.m[0][3] = m[0][0] * m2.m[0][3] + m[0][1] * m2.m[1][3] + m[0][2] * m2.m[2][3] + m[0][3] * m2.m[3][3];
  165. r.m[1][0] = m[1][0] * m2.m[0][0] + m[1][1] * m2.m[1][0] + m[1][2] * m2.m[2][0] + m[1][3] * m2.m[3][0];
  166. r.m[1][1] = m[1][0] * m2.m[0][1] + m[1][1] * m2.m[1][1] + m[1][2] * m2.m[2][1] + m[1][3] * m2.m[3][1];
  167. r.m[1][2] = m[1][0] * m2.m[0][2] + m[1][1] * m2.m[1][2] + m[1][2] * m2.m[2][2] + m[1][3] * m2.m[3][2];
  168. r.m[1][3] = m[1][0] * m2.m[0][3] + m[1][1] * m2.m[1][3] + m[1][2] * m2.m[2][3] + m[1][3] * m2.m[3][3];
  169. r.m[2][0] = m[2][0] * m2.m[0][0] + m[2][1] * m2.m[1][0] + m[2][2] * m2.m[2][0] + m[2][3] * m2.m[3][0];
  170. r.m[2][1] = m[2][0] * m2.m[0][1] + m[2][1] * m2.m[1][1] + m[2][2] * m2.m[2][1] + m[2][3] * m2.m[3][1];
  171. r.m[2][2] = m[2][0] * m2.m[0][2] + m[2][1] * m2.m[1][2] + m[2][2] * m2.m[2][2] + m[2][3] * m2.m[3][2];
  172. r.m[2][3] = m[2][0] * m2.m[0][3] + m[2][1] * m2.m[1][3] + m[2][2] * m2.m[2][3] + m[2][3] * m2.m[3][3];
  173. r.m[3][0] = m[3][0] * m2.m[0][0] + m[3][1] * m2.m[1][0] + m[3][2] * m2.m[2][0] + m[3][3] * m2.m[3][0];
  174. r.m[3][1] = m[3][0] * m2.m[0][1] + m[3][1] * m2.m[1][1] + m[3][2] * m2.m[2][1] + m[3][3] * m2.m[3][1];
  175. r.m[3][2] = m[3][0] * m2.m[0][2] + m[3][1] * m2.m[1][2] + m[3][2] * m2.m[2][2] + m[3][3] * m2.m[3][2];
  176. r.m[3][3] = m[3][0] * m2.m[0][3] + m[3][1] * m2.m[1][3] + m[3][2] * m2.m[2][3] + m[3][3] * m2.m[3][3];
  177. return r;
  178. }
  179. /** Matrix concatenation using '*'.
  180. */
  181. inline Matrix4 operator * ( const Matrix4 &m2 ) const
  182. {
  183. return concatenate( m2 );
  184. }
  185. /** Vector transformation using '*'.
  186. @remarks
  187. Transforms the given 3-D vector by the matrix, projecting the
  188. result back into <i>w</i> = 1.
  189. @note
  190. This means that the initial <i>w</i> is considered to be 1.0,
  191. and then all the tree elements of the resulting 3-D vector are
  192. divided by the resulting <i>w</i>.
  193. */
  194. inline Vector3 operator * ( const Vector3 &v ) const
  195. {
  196. Vector3 r;
  197. float fInvW = 1.0f / ( m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] );
  198. r.x = ( m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] ) * fInvW;
  199. r.y = ( m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] ) * fInvW;
  200. r.z = ( m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] ) * fInvW;
  201. return r;
  202. }
  203. inline Vector4 operator * (const Vector4& v) const
  204. {
  205. return Vector4(
  206. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w,
  207. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w,
  208. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w,
  209. m[3][0] * v.x + m[3][1] * v.y + m[3][2] * v.z + m[3][3] * v.w
  210. );
  211. }
  212. inline Plane operator * (const Plane& p) const
  213. {
  214. Plane ret;
  215. Matrix4 invTrans = inverse().transpose();
  216. Vector4 v4( p.normal.x, p.normal.y, p.normal.z, p.d );
  217. v4 = invTrans * v4;
  218. ret.normal.x = v4.x;
  219. ret.normal.y = v4.y;
  220. ret.normal.z = v4.z;
  221. ret.d = v4.w / ret.normal.normalize();
  222. return ret;
  223. }
  224. /** Matrix addition.
  225. */
  226. inline Matrix4 operator + ( const Matrix4 &m2 ) const
  227. {
  228. Matrix4 r;
  229. r.m[0][0] = m[0][0] + m2.m[0][0];
  230. r.m[0][1] = m[0][1] + m2.m[0][1];
  231. r.m[0][2] = m[0][2] + m2.m[0][2];
  232. r.m[0][3] = m[0][3] + m2.m[0][3];
  233. r.m[1][0] = m[1][0] + m2.m[1][0];
  234. r.m[1][1] = m[1][1] + m2.m[1][1];
  235. r.m[1][2] = m[1][2] + m2.m[1][2];
  236. r.m[1][3] = m[1][3] + m2.m[1][3];
  237. r.m[2][0] = m[2][0] + m2.m[2][0];
  238. r.m[2][1] = m[2][1] + m2.m[2][1];
  239. r.m[2][2] = m[2][2] + m2.m[2][2];
  240. r.m[2][3] = m[2][3] + m2.m[2][3];
  241. r.m[3][0] = m[3][0] + m2.m[3][0];
  242. r.m[3][1] = m[3][1] + m2.m[3][1];
  243. r.m[3][2] = m[3][2] + m2.m[3][2];
  244. r.m[3][3] = m[3][3] + m2.m[3][3];
  245. return r;
  246. }
  247. /** Matrix subtraction.
  248. */
  249. inline Matrix4 operator - ( const Matrix4 &m2 ) const
  250. {
  251. Matrix4 r;
  252. r.m[0][0] = m[0][0] - m2.m[0][0];
  253. r.m[0][1] = m[0][1] - m2.m[0][1];
  254. r.m[0][2] = m[0][2] - m2.m[0][2];
  255. r.m[0][3] = m[0][3] - m2.m[0][3];
  256. r.m[1][0] = m[1][0] - m2.m[1][0];
  257. r.m[1][1] = m[1][1] - m2.m[1][1];
  258. r.m[1][2] = m[1][2] - m2.m[1][2];
  259. r.m[1][3] = m[1][3] - m2.m[1][3];
  260. r.m[2][0] = m[2][0] - m2.m[2][0];
  261. r.m[2][1] = m[2][1] - m2.m[2][1];
  262. r.m[2][2] = m[2][2] - m2.m[2][2];
  263. r.m[2][3] = m[2][3] - m2.m[2][3];
  264. r.m[3][0] = m[3][0] - m2.m[3][0];
  265. r.m[3][1] = m[3][1] - m2.m[3][1];
  266. r.m[3][2] = m[3][2] - m2.m[3][2];
  267. r.m[3][3] = m[3][3] - m2.m[3][3];
  268. return r;
  269. }
  270. /** Tests 2 matrices for equality.
  271. */
  272. inline bool operator == ( const Matrix4& m2 ) const
  273. {
  274. if(
  275. m[0][0] != m2.m[0][0] || m[0][1] != m2.m[0][1] || m[0][2] != m2.m[0][2] || m[0][3] != m2.m[0][3] ||
  276. m[1][0] != m2.m[1][0] || m[1][1] != m2.m[1][1] || m[1][2] != m2.m[1][2] || m[1][3] != m2.m[1][3] ||
  277. m[2][0] != m2.m[2][0] || m[2][1] != m2.m[2][1] || m[2][2] != m2.m[2][2] || m[2][3] != m2.m[2][3] ||
  278. m[3][0] != m2.m[3][0] || m[3][1] != m2.m[3][1] || m[3][2] != m2.m[3][2] || m[3][3] != m2.m[3][3] )
  279. return false;
  280. return true;
  281. }
  282. /** Tests 2 matrices for inequality.
  283. */
  284. inline bool operator != ( const Matrix4& m2 ) const
  285. {
  286. if(
  287. m[0][0] != m2.m[0][0] || m[0][1] != m2.m[0][1] || m[0][2] != m2.m[0][2] || m[0][3] != m2.m[0][3] ||
  288. m[1][0] != m2.m[1][0] || m[1][1] != m2.m[1][1] || m[1][2] != m2.m[1][2] || m[1][3] != m2.m[1][3] ||
  289. m[2][0] != m2.m[2][0] || m[2][1] != m2.m[2][1] || m[2][2] != m2.m[2][2] || m[2][3] != m2.m[2][3] ||
  290. m[3][0] != m2.m[3][0] || m[3][1] != m2.m[3][1] || m[3][2] != m2.m[3][2] || m[3][3] != m2.m[3][3] )
  291. return true;
  292. return false;
  293. }
  294. /** Assignment from 3x3 matrix.
  295. */
  296. inline void operator = ( const Matrix3& mat3 )
  297. {
  298. m[0][0] = mat3.m[0][0]; m[0][1] = mat3.m[0][1]; m[0][2] = mat3.m[0][2];
  299. m[1][0] = mat3.m[1][0]; m[1][1] = mat3.m[1][1]; m[1][2] = mat3.m[1][2];
  300. m[2][0] = mat3.m[2][0]; m[2][1] = mat3.m[2][1]; m[2][2] = mat3.m[2][2];
  301. }
  302. inline Matrix4 transpose(void) const
  303. {
  304. return Matrix4(m[0][0], m[1][0], m[2][0], m[3][0],
  305. m[0][1], m[1][1], m[2][1], m[3][1],
  306. m[0][2], m[1][2], m[2][2], m[3][2],
  307. m[0][3], m[1][3], m[2][3], m[3][3]);
  308. }
  309. /*
  310. -----------------------------------------------------------------------
  311. Translation Transformation
  312. -----------------------------------------------------------------------
  313. */
  314. /** Sets the translation transformation part of the matrix.
  315. */
  316. inline void setTrans( const Vector3& v )
  317. {
  318. m[0][3] = v.x;
  319. m[1][3] = v.y;
  320. m[2][3] = v.z;
  321. }
  322. /** Extracts the translation transformation part of the matrix.
  323. */
  324. inline Vector3 getTrans() const
  325. {
  326. return Vector3(m[0][3], m[1][3], m[2][3]);
  327. }
  328. /** Builds a translation matrix
  329. */
  330. inline void makeTrans( const Vector3& v )
  331. {
  332. m[0][0] = 1.0; m[0][1] = 0.0; m[0][2] = 0.0; m[0][3] = v.x;
  333. m[1][0] = 0.0; m[1][1] = 1.0; m[1][2] = 0.0; m[1][3] = v.y;
  334. m[2][0] = 0.0; m[2][1] = 0.0; m[2][2] = 1.0; m[2][3] = v.z;
  335. m[3][0] = 0.0; m[3][1] = 0.0; m[3][2] = 0.0; m[3][3] = 1.0;
  336. }
  337. inline void makeTrans( float tx, float ty, float tz )
  338. {
  339. m[0][0] = 1.0; m[0][1] = 0.0; m[0][2] = 0.0; m[0][3] = tx;
  340. m[1][0] = 0.0; m[1][1] = 1.0; m[1][2] = 0.0; m[1][3] = ty;
  341. m[2][0] = 0.0; m[2][1] = 0.0; m[2][2] = 1.0; m[2][3] = tz;
  342. m[3][0] = 0.0; m[3][1] = 0.0; m[3][2] = 0.0; m[3][3] = 1.0;
  343. }
  344. /** Gets a translation matrix.
  345. */
  346. inline static Matrix4 getTrans( const Vector3& v )
  347. {
  348. Matrix4 r;
  349. r.m[0][0] = 1.0; r.m[0][1] = 0.0; r.m[0][2] = 0.0; r.m[0][3] = v.x;
  350. r.m[1][0] = 0.0; r.m[1][1] = 1.0; r.m[1][2] = 0.0; r.m[1][3] = v.y;
  351. r.m[2][0] = 0.0; r.m[2][1] = 0.0; r.m[2][2] = 1.0; r.m[2][3] = v.z;
  352. r.m[3][0] = 0.0; r.m[3][1] = 0.0; r.m[3][2] = 0.0; r.m[3][3] = 1.0;
  353. return r;
  354. }
  355. /** Gets a translation matrix - variation for not using a vector.
  356. */
  357. inline static Matrix4 getTrans( float t_x, float t_y, float t_z )
  358. {
  359. Matrix4 r;
  360. r.m[0][0] = 1.0; r.m[0][1] = 0.0; r.m[0][2] = 0.0; r.m[0][3] = t_x;
  361. r.m[1][0] = 0.0; r.m[1][1] = 1.0; r.m[1][2] = 0.0; r.m[1][3] = t_y;
  362. r.m[2][0] = 0.0; r.m[2][1] = 0.0; r.m[2][2] = 1.0; r.m[2][3] = t_z;
  363. r.m[3][0] = 0.0; r.m[3][1] = 0.0; r.m[3][2] = 0.0; r.m[3][3] = 1.0;
  364. return r;
  365. }
  366. /*
  367. -----------------------------------------------------------------------
  368. Scale Transformation
  369. -----------------------------------------------------------------------
  370. */
  371. /** Sets the scale part of the matrix.
  372. */
  373. inline void setScale( const Vector3& v )
  374. {
  375. m[0][0] = v.x;
  376. m[1][1] = v.y;
  377. m[2][2] = v.z;
  378. }
  379. /** Gets a scale matrix.
  380. */
  381. inline static Matrix4 getScale( const Vector3& v )
  382. {
  383. Matrix4 r;
  384. r.m[0][0] = v.x; r.m[0][1] = 0.0; r.m[0][2] = 0.0; r.m[0][3] = 0.0;
  385. r.m[1][0] = 0.0; r.m[1][1] = v.y; r.m[1][2] = 0.0; r.m[1][3] = 0.0;
  386. r.m[2][0] = 0.0; r.m[2][1] = 0.0; r.m[2][2] = v.z; r.m[2][3] = 0.0;
  387. r.m[3][0] = 0.0; r.m[3][1] = 0.0; r.m[3][2] = 0.0; r.m[3][3] = 1.0;
  388. return r;
  389. }
  390. /** Gets a scale matrix - variation for not using a vector.
  391. */
  392. inline static Matrix4 getScale( float s_x, float s_y, float s_z )
  393. {
  394. Matrix4 r;
  395. r.m[0][0] = s_x; r.m[0][1] = 0.0; r.m[0][2] = 0.0; r.m[0][3] = 0.0;
  396. r.m[1][0] = 0.0; r.m[1][1] = s_y; r.m[1][2] = 0.0; r.m[1][3] = 0.0;
  397. r.m[2][0] = 0.0; r.m[2][1] = 0.0; r.m[2][2] = s_z; r.m[2][3] = 0.0;
  398. r.m[3][0] = 0.0; r.m[3][1] = 0.0; r.m[3][2] = 0.0; r.m[3][3] = 1.0;
  399. return r;
  400. }
  401. /** Extracts the rotation / scaling part of the Matrix as a 3x3 matrix.
  402. @param m3x3 Destination Matrix3
  403. */
  404. inline void extract3x3Matrix(Matrix3& m3x3) const
  405. {
  406. m3x3.m[0][0] = m[0][0];
  407. m3x3.m[0][1] = m[0][1];
  408. m3x3.m[0][2] = m[0][2];
  409. m3x3.m[1][0] = m[1][0];
  410. m3x3.m[1][1] = m[1][1];
  411. m3x3.m[1][2] = m[1][2];
  412. m3x3.m[2][0] = m[2][0];
  413. m3x3.m[2][1] = m[2][1];
  414. m3x3.m[2][2] = m[2][2];
  415. }
  416. /** Determines if this matrix involves a scaling. */
  417. inline bool hasScale() const
  418. {
  419. // check magnitude of column vectors (==local axes)
  420. float t = m[0][0] * m[0][0] + m[1][0] * m[1][0] + m[2][0] * m[2][0];
  421. if (!Math::RealEqual(t, 1.0, (float)1e-04))
  422. return true;
  423. t = m[0][1] * m[0][1] + m[1][1] * m[1][1] + m[2][1] * m[2][1];
  424. if (!Math::RealEqual(t, 1.0, (float)1e-04))
  425. return true;
  426. t = m[0][2] * m[0][2] + m[1][2] * m[1][2] + m[2][2] * m[2][2];
  427. if (!Math::RealEqual(t, 1.0, (float)1e-04))
  428. return true;
  429. return false;
  430. }
  431. /** Determines if this matrix involves a negative scaling. */
  432. inline bool hasNegativeScale() const
  433. {
  434. return determinant() < 0;
  435. }
  436. /** Extracts the rotation / scaling part as a quaternion from the Matrix.
  437. */
  438. inline Quaternion extractQuaternion() const
  439. {
  440. Matrix3 m3x3;
  441. extract3x3Matrix(m3x3);
  442. return Quaternion(m3x3);
  443. }
  444. static const Matrix4 ZERO;
  445. static const Matrix4 IDENTITY;
  446. /** Useful little matrix which takes 2D clipspace {-1, 1} to {0,1}
  447. and inverts the Y. */
  448. static const Matrix4 CLIPSPACE2DTOIMAGESPACE;
  449. inline Matrix4 operator*(float scalar) const
  450. {
  451. return Matrix4(
  452. scalar*m[0][0], scalar*m[0][1], scalar*m[0][2], scalar*m[0][3],
  453. scalar*m[1][0], scalar*m[1][1], scalar*m[1][2], scalar*m[1][3],
  454. scalar*m[2][0], scalar*m[2][1], scalar*m[2][2], scalar*m[2][3],
  455. scalar*m[3][0], scalar*m[3][1], scalar*m[3][2], scalar*m[3][3]);
  456. }
  457. /** Function for writing to a stream.
  458. */
  459. inline CM_UTILITY_EXPORT friend std::ostream& operator <<
  460. ( std::ostream& o, const Matrix4& mat )
  461. {
  462. o << "Matrix4(";
  463. for (size_t i = 0; i < 4; ++i)
  464. {
  465. o << " row" << (unsigned)i << "{";
  466. for(size_t j = 0; j < 4; ++j)
  467. {
  468. o << mat[i][j] << " ";
  469. }
  470. o << "}";
  471. }
  472. o << ")";
  473. return o;
  474. }
  475. Matrix4 adjoint() const;
  476. float determinant() const;
  477. Matrix4 inverse() const;
  478. /** Building a Matrix4 from orientation / scale / position.
  479. @remarks
  480. Transform is performed in the order scale, rotate, translation, i.e. translation is independent
  481. of orientation axes, scale does not affect size of translation, rotation and scaling are always
  482. centered on the origin.
  483. */
  484. void makeTransform(const Vector3& position, const Vector3& scale, const Quaternion& orientation);
  485. /** Building an inverse Matrix4 from orientation / scale / position.
  486. @remarks
  487. As makeTransform except it build the inverse given the same data as makeTransform, so
  488. performing -translation, -rotate, 1/scale in that order.
  489. */
  490. void makeInverseTransform(const Vector3& position, const Vector3& scale, const Quaternion& orientation);
  491. /** Decompose a Matrix4 to orientation / scale / position.
  492. */
  493. void decomposition(Vector3& position, Vector3& scale, Quaternion& orientation) const;
  494. /** Check whether or not the matrix is affine matrix.
  495. @remarks
  496. An affine matrix is a 4x4 matrix with row 3 equal to (0, 0, 0, 1),
  497. e.g. no projective coefficients.
  498. */
  499. inline bool isAffine(void) const
  500. {
  501. return m[3][0] == 0 && m[3][1] == 0 && m[3][2] == 0 && m[3][3] == 1;
  502. }
  503. /** Returns the inverse of the affine matrix.
  504. @note
  505. The matrix must be an affine matrix. @see Matrix4::isAffine.
  506. */
  507. Matrix4 inverseAffine(void) const;
  508. /** Concatenate two affine matrices.
  509. @note
  510. The matrices must be affine matrix. @see Matrix4::isAffine.
  511. */
  512. inline Matrix4 concatenateAffine(const Matrix4 &m2) const
  513. {
  514. assert(isAffine() && m2.isAffine());
  515. return Matrix4(
  516. m[0][0] * m2.m[0][0] + m[0][1] * m2.m[1][0] + m[0][2] * m2.m[2][0],
  517. m[0][0] * m2.m[0][1] + m[0][1] * m2.m[1][1] + m[0][2] * m2.m[2][1],
  518. m[0][0] * m2.m[0][2] + m[0][1] * m2.m[1][2] + m[0][2] * m2.m[2][2],
  519. m[0][0] * m2.m[0][3] + m[0][1] * m2.m[1][3] + m[0][2] * m2.m[2][3] + m[0][3],
  520. m[1][0] * m2.m[0][0] + m[1][1] * m2.m[1][0] + m[1][2] * m2.m[2][0],
  521. m[1][0] * m2.m[0][1] + m[1][1] * m2.m[1][1] + m[1][2] * m2.m[2][1],
  522. m[1][0] * m2.m[0][2] + m[1][1] * m2.m[1][2] + m[1][2] * m2.m[2][2],
  523. m[1][0] * m2.m[0][3] + m[1][1] * m2.m[1][3] + m[1][2] * m2.m[2][3] + m[1][3],
  524. m[2][0] * m2.m[0][0] + m[2][1] * m2.m[1][0] + m[2][2] * m2.m[2][0],
  525. m[2][0] * m2.m[0][1] + m[2][1] * m2.m[1][1] + m[2][2] * m2.m[2][1],
  526. m[2][0] * m2.m[0][2] + m[2][1] * m2.m[1][2] + m[2][2] * m2.m[2][2],
  527. m[2][0] * m2.m[0][3] + m[2][1] * m2.m[1][3] + m[2][2] * m2.m[2][3] + m[2][3],
  528. 0, 0, 0, 1);
  529. }
  530. /** 3-D Vector transformation specially for an affine matrix.
  531. @remarks
  532. Transforms the given 3-D vector by the matrix, projecting the
  533. result back into <i>w</i> = 1.
  534. @note
  535. The matrix must be an affine matrix. @see Matrix4::isAffine.
  536. */
  537. inline Vector3 transformAffine(const Vector3& v) const
  538. {
  539. assert(isAffine());
  540. return Vector3(
  541. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3],
  542. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3],
  543. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3]);
  544. }
  545. /** 4-D Vector transformation specially for an affine matrix.
  546. @note
  547. The matrix must be an affine matrix. @see Matrix4::isAffine.
  548. */
  549. inline Vector4 transformAffine(const Vector4& v) const
  550. {
  551. assert(isAffine());
  552. return Vector4(
  553. m[0][0] * v.x + m[0][1] * v.y + m[0][2] * v.z + m[0][3] * v.w,
  554. m[1][0] * v.x + m[1][1] * v.y + m[1][2] * v.z + m[1][3] * v.w,
  555. m[2][0] * v.x + m[2][1] * v.y + m[2][2] * v.z + m[2][3] * v.w,
  556. v.w);
  557. }
  558. };
  559. /* Removed from Vector4 and made a non-member here because otherwise
  560. CmMatrix4.h and CmVector4.h have to try to include and inline each
  561. other, which frankly doesn't work ;)
  562. */
  563. inline Vector4 operator * (const Vector4& v, const Matrix4& mat)
  564. {
  565. return Vector4(
  566. v.x*mat[0][0] + v.y*mat[1][0] + v.z*mat[2][0] + v.w*mat[3][0],
  567. v.x*mat[0][1] + v.y*mat[1][1] + v.z*mat[2][1] + v.w*mat[3][1],
  568. v.x*mat[0][2] + v.y*mat[1][2] + v.z*mat[2][2] + v.w*mat[3][2],
  569. v.x*mat[0][3] + v.y*mat[1][3] + v.z*mat[2][3] + v.w*mat[3][3]
  570. );
  571. }
  572. /** @} */
  573. /** @} */
  574. CM_ALLOW_MEMCPY_SERIALIZATION(Matrix4);
  575. }
  576. #endif