mMatrix.h 14 KB

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