mMatrix.h 48 KB

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