Jet  v1.3.3
matrix4x4.h
Go to the documentation of this file.
1 // Copyright (c) 2018 Doyub Kim
2 //
3 // I am making my contributions/submissions to this project solely in my
4 // personal capacity and am not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef INCLUDE_JET_MATRIX4X4_H_
8 #define INCLUDE_JET_MATRIX4X4_H_
9 
10 #include <jet/matrix3x3.h>
11 #include <jet/vector4.h>
12 
13 #include <array>
14 #include <limits>
15 
16 namespace jet {
17 
26 template <typename T>
27 class Matrix<T, 4, 4> {
28  public:
29  static_assert(std::is_floating_point<T>::value,
30  "Matrix only can be instantiated with floating point types");
31 
32  // MARK: Constructors
33 
35  Matrix();
36 
38  explicit Matrix(T s);
39 
44  Matrix(T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22);
45 
48  Matrix(T m00, T m01, T m02, T m03, T m10, T m11, T m12, T m13, T m20, T m21,
49  T m22, T m23, T m30, T m31, T m32, T m33);
50 
70  template <typename U>
71  Matrix(const std::initializer_list<std::initializer_list<U>>& lst);
72 
76  explicit Matrix(const Matrix3x3<T>& m33);
77 
79  Matrix(const Matrix& m);
80 
83  explicit Matrix(const T* arr);
84 
85  // MARK: Basic setters
86 
88  void set(T s);
89 
94  void set(T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22);
95 
98  void set(T m00, T m01, T m02, T m03, T m10, T m11, T m12, T m13, T m20,
99  T m21, T m22, T m23, T m30, T m31, T m32, T m33);
100 
121  template <typename U>
122  void set(const std::initializer_list<std::initializer_list<U>>& lst);
123 
127  void set(const Matrix3x3<T>& m33);
128 
130  void set(const Matrix& m);
131 
134  void set(const T* arr);
135 
137  void setDiagonal(T s);
138 
140  void setOffDiagonal(T s);
141 
143  void setRow(size_t i, const Vector4<T>& row);
144 
146  void setColumn(size_t i, const Vector4<T>& col);
147 
148  // MARK: Basic getters
149 
152  bool isSimilar(const Matrix& m,
153  double tol = std::numeric_limits<double>::epsilon()) const;
154 
156  bool isSquare() const;
157 
159  size_t rows() const;
160 
162  size_t cols() const;
163 
165  T* data();
166 
168  const T* data() const;
169 
172 
173  // MARK: Binary operator methods - new instance = this instance (+) input
175  Matrix add(T s) const;
176 
178  Matrix add(const Matrix& m) const;
179 
181  Matrix sub(T s) const;
182 
184  Matrix sub(const Matrix& m) const;
185 
187  Matrix mul(T s) const;
188 
190  Vector4<T> mul(const Vector4<T>& v) const;
191 
193  Matrix mul(const Matrix& m) const;
194 
196  Matrix div(T s) const;
197 
198  // MARK: Binary operator methods - new instance = input (+) this instance
200  Matrix radd(T s) const;
201 
203  Matrix radd(const Matrix& m) const;
204 
206  Matrix rsub(T s) const;
207 
209  Matrix rsub(const Matrix& m) const;
210 
212  Matrix rmul(T s) const;
213 
215  Matrix rmul(const Matrix& m) const;
216 
218  Matrix rdiv(T s) const;
219 
220  // MARK: Augmented operator methods - this instance (+)= input
222  void iadd(T s);
223 
225  void iadd(const Matrix& m);
226 
228  void isub(T s);
229 
231  void isub(const Matrix& m);
232 
234  void imul(T s);
235 
239  void imul(const Matrix3x3<T>& m33);
240 
242  void imul(const Matrix& m);
243 
245  void idiv(T s);
246 
247  // MARK: Modifiers
249  void transpose();
250 
252  void invert();
253 
254  // MARK: Complex getters
256  T sum() const;
257 
259  T avg() const;
260 
262  T min() const;
263 
265  T max() const;
266 
268  T absmin() const;
269 
271  T absmax() const;
272 
274  T trace() const;
275 
277  T determinant() const;
278 
280  Matrix diagonal() const;
281 
284 
287 
290 
292  Matrix lowerTri() const;
293 
295  Matrix upperTri() const;
296 
299 
301  Matrix inverse() const;
302 
303  template <typename U>
305 
306  // MARK: Setter operators
308  Matrix& operator=(const Matrix& m);
309 
312 
315 
318 
321 
324 
327 
332 
335 
336  // MARK: Getter operators
338  T& operator[](size_t i);
339 
341  const T& operator[](size_t i) const;
342 
344  T& operator()(size_t i, size_t j);
345 
347  const T& operator()(size_t i, size_t j) const;
348 
350  bool operator==(const Matrix& m) const;
351 
353  bool operator!=(const Matrix& m) const;
354 
355  // MARK: Helpers
357  static Matrix makeZero();
358 
361 
363  static Matrix makeScaleMatrix(T sx, T sy, T sz);
364 
367 
370  static Matrix makeRotationMatrix(const Vector3<T>& axis, T rad);
371 
374 
375  private:
376  std::array<T, 16> _elements;
377 };
378 
380 template <typename T>
382 
383 // Operator overloadings
385 template <typename T>
387 
389 template <typename T>
391 
393 template <typename T>
395 
397 template <typename T>
399 
401 template <typename T>
403 
405 template <typename T>
407 
409 template <typename T>
411 
413 template <typename T>
415 
417 template <typename T>
419 
421 template <typename T>
423 
425 template <typename T>
427 
429 template <typename T>
431 
433 template <typename T>
435 
437 template <typename T>
439 
441 template <typename T>
443 
445 template <typename T>
446 Matrix4x4<T> operator/(const T& a, const Matrix4x4<T>& b);
447 
450 
453 
454 } // namespace jet
455 
456 #include "detail/matrix4x4-inl.h"
457 
458 #endif // INCLUDE_JET_MATRIX4X4_H_
jet::Matrix< T, 4, 4 >::trace
T trace() const
Returns sum of all diagonal elements.
jet::Matrix< T, 4, 4 >::mul
Matrix mul(T s) const
Returns this matrix * input scalar.
matrix3x3.h
jet::Matrix< T, 4, 4 >::mul
Matrix mul(const Matrix &m) const
Returns this matrix * input matrix.
jet::Matrix< T, 4, 4 >::rows
size_t rows() const
Returns number of rows of this matrix.
jet::Matrix< T, 4, 4 >::data
T * data()
Returns data pointer of this matrix.
jet::Matrix< T, 4, 4 >::operator*=
Matrix & operator*=(const Matrix &m)
Multiplication assignment with input matrix.
jet::Matrix< T, 4, 4 >::Matrix
Matrix(const T *arr)
jet::Matrix< T, 4, 4 >::strictUpperTri
Matrix strictUpperTri() const
Returns strictly upper triangle part of this matrix.
jet::Matrix< T, 4, 4 >::rdiv
Matrix rdiv(T s) const
Returns input matrix / this scalar.
jet::Matrix< T, 4, 4 >::min
T min() const
Returns minimum among all elements.
jet::Matrix< T, 4, 4 >::lowerTri
Matrix lowerTri() const
Returns lower triangle part of this matrix (including the diagonal).
jet::Matrix< T, 4, 4 >::makeTranslationMatrix
static Matrix makeTranslationMatrix(const Vector3< T > &t)
Makes translation matrix.
jet::Matrix< T, 4, 4 >::castTo
Matrix< U, 4, 4 > castTo() const
jet::Matrix< T, 4, 4 >::operator+=
Matrix & operator+=(const Matrix &m)
Addition assignment with input matrix (element-wise).
jet::Matrix< T, 4, 4 >::imul
void imul(const Matrix3x3< T > &m33)
jet::Matrix< T, 4, 4 >::transpose
void transpose()
Transposes this matrix.
jet::Matrix< T, 4, 4 >::operator==
bool operator==(const Matrix &m) const
Returns true if is equal to m.
jet::Matrix
Static-sized M x N matrix class.
Definition: matrix.h:29
jet::Matrix< T, 4, 4 >::absmax
T absmax() const
Returns absolute maximum among all elements.
jet::Matrix< T, 4, 4 >::isSimilar
bool isSimilar(const Matrix &m, double tol=std::numeric_limits< double >::epsilon()) const
jet::Matrix< T, 4, 4 >::idiv
void idiv(T s)
Divides this matrix with input scalar.
jet::Matrix4x4F
Matrix4x4< float > Matrix4x4F
Float-type 4x4 matrix.
Definition: matrix4x4.h:449
jet::Matrix< T, 4, 4 >::Matrix
Matrix()
Constructs identity matrix.
jet::Matrix< T, 4, 4 >::imul
void imul(T s)
Multiplies input scalar to this matrix.
jet::Matrix< T, 4, 4 >::div
Matrix div(T s) const
Returns this matrix / input scalar.
jet::Matrix< T, 4, 4 >::operator*=
Matrix & operator*=(T s)
Multiplication assignment with input scalar.
jet::Matrix< T, 4, 4 >::operator-=
Matrix & operator-=(const Matrix &m)
Subtraction assignment with input matrix (element-wise).
jet::Matrix< T, 4, 4 >::max
T max() const
Returns maximum among all elements.
jet::Matrix< T, 4, 4 >::data
const T * data() const
Returns constant pointer of this matrix.
jet::Matrix< T, 4, 4 >::Matrix
Matrix(T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22)
jet::Matrix< T, 4, 4 >::makeIdentity
static Matrix makeIdentity()
Makes all diagonal elements to 1, and other elements to 0.
jet::Matrix< T, 4, 4 >::operator[]
const T & operator[](size_t i) const
Returns constant reference of i-th element.
jet::Matrix< T, 4, 4 >::operator()
const T & operator()(size_t i, size_t j) const
Returns constant reference of (i,j) element.
jet::Matrix< T, 3, 3 >
3-D matrix class.
Definition: matrix3x3.h:28
jet::Matrix< T, 4, 4 >::Matrix
Matrix(T m00, T m01, T m02, T m03, T m10, T m11, T m12, T m13, T m20, T m21, T m22, T m23, T m30, T m31, T m32, T m33)
jet::Matrix< T, 4, 4 >::setColumn
void setColumn(size_t i, const Vector4< T > &col)
Sets i-th column with input vector.
jet::Matrix< T, 4, 4 >::set
void set(T s)
Sets whole matrix with input scalar.
jet::Matrix< T, 4, 4 >::setRow
void setRow(size_t i, const Vector4< T > &row)
Sets i-th row with input vector.
jet::Vector< T, 4 >
4-D vector class.
Definition: vector4.h:25
jet::Matrix< T, 4, 4 >::operator-=
Matrix & operator-=(T s)
Subtraction assignment with input scalar.
jet
Definition: advection_solver2.h:18
jet::Matrix< T, 4, 4 >::set
void set(const T *arr)
jet::Matrix< T, 4, 4 >::radd
Matrix radd(const Matrix &m) const
Returns input matrix + this matrix (element-wise).
jet::Matrix< T, 4, 4 >::set
void set(T m00, T m01, T m02, T m03, T m10, T m11, T m12, T m13, T m20, T m21, T m22, T m23, T m30, T m31, T m32, T m33)
jet::Matrix< T, 4, 4 >::Matrix
Matrix(const std::initializer_list< std::initializer_list< U >> &lst)
Constructs a matrix with given initializer list lst.
jet::Matrix< T, 4, 4 >::set
void set(const Matrix3x3< T > &m33)
jet::Matrix< T, 4, 4 >::iadd
void iadd(const Matrix &m)
Adds input matrix to this matrix (element-wise).
jet::Matrix< T, 4, 4 >::sub
Matrix sub(const Matrix &m) const
Returns this matrix - input matrix (element-wise).
jet::Matrix< T, 4, 4 >::Matrix
Matrix(const Matrix3x3< T > &m33)
jet::Matrix< T, 4, 4 >::isSquare
bool isSquare() const
Returns true if this matrix is a square matrix.
jet::Matrix< T, 4, 4 >::isub
void isub(const Matrix &m)
Subtracts input matrix from this matrix (element-wise).
jet::Matrix< T, 4, 4 >::set
void set(T m00, T m01, T m02, T m10, T m11, T m12, T m20, T m21, T m22)
jet::Matrix< T, 4, 4 >::set
void set(const std::initializer_list< std::initializer_list< U >> &lst)
Sets a matrix with given initializer list lst.
jet::Matrix< T, 4, 4 >::offDiagonal
Matrix offDiagonal() const
Returns off-diagonal part of this matrix.
jet::Matrix< T, 4, 4 >::makeScaleMatrix
static Matrix makeScaleMatrix(const Vector3< T > &s)
Makes scale matrix.
jet::Matrix< T, 4, 4 >::operator/=
Matrix & operator/=(T s)
Division assignment with input scalar.
jet::operator+
Matrix2x2< T > operator+(const Matrix2x2< T > &a, const Matrix2x2< T > &b)
Returns a + b (element-size).
jet::Matrix< T, 4, 4 >::invert
void invert()
Inverts this matrix.
jet::Matrix< T, 4, 4 >::radd
Matrix radd(T s) const
Returns input scalar + this matrix.
vector4.h
jet::Matrix< T, 4, 4 >
4-D matrix class.
Definition: matrix4x4.h:27
jet::Matrix< T, 4, 4 >::sub
Matrix sub(T s) const
Returns this matrix - input scalar.
jet::Matrix< T, 4, 4 >::upperTri
Matrix upperTri() const
Returns upper triangle part of this matrix (including the diagonal).
jet::Matrix< T, 4, 4 >::rmul
Matrix rmul(T s) const
Returns input scalar * this matrix.
jet::Matrix< T, 4, 4 >::transposed
Matrix transposed() const
Returns transposed matrix.
jet::Matrix< T, 4, 4 >::makeZero
static Matrix makeZero()
Sets all matrix entries to zero.
jet::Matrix4x4D
Matrix4x4< double > Matrix4x4D
Double-type 4x4 matrix.
Definition: matrix4x4.h:452
jet::Matrix< T, 4, 4 >::Matrix
Matrix(const Matrix &m)
Constructs a matrix with input matrix.
jet::Matrix< T, 4, 4 >::strictLowerTri
Matrix strictLowerTri() const
Returns strictly lower triangle part of this matrix.
jet::Matrix< T, 4, 4 >::operator*=
Matrix & operator*=(const Matrix3x3< T > &m)
jet::Matrix< T, 4, 4 >::makeScaleMatrix
static Matrix makeScaleMatrix(T sx, T sy, T sz)
Makes scale matrix.
jet::Matrix< T, 4, 4 >::absmin
T absmin() const
Returns absolute minimum among all elements.
jet::Matrix< T, 4, 4 >::determinant
T determinant() const
Returns determinant of this matrix.
jet::Matrix< T, 4, 4 >::operator=
Matrix & operator=(const Matrix &m)
Assigns input matrix.
jet::Vector< T, 3 >
3-D vector class.
Definition: vector3.h:25
jet::Matrix< T, 4, 4 >::makeRotationMatrix
static Matrix makeRotationMatrix(const Vector3< T > &axis, T rad)
jet::Matrix< T, 4, 4 >::inverse
Matrix inverse() const
Returns inverse matrix.
jet::Matrix< T, 4, 4 >::rmul
Matrix rmul(const Matrix &m) const
Returns input matrix * this matrix.
jet::Matrix< T, 4, 4 >::rsub
Matrix rsub(T s) const
Returns input scalar - this matrix.
jet::Matrix< T, 4, 4 >::imul
void imul(const Matrix &m)
Multiplies input matrix to this matrix.
jet::Matrix< T, 4, 4 >::operator()
T & operator()(size_t i, size_t j)
Returns reference of (i,j) element.
jet::operator/
Matrix2x2< T > operator/(const Matrix2x2< T > &a, T b)
Returns a' / b, where every element of matrix a' is a.
jet::Matrix< T, 4, 4 >::setDiagonal
void setDiagonal(T s)
Sets diagonal elements with input scalar.
jet::Matrix< T, 4, 4 >::sum
T sum() const
Returns sum of all elements.
jet::Matrix< T, 4, 4 >::matrix3
Matrix< T, 3, 3 > matrix3() const
Returns 3x3 part of this matrix.
jet::Matrix< T, 4, 4 >::avg
T avg() const
Returns average of all elements.
jet::Matrix< T, 4, 4 >::operator+=
Matrix & operator+=(T s)
Addition assignment with input scalar.
jet::operator-
Matrix2x2< T > operator-(const Matrix2x2< T > &a)
Returns a matrix with opposite sign.
jet::Matrix< T, 4, 4 >::cols
size_t cols() const
Returns number of columns of this matrix.
jet::Matrix< T, 4, 4 >::iadd
void iadd(T s)
Adds input scalar to this matrix.
jet::Matrix< T, 4, 4 >::operator!=
bool operator!=(const Matrix &m) const
Returns true if is not equal to m.
jet::Matrix< T, 4, 4 >::setOffDiagonal
void setOffDiagonal(T s)
Sets off-diagonal elements with input scalar.
jet::Matrix< T, 4, 4 >::rsub
Matrix rsub(const Matrix &m) const
Returns input matrix - this matrix (element-wise).
jet::Matrix< T, 4, 4 >::diagonal
Matrix diagonal() const
Returns diagonal part of this matrix.
jet::operator*
Matrix2x2< T > operator*(const Matrix2x2< T > &a, T b)
Returns a * b', where every element of matrix b' is b.
jet::Matrix< T, 4, 4 >::isub
void isub(T s)
Subtracts input scalar from this matrix.
jet::Matrix< T, 4, 4 >::set
void set(const Matrix &m)
Copies from input matrix.
jet::Matrix< T, 4, 4 >::add
Matrix add(const Matrix &m) const
Returns this matrix + input matrix (element-wise).
jet::Matrix< T, 4, 4 >::add
Matrix add(T s) const
Returns this matrix + input scalar.
jet::Matrix< T, 4, 4 >::Matrix
Matrix(T s)
Constructs constant value matrix.
jet::Matrix< T, 4, 4 >::operator[]
T & operator[](size_t i)
Returns reference of i-th element.
jet::Matrix< T, 4, 4 >::mul
Vector4< T > mul(const Vector4< T > &v) const
Returns this matrix * input vector.