3es  0.7
3esmatrix4.h
1 //
2 // author: Kazys Stepanas
3 //
4 #ifndef _3ESMATRIX4_H_
5 #define _3ESMATRIX4_H_
6 
7 #include "3es-core.h"
8 
9 #include "3esvector3.h"
10 #include "3esvector4.h"
11 
12 namespace tes
13 {
26  template <typename T>
27  class Matrix4
28  {
29  public:
30  union
31  {
32  T rc[4][4];
33  T m[16];
34  };
35 
36  static const Matrix4<T> zero;
37  static const Matrix4<T> identity;
38 
40  inline Matrix4() {}
43  Matrix4(const T *array16);
46  Matrix4(const Matrix4<T> &other);
49  template <typename Q>
50  Matrix4(const Matrix4<Q> &other);
51 
69  Matrix4(const T &rc00, const T &rc01, const T &rc02, const T &rc03,
70  const T &rc10, const T &rc11, const T &rc12, const T &rc13,
71  const T &rc20, const T &rc21, const T &rc22, const T &rc23,
72  const T &rc30, const T &rc31, const T &rc32, const T &rc33);
73 
77  inline T &operator()(int r, int c) { return rc[r][c]; }
81  inline const T &operator()(int r, int c) const { return rc[r][c]; }
82 
86  inline T &operator[](int index) { return m[index]; }
87 
91  inline const T &operator[](int index) const { return m[index]; }
92 
96  static Matrix4<T> rotationX(const T &angle);
97 
101  inline Matrix4<T> &initRotationX(const T &angle) { *this = rotationX(angle); return *this; }
102 
106  static Matrix4<T> rotationY(const T &angle);
107 
111  inline Matrix4<T> &initRotationY(const T &angle) { *this = rotationY(angle); return *this; }
112 
116  static Matrix4<T> rotationZ(const T &angle);
117 
121  inline Matrix4<T> &initRotationZ(const T &angle) { *this = rotationZ(angle); return *this; }
122 
129  static Matrix4<T> rotation(const T &x, const T &y, const T &z);
130 
136  inline Matrix4<T> &initRotation(const T &x, const T &y, const T &z) { *this = rotation(x, y, z); return *this; }
137 
141  static Matrix4<T> translation(const Vector3<T> &trans);
142 
146  inline Matrix4<T> initTranslation(const Vector3<T> &trans) { *this = translation(trans); return *this; }
147 
154  static Matrix4<T> rotationTranslation(const T &x, const T &y, const T &z, const Vector3<T> &trans);
156  inline Matrix4<T> &initRotationTranslation(const T &x, const T &y, const T &z, const Vector3<T> &trans) { *this = rotationTranslation(x, y, z, trans); return *this; }
157 
161  static Matrix4<T> scaling(const Vector3<T> &scale);
162 
166  inline Matrix4<T> &initScaling(const Vector3<T> &scale) { *this = scaling(scale); return *this; }
167 
182  static Matrix4<T> lookAt(const Vector3<T> &eye, const Vector3<T> &target, const Vector3<T> &axisUp, int forwardAxisIndex = 1, int upAxisIndex = 2);
183 
192  inline Matrix4<T> &initLookAt(const Vector3<T> &eye, const Vector3<T> &target, const Vector3<T> &axisUp, int forwardAxisIndex = 1, int upAxisIndex = 2)
193  {
194  *this = lookAt(eye, target, axisUp, forwardAxisIndex, upAxisIndex);
195  return *this;
196  }
197 
201 
204  Matrix4<T> transposed() const;
205 
208  Matrix4<T> &invert();
209 
212  Matrix4<T> inverse() const;
213 
223 
228 
231  T determinant() const;
232 
235  Vector3<T> axisX() const;
238  Vector3<T> axisY() const;
241  Vector3<T> axisZ() const;
242 
245  Vector3<T> axisT() const;
246 
249  Vector3<T> translation() const;
250 
255  Vector3<T> axis(int index) const;
256 
260  Matrix4<T> &setAxisX(const Vector3<T> &axis);
261 
265  Matrix4<T> &setAxisY(const Vector3<T> &axis);
266 
270  Matrix4<T> &setAxisZ(const Vector3<T> &axis);
271 
275  Matrix4<T> &setAxisT(const Vector3<T> &axis);
276 
280  Matrix4<T> &setTranslation(const Vector3<T> &axis);
281 
287  Matrix4<T> &setAxis(int index, const Vector3<T> &axis);
288 
291  Vector3<T> scale() const;
292 
296 
300  Matrix4<T> &scale(const Vector3<T> &scaling);
301 
304  Vector3<T> transform(const Vector3<T> &v) const;
305 
308  Vector4<T> transform(const Vector4<T> &v) const;
309 
312  Vector3<T> rotate(const Vector3<T> &v) const;
313 
316  Vector4<T> rotate(const Vector4<T> &v) const;
317  };
318 
323 
324  template class _3es_coreAPI Matrix4<float>;
325  template class _3es_coreAPI Matrix4<double>;
326 
329  template <typename T>
330  Matrix4<T> operator * (const Matrix4<T> &a, const Matrix4<T> &b);
331 
334  template <typename T>
335  Vector3<T> operator * (const Matrix4<T> &a, const Vector3<T> &v);
336 
339  template <typename T>
340  Vector4<T> operator * (const Matrix4<T> &a, const Vector4<T> &v);
341 }
342 
343 #include "3esmatrix4.inl"
344 
345 #endif // _3ESMATRIX4_H_
Matrix4< T > & initRotationX(const T &angle)
Initialise this matrix as a rotation around the X axis.
Definition: 3esmatrix4.h:101
Matrix4< T > & transpose()
Transposes this matrix.
Matrix4< T > & initScaling(const Vector3< T > &scale)
Initialise this matrix as scaling matrix with no rotation or translation.
Definition: 3esmatrix4.h:166
Vector3< T > axisY() const
Returns the Y axis of this matrix (elements (0, 1), (1, 1), (2, 1)).
Vector3< T > axisZ() const
Returns the Z axis of this matrix (elements (0, 2), (1, 2), (2, 2)).
T & operator()(int r, int c)
Row/column access.
Definition: 3esmatrix4.h:77
Matrix4< T > & setAxisY(const Vector3< T > &axis)
Sets the Y axis of this matrix.
static Matrix4< T > rotationY(const T &angle)
Create a matrix which represents a rotation around the Y axis.
A row major 4x4 transformation matrix.
Definition: 3esmatrix4.h:27
Vector3< T > axis(int index) const
Returns one of the axes of this matrix.
static Matrix4< T > rotationZ(const T &angle)
Create a matrix which represents a rotation around the Z axis.
static Matrix4< T > rotationX(const T &angle)
Create a matrix which represents a rotation around the X axis.
Matrix4< T > initTranslation(const Vector3< T > &trans)
Initialise this matrix as a translation only matrix.
Definition: 3esmatrix4.h:146
Matrix4< T > & rigidBodyInvert()
Inverts this matrix assuming this matrix represents a rigid body transformation.
Definition: 3esbounds.h:13
static const Matrix4< T > identity
The identity matrix.
Definition: 3esmatrix4.h:37
const T & operator()(int r, int c) const
Row/column immutable access.
Definition: 3esmatrix4.h:81
Represents a vector in R4.
Definition: 3esvector4.h:13
T rc[4][4]
Row/column indexing representation.
Definition: 3esmatrix4.h:32
Matrix4< T > & initRotation(const T &x, const T &y, const T &z)
Initialise this matrix as a rotation using the given Euler angles.
Definition: 3esmatrix4.h:136
Matrix4< T > & setAxisX(const Vector3< T > &axis)
Sets the X axis of this matrix.
static Matrix4< T > rotation(const T &x, const T &y, const T &z)
Create a matrix which represents a rotation around each axis (Euler angles).
Matrix4< T > & setAxisT(const Vector3< T > &axis)
Sets the translation of this matrix.
Matrix3< T > operator*(const Matrix3< T > &a, const Matrix3< T > &b)
Performs the matrix multiplication AB.
Matrix4< T > & initRotationTranslation(const T &x, const T &y, const T &z, const Vector3< T > &trans)
Definition: 3esmatrix4.h:156
Vector3< T > axisX() const
Returns the X axis of this matrix (elements (0, 0), (1, 0), (2, 0)).
Vector3< T > translation() const
Returns the T axis or translation component of this matrix (elements (0, 3), (1, 3), (2, 3)).
static Matrix4< T > scaling(const Vector3< T > &scale)
Create a scaling matrix.
Matrix4< T > transposed() const
Returns the transpose of this matrix, leaving this matrix unchanged.
static const Matrix4< T > zero
A matrix with all zero elements.
Definition: 3esmatrix4.h:36
Vector3< T > scale() const
Returns the scale contained in this matrix.
Matrix4< T > & invert()
Inverts this matrix.
Represents a vector in R3.
Definition: 3esvector3.h:14
Vector3< T > axisT() const
Returns the T axis or translation component of this matrix (elements (0, 3), (1, 3), (2, 3)).
Matrix4< T > & setTranslation(const Vector3< T > &axis)
Sets the translation of this matrix.
Matrix4< T > & initRotationY(const T &angle)
Initialise this matrix as a rotation around the Y axis.
Definition: 3esmatrix4.h:111
Matrix4< T > rigidBodyInverse() const
Returns the inverse of this matrix assuming this is a rigid body transformation.
static Matrix4< 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.
Matrix4< float > Matrix4f
Defines a single precision 4x4 matrix.
Definition: 3esmatrix4.h:320
T m[16]
Array representation.
Definition: 3esmatrix4.h:33
static Matrix4< T > rotationTranslation(const T &x, const T &y, const T &z, const Vector3< T > &trans)
Create a transformation matrix with rotation (Euler angles) and translation.
Matrix4< 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: 3esmatrix4.h:192
Matrix4< T > & initRotationZ(const T &angle)
Initialise this matrix as a rotation around the Z axis.
Definition: 3esmatrix4.h:121
Matrix4< T > & setAxisZ(const Vector3< T > &axis)
Sets the Z axis of this matrix.
Vector3< T > removeScale()
Removes scale from the matrix, leaving a rotation/translation matrix.
Matrix4()
Empty constructor; contents are undefined.
Definition: 3esmatrix4.h:40
Matrix4< T > inverse() const
Returns the inverse of this matrix, leaving this matrix unchanged.
const T & operator[](int index) const
Indexing operator (no bounds checking).
Definition: 3esmatrix4.h:91
Matrix4< double > Matrix4d
Defines a double precision 4x4 matrix.
Definition: 3esmatrix4.h:322
T determinant() const
Calculates the determinant of this matrix.
Vector3< T > transform(const Vector3< T > &v) const
Transforms the vector v by this matrix.
T & operator[](int index)
Indexing operator (no bounds checking).
Definition: 3esmatrix4.h:86
Vector3< T > rotate(const Vector3< T > &v) const
Transforms the vector v by the rotation component of this matrix.
Matrix4< T > & setAxis(int index, const Vector3< T > &axis)
Sets the indexed axis of this matrix.