mMatrix.h 58 KB

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