mMatrix.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MMATRIX_H_
  23. #define _MMATRIX_H_
  24. #include <algorithm>
  25. #ifndef _MPLANE_H_
  26. #include "math/mPlane.h"
  27. #endif
  28. #ifndef _MBOX_H_
  29. #include "math/mBox.h"
  30. #endif
  31. #ifndef _MPOINT4_H_
  32. #include "math/mPoint4.h"
  33. #endif
  34. /// 4x4 Matrix Class
  35. ///
  36. /// This runs at F32 precision.
  37. class MatrixF
  38. {
  39. private:
  40. F32 m[16]; ///< Note: Torque uses row-major matrices
  41. public:
  42. /// Create an uninitialized matrix.
  43. ///
  44. /// @param identity If true, initialize to the identity matrix.
  45. explicit MatrixF(bool identity=false);
  46. /// Create a matrix to rotate about origin by e.
  47. /// @see set
  48. explicit MatrixF( const EulerF &e);
  49. /// Create a matrix to rotate about p by e.
  50. /// @see set
  51. MatrixF( const EulerF &e, const Point3F& p);
  52. /// Get the index in m to element in column i, row j
  53. ///
  54. /// This is necessary as we have m as a one dimensional array.
  55. ///
  56. /// @param i Column desired.
  57. /// @param j Row desired.
  58. static U32 idx(U32 i, U32 j) { return (i + j*4); }
  59. /// Initialize matrix to rotate about origin by e.
  60. MatrixF& set( const EulerF &e);
  61. /// Initialize matrix to rotate about p by e.
  62. MatrixF& set( const EulerF &e, const Point3F& p);
  63. /// Initialize matrix with a cross product of p.
  64. MatrixF& setCrossProduct( const Point3F &p);
  65. /// Initialize matrix with a tensor product of p.
  66. MatrixF& setTensorProduct( const Point3F &p, const Point3F& q);
  67. operator F32*() { return (m); } ///< Allow people to get at m.
  68. operator const F32*() const { return (F32*)(m); } ///< Allow people to get at m.
  69. bool isAffine() const; ///< Check to see if this is an affine matrix.
  70. bool isIdentity() const; ///< Checks for identity matrix.
  71. /// Make this an identity matrix.
  72. MatrixF& identity();
  73. /// Invert m.
  74. MatrixF& inverse();
  75. /// Copy the inversion of this into out matrix.
  76. void invertTo( MatrixF *out );
  77. /// Take inverse of matrix assuming it is affine (rotation,
  78. /// scale, sheer, translation only).
  79. MatrixF& affineInverse();
  80. /// Swap rows and columns.
  81. MatrixF& transpose();
  82. /// M * Matrix(p) -> M
  83. MatrixF& scale( const Point3F &s );
  84. MatrixF& scale( F32 s ) { return scale( Point3F( s, s, s ) ); }
  85. /// Return scale assuming scale was applied via mat.scale(s).
  86. Point3F getScale() const;
  87. EulerF toEuler() const;
  88. /// Compute the inverse of the matrix.
  89. ///
  90. /// Computes inverse of full 4x4 matrix. Returns false and performs no inverse if
  91. /// the determinant is 0.
  92. ///
  93. /// Note: In most cases you want to use the normal inverse function. This method should
  94. /// be used if the matrix has something other than (0,0,0,1) in the bottom row.
  95. bool fullInverse();
  96. /// Swaps rows and columns into matrix.
  97. void transposeTo(F32 *matrix) const;
  98. /// Normalize the matrix.
  99. void normalize();
  100. /// Copy the requested column into a Point4F.
  101. void getColumn(S32 col, Point4F *cptr) const;
  102. Point4F getColumn4F(S32 col) const { Point4F ret; getColumn(col,&ret); return ret; }
  103. /// Copy the requested column into a Point3F.
  104. ///
  105. /// This drops the bottom-most row.
  106. void getColumn(S32 col, Point3F *cptr) const;
  107. Point3F getColumn3F(S32 col) const { Point3F ret; getColumn(col,&ret); return ret; }
  108. /// Set the specified column from a Point4F.
  109. void setColumn(S32 col, const Point4F& cptr);
  110. /// Set the specified column from a Point3F.
  111. ///
  112. /// The bottom-most row is not set.
  113. void setColumn(S32 col, const Point3F& cptr);
  114. /// Copy the specified row into a Point4F.
  115. void getRow(S32 row, Point4F *cptr) const;
  116. Point4F getRow4F(S32 row) const { Point4F ret; getRow(row,&ret); return ret; }
  117. /// Copy the specified row into a Point3F.
  118. ///
  119. /// Right-most item is dropped.
  120. void getRow(S32 row, Point3F *cptr) const;
  121. Point3F getRow3F(S32 row) const { Point3F ret; getRow(row,&ret); return ret; }
  122. /// Set the specified row from a Point4F.
  123. void setRow(S32 row, const Point4F& cptr);
  124. /// Set the specified row from a Point3F.
  125. ///
  126. /// The right-most item is not set.
  127. void setRow(S32 row, const Point3F& cptr);
  128. /// Get the position of the matrix.
  129. ///
  130. /// This is the 4th column of the matrix.
  131. Point3F getPosition() const;
  132. /// Set the position of the matrix.
  133. ///
  134. /// This is the 4th column of the matrix.
  135. void setPosition( const Point3F &pos ) { setColumn( 3, pos ); }
  136. /// Add the passed delta to the matrix position.
  137. void displace( const Point3F &delta );
  138. /// Get the x axis of the matrix.
  139. ///
  140. /// This is the 1st column of the matrix and is
  141. /// normally considered the right vector.
  142. VectorF getRightVector() const;
  143. /// Get the y axis of the matrix.
  144. ///
  145. /// This is the 2nd column of the matrix and is
  146. /// normally considered the forward vector.
  147. VectorF getForwardVector() const;
  148. /// Get the z axis of the matrix.
  149. ///
  150. /// This is the 3rd column of the matrix and is
  151. /// normally considered the up vector.
  152. VectorF getUpVector() const;
  153. MatrixF& mul(const MatrixF &a); ///< M * a -> M
  154. MatrixF& mulL(const MatrixF &a); ///< a * M -> M
  155. MatrixF& mul(const MatrixF &a, const MatrixF &b); ///< a * b -> M
  156. // Scalar multiplies
  157. MatrixF& mul(const F32 a); ///< M * a -> M
  158. MatrixF& mul(const MatrixF &a, const F32 b); ///< a * b -> M
  159. void mul( Point4F& p ) const; ///< M * p -> p (full [4x4] * [1x4])
  160. void mulP( Point3F& p ) const; ///< M * p -> p (assume w = 1.0f)
  161. void mulP( const Point3F &p, Point3F *d) const; ///< M * p -> d (assume w = 1.0f)
  162. void mulV( VectorF& p ) const; ///< M * v -> v (assume w = 0.0f)
  163. void mulV( const VectorF &p, Point3F *d) const; ///< M * v -> d (assume w = 0.0f)
  164. void mul(Box3F& b) const; ///< Axial box -> Axial Box
  165. MatrixF& add( const MatrixF& m );
  166. /// Convenience function to allow people to treat this like an array.
  167. F32& operator ()(S32 row, S32 col) { return m[idx(col,row)]; }
  168. F32 operator ()(S32 row, S32 col) const { return m[idx(col,row)]; }
  169. void dumpMatrix(const char *caption=NULL) const;
  170. // Math operator overloads
  171. //------------------------------------
  172. friend MatrixF operator * ( const MatrixF &m1, const MatrixF &m2 );
  173. MatrixF& operator *= ( const MatrixF &m );
  174. // Static identity matrix
  175. const static MatrixF Identity;
  176. };
  177. //--------------------------------------
  178. // Inline Functions
  179. inline MatrixF::MatrixF(bool _identity)
  180. {
  181. if (_identity)
  182. identity();
  183. else
  184. std::fill_n(m, 16, 0);
  185. }
  186. inline MatrixF::MatrixF( const EulerF &e )
  187. {
  188. set(e);
  189. }
  190. inline MatrixF::MatrixF( const EulerF &e, const Point3F& p )
  191. {
  192. set(e,p);
  193. }
  194. inline MatrixF& MatrixF::set( const EulerF &e)
  195. {
  196. m_matF_set_euler( e, *this );
  197. return (*this);
  198. }
  199. inline MatrixF& MatrixF::set( const EulerF &e, const Point3F& p)
  200. {
  201. m_matF_set_euler_point( e, p, *this );
  202. return (*this);
  203. }
  204. inline MatrixF& MatrixF::setCrossProduct( const Point3F &p)
  205. {
  206. m[1] = -(m[4] = p.z);
  207. m[8] = -(m[2] = p.y);
  208. m[6] = -(m[9] = p.x);
  209. m[0] = m[3] = m[5] = m[7] = m[10] = m[11] =
  210. m[12] = m[13] = m[14] = 0.0f;
  211. m[15] = 1;
  212. return (*this);
  213. }
  214. inline MatrixF& MatrixF::setTensorProduct( const Point3F &p, const Point3F &q)
  215. {
  216. m[0] = p.x * q.x;
  217. m[1] = p.x * q.y;
  218. m[2] = p.x * q.z;
  219. m[4] = p.y * q.x;
  220. m[5] = p.y * q.y;
  221. m[6] = p.y * q.z;
  222. m[8] = p.z * q.x;
  223. m[9] = p.z * q.y;
  224. m[10] = p.z * q.z;
  225. m[3] = m[7] = m[11] = m[12] = m[13] = m[14] = 0.0f;
  226. m[15] = 1.0f;
  227. return (*this);
  228. }
  229. inline bool MatrixF::isIdentity() const
  230. {
  231. return
  232. m[0] == 1.0f &&
  233. m[1] == 0.0f &&
  234. m[2] == 0.0f &&
  235. m[3] == 0.0f &&
  236. m[4] == 0.0f &&
  237. m[5] == 1.0f &&
  238. m[6] == 0.0f &&
  239. m[7] == 0.0f &&
  240. m[8] == 0.0f &&
  241. m[9] == 0.0f &&
  242. m[10] == 1.0f &&
  243. m[11] == 0.0f &&
  244. m[12] == 0.0f &&
  245. m[13] == 0.0f &&
  246. m[14] == 0.0f &&
  247. m[15] == 1.0f;
  248. }
  249. inline MatrixF& MatrixF::identity()
  250. {
  251. m[0] = 1.0f;
  252. m[1] = 0.0f;
  253. m[2] = 0.0f;
  254. m[3] = 0.0f;
  255. m[4] = 0.0f;
  256. m[5] = 1.0f;
  257. m[6] = 0.0f;
  258. m[7] = 0.0f;
  259. m[8] = 0.0f;
  260. m[9] = 0.0f;
  261. m[10] = 1.0f;
  262. m[11] = 0.0f;
  263. m[12] = 0.0f;
  264. m[13] = 0.0f;
  265. m[14] = 0.0f;
  266. m[15] = 1.0f;
  267. return (*this);
  268. }
  269. inline MatrixF& MatrixF::inverse()
  270. {
  271. m_matF_inverse(m);
  272. return (*this);
  273. }
  274. inline void MatrixF::invertTo( MatrixF *out )
  275. {
  276. m_matF_invert_to(m,*out);
  277. }
  278. inline MatrixF& MatrixF::affineInverse()
  279. {
  280. // AssertFatal(isAffine() == true, "Error, this matrix is not an affine transform");
  281. m_matF_affineInverse(m);
  282. return (*this);
  283. }
  284. inline MatrixF& MatrixF::transpose()
  285. {
  286. m_matF_transpose(m);
  287. return (*this);
  288. }
  289. inline MatrixF& MatrixF::scale(const Point3F& p)
  290. {
  291. m_matF_scale(m,p);
  292. return *this;
  293. }
  294. inline Point3F MatrixF::getScale() const
  295. {
  296. Point3F scale;
  297. scale.x = mSqrt(m[0]*m[0] + m[4] * m[4] + m[8] * m[8]);
  298. scale.y = mSqrt(m[1]*m[1] + m[5] * m[5] + m[9] * m[9]);
  299. scale.z = mSqrt(m[2]*m[2] + m[6] * m[6] + m[10] * m[10]);
  300. return scale;
  301. }
  302. inline void MatrixF::normalize()
  303. {
  304. m_matF_normalize(m);
  305. }
  306. inline MatrixF& MatrixF::mul( const MatrixF &a )
  307. { // M * a -> M
  308. AssertFatal(&a != this, "MatrixF::mul - a.mul(a) is invalid!");
  309. MatrixF tempThis(*this);
  310. m_matF_x_matF(tempThis, a, *this);
  311. return (*this);
  312. }
  313. inline MatrixF& MatrixF::mulL( const MatrixF &a )
  314. { // a * M -> M
  315. AssertFatal(&a != this, "MatrixF::mulL - a.mul(a) is invalid!");
  316. MatrixF tempThis(*this);
  317. m_matF_x_matF(a, tempThis, *this);
  318. return (*this);
  319. }
  320. inline MatrixF& MatrixF::mul( const MatrixF &a, const MatrixF &b )
  321. { // a * b -> M
  322. AssertFatal((&a != this) && (&b != this), "MatrixF::mul - a.mul(a, b) a.mul(b, a) a.mul(a, a) is invalid!");
  323. m_matF_x_matF(a, b, *this);
  324. return (*this);
  325. }
  326. inline MatrixF& MatrixF::mul(const F32 a)
  327. {
  328. for (U32 i = 0; i < 16; i++)
  329. m[i] *= a;
  330. return *this;
  331. }
  332. inline MatrixF& MatrixF::mul(const MatrixF &a, const F32 b)
  333. {
  334. *this = a;
  335. mul(b);
  336. return *this;
  337. }
  338. inline void MatrixF::mul( Point4F& p ) const
  339. {
  340. Point4F temp;
  341. m_matF_x_point4F(*this, &p.x, &temp.x);
  342. p = temp;
  343. }
  344. inline void MatrixF::mulP( Point3F& p) const
  345. {
  346. // M * p -> d
  347. Point3F d;
  348. m_matF_x_point3F(*this, &p.x, &d.x);
  349. p = d;
  350. }
  351. inline void MatrixF::mulP( const Point3F &p, Point3F *d) const
  352. {
  353. // M * p -> d
  354. m_matF_x_point3F(*this, &p.x, &d->x);
  355. }
  356. inline void MatrixF::mulV( VectorF& v) const
  357. {
  358. // M * v -> v
  359. VectorF temp;
  360. m_matF_x_vectorF(*this, &v.x, &temp.x);
  361. v = temp;
  362. }
  363. inline void MatrixF::mulV( const VectorF &v, Point3F *d) const
  364. {
  365. // M * v -> d
  366. m_matF_x_vectorF(*this, &v.x, &d->x);
  367. }
  368. inline void MatrixF::mul(Box3F& b) const
  369. {
  370. m_matF_x_box3F(*this, &b.minExtents.x, &b.maxExtents.x);
  371. }
  372. inline MatrixF& MatrixF::add( const MatrixF& a )
  373. {
  374. for( U32 i = 0; i < 16; ++ i )
  375. m[ i ] += a.m[ i ];
  376. return *this;
  377. }
  378. inline void MatrixF::getColumn(S32 col, Point4F *cptr) const
  379. {
  380. cptr->x = m[col];
  381. cptr->y = m[col+4];
  382. cptr->z = m[col+8];
  383. cptr->w = m[col+12];
  384. }
  385. inline void MatrixF::getColumn(S32 col, Point3F *cptr) const
  386. {
  387. cptr->x = m[col];
  388. cptr->y = m[col+4];
  389. cptr->z = m[col+8];
  390. }
  391. inline void MatrixF::setColumn(S32 col, const Point4F &cptr)
  392. {
  393. m[col] = cptr.x;
  394. m[col+4] = cptr.y;
  395. m[col+8] = cptr.z;
  396. m[col+12]= cptr.w;
  397. }
  398. inline void MatrixF::setColumn(S32 col, const Point3F &cptr)
  399. {
  400. m[col] = cptr.x;
  401. m[col+4] = cptr.y;
  402. m[col+8] = cptr.z;
  403. }
  404. inline void MatrixF::getRow(S32 col, Point4F *cptr) const
  405. {
  406. col *= 4;
  407. cptr->x = m[col++];
  408. cptr->y = m[col++];
  409. cptr->z = m[col++];
  410. cptr->w = m[col];
  411. }
  412. inline void MatrixF::getRow(S32 col, Point3F *cptr) const
  413. {
  414. col *= 4;
  415. cptr->x = m[col++];
  416. cptr->y = m[col++];
  417. cptr->z = m[col];
  418. }
  419. inline void MatrixF::setRow(S32 col, const Point4F &cptr)
  420. {
  421. col *= 4;
  422. m[col++] = cptr.x;
  423. m[col++] = cptr.y;
  424. m[col++] = cptr.z;
  425. m[col] = cptr.w;
  426. }
  427. inline void MatrixF::setRow(S32 col, const Point3F &cptr)
  428. {
  429. col *= 4;
  430. m[col++] = cptr.x;
  431. m[col++] = cptr.y;
  432. m[col] = cptr.z;
  433. }
  434. inline Point3F MatrixF::getPosition() const
  435. {
  436. return Point3F( m[3], m[3+4], m[3+8] );
  437. }
  438. inline void MatrixF::displace( const Point3F &delta )
  439. {
  440. m[3] += delta.x;
  441. m[3+4] += delta.y;
  442. m[3+8] += delta.z;
  443. }
  444. inline VectorF MatrixF::getForwardVector() const
  445. {
  446. VectorF vec;
  447. getColumn( 1, &vec );
  448. return vec;
  449. }
  450. inline VectorF MatrixF::getRightVector() const
  451. {
  452. VectorF vec;
  453. getColumn( 0, &vec );
  454. return vec;
  455. }
  456. inline VectorF MatrixF::getUpVector() const
  457. {
  458. VectorF vec;
  459. getColumn( 2, &vec );
  460. return vec;
  461. }
  462. //------------------------------------
  463. // Math operator overloads
  464. //------------------------------------
  465. inline MatrixF operator * ( const MatrixF &m1, const MatrixF &m2 )
  466. {
  467. // temp = m1 * m2
  468. MatrixF temp;
  469. m_matF_x_matF(m1, m2, temp);
  470. return temp;
  471. }
  472. inline MatrixF& MatrixF::operator *= ( const MatrixF &m1 )
  473. {
  474. MatrixF tempThis(*this);
  475. m_matF_x_matF(tempThis, m1, *this);
  476. return (*this);
  477. }
  478. //------------------------------------
  479. // Non-member methods
  480. //------------------------------------
  481. inline void mTransformPlane(const MatrixF& mat, const Point3F& scale, const PlaneF& plane, PlaneF * result)
  482. {
  483. m_matF_x_scale_x_planeF(mat, &scale.x, &plane.x, &result->x);
  484. }
  485. #endif //_MMATRIX_H_