Matrix4x4.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Matrix4x4.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 4-D matrix class.
6 > Created Time: 2017/03/06
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_MATRIX4X4_H
10 #define CUBBYFLOW_MATRIX4X4_H
11 
12 #include <Core/Matrix/Matrix3x3.h>
13 #include <Core/Vector/Vector4.h>
14 
15 #include <array>
16 #include <limits>
17 
18 namespace CubbyFlow
19 {
28  template <typename T>
29  class Matrix<T, 4, 4>
30  {
31  public:
32  static_assert(std::is_floating_point<T>::value, "Matrix only can be instantiated with floating point types");
33 
34  // MARK: Constructors
36  Matrix();
37 
39  explicit Matrix(T s);
40 
45  Matrix(
46  T m00, T m01, T m02,
47  T m10, T m11, T m12,
48  T m20, T m21, T m22);
49 
52  Matrix(
53  T m00, T m01, T m02, T m03,
54  T m10, T m11, T m12, T m13,
55  T m20, T m21, T m22, T m23,
56  T m30, T m31, T m32, T m33);
57 
78  template <typename U>
79  Matrix(const std::initializer_list<std::initializer_list<U>>& list);
80 
84  explicit Matrix(const Matrix3x3<T>& m33);
85 
87  Matrix(const Matrix& m);
88 
91  explicit Matrix(const T* arr);
92 
93  // MARK: Basic setters
95  void Set(T s);
96 
101  void Set(
102  T m00, T m01, T m02,
103  T m10, T m11, T m12,
104  T m20, T m21, T m22);
105 
108  void Set(
109  T m00, T m01, T m02, T m03,
110  T m10, T m11, T m12, T m13,
111  T m20, T m21, T m22, T m23,
112  T m30, T m31, T m32, T m33);
113 
135  template <typename U>
136  void Set(const std::initializer_list<std::initializer_list<U>>& list);
137 
141  void Set(const Matrix<T, 3, 3>& m33);
142 
144  void Set(const Matrix& m);
145 
148  void Set(const T* arr);
149 
151  void SetDiagonal(T s);
152 
154  void SetOffDiagonal(T s);
155 
157  void SetRow(size_t i, const Vector4<T>& row);
158 
160  void SetColumn(size_t i, const Vector4<T>& col);
161 
162  // MARK: Basic getters
165  bool IsSimilar(const Matrix& m, double tol = std::numeric_limits<double>::epsilon()) const;
166 
168  bool IsSquare() const;
169 
171  size_t Rows() const;
172 
174  size_t Cols() const;
175 
177  T* data();
178 
180  const T* data() const;
181 
183  Matrix<T, 3, 3> Matrix3() const;
184 
185  // MARK: Binary operator methods - new instance = this instance (+) input
187  Matrix Add(T s) const;
188 
190  Matrix Add(const Matrix& m) const;
191 
193  Matrix Sub(T s) const;
194 
196  Matrix Sub(const Matrix& m) const;
197 
199  Matrix Mul(T s) const;
200 
202  Vector4<T> Mul(const Vector4<T>& v) const;
203 
205  Matrix Mul(const Matrix& m) const;
206 
208  Matrix Div(T s) const;
209 
210  // MARK: Binary operator methods - new instance = input (+) this instance
212  Matrix RAdd(T s) const;
213 
215  Matrix RAdd(const Matrix& m) const;
216 
218  Matrix RSub(T s) const;
219 
221  Matrix RSub(const Matrix& m) const;
222 
224  Matrix RMul(T s) const;
225 
227  Matrix RMul(const Matrix& m) const;
228 
230  Matrix RDiv(T s) const;
231 
232  // MARK: Augmented operator methods - this instance (+)= input
234  void IAdd(T s);
235 
237  void IAdd(const Matrix& m);
238 
240  void ISub(T s);
241 
243  void ISub(const Matrix& m);
244 
246  void IMul(T s);
247 
249  void IMul(const Matrix& m);
250 
252  void IDiv(T s);
253 
254  // MARK: Modifiers
256  void Transpose();
257 
259  void Invert();
260 
261  // MARK: Complex getters
263  T Sum() const;
264 
266  T Avg() const;
267 
269  T Min() const;
270 
272  T Max() const;
273 
275  T AbsMin() const;
276 
278  T AbsMax() const;
279 
281  T Trace() const;
282 
284  T Determinant() const;
285 
287  Matrix Diagonal() const;
288 
290  Matrix OffDiagonal() const;
291 
293  Matrix StrictLowerTriangle() const;
294 
296  Matrix StrictUpperTriangle() const;
297 
299  Matrix LowerTriangle() const;
300 
302  Matrix UpperTriangle() const;
303 
305  Matrix Transposed() const;
306 
308  Matrix Inverse() const;
309 
310  template <typename U>
311  Matrix<U, 4, 4> CastTo() const;
312 
313  // MARK: Setter operators
315  Matrix& operator=(const Matrix& m);
316 
318  Matrix& operator+=(T s);
319 
321  Matrix& operator+=(const Matrix& m);
322 
324  Matrix& operator-=(T s);
325 
327  Matrix& operator-=(const Matrix& m);
328 
330  Matrix& operator*=(T s);
331 
333  Matrix& operator*=(const Matrix& m);
334 
336  Matrix& operator/=(T s);
337 
338  // MARK: Getter operators
340  T& operator[](size_t i);
341 
343  const T& operator[](size_t i) const;
344 
346  T& operator()(size_t i, size_t j);
347 
349  const T& operator()(size_t i, size_t j) const;
350 
352  bool operator==(const Matrix& m) const;
353 
355  bool operator!=(const Matrix& m) const;
356 
357  // MARK: Helpers
359  static Matrix MakeZero();
360 
362  static Matrix MakeIdentity();
363 
365  static Matrix MakeScaleMatrix(T sx, T sy, T sz);
366 
368  static Matrix MakeScaleMatrix(const Vector3<T>& s);
369 
372  static Matrix MakeRotationMatrix(const Vector3<T>& axis, T rad);
373 
375  static Matrix MakeTranslationMatrix(const Vector3<T>& t);
376 
377  private:
378  std::array<T, 16> m_elements;
379  };
380 
382  template <typename T> using Matrix4x4 = Matrix<T, 4, 4>;
383 
384  // Operator overloading
386  template <typename T>
388 
390  template <typename T>
392 
394  template <typename T>
396 
398  template <typename T>
400 
402  template <typename T>
404 
406  template <typename T>
408 
410  template <typename T>
412 
414  template <typename T>
416 
418  template <typename T>
420 
422  template <typename T>
423  Vector3<T> operator*(const Matrix<T, 4, 4>& a, const Vector3<T>& b);
424 
426  template <typename T>
427  Vector4<T> operator*(const Matrix<T, 4, 4>& a, const Vector4<T>& b);
428 
430  template <typename T>
432 
434  template <typename T>
436 
438  template <typename T>
439  Matrix<T, 4, 4> operator/(const T& a, const Matrix<T, 4, 4>& b);
440 
443 
446 }
447 
449 
450 #endif
3-D vector class.
Definition: Vector3.h:26
bool IsSimilar(const MatrixExpression< T, E > &other, double tol=std::numeric_limits< double >::epsilon()) const
Definition: Matrix-Impl.h:169
static MatrixConstant< T > MakeZero()
Makes a M x N matrix with zeros.
Definition: Matrix-Impl.h:823
MatrixDiagonal< T, Matrix > Diagonal() const
Returns diagonal part of this matrix.
Definition: Matrix-Impl.h:633
MatrixDiagonal< T, Matrix > OffDiagonal() const
Returns off-diagonal part of this matrix.
Definition: Matrix-Impl.h:639
T Sum() const
Returns sum of all elements.
Definition: Matrix-Impl.h:483
Matrix & operator*=(const T &s)
Multiplication assignment with input scalar.
Definition: Matrix-Impl.h:737
Matrix & operator/=(const T &s)
Division assignment with input scalar.
Definition: Matrix-Impl.h:752
T * data()
Returns data pointer of this matrix.
Definition: Matrix-Impl.h:217
void SetDiagonal(const T &s)
Sets diagonal elements with input scalar.
Definition: Matrix-Impl.h:92
Matrix< T, 2, 2 > operator+(const Matrix< T, 2, 2 > &a, const Matrix< T, 2, 2 > &b)
Returns a + b (element-size).
Definition: Matrix2x2-Impl.h:660
void IMul(const T &s)
Multiplies input scalar to this matrix.
Definition: Matrix-Impl.h:376
MatrixScalarRSub< T, Matrix > RSub(const T &s) const
Returns input scalar - this matrix.
Definition: Matrix-Impl.h:318
void IAdd(const T &s)
Adds input scalar to this matrix.
Definition: Matrix-Impl.h:350
void Set(const T &s)
Sets whole matrix with input scalar.
Definition: Matrix-Impl.h:52
MatrixScalarAdd< T, Matrix > Add(const T &s) const
Returns this matrix + input scalar.
Definition: Matrix-Impl.h:253
void SetRow(size_t i, const VectorExpression< T, E > &row)
Sets i-th row with input vector.
Definition: Matrix-Impl.h:116
void IDiv(const T &s)
Divides this matrix with input scalar.
Definition: Matrix-Impl.h:390
bool operator!=(const MatrixExpression< T, E > &m) const
Returns true if is not equal to m.
Definition: Matrix-Impl.h:791
void ISub(const T &s)
Subtracts input scalar from this matrix.
Definition: Matrix-Impl.h:363
constexpr size_t Cols() const
Returns number of columns of this matrix.
Definition: Matrix-Impl.h:211
T Max() const
Returns maximum among all elements.
Definition: Matrix-Impl.h:515
Matrix & operator+=(const T &s)
Addition assignment with input scalar.
Definition: Matrix-Impl.h:707
MatrixScalarDiv< T, Matrix > Div(const T &s) const
Returns this matrix / input scalar.
Definition: Matrix-Impl.h:299
MatrixScalarSub< T, Matrix > Sub(const T &s) const
Returns this matrix - input scalar.
Definition: Matrix-Impl.h:266
Matrix Inverse() const
Returns inverse matrix.
Definition: Matrix-Impl.h:677
MatrixScalarMul< T, Matrix > RMul(const T &s) const
Returns input scalar * this matrix.
Definition: Matrix-Impl.h:331
MatrixScalarAdd< T, Matrix > RAdd(const T &s) const
Returns input scalar + this matrix.
Definition: Matrix-Impl.h:305
Static-sized M x N matrix class.
Definition: Matrix.h:30
MatrixScalarMul< T, Matrix > Mul(const T &s) const
Returns this matrix * input scalar.
Definition: Matrix-Impl.h:279
void Invert()
Inverts this matrix.
Definition: Matrix-Impl.h:402
Matrix< T, 2, 2 > operator/(const Matrix< T, 2, 2 > &a, T b)
Definition: Matrix2x2-Impl.h:720
Definition: pybind11Utils.h:24
3-D matrix class.
Definition: Matrix3x3.h:30
4-D matrix class.
Definition: Matrix4x4.h:29
T & operator[](size_t i)
Returns reference of i-th element.
Definition: Matrix-Impl.h:759
bool operator==(const MatrixExpression< T, E > &m) const
Returns true if is equal to m.
Definition: Matrix-Impl.h:784
constexpr bool IsSquare() const
Returns true if this matrix is a square matrix.
Definition: Matrix-Impl.h:193
Matrix< T, 2, 2 > operator-(const Matrix< T, 2, 2 > &a)
Returns a matrix with opposite sign.
Definition: Matrix2x2-Impl.h:654
4-D vector class.
Definition: Vector4.h:26
Matrix & operator-=(const T &s)
Subtraction assignment with input scalar.
Definition: Matrix-Impl.h:722
Matrix & operator=(const E &m)
Assigns input matrix.
const Matrix< T, M, N > & operator()() const
Returns actual implementation (the subclass).
Definition: MatrixExpression-Impl.h:34
T Trace() const
Definition: Matrix-Impl.h:554
T Avg() const
Returns average of all elements.
Definition: Matrix-Impl.h:496
T Determinant() const
Returns determinant of this matrix.
Definition: Matrix-Impl.h:569
void Transpose()
Transposes this matrix.
Definition: Matrix-Impl.h:396
static MatrixIdentity< T > MakeIdentity()
Makes a M x N matrix with all diagonal elements to 1, and other elements to 0.
Definition: Matrix-Impl.h:829
T Min() const
Returns minimum among all elements.
Definition: Matrix-Impl.h:502
T AbsMin() const
Returns absolute minimum among all elements.
Definition: Matrix-Impl.h:528
Vector< T, 3 > operator*(const Quaternion< T > &q, const Vector< T, 3 > &v)
Returns quaternion q * vector v.
Definition: Quaternion-Impl.h:481
void SetOffDiagonal(const T &s)
Sets off-diagonal elements with input scalar.
Definition: Matrix-Impl.h:103
MatrixTypeCast< U, Matrix, T > CastTo() const
MatrixScalarRDiv< T, Matrix > RDiv(const T &s) const
Returns input matrix / this scalar.
Definition: Matrix-Impl.h:344
constexpr size_t Rows() const
Returns number of rows of this matrix.
Definition: Matrix-Impl.h:205
Matrix< T, N, M > Transposed() const
Returns transposed matrix.
Definition: Matrix-Impl.h:669
T AbsMax() const
Returns absolute maximum among all elements.
Definition: Matrix-Impl.h:541
Matrix()
Definition: Matrix-Impl.h:15
void SetColumn(size_t j, const VectorExpression< T, E > &col)
Sets j-th column with input vector.
Definition: Matrix-Impl.h:130