mMatrix.h 57 KB

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