mMatrix.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  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. /// Make this an identity matrix.
  549. Matrix<DATA_TYPE, rows, cols>& identity();
  550. void reverseProjection();
  551. void normalize();
  552. Matrix<DATA_TYPE, rows, cols>& set(const EulerF& e);
  553. Matrix(const EulerF& e, const Point3F p);
  554. Matrix<DATA_TYPE, rows, cols>& set(const EulerF& e, const Point3F p);
  555. Matrix<DATA_TYPE, rows, cols> inverse();
  556. Matrix<DATA_TYPE, rows, cols>& transpose();
  557. void invert();
  558. Matrix<DATA_TYPE, rows, cols>& setCrossProduct(const Point3F& p);
  559. Matrix<DATA_TYPE, rows, cols>& setTensorProduct(const Point3F& p, const Point3F& q);
  560. /// M * Matrix(p) -> M
  561. Matrix<DATA_TYPE, rows, cols>& scale(const Point3F& s);
  562. Matrix<DATA_TYPE, rows, cols>& scale(DATA_TYPE s) { return scale(Point3F(s, s, s)); }
  563. void setColumn(S32 col, const Point4F& cptr);
  564. void setColumn(S32 col, const Point3F& cptr);
  565. void setRow(S32 row, const Point4F& cptr);
  566. void setRow(S32 row, const Point3F& cptr);
  567. void displace(const Point3F& delta);
  568. bool fullInverse();
  569. void setPosition(const Point3F& pos) { setColumn(3, pos); }
  570. ///< M * a -> M
  571. Matrix<DATA_TYPE, rows, cols>& mul(const Matrix<DATA_TYPE, rows, cols>& a)
  572. {
  573. *this = *this * a; return *this;
  574. }
  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 * 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. ///< M * p -> p (full [4x4] * [1x4])
  588. void mul(Point4F& p) const { p = *this * p; }
  589. ///< M * p -> p (assume w = 1.0f)
  590. void mulP(Point3F& p) const { p = *this * p; }
  591. ///< M * p -> d (assume w = 1.0f)
  592. void mulP(const Point3F& p, Point3F* d) const { *d = *this * p; }
  593. ///< M * v -> v (assume w = 0.0f)
  594. void mulV(VectorF& v) const
  595. {
  596. AssertFatal(rows == 4 && cols == 4, "Multiplying VectorF with matrix requires 4x4");
  597. VectorF result(
  598. (*this)(0, 0) * v.x + (*this)(0, 1) * v.y + (*this)(0, 2) * v.z,
  599. (*this)(1, 0) * v.x + (*this)(1, 1) * v.y + (*this)(1, 2) * v.z,
  600. (*this)(2, 0) * v.x + (*this)(2, 1) * v.y + (*this)(2, 2) * v.z
  601. );
  602. v = result;
  603. }
  604. ///< M * v -> d (assume w = 0.0f)
  605. void mulV(const VectorF& v, Point3F* d) const
  606. {
  607. AssertFatal(rows == 4 && cols == 4, "Multiplying VectorF with matrix requires 4x4");
  608. VectorF result(
  609. (*this)(0, 0) * v.x + (*this)(0, 1) * v.y + (*this)(0, 2) * v.z,
  610. (*this)(1, 0) * v.x + (*this)(1, 1) * v.y + (*this)(1, 2) * v.z,
  611. (*this)(2, 0) * v.x + (*this)(2, 1) * v.y + (*this)(2, 2) * v.z
  612. );
  613. d->x = result.x;
  614. d->y = result.y;
  615. d->z = result.z;
  616. }
  617. ///< Axial box -> Axial Box (too big a function to be inline)
  618. void mul(Box3F& box) const;
  619. // ------ Getters ------
  620. bool isNaN() {
  621. for (U32 i = 0; i < rows; i++) {
  622. for (U32 j = 0; j < cols; j++) {
  623. if (mIsNaN_F((*this)(i, j)))
  624. return true;
  625. }
  626. }
  627. return false;
  628. }
  629. // row + col * cols
  630. static U32 idx(U32 i, U32 j) { return (i + j * cols); }
  631. bool isAffine() const;
  632. bool isIdentity() const;
  633. /// Take inverse of matrix assuming it is affine (rotation,
  634. /// scale, sheer, translation only).
  635. Matrix<DATA_TYPE, rows, cols> affineInverse();
  636. Point3F getScale() const;
  637. EulerF toEuler() const;
  638. Point3F getPosition() const;
  639. void getColumn(S32 col, Point4F* cptr) const;
  640. Point4F getColumn4F(S32 col) const { Point4F ret; getColumn(col, &ret); return ret; }
  641. void getColumn(S32 col, Point3F* cptr) const;
  642. Point3F getColumn3F(S32 col) const { Point3F ret; getColumn(col, &ret); return ret; }
  643. void getRow(S32 row, Point4F* cptr) const;
  644. Point4F getRow4F(S32 row) const { Point4F ret; getRow(row, &ret); return ret; }
  645. void getRow(S32 row, Point3F* cptr) const;
  646. Point3F getRow3F(S32 row) const { Point3F ret; getRow(row, &ret); return ret; }
  647. VectorF getRightVector() const;
  648. VectorF getForwardVector() const;
  649. VectorF getUpVector() const;
  650. DATA_TYPE* getData() {
  651. return data;
  652. }
  653. const DATA_TYPE* getData() const {
  654. return data;
  655. }
  656. void transposeTo(Matrix<DATA_TYPE, cols, rows>& matrix) const {
  657. for (U32 i = 0; i < rows; ++i) {
  658. for (U32 j = 0; j < cols; ++j) {
  659. matrix(j, i) = (*this)(i, j);
  660. }
  661. }
  662. }
  663. void invertTo(Matrix<DATA_TYPE, cols, rows>* matrix) const;
  664. void invertTo(Matrix<DATA_TYPE, cols, rows>* matrix);
  665. void dumpMatrix(const char* caption = NULL) const;
  666. // Static identity matrix
  667. static const Matrix Identity;
  668. // ------ Operators ------
  669. Matrix<DATA_TYPE, rows, cols> operator * (const Matrix<DATA_TYPE, rows, cols>& other) const {
  670. Matrix<DATA_TYPE, rows, cols> result;
  671. for (U32 i = 0; i < rows; i++) {
  672. for (U32 j = 0; j < cols; j++) {
  673. result(i, j) = 0;
  674. for (U32 k = 0; k < cols; k++) {
  675. result(i, j) += (*this)(i, k) * other(k, j);
  676. }
  677. }
  678. }
  679. return result;
  680. }
  681. Matrix<DATA_TYPE, rows, cols> operator *= (const Matrix<DATA_TYPE, rows, cols>& other) {
  682. *this = *this * other;
  683. return *this;
  684. }
  685. Matrix<DATA_TYPE, rows, cols> operator * (const DATA_TYPE scalar) const {
  686. Matrix<DATA_TYPE, rows, cols> result;
  687. for (U32 i = 0; i < rows; i++) {
  688. for (U32 j = 0; j < cols; j++) {
  689. result(i, j) = (*this)(i, j) * scalar;
  690. }
  691. }
  692. return result;
  693. }
  694. Matrix<DATA_TYPE, rows, cols>& operator *= (const DATA_TYPE scalar) {
  695. for (U32 i = 0; i < rows; i++) {
  696. for (U32 j = 0; j < cols; j++) {
  697. (*this)(i, j) *= scalar;
  698. }
  699. }
  700. return *this;
  701. }
  702. Point3F operator*(const Point3F& point) const {
  703. AssertFatal(rows == 4 && cols == 4, "Multiplying point3 with matrix requires 4x4");
  704. return Point3F(
  705. (*this)(0, 0) * point.x + (*this)(0, 1) * point.y + (*this)(0, 2) * point.z + (*this)(0, 3),
  706. (*this)(1, 0) * point.x + (*this)(1, 1) * point.y + (*this)(1, 2) * point.z + (*this)(1, 3),
  707. (*this)(2, 0) * point.x + (*this)(2, 1) * point.y + (*this)(2, 2) * point.z + (*this)(2, 3)
  708. );
  709. }
  710. Point4F operator*(const Point4F& point) const {
  711. AssertFatal(rows == 4 && cols == 4, "Multiplying point4 with matrix requires 4x4");
  712. return Point4F(
  713. (*this)(0, 0) * point.x + (*this)(0, 1) * point.y + (*this)(0, 2) * point.z + (*this)(0, 3) * point.w,
  714. (*this)(1, 0) * point.x + (*this)(1, 1) * point.y + (*this)(1, 2) * point.z + (*this)(1, 3) * point.w,
  715. (*this)(2, 0) * point.x + (*this)(2, 1) * point.y + (*this)(2, 2) * point.z + (*this)(2, 3) * point.w,
  716. (*this)(3, 0) * point.x + (*this)(3, 1) * point.y + (*this)(3, 2) * point.z + (*this)(3, 3) * point.w
  717. );
  718. }
  719. Matrix<DATA_TYPE, rows, cols>& operator = (const Matrix<DATA_TYPE, rows, cols>& other) {
  720. if (this != &other) {
  721. std::copy(other.data, other.data + rows * cols, this->data);
  722. }
  723. return *this;
  724. }
  725. bool operator == (const Matrix<DATA_TYPE, rows, cols>& other) const {
  726. for (U32 i = 0; i < rows; i++) {
  727. for (U32 j = 0; j < cols; j++) {
  728. if ((*this)(i, j) != other(i, j))
  729. return false;
  730. }
  731. }
  732. return true;
  733. }
  734. bool operator != (const Matrix<DATA_TYPE, rows, cols>& other) const {
  735. return !(*this == other);
  736. }
  737. operator DATA_TYPE* () { return (data); }
  738. operator const DATA_TYPE* () const { return (DATA_TYPE*)(data); }
  739. DATA_TYPE& operator () (U32 row, U32 col) {
  740. if (row >= rows || col >= cols)
  741. AssertFatal(false, "Matrix indices out of range");
  742. return data[idx(col,row)];
  743. }
  744. const DATA_TYPE& operator () (U32 row, U32 col) const {
  745. if (row >= rows || col >= cols)
  746. AssertFatal(false, "Matrix indices out of range");
  747. return data[idx(col, row)];
  748. }
  749. };
  750. //--------------------------------------------
  751. // INLINE FUNCTIONS
  752. //--------------------------------------------
  753. template<typename DATA_TYPE, U32 rows, U32 cols>
  754. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::transpose()
  755. {
  756. Matrix<DATA_TYPE, rows, cols> result;
  757. for (U32 i = 0; i < rows; i++) {
  758. for (U32 j = 0; j < cols; j++) {
  759. result(j, i) = (*this)(i, j);
  760. }
  761. }
  762. std::copy(std::begin(result.data), std::end(result.data), std::begin(data));
  763. return (*this);
  764. }
  765. template<typename DATA_TYPE, U32 rows, U32 cols>
  766. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::identity()
  767. {
  768. for (U32 i = 0; i < rows; i++) {
  769. for (U32 j = 0; j < cols; j++) {
  770. if (j == i)
  771. (*this)(i, j) = static_cast<DATA_TYPE>(1);
  772. else
  773. (*this)(i, j) = static_cast<DATA_TYPE>(0);
  774. }
  775. }
  776. return (*this);
  777. }
  778. template<typename DATA_TYPE, U32 rows, U32 cols>
  779. inline void Matrix<DATA_TYPE, rows, cols>::normalize()
  780. {
  781. AssertFatal(rows >= 3 && cols >= 3, "Normalize can only be applied 3x3 or more");
  782. Point3F col0, col1, col2;
  783. getColumn(0, &col0);
  784. getColumn(1, &col1);
  785. mCross(col0, col1, &col2);
  786. mCross(col2, col0, &col1);
  787. col0.normalize();
  788. col1.normalize();
  789. col2.normalize();
  790. setColumn(0, col0);
  791. setColumn(1, col1);
  792. setColumn(2, col2);
  793. }
  794. template<typename DATA_TYPE, U32 rows, U32 cols>
  795. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::scale(const Point3F& s)
  796. {
  797. // torques scale applies directly, does not create another matrix to multiply with the translation matrix.
  798. AssertFatal(rows >= 3 && cols >= 3, "Scale can only be applied 3x3 or more");
  799. for (U32 i = 0; i < 3; i++) {
  800. for (U32 j = 0; j < 3; j++) {
  801. DATA_TYPE scale = (i == 0) ? s.x : (i == 1) ? s.y : s.z;
  802. (*this)(i, j) *= scale;
  803. }
  804. }
  805. return (*this);
  806. }
  807. template<typename DATA_TYPE, U32 rows, U32 cols>
  808. inline bool Matrix<DATA_TYPE, rows, cols>::isIdentity() const {
  809. for (U32 i = 0; i < rows; i++) {
  810. for (U32 j = 0; j < cols; j++) {
  811. if (j == i) {
  812. if((*this)(i, j) != static_cast<DATA_TYPE>(1)) {
  813. return false;
  814. }
  815. }
  816. else {
  817. if((*this)(i, j) != static_cast<DATA_TYPE>(0)) {
  818. return false;
  819. }
  820. }
  821. }
  822. }
  823. return true;
  824. }
  825. template<typename DATA_TYPE, U32 rows, U32 cols>
  826. inline Point3F Matrix<DATA_TYPE, rows, cols>::getScale() const
  827. {
  828. // this function assumes the matrix has scale applied through the scale(const Point3F& s) function.
  829. // for now assume float since we have point3F.
  830. AssertFatal(rows >= 3 && cols >= 3, "Scale can only be applied 3x3 or more");
  831. Point3F scale;
  832. scale.x = mSqrt((*this)(0, 0) * (*this)(0, 0) + (*this)(1, 0) * (*this)(1, 0) + (*this)(2, 0) * (*this)(2, 0));
  833. scale.y = mSqrt((*this)(0, 1) * (*this)(0, 1) + (*this)(1, 1) * (*this)(1, 1) + (*this)(2, 1) * (*this)(2, 1));
  834. scale.z = mSqrt((*this)(0, 2) * (*this)(0, 2) + (*this)(1, 2) * (*this)(1, 2) + (*this)(2, 2) * (*this)(2, 2));
  835. return scale;
  836. }
  837. template<typename DATA_TYPE, U32 rows, U32 cols>
  838. inline Point3F Matrix<DATA_TYPE, rows, cols>::getPosition() const
  839. {
  840. Point3F pos;
  841. getColumn(3, &pos);
  842. return pos;
  843. }
  844. template<typename DATA_TYPE, U32 rows, U32 cols>
  845. inline void Matrix<DATA_TYPE, rows, cols>::getColumn(S32 col, Point4F* cptr) const
  846. {
  847. if (rows >= 2)
  848. {
  849. cptr->x = (*this)(0, col);
  850. cptr->y = (*this)(1, col);
  851. }
  852. if (rows >= 3)
  853. cptr->z = (*this)(2, col);
  854. else
  855. cptr->z = 0.0f;
  856. if (rows >= 4)
  857. cptr->w = (*this)(3, col);
  858. else
  859. cptr->w = 0.0f;
  860. }
  861. template<typename DATA_TYPE, U32 rows, U32 cols>
  862. inline void Matrix<DATA_TYPE, rows, cols>::getColumn(S32 col, Point3F* cptr) const
  863. {
  864. if (rows >= 2)
  865. {
  866. cptr->x = (*this)(0, col);
  867. cptr->y = (*this)(1, col);
  868. }
  869. if (rows >= 3)
  870. cptr->z = (*this)(2, col);
  871. else
  872. cptr->z = 0.0f;
  873. }
  874. template<typename DATA_TYPE, U32 rows, U32 cols>
  875. inline void Matrix<DATA_TYPE, rows, cols>::setColumn(S32 col, const Point4F &cptr) {
  876. if(rows >= 2)
  877. {
  878. (*this)(0, col) = cptr.x;
  879. (*this)(1, col) = cptr.y;
  880. }
  881. if(rows >= 3)
  882. (*this)(2, col) = cptr.z;
  883. if(rows >= 4)
  884. (*this)(3, col) = cptr.w;
  885. }
  886. template<typename DATA_TYPE, U32 rows, U32 cols>
  887. inline void Matrix<DATA_TYPE, rows, cols>::setColumn(S32 col, const Point3F &cptr) {
  888. if(rows >= 2)
  889. {
  890. (*this)(0, col) = cptr.x;
  891. (*this)(1, col) = cptr.y;
  892. }
  893. if(rows >= 3)
  894. (*this)(2, col) = cptr.z;
  895. }
  896. template<typename DATA_TYPE, U32 rows, U32 cols>
  897. inline void Matrix<DATA_TYPE, rows, cols>::getRow(S32 row, Point4F* cptr) const
  898. {
  899. if (cols >= 2)
  900. {
  901. cptr->x = (*this)(row, 0);
  902. cptr->y = (*this)(row, 1);
  903. }
  904. if (cols >= 3)
  905. cptr->z = (*this)(row, 2);
  906. else
  907. cptr->z = 0.0f;
  908. if (cols >= 4)
  909. cptr->w = (*this)(row, 3);
  910. else
  911. cptr->w = 0.0f;
  912. }
  913. template<typename DATA_TYPE, U32 rows, U32 cols>
  914. inline void Matrix<DATA_TYPE, rows, cols>::getRow(S32 row, Point3F* cptr) const
  915. {
  916. if (cols >= 2)
  917. {
  918. cptr->x = (*this)(row, 0);
  919. cptr->y = (*this)(row, 1);
  920. }
  921. if (cols >= 3)
  922. cptr->z = (*this)(row, 2);
  923. else
  924. cptr->z = 0.0f;
  925. }
  926. template<typename DATA_TYPE, U32 rows, U32 cols>
  927. inline VectorF Matrix<DATA_TYPE, rows, cols>::getRightVector() const
  928. {
  929. VectorF vec;
  930. getColumn(0, &vec);
  931. return vec;
  932. }
  933. template<typename DATA_TYPE, U32 rows, U32 cols>
  934. inline VectorF Matrix<DATA_TYPE, rows, cols>::getForwardVector() const
  935. {
  936. VectorF vec;
  937. getColumn(1, &vec);
  938. return vec;
  939. }
  940. template<typename DATA_TYPE, U32 rows, U32 cols>
  941. inline VectorF Matrix<DATA_TYPE, rows, cols>::getUpVector() const
  942. {
  943. VectorF vec;
  944. getColumn(2, &vec);
  945. return vec;
  946. }
  947. template<typename DATA_TYPE, U32 rows, U32 cols>
  948. inline void Matrix<DATA_TYPE, rows, cols>::invertTo(Matrix<DATA_TYPE, cols, rows>* matrix) const
  949. {
  950. Matrix<DATA_TYPE, rows, cols> invMatrix;
  951. for (U32 i = 0; i < rows; ++i) {
  952. for (U32 j = 0; j < cols; ++j) {
  953. invMatrix(i, j) = (*this)(i, j);
  954. }
  955. }
  956. invMatrix.inverse();
  957. for (U32 i = 0; i < rows; ++i) {
  958. for (U32 j = 0; j < cols; ++j) {
  959. (*matrix)(i, j) = invMatrix(i, j);
  960. }
  961. }
  962. }
  963. template<typename DATA_TYPE, U32 rows, U32 cols>
  964. inline void Matrix<DATA_TYPE, rows, cols>::invertTo(Matrix<DATA_TYPE, cols, rows>* matrix)
  965. {
  966. Matrix<DATA_TYPE, rows, cols> invMatrix = this->inverse();
  967. for (U32 i = 0; i < rows; ++i) {
  968. for (U32 j = 0; j < cols; ++j) {
  969. (*matrix)(i, j) = invMatrix(i, j);
  970. }
  971. }
  972. }
  973. template<typename DATA_TYPE, U32 rows, U32 cols>
  974. inline void Matrix<DATA_TYPE, rows, cols>::setRow(S32 row, const Point4F& cptr) {
  975. if(cols >= 2)
  976. {
  977. (*this)(row, 0) = cptr.x;
  978. (*this)(row, 1) = cptr.y;
  979. }
  980. if(cols >= 3)
  981. (*this)(row, 2) = cptr.z;
  982. if(cols >= 4)
  983. (*this)(row, 3) = cptr.w;
  984. }
  985. template<typename DATA_TYPE, U32 rows, U32 cols>
  986. inline void Matrix<DATA_TYPE, rows, cols>::setRow(S32 row, const Point3F& cptr) {
  987. if(cols >= 2)
  988. {
  989. (*this)(row, 0) = cptr.x;
  990. (*this)(row, 1) = cptr.y;
  991. }
  992. if(cols >= 3)
  993. (*this)(row, 2) = cptr.z;
  994. }
  995. template<typename DATA_TYPE, U32 rows, U32 cols>
  996. inline void Matrix<DATA_TYPE, rows, cols>::displace(const Point3F& delta)
  997. {
  998. (*this)(0, 3) += delta.x;
  999. (*this)(1, 3) += delta.y;
  1000. (*this)(2, 3) += delta.z;
  1001. }
  1002. template<typename DATA_TYPE, U32 rows, U32 cols>
  1003. void Matrix<DATA_TYPE, rows, cols>::reverseProjection()
  1004. {
  1005. AssertFatal(rows == 4 && cols == 4, "reverseProjection requires a 4x4 matrix.");
  1006. (*this)(2, 0) = (*this)(3, 0) - (*this)(2, 0);
  1007. (*this)(2, 1) = (*this)(3, 1) - (*this)(2, 1);
  1008. (*this)(2, 2) = (*this)(3, 2) - (*this)(2, 2);
  1009. (*this)(2, 3) = (*this)(3, 3) - (*this)(2, 3);
  1010. }
  1011. template<typename DATA_TYPE, U32 rows, U32 cols>
  1012. const Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::Identity = []() {
  1013. Matrix<DATA_TYPE, rows, cols> identity(true);
  1014. return identity;
  1015. }();
  1016. template<typename DATA_TYPE, U32 rows, U32 cols>
  1017. Matrix<DATA_TYPE, rows, cols>::Matrix(const EulerF& e)
  1018. {
  1019. set(e);
  1020. }
  1021. template<typename DATA_TYPE, U32 rows, U32 cols>
  1022. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::set(const EulerF& e)
  1023. {
  1024. // when the template refactor is done, euler will be able to be setup in different ways
  1025. AssertFatal(rows >= 3 && cols >= 3, "EulerF can only initialize 3x3 or more");
  1026. static_assert(std::is_same<DATA_TYPE, float>::value, "Can only initialize eulers with floats for now");
  1027. F32 cosPitch, sinPitch;
  1028. mSinCos(e.x, sinPitch, cosPitch);
  1029. F32 cosYaw, sinYaw;
  1030. mSinCos(e.y, sinYaw, cosYaw);
  1031. F32 cosRoll, sinRoll;
  1032. mSinCos(e.z, sinRoll, cosRoll);
  1033. enum {
  1034. AXIS_X = (1 << 0),
  1035. AXIS_Y = (1 << 1),
  1036. AXIS_Z = (1 << 2)
  1037. };
  1038. U32 axis = 0;
  1039. if (e.x != 0.0f) axis |= AXIS_X;
  1040. if (e.y != 0.0f) axis |= AXIS_Y;
  1041. if (e.z != 0.0f) axis |= AXIS_Z;
  1042. switch (axis) {
  1043. case 0:
  1044. (*this) = Matrix<DATA_TYPE, rows, cols>(true);
  1045. break;
  1046. case AXIS_X:
  1047. (*this)(0, 0) = 1.0f; (*this)(1, 0) = 0.0f; (*this)(2, 0) = 0.0f;
  1048. (*this)(0, 1) = 0.0f; (*this)(1, 1) = cosPitch; (*this)(2, 1) = -sinPitch;
  1049. (*this)(0, 2) = 0.0f; (*this)(1, 2) = sinPitch; (*this)(2, 2) = cosPitch;
  1050. break;
  1051. case AXIS_Y:
  1052. (*this)(0, 0) = cosYaw; (*this)(1, 0) = 0.0f; (*this)(2, 0) = sinYaw;
  1053. (*this)(0, 1) = 0.0f; (*this)(1, 1) = 1.0f; (*this)(2, 1) = 0.0f;
  1054. (*this)(0, 2) = -sinYaw; (*this)(1, 2) = 0.0f; (*this)(2, 2) = cosYaw;
  1055. break;
  1056. case AXIS_Z:
  1057. (*this)(0, 0) = cosRoll; (*this)(1, 0) = -sinRoll; (*this)(2, 0) = 0.0f;
  1058. (*this)(0, 1) = sinRoll; (*this)(1, 1) = cosRoll; (*this)(2, 1) = 0.0f;
  1059. (*this)(0, 2) = 0.0f; (*this)(1, 2) = 0.0f; (*this)(2, 2) = 0.0f;
  1060. break;
  1061. default:
  1062. F32 r1 = cosYaw * cosRoll;
  1063. F32 r2 = cosYaw * sinRoll;
  1064. F32 r3 = sinYaw * cosRoll;
  1065. F32 r4 = sinYaw * sinRoll;
  1066. // the matrix looks like this:
  1067. // r1 - (r4 * sin(x)) r2 + (r3 * sin(x)) -cos(x) * sin(y)
  1068. // -cos(x) * sin(z) cos(x) * cos(z) sin(x)
  1069. // r3 + (r2 * sin(x)) r4 - (r1 * sin(x)) cos(x) * cos(y)
  1070. //
  1071. // where:
  1072. // r1 = cos(y) * cos(z)
  1073. // r2 = cos(y) * sin(z)
  1074. // r3 = sin(y) * cos(z)
  1075. // r4 = sin(y) * sin(z)
  1076. // init the euler 3x3 rotation matrix.
  1077. (*this)(0, 0) = r1 - (r4 * sinPitch); (*this)(1, 0) = -cosPitch * sinRoll; (*this)(2, 0) = r3 + (r2 * sinPitch);
  1078. (*this)(0, 1) = r2 + (r3 * sinPitch); (*this)(1, 1) = cosPitch * cosRoll; (*this)(2, 1) = r4 - (r1 * sinPitch);
  1079. (*this)(0, 2) = -cosPitch * sinYaw; (*this)(1, 2) = sinPitch; (*this)(2, 2) = cosPitch * cosYaw;
  1080. break;
  1081. }
  1082. if (rows == 4) {
  1083. (*this)(3, 0) = 0.0f;
  1084. (*this)(3, 1) = 0.0f;
  1085. (*this)(3, 2) = 0.0f;
  1086. }
  1087. if (cols == 4) {
  1088. (*this)(0, 3) = 0.0f;
  1089. (*this)(1, 3) = 0.0f;
  1090. (*this)(2, 3) = 0.0f;
  1091. }
  1092. if (rows == 4 && cols == 4) {
  1093. (*this)(3, 3) = 1.0f;
  1094. }
  1095. return(*this);
  1096. }
  1097. template<typename DATA_TYPE, U32 rows, U32 cols>
  1098. Matrix<DATA_TYPE, rows, cols>::Matrix(const EulerF& e, const Point3F p)
  1099. {
  1100. set(e, p);
  1101. }
  1102. template<typename DATA_TYPE, U32 rows, U32 cols>
  1103. Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::set(const EulerF& e, const Point3F p)
  1104. {
  1105. AssertFatal(rows >= 3 && cols >= 4, "Euler and Point can only initialize 3x4 or more");
  1106. // call set euler, this already sets the last row if it exists.
  1107. set(e);
  1108. // does this need to multiply with the result of the euler? or are we just setting position.
  1109. (*this)(0, 3) = p.x;
  1110. (*this)(1, 3) = p.y;
  1111. (*this)(2, 3) = p.z;
  1112. return (*this);
  1113. }
  1114. template<typename DATA_TYPE, U32 rows, U32 cols>
  1115. Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::inverse()
  1116. {
  1117. // TODO: insert return statement here
  1118. AssertFatal(rows == cols, "Can only perform inverse on square matrices.");
  1119. const U32 size = rows;
  1120. // Create augmented matrix [this | I]
  1121. Matrix<DATA_TYPE, size, 2 * size> augmentedMatrix;
  1122. Matrix<DATA_TYPE, size, size> resultMatrix;
  1123. for (U32 i = 0; i < size; i++) {
  1124. for (U32 j = 0; j < size; j++) {
  1125. augmentedMatrix(i, j) = (*this)(i, j);
  1126. augmentedMatrix(i, j + size) = (i == j) ? static_cast<DATA_TYPE>(1) : static_cast<DATA_TYPE>(0);
  1127. }
  1128. }
  1129. // Apply gauss-joran elimination
  1130. for (U32 i = 0; i < size; i++) {
  1131. U32 pivotRow = i;
  1132. for (U32 k = i + 1; k < size; k++) {
  1133. // use std::abs until the templated math functions are in place.
  1134. if (std::abs(augmentedMatrix(k, i)) > std::abs(augmentedMatrix(pivotRow, i))) {
  1135. pivotRow = k;
  1136. }
  1137. }
  1138. // Swap if needed.
  1139. if (i != pivotRow) {
  1140. for (U32 j = 0; j < 2 * size; j++) {
  1141. std::swap(augmentedMatrix(i, j), augmentedMatrix(pivotRow, j));
  1142. }
  1143. }
  1144. // Early out if pivot is 0, return identity matrix.
  1145. if (augmentedMatrix(i, i) == static_cast<DATA_TYPE>(0)) {
  1146. return Matrix<DATA_TYPE, rows, cols>(true);
  1147. }
  1148. DATA_TYPE pivotVal = augmentedMatrix(i, i);
  1149. // scale the pivot
  1150. for (U32 j = 0; j < 2 * size; j++) {
  1151. augmentedMatrix(i, j) /= pivotVal;
  1152. }
  1153. // Eliminate the current column in all other rows
  1154. for (U32 k = 0; k < size; k++) {
  1155. if (k != i) {
  1156. DATA_TYPE factor = augmentedMatrix(k, i);
  1157. for (U32 j = 0; j < 2 * size; j++) {
  1158. augmentedMatrix(k, j) -= factor * augmentedMatrix(i, j);
  1159. }
  1160. }
  1161. }
  1162. }
  1163. for (U32 i = 0; i < size; i++) {
  1164. for (U32 j = 0; j < size; j++) {
  1165. resultMatrix(i, j) = augmentedMatrix(i, j + size);
  1166. }
  1167. }
  1168. return resultMatrix;
  1169. }
  1170. template<typename DATA_TYPE, U32 rows, U32 cols>
  1171. inline bool Matrix<DATA_TYPE, rows, cols>::fullInverse()
  1172. {
  1173. Matrix<DATA_TYPE, rows, cols> inv = this->inverse();
  1174. if (inv.isIdentity())
  1175. return false;
  1176. *this = inv;
  1177. return true;
  1178. }
  1179. template<typename DATA_TYPE, U32 rows, U32 cols>
  1180. inline void Matrix<DATA_TYPE, rows, cols>::invert()
  1181. {
  1182. (*this) = inverse();
  1183. }
  1184. template<typename DATA_TYPE, U32 rows, U32 cols>
  1185. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::setCrossProduct(const Point3F& p)
  1186. {
  1187. AssertFatal(rows == 4 && cols == 4, "Cross product only supported on 4x4 for now");
  1188. (*this)(0, 0) = 0;
  1189. (*this)(0, 1) = -p.z;
  1190. (*this)(0, 2) = p.y;
  1191. (*this)(0, 3) = 0;
  1192. (*this)(1, 0) = p.z;
  1193. (*this)(1, 1) = 0;
  1194. (*this)(1, 2) = -p.x;
  1195. (*this)(1, 3) = 0;
  1196. (*this)(2, 0) = -p.y;
  1197. (*this)(2, 1) = p.x;
  1198. (*this)(2, 2) = 0;
  1199. (*this)(2, 3) = 0;
  1200. (*this)(3, 0) = 0;
  1201. (*this)(3, 1) = 0;
  1202. (*this)(3, 2) = 0;
  1203. (*this)(3, 3) = 1;
  1204. return (*this);
  1205. }
  1206. template<typename DATA_TYPE, U32 rows, U32 cols>
  1207. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::setTensorProduct(const Point3F& p, const Point3F& q)
  1208. {
  1209. AssertFatal(rows == 4 && cols == 4, "Tensor product only supported on 4x4 for now");
  1210. (*this)(0, 0) = p.x * q.x;
  1211. (*this)(0, 1) = p.x * q.y;
  1212. (*this)(0, 2) = p.x * q.z;
  1213. (*this)(0, 3) = 0;
  1214. (*this)(1, 0) = p.y * q.x;
  1215. (*this)(1, 1) = p.y * q.y;
  1216. (*this)(1, 2) = p.y * q.z;
  1217. (*this)(1, 3) = 0;
  1218. (*this)(2, 0) = p.z * q.x;
  1219. (*this)(2, 1) = p.z * q.y;
  1220. (*this)(2, 2) = p.z * q.z;
  1221. (*this)(2, 3) = 0;
  1222. (*this)(3, 0) = 0;
  1223. (*this)(3, 1) = 0;
  1224. (*this)(3, 2) = 0;
  1225. (*this)(3, 3) = 1;
  1226. return (*this);
  1227. }
  1228. template<typename DATA_TYPE, U32 rows, U32 cols>
  1229. inline void Matrix<DATA_TYPE, rows, cols>::mul(Box3F& box) const
  1230. {
  1231. AssertFatal(rows == 4 && cols == 4, "Multiplying Box3F with matrix requires 4x4");
  1232. // Create an array of all 8 corners of the box
  1233. Point3F corners[8] = {
  1234. Point3F(box.minExtents.x, box.minExtents.y, box.minExtents.z),
  1235. Point3F(box.minExtents.x, box.minExtents.y, box.maxExtents.z),
  1236. Point3F(box.minExtents.x, box.maxExtents.y, box.minExtents.z),
  1237. Point3F(box.minExtents.x, box.maxExtents.y, box.maxExtents.z),
  1238. Point3F(box.maxExtents.x, box.minExtents.y, box.minExtents.z),
  1239. Point3F(box.maxExtents.x, box.minExtents.y, box.maxExtents.z),
  1240. Point3F(box.maxExtents.x, box.maxExtents.y, box.minExtents.z),
  1241. Point3F(box.maxExtents.x, box.maxExtents.y, box.maxExtents.z),
  1242. };
  1243. for (U32 i = 0; i < 8; i++) {
  1244. corners[i] = (*this) * corners[i];
  1245. }
  1246. box.minExtents = corners[0];
  1247. box.maxExtents = corners[0];
  1248. for (U32 i = 1; i < 8; ++i) {
  1249. box.minExtents.x = mMin(box.minExtents.x, corners[i].x);
  1250. box.minExtents.y = mMin(box.minExtents.y, corners[i].y);
  1251. box.minExtents.z = mMin(box.minExtents.z, corners[i].z);
  1252. box.maxExtents.x = mMax(box.maxExtents.x, corners[i].x);
  1253. box.maxExtents.y = mMax(box.maxExtents.y, corners[i].y);
  1254. box.maxExtents.z = mMax(box.maxExtents.z, corners[i].z);
  1255. }
  1256. }
  1257. template<typename DATA_TYPE, U32 rows, U32 cols>
  1258. inline bool Matrix<DATA_TYPE, rows, cols>::isAffine() const
  1259. {
  1260. if ((*this)(rows - 1, cols - 1) != 1.0f) {
  1261. return false;
  1262. }
  1263. for (U32 col = 0; col < cols - 1; ++col) {
  1264. if ((*this)(rows - 1, col) != 0.0f) {
  1265. return false;
  1266. }
  1267. }
  1268. Point3F one, two, three;
  1269. getColumn(0, &one);
  1270. getColumn(1, &two);
  1271. getColumn(2, &three);
  1272. // check columns
  1273. {
  1274. if (mDot(one, two) > 0.0001f ||
  1275. mDot(one, three) > 0.0001f ||
  1276. mDot(two, three) > 0.0001f)
  1277. return false;
  1278. if (mFabs(1.0f - one.lenSquared()) > 0.0001f ||
  1279. mFabs(1.0f - two.lenSquared()) > 0.0001f ||
  1280. mFabs(1.0f - three.lenSquared()) > 0.0001f)
  1281. return false;
  1282. }
  1283. getRow(0, &one);
  1284. getRow(1, &two);
  1285. getRow(2, &three);
  1286. // check rows
  1287. {
  1288. if (mDot(one, two) > 0.0001f ||
  1289. mDot(one, three) > 0.0001f ||
  1290. mDot(two, three) > 0.0001f)
  1291. return false;
  1292. if (mFabs(1.0f - one.lenSquared()) > 0.0001f ||
  1293. mFabs(1.0f - two.lenSquared()) > 0.0001f ||
  1294. mFabs(1.0f - three.lenSquared()) > 0.0001f)
  1295. return false;
  1296. }
  1297. return true;
  1298. }
  1299. template<typename DATA_TYPE, U32 rows, U32 cols>
  1300. inline Matrix<DATA_TYPE, rows, cols> Matrix<DATA_TYPE, rows, cols>::affineInverse()
  1301. {
  1302. AssertFatal(rows >= 4 && cols >= 4, "affineInverse requires at least 4x4");
  1303. Matrix<DATA_TYPE, 3, 3> subMatrix;
  1304. for (U32 i = 0; i < 3; i++) {
  1305. for (U32 j = 0; j < 3; j++) {
  1306. subMatrix(i, j) = (*this)(i, j);
  1307. }
  1308. }
  1309. subMatrix.transpose();
  1310. Point3F pos = getPosition();
  1311. (*this)(0, 3) = mDot(subMatrix.getColumn3F(0), pos);
  1312. (*this)(1, 3) = mDot(subMatrix.getColumn3F(1), pos);
  1313. (*this)(2, 3) = mDot(subMatrix.getColumn3F(2), pos);
  1314. return *this;
  1315. }
  1316. template<typename DATA_TYPE, U32 rows, U32 cols>
  1317. inline EulerF Matrix<DATA_TYPE, rows, cols>::toEuler() const
  1318. {
  1319. AssertFatal(rows >= 3 && cols >= 3, "Euler rotations require at least a 3x3 matrix.");
  1320. // Extract rotation matrix components
  1321. const DATA_TYPE m00 = (*this)(0, 0);
  1322. const DATA_TYPE m01 = (*this)(0, 1);
  1323. const DATA_TYPE m02 = (*this)(0, 2);
  1324. const DATA_TYPE m10 = (*this)(1, 0);
  1325. const DATA_TYPE m11 = (*this)(1, 1);
  1326. const DATA_TYPE m21 = (*this)(2, 1);
  1327. const DATA_TYPE m22 = (*this)(2, 2);
  1328. // like all others assume float for now.
  1329. EulerF r;
  1330. r.x = mAsin(mClampF(m21, -1.0, 1.0));
  1331. if (mCos(r.x) != 0.0f) {
  1332. r.y = mAtan2(-m02, m22); // yaw
  1333. r.z = mAtan2(-m10, m11); // roll
  1334. }
  1335. else {
  1336. r.y = 0.0f;
  1337. r.z = mAtan2(m01, m00); // this rolls when pitch is +90 degrees
  1338. }
  1339. return r;
  1340. }
  1341. template<typename DATA_TYPE, U32 rows, U32 cols>
  1342. inline void Matrix<DATA_TYPE, rows, cols>::dumpMatrix(const char* caption) const
  1343. {
  1344. U32 size = (caption == NULL) ? 0 : dStrlen(caption);
  1345. FrameTemp<char> spacer(size + 1);
  1346. char* spacerRef = spacer;
  1347. // is_floating_point should return true for floats and doubles.
  1348. const char* formatSpec = std::is_floating_point_v<DATA_TYPE> ? " %-8.4f" : " %d";
  1349. dMemset(spacerRef, ' ', size);
  1350. // null terminate.
  1351. spacerRef[size] = '\0';
  1352. /*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)]);
  1353. 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)]);
  1354. 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)]);
  1355. 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)]);*/
  1356. StringBuilder str;
  1357. str.format("%s = |", caption);
  1358. for (U32 i = 0; i < rows; i++) {
  1359. if (i > 0) {
  1360. str.append(spacerRef);
  1361. }
  1362. for (U32 j = 0; j < cols; j++) {
  1363. str.format(formatSpec, (*this)(i, j));
  1364. }
  1365. str.append(" |\n");
  1366. }
  1367. Con::printf("%s", str.end().c_str());
  1368. }
  1369. //------------------------------------
  1370. // Non-member methods
  1371. //------------------------------------
  1372. inline void mTransformPlane(
  1373. const MatrixF& mat,
  1374. const Point3F& scale,
  1375. const PlaneF& plane,
  1376. PlaneF* result
  1377. ) {
  1378. // Create a non-const copy of the matrix
  1379. MatrixF matCopy = mat;
  1380. // Create the inverse scale matrix
  1381. MatrixF invScale = MatrixF::Identity;
  1382. invScale(0, 0) = 1.0f / scale.x;
  1383. invScale(1, 1) = 1.0f / scale.y;
  1384. invScale(2, 2) = 1.0f / scale.z;
  1385. // Compute the inverse transpose of the matrix
  1386. MatrixF invTrMatrix = matCopy.transpose().affineInverse() * invScale;
  1387. // Transform the plane normal
  1388. Point3F norm(plane.x, plane.y, plane.z);
  1389. norm = invTrMatrix * norm;
  1390. float normLength = std::sqrt(norm.x * norm.x + norm.y * norm.y + norm.z * norm.z);
  1391. norm.x /= normLength;
  1392. norm.y /= normLength;
  1393. norm.z /= normLength;
  1394. // Transform the plane point
  1395. Point3F point = norm * (-plane.d);
  1396. MMatrixF temp = mat;
  1397. point.x *= scale.x;
  1398. point.y *= scale.y;
  1399. point.z *= scale.z;
  1400. point = temp * point;
  1401. // Recompute the plane distance
  1402. PlaneF resultPlane(point, norm);
  1403. result->x = resultPlane.x;
  1404. result->y = resultPlane.y;
  1405. result->z = resultPlane.z;
  1406. result->d = resultPlane.d;
  1407. }
  1408. //--------------------------------------------
  1409. // INLINE FUNCTIONS END
  1410. //--------------------------------------------
  1411. typedef Matrix<F32, 4, 4> MatrixF;
  1412. class MatrixTemplateExport
  1413. {
  1414. public:
  1415. template <typename T, U32 rows, U32 cols>
  1416. static EngineFieldTable::Field getMatrixField();
  1417. };
  1418. template<typename T, U32 rows, U32 cols>
  1419. inline EngineFieldTable::Field MatrixTemplateExport::getMatrixField()
  1420. {
  1421. typedef Matrix<T, rows, cols> ThisType;
  1422. return _FIELD_AS(T, data, data, rows * cols, "");
  1423. }
  1424. #endif // !USE_TEMPLATE_MATRIX
  1425. #endif //_MMATRIX_H_