IceMatrix4x4.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. /**
  3. * Contains code for 4x4 matrices.
  4. * \file IceMatrix4x4.h
  5. * \author Pierre Terdiman
  6. * \date April, 4, 2000
  7. */
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  9. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  10. // Include Guard
  11. #ifndef __ICEMATRIX4X4_H__
  12. #define __ICEMATRIX4X4_H__
  13. // Forward declarations
  14. class PRS;
  15. class PR;
  16. #define MATRIX4X4_EPSILON (1.0e-7f)
  17. class ICEMATHS_API Matrix4x4
  18. {
  19. // void LUBackwardSubstitution( sdword *indx, float* b );
  20. // void LUDecomposition( sdword* indx, float* d );
  21. public:
  22. //! Empty constructor.
  23. inline_ Matrix4x4() {}
  24. //! Constructor from 16 values
  25. inline_ Matrix4x4( float m00, float m01, float m02, float m03,
  26. float m10, float m11, float m12, float m13,
  27. float m20, float m21, float m22, float m23,
  28. float m30, float m31, float m32, float m33)
  29. {
  30. m[0][0] = m00; m[0][1] = m01; m[0][2] = m02; m[0][3] = m03;
  31. m[1][0] = m10; m[1][1] = m11; m[1][2] = m12; m[1][3] = m13;
  32. m[2][0] = m20; m[2][1] = m21; m[2][2] = m22; m[2][3] = m23;
  33. m[3][0] = m30; m[3][1] = m31; m[3][2] = m32; m[3][3] = m33;
  34. }
  35. //! Copy constructor
  36. inline_ Matrix4x4(const Matrix4x4& mat) { CopyMemory(m, &mat.m, 16*sizeof(float)); }
  37. //! Destructor.
  38. inline_ ~Matrix4x4() {}
  39. //! Assign values (rotation only)
  40. template<typename trotationfloat>
  41. inline_ Matrix4x4& Set( trotationfloat m00, trotationfloat m01, trotationfloat m02,
  42. trotationfloat m10, trotationfloat m11, trotationfloat m12,
  43. trotationfloat m20, trotationfloat m21, trotationfloat m22)
  44. {
  45. m[0][0] = (float)m00; m[0][1] = (float)m01; m[0][2] = (float)m02;
  46. m[1][0] = (float)m10; m[1][1] = (float)m11; m[1][2] = (float)m12;
  47. m[2][0] = (float)m20; m[2][1] = (float)m21; m[2][2] = (float)m22;
  48. return *this;
  49. }
  50. //! Assign values
  51. template<typename trotationfloat, typename toffsetfloat, typename textrafloat>
  52. inline_ Matrix4x4& Set( trotationfloat m00, trotationfloat m01, trotationfloat m02, textrafloat m03,
  53. trotationfloat m10, trotationfloat m11, trotationfloat m12, textrafloat m13,
  54. trotationfloat m20, trotationfloat m21, trotationfloat m22, textrafloat m23,
  55. toffsetfloat m30, toffsetfloat m31, toffsetfloat m32, textrafloat m33)
  56. {
  57. m[0][0] = (float)m00; m[0][1] = (float)m01; m[0][2] = (float)m02; m[0][3] = (float)m03;
  58. m[1][0] = (float)m10; m[1][1] = (float)m11; m[1][2] = (float)m12; m[1][3] = (float)m13;
  59. m[2][0] = (float)m20; m[2][1] = (float)m21; m[2][2] = (float)m22; m[2][3] = (float)m23;
  60. m[3][0] = (float)m30; m[3][1] = (float)m31; m[3][2] = (float)m32; m[3][3] = (float)m33;
  61. return *this;
  62. }
  63. //! Copy from a Matrix4x4
  64. inline_ void Copy(const Matrix4x4& source) { CopyMemory(m, source.m, 16*sizeof(float)); }
  65. // Row-column access
  66. //! Returns a row.
  67. inline_ void GetRow(const udword r, HPoint& p) const { p.x=m[r][0]; p.y=m[r][1]; p.z=m[r][2]; p.w=m[r][3]; }
  68. //! Returns a row.
  69. inline_ void GetRow(const udword r, Point& p) const { p.x=m[r][0]; p.y=m[r][1]; p.z=m[r][2]; }
  70. //! Returns a row.
  71. inline_ const HPoint& GetRow(const udword r) const { return *(const HPoint*)&m[r][0]; }
  72. //! Returns a row.
  73. inline_ HPoint& GetRow(const udword r) { return *(HPoint*)&m[r][0]; }
  74. //! Sets a row.
  75. inline_ void SetRow(const udword r, const HPoint& p) { m[r][0]=p.x; m[r][1]=p.y; m[r][2]=p.z; m[r][3]=p.w; }
  76. //! Sets a row.
  77. inline_ void SetRow(const udword r, const Point& p) { m[r][0]=p.x; m[r][1]=p.y; m[r][2]=p.z; m[r][3]= (r!=3) ? 0.0f : 1.0f; }
  78. //! Returns a column.
  79. inline_ void GetCol(const udword c, HPoint& p) const { p.x=m[0][c]; p.y=m[1][c]; p.z=m[2][c]; p.w=m[3][c]; }
  80. //! Returns a column.
  81. inline_ void GetCol(const udword c, Point& p) const { p.x=m[0][c]; p.y=m[1][c]; p.z=m[2][c]; }
  82. //! Sets a column.
  83. inline_ void SetCol(const udword c, const HPoint& p) { m[0][c]=p.x; m[1][c]=p.y; m[2][c]=p.z; m[3][c]=p.w; }
  84. //! Sets a column.
  85. inline_ void SetCol(const udword c, const Point& p) { m[0][c]=p.x; m[1][c]=p.y; m[2][c]=p.z; m[3][c]= (c!=3) ? 0.0f : 1.0f; }
  86. // Translation
  87. //! Returns the translation part of the matrix.
  88. inline_ const HPoint& GetTrans() const { return GetRow(3); }
  89. //! Gets the translation part of the matrix
  90. inline_ void GetTrans(Point& p) const { p.x=m[3][0]; p.y=m[3][1]; p.z=m[3][2]; }
  91. //! Sets the translation part of the matrix, from a Point.
  92. inline_ void SetTrans(const Point& p) { m[3][0]=p.x; m[3][1]=p.y; m[3][2]=p.z; }
  93. //! Sets the translation part of the matrix, from a HPoint.
  94. inline_ void SetTrans(const HPoint& p) { m[3][0]=p.x; m[3][1]=p.y; m[3][2]=p.z; m[3][3]=p.w; }
  95. //! Sets the translation part of the matrix, from floats.
  96. inline_ void SetTrans(float tx, float ty, float tz) { m[3][0]=tx; m[3][1]=ty; m[3][2]=tz; }
  97. // Scale
  98. //! Sets the scale from a Point. The point is put on the diagonal.
  99. inline_ void SetScale(const Point& p) { m[0][0]=p.x; m[1][1]=p.y; m[2][2]=p.z; }
  100. //! Sets the scale from floats. Values are put on the diagonal.
  101. inline_ void SetScale(float sx, float sy, float sz) { m[0][0]=sx; m[1][1]=sy; m[2][2]=sz; }
  102. //! Scales from a Point. Each row is multiplied by a component.
  103. void Scale(const Point& p)
  104. {
  105. m[0][0] *= p.x; m[1][0] *= p.y; m[2][0] *= p.z;
  106. m[0][1] *= p.x; m[1][1] *= p.y; m[2][1] *= p.z;
  107. m[0][2] *= p.x; m[1][2] *= p.y; m[2][2] *= p.z;
  108. }
  109. //! Scales from floats. Each row is multiplied by a value.
  110. void Scale(float sx, float sy, float sz)
  111. {
  112. m[0][0] *= sx; m[1][0] *= sy; m[2][0] *= sz;
  113. m[0][1] *= sx; m[1][1] *= sy; m[2][1] *= sz;
  114. m[0][2] *= sx; m[1][2] *= sy; m[2][2] *= sz;
  115. }
  116. /*
  117. //! Returns a row.
  118. inline_ HPoint GetRow(const udword row) const { return mRow[row]; }
  119. //! Sets a row.
  120. inline_ Matrix4x4& SetRow(const udword row, const HPoint& p) { mRow[row] = p; return *this; }
  121. //! Sets a row.
  122. Matrix4x4& SetRow(const udword row, const Point& p)
  123. {
  124. m[row][0] = p.x;
  125. m[row][1] = p.y;
  126. m[row][2] = p.z;
  127. m[row][3] = (row != 3) ? 0.0f : 1.0f;
  128. return *this;
  129. }
  130. //! Returns a column.
  131. HPoint GetCol(const udword col) const
  132. {
  133. HPoint Res;
  134. Res.x = m[0][col];
  135. Res.y = m[1][col];
  136. Res.z = m[2][col];
  137. Res.w = m[3][col];
  138. return Res;
  139. }
  140. //! Sets a column.
  141. Matrix4x4& SetCol(const udword col, const HPoint& p)
  142. {
  143. m[0][col] = p.x;
  144. m[1][col] = p.y;
  145. m[2][col] = p.z;
  146. m[3][col] = p.w;
  147. return *this;
  148. }
  149. //! Sets a column.
  150. Matrix4x4& SetCol(const udword col, const Point& p)
  151. {
  152. m[0][col] = p.x;
  153. m[1][col] = p.y;
  154. m[2][col] = p.z;
  155. m[3][col] = (col != 3) ? 0.0f : 1.0f;
  156. return *this;
  157. }
  158. */
  159. //! Computes the trace. The trace is the sum of the 4 diagonal components.
  160. inline_ float Trace() const { return m[0][0] + m[1][1] + m[2][2] + m[3][3]; }
  161. //! Computes the trace of the upper 3x3 matrix.
  162. inline_ float Trace3x3() const { return m[0][0] + m[1][1] + m[2][2]; }
  163. //! Clears the matrix.
  164. inline_ void Zero() { ZeroMemory(&m, sizeof(m)); }
  165. //! Sets the identity matrix.
  166. inline_ void Identity() { Zero(); m[0][0] = m[1][1] = m[2][2] = m[3][3] = 1.0f; }
  167. //! Checks for identity
  168. inline_ bool IsIdentity() const
  169. {
  170. if(IR(m[0][0])!=IEEE_1_0) return false;
  171. if(IR(m[0][1])!=0) return false;
  172. if(IR(m[0][2])!=0) return false;
  173. if(IR(m[0][3])!=0) return false;
  174. if(IR(m[1][0])!=0) return false;
  175. if(IR(m[1][1])!=IEEE_1_0) return false;
  176. if(IR(m[1][2])!=0) return false;
  177. if(IR(m[1][3])!=0) return false;
  178. if(IR(m[2][0])!=0) return false;
  179. if(IR(m[2][1])!=0) return false;
  180. if(IR(m[2][2])!=IEEE_1_0) return false;
  181. if(IR(m[2][3])!=0) return false;
  182. if(IR(m[3][0])!=0) return false;
  183. if(IR(m[3][1])!=0) return false;
  184. if(IR(m[3][2])!=0) return false;
  185. if(IR(m[3][3])!=IEEE_1_0) return false;
  186. return true;
  187. }
  188. //! Checks matrix validity
  189. inline_ BOOL IsValid() const
  190. {
  191. for(udword j=0;j<4;j++)
  192. {
  193. for(udword i=0;i<4;i++)
  194. {
  195. if(!IsValidFloat(m[j][i])) return FALSE;
  196. }
  197. }
  198. return TRUE;
  199. }
  200. //! Sets a rotation matrix around the X axis.
  201. void RotX(float angle) { float Cos = cosf(angle), Sin = sinf(angle); Identity(); m[1][1] = m[2][2] = Cos; m[2][1] = -Sin; m[1][2] = Sin; }
  202. //! Sets a rotation matrix around the Y axis.
  203. void RotY(float angle) { float Cos = cosf(angle), Sin = sinf(angle); Identity(); m[0][0] = m[2][2] = Cos; m[2][0] = Sin; m[0][2] = -Sin; }
  204. //! Sets a rotation matrix around the Z axis.
  205. void RotZ(float angle) { float Cos = cosf(angle), Sin = sinf(angle); Identity(); m[0][0] = m[1][1] = Cos; m[1][0] = -Sin; m[0][1] = Sin; }
  206. //! Makes a rotation matrix about an arbitrary axis
  207. Matrix4x4& Rot(float angle, Point& p1, Point& p2);
  208. //! Transposes the matrix.
  209. void Transpose()
  210. {
  211. TSwap(m[1][0], m[0][1]);
  212. TSwap(m[2][0], m[0][2]);
  213. TSwap(m[3][0], m[0][3]);
  214. TSwap(m[1][2], m[2][1]);
  215. TSwap(m[1][3], m[3][1]);
  216. TSwap(m[2][3], m[3][2]);
  217. }
  218. //! Computes a cofactor. Used for matrix inversion.
  219. float CoFactor(udword row, udword col) const;
  220. //! Computes the determinant of the matrix.
  221. float Determinant() const;
  222. //! Inverts the matrix. Determinant must be different from zero, else matrix can't be inverted.
  223. Matrix4x4& Invert();
  224. // Matrix& ComputeAxisMatrix(Point& axis, float angle);
  225. // Cast operators
  226. //! Casts a Matrix4x4 to a Matrix3x3.
  227. inline_ operator Matrix3x3() const
  228. {
  229. return Matrix3x3(
  230. m[0][0], m[0][1], m[0][2],
  231. m[1][0], m[1][1], m[1][2],
  232. m[2][0], m[2][1], m[2][2]);
  233. }
  234. //! Casts a Matrix4x4 to a Quat.
  235. operator Quat() const;
  236. //! Casts a Matrix4x4 to a PR.
  237. operator PR() const;
  238. // Arithmetic operators
  239. //! Operator for Matrix4x4 Plus = Matrix4x4 + Matrix4x4;
  240. inline_ Matrix4x4 operator+(const Matrix4x4& mat) const
  241. {
  242. return Matrix4x4(
  243. m[0][0]+mat.m[0][0], m[0][1]+mat.m[0][1], m[0][2]+mat.m[0][2], m[0][3]+mat.m[0][3],
  244. m[1][0]+mat.m[1][0], m[1][1]+mat.m[1][1], m[1][2]+mat.m[1][2], m[1][3]+mat.m[1][3],
  245. m[2][0]+mat.m[2][0], m[2][1]+mat.m[2][1], m[2][2]+mat.m[2][2], m[2][3]+mat.m[2][3],
  246. m[3][0]+mat.m[3][0], m[3][1]+mat.m[3][1], m[3][2]+mat.m[3][2], m[3][3]+mat.m[3][3]);
  247. }
  248. //! Operator for Matrix4x4 Minus = Matrix4x4 - Matrix4x4;
  249. inline_ Matrix4x4 operator-(const Matrix4x4& mat) const
  250. {
  251. return Matrix4x4(
  252. m[0][0]-mat.m[0][0], m[0][1]-mat.m[0][1], m[0][2]-mat.m[0][2], m[0][3]-mat.m[0][3],
  253. m[1][0]-mat.m[1][0], m[1][1]-mat.m[1][1], m[1][2]-mat.m[1][2], m[1][3]-mat.m[1][3],
  254. m[2][0]-mat.m[2][0], m[2][1]-mat.m[2][1], m[2][2]-mat.m[2][2], m[2][3]-mat.m[2][3],
  255. m[3][0]-mat.m[3][0], m[3][1]-mat.m[3][1], m[3][2]-mat.m[3][2], m[3][3]-mat.m[3][3]);
  256. }
  257. //! Operator for Matrix4x4 Mul = Matrix4x4 * Matrix4x4;
  258. inline_ Matrix4x4 operator*(const Matrix4x4& mat) const
  259. {
  260. return Matrix4x4(
  261. m[0][0]*mat.m[0][0] + m[0][1]*mat.m[1][0] + m[0][2]*mat.m[2][0] + m[0][3]*mat.m[3][0],
  262. m[0][0]*mat.m[0][1] + m[0][1]*mat.m[1][1] + m[0][2]*mat.m[2][1] + m[0][3]*mat.m[3][1],
  263. m[0][0]*mat.m[0][2] + m[0][1]*mat.m[1][2] + m[0][2]*mat.m[2][2] + m[0][3]*mat.m[3][2],
  264. m[0][0]*mat.m[0][3] + m[0][1]*mat.m[1][3] + m[0][2]*mat.m[2][3] + m[0][3]*mat.m[3][3],
  265. m[1][0]*mat.m[0][0] + m[1][1]*mat.m[1][0] + m[1][2]*mat.m[2][0] + m[1][3]*mat.m[3][0],
  266. m[1][0]*mat.m[0][1] + m[1][1]*mat.m[1][1] + m[1][2]*mat.m[2][1] + m[1][3]*mat.m[3][1],
  267. m[1][0]*mat.m[0][2] + m[1][1]*mat.m[1][2] + m[1][2]*mat.m[2][2] + m[1][3]*mat.m[3][2],
  268. m[1][0]*mat.m[0][3] + m[1][1]*mat.m[1][3] + m[1][2]*mat.m[2][3] + m[1][3]*mat.m[3][3],
  269. m[2][0]*mat.m[0][0] + m[2][1]*mat.m[1][0] + m[2][2]*mat.m[2][0] + m[2][3]*mat.m[3][0],
  270. m[2][0]*mat.m[0][1] + m[2][1]*mat.m[1][1] + m[2][2]*mat.m[2][1] + m[2][3]*mat.m[3][1],
  271. m[2][0]*mat.m[0][2] + m[2][1]*mat.m[1][2] + m[2][2]*mat.m[2][2] + m[2][3]*mat.m[3][2],
  272. m[2][0]*mat.m[0][3] + m[2][1]*mat.m[1][3] + m[2][2]*mat.m[2][3] + m[2][3]*mat.m[3][3],
  273. m[3][0]*mat.m[0][0] + m[3][1]*mat.m[1][0] + m[3][2]*mat.m[2][0] + m[3][3]*mat.m[3][0],
  274. m[3][0]*mat.m[0][1] + m[3][1]*mat.m[1][1] + m[3][2]*mat.m[2][1] + m[3][3]*mat.m[3][1],
  275. m[3][0]*mat.m[0][2] + m[3][1]*mat.m[1][2] + m[3][2]*mat.m[2][2] + m[3][3]*mat.m[3][2],
  276. m[3][0]*mat.m[0][3] + m[3][1]*mat.m[1][3] + m[3][2]*mat.m[2][3] + m[3][3]*mat.m[3][3]);
  277. }
  278. //! Operator for HPoint Mul = Matrix4x4 * HPoint;
  279. inline_ HPoint operator*(const HPoint& v) const { return HPoint(GetRow(0)|v, GetRow(1)|v, GetRow(2)|v, GetRow(3)|v); }
  280. //! Operator for Point Mul = Matrix4x4 * Point;
  281. inline_ Point operator*(const Point& v) const
  282. {
  283. return Point( m[0][0]*v.x + m[0][1]*v.y + m[0][2]*v.z + m[0][3],
  284. m[1][0]*v.x + m[1][1]*v.y + m[1][2]*v.z + m[1][3],
  285. m[2][0]*v.x + m[2][1]*v.y + m[2][2]*v.z + m[2][3] );
  286. }
  287. //! Operator for Matrix4x4 Scale = Matrix4x4 * float;
  288. inline_ Matrix4x4 operator*(float s) const
  289. {
  290. return Matrix4x4(
  291. m[0][0]*s, m[0][1]*s, m[0][2]*s, m[0][3]*s,
  292. m[1][0]*s, m[1][1]*s, m[1][2]*s, m[1][3]*s,
  293. m[2][0]*s, m[2][1]*s, m[2][2]*s, m[2][3]*s,
  294. m[3][0]*s, m[3][1]*s, m[3][2]*s, m[3][3]*s);
  295. }
  296. //! Operator for Matrix4x4 Scale = float * Matrix4x4;
  297. inline_ friend Matrix4x4 operator*(float s, const Matrix4x4& mat)
  298. {
  299. return Matrix4x4(
  300. s*mat.m[0][0], s*mat.m[0][1], s*mat.m[0][2], s*mat.m[0][3],
  301. s*mat.m[1][0], s*mat.m[1][1], s*mat.m[1][2], s*mat.m[1][3],
  302. s*mat.m[2][0], s*mat.m[2][1], s*mat.m[2][2], s*mat.m[2][3],
  303. s*mat.m[3][0], s*mat.m[3][1], s*mat.m[3][2], s*mat.m[3][3]);
  304. }
  305. //! Operator for Matrix4x4 Div = Matrix4x4 / float;
  306. inline_ Matrix4x4 operator/(float s) const
  307. {
  308. if(s) s = 1.0f / s;
  309. return Matrix4x4(
  310. m[0][0]*s, m[0][1]*s, m[0][2]*s, m[0][3]*s,
  311. m[1][0]*s, m[1][1]*s, m[1][2]*s, m[1][3]*s,
  312. m[2][0]*s, m[2][1]*s, m[2][2]*s, m[2][3]*s,
  313. m[3][0]*s, m[3][1]*s, m[3][2]*s, m[3][3]*s);
  314. }
  315. //! Operator for Matrix4x4 Div = float / Matrix4x4;
  316. inline_ friend Matrix4x4 operator/(float s, const Matrix4x4& mat)
  317. {
  318. return Matrix4x4(
  319. s/mat.m[0][0], s/mat.m[0][1], s/mat.m[0][2], s/mat.m[0][3],
  320. s/mat.m[1][0], s/mat.m[1][1], s/mat.m[1][2], s/mat.m[1][3],
  321. s/mat.m[2][0], s/mat.m[2][1], s/mat.m[2][2], s/mat.m[2][3],
  322. s/mat.m[3][0], s/mat.m[3][1], s/mat.m[3][2], s/mat.m[3][3]);
  323. }
  324. //! Operator for Matrix4x4 += Matrix4x4;
  325. inline_ Matrix4x4& operator+=(const Matrix4x4& mat)
  326. {
  327. m[0][0]+=mat.m[0][0]; m[0][1]+=mat.m[0][1]; m[0][2]+=mat.m[0][2]; m[0][3]+=mat.m[0][3];
  328. m[1][0]+=mat.m[1][0]; m[1][1]+=mat.m[1][1]; m[1][2]+=mat.m[1][2]; m[1][3]+=mat.m[1][3];
  329. m[2][0]+=mat.m[2][0]; m[2][1]+=mat.m[2][1]; m[2][2]+=mat.m[2][2]; m[2][3]+=mat.m[2][3];
  330. m[3][0]+=mat.m[3][0]; m[3][1]+=mat.m[3][1]; m[3][2]+=mat.m[3][2]; m[3][3]+=mat.m[3][3];
  331. return *this;
  332. }
  333. //! Operator for Matrix4x4 -= Matrix4x4;
  334. inline_ Matrix4x4& operator-=(const Matrix4x4& mat)
  335. {
  336. m[0][0]-=mat.m[0][0]; m[0][1]-=mat.m[0][1]; m[0][2]-=mat.m[0][2]; m[0][3]-=mat.m[0][3];
  337. m[1][0]-=mat.m[1][0]; m[1][1]-=mat.m[1][1]; m[1][2]-=mat.m[1][2]; m[1][3]-=mat.m[1][3];
  338. m[2][0]-=mat.m[2][0]; m[2][1]-=mat.m[2][1]; m[2][2]-=mat.m[2][2]; m[2][3]-=mat.m[2][3];
  339. m[3][0]-=mat.m[3][0]; m[3][1]-=mat.m[3][1]; m[3][2]-=mat.m[3][2]; m[3][3]-=mat.m[3][3];
  340. return *this;
  341. }
  342. //! Operator for Matrix4x4 *= Matrix4x4;
  343. Matrix4x4& operator*=(const Matrix4x4& mat)
  344. {
  345. HPoint TempRow;
  346. GetRow(0, TempRow);
  347. m[0][0] = TempRow.x*mat.m[0][0] + TempRow.y*mat.m[1][0] + TempRow.z*mat.m[2][0] + TempRow.w*mat.m[3][0];
  348. m[0][1] = TempRow.x*mat.m[0][1] + TempRow.y*mat.m[1][1] + TempRow.z*mat.m[2][1] + TempRow.w*mat.m[3][1];
  349. m[0][2] = TempRow.x*mat.m[0][2] + TempRow.y*mat.m[1][2] + TempRow.z*mat.m[2][2] + TempRow.w*mat.m[3][2];
  350. m[0][3] = TempRow.x*mat.m[0][3] + TempRow.y*mat.m[1][3] + TempRow.z*mat.m[2][3] + TempRow.w*mat.m[3][3];
  351. GetRow(1, TempRow);
  352. m[1][0] = TempRow.x*mat.m[0][0] + TempRow.y*mat.m[1][0] + TempRow.z*mat.m[2][0] + TempRow.w*mat.m[3][0];
  353. m[1][1] = TempRow.x*mat.m[0][1] + TempRow.y*mat.m[1][1] + TempRow.z*mat.m[2][1] + TempRow.w*mat.m[3][1];
  354. m[1][2] = TempRow.x*mat.m[0][2] + TempRow.y*mat.m[1][2] + TempRow.z*mat.m[2][2] + TempRow.w*mat.m[3][2];
  355. m[1][3] = TempRow.x*mat.m[0][3] + TempRow.y*mat.m[1][3] + TempRow.z*mat.m[2][3] + TempRow.w*mat.m[3][3];
  356. GetRow(2, TempRow);
  357. m[2][0] = TempRow.x*mat.m[0][0] + TempRow.y*mat.m[1][0] + TempRow.z*mat.m[2][0] + TempRow.w*mat.m[3][0];
  358. m[2][1] = TempRow.x*mat.m[0][1] + TempRow.y*mat.m[1][1] + TempRow.z*mat.m[2][1] + TempRow.w*mat.m[3][1];
  359. m[2][2] = TempRow.x*mat.m[0][2] + TempRow.y*mat.m[1][2] + TempRow.z*mat.m[2][2] + TempRow.w*mat.m[3][2];
  360. m[2][3] = TempRow.x*mat.m[0][3] + TempRow.y*mat.m[1][3] + TempRow.z*mat.m[2][3] + TempRow.w*mat.m[3][3];
  361. GetRow(3, TempRow);
  362. m[3][0] = TempRow.x*mat.m[0][0] + TempRow.y*mat.m[1][0] + TempRow.z*mat.m[2][0] + TempRow.w*mat.m[3][0];
  363. m[3][1] = TempRow.x*mat.m[0][1] + TempRow.y*mat.m[1][1] + TempRow.z*mat.m[2][1] + TempRow.w*mat.m[3][1];
  364. m[3][2] = TempRow.x*mat.m[0][2] + TempRow.y*mat.m[1][2] + TempRow.z*mat.m[2][2] + TempRow.w*mat.m[3][2];
  365. m[3][3] = TempRow.x*mat.m[0][3] + TempRow.y*mat.m[1][3] + TempRow.z*mat.m[2][3] + TempRow.w*mat.m[3][3];
  366. return *this;
  367. }
  368. //! Operator for Matrix4x4 *= float;
  369. inline_ Matrix4x4& operator*=(float s)
  370. {
  371. m[0][0]*=s; m[0][1]*=s; m[0][2]*=s; m[0][3]*=s;
  372. m[1][0]*=s; m[1][1]*=s; m[1][2]*=s; m[1][3]*=s;
  373. m[2][0]*=s; m[2][1]*=s; m[2][2]*=s; m[2][3]*=s;
  374. m[3][0]*=s; m[3][1]*=s; m[3][2]*=s; m[3][3]*=s;
  375. return *this;
  376. }
  377. //! Operator for Matrix4x4 /= float;
  378. inline_ Matrix4x4& operator/=(float s)
  379. {
  380. if(s) s = 1.0f / s;
  381. m[0][0]*=s; m[0][1]*=s; m[0][2]*=s; m[0][3]*=s;
  382. m[1][0]*=s; m[1][1]*=s; m[1][2]*=s; m[1][3]*=s;
  383. m[2][0]*=s; m[2][1]*=s; m[2][2]*=s; m[2][3]*=s;
  384. m[3][0]*=s; m[3][1]*=s; m[3][2]*=s; m[3][3]*=s;
  385. return *this;
  386. }
  387. inline_ const HPoint& operator[](int row) const { return *(const HPoint*)&m[row][0]; }
  388. inline_ HPoint& operator[](int row) { return *(HPoint*)&m[row][0]; }
  389. public:
  390. float m[4][4];
  391. };
  392. //! Quickly rotates & translates a vector, using the 4x3 part of a 4x4 matrix
  393. inline_ void TransformPoint4x3(Point& dest, const Point& source, const Matrix4x4& rot)
  394. {
  395. dest.x = rot.m[3][0] + source.x * rot.m[0][0] + source.y * rot.m[1][0] + source.z * rot.m[2][0];
  396. dest.y = rot.m[3][1] + source.x * rot.m[0][1] + source.y * rot.m[1][1] + source.z * rot.m[2][1];
  397. dest.z = rot.m[3][2] + source.x * rot.m[0][2] + source.y * rot.m[1][2] + source.z * rot.m[2][2];
  398. }
  399. //! Quickly rotates a vector, using the 3x3 part of a 4x4 matrix
  400. inline_ void TransformPoint3x3(Point& dest, const Point& source, const Matrix4x4& rot)
  401. {
  402. dest.x = source.x * rot.m[0][0] + source.y * rot.m[1][0] + source.z * rot.m[2][0];
  403. dest.y = source.x * rot.m[0][1] + source.y * rot.m[1][1] + source.z * rot.m[2][1];
  404. dest.z = source.x * rot.m[0][2] + source.y * rot.m[1][2] + source.z * rot.m[2][2];
  405. }
  406. ICEMATHS_API void InvertPRMatrix(Matrix4x4& dest, const Matrix4x4& src);
  407. #endif // __ICEMATRIX4X4_H__