mMatrix.h 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  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. #ifndef USE_TEMPLATE_MATRIX
  38. /// 4x4 Matrix Class
  39. ///
  40. /// This runs at F32 precision.
  41. class MatrixF
  42. {
  43. friend class MatrixFEngineExport;
  44. private:
  45. F32 m[16]; ///< Note: Torque uses row-major matrices
  46. public:
  47. /// Create an uninitialized matrix.
  48. ///
  49. /// @param identity If true, initialize to the identity matrix.
  50. explicit MatrixF(bool identity=false);
  51. /// Create a matrix to rotate about origin by e.
  52. /// @see set
  53. explicit MatrixF( const EulerF &e);
  54. /// Create a matrix to rotate about p by e.
  55. /// @see set
  56. MatrixF( const EulerF &e, const Point3F& p);
  57. /// Get the index in m to element in column i, row j
  58. ///
  59. /// This is necessary as we have m as a one dimensional array.
  60. ///
  61. /// @param i Column desired.
  62. /// @param j Row desired.
  63. static U32 idx(U32 i, U32 j) { return (i + j*4); }
  64. /// Initialize matrix to rotate about origin by e.
  65. MatrixF& set( const EulerF &e);
  66. /// Initialize matrix to rotate about p by e.
  67. MatrixF& set( const EulerF &e, const Point3F& p);
  68. /// Initialize matrix with a cross product of p.
  69. MatrixF& setCrossProduct( const Point3F &p);
  70. /// Initialize matrix with a tensor product of p.
  71. MatrixF& setTensorProduct( const Point3F &p, const Point3F& q);
  72. operator F32*() { return (m); } ///< Allow people to get at m.
  73. operator const F32*() const { return (F32*)(m); } ///< Allow people to get at m.
  74. bool isAffine() const; ///< Check to see if this is an affine matrix.
  75. bool isIdentity() const; ///< Checks for identity matrix.
  76. /// Make this an identity matrix.
  77. MatrixF& identity();
  78. /// Invert m.
  79. MatrixF& inverse();
  80. /// Copy the inversion of this into out matrix.
  81. void invertTo( MatrixF *out );
  82. /// Take inverse of matrix assuming it is affine (rotation,
  83. /// scale, sheer, translation only).
  84. MatrixF& affineInverse();
  85. /// Swap rows and columns.
  86. MatrixF& transpose();
  87. /// M * Matrix(p) -> M
  88. MatrixF& scale( const Point3F &s );
  89. MatrixF& scale( F32 s ) { return scale( Point3F( s, s, s ) ); }
  90. /// Return scale assuming scale was applied via mat.scale(s).
  91. Point3F getScale() const;
  92. EulerF toEuler() const;
  93. /// Compute the inverse of the matrix.
  94. ///
  95. /// Computes inverse of full 4x4 matrix. Returns false and performs no inverse if
  96. /// the determinant is 0.
  97. ///
  98. /// Note: In most cases you want to use the normal inverse function. This method should
  99. /// be used if the matrix has something other than (0,0,0,1) in the bottom row.
  100. bool fullInverse();
  101. /// Reverse depth for projection matrix
  102. /// Simplifies reversal matrix mult to 4 subtractions
  103. void reverseProjection();
  104. /// Swaps rows and columns into matrix.
  105. void transposeTo(F32 *matrix) const;
  106. /// Normalize the matrix.
  107. void normalize();
  108. /// Copy the requested column into a Point4F.
  109. void getColumn(S32 col, Point4F *cptr) const;
  110. Point4F getColumn4F(S32 col) const { Point4F ret; getColumn(col,&ret); return ret; }
  111. /// Copy the requested column into a Point3F.
  112. ///
  113. /// This drops the bottom-most row.
  114. void getColumn(S32 col, Point3F *cptr) const;
  115. Point3F getColumn3F(S32 col) const { Point3F ret; getColumn(col,&ret); return ret; }
  116. /// Set the specified column from a Point4F.
  117. void setColumn(S32 col, const Point4F& cptr);
  118. /// Set the specified column from a Point3F.
  119. ///
  120. /// The bottom-most row is not set.
  121. void setColumn(S32 col, const Point3F& cptr);
  122. /// Copy the specified row into a Point4F.
  123. void getRow(S32 row, Point4F *cptr) const;
  124. Point4F getRow4F(S32 row) const { Point4F ret; getRow(row,&ret); return ret; }
  125. /// Copy the specified row into a Point3F.
  126. ///
  127. /// Right-most item is dropped.
  128. void getRow(S32 row, Point3F *cptr) const;
  129. Point3F getRow3F(S32 row) const { Point3F ret; getRow(row,&ret); return ret; }
  130. /// Set the specified row from a Point4F.
  131. void setRow(S32 row, const Point4F& cptr);
  132. /// Set the specified row from a Point3F.
  133. ///
  134. /// The right-most item is not set.
  135. void setRow(S32 row, const Point3F& cptr);
  136. /// Get the position of the matrix.
  137. ///
  138. /// This is the 4th column of the matrix.
  139. Point3F getPosition() const;
  140. /// Set the position of the matrix.
  141. ///
  142. /// This is the 4th column of the matrix.
  143. void setPosition( const Point3F &pos ) { setColumn( 3, pos ); }
  144. /// Add the passed delta to the matrix position.
  145. void displace( const Point3F &delta );
  146. /// Get the x axis of the matrix.
  147. ///
  148. /// This is the 1st column of the matrix and is
  149. /// normally considered the right vector.
  150. VectorF getRightVector() const;
  151. /// Get the y axis of the matrix.
  152. ///
  153. /// This is the 2nd column of the matrix and is
  154. /// normally considered the forward vector.
  155. VectorF getForwardVector() const;
  156. /// Get the z axis of the matrix.
  157. ///
  158. /// This is the 3rd column of the matrix and is
  159. /// normally considered the up vector.
  160. VectorF getUpVector() const;
  161. MatrixF& mul(const MatrixF &a); ///< M * a -> M
  162. MatrixF& mulL(const MatrixF &a); ///< a * M -> M
  163. MatrixF& mul(const MatrixF &a, const MatrixF &b); ///< a * b -> M
  164. // Scalar multiplies
  165. MatrixF& mul(const F32 a); ///< M * a -> M
  166. MatrixF& mul(const MatrixF &a, const F32 b); ///< a * b -> M
  167. void mul( Point4F& p ) const; ///< M * p -> p (full [4x4] * [1x4])
  168. void mulP( Point3F& p ) const; ///< M * p -> p (assume w = 1.0f)
  169. void mulP( const Point3F &p, Point3F *d) const; ///< M * p -> d (assume w = 1.0f)
  170. void mulV( VectorF& p ) const; ///< M * v -> v (assume w = 0.0f)
  171. void mulV( const VectorF &p, Point3F *d) const; ///< M * v -> d (assume w = 0.0f)
  172. void mul(Box3F& b) const; ///< Axial box -> Axial Box
  173. MatrixF& add( const MatrixF& m );
  174. /// Convenience function to allow people to treat this like an array.
  175. F32& operator ()(S32 row, S32 col) { return m[idx(col,row)]; }
  176. F32 operator ()(S32 row, S32 col) const { return m[idx(col,row)]; }
  177. void dumpMatrix(const char *caption=NULL) const;
  178. // Math operator overloads
  179. //------------------------------------
  180. friend MatrixF operator * ( const MatrixF &m1, const MatrixF &m2 );
  181. MatrixF& operator *= ( const MatrixF &m );
  182. MatrixF &operator = (const MatrixF &m);
  183. bool isNaN();
  184. // Static identity matrix
  185. const static MatrixF Identity;
  186. };
  187. class MatrixFEngineExport
  188. {
  189. public:
  190. static EngineFieldTable::Field getMatrixField();
  191. };
  192. //--------------------------------------
  193. // Inline Functions
  194. inline MatrixF::MatrixF(bool _identity)
  195. {
  196. if (_identity)
  197. identity();
  198. else
  199. std::fill_n(m, 16, 0);
  200. }
  201. inline MatrixF::MatrixF( const EulerF &e )
  202. {
  203. set(e);
  204. }
  205. inline MatrixF::MatrixF( const EulerF &e, const Point3F& p )
  206. {
  207. set(e,p);
  208. }
  209. inline MatrixF& MatrixF::set( const EulerF &e)
  210. {
  211. m_matF_set_euler( e, *this );
  212. return (*this);
  213. }
  214. inline MatrixF& MatrixF::set( const EulerF &e, const Point3F& p)
  215. {
  216. m_matF_set_euler_point( e, p, *this );
  217. return (*this);
  218. }
  219. inline MatrixF& MatrixF::setCrossProduct( const Point3F &p)
  220. {
  221. m[1] = -(m[4] = p.z);
  222. m[8] = -(m[2] = p.y);
  223. m[6] = -(m[9] = p.x);
  224. m[0] = m[3] = m[5] = m[7] = m[10] = m[11] =
  225. m[12] = m[13] = m[14] = 0.0f;
  226. m[15] = 1;
  227. return (*this);
  228. }
  229. inline MatrixF& MatrixF::setTensorProduct( const Point3F &p, const Point3F &q)
  230. {
  231. m[0] = p.x * q.x;
  232. m[1] = p.x * q.y;
  233. m[2] = p.x * q.z;
  234. m[4] = p.y * q.x;
  235. m[5] = p.y * q.y;
  236. m[6] = p.y * q.z;
  237. m[8] = p.z * q.x;
  238. m[9] = p.z * q.y;
  239. m[10] = p.z * q.z;
  240. m[3] = m[7] = m[11] = m[12] = m[13] = m[14] = 0.0f;
  241. m[15] = 1.0f;
  242. return (*this);
  243. }
  244. inline bool MatrixF::isIdentity() const
  245. {
  246. return
  247. m[0] == 1.0f &&
  248. m[1] == 0.0f &&
  249. m[2] == 0.0f &&
  250. m[3] == 0.0f &&
  251. m[4] == 0.0f &&
  252. m[5] == 1.0f &&
  253. m[6] == 0.0f &&
  254. m[7] == 0.0f &&
  255. m[8] == 0.0f &&
  256. m[9] == 0.0f &&
  257. m[10] == 1.0f &&
  258. m[11] == 0.0f &&
  259. m[12] == 0.0f &&
  260. m[13] == 0.0f &&
  261. m[14] == 0.0f &&
  262. m[15] == 1.0f;
  263. }
  264. inline MatrixF& MatrixF::identity()
  265. {
  266. m[0] = 1.0f;
  267. m[1] = 0.0f;
  268. m[2] = 0.0f;
  269. m[3] = 0.0f;
  270. m[4] = 0.0f;
  271. m[5] = 1.0f;
  272. m[6] = 0.0f;
  273. m[7] = 0.0f;
  274. m[8] = 0.0f;
  275. m[9] = 0.0f;
  276. m[10] = 1.0f;
  277. m[11] = 0.0f;
  278. m[12] = 0.0f;
  279. m[13] = 0.0f;
  280. m[14] = 0.0f;
  281. m[15] = 1.0f;
  282. return (*this);
  283. }
  284. inline MatrixF& MatrixF::inverse()
  285. {
  286. m_matF_inverse(m);
  287. return (*this);
  288. }
  289. inline void MatrixF::invertTo( MatrixF *out )
  290. {
  291. m_matF_invert_to(m,*out);
  292. }
  293. inline MatrixF& MatrixF::affineInverse()
  294. {
  295. // AssertFatal(isAffine() == true, "Error, this matrix is not an affine transform");
  296. m_matF_affineInverse(m);
  297. return (*this);
  298. }
  299. inline MatrixF& MatrixF::transpose()
  300. {
  301. m_matF_transpose(m);
  302. return (*this);
  303. }
  304. inline MatrixF& MatrixF::scale(const Point3F& p)
  305. {
  306. m_matF_scale(m,p);
  307. return *this;
  308. }
  309. inline Point3F MatrixF::getScale() const
  310. {
  311. Point3F scale;
  312. scale.x = mSqrt(m[0]*m[0] + m[4] * m[4] + m[8] * m[8]);
  313. scale.y = mSqrt(m[1]*m[1] + m[5] * m[5] + m[9] * m[9]);
  314. scale.z = mSqrt(m[2]*m[2] + m[6] * m[6] + m[10] * m[10]);
  315. return scale;
  316. }
  317. inline void MatrixF::normalize()
  318. {
  319. m_matF_normalize(m);
  320. }
  321. inline MatrixF& MatrixF::mul( const MatrixF &a )
  322. { // M * a -> M
  323. AssertFatal(&a != this, "MatrixF::mul - a.mul(a) is invalid!");
  324. MatrixF tempThis(*this);
  325. m_matF_x_matF(tempThis, a, *this);
  326. return (*this);
  327. }
  328. inline MatrixF& MatrixF::mulL( const MatrixF &a )
  329. { // a * M -> M
  330. AssertFatal(&a != this, "MatrixF::mulL - a.mul(a) is invalid!");
  331. MatrixF tempThis(*this);
  332. m_matF_x_matF(a, tempThis, *this);
  333. return (*this);
  334. }
  335. inline MatrixF& MatrixF::mul( const MatrixF &a, const MatrixF &b )
  336. { // a * b -> M
  337. AssertFatal((&a != this) && (&b != this), "MatrixF::mul - a.mul(a, b) a.mul(b, a) a.mul(a, a) is invalid!");
  338. m_matF_x_matF(a, b, *this);
  339. return (*this);
  340. }
  341. inline MatrixF& MatrixF::mul(const F32 a)
  342. {
  343. for (U32 i = 0; i < 16; i++)
  344. m[i] *= a;
  345. return *this;
  346. }
  347. inline MatrixF& MatrixF::mul(const MatrixF &a, const F32 b)
  348. {
  349. *this = a;
  350. mul(b);
  351. return *this;
  352. }
  353. inline void MatrixF::mul( Point4F& p ) const
  354. {
  355. Point4F temp;
  356. m_matF_x_point4F(*this, &p.x, &temp.x);
  357. p = temp;
  358. }
  359. inline void MatrixF::mulP( Point3F& p) const
  360. {
  361. // M * p -> d
  362. Point3F d;
  363. m_matF_x_point3F(*this, &p.x, &d.x);
  364. p = d;
  365. }
  366. inline void MatrixF::mulP( const Point3F &p, Point3F *d) const
  367. {
  368. // M * p -> d
  369. m_matF_x_point3F(*this, &p.x, &d->x);
  370. }
  371. inline void MatrixF::mulV( VectorF& v) const
  372. {
  373. // M * v -> v
  374. VectorF temp;
  375. m_matF_x_vectorF(*this, &v.x, &temp.x);
  376. v = temp;
  377. }
  378. inline void MatrixF::mulV( const VectorF &v, Point3F *d) const
  379. {
  380. // M * v -> d
  381. m_matF_x_vectorF(*this, &v.x, &d->x);
  382. }
  383. inline void MatrixF::mul(Box3F& b) const
  384. {
  385. m_matF_x_box3F(*this, &b.minExtents.x, &b.maxExtents.x);
  386. }
  387. inline MatrixF& MatrixF::add( const MatrixF& a )
  388. {
  389. for( U32 i = 0; i < 16; ++ i )
  390. m[ i ] += a.m[ i ];
  391. return *this;
  392. }
  393. inline void MatrixF::getColumn(S32 col, Point4F *cptr) const
  394. {
  395. cptr->x = m[col];
  396. cptr->y = m[col+4];
  397. cptr->z = m[col+8];
  398. cptr->w = m[col+12];
  399. }
  400. inline void MatrixF::getColumn(S32 col, Point3F *cptr) const
  401. {
  402. cptr->x = m[col];
  403. cptr->y = m[col+4];
  404. cptr->z = m[col+8];
  405. }
  406. inline void MatrixF::setColumn(S32 col, const Point4F &cptr)
  407. {
  408. m[col] = cptr.x;
  409. m[col+4] = cptr.y;
  410. m[col+8] = cptr.z;
  411. m[col+12]= cptr.w;
  412. }
  413. inline void MatrixF::setColumn(S32 col, const Point3F &cptr)
  414. {
  415. m[col] = cptr.x;
  416. m[col+4] = cptr.y;
  417. m[col+8] = cptr.z;
  418. }
  419. inline void MatrixF::getRow(S32 col, Point4F *cptr) const
  420. {
  421. col *= 4;
  422. cptr->x = m[col++];
  423. cptr->y = m[col++];
  424. cptr->z = m[col++];
  425. cptr->w = m[col];
  426. }
  427. inline void MatrixF::getRow(S32 col, Point3F *cptr) const
  428. {
  429. col *= 4;
  430. cptr->x = m[col++];
  431. cptr->y = m[col++];
  432. cptr->z = m[col];
  433. }
  434. inline void MatrixF::setRow(S32 col, const Point4F &cptr)
  435. {
  436. col *= 4;
  437. m[col++] = cptr.x;
  438. m[col++] = cptr.y;
  439. m[col++] = cptr.z;
  440. m[col] = cptr.w;
  441. }
  442. inline void MatrixF::setRow(S32 col, const Point3F &cptr)
  443. {
  444. col *= 4;
  445. m[col++] = cptr.x;
  446. m[col++] = cptr.y;
  447. m[col] = cptr.z;
  448. }
  449. inline Point3F MatrixF::getPosition() const
  450. {
  451. return Point3F( m[3], m[3+4], m[3+8] );
  452. }
  453. inline void MatrixF::displace( const Point3F &delta )
  454. {
  455. m[3] += delta.x;
  456. m[3+4] += delta.y;
  457. m[3+8] += delta.z;
  458. }
  459. inline VectorF MatrixF::getForwardVector() const
  460. {
  461. VectorF vec;
  462. getColumn( 1, &vec );
  463. return vec;
  464. }
  465. inline VectorF MatrixF::getRightVector() const
  466. {
  467. VectorF vec;
  468. getColumn( 0, &vec );
  469. return vec;
  470. }
  471. inline VectorF MatrixF::getUpVector() const
  472. {
  473. VectorF vec;
  474. getColumn( 2, &vec );
  475. return vec;
  476. }
  477. //------------------------------------
  478. // Math operator overloads
  479. //------------------------------------
  480. inline MatrixF operator * ( const MatrixF &m1, const MatrixF &m2 )
  481. {
  482. // temp = m1 * m2
  483. MatrixF temp;
  484. m_matF_x_matF(m1, m2, temp);
  485. return temp;
  486. }
  487. inline MatrixF& MatrixF::operator *= ( const MatrixF &m1 )
  488. {
  489. MatrixF tempThis(*this);
  490. m_matF_x_matF(tempThis, m1, *this);
  491. return (*this);
  492. }
  493. inline MatrixF &MatrixF::operator = (const MatrixF &m1)
  494. {
  495. for (U32 i=0;i<16;i++)
  496. this->m[i] = m1.m[i];
  497. return (*this);
  498. }
  499. inline bool MatrixF::isNaN()
  500. {
  501. bool isaNaN = false;
  502. for (U32 i = 0; i < 16; i++)
  503. if (mIsNaN_F(m[i]))
  504. isaNaN = true;
  505. return isaNaN;
  506. }
  507. //------------------------------------
  508. // Non-member methods
  509. //------------------------------------
  510. inline void mTransformPlane(const MatrixF& mat, const Point3F& scale, const PlaneF& plane, PlaneF * result)
  511. {
  512. m_matF_x_scale_x_planeF(mat, &scale.x, &plane.x, &result->x);
  513. }
  514. #else // !USE_TEMPLATE_MATRIX
  515. //------------------------------------
  516. // Templatized matrix class to replace MATRIXF above
  517. //------------------------------------
  518. template<typename DATA_TYPE, U32 rows, U32 cols>
  519. class Matrix {
  520. friend class MatrixTemplateExport;
  521. private:
  522. DATA_TYPE data[rows * cols];
  523. public:
  524. static_assert(rows >= 2 && cols >= 2, "Matrix must have at least 2 rows and 2 cols.");
  525. // ------ Setters and initializers ------
  526. explicit Matrix(bool identity = false) {
  527. std::fill(data, data + (rows * cols), DATA_TYPE(0));
  528. if (identity) {
  529. for (U32 i = 0; i < rows; i++) {
  530. for (U32 j = 0; j < cols; j++) {
  531. // others already get filled with 0
  532. if (j == i)
  533. (*this)(i, j) = static_cast<DATA_TYPE>(1);
  534. }
  535. }
  536. }
  537. }
  538. explicit Matrix(const EulerF& e);
  539. /// Make this an identity matrix.
  540. Matrix<DATA_TYPE, rows, cols>& identity();
  541. void reverseProjection();
  542. void normalize();
  543. Matrix<DATA_TYPE, rows, cols>& set(const EulerF& e);
  544. Matrix(const EulerF& e, const Point3F p);
  545. Matrix<DATA_TYPE, rows, cols>& set(const EulerF& e, const Point3F p);
  546. Matrix<DATA_TYPE, rows, cols> inverse();
  547. Matrix<DATA_TYPE, rows, cols>& transpose();
  548. void invert();
  549. Matrix<DATA_TYPE, rows, cols>& setCrossProduct(const Point3F& p);
  550. Matrix<DATA_TYPE, rows, cols>& setTensorProduct(const Point3F& p, const Point3F& q);
  551. /// M * Matrix(p) -> M
  552. Matrix<DATA_TYPE, rows, cols>& scale(const Point3F& s);
  553. Matrix<DATA_TYPE, rows, cols>& scale(DATA_TYPE s) { return scale(Point3F(s, s, s)); }
  554. void setColumn(S32 col, const Point4F& cptr);
  555. void setColumn(S32 col, const Point3F& cptr);
  556. void setRow(S32 row, const Point4F& cptr);
  557. void setRow(S32 row, const Point3F& cptr);
  558. void displace(const Point3F& delta);
  559. bool fullInverse();
  560. void setPosition(const Point3F& pos) { setColumn(3, pos); }
  561. ///< M * a -> M
  562. Matrix<DATA_TYPE, rows, cols>& mul(const Matrix<DATA_TYPE, rows, cols>& a)
  563. {
  564. *this = *this * a; return *this;
  565. }
  566. ///< a * M -> M
  567. Matrix<DATA_TYPE, rows, cols>& mulL(const Matrix<DATA_TYPE, rows, cols>& a)
  568. { return *this = a * *this; }
  569. ///< a * b -> M
  570. Matrix<DATA_TYPE, rows, cols>& mul(const Matrix<DATA_TYPE, rows, cols>& a, const Matrix<DATA_TYPE, rows, cols>& b)
  571. { return *this = a * b; }
  572. ///< M * a -> M
  573. Matrix<DATA_TYPE, rows, cols>& mul(const F32 a)
  574. { return *this * a; }
  575. ///< a * b -> M
  576. Matrix<DATA_TYPE, rows, cols>& mul(const Matrix<DATA_TYPE, rows, cols>& a, const F32 b)
  577. { return *this = a * b; }
  578. ///< M * p -> p (full [4x4] * [1x4])
  579. void mul(Point4F& p) const { p = *this * p; }
  580. ///< M * p -> p (assume w = 1.0f)
  581. void mulP(Point3F& p) const { p = *this * p; }
  582. ///< M * p -> d (assume w = 1.0f)
  583. void mulP(const Point3F& p, Point3F* d) const { *d = *this * p; }
  584. ///< M * v -> v (assume w = 0.0f)
  585. void mulV(VectorF& v) const
  586. {
  587. AssertFatal(rows == 4 && cols == 4, "Multiplying VectorF with matrix requires 4x4");
  588. VectorF result(
  589. (*this)(0, 0) * v.x + (*this)(0, 1) * v.y + (*this)(0, 2) * v.z,
  590. (*this)(1, 0) * v.x + (*this)(1, 1) * v.y + (*this)(1, 2) * v.z,
  591. (*this)(2, 0) * v.x + (*this)(2, 1) * v.y + (*this)(2, 2) * v.z
  592. );
  593. v = result;
  594. }
  595. ///< M * v -> d (assume w = 0.0f)
  596. void mulV(const VectorF& v, Point3F* d) const
  597. {
  598. AssertFatal(rows == 4 && cols == 4, "Multiplying VectorF with matrix requires 4x4");
  599. VectorF result(
  600. (*this)(0, 0) * v.x + (*this)(0, 1) * v.y + (*this)(0, 2) * v.z,
  601. (*this)(1, 0) * v.x + (*this)(1, 1) * v.y + (*this)(1, 2) * v.z,
  602. (*this)(2, 0) * v.x + (*this)(2, 1) * v.y + (*this)(2, 2) * v.z
  603. );
  604. d->x = result.x;
  605. d->y = result.y;
  606. d->z = result.z;
  607. }
  608. ///< Axial box -> Axial Box (too big a function to be inline)
  609. void mul(Box3F& box) const;
  610. // ------ Getters ------
  611. bool isNaN() {
  612. for (U32 i = 0; i < rows; i++) {
  613. for (U32 j = 0; j < cols; j++) {
  614. if (mIsNaN_F((*this)(i, j)))
  615. return true;
  616. }
  617. }
  618. return false;
  619. }
  620. // col * rows + row
  621. static U32 idx(U32 i, U32 j) { return (i * rows + j); }
  622. bool isAffine() const;
  623. bool isIdentity() const;
  624. /// Take inverse of matrix assuming it is affine (rotation,
  625. /// scale, sheer, translation only).
  626. Matrix<DATA_TYPE, rows, cols> affineInverse();
  627. Point3F getScale() const;
  628. EulerF toEuler() const;
  629. Point3F getPosition() const;
  630. void getColumn(S32 col, Point4F* cptr) const;
  631. Point4F getColumn4F(S32 col) const { Point4F ret; getColumn(col, &ret); return ret; }
  632. void getColumn(S32 col, Point3F* cptr) const;
  633. Point3F getColumn3F(S32 col) const { Point3F ret; getColumn(col, &ret); return ret; }
  634. void getRow(S32 row, Point4F* cptr) const;
  635. Point4F getRow4F(S32 row) const { Point4F ret; getRow(row, &ret); return ret; }
  636. void getRow(S32 row, Point3F* cptr) const;
  637. Point3F getRow3F(S32 row) const { Point3F ret; getRow(row, &ret); return ret; }
  638. VectorF getRightVector() const;
  639. VectorF getForwardVector() const;
  640. VectorF getUpVector() const;
  641. DATA_TYPE* getData() {
  642. return data;
  643. }
  644. const DATA_TYPE* getData() const {
  645. return data;
  646. }
  647. void transposeTo(Matrix<DATA_TYPE, cols, rows>& matrix) const {
  648. for (U32 i = 0; i < rows; ++i) {
  649. for (U32 j = 0; j < cols; ++j) {
  650. matrix(j, i) = (*this)(i, j);
  651. }
  652. }
  653. }
  654. void invertTo(Matrix<DATA_TYPE, cols, rows>* matrix) const;
  655. void invertTo(Matrix<DATA_TYPE, cols, rows>* matrix);
  656. void dumpMatrix(const char* caption = NULL) const;
  657. // Static identity matrix
  658. static const Matrix Identity;
  659. // ------ Operators ------
  660. Matrix<DATA_TYPE, rows, cols> operator * (const Matrix<DATA_TYPE, rows, cols>& other) const {
  661. Matrix<DATA_TYPE, rows, cols> result;
  662. for (U32 i = 0; i < rows; i++) {
  663. for (U32 j = 0; j < cols; j++) {
  664. result(i, j) = 0;
  665. for (U32 k = 0; k < cols; k++) {
  666. result(i, j) += (*this)(i, k) * other(k, j);
  667. }
  668. }
  669. }
  670. return result;
  671. }
  672. Matrix<DATA_TYPE, rows, cols> operator *= (const Matrix<DATA_TYPE, rows, cols>& other) {
  673. *this = *this * other;
  674. return *this;
  675. }
  676. Matrix<DATA_TYPE, rows, cols> operator * (const DATA_TYPE scalar) const {
  677. Matrix<DATA_TYPE, rows, cols> result;
  678. for (U32 i = 0; i < rows; i++) {
  679. for (U32 j = 0; j < cols; j++) {
  680. result(i, j) = (*this)(i, j) * scalar;
  681. }
  682. }
  683. return result;
  684. }
  685. Matrix<DATA_TYPE, rows, cols>& operator *= (const DATA_TYPE scalar) {
  686. for (U32 i = 0; i < rows; i++) {
  687. for (U32 j = 0; j < cols; j++) {
  688. (*this)(i, j) *= scalar;
  689. }
  690. }
  691. return *this;
  692. }
  693. Point3F operator*(const Point3F& point) const {
  694. AssertFatal(rows == 4 && cols == 4, "Multiplying point3 with matrix requires 4x4");
  695. return Point3F(
  696. (*this)(0, 0) * point.x + (*this)(0, 1) * point.y + (*this)(0, 2) * point.z + (*this)(0, 3),
  697. (*this)(1, 0) * point.x + (*this)(1, 1) * point.y + (*this)(1, 2) * point.z + (*this)(1, 3),
  698. (*this)(2, 0) * point.x + (*this)(2, 1) * point.y + (*this)(2, 2) * point.z + (*this)(2, 3)
  699. );
  700. }
  701. Point4F operator*(const Point4F& point) const {
  702. AssertFatal(rows == 4 && cols == 4, "Multiplying point4 with matrix requires 4x4");
  703. return Point4F(
  704. (*this)(0, 0) * point.x + (*this)(0, 1) * point.y + (*this)(0, 2) * point.z + (*this)(0, 3) * point.w,
  705. (*this)(1, 0) * point.x + (*this)(1, 1) * point.y + (*this)(1, 2) * point.z + (*this)(1, 3) * point.w,
  706. (*this)(2, 0) * point.x + (*this)(2, 1) * point.y + (*this)(2, 2) * point.z + (*this)(2, 3) * point.w,
  707. (*this)(3, 0) * point.x + (*this)(3, 1) * point.y + (*this)(3, 2) * point.z + (*this)(3, 3) * point.w
  708. );
  709. }
  710. Matrix<DATA_TYPE, rows, cols>& operator = (const Matrix<DATA_TYPE, rows, cols>& other) {
  711. if (this != &other) {
  712. std::copy(other.data, other.data + rows * cols, this->data);
  713. }
  714. return *this;
  715. }
  716. bool operator == (const Matrix<DATA_TYPE, rows, cols>& other) const {
  717. for (U32 i = 0; i < rows; i++) {
  718. for (U32 j = 0; j < cols; j++) {
  719. if ((*this)(i, j) != other(i, j))
  720. return false;
  721. }
  722. }
  723. return true;
  724. }
  725. bool operator != (const Matrix<DATA_TYPE, rows, cols>& other) const {
  726. return !(*this == other);
  727. }
  728. operator DATA_TYPE* () { return (data); }
  729. operator const DATA_TYPE* () const { return (DATA_TYPE*)(data); }
  730. DATA_TYPE& operator () (U32 row, U32 col) {
  731. if (row >= rows || col >= cols)
  732. AssertFatal(false, "Matrix indices out of range");
  733. return data[col * rows + row];
  734. }
  735. const DATA_TYPE& operator () (U32 row, U32 col) const {
  736. if (row >= rows || col >= cols)
  737. AssertFatal(false, "Matrix indices out of range");
  738. return data[col * rows + row];
  739. }
  740. };
  741. //--------------------------------------------
  742. // INLINE FUNCTIONS
  743. //--------------------------------------------
  744. template<typename DATA_TYPE, U32 rows, U32 cols>
  745. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::transpose()
  746. {
  747. // square matrices can just swap, non square requires a temp mat.
  748. if (rows == cols) {
  749. for (U32 i = 0; i < rows; i++) {
  750. for (U32 j = 0; j < cols; j++) {
  751. std::swap((*this)(j, i), (*this)(i, j));
  752. }
  753. }
  754. }
  755. else {
  756. Matrix<DATA_TYPE, rows, cols> result;
  757. for (U32 i = 0; i < rows; i++) {
  758. for (U32 j = 0; j < cols; j++) {
  759. result(j, i) = (*this)(i, j);
  760. }
  761. }
  762. std::copy(std::begin(result.data), std::end(result.data), std::begin(data));
  763. }
  764. return (*this);
  765. }
  766. template<typename DATA_TYPE, U32 rows, U32 cols>
  767. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::identity()
  768. {
  769. for (U32 i = 0; i < rows; i++) {
  770. for (U32 j = 0; j < cols; j++) {
  771. if (j == i)
  772. (*this)(i, j) = static_cast<DATA_TYPE>(1);
  773. else
  774. (*this)(i, j) = static_cast<DATA_TYPE>(0);
  775. }
  776. }
  777. return (*this);
  778. }
  779. template<typename DATA_TYPE, U32 rows, U32 cols>
  780. inline void Matrix<DATA_TYPE, rows, cols>::normalize()
  781. {
  782. AssertFatal(rows >= 3 && cols >= 3, "Normalize can only be applied 3x3 or more");
  783. Point3F col0, col1, col2;
  784. getColumn(0, &col0);
  785. getColumn(1, &col1);
  786. mCross(col0, col1, &col2);
  787. mCross(col2, col0, &col1);
  788. col0.normalize();
  789. col1.normalize();
  790. col2.normalize();
  791. setColumn(0, col0);
  792. setColumn(1, col1);
  793. setColumn(2, col2);
  794. }
  795. template<typename DATA_TYPE, U32 rows, U32 cols>
  796. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::scale(const Point3F& s)
  797. {
  798. // torques scale applies directly, does not create another matrix to multiply with the translation matrix.
  799. AssertFatal(rows >= 3 && cols >= 3, "Scale can only be applied 3x3 or more");
  800. for (U32 i = 0; i < 3; i++) {
  801. for (U32 j = 0; j < 3; j++) {
  802. DATA_TYPE scale = (i == 0) ? s.x : (i == 1) ? s.y : s.z;
  803. (*this)(i, j) *= scale;
  804. }
  805. }
  806. return (*this);
  807. }
  808. template<typename DATA_TYPE, U32 rows, U32 cols>
  809. inline bool Matrix<DATA_TYPE, rows, cols>::isIdentity() const {
  810. for (U32 i = 0; i < rows; i++) {
  811. for (U32 j = 0; j < cols; j++) {
  812. if (j == i) {
  813. if((*this)(i, j) != static_cast<DATA_TYPE>(1)) {
  814. return false;
  815. }
  816. }
  817. else {
  818. if((*this)(i, j) != static_cast<DATA_TYPE>(0)) {
  819. return false;
  820. }
  821. }
  822. }
  823. }
  824. return true;
  825. }
  826. template<typename DATA_TYPE, U32 rows, U32 cols>
  827. inline Point3F Matrix<DATA_TYPE, rows, cols>::getScale() const
  828. {
  829. // this function assumes the matrix has scale applied through the scale(const Point3F& s) function.
  830. // for now assume float since we have point3F.
  831. AssertFatal(rows >= 3 && cols >= 3, "Scale can only be applied 3x3 or more");
  832. Point3F scale;
  833. scale.x = mSqrt((*this)(0, 0) * (*this)(0, 0) + (*this)(1, 0) * (*this)(1, 0) + (*this)(2, 0) * (*this)(2, 0));
  834. scale.y = mSqrt((*this)(0, 1) * (*this)(0, 1) + (*this)(1, 1) * (*this)(1, 1) + (*this)(2, 1) * (*this)(2, 1));
  835. scale.z = mSqrt((*this)(0, 2) * (*this)(0, 2) + (*this)(1, 2) * (*this)(1, 2) + (*this)(2, 2) * (*this)(2, 2));
  836. return scale;
  837. }
  838. template<typename DATA_TYPE, U32 rows, U32 cols>
  839. inline Point3F Matrix<DATA_TYPE, rows, cols>::getPosition() const
  840. {
  841. Point3F pos;
  842. getColumn(3, &pos);
  843. return pos;
  844. }
  845. template<typename DATA_TYPE, U32 rows, U32 cols>
  846. inline void Matrix<DATA_TYPE, rows, cols>::getColumn(S32 col, Point4F* cptr) const
  847. {
  848. if (rows >= 2)
  849. {
  850. cptr->x = (*this)(0, col);
  851. cptr->y = (*this)(1, col);
  852. }
  853. if (rows >= 3)
  854. cptr->z = (*this)(2, col);
  855. else
  856. cptr->z = 0.0f;
  857. if (rows >= 4)
  858. cptr->w = (*this)(3, col);
  859. else
  860. cptr->w = 0.0f;
  861. }
  862. template<typename DATA_TYPE, U32 rows, U32 cols>
  863. inline void Matrix<DATA_TYPE, rows, cols>::getColumn(S32 col, Point3F* cptr) const
  864. {
  865. if (rows >= 2)
  866. {
  867. cptr->x = (*this)(0, col);
  868. cptr->y = (*this)(1, col);
  869. }
  870. if (rows >= 3)
  871. cptr->z = (*this)(2, col);
  872. else
  873. cptr->z = 0.0f;
  874. }
  875. template<typename DATA_TYPE, U32 rows, U32 cols>
  876. inline void Matrix<DATA_TYPE, rows, cols>::setColumn(S32 col, const Point4F &cptr) {
  877. if(rows >= 2)
  878. {
  879. (*this)(0, col) = cptr.x;
  880. (*this)(1, col) = cptr.y;
  881. }
  882. if(rows >= 3)
  883. (*this)(2, col) = cptr.z;
  884. if(rows >= 4)
  885. (*this)(3, col) = cptr.w;
  886. }
  887. template<typename DATA_TYPE, U32 rows, U32 cols>
  888. inline void Matrix<DATA_TYPE, rows, cols>::setColumn(S32 col, const Point3F &cptr) {
  889. if(rows >= 2)
  890. {
  891. (*this)(0, col) = cptr.x;
  892. (*this)(1, col) = cptr.y;
  893. }
  894. if(rows >= 3)
  895. (*this)(2, col) = cptr.z;
  896. }
  897. template<typename DATA_TYPE, U32 rows, U32 cols>
  898. inline void Matrix<DATA_TYPE, rows, cols>::getRow(S32 row, Point4F* cptr) const
  899. {
  900. if (cols >= 2)
  901. {
  902. cptr->x = (*this)(row, 0);
  903. cptr->y = (*this)(row, 1);
  904. }
  905. if (cols >= 3)
  906. cptr->z = (*this)(row, 2);
  907. else
  908. cptr->z = 0.0f;
  909. if (cols >= 4)
  910. cptr->w = (*this)(row, 3);
  911. else
  912. cptr->w = 0.0f;
  913. }
  914. template<typename DATA_TYPE, U32 rows, U32 cols>
  915. inline void Matrix<DATA_TYPE, rows, cols>::getRow(S32 row, Point3F* cptr) const
  916. {
  917. if (cols >= 2)
  918. {
  919. cptr->x = (*this)(row, 0);
  920. cptr->y = (*this)(row, 1);
  921. }
  922. if (cols >= 3)
  923. cptr->z = (*this)(row, 2);
  924. else
  925. cptr->z = 0.0f;
  926. }
  927. template<typename DATA_TYPE, U32 rows, U32 cols>
  928. inline VectorF Matrix<DATA_TYPE, rows, cols>::getRightVector() const
  929. {
  930. VectorF vec;
  931. getColumn(0, &vec);
  932. return vec;
  933. }
  934. template<typename DATA_TYPE, U32 rows, U32 cols>
  935. inline VectorF Matrix<DATA_TYPE, rows, cols>::getForwardVector() const
  936. {
  937. VectorF vec;
  938. getColumn(1, &vec);
  939. return vec;
  940. }
  941. template<typename DATA_TYPE, U32 rows, U32 cols>
  942. inline VectorF Matrix<DATA_TYPE, rows, cols>::getUpVector() const
  943. {
  944. VectorF vec;
  945. getColumn(2, &vec);
  946. return vec;
  947. }
  948. template<typename DATA_TYPE, U32 rows, U32 cols>
  949. inline void Matrix<DATA_TYPE, rows, cols>::invertTo(Matrix<DATA_TYPE, cols, rows>* matrix) const
  950. {
  951. Matrix<DATA_TYPE, rows, cols> invMatrix;
  952. for (U32 i = 0; i < rows; ++i) {
  953. for (U32 j = 0; j < cols; ++j) {
  954. invMatrix(i, j) = (*this)(i, j);
  955. }
  956. }
  957. invMatrix.inverse();
  958. for (U32 i = 0; i < rows; ++i) {
  959. for (U32 j = 0; j < cols; ++j) {
  960. (*matrix)(i, j) = invMatrix(i, j);
  961. }
  962. }
  963. }
  964. template<typename DATA_TYPE, U32 rows, U32 cols>
  965. inline void Matrix<DATA_TYPE, rows, cols>::invertTo(Matrix<DATA_TYPE, cols, rows>* matrix)
  966. {
  967. Matrix<DATA_TYPE, rows, cols> invMatrix = this->inverse();
  968. for (U32 i = 0; i < rows; ++i) {
  969. for (U32 j = 0; j < cols; ++j) {
  970. (*matrix)(i, j) = invMatrix(i, j);
  971. }
  972. }
  973. }
  974. template<typename DATA_TYPE, U32 rows, U32 cols>
  975. inline void Matrix<DATA_TYPE, rows, cols>::setRow(S32 row, const Point4F& cptr) {
  976. if(cols >= 2)
  977. {
  978. (*this)(row, 0) = cptr.x;
  979. (*this)(row, 1) = cptr.y;
  980. }
  981. if(cols >= 3)
  982. (*this)(row, 2) = cptr.z;
  983. if(cols >= 4)
  984. (*this)(row, 3) = cptr.w;
  985. }
  986. template<typename DATA_TYPE, U32 rows, U32 cols>
  987. inline void Matrix<DATA_TYPE, rows, cols>::setRow(S32 row, const Point3F& cptr) {
  988. if(cols >= 2)
  989. {
  990. (*this)(row, 0) = cptr.x;
  991. (*this)(row, 1) = cptr.y;
  992. }
  993. if(cols >= 3)
  994. (*this)(row, 2) = cptr.z;
  995. }
  996. template<typename DATA_TYPE, U32 rows, U32 cols>
  997. inline void Matrix<DATA_TYPE, rows, cols>::displace(const Point3F& delta)
  998. {
  999. (*this)(0, 3) += delta.x;
  1000. (*this)(1, 3) += delta.y;
  1001. (*this)(2, 3) += delta.z;
  1002. }
  1003. template<typename DATA_TYPE, U32 rows, U32 cols>
  1004. void Matrix<DATA_TYPE, rows, cols>::reverseProjection()
  1005. {
  1006. AssertFatal(rows == 4 && cols == 4, "reverseProjection requires a 4x4 matrix.");
  1007. (*this)(2, 0) = (*this)(3, 0) - (*this)(2, 0);
  1008. (*this)(2, 1) = (*this)(3, 1) - (*this)(2, 1);
  1009. (*this)(2, 2) = (*this)(3, 2) - (*this)(2, 2);
  1010. (*this)(2, 3) = (*this)(3, 3) - (*this)(2, 3);
  1011. }
  1012. template<typename DATA_TYPE, U32 rows, U32 cols>
  1013. const Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::Identity = []() {
  1014. Matrix<DATA_TYPE, rows, cols> identity(true);
  1015. return identity;
  1016. }();
  1017. template<typename DATA_TYPE, U32 rows, U32 cols>
  1018. Matrix<DATA_TYPE, rows, cols>::Matrix(const EulerF& e)
  1019. {
  1020. set(e);
  1021. }
  1022. template<typename DATA_TYPE, U32 rows, U32 cols>
  1023. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::set(const EulerF& e)
  1024. {
  1025. // when the template refactor is done, euler will be able to be setup in different ways
  1026. AssertFatal(rows >= 3 && cols >= 3, "EulerF can only initialize 3x3 or more");
  1027. static_assert(std::is_same<DATA_TYPE, float>::value, "Can only initialize eulers with floats for now");
  1028. F32 cosPitch, sinPitch;
  1029. mSinCos(e.x, sinPitch, cosPitch);
  1030. F32 cosYaw, sinYaw;
  1031. mSinCos(e.y, sinYaw, cosYaw);
  1032. F32 cosRoll, sinRoll;
  1033. mSinCos(e.z, sinRoll, cosRoll);
  1034. enum {
  1035. AXIS_X = (1 << 0),
  1036. AXIS_Y = (1 << 1),
  1037. AXIS_Z = (1 << 2)
  1038. };
  1039. U32 axis = 0;
  1040. if (e.x != 0.0f) axis |= AXIS_X;
  1041. if (e.y != 0.0f) axis |= AXIS_Y;
  1042. if (e.z != 0.0f) axis |= AXIS_Z;
  1043. switch (axis) {
  1044. case 0:
  1045. (*this) = Matrix<DATA_TYPE, rows, cols>(true);
  1046. break;
  1047. case AXIS_X:
  1048. (*this)(0, 0) = 1.0f; (*this)(1, 0) = 0.0f; (*this)(2, 0) = 0.0f;
  1049. (*this)(0, 1) = 0.0f; (*this)(1, 1) = cosPitch; (*this)(2, 1) = -sinPitch;
  1050. (*this)(0, 2) = 0.0f; (*this)(1, 2) = sinPitch; (*this)(2, 2) = cosPitch;
  1051. break;
  1052. case AXIS_Y:
  1053. (*this)(0, 0) = cosYaw; (*this)(1, 0) = 0.0f; (*this)(2, 0) = sinYaw;
  1054. (*this)(0, 1) = 0.0f; (*this)(1, 1) = 1.0f; (*this)(2, 1) = 0.0f;
  1055. (*this)(0, 2) = -sinYaw; (*this)(1, 2) = 0.0f; (*this)(2, 2) = cosYaw;
  1056. break;
  1057. case AXIS_Z:
  1058. (*this)(0, 0) = cosRoll; (*this)(1, 0) = -sinRoll; (*this)(2, 0) = 0.0f;
  1059. (*this)(0, 1) = sinRoll; (*this)(1, 1) = cosRoll; (*this)(2, 1) = 0.0f;
  1060. (*this)(0, 2) = 0.0f; (*this)(1, 2) = 0.0f; (*this)(2, 2) = 0.0f;
  1061. break;
  1062. default:
  1063. F32 r1 = cosYaw * cosRoll;
  1064. F32 r2 = cosYaw * sinRoll;
  1065. F32 r3 = sinYaw * cosRoll;
  1066. F32 r4 = sinYaw * sinRoll;
  1067. // the matrix looks like this:
  1068. // r1 - (r4 * sin(x)) r2 + (r3 * sin(x)) -cos(x) * sin(y)
  1069. // -cos(x) * sin(z) cos(x) * cos(z) sin(x)
  1070. // r3 + (r2 * sin(x)) r4 - (r1 * sin(x)) cos(x) * cos(y)
  1071. //
  1072. // where:
  1073. // r1 = cos(y) * cos(z)
  1074. // r2 = cos(y) * sin(z)
  1075. // r3 = sin(y) * cos(z)
  1076. // r4 = sin(y) * sin(z)
  1077. // init the euler 3x3 rotation matrix.
  1078. (*this)(0, 0) = r1 - (r4 * sinPitch); (*this)(1, 0) = -cosPitch * sinRoll; (*this)(2, 0) = r3 + (r2 * sinPitch);
  1079. (*this)(0, 1) = r2 + (r3 * sinPitch); (*this)(1, 1) = cosPitch * cosRoll; (*this)(2, 1) = r4 - (r1 * sinPitch);
  1080. (*this)(0, 2) = -cosPitch * sinYaw; (*this)(1, 2) = sinPitch; (*this)(2, 2) = cosPitch * cosYaw;
  1081. break;
  1082. }
  1083. if (rows == 4) {
  1084. (*this)(3, 0) = 0.0f;
  1085. (*this)(3, 1) = 0.0f;
  1086. (*this)(3, 2) = 0.0f;
  1087. }
  1088. if (cols == 4) {
  1089. (*this)(0, 3) = 0.0f;
  1090. (*this)(1, 3) = 0.0f;
  1091. (*this)(2, 3) = 0.0f;
  1092. }
  1093. if (rows == 4 && cols == 4) {
  1094. (*this)(3, 3) = 1.0f;
  1095. }
  1096. return(*this);
  1097. }
  1098. template<typename DATA_TYPE, U32 rows, U32 cols>
  1099. Matrix<DATA_TYPE, rows, cols>::Matrix(const EulerF& e, const Point3F p)
  1100. {
  1101. set(e, p);
  1102. }
  1103. template<typename DATA_TYPE, U32 rows, U32 cols>
  1104. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::set(const EulerF& e, const Point3F p)
  1105. {
  1106. AssertFatal(rows >= 3 && cols >= 4, "Euler and Point can only initialize 3x4 or more");
  1107. // call set euler, this already sets the last row if it exists.
  1108. set(e);
  1109. // does this need to multiply with the result of the euler? or are we just setting position.
  1110. (*this)(0, 3) = p.x;
  1111. (*this)(1, 3) = p.y;
  1112. (*this)(2, 3) = p.z;
  1113. return (*this);
  1114. }
  1115. template<typename DATA_TYPE, U32 rows, U32 cols>
  1116. Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::inverse()
  1117. {
  1118. // TODO: insert return statement here
  1119. AssertFatal(rows == cols, "Can only perform inverse on square matrices.");
  1120. const U32 size = rows;
  1121. // Create augmented matrix [this | I]
  1122. Matrix<DATA_TYPE, size, 2 * size> augmentedMatrix;
  1123. Matrix<DATA_TYPE, size, size> resultMatrix;
  1124. for (U32 i = 0; i < size; i++) {
  1125. for (U32 j = 0; j < size; j++) {
  1126. augmentedMatrix(i, j) = (*this)(i, j);
  1127. augmentedMatrix(i, j + size) = (i == j) ? static_cast<DATA_TYPE>(1) : static_cast<DATA_TYPE>(0);
  1128. }
  1129. }
  1130. // Apply gauss-joran elimination
  1131. for (U32 i = 0; i < size; i++) {
  1132. U32 pivotRow = i;
  1133. for (U32 k = i + 1; k < size; k++) {
  1134. // use std::abs until the templated math functions are in place.
  1135. if (std::abs(augmentedMatrix(k, i)) > std::abs(augmentedMatrix(pivotRow, i))) {
  1136. pivotRow = k;
  1137. }
  1138. }
  1139. // Swap if needed.
  1140. if (i != pivotRow) {
  1141. for (U32 j = 0; j < 2 * size; j++) {
  1142. std::swap(augmentedMatrix(i, j), augmentedMatrix(pivotRow, j));
  1143. }
  1144. }
  1145. // Early out if pivot is 0, return identity matrix.
  1146. if (augmentedMatrix(i, i) == static_cast<DATA_TYPE>(0)) {
  1147. return Matrix<DATA_TYPE, rows, cols>(true);
  1148. }
  1149. DATA_TYPE pivotVal = augmentedMatrix(i, i);
  1150. // scale the pivot
  1151. for (U32 j = 0; j < 2 * size; j++) {
  1152. augmentedMatrix(i, j) /= pivotVal;
  1153. }
  1154. // Eliminate the current column in all other rows
  1155. for (U32 k = 0; k < size; k++) {
  1156. if (k != i) {
  1157. DATA_TYPE factor = augmentedMatrix(k, i);
  1158. for (U32 j = 0; j < 2 * size; j++) {
  1159. augmentedMatrix(k, j) -= factor * augmentedMatrix(i, j);
  1160. }
  1161. }
  1162. }
  1163. }
  1164. for (U32 i = 0; i < size; i++) {
  1165. for (U32 j = 0; j < size; j++) {
  1166. resultMatrix(i, j) = augmentedMatrix(i, j + size);
  1167. }
  1168. }
  1169. return resultMatrix;
  1170. }
  1171. template<typename DATA_TYPE, U32 rows, U32 cols>
  1172. inline bool Matrix<DATA_TYPE, rows, cols>::fullInverse()
  1173. {
  1174. Matrix<DATA_TYPE, rows, cols> inv = this->inverse();
  1175. if (inv.isIdentity())
  1176. return false;
  1177. *this = inv;
  1178. return true;
  1179. }
  1180. template<typename DATA_TYPE, U32 rows, U32 cols>
  1181. inline void Matrix<DATA_TYPE, rows, cols>::invert()
  1182. {
  1183. (*this) = inverse();
  1184. }
  1185. template<typename DATA_TYPE, U32 rows, U32 cols>
  1186. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::setCrossProduct(const Point3F& p)
  1187. {
  1188. AssertFatal(rows == 4 && cols == 4, "Cross product only supported on 4x4 for now");
  1189. (*this)(0, 0) = 0;
  1190. (*this)(0, 1) = -p.z;
  1191. (*this)(0, 2) = p.y;
  1192. (*this)(0, 3) = 0;
  1193. (*this)(1, 0) = p.z;
  1194. (*this)(1, 1) = 0;
  1195. (*this)(1, 2) = -p.x;
  1196. (*this)(1, 3) = 0;
  1197. (*this)(2, 0) = -p.y;
  1198. (*this)(2, 1) = p.x;
  1199. (*this)(2, 2) = 0;
  1200. (*this)(2, 3) = 0;
  1201. (*this)(3, 0) = 0;
  1202. (*this)(3, 1) = 0;
  1203. (*this)(3, 2) = 0;
  1204. (*this)(3, 3) = 1;
  1205. return (*this);
  1206. }
  1207. template<typename DATA_TYPE, U32 rows, U32 cols>
  1208. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::setTensorProduct(const Point3F& p, const Point3F& q)
  1209. {
  1210. AssertFatal(rows == 4 && cols == 4, "Tensor product only supported on 4x4 for now");
  1211. (*this)(0, 0) = p.x * q.x;
  1212. (*this)(0, 1) = p.x * q.y;
  1213. (*this)(0, 2) = p.x * q.z;
  1214. (*this)(0, 3) = 0;
  1215. (*this)(1, 0) = p.y * q.x;
  1216. (*this)(1, 1) = p.y * q.y;
  1217. (*this)(1, 2) = p.y * q.z;
  1218. (*this)(1, 3) = 0;
  1219. (*this)(2, 0) = p.z * q.x;
  1220. (*this)(2, 1) = p.z * q.y;
  1221. (*this)(2, 2) = p.z * q.z;
  1222. (*this)(2, 3) = 0;
  1223. (*this)(3, 0) = 0;
  1224. (*this)(3, 1) = 0;
  1225. (*this)(3, 2) = 0;
  1226. (*this)(3, 3) = 1;
  1227. return (*this);
  1228. }
  1229. template<typename DATA_TYPE, U32 rows, U32 cols>
  1230. inline void Matrix<DATA_TYPE, rows, cols>::mul(Box3F& box) const
  1231. {
  1232. AssertFatal(rows == 4 && cols == 4, "Multiplying Box3F with matrix requires 4x4");
  1233. // Create an array of all 8 corners of the box
  1234. Point3F corners[8] = {
  1235. Point3F(box.minExtents.x, box.minExtents.y, box.minExtents.z),
  1236. Point3F(box.minExtents.x, box.minExtents.y, box.maxExtents.z),
  1237. Point3F(box.minExtents.x, box.maxExtents.y, box.minExtents.z),
  1238. Point3F(box.minExtents.x, box.maxExtents.y, box.maxExtents.z),
  1239. Point3F(box.maxExtents.x, box.minExtents.y, box.minExtents.z),
  1240. Point3F(box.maxExtents.x, box.minExtents.y, box.maxExtents.z),
  1241. Point3F(box.maxExtents.x, box.maxExtents.y, box.minExtents.z),
  1242. Point3F(box.maxExtents.x, box.maxExtents.y, box.maxExtents.z),
  1243. };
  1244. for (U32 i = 0; i < 8; i++) {
  1245. corners[i] = (*this) * corners[i];
  1246. }
  1247. box.minExtents = corners[0];
  1248. box.maxExtents = corners[0];
  1249. for (U32 i = 1; i < 8; ++i) {
  1250. box.minExtents.x = mMin(box.minExtents.x, corners[i].x);
  1251. box.minExtents.y = mMin(box.minExtents.y, corners[i].y);
  1252. box.minExtents.z = mMin(box.minExtents.z, corners[i].z);
  1253. box.maxExtents.x = mMax(box.maxExtents.x, corners[i].x);
  1254. box.maxExtents.y = mMax(box.maxExtents.y, corners[i].y);
  1255. box.maxExtents.z = mMax(box.maxExtents.z, corners[i].z);
  1256. }
  1257. }
  1258. template<typename DATA_TYPE, U32 rows, U32 cols>
  1259. inline bool Matrix<DATA_TYPE, rows, cols>::isAffine() const
  1260. {
  1261. if ((*this)(rows - 1, cols - 1) != 1.0f) {
  1262. return false;
  1263. }
  1264. for (U32 col = 0; col < cols - 1; ++col) {
  1265. if ((*this)(rows - 1, col) != 0.0f) {
  1266. return false;
  1267. }
  1268. }
  1269. Point3F one, two, three;
  1270. getColumn(0, &one);
  1271. getColumn(1, &two);
  1272. getColumn(2, &three);
  1273. // check columns
  1274. {
  1275. if (mDot(one, two) > 0.0001f ||
  1276. mDot(one, three) > 0.0001f ||
  1277. mDot(two, three) > 0.0001f)
  1278. return false;
  1279. if (mFabs(1.0f - one.lenSquared()) > 0.0001f ||
  1280. mFabs(1.0f - two.lenSquared()) > 0.0001f ||
  1281. mFabs(1.0f - three.lenSquared()) > 0.0001f)
  1282. return false;
  1283. }
  1284. getRow(0, &one);
  1285. getRow(1, &two);
  1286. getRow(2, &three);
  1287. // check rows
  1288. {
  1289. if (mDot(one, two) > 0.0001f ||
  1290. mDot(one, three) > 0.0001f ||
  1291. mDot(two, three) > 0.0001f)
  1292. return false;
  1293. if (mFabs(1.0f - one.lenSquared()) > 0.0001f ||
  1294. mFabs(1.0f - two.lenSquared()) > 0.0001f ||
  1295. mFabs(1.0f - three.lenSquared()) > 0.0001f)
  1296. return false;
  1297. }
  1298. return true;
  1299. }
  1300. template<typename DATA_TYPE, U32 rows, U32 cols>
  1301. inline Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::affineInverse()
  1302. {
  1303. AssertFatal(rows >= 4 && cols >= 4, "affineInverse requires at least 4x4");
  1304. Matrix<DATA_TYPE, 3, 3> subMatrix;
  1305. for (U32 i = 0; i < 3; i++) {
  1306. for (U32 j = 0; j < 3; j++) {
  1307. subMatrix(i, j) = (*this)(i, j);
  1308. }
  1309. }
  1310. subMatrix.transpose();
  1311. Point3F pos = getPosition();
  1312. (*this)(0, 3) = mDot(subMatrix.getColumn3F(0), pos);
  1313. (*this)(1, 3) = mDot(subMatrix.getColumn3F(1), pos);
  1314. (*this)(2, 3) = mDot(subMatrix.getColumn3F(2), pos);
  1315. return *this;
  1316. }
  1317. template<typename DATA_TYPE, U32 rows, U32 cols>
  1318. inline EulerF Matrix<DATA_TYPE, rows, cols>::toEuler() const
  1319. {
  1320. AssertFatal(rows >= 3 && cols >= 3, "Euler rotations require at least a 3x3 matrix.");
  1321. // Extract rotation matrix components
  1322. const DATA_TYPE m00 = (*this)(0, 0);
  1323. const DATA_TYPE m01 = (*this)(0, 1);
  1324. const DATA_TYPE m02 = (*this)(0, 2);
  1325. const DATA_TYPE m10 = (*this)(1, 0);
  1326. const DATA_TYPE m11 = (*this)(1, 1);
  1327. const DATA_TYPE m21 = (*this)(2, 1);
  1328. const DATA_TYPE m22 = (*this)(2, 2);
  1329. // like all others assume float for now.
  1330. EulerF r;
  1331. r.x = mAsin(mClampF(m21, -1.0, 1.0));
  1332. if (mCos(r.x) != 0.0f) {
  1333. r.y = mAtan2(-m02, m22); // yaw
  1334. r.z = mAtan2(-m10, m11); // roll
  1335. }
  1336. else {
  1337. r.y = 0.0f;
  1338. r.z = mAtan2(m01, m00); // this rolls when pitch is +90 degrees
  1339. }
  1340. return r;
  1341. }
  1342. template<typename DATA_TYPE, U32 rows, U32 cols>
  1343. inline void Matrix<DATA_TYPE, rows, cols>::dumpMatrix(const char* caption) const
  1344. {
  1345. U32 size = (caption == NULL) ? 0 : dStrlen(caption);
  1346. FrameTemp<char> spacer(size + 1);
  1347. char* spacerRef = spacer;
  1348. // is_floating_point should return true for floats and doubles.
  1349. const char* formatSpec = std::is_floating_point_v<DATA_TYPE> ? " %-8.4f" : " %d";
  1350. dMemset(spacerRef, ' ', size);
  1351. // null terminate.
  1352. spacerRef[size] = '\0';
  1353. /*Con::printf("%s = | %-8.4f %-8.4f %-8.4f %-8.4f |", caption, m[idx(0, 0)], m[idx(0, 1)], m[idx(0, 2)], m[idx(0, 3)]);
  1354. Con::printf("%s | %-8.4f %-8.4f %-8.4f %-8.4f |", spacerRef, m[idx(1, 0)], m[idx(1, 1)], m[idx(1, 2)], m[idx(1, 3)]);
  1355. Con::printf("%s | %-8.4f %-8.4f %-8.4f %-8.4f |", spacerRef, m[idx(2, 0)], m[idx(2, 1)], m[idx(2, 2)], m[idx(2, 3)]);
  1356. Con::printf("%s | %-8.4f %-8.4f %-8.4f %-8.4f |", spacerRef, m[idx(3, 0)], m[idx(3, 1)], m[idx(3, 2)], m[idx(3, 3)]);*/
  1357. StringBuilder str;
  1358. str.format("%s = |", caption);
  1359. for (U32 i = 0; i < rows; i++) {
  1360. if (i > 0) {
  1361. str.append(spacerRef);
  1362. }
  1363. for (U32 j = 0; j < cols; j++) {
  1364. str.format(formatSpec, (*this)(i, j));
  1365. }
  1366. str.append(" |\n");
  1367. }
  1368. Con::printf("%s", str.end().c_str());
  1369. }
  1370. //------------------------------------
  1371. // Non-member methods
  1372. //------------------------------------
  1373. template<typename DATA_TYPE, std::size_t Rows, std::size_t Cols>
  1374. inline void mTransformPlane(
  1375. const Matrix<DATA_TYPE, Rows, Cols>& mat,
  1376. const Point3F& scale,
  1377. const PlaneF& plane,
  1378. PlaneF* result
  1379. ) {
  1380. AssertFatal(Rows == 4 && Cols == 4, "Matrix must be 4x4");
  1381. // Create a non-const copy of the matrix
  1382. Matrix<float, 4, 4> matCopy = mat;
  1383. // Create the inverse scale matrix
  1384. Matrix<DATA_TYPE, 4, 4> invScale = Matrix<DATA_TYPE, 4, 4>::Identity;
  1385. invScale(0, 0) = 1.0f / scale.x;
  1386. invScale(1, 1) = 1.0f / scale.y;
  1387. invScale(2, 2) = 1.0f / scale.z;
  1388. // Compute the inverse transpose of the matrix
  1389. Matrix<DATA_TYPE, 4, 4> invTrMatrix = matCopy.transpose().affineInverse() * invScale;
  1390. // Transform the plane normal
  1391. Point3F norm(plane.x, plane.y, plane.z);
  1392. norm = invTrMatrix * norm;
  1393. float normLength = std::sqrt(norm.x * norm.x + norm.y * norm.y + norm.z * norm.z);
  1394. norm.x /= normLength;
  1395. norm.y /= normLength;
  1396. norm.z /= normLength;
  1397. // Transform the plane point
  1398. Point3F point = norm * (-plane.d);
  1399. Matrix<DATA_TYPE, 4, 4> temp = mat;
  1400. point.x *= scale.x;
  1401. point.y *= scale.y;
  1402. point.z *= scale.z;
  1403. point = temp * point;
  1404. // Recompute the plane distance
  1405. PlaneF resultPlane(point, norm);
  1406. result->x = resultPlane.x;
  1407. result->y = resultPlane.y;
  1408. result->z = resultPlane.z;
  1409. result->d = resultPlane.d;
  1410. }
  1411. //--------------------------------------------
  1412. // INLINE FUNCTIONS END
  1413. //--------------------------------------------
  1414. typedef Matrix<F32, 4, 4> MatrixF;
  1415. class MatrixTemplateExport
  1416. {
  1417. public:
  1418. template <typename T, U32 rows, U32 cols>
  1419. static EngineFieldTable::Field getMatrixField();
  1420. };
  1421. template<typename T, U32 rows, U32 cols>
  1422. inline EngineFieldTable::Field MatrixTemplateExport::getMatrixField()
  1423. {
  1424. typedef Matrix<T, rows, cols> ThisType;
  1425. return _FIELD_AS(T, data, data, rows * cols, "");
  1426. }
  1427. #endif // !USE_TEMPLATE_MATRIX
  1428. #endif //_MMATRIX_H_