mMatrix.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _MMATH_H_
  25. #include "math/mMath.h"
  26. #endif
  27. /// 4x4 Matrix Class
  28. ///
  29. /// This runs at F32 precision.
  30. class MatrixF
  31. {
  32. private:
  33. F32 m[16]; ///< Note: this is stored in ROW MAJOR format. OpenGL is
  34. /// COLUMN MAJOR. Transpose before sending down.
  35. public:
  36. /// Create an uninitialized matrix.
  37. ///
  38. /// @param identity If true, initialize to the identity matrix.
  39. explicit MatrixF(bool identity=false);
  40. /// Create a matrix to rotate about origin by e.
  41. /// @see set
  42. explicit MatrixF( const EulerF &e);
  43. /// Create a matrix to rotate about p by e.
  44. /// @see set
  45. MatrixF( const EulerF &e, const Point3F& p);
  46. /// Get the index in m to element in column i, row j
  47. ///
  48. /// This is necessary as we have m as a one dimensional array.
  49. ///
  50. /// @param i Column desired.
  51. /// @param j Row desired.
  52. static U32 idx(U32 i, U32 j) { return (i + j*4); }
  53. /// Initialize matrix to rotate about origin by e.
  54. MatrixF& set( const EulerF &e);
  55. /// Initialize matrix to rotate about p by e.
  56. MatrixF& set( const EulerF &e, const Point3F& p);
  57. /// Initialize matrix with a cross product of p.
  58. MatrixF& setCrossProduct( const Point3F &p);
  59. /// Initialize matrix with a tensor product of p.
  60. MatrixF& setTensorProduct( const Point3F &p, const Point3F& q);
  61. operator F32*() { return (m); } ///< Allow people to get at m.
  62. operator F32*() const { return (F32*)(m); } ///< Allow people to get at m.
  63. bool isAffine() const; ///< Check to see if this is an affine matrix.
  64. bool isIdentity() const; ///< Checks for identity matrix.
  65. /// Make this an identity matrix.
  66. MatrixF& identity();
  67. /// Invert m.
  68. MatrixF& inverse();
  69. /// Take inverse without disturbing position data.
  70. ///
  71. /// Ie, take inverse of 3x3 submatrix.
  72. MatrixF& affineInverse();
  73. MatrixF& transpose(); ///< Swap rows and columns.
  74. MatrixF& scale(const Point3F& p); ///< M * Matrix(p) -> M
  75. EulerF toEuler() const;
  76. /// Compute the inverse of the matrix.
  77. ///
  78. /// Computes inverse of full 4x4 matrix. Returns false and performs no inverse if
  79. /// the determinant is 0.
  80. ///
  81. /// Note: In most cases you want to use the normal inverse function. This method should
  82. /// be used if the matrix has something other than (0,0,0,1) in the bottom row.
  83. bool fullInverse();
  84. /// Swaps rows and columns into matrix.
  85. void transposeTo(F32 *matrix) const;
  86. /// Normalize the matrix.
  87. void normalize();
  88. /// Copy the requested column into a Point4F.
  89. void getColumn(S32 col, Point4F *cptr) const;
  90. /// Copy the requested column into a Point3F.
  91. ///
  92. /// This drops the bottom-most row.
  93. void getColumn(S32 col, Point3F *cptr) const;
  94. /// Set the specified column from a Point4F.
  95. void setColumn(S32 col, const Point4F& cptr);
  96. /// Set the specified column from a Point3F.
  97. ///
  98. /// The bottom-most row is not set.
  99. void setColumn(S32 col, const Point3F& cptr);
  100. /// Copy the specified row into a Point4F.
  101. void getRow(S32 row, Point4F *cptr) const;
  102. /// Copy the specified row into a Point3F.
  103. ///
  104. /// Right-most item is dropped.
  105. void getRow(S32 row, Point3F *cptr) const;
  106. /// Set the specified row from a Point4F.
  107. void setRow(S32 row, const Point4F& cptr);
  108. /// Set the specified row from a Point3F.
  109. ///
  110. /// The right-most item is not set.
  111. void setRow(S32 row, const Point3F& cptr);
  112. /// Get the position of the matrix.
  113. ///
  114. /// This is the 4th column of the matrix.
  115. Point3F getPosition() const;
  116. /// Set the position of the matrix.
  117. ///
  118. /// This is the 4th column of the matrix.
  119. void setPosition( const Point3F &pos ){ setColumn( 3, pos ); }
  120. /// Get the z axis of the matrix.
  121. ///
  122. /// This is the 3rd column of the matrix and is
  123. /// normally considered the up vector.
  124. VectorF getUpVector() const;
  125. MatrixF& mul(const MatrixF &a); ///< M * a -> M
  126. MatrixF& mul(const MatrixF &a, const MatrixF &b); ///< a * b -> M
  127. // Scalar multiplies
  128. MatrixF& mul(const F32 a); ///< M * a -> M
  129. MatrixF& mul(const MatrixF &a, const F32 b); ///< a * b -> M
  130. void mul( Point4F& p ) const; ///< M * p -> p (full [4x4] * [1x4])
  131. void mulP( Point3F& p ) const; ///< M * p -> p (assume w = 1.0f)
  132. void mulP( const Point3F &p, Point3F *d) const; ///< M * p -> d (assume w = 1.0f)
  133. void mulV( VectorF& p ) const; ///< M * v -> v (assume w = 0.0f)
  134. void mulV( const VectorF &p, Point3F *d) const; ///< M * v -> d (assume w = 0.0f)
  135. void mul(Box3F& b) const; ///< Axial box -> Axial Box
  136. /// Convenience function to allow people to treat this like an array.
  137. F32& operator ()(S32 row, S32 col) { return m[idx(col,row)]; }
  138. void dumpMatrix(const char *caption=NULL) const;
  139. }
  140. #if defined(__VEC__)
  141. __attribute__ ((aligned (16)))
  142. #endif
  143. ;
  144. //--------------------------------------
  145. // Inline Functions
  146. inline MatrixF::MatrixF(bool _identity)
  147. {
  148. if (_identity)
  149. identity();
  150. }
  151. inline MatrixF::MatrixF( const EulerF &e )
  152. {
  153. set(e);
  154. }
  155. inline MatrixF::MatrixF( const EulerF &e, const Point3F& p )
  156. {
  157. set(e,p);
  158. }
  159. inline MatrixF& MatrixF::set( const EulerF &e)
  160. {
  161. m_matF_set_euler( e, *this );
  162. return (*this);
  163. }
  164. inline MatrixF& MatrixF::set( const EulerF &e, const Point3F& p)
  165. {
  166. m_matF_set_euler_point( e, p, *this );
  167. return (*this);
  168. }
  169. inline MatrixF& MatrixF::setCrossProduct( const Point3F &p)
  170. {
  171. m[1] = -(m[4] = p.z);
  172. m[8] = -(m[2] = p.y);
  173. m[6] = -(m[9] = p.x);
  174. m[0] = m[3] = m[5] = m[7] = m[10] = m[11] =
  175. m[12] = m[13] = m[14] = 0;
  176. m[15] = 1;
  177. return (*this);
  178. }
  179. inline MatrixF& MatrixF::setTensorProduct( const Point3F &p, const Point3F &q)
  180. {
  181. m[0] = p.x * q.x;
  182. m[1] = p.x * q.y;
  183. m[2] = p.x * q.z;
  184. m[4] = p.y * q.x;
  185. m[5] = p.y * q.y;
  186. m[6] = p.y * q.z;
  187. m[8] = p.z * q.x;
  188. m[9] = p.z * q.y;
  189. m[10] = p.z * q.z;
  190. m[3] = m[7] = m[11] = m[12] = m[13] = m[14] = 0;
  191. m[15] = 1;
  192. return (*this);
  193. }
  194. inline bool MatrixF::isIdentity() const
  195. {
  196. return
  197. m[0] == 1.0f &&
  198. m[1] == 0.0f &&
  199. m[2] == 0.0f &&
  200. m[3] == 0.0f &&
  201. m[4] == 0.0f &&
  202. m[5] == 1.0f &&
  203. m[6] == 0.0f &&
  204. m[7] == 0.0f &&
  205. m[8] == 0.0f &&
  206. m[9] == 0.0f &&
  207. m[10] == 1.0f &&
  208. m[11] == 0.0f &&
  209. m[12] == 0.0f &&
  210. m[13] == 0.0f &&
  211. m[14] == 0.0f &&
  212. m[15] == 1.0f;
  213. }
  214. inline MatrixF& MatrixF::identity()
  215. {
  216. m[0] = 1.0f;
  217. m[1] = 0.0f;
  218. m[2] = 0.0f;
  219. m[3] = 0.0f;
  220. m[4] = 0.0f;
  221. m[5] = 1.0f;
  222. m[6] = 0.0f;
  223. m[7] = 0.0f;
  224. m[8] = 0.0f;
  225. m[9] = 0.0f;
  226. m[10] = 1.0f;
  227. m[11] = 0.0f;
  228. m[12] = 0.0f;
  229. m[13] = 0.0f;
  230. m[14] = 0.0f;
  231. m[15] = 1.0f;
  232. return (*this);
  233. }
  234. inline MatrixF& MatrixF::inverse()
  235. {
  236. m_matF_inverse(m);
  237. return (*this);
  238. }
  239. inline MatrixF& MatrixF::affineInverse()
  240. {
  241. // AssertFatal(isAffine() == true, "Error, this matrix is not an affine transform");
  242. m_matF_affineInverse(m);
  243. return (*this);
  244. }
  245. inline MatrixF& MatrixF::transpose()
  246. {
  247. m_matF_transpose(m);
  248. return (*this);
  249. }
  250. inline MatrixF& MatrixF::scale(const Point3F& p)
  251. {
  252. m_matF_scale(m,p);
  253. return *this;
  254. }
  255. inline void MatrixF::normalize()
  256. {
  257. m_matF_normalize(m);
  258. }
  259. inline MatrixF& MatrixF::mul( const MatrixF &a )
  260. { // M * a -> M
  261. MatrixF tempThis(*this);
  262. m_matF_x_matF(tempThis, a, *this);
  263. return (*this);
  264. }
  265. inline MatrixF& MatrixF::mul( const MatrixF &a, const MatrixF &b )
  266. { // a * b -> M
  267. m_matF_x_matF(a, b, *this);
  268. return (*this);
  269. }
  270. inline MatrixF& MatrixF::mul(const F32 a)
  271. {
  272. for (U32 i = 0; i < 16; i++)
  273. m[i] *= a;
  274. return *this;
  275. }
  276. inline MatrixF& MatrixF::mul(const MatrixF &a, const F32 b)
  277. {
  278. *this = a;
  279. mul(b);
  280. return *this;
  281. }
  282. inline void MatrixF::mul( Point4F& p ) const
  283. {
  284. Point4F temp;
  285. m_matF_x_point4F(*this, &p.x, &temp.x);
  286. p = temp;
  287. }
  288. inline void MatrixF::mulP( Point3F& p) const
  289. {
  290. // M * p -> d
  291. Point3F d;
  292. m_matF_x_point3F(*this, &p.x, &d.x);
  293. p = d;
  294. }
  295. inline void MatrixF::mulP( const Point3F &p, Point3F *d) const
  296. {
  297. // M * p -> d
  298. m_matF_x_point3F(*this, &p.x, &d->x);
  299. }
  300. inline void MatrixF::mulV( VectorF& v) const
  301. {
  302. // M * v -> v
  303. VectorF temp;
  304. m_matF_x_vectorF(*this, &v.x, &temp.x);
  305. v = temp;
  306. }
  307. inline void MatrixF::mulV( const VectorF &v, Point3F *d) const
  308. {
  309. // M * v -> d
  310. m_matF_x_vectorF(*this, &v.x, &d->x);
  311. }
  312. inline void MatrixF::mul(Box3F& b) const
  313. {
  314. m_matF_x_box3F(*this, &b.mMin.x, &b.mMax.x);
  315. }
  316. inline void MatrixF::getColumn(S32 col, Point4F *cptr) const
  317. {
  318. cptr->x = m[col];
  319. cptr->y = m[col+4];
  320. cptr->z = m[col+8];
  321. cptr->w = m[col+12];
  322. }
  323. inline void MatrixF::getColumn(S32 col, Point3F *cptr) const
  324. {
  325. cptr->x = m[col];
  326. cptr->y = m[col+4];
  327. cptr->z = m[col+8];
  328. }
  329. inline void MatrixF::setColumn(S32 col, const Point4F &cptr)
  330. {
  331. m[col] = cptr.x;
  332. m[col+4] = cptr.y;
  333. m[col+8] = cptr.z;
  334. m[col+12]= cptr.w;
  335. }
  336. inline void MatrixF::setColumn(S32 col, const Point3F &cptr)
  337. {
  338. m[col] = cptr.x;
  339. m[col+4] = cptr.y;
  340. m[col+8] = cptr.z;
  341. }
  342. inline void MatrixF::getRow(S32 col, Point4F *cptr) const
  343. {
  344. col *= 4;
  345. cptr->x = m[col++];
  346. cptr->y = m[col++];
  347. cptr->z = m[col++];
  348. cptr->w = m[col];
  349. }
  350. inline void MatrixF::getRow(S32 col, Point3F *cptr) const
  351. {
  352. col *= 4;
  353. cptr->x = m[col++];
  354. cptr->y = m[col++];
  355. cptr->z = m[col];
  356. }
  357. inline void MatrixF::setRow(S32 col, const Point4F &cptr)
  358. {
  359. col *= 4;
  360. m[col++] = cptr.x;
  361. m[col++] = cptr.y;
  362. m[col++] = cptr.z;
  363. m[col] = cptr.w;
  364. }
  365. inline void MatrixF::setRow(S32 col, const Point3F &cptr)
  366. {
  367. col *= 4;
  368. m[col++] = cptr.x;
  369. m[col++] = cptr.y;
  370. m[col] = cptr.z;
  371. }
  372. // not too speedy, but convienient
  373. inline Point3F MatrixF::getPosition() const
  374. {
  375. Point3F pos;
  376. getColumn( 3, &pos );
  377. return pos;
  378. }
  379. inline VectorF MatrixF::getUpVector() const
  380. {
  381. VectorF vec;
  382. getColumn( 2, &vec );
  383. return vec;
  384. }
  385. #endif //_MMATRIX_H_