mMatrix.h 47 KB

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