matrix.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #ifndef _matrix_h_
  2. #define _matrix_h_
  3. //////////////////////////////////////////////////////////////////////////////
  4. //
  5. // Transform Types
  6. //
  7. //////////////////////////////////////////////////////////////////////////////
  8. typedef DWORD TransformType;
  9. #define TransformUnknown -1
  10. #define TransformIdentity 0
  11. #define TransformTranslate 1
  12. #define TransformScale 2
  13. #define TransformRotate 4
  14. #define TransformPerspective 8
  15. //////////////////////////////////////////////////////////////////////////////
  16. //
  17. // Determinants
  18. //
  19. //////////////////////////////////////////////////////////////////////////////
  20. template<class Type>
  21. inline Type Determinant2( // 2 mul 1 add
  22. Type m00, Type m01,
  23. Type m10, Type m11
  24. ) {
  25. return m00 * m11 - m10 * m01;
  26. }
  27. template<class Type>
  28. inline Type Determinant3( // 9 mul 5 add
  29. Type m00, Type m01, Type m02,
  30. Type m10, Type m11, Type m12,
  31. Type m20, Type m21, Type m22
  32. ) {
  33. Type m0 = m00 * Determinant2(m11, m12, m21, m22);
  34. Type m1 = m10 * Determinant2(m21, m22, m01, m02);
  35. Type m2 = m20 * Determinant2(m01, m02, m11, m12);
  36. return m0 + m1 + m2;
  37. }
  38. float Determinant(
  39. float m00, float m01, float m02, float m03,
  40. float m10, float m11, float m12, float m13,
  41. float m20, float m21, float m22, float m23,
  42. float m30, float m31, float m32, float m33
  43. );
  44. //////////////////////////////////////////////////////////////////////////////
  45. //
  46. // Matrix2
  47. //
  48. //////////////////////////////////////////////////////////////////////////////
  49. class Matrix2 {
  50. private:
  51. float m_m[3][3];
  52. TransformType m_type;
  53. void CalcType();
  54. public:
  55. static const Matrix2& GetIdentity();
  56. Matrix2() : m_type(TransformUnknown) {}
  57. Matrix2(
  58. float v00, float v01, float v02,
  59. float v10, float v11, float v12,
  60. float v20 = 0, float v21 = 0, float v22 = 1
  61. );
  62. //
  63. // Types
  64. //
  65. TransformType GetType() const;
  66. void InvalidateType();
  67. //
  68. // operators
  69. //
  70. friend bool operator==(const Matrix& m1, const Matrix& m2);
  71. friend bool operator!=(const Matrix& m1, const Matrix& m2);
  72. friend Matrix operator*(const Matrix& m1, const Matrix& m2);
  73. //
  74. // Accessors
  75. //
  76. const float* operator[](int row) const { return m_m[row]; }
  77. float GetScale() const;
  78. Point GetTranslate() const;
  79. //
  80. // Set Methods
  81. //
  82. Matrix2& SetIdentity();
  83. void Set(int row, int col, float value);
  84. Matrix2& SetScale(float scale);
  85. Matrix2& Scale(float scale);
  86. Matrix2& PreScale(float scale);
  87. Matrix2& SetScale(const Point& point);
  88. Matrix2& Scale(const Point& point);
  89. Matrix2& PreScale(const Point& point);
  90. Matrix2& SetTranslate(const Point& point);
  91. Matrix2& Translate(const Point& point);
  92. Matrix2& PreTranslate(const Point& point);
  93. Matrix2& SetRotate(float angle);
  94. Matrix2& Rotate(float angle);
  95. Matrix2& PreRotate(float angle);
  96. Matrix2& SetMultiply(const Matrix2& m1, const Matrix2& m2);
  97. Matrix2& Multiply(const Matrix2& m2);
  98. Matrix2& PreMultiply(const Matrix2& m1);
  99. float Determinant() const;
  100. Matrix2& SetInverse(const Matrix2& m);
  101. Matrix2& Inverse();
  102. Matrix2& SetTranspose(const Matrix2& m);
  103. Matrix2& Transpose();
  104. //
  105. // Transforms
  106. //
  107. Point Transform(const Point& point) const;
  108. void Transform(const TVector<Point>&, TVector<Point>&) const;
  109. };
  110. //////////////////////////////////////////////////////////////////////////////
  111. //
  112. // Matrix
  113. //
  114. //////////////////////////////////////////////////////////////////////////////
  115. class Matrix {
  116. private:
  117. float m_m[4][4];
  118. TransformType m_type;
  119. void CalcType();
  120. public:
  121. static const Matrix& GetIdentity();
  122. Matrix() : m_type(TransformUnknown) {}
  123. Matrix(const Matrix2& mat2);
  124. Matrix(
  125. float v00, float v01, float v02, float v03,
  126. float v10, float v11, float v12, float v13,
  127. float v20, float v21, float v22, float v23,
  128. float v30 = 0, float v31 = 0, float v32 = 0, float v33 = 1
  129. );
  130. Matrix(
  131. const Orientation& orientation,
  132. const Vector& position,
  133. float scale);
  134. //
  135. // Types
  136. //
  137. TransformType GetType() const;
  138. void InvalidateType();
  139. //
  140. // Operators
  141. //
  142. friend bool operator==(const Matrix& m1, const Matrix& m2);
  143. friend bool operator!=(const Matrix& m1, const Matrix& m2);
  144. friend Matrix operator*(const Matrix& m1, const Matrix& m2);
  145. //
  146. // Accessors
  147. //
  148. const float* operator[](int row) const { return m_m[row]; }
  149. float GetScale() const;
  150. Vector GetTranslate() const;
  151. //
  152. // Set Methods
  153. //
  154. Matrix& SetIdentity();
  155. void Set(int row, int col, float value);
  156. Matrix& SetLookAtFrom(const Vector& vecAt, const Vector& vecFrom, const Vector& vecUp);
  157. Matrix& LookAtFrom(const Vector& vecAt, const Vector& vecFrom, const Vector& vecUp);
  158. Matrix& PreLookAtFrom(const Vector& vecAt, const Vector& vecFrom, const Vector& vecUp);
  159. Matrix& SetScale(float scale);
  160. Matrix& Scale(float scale);
  161. Matrix& PreScale(float scale);
  162. Matrix& SetScale(const Vector& vec);
  163. Matrix& Scale(const Vector& vec);
  164. Matrix& PreScale(const Vector& vec);
  165. Matrix& SetTranslate(const Vector& vec);
  166. Matrix& Translate(const Vector& vec);
  167. Matrix& PreTranslate(const Vector& vec);
  168. Matrix& SetRotateX(float angle);
  169. Matrix& SetRotateY(float angle);
  170. Matrix& SetRotateZ(float angle);
  171. Matrix& SetRotate(const Vector& vec, float angle);
  172. Matrix& Rotate(const Vector& vec, float angle);
  173. Matrix& PreRotate(const Vector& vec, float angle);
  174. Matrix& SetMultiply(const Matrix& m1, const Matrix& m2);
  175. Matrix& Multiply(const Matrix& m2);
  176. Matrix& PreMultiply(const Matrix& m1);
  177. float Determinant() const;
  178. Matrix& SetInverse(const Matrix& m);
  179. Matrix& Inverse();
  180. Matrix& SetTranspose(const Matrix& m);
  181. Matrix& Transpose();
  182. //
  183. // Transforms
  184. //
  185. Point Transform(const Point& point) const;
  186. HVector Transform(const HVector& vec) const;
  187. Vector Transform(const Vector& vec) const;
  188. Vector TransformDirection(const Vector& vec) const;
  189. void Transform(const TVector<Point>&, TVector<Point>&) const;
  190. void Transform(const TVector<HVector>&, TVector<HVector>&) const;
  191. void Transform(const TVector<Vector>&, TVector<Vector>&) const;
  192. };
  193. #endif