mMatrix.h 15 KB

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