Matrix3x3.hpp
1 /*
2 Cafu Engine, http://www.cafu.de/
3 Copyright (c) Carsten Fuchs and other contributors.
5 */
6
7 #ifndef CAFU_MATH_MATRIX_3X3_HPP_INCLUDED
8 #define CAFU_MATH_MATRIX_3X3_HPP_INCLUDED
9
10 #include "Vector3.hpp"
11
12
13 namespace cf
14 {
15  namespace math
16  {
17  template<class T> class AnglesT;
18  template<class T> class QuaternionT;
19
20
21  /// This class represents a generic 3x3 matrix.
22  template<class T>
23  class Matrix3x3T
24  {
25  public:
26
27  /// The default constructor for creating a "1" (identity) matrix.
29  {
30  for (unsigned long i=0; i<3; i++)
31  for (unsigned long j=0; j<3; j++)
32  m[i][j]=(i==j) ? T(1.0) : 0;
33  }
34
35  /// Constructor for creating an arbitrary matrix.
36  Matrix3x3T(const float M[3][3])
37  {
38  for (unsigned long i=0; i<3; i++)
39  for (unsigned long j=0; j<3; j++)
40  m[i][j]=T(M[i][j]);
41  }
42
43  /// Constructor for creating an arbitrary matrix.
44  Matrix3x3T(const double M[3][3])
45  {
46  for (unsigned long i=0; i<3; i++)
47  for (unsigned long j=0; j<3; j++)
48  m[i][j]=T(M[i][j]);
49  }
50
51  /// Constructor for creating an arbitrary matrix.
52  Matrix3x3T(T m00, T m01, T m02,
53  T m10, T m11, T m12,
54  T m20, T m21, T m22)
55  {
56  m[0][0]=m00; m[0][1]=m01; m[0][2]=m02;
57  m[1][0]=m10; m[1][1]=m11; m[1][2]=m12;
58  m[2][0]=m20; m[2][1]=m21; m[2][2]=m22;
59  }
60
61  /// Constructs a rotation matrix from the given angles.
62  /// See the documentation of the AnglesT class for details.
63  Matrix3x3T(const AnglesT<T>& Angles);
64
65  /// Constructs a rotation matrix from the given quaternion.
66  Matrix3x3T(const QuaternionT<T>& Quat);
67
68  /// @name Named constructors.
69  //@{
70  static Matrix3x3T GetScaleMatrix(T sx, T sy, T sz); ///< Returns a scale matrix with scale factors (sx sy sz).
71  static Matrix3x3T GetRotateXMatrix(T Angle); ///< Returns a rotation matrix about Angle degrees around the x-axis.
72  static Matrix3x3T GetRotateYMatrix(T Angle); ///< Returns a rotation matrix about Angle degrees around the y-axis.
73  static Matrix3x3T GetRotateZMatrix(T Angle); ///< Returns a rotation matrix about Angle degrees around the z-axis.
74  static Matrix3x3T GetRotateMatrix(T Angle, const Vector3T<T>& Axis); ///< Returns a rotation matrix about Angle degrees around Axis.
75  static Matrix3x3T GetFromAngles_COMPAT(const AnglesT<T>& Angles); ///< Returns a rotation matrix built from the given angles in a "compatibility-to-old-code" fashion.
76  //@}
77
78
79
80  /// Returns the i-th row of this matrix.
81  T* operator [] (unsigned long i) { assert(i<3); return m[i]; }
82
83  /// Returns the i-th row of this matrix.
84  const T* operator [] (unsigned long i) const { assert(i<3); return m[i]; }
85
86  /// Returns the i-th column of this matrix.
87  /// The i-th column of the matrix corresponds to the i-th axis of the represented coordinate-system.
88  Vector3T<T> GetAxis(unsigned int i) const { return Vector3T<T>(m[0][i], m[1][i], m[2][i]); }
89
90  /// Computes M*Other, that is, the matrix product of this and the Other matrix.
91  /// @param Other The other matrix (right side).
92  /// @return The matrix product of this and the Other matrix.
93  Matrix3x3T operator * (const Matrix3x3T& Other) const;
94
95  /// Computes M*v, where M is this matrix.
96  /// @param v A vector.
97  /// @return M*v.
99  {
100  return Mul(v);
101  }
102
103  /// Determines if this matrix is equal to Other. Note that this is a bit-wise comparison, no epsilon is taken into account.
104  /// @param Other The other matrix (right side).
105  /// @returns whether this matrix and Other are equal.
106  bool operator == (const Matrix3x3T& Other) const
107  {
108  for (unsigned long i=0; i<3; i++)
109  for (unsigned long j=0; j<3; j++)
110  if (m[i][j]!=Other.m[i][j])
111  return false;
112
113  return true;
114  }
115
116  /// Determines if this matrix is not equal to Other. Note that this is a bit-wise comparison, no epsilon is taken into account.
117  /// @param Other The other matrix (right side).
118  /// @returns whether this matrix and Other are not equal.
119  bool operator != (const Matrix3x3T& Other) const
120  {
121  return !(*this==Other);
122  }
123
124
125  void Scale_MS (T sx, T sy, T sz); ///< Computes M=M*S, where S=GetScaleMatrix (sx, sy, sz).
126  void Scale_SM (T sx, T sy, T sz); ///< Computes M=S*M, where S=GetScaleMatrix (sx, sy, sz).
127  void RotateX_MR(T Angle); ///< Computes M=M*R, where R=GetRotateXMatrix (Angle).
128  void RotateX_RM(T Angle); ///< Computes M=R*M, where R=GetRotateXMatrix (Angle).
129  void RotateY_MR(T Angle); ///< Computes M=M*R, where R=GetRotateYMatrix (Angle).
130  void RotateY_RM(T Angle); ///< Computes M=R*M, where R=GetRotateYMatrix (Angle).
131  void RotateZ_MR(T Angle); ///< Computes M=M*R, where R=GetRotateZMatrix (Angle).
132  void RotateZ_RM(T Angle); ///< Computes M=R*M, where R=GetRotateZMatrix (Angle).
133
134  /// Returns whether this matrix is equal to Other.
135  /// The matrices are considered equal if the element-wise comparison yields no difference larger than Epsilon.
136  /// @param Other Matrix to compare to.
137  /// @param Epsilon Tolerance value.
138  /// @see operator ==
139  bool IsEqual(const Matrix3x3T& Other, const T Epsilon=0) const
140  {
141  for (unsigned long i=0; i<3; i++)
142  for (unsigned long j=0; j<3; j++)
143  if (fabs(m[i][j]-Other.m[i][j]) > Epsilon)
144  return false;
145
146  return true;
147  }
148
149  /// Computes M*v, where M is this matrix.
150  /// @param v A vector.
151  /// @return M*v.
152  Vector3T<T> Mul(const Vector3T<T>& v) const
153  {
154  return Vector3T<T>(m[0][0]*v.x + m[0][1]*v.y + m[0][2]*v.z,
155  m[1][0]*v.x + m[1][1]*v.y + m[1][2]*v.z,
156  m[2][0]*v.x + m[2][1]*v.y + m[2][2]*v.z);
157  }
158
159  /// A shortcut for M.GetTranspose()*v, where M is this matrix.
160  /// @param v A vector.
161  /// @return Mt*v, where Mt is the transpose of this matrix.
163  {
164  return Vector3T<T>(m[0][0]*v.x + m[1][0]*v.y + m[2][0]*v.z,
165  m[0][1]*v.x + m[1][1]*v.y + m[2][1]*v.z,
166  m[0][2]*v.x + m[1][2]*v.y + m[2][2]*v.z);
167  }
168
169  /// Computes M*v, where M is this matrix.
170  /// @param v The three-component vector that is multiplied with M.
171  /// @param out The three-component result vector.
172  /// @return The three-component result vector M*v is returned via the out parameter.
173  void Mul(const T v[3], T out[3]) const
174  {
175  // We need the Result variable in case that v==out.
176  T Result[3]={ m[0][0]*v[0] + m[0][1]*v[1] + m[0][2]*v[2],
177  m[1][0]*v[0] + m[1][1]*v[1] + m[1][2]*v[2],
178  m[2][0]*v[0] + m[2][1]*v[1] + m[2][2]*v[2] };
179
180  for (int i=0; i<3; i++) out[i]=Result[i];
181  }
182
183  /// Computes the inverse of this matrix.
184  /// @param Result Whether the operation was successful.
185  /// @return If this matrix is invertible, the inverse is returned by this method, and `*Result`, if not at `NULL`, is set to `true`.
186  /// Otherwise, an undefined matrix is returned, and `*Result`, if not at `NULL`, is set to `false`.
187  Matrix3x3T GetInverse(bool* Result=NULL) const;
188
189  /// Returns the transpose of this matrix.
190  /// @return The transpose of this matrix.
191  Matrix3x3T GetTranspose() const;
192
193  /// Converts the rotation that is described by this matrix to an AnglesT<T> instance that describes the same rotation.
194  /// @returns An AnglesT<T> instance that describes the same rotation as this matrix.
195  AnglesT<T> ToAngles_COMPAT() const;
196
197
198
199  /// The matrix values.
200  T m[3][3];
201
202  static const Matrix3x3T<T> Identity; ///< Identity matrix.
203  };
204
205
206  /// Typedef for a Matrix3x3T of floats.
208
209  /// Typedef for a Matrix3x3T of doubles.
211  }
212 }
213
214 #endif
bool operator!=(const Matrix3x3T &Other) const
Determines if this matrix is not equal to Other.
Definition: Matrix3x3.hpp:119
Matrix3x3T(T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22)
Constructor for creating an arbitrary matrix.
Definition: Matrix3x3.hpp:52
Matrix3x3T GetTranspose() const
Returns the transpose of this matrix.
Definition: Matrix3x3.cpp:409
void Scale_MS(T sx, T sy, T sz)
Computes M=M*S, where S=GetScaleMatrix (sx, sy, sz).
Definition: Matrix3x3.cpp:192
Vector3T< T > MulTranspose(const Vector3T< T > &v) const
A shortcut for M.GetTranspose()*v, where M is this matrix.
Definition: Matrix3x3.hpp:162
Vector3T< T > Mul(const Vector3T< T > &v) const
Computes M*v, where M is this matrix.
Definition: Matrix3x3.hpp:152
This class represents a triple of angles.
Definition: Angles.hpp:71
void RotateX_RM(T Angle)
Computes M=R*M, where R=GetRotateXMatrix (Angle).
Definition: Matrix3x3.cpp:231
Matrix3x3T(const double M[3][3])
Constructor for creating an arbitrary matrix.
Definition: Matrix3x3.hpp:44
void Scale_SM(T sx, T sy, T sz)
Computes M=S*M, where S=GetScaleMatrix (sx, sy, sz).
Definition: Matrix3x3.cpp:203
static Matrix3x3T GetFromAngles_COMPAT(const AnglesT< T > &Angles)
Returns a rotation matrix built from the given angles in a "compatibility-to-old-code" fashion...
Definition: Matrix3x3.cpp:143
Matrix3x3T(const float M[3][3])
Constructor for creating an arbitrary matrix.
Definition: Matrix3x3.hpp:36
Vector3T< T > GetAxis(unsigned int i) const
Returns the i-th column of this matrix.
Definition: Matrix3x3.hpp:88
T m[3][3]
The matrix values.
Definition: Matrix3x3.hpp:200
static const Matrix3x3T< T > Identity
Identity matrix.
Definition: Matrix3x3.hpp:202
bool IsEqual(const Matrix3x3T &Other, const T Epsilon=0) const
Returns whether this matrix is equal to Other.
Definition: Matrix3x3.hpp:139
T y
The y-component of this vector.
Definition: Vector3.hpp:41
Matrix3x3T operator*(const Matrix3x3T &Other) const
Computes M*Other, that is, the matrix product of this and the Other matrix.
Definition: Matrix3x3.cpp:175
T * operator[](unsigned long i)
Returns the i-th row of this matrix.
Definition: Matrix3x3.hpp:81
void RotateZ_RM(T Angle)
Computes M=R*M, where R=GetRotateZMatrix (Angle).
Definition: Matrix3x3.cpp:299
This class represents a polymorphic 3-dimensional vector.
Definition: Misc.hpp:11
static Matrix3x3T GetRotateZMatrix(T Angle)
Returns a rotation matrix about Angle degrees around the z-axis.
Definition: Matrix3x3.cpp:105
static Matrix3x3T GetScaleMatrix(T sx, T sy, T sz)
Returns a scale matrix with scale factors (sx sy sz).
Definition: Matrix3x3.cpp:61
void Mul(const T v[3], T out[3]) const
Computes M*v, where M is this matrix.
Definition: Matrix3x3.hpp:173
void RotateX_MR(T Angle)
Computes M=M*R, where R=GetRotateXMatrix (Angle).
Definition: Matrix3x3.cpp:214
static Matrix3x3T GetRotateMatrix(T Angle, const Vector3T< T > &Axis)
Returns a rotation matrix about Angle degrees around Axis.
Definition: Matrix3x3.cpp:121
void RotateY_RM(T Angle)
Computes M=R*M, where R=GetRotateYMatrix (Angle).
Definition: Matrix3x3.cpp:265
Matrix3x3T()
The default constructor for creating a "1" (identity) matrix.
Definition: Matrix3x3.hpp:28
AnglesT< T > ToAngles_COMPAT() const
Converts the rotation that is described by this matrix to an AnglesT<T> instance that describes the s...
Definition: Matrix3x3.cpp:421
void RotateY_MR(T Angle)
Computes M=M*R, where R=GetRotateYMatrix (Angle).
Definition: Matrix3x3.cpp:248
T z
The z-component of this vector.
Definition: Vector3.hpp:42
void RotateZ_MR(T Angle)
Computes M=M*R, where R=GetRotateZMatrix (Angle).
Definition: Matrix3x3.cpp:282
Matrix3x3T GetInverse(bool *Result=NULL) const
Computes the inverse of this matrix.
Definition: Matrix3x3.cpp:316
This class represents a quaternion.
Definition: Angles.hpp:18
T x
The x-component of this vector.
Definition: Vector3.hpp:40
static Matrix3x3T GetRotateXMatrix(T Angle)
Returns a rotation matrix about Angle degrees around the x-axis.
Definition: Matrix3x3.cpp:73
static Matrix3x3T GetRotateYMatrix(T Angle)
Returns a rotation matrix about Angle degrees around the y-axis.
Definition: Matrix3x3.cpp:89
This class represents a generic 3x3 matrix.
Definition: Angles.hpp:17
bool operator==(const Matrix3x3T &Other) const
Determines if this matrix is equal to Other.
Definition: Matrix3x3.hpp:106