3es  0.7
3esmatrix3.h
1 //
2 // author: Kazys Stepanas
3 //
4 #ifndef _3ESMATRIX3_H
5 #define _3ESMATRIX3_H
6 
7 #include "3es-core.h"
8 
9 #include "3esvector3.h"
10 
11 namespace tes
12 {
23  template <typename T>
24  class Matrix3
25  {
26  public:
27  union
28  {
29  T rc[3][3];
30  T m[9];
31  };
32  static const Matrix3<T> zero;
33  static const Matrix3<T> identity;
34 
36  inline Matrix3() {}
39  Matrix3(const T *array9);
42  Matrix3(const Matrix3<T> &other);
45  template <typename Q>
46  Matrix3(const Matrix3<Q> &other);
47 
58  Matrix3(const T &rc00, const T &rc01, const T &rc02,
59  const T &rc10, const T &rc11, const T &rc12,
60  const T &rc20, const T &rc21, const T &rc22);
61 
65  inline T &operator()(int r, int c) { return rc[r][c]; }
69  inline const T &operator()(int r, int c) const { return rc[r][c]; }
70 
74  inline T &operator[](int index) { return m[index]; }
75 
79  inline const T &operator[](int index) const { return m[index]; }
80 
84  static Matrix3<T> rotationX(const T &angle);
85  inline Matrix3<T> &initRotationX(const T &angle) { *this = rotationX(angle); return *this; }
86 
90  static Matrix3<T> rotationY(const T &angle);
91  inline Matrix3<T> &initRotationY(const T &angle) { *this = rotationY(angle); return *this; }
92 
96  static Matrix3<T> rotationZ(const T &angle);
97  inline Matrix3<T> &initRotationZ(const T &angle) { *this = rotationZ(angle); return *this; }
98 
105  static Matrix3<T> rotation(const T &x, const T &y, const T &z);
106  inline Matrix3<T> &initRotation(const T &x, const T &y, const T &z) { *this = rotation(x, y, z); return *this; }
107 
111  static Matrix3<T> scaling(const Vector3<T> &scale);
112  inline Matrix3<T> &initScaling(const Vector3<T> &scale) { *this = scaling(scale); return *this; }
113 
132  static Matrix3<T> lookAt(const Vector3<T> &eye, const Vector3<T> &target, const Vector3<T> &axisUp, int forwardAxisIndex = 1, int upAxisIndex = 2);
133 
142  inline Matrix3<T> &initLookAt(const Vector3<T> &eye, const Vector3<T> &target, const Vector3<T> &axisUp, int forwardAxisIndex = 1, int upAxisIndex = 2)
143  {
144  *this = lookAt(eye, target, axisUp, forwardAxisIndex, upAxisIndex);
145  return *this;
146  }
147 
151 
154  Matrix3<T> transposed() const;
155 
158  Matrix3<T> &invert();
159 
162  Matrix3<T> inverse() const;
163 
167  T getAdjoint(Matrix3<T> &adj) const;
168 
178 
183 
186  T determinant() const;
187 
190  Vector3<T> axisX() const;
193  Vector3<T> axisY() const;
196  Vector3<T> axisZ() const;
197 
202  Vector3<T> axis(int index) const;
203 
207  Matrix3<T> &setAxisX(const Vector3<T> &axis);
208 
212  Matrix3<T> &setAxisY(const Vector3<T> &axis);
213 
217  Matrix3<T> &setAxisZ(const Vector3<T> &axis);
218 
224  Matrix3<T> &setAxis(int index, const Vector3<T> &axis);
225 
228  Vector3<T> scale() const;
229 
233  Matrix3<T> &scale(const Vector3<T> &scaling);
234 
237  Vector3<T> transform(const Vector3<T> &v) const;
238 
241  Vector3<T> rotate(const Vector3<T> &v) const;
242  };
243 
248 
249  template class _3es_coreAPI Matrix3<float>;
250  template class _3es_coreAPI Matrix3<double>;
251 
254  template <typename T>
255  Matrix3<T> operator * (const Matrix3<T> &a, const Matrix3<T> &b);
256 
259  template <typename T>
260  Vector3<T> operator * (const Matrix3<T> &a, const Vector3<T> &v);
261 }
262 
263 #include "3esmatrix3.inl"
264 
265 #endif // _3ESMATRIX3_H
static Matrix3< T > lookAt(const Vector3< T > &eye, const Vector3< T > &target, const Vector3< T > &axisUp, int forwardAxisIndex=1, int upAxisIndex=2)
Create a model or camera matrix at eye looking at target.
static Matrix3< T > rotationX(const T &angle)
Create a matrix which represents a rotation around the X axis.
T & operator()(int r, int c)
Row/column access.
Definition: 3esmatrix3.h:65
Vector3< T > scale() const
Returns the scale contained in this matrix.
Matrix3< T > & transpose()
Transposes this matrix.
static Matrix3< T > scaling(const Vector3< T > &scale)
Create a scaling matrix.
static Matrix3< T > rotationY(const T &angle)
Create a matrix which represents a rotation around the Y axis.
static const Matrix3< T > zero
A matrix with all zero elements.
Definition: 3esmatrix3.h:32
Definition: 3esbounds.h:13
Vector3< T > axis(int index) const
Returns one of the axes of this matrix.
Matrix3< T > & setAxisX(const Vector3< T > &axis)
Sets the X axis of this matrix.
static const Matrix3< T > identity
The identity matrix.
Definition: 3esmatrix3.h:33
Matrix3< T > operator*(const Matrix3< T > &a, const Matrix3< T > &b)
Performs the matrix multiplication AB.
T determinant() const
Calculates the determinant of this matrix.
Vector3< T > axisZ() const
Returns the Z axis of this matrix (elements (0, 2), (1, 2), (2, 2)).
Matrix3()
Empty constructor; contents are undefined.
Definition: 3esmatrix3.h:36
T m[9]
Array representation.
Definition: 3esmatrix3.h:30
Matrix3< T > transposed() const
Returns the transpose of this matrix, leaving this matrix unchanged.
Matrix3< float > Matrix3f
Defines a single precision 4x4 matrix.
Definition: 3esmatrix3.h:245
Represents a vector in R3.
Definition: 3esvector3.h:14
Matrix3< T > & setAxisZ(const Vector3< T > &axis)
Sets the Z axis of this matrix.
Matrix3< T > & setAxis(int index, const Vector3< T > &axis)
Sets the indexed axis of this matrix.
const T & operator[](int index) const
Indexing operator (no bounds checking).
Definition: 3esmatrix3.h:79
Matrix3< T > & invert()
Inverts this matrix.
T rc[3][3]
Row/column indexing representation.
Definition: 3esmatrix3.h:29
Matrix3< T > & setAxisY(const Vector3< T > &axis)
Sets the Y axis of this matrix.
Vector3< T > axisY() const
Returns the Y axis of this matrix (elements (0, 1), (1, 1), (2, 1)).
Vector3< T > axisX() const
Returns the X axis of this matrix (elements (0, 0), (1, 0), (2, 0)).
Matrix3< T > & initLookAt(const Vector3< T > &eye, const Vector3< T > &target, const Vector3< T > &axisUp, int forwardAxisIndex=1, int upAxisIndex=2)
Initialise this matrix as a model or camera matrix.
Definition: 3esmatrix3.h:142
const T & operator()(int r, int c) const
Row/column immutable access.
Definition: 3esmatrix3.h:69
static Matrix3< T > rotationZ(const T &angle)
Create a matrix which represents a rotation around the Z axis.
Matrix3< double > Matrix3d
Defines a double precision 4x4 matrix.
Definition: 3esmatrix3.h:247
T getAdjoint(Matrix3< T > &adj) const
Gets the adjoint of this matrix.
static Matrix3< T > rotation(const T &x, const T &y, const T &z)
Create a matrix which represents a rotation around each axis (Euler angles).
Vector3< T > transform(const Vector3< T > &v) const
Transforms the vector v by this matrix.
Vector3< T > rotate(const Vector3< T > &v) const
An alias for transform().
Matrix3< T > inverse() const
Returns the inverse of this matrix, leaving this matrix unchanged.
A row major 3x3 rotation matrix.
Definition: 3esmatrix3.h:24
Matrix3< T > rigidBodyInverse() const
Returns the inverse of this matrix assuming this is a rigid body transformation.
T & operator[](int index)
Indexing operator (no bounds checking).
Definition: 3esmatrix3.h:74
Matrix3< T > & rigidBodyInvert()
Inverts this matrix assuming this matrix represents a rigid body transformation.