mMatrix.h 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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. /// 4x4 Matrix Class
  38. ///
  39. /// This runs at F32 precision.
  40. class MatrixF
  41. {
  42. friend class MatrixFEngineExport;
  43. private:
  44. F32 m[16]; ///< Note: Torque uses row-major matrices
  45. public:
  46. /// Create an uninitialized matrix.
  47. ///
  48. /// @param identity If true, initialize to the identity matrix.
  49. explicit MatrixF(bool identity=false);
  50. /// Create a matrix to rotate about origin by e.
  51. /// @see set
  52. explicit MatrixF( const EulerF &e);
  53. /// Create a matrix to rotate about p by e.
  54. /// @see set
  55. MatrixF( const EulerF &e, const Point3F& p);
  56. /// Get the index in m to element in column i, row j
  57. ///
  58. /// This is necessary as we have m as a one dimensional array.
  59. ///
  60. /// @param i Column desired.
  61. /// @param j Row desired.
  62. static U32 idx(U32 i, U32 j) { return (i + j*4); }
  63. /// Initialize matrix to rotate about origin by e.
  64. MatrixF& set( const EulerF &e);
  65. /// Initialize matrix to rotate about p by e.
  66. MatrixF& set( const EulerF &e, const Point3F& p);
  67. /// Initialize matrix with a cross product of p.
  68. MatrixF& setCrossProduct( const Point3F &p);
  69. /// Initialize matrix with a tensor product of p.
  70. MatrixF& setTensorProduct( const Point3F &p, const Point3F& q);
  71. operator F32*() { return (m); } ///< Allow people to get at m.
  72. operator const F32*() const { return (F32*)(m); } ///< Allow people to get at m.
  73. bool isAffine() const; ///< Check to see if this is an affine matrix.
  74. bool isIdentity() const; ///< Checks for identity matrix.
  75. /// Make this an identity matrix.
  76. MatrixF& identity();
  77. /// Invert m.
  78. MatrixF& inverse();
  79. /// Copy the inversion of this into out matrix.
  80. void invertTo( MatrixF *out );
  81. /// Take inverse of matrix assuming it is affine (rotation,
  82. /// scale, sheer, translation only).
  83. MatrixF& affineInverse();
  84. /// Swap rows and columns.
  85. MatrixF& transpose();
  86. /// M * Matrix(p) -> M
  87. MatrixF& scale( const Point3F &s );
  88. MatrixF& scale( F32 s ) { return scale( Point3F( s, s, s ) ); }
  89. /// Return scale assuming scale was applied via mat.scale(s).
  90. Point3F getScale() const;
  91. EulerF toEuler() const;
  92. /// Compute the inverse of the matrix.
  93. ///
  94. /// Computes inverse of full 4x4 matrix. Returns false and performs no inverse if
  95. /// the determinant is 0.
  96. ///
  97. /// Note: In most cases you want to use the normal inverse function. This method should
  98. /// be used if the matrix has something other than (0,0,0,1) in the bottom row.
  99. bool fullInverse();
  100. /// Reverse depth for projection matrix
  101. /// Simplifies reversal matrix mult to 4 subtractions
  102. void reverseProjection();
  103. /// Swaps rows and columns into matrix.
  104. void transposeTo(F32 *matrix) const;
  105. /// Normalize the matrix.
  106. void normalize();
  107. /// Copy the requested column into a Point4F.
  108. void getColumn(S32 col, Point4F *cptr) const;
  109. Point4F getColumn4F(S32 col) const { Point4F ret; getColumn(col,&ret); return ret; }
  110. /// Copy the requested column into a Point3F.
  111. ///
  112. /// This drops the bottom-most row.
  113. void getColumn(S32 col, Point3F *cptr) const;
  114. Point3F getColumn3F(S32 col) const { Point3F ret; getColumn(col,&ret); return ret; }
  115. /// Set the specified column from a Point4F.
  116. void setColumn(S32 col, const Point4F& cptr);
  117. /// Set the specified column from a Point3F.
  118. ///
  119. /// The bottom-most row is not set.
  120. void setColumn(S32 col, const Point3F& cptr);
  121. /// Copy the specified row into a Point4F.
  122. void getRow(S32 row, Point4F *cptr) const;
  123. Point4F getRow4F(S32 row) const { Point4F ret; getRow(row,&ret); return ret; }
  124. /// Copy the specified row into a Point3F.
  125. ///
  126. /// Right-most item is dropped.
  127. void getRow(S32 row, Point3F *cptr) const;
  128. Point3F getRow3F(S32 row) const { Point3F ret; getRow(row,&ret); return ret; }
  129. /// Set the specified row from a Point4F.
  130. void setRow(S32 row, const Point4F& cptr);
  131. /// Set the specified row from a Point3F.
  132. ///
  133. /// The right-most item is not set.
  134. void setRow(S32 row, const Point3F& cptr);
  135. /// Get the position of the matrix.
  136. ///
  137. /// This is the 4th column of the matrix.
  138. Point3F getPosition() const;
  139. /// Set the position of the matrix.
  140. ///
  141. /// This is the 4th column of the matrix.
  142. void setPosition( const Point3F &pos ) { setColumn( 3, pos ); }
  143. /// Add the passed delta to the matrix position.
  144. void displace( const Point3F &delta );
  145. /// Get the x axis of the matrix.
  146. ///
  147. /// This is the 1st column of the matrix and is
  148. /// normally considered the right vector.
  149. VectorF getRightVector() const;
  150. /// Get the y axis of the matrix.
  151. ///
  152. /// This is the 2nd column of the matrix and is
  153. /// normally considered the forward vector.
  154. VectorF getForwardVector() const;
  155. /// Get the z axis of the matrix.
  156. ///
  157. /// This is the 3rd column of the matrix and is
  158. /// normally considered the up vector.
  159. VectorF getUpVector() const;
  160. MatrixF& mul(const MatrixF &a); ///< M * a -> M
  161. MatrixF& mulL(const MatrixF &a); ///< a * M -> M
  162. MatrixF& mul(const MatrixF &a, const MatrixF &b); ///< a * b -> M
  163. // Scalar multiplies
  164. MatrixF& mul(const F32 a); ///< M * a -> M
  165. MatrixF& mul(const MatrixF &a, const F32 b); ///< a * b -> M
  166. void mul( Point4F& p ) const; ///< M * p -> p (full [4x4] * [1x4])
  167. void mulP( Point3F& p ) const; ///< M * p -> p (assume w = 1.0f)
  168. void mulP( const Point3F &p, Point3F *d) const; ///< M * p -> d (assume w = 1.0f)
  169. void mulV( VectorF& p ) const; ///< M * v -> v (assume w = 0.0f)
  170. void mulV( const VectorF &p, Point3F *d) const; ///< M * v -> d (assume w = 0.0f)
  171. void mul(Box3F& b) const; ///< Axial box -> Axial Box
  172. MatrixF& add( const MatrixF& m );
  173. /// Convenience function to allow people to treat this like an array.
  174. F32& operator ()(S32 row, S32 col) { return m[idx(col,row)]; }
  175. F32 operator ()(S32 row, S32 col) const { return m[idx(col,row)]; }
  176. void dumpMatrix(const char *caption=NULL) const;
  177. // Math operator overloads
  178. //------------------------------------
  179. friend MatrixF operator * ( const MatrixF &m1, const MatrixF &m2 );
  180. MatrixF& operator *= ( const MatrixF &m );
  181. MatrixF &operator = (const MatrixF &m);
  182. bool isNaN();
  183. // Static identity matrix
  184. const static MatrixF Identity;
  185. };
  186. class MatrixFEngineExport
  187. {
  188. public:
  189. static EngineFieldTable::Field getMatrixField();
  190. };
  191. //--------------------------------------
  192. // Inline Functions
  193. inline MatrixF::MatrixF(bool _identity)
  194. {
  195. if (_identity)
  196. identity();
  197. else
  198. std::fill_n(m, 16, 0);
  199. }
  200. inline MatrixF::MatrixF( const EulerF &e )
  201. {
  202. set(e);
  203. }
  204. inline MatrixF::MatrixF( const EulerF &e, const Point3F& p )
  205. {
  206. set(e,p);
  207. }
  208. inline MatrixF& MatrixF::set( const EulerF &e)
  209. {
  210. m_matF_set_euler( e, *this );
  211. return (*this);
  212. }
  213. inline MatrixF& MatrixF::set( const EulerF &e, const Point3F& p)
  214. {
  215. m_matF_set_euler_point( e, p, *this );
  216. return (*this);
  217. }
  218. inline MatrixF& MatrixF::setCrossProduct( const Point3F &p)
  219. {
  220. m[1] = -(m[4] = p.z);
  221. m[8] = -(m[2] = p.y);
  222. m[6] = -(m[9] = p.x);
  223. m[0] = m[3] = m[5] = m[7] = m[10] = m[11] =
  224. m[12] = m[13] = m[14] = 0.0f;
  225. m[15] = 1;
  226. return (*this);
  227. }
  228. inline MatrixF& MatrixF::setTensorProduct( const Point3F &p, const Point3F &q)
  229. {
  230. m[0] = p.x * q.x;
  231. m[1] = p.x * q.y;
  232. m[2] = p.x * q.z;
  233. m[4] = p.y * q.x;
  234. m[5] = p.y * q.y;
  235. m[6] = p.y * q.z;
  236. m[8] = p.z * q.x;
  237. m[9] = p.z * q.y;
  238. m[10] = p.z * q.z;
  239. m[3] = m[7] = m[11] = m[12] = m[13] = m[14] = 0.0f;
  240. m[15] = 1.0f;
  241. return (*this);
  242. }
  243. inline bool MatrixF::isIdentity() const
  244. {
  245. return
  246. m[0] == 1.0f &&
  247. m[1] == 0.0f &&
  248. m[2] == 0.0f &&
  249. m[3] == 0.0f &&
  250. m[4] == 0.0f &&
  251. m[5] == 1.0f &&
  252. m[6] == 0.0f &&
  253. m[7] == 0.0f &&
  254. m[8] == 0.0f &&
  255. m[9] == 0.0f &&
  256. m[10] == 1.0f &&
  257. m[11] == 0.0f &&
  258. m[12] == 0.0f &&
  259. m[13] == 0.0f &&
  260. m[14] == 0.0f &&
  261. m[15] == 1.0f;
  262. }
  263. inline MatrixF& MatrixF::identity()
  264. {
  265. m[0] = 1.0f;
  266. m[1] = 0.0f;
  267. m[2] = 0.0f;
  268. m[3] = 0.0f;
  269. m[4] = 0.0f;
  270. m[5] = 1.0f;
  271. m[6] = 0.0f;
  272. m[7] = 0.0f;
  273. m[8] = 0.0f;
  274. m[9] = 0.0f;
  275. m[10] = 1.0f;
  276. m[11] = 0.0f;
  277. m[12] = 0.0f;
  278. m[13] = 0.0f;
  279. m[14] = 0.0f;
  280. m[15] = 1.0f;
  281. return (*this);
  282. }
  283. inline MatrixF& MatrixF::inverse()
  284. {
  285. m_matF_inverse(m);
  286. return (*this);
  287. }
  288. inline void MatrixF::invertTo( MatrixF *out )
  289. {
  290. m_matF_invert_to(m,*out);
  291. }
  292. inline MatrixF& MatrixF::affineInverse()
  293. {
  294. // AssertFatal(isAffine() == true, "Error, this matrix is not an affine transform");
  295. m_matF_affineInverse(m);
  296. return (*this);
  297. }
  298. inline MatrixF& MatrixF::transpose()
  299. {
  300. m_matF_transpose(m);
  301. return (*this);
  302. }
  303. inline MatrixF& MatrixF::scale(const Point3F& p)
  304. {
  305. m_matF_scale(m,p);
  306. return *this;
  307. }
  308. inline Point3F MatrixF::getScale() const
  309. {
  310. Point3F scale;
  311. scale.x = mSqrt(m[0]*m[0] + m[4] * m[4] + m[8] * m[8]);
  312. scale.y = mSqrt(m[1]*m[1] + m[5] * m[5] + m[9] * m[9]);
  313. scale.z = mSqrt(m[2]*m[2] + m[6] * m[6] + m[10] * m[10]);
  314. return scale;
  315. }
  316. inline void MatrixF::normalize()
  317. {
  318. m_matF_normalize(m);
  319. }
  320. inline MatrixF& MatrixF::mul( const MatrixF &a )
  321. { // M * a -> M
  322. AssertFatal(&a != this, "MatrixF::mul - a.mul(a) is invalid!");
  323. MatrixF tempThis(*this);
  324. m_matF_x_matF(tempThis, a, *this);
  325. return (*this);
  326. }
  327. inline MatrixF& MatrixF::mulL( const MatrixF &a )
  328. { // a * M -> M
  329. AssertFatal(&a != this, "MatrixF::mulL - a.mul(a) is invalid!");
  330. MatrixF tempThis(*this);
  331. m_matF_x_matF(a, tempThis, *this);
  332. return (*this);
  333. }
  334. inline MatrixF& MatrixF::mul( const MatrixF &a, const MatrixF &b )
  335. { // a * b -> M
  336. AssertFatal((&a != this) && (&b != this), "MatrixF::mul - a.mul(a, b) a.mul(b, a) a.mul(a, a) is invalid!");
  337. m_matF_x_matF(a, b, *this);
  338. return (*this);
  339. }
  340. inline MatrixF& MatrixF::mul(const F32 a)
  341. {
  342. for (U32 i = 0; i < 16; i++)
  343. m[i] *= a;
  344. return *this;
  345. }
  346. inline MatrixF& MatrixF::mul(const MatrixF &a, const F32 b)
  347. {
  348. *this = a;
  349. mul(b);
  350. return *this;
  351. }
  352. inline void MatrixF::mul( Point4F& p ) const
  353. {
  354. Point4F temp;
  355. m_matF_x_point4F(*this, &p.x, &temp.x);
  356. p = temp;
  357. }
  358. inline void MatrixF::mulP( Point3F& p) const
  359. {
  360. // M * p -> d
  361. Point3F d;
  362. m_matF_x_point3F(*this, &p.x, &d.x);
  363. p = d;
  364. }
  365. inline void MatrixF::mulP( const Point3F &p, Point3F *d) const
  366. {
  367. // M * p -> d
  368. m_matF_x_point3F(*this, &p.x, &d->x);
  369. }
  370. inline void MatrixF::mulV( VectorF& v) const
  371. {
  372. // M * v -> v
  373. VectorF temp;
  374. m_matF_x_vectorF(*this, &v.x, &temp.x);
  375. v = temp;
  376. }
  377. inline void MatrixF::mulV( const VectorF &v, Point3F *d) const
  378. {
  379. // M * v -> d
  380. m_matF_x_vectorF(*this, &v.x, &d->x);
  381. }
  382. inline void MatrixF::mul(Box3F& b) const
  383. {
  384. m_matF_x_box3F(*this, &b.minExtents.x, &b.maxExtents.x);
  385. }
  386. inline MatrixF& MatrixF::add( const MatrixF& a )
  387. {
  388. for( U32 i = 0; i < 16; ++ i )
  389. m[ i ] += a.m[ i ];
  390. return *this;
  391. }
  392. inline void MatrixF::getColumn(S32 col, Point4F *cptr) const
  393. {
  394. cptr->x = m[col];
  395. cptr->y = m[col+4];
  396. cptr->z = m[col+8];
  397. cptr->w = m[col+12];
  398. }
  399. inline void MatrixF::getColumn(S32 col, Point3F *cptr) const
  400. {
  401. cptr->x = m[col];
  402. cptr->y = m[col+4];
  403. cptr->z = m[col+8];
  404. }
  405. inline void MatrixF::setColumn(S32 col, const Point4F &cptr)
  406. {
  407. m[col] = cptr.x;
  408. m[col+4] = cptr.y;
  409. m[col+8] = cptr.z;
  410. m[col+12]= cptr.w;
  411. }
  412. inline void MatrixF::setColumn(S32 col, const Point3F &cptr)
  413. {
  414. m[col] = cptr.x;
  415. m[col+4] = cptr.y;
  416. m[col+8] = cptr.z;
  417. }
  418. inline void MatrixF::getRow(S32 col, Point4F *cptr) const
  419. {
  420. col *= 4;
  421. cptr->x = m[col++];
  422. cptr->y = m[col++];
  423. cptr->z = m[col++];
  424. cptr->w = m[col];
  425. }
  426. inline void MatrixF::getRow(S32 col, Point3F *cptr) const
  427. {
  428. col *= 4;
  429. cptr->x = m[col++];
  430. cptr->y = m[col++];
  431. cptr->z = m[col];
  432. }
  433. inline void MatrixF::setRow(S32 col, const Point4F &cptr)
  434. {
  435. col *= 4;
  436. m[col++] = cptr.x;
  437. m[col++] = cptr.y;
  438. m[col++] = cptr.z;
  439. m[col] = cptr.w;
  440. }
  441. inline void MatrixF::setRow(S32 col, const Point3F &cptr)
  442. {
  443. col *= 4;
  444. m[col++] = cptr.x;
  445. m[col++] = cptr.y;
  446. m[col] = cptr.z;
  447. }
  448. inline Point3F MatrixF::getPosition() const
  449. {
  450. return Point3F( m[3], m[3+4], m[3+8] );
  451. }
  452. inline void MatrixF::displace( const Point3F &delta )
  453. {
  454. m[3] += delta.x;
  455. m[3+4] += delta.y;
  456. m[3+8] += delta.z;
  457. }
  458. inline VectorF MatrixF::getForwardVector() const
  459. {
  460. VectorF vec;
  461. getColumn( 1, &vec );
  462. return vec;
  463. }
  464. inline VectorF MatrixF::getRightVector() const
  465. {
  466. VectorF vec;
  467. getColumn( 0, &vec );
  468. return vec;
  469. }
  470. inline VectorF MatrixF::getUpVector() const
  471. {
  472. VectorF vec;
  473. getColumn( 2, &vec );
  474. return vec;
  475. }
  476. //------------------------------------
  477. // Math operator overloads
  478. //------------------------------------
  479. inline MatrixF operator * ( const MatrixF &m1, const MatrixF &m2 )
  480. {
  481. // temp = m1 * m2
  482. MatrixF temp;
  483. m_matF_x_matF(m1, m2, temp);
  484. return temp;
  485. }
  486. inline MatrixF& MatrixF::operator *= ( const MatrixF &m1 )
  487. {
  488. MatrixF tempThis(*this);
  489. m_matF_x_matF(tempThis, m1, *this);
  490. return (*this);
  491. }
  492. inline MatrixF &MatrixF::operator = (const MatrixF &m1)
  493. {
  494. for (U32 i=0;i<16;i++)
  495. this->m[i] = m1.m[i];
  496. return (*this);
  497. }
  498. inline bool MatrixF::isNaN()
  499. {
  500. bool isaNaN = false;
  501. for (U32 i = 0; i < 16; i++)
  502. if (mIsNaN_F(m[i]))
  503. isaNaN = true;
  504. return isaNaN;
  505. }
  506. //------------------------------------
  507. // Non-member methods
  508. //------------------------------------
  509. inline void mTransformPlane(const MatrixF& mat, const Point3F& scale, const PlaneF& plane, PlaneF * result)
  510. {
  511. m_matF_x_scale_x_planeF(mat, &scale.x, &plane.x, &result->x);
  512. }
  513. //------------------------------------
  514. // Templatized matrix class to replace MATRIXF above
  515. //------------------------------------
  516. template<typename DATA_TYPE, U32 rows, U32 cols>
  517. class Matrix {
  518. friend class MatrixTemplateExport;
  519. private:
  520. DATA_TYPE data[rows * cols];
  521. public:
  522. static_assert(rows >= 2 && cols >= 2, "Matrix must have at least 2 rows and 2 cols.");
  523. // ------ Setters and initializers ------
  524. explicit Matrix(bool identity = false) {
  525. std::fill(data, data + (rows * cols), DATA_TYPE(0));
  526. if (identity) {
  527. for (U32 i = 0; i < rows; i++) {
  528. for (U32 j = 0; j < cols; j++) {
  529. // others already get filled with 0
  530. if (j == i)
  531. (*this)(i, j) = static_cast<DATA_TYPE>(1);
  532. }
  533. }
  534. }
  535. }
  536. explicit Matrix(const EulerF& e);
  537. /// Make this an identity matrix.
  538. Matrix<DATA_TYPE, rows, cols>& identity();
  539. Matrix<DATA_TYPE, rows, cols>& set(const EulerF& e);
  540. Matrix(const EulerF& e, const Point3F p);
  541. Matrix<DATA_TYPE, rows, cols>& set(const EulerF& e, const Point3F p);
  542. Matrix<DATA_TYPE, rows, cols>& inverse();
  543. Matrix<DATA_TYPE, rows, cols>& transpose();
  544. void invert();
  545. Matrix<DATA_TYPE, rows, cols>& setCrossProduct(const Point3F& p);
  546. Matrix<DATA_TYPE, rows, cols>& setTensorProduct(const Point3F& p, const Point3F& q);
  547. /// M * Matrix(p) -> M
  548. Matrix<DATA_TYPE, rows, cols>& scale(const Point3F& s);
  549. Matrix<DATA_TYPE, rows, cols>& scale(DATA_TYPE s) { return scale(Point3F(s, s, s)); }
  550. void setColumn(S32 col, const Point4F& cptr);
  551. void setColumn(S32 col, const Point3F& cptr);
  552. void setRow(S32 row, const Point4F& cptr);
  553. void setRow(S32 row, const Point3F& cptr);
  554. ///< M * a -> M
  555. Matrix<DATA_TYPE, rows, cols>& mul(const Matrix<DATA_TYPE, rows, cols>& a)
  556. { return *this * a; }
  557. ///< a * M -> M
  558. Matrix<DATA_TYPE, rows, cols>& mulL(const Matrix<DATA_TYPE, rows, cols>& a)
  559. { return *this = a * *this; }
  560. ///< a * b -> M
  561. Matrix<DATA_TYPE, rows, cols>& mul(const Matrix<DATA_TYPE, rows, cols>& a, const Matrix<DATA_TYPE, rows, cols>& b)
  562. { return *this = a * b; }
  563. ///< M * a -> M
  564. Matrix<DATA_TYPE, rows, cols>& mul(const F32 a)
  565. { return *this * a; }
  566. ///< a * b -> M
  567. Matrix<DATA_TYPE, rows, cols>& mul(const Matrix<DATA_TYPE, rows, cols>& a, const F32 b)
  568. { return *this = a * b; }
  569. ///< M * p -> p (full [4x4] * [1x4])
  570. void mul(Point4F& p) const { p = *this * p; }
  571. ///< M * p -> p (assume w = 1.0f)
  572. void mulP(Point3F& p) const { p = *this * p; }
  573. ///< M * p -> d (assume w = 1.0f)
  574. void mulP(const Point3F& p, Point3F* d) const { *d = *this * p; }
  575. ///< M * v -> v (assume w = 0.0f)
  576. void mulV(VectorF& v) const
  577. {
  578. AssertFatal(rows == 4 && cols == 4, "Multiplying VectorF with matrix requires 4x4");
  579. VectorF result(
  580. (*this)(0, 0) * v.x + (*this)(0, 1) * v.y + (*this)(0, 2) * v.z,
  581. (*this)(1, 0) * v.x + (*this)(1, 1) * v.y + (*this)(1, 2) * v.z,
  582. (*this)(2, 0) * v.x + (*this)(2, 1) * v.y + (*this)(2, 2) * v.z
  583. );
  584. v = result;
  585. }
  586. ///< M * v -> d (assume w = 0.0f)
  587. void mulV(const VectorF& v, Point3F* d) const
  588. {
  589. AssertFatal(rows == 4 && cols == 4, "Multiplying VectorF with matrix requires 4x4");
  590. VectorF result(
  591. (*this)(0, 0) * v.x + (*this)(0, 1) * v.y + (*this)(0, 2) * v.z,
  592. (*this)(1, 0) * v.x + (*this)(1, 1) * v.y + (*this)(1, 2) * v.z,
  593. (*this)(2, 0) * v.x + (*this)(2, 1) * v.y + (*this)(2, 2) * v.z
  594. );
  595. d->x = result.x;
  596. d->y = result.y;
  597. d->z = result.z;
  598. }
  599. ///< Axial box -> Axial Box (too big a function to be inline)
  600. void mul(Box3F& box) const;
  601. // ------ Getters ------
  602. bool isAffine() const;
  603. bool isIdentity() const;
  604. Point3F getScale() const;
  605. EulerF toEuler() const;
  606. Point3F getPosition() const;
  607. void getColumn(S32 col, Point4F* cptr) const;
  608. Point4F getColumn4F(S32 col) const { Point4F ret; getColumn(col, &ret); return ret; }
  609. void getColumn(S32 col, Point3F* cptr) const;
  610. Point3F getColumn3F(S32 col) const { Point3F ret; getColumn(col, &ret); return ret; }
  611. void getRow(S32 row, Point4F* cptr) const;
  612. Point4F getRow4F(S32 row) const { Point4F ret; getRow(row, &ret); return ret; }
  613. void getRow(S32 row, Point3F* cptr) const;
  614. Point3F getRow3F(S32 row) const { Point3F ret; getRow(row, &ret); return ret; }
  615. DATA_TYPE* getData() {
  616. return data;
  617. }
  618. const DATA_TYPE* getData() const {
  619. return data;
  620. }
  621. void dumpMatrix(const char* caption = NULL) const;
  622. // Static identity matrix
  623. static const Matrix Identity;
  624. // ------ Operators ------
  625. Matrix<DATA_TYPE, rows, cols> operator * (const Matrix<DATA_TYPE, rows, cols>& other) const {
  626. Matrix<DATA_TYPE, rows, cols> result;
  627. for (U32 i = 0; i < rows; i++) {
  628. for (U32 j = 0; j < cols; j++) {
  629. result(i, j) = 0;
  630. for (U32 k = 0; k < cols; k++) {
  631. result(i, j) += (*this)(i, k) * other(k, j);
  632. }
  633. }
  634. }
  635. return result;
  636. }
  637. Matrix<DATA_TYPE, rows, cols> operator *= (const Matrix<DATA_TYPE, rows, cols>& other) {
  638. *this = *this * other;
  639. return *this;
  640. }
  641. Matrix<DATA_TYPE, rows, cols> operator * (const DATA_TYPE scalar) const {
  642. Matrix<DATA_TYPE, rows, cols> result;
  643. for (U32 i = 0; i < rows; i++) {
  644. for (U32 j = 0; j < cols; j++) {
  645. result(i, j) = (*this)(i, j) * scalar;
  646. }
  647. }
  648. return result;
  649. }
  650. Matrix<DATA_TYPE, rows, cols>& operator *= (const DATA_TYPE scalar) {
  651. for (U32 i = 0; i < rows; i++) {
  652. for (U32 j = 0; j < cols; j++) {
  653. (*this)(i, j) *= scalar;
  654. }
  655. }
  656. return *this;
  657. }
  658. Point3F operator*(const Point3F& point) const {
  659. AssertFatal(rows == 4 && cols == 4, "Multiplying point3 with matrix requires 4x4");
  660. return Point3F(
  661. (*this)(0, 0) * point.x + (*this)(0, 1) * point.y + (*this)(0, 2) * point.z + (*this)(0, 3),
  662. (*this)(1, 0) * point.x + (*this)(1, 1) * point.y + (*this)(1, 2) * point.z + (*this)(1, 3),
  663. (*this)(2, 0) * point.x + (*this)(2, 1) * point.y + (*this)(2, 2) * point.z + (*this)(2, 3)
  664. );
  665. }
  666. Point4F operator*(const Point4F& point) const {
  667. AssertFatal(rows == 4 && cols == 4, "Multiplying point4 with matrix requires 4x4");
  668. return Point4F(
  669. (*this)(0, 0) * point.x + (*this)(0, 1) * point.y + (*this)(0, 2) * point.z + (*this)(0, 3) * point.w,
  670. (*this)(1, 0) * point.x + (*this)(1, 1) * point.y + (*this)(1, 2) * point.z + (*this)(1, 3) * point.w,
  671. (*this)(2, 0) * point.x + (*this)(2, 1) * point.y + (*this)(2, 2) * point.z + (*this)(2, 3) * point.w,
  672. (*this)(3, 0) * point.x + (*this)(3, 1) * point.y + (*this)(3, 2) * point.z + (*this)(3, 3) * point.w
  673. );
  674. }
  675. Matrix<DATA_TYPE, rows, cols>& operator = (const Matrix<DATA_TYPE, rows, cols>& other) {
  676. if (this != &other) {
  677. std::copy(other.data, other.data + rows * cols, this->data);
  678. }
  679. return *this;
  680. }
  681. bool operator == (const Matrix<DATA_TYPE, rows, cols>& other) const {
  682. for (U32 i = 0; i < rows; i++) {
  683. for (U32 j = 0; j < cols; j++) {
  684. if ((*this)(i, j) != other(i, j))
  685. return false;
  686. }
  687. }
  688. return true;
  689. }
  690. bool operator != (const Matrix<DATA_TYPE, rows, cols>& other) const {
  691. return !(*this == other);
  692. }
  693. operator DATA_TYPE* () { return (data); }
  694. operator const DATA_TYPE* () const { return (DATA_TYPE*)(data); }
  695. DATA_TYPE& operator () (U32 row, U32 col) {
  696. if (row >= rows || col >= cols)
  697. AssertFatal(false, "Matrix indices out of range");
  698. return data[col * rows + row];
  699. }
  700. const DATA_TYPE& operator () (U32 row, U32 col) const {
  701. if (row >= rows || col >= cols)
  702. AssertFatal(false, "Matrix indices out of range");
  703. return data[col * rows + row];
  704. }
  705. };
  706. //--------------------------------------------
  707. // INLINE FUNCTIONS
  708. //--------------------------------------------
  709. template<typename DATA_TYPE, U32 rows, U32 cols>
  710. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::transpose()
  711. {
  712. // square matrices can just swap, non square requires a temp mat.
  713. if (rows == cols) {
  714. for (U32 i = 0; i < rows; i++) {
  715. for (U32 j = 0; j < cols; j++) {
  716. std::swap((*this)(j, i), (*this)(i, j));
  717. }
  718. }
  719. }
  720. else {
  721. Matrix<DATA_TYPE, rows, cols> result;
  722. for (U32 i = 0; i < rows; i++) {
  723. for (U32 j = 0; j < cols; j++) {
  724. result(j, i) = (*this)(i, j);
  725. }
  726. }
  727. std::copy(std::begin(result.data), std::end(result.data), std::begin(data));
  728. }
  729. return (*this);
  730. }
  731. template<typename DATA_TYPE, U32 rows, U32 cols>
  732. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::identity()
  733. {
  734. for (U32 i = 0; i < rows; i++) {
  735. for (U32 j = 0; j < cols; j++) {
  736. if (j == i)
  737. (*this)(i, j) = static_cast<DATA_TYPE>(1);
  738. else
  739. (*this)(i, j) = static_cast<DATA_TYPE>(0);
  740. }
  741. }
  742. return (*this);
  743. }
  744. template<typename DATA_TYPE, U32 rows, U32 cols>
  745. inline Matrix<DATA_TYPE, rows, cols>& Matrix<DATA_TYPE, rows, cols>::scale(const Point3F& s)
  746. {
  747. // torques scale applies directly, does not create another matrix to multiply with the translation matrix.
  748. AssertFatal(rows >= 3 && cols >= 3, "Scale can only be applied 3x3 or more");
  749. for (U32 i = 0; i < 3; i++) {
  750. for (U32 j = 0; j < 3; j++) {
  751. DATA_TYPE scale = (i == 0) ? s.x : (i == 1) ? s.y : s.z;
  752. (*this)(i, j) *= scale;
  753. }
  754. }
  755. return (*this);
  756. }
  757. template<typename DATA_TYPE, U32 rows, U32 cols>
  758. inline bool Matrix<DATA_TYPE, rows, cols>::isIdentity() const {
  759. for (U32 i = 0; i < rows; i++) {
  760. for (U32 j = 0; j < cols; j++) {
  761. if (j == i) {
  762. if((*this)(i, j) != static_cast<DATA_TYPE>(1)) {
  763. return false;
  764. }
  765. }
  766. else {
  767. if((*this)(i, j) != static_cast<DATA_TYPE>(0)) {
  768. return false;
  769. }
  770. }
  771. }
  772. }
  773. return true;
  774. }
  775. template<typename DATA_TYPE, U32 rows, U32 cols>
  776. inline Point3F Matrix<DATA_TYPE, rows, cols>::getScale() const
  777. {
  778. // this function assumes the matrix has scale applied through the scale(const Point3F& s) function.
  779. // for now assume float since we have point3F.
  780. AssertFatal(rows >= 3 && cols >= 3, "Scale can only be applied 3x3 or more");
  781. Point3F scale;
  782. scale.x = mSqrt((*this)(0, 0) * (*this)(0, 0) + (*this)(1, 0) * (*this)(1, 0) + (*this)(2, 0) * (*this)(2, 0));
  783. scale.y = mSqrt((*this)(0, 1) * (*this)(0, 1) + (*this)(1, 1) * (*this)(1, 1) + (*this)(2, 1) * (*this)(2, 1));
  784. scale.z = mSqrt((*this)(0, 2) * (*this)(0, 2) + (*this)(1, 2) * (*this)(1, 2) + (*this)(2, 2) * (*this)(2, 2));
  785. return scale;
  786. }
  787. template<typename DATA_TYPE, U32 rows, U32 cols>
  788. inline Point3F Matrix<DATA_TYPE, rows, cols>::getPosition() const
  789. {
  790. Point3F pos;
  791. getColumn(3, &pos);
  792. return pos;
  793. }
  794. template<typename DATA_TYPE, U32 rows, U32 cols>
  795. inline void Matrix<DATA_TYPE, rows, cols>::getColumn(S32 col, Point4F* cptr) const
  796. {
  797. if (rows >= 2)
  798. {
  799. cptr->x = (*this)(0, col);
  800. cptr->y = (*this)(1, col);
  801. }
  802. if (rows >= 3)
  803. cptr->z = (*this)(2, col);
  804. else
  805. cptr->z = 0.0f;
  806. if (rows >= 4)
  807. cptr->w = (*this)(3, col);
  808. else
  809. cptr->w = 0.0f;
  810. }
  811. template<typename DATA_TYPE, U32 rows, U32 cols>
  812. inline void Matrix<DATA_TYPE, rows, cols>::getColumn(S32 col, Point3F* cptr) const
  813. {
  814. if (rows >= 2)
  815. {
  816. cptr->x = (*this)(0, col);
  817. cptr->y = (*this)(1, col);
  818. }
  819. if (rows >= 3)
  820. cptr->z = (*this)(2, col);
  821. else
  822. cptr->z = 0.0f;
  823. }
  824. template<typename DATA_TYPE, U32 rows, U32 cols>
  825. inline void Matrix<DATA_TYPE, rows, cols>::setColumn(S32 col, const Point4F &cptr) {
  826. if(rows >= 2)
  827. {
  828. (*this)(0, col) = cptr.x;
  829. (*this)(1, col) = cptr.y;
  830. }
  831. if(rows >= 3)
  832. (*this)(2, col) = cptr.z;
  833. if(rows >= 4)
  834. (*this)(3, col) = cptr.w;
  835. }
  836. template<typename DATA_TYPE, U32 rows, U32 cols>
  837. inline void Matrix<DATA_TYPE, rows, cols>::setColumn(S32 col, const Point3F &cptr) {
  838. if(rows >= 2)
  839. {
  840. (*this)(0, col) = cptr.x;
  841. (*this)(1, col) = cptr.y;
  842. }
  843. if(rows >= 3)
  844. (*this)(2, col) = cptr.z;
  845. }
  846. template<typename DATA_TYPE, U32 rows, U32 cols>
  847. inline void Matrix<DATA_TYPE, rows, cols>::getRow(S32 row, Point4F* cptr) const
  848. {
  849. if (cols >= 2)
  850. {
  851. cptr->x = (*this)(row, 0);
  852. cptr->y = (*this)(row, 1);
  853. }
  854. if (cols >= 3)
  855. cptr->z = (*this)(row, 2);
  856. else
  857. cptr->z = 0.0f;
  858. if (cols >= 4)
  859. cptr->w = (*this)(row, 3);
  860. else
  861. cptr->w = 0.0f;
  862. }
  863. template<typename DATA_TYPE, U32 rows, U32 cols>
  864. inline void Matrix<DATA_TYPE, rows, cols>::getRow(S32 row, Point3F* cptr) const
  865. {
  866. if (cols >= 2)
  867. {
  868. cptr->x = (*this)(row, 0);
  869. cptr->y = (*this)(row, 1);
  870. }
  871. if (cols >= 3)
  872. cptr->z = (*this)(row, 2);
  873. else
  874. cptr->z = 0.0f;
  875. }
  876. template<typename DATA_TYPE, U32 rows, U32 cols>
  877. inline void Matrix<DATA_TYPE, rows, cols>::setRow(S32 row, const Point4F& cptr) {
  878. if(cols >= 2)
  879. {
  880. (*this)(row, 0) = cptr.x;
  881. (*this)(row, 1) = cptr.y;
  882. }
  883. if(cols >= 3)
  884. (*this)(row, 2) = cptr.z;
  885. if(cols >= 4)
  886. (*this)(row, 3) = cptr.w;
  887. }
  888. template<typename DATA_TYPE, U32 rows, U32 cols>
  889. inline void Matrix<DATA_TYPE, rows, cols>::setRow(S32 row, const Point3F& cptr) {
  890. if(cols >= 2)
  891. {
  892. (*this)(row, 0) = cptr.x;
  893. (*this)(row, 1) = cptr.y;
  894. }
  895. if(cols >= 3)
  896. (*this)(row, 2) = cptr.z;
  897. }
  898. //--------------------------------------------
  899. // INLINE FUNCTIONS END
  900. //--------------------------------------------
  901. typedef Matrix<F32, 4, 4> Matrix4F;
  902. class MatrixTemplateExport
  903. {
  904. public:
  905. template <typename T, U32 rows, U32 cols>
  906. static EngineFieldTable::Field getMatrixField();
  907. };
  908. template<typename T, U32 rows, U32 cols>
  909. inline EngineFieldTable::Field MatrixTemplateExport::getMatrixField()
  910. {
  911. typedef Matrix<T, rows, cols> ThisType;
  912. return _FIELD_AS(T, data, data, rows * cols, "");
  913. }
  914. #endif //_MMATRIX_H_