Matrix3x3.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Matrix3x3.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 3-D matrix class.
6 > Created Time: 2017/03/06
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_MATRIX3X3_H
10 #define CUBBYFLOW_MATRIX3X3_H
11 
12 #include <Core/Matrix/Matrix.h>
13 #include <Core/Vector/Vector3.h>
14 
15 #include <array>
16 #include <limits>
17 
18 namespace CubbyFlow
19 {
29  template <typename T>
30  class Matrix<T, 3, 3>
31  {
32  public:
33  static_assert(std::is_floating_point<T>::value, "Matrix only can be instantiated with floating point types");
34 
35  // MARK: Constructors
37  Matrix();
38 
40  explicit Matrix(T s);
41 
44  Matrix(
45  T m00, T m01, T m02,
46  T m10, T m11, T m12,
47  T m20, T m21, T m22);
48 
68  template <typename U>
69  Matrix(const std::initializer_list<std::initializer_list<U>>& list);
70 
72  Matrix(const Matrix& m);
73 
76  explicit Matrix(const T* arr);
77 
78  // MARK: Basic setters
80  void Set(T s);
81 
84  void Set(
85  T m00, T m01, T m02,
86  T m10, T m11, T m12,
87  T m20, T m21, T m22);
88 
109  template <typename U>
110  void Set(const std::initializer_list<std::initializer_list<U>>& list);
111 
113  void Set(const Matrix& m);
114 
117  void Set(const T* arr);
118 
120  void SetDiagonal(T s);
121 
123  void SetOffDiagonal(T s);
124 
126  void SetRow(size_t i, const Vector3<T>& row);
127 
129  void SetColumn(size_t i, const Vector3<T>& col);
130 
131  // MARK: Basic getters
134  bool IsSimilar(const Matrix& m, double tol = std::numeric_limits<double>::epsilon()) const;
135 
137  bool IsSquare() const;
138 
140  size_t Rows() const;
141 
143  size_t Cols() const;
144 
146  T* data();
147 
149  const T* data() const;
150 
151  // MARK: Binary operator methods - new instance = this instance (+) input
153  Matrix Add(T s) const;
154 
156  Matrix Add(const Matrix& m) const;
157 
159  Matrix Sub(T s) const;
160 
162  Matrix Sub(const Matrix& m) const;
163 
165  Matrix Mul(T s) const;
166 
168  Vector3<T> Mul(const Vector3<T>& v) const;
169 
171  Matrix Mul(const Matrix& m) const;
172 
174  Matrix Div(T s) const;
175 
176  // MARK: Binary operator methods - new instance = input (+) this instance
178  Matrix RAdd(T s) const;
179 
181  Matrix RAdd(const Matrix& m) const;
182 
184  Matrix RSub(T s) const;
185 
187  Matrix RSub(const Matrix& m) const;
188 
190  Matrix RMul(T s) const;
191 
193  Matrix RMul(const Matrix& m) const;
194 
196  Matrix RDiv(T s) const;
197 
198  // MARK: Augmented operator methods - this instance (+)= input
200  void IAdd(T s);
201 
203  void IAdd(const Matrix& m);
204 
206  void ISub(T s);
207 
209  void ISub(const Matrix& m);
210 
212  void IMul(T s);
213 
215  void IMul(const Matrix& m);
216 
218  void IDiv(T s);
219 
220  // MARK: Modifiers
222  void Transpose();
223 
225  void Invert();
226 
227  // MARK: Complex getters
229  T Sum() const;
230 
232  T Avg() const;
233 
235  T Min() const;
236 
238  T Max() const;
239 
241  T AbsMin() const;
242 
244  T AbsMax() const;
245 
247  T Trace() const;
248 
250  T Determinant() const;
251 
253  Matrix Diagonal() const;
254 
256  Matrix OffDiagonal() const;
257 
259  Matrix StrictLowerTriangle() const;
260 
262  Matrix StrictUpperTriangle() const;
263 
265  Matrix LowerTriangle() const;
266 
268  Matrix UpperTriangle() const;
269 
271  Matrix Transposed() const;
272 
274  Matrix Inverse() const;
275 
276  template <typename U>
277  Matrix<U, 3, 3> CastTo() const;
278 
279  // MARK: Setter operators
281  Matrix& operator=(const Matrix& m);
282 
284  Matrix& operator+=(T s);
285 
287  Matrix& operator+=(const Matrix& m);
288 
290  Matrix& operator-=(T s);
291 
293  Matrix& operator-=(const Matrix& m);
294 
296  Matrix& operator*=(T s);
297 
299  Matrix& operator*=(const Matrix& m);
300 
302  Matrix& operator/=(T s);
303 
304  // MARK: Getter operators
306  T& operator[](size_t i);
307 
309  const T& operator[](size_t i) const;
310 
312  T& operator()(size_t i, size_t j);
313 
315  const T& operator()(size_t i, size_t j) const;
316 
318  bool operator==(const Matrix& m) const;
319 
321  bool operator!=(const Matrix& m) const;
322 
323  // MARK: Helpers
325  static Matrix MakeZero();
326 
328  static Matrix MakeIdentity();
329 
331  static Matrix MakeScaleMatrix(T sx, T sy, T sz);
332 
334  static Matrix MakeScaleMatrix(const Vector3<T>& s);
335 
338  static Matrix MakeRotationMatrix(const Vector3<T>& axis, T rad);
339 
340  private:
341  std::array<T, 9> m_elements;
342  };
343 
345  template <typename T> using Matrix3x3 = Matrix<T, 3, 3>;
346 
347  // Operator overloading
349  template <typename T>
351 
353  template <typename T>
355 
357  template <typename T>
359 
361  template <typename T>
363 
365  template <typename T>
367 
369  template <typename T>
371 
373  template <typename T>
375 
377  template <typename T>
379 
381  template <typename T>
383 
385  template <typename T>
386  Vector3<T> operator*(const Matrix<T, 3, 3>& a, const Vector3<T>& b);
387 
389  template <typename T>
391 
393  template <typename T>
395 
397  template <typename T>
399 
402 
405 }
406 
408 
409 #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
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
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