Matrix2x2.h
Go to the documentation of this file.
1 /*************************************************************************
2 > File Name: Matrix2x2.h
3 > Project Name: CubbyFlow
4 > Author: Chan-Ho Chris Ohk
5 > Purpose: 2-D matrix class.
6 > Created Time: 2017/03/06
7 > Copyright (c) 2018, Chan-Ho Chris Ohk
8 *************************************************************************/
9 #ifndef CUBBYFLOW_MATRIX2X2_H
10 #define CUBBYFLOW_MATRIX2X2_H
11 
12 #include <Core/Matrix/Matrix.h>
13 #include <Core/Vector/Vector2.h>
14 
15 #include <array>
16 #include <limits>
17 
18 namespace CubbyFlow
19 {
27  template <typename T>
28  class Matrix<T, 2, 2>
29  {
30  public:
31  static_assert(std::is_floating_point<T>::value, "Matrix only can be instantiated with floating point types");
32 
33  // MARK: Constructors
35  Matrix();
36 
38  explicit Matrix(T s);
39 
42  Matrix(
43  T m00, T m01,
44  T m10, T m11);
45 
64  template <typename U>
65  Matrix(const std::initializer_list<std::initializer_list<U>>& list);
66 
68  Matrix(const Matrix& m);
69 
72  explicit Matrix(const T* arr);
73 
74  // MARK: Basic setters
76  void Set(T s);
77 
80  void Set(
81  T m00, T m01,
82  T m10, T m11);
83 
103  template <typename U>
104  void Set(const std::initializer_list<std::initializer_list<U>>& list);
105 
107  void Set(const Matrix& m);
108 
111  void Set(const T* arr);
112 
114  void SetDiagonal(T s);
115 
117  void SetOffDiagonal(T s);
118 
120  void SetRow(size_t i, const Vector2<T>& row);
121 
123  void SetColumn(size_t i, const Vector2<T>& col);
124 
125  // MARK: Basic getters
127  bool IsSimilar(const Matrix& m, double tol = std::numeric_limits<double>::epsilon()) const;
128 
130  bool IsSquare() const;
131 
133  size_t Rows() const;
134 
136  size_t Cols() const;
137 
139  T* data();
140 
142  const T* data() const;
143 
144  // MARK: Binary operator methods - new instance = this instance (+) input
146  Matrix Add(T s) const;
147 
149  Matrix Add(const Matrix& m) const;
150 
152  Matrix Sub(T s) const;
153 
155  Matrix Sub(const Matrix& m) const;
156 
158  Matrix Mul(T s) const;
159 
161  Vector2<T> Mul(const Vector2<T>& v) const;
162 
164  Matrix Mul(const Matrix& m) const;
165 
167  Matrix Div(T s) const;
168 
169  // MARK: Binary operator methods - new instance = input (+) this instance
171  Matrix RAdd(T s) const;
172 
174  Matrix RAdd(const Matrix& m) const;
175 
177  Matrix RSub(T s) const;
178 
180  Matrix RSub(const Matrix& m) const;
181 
183  Matrix RMul(T s) const;
184 
186  Matrix RMul(const Matrix& m) const;
187 
189  Matrix RDiv(T s) const;
190 
191  // MARK: Augmented operator methods - this instance (+)= input
193  void IAdd(T s);
194 
196  void IAdd(const Matrix& m);
197 
199  void ISub(T s);
200 
202  void ISub(const Matrix& m);
203 
205  void IMul(T s);
206 
208  void IMul(const Matrix& m);
209 
211  void IDiv(T s);
212 
213  // MARK: Modifiers
215  void Transpose();
216 
218  void Invert();
219 
220  // MARK: Complex getters
222  T Sum() const;
223 
225  T Avg() const;
226 
228  T Min() const;
229 
231  T Max() const;
232 
234  T AbsMin() const;
235 
237  T AbsMax() const;
238 
240  T Trace() const;
241 
243  T Determinant() const;
244 
246  Matrix Diagonal() const;
247 
249  Matrix OffDiagonal() const;
250 
252  Matrix StrictLowerTriangle() const;
253 
255  Matrix StrictUpperTriangle() const;
256 
258  Matrix LowerTriangle() const;
259 
261  Matrix UpperTriangle() const;
262 
264  Matrix Transposed() const;
265 
267  Matrix Inverse() const;
268 
269  template <typename U>
270  Matrix<U, 2, 2> CastTo() const;
271 
272  // MARK: Setter operators
274  Matrix& operator=(const Matrix& m);
275 
277  Matrix& operator+=(T s);
278 
280  Matrix& operator+=(const Matrix& m);
281 
283  Matrix& operator-=(T s);
284 
286  Matrix& operator-=(const Matrix& m);
287 
289  Matrix& operator*=(T s);
290 
292  Matrix& operator*=(const Matrix& m);
293 
295  Matrix& operator/=(T s);
296 
297  // MARK: Getter operators
299  T& operator[](size_t i);
300 
302  const T& operator[](size_t i) const;
303 
305  T& operator()(size_t i, size_t j);
306 
308  const T& operator()(size_t i, size_t j) const;
309 
311  bool operator==(const Matrix& m) const;
312 
314  bool operator!=(const Matrix& m) const;
315 
316  // MARK: Helpers
318  static Matrix MakeZero();
319 
321  static Matrix MakeIdentity();
322 
324  static Matrix MakeScaleMatrix(T sx, T sy);
325 
327  static Matrix MakeScaleMatrix(const Vector2<T>& s);
328 
331  static Matrix MakeRotationMatrix(const T& rad);
332 
333  private:
334  std::array<T, 4> m_elements;
335  };
336 
338  template <typename T> using Matrix2x2 = Matrix<T, 2, 2>;
339 
340  // MARK: Operator overloading
342  template <typename T>
344 
346  template <typename T>
348 
350  template <typename T>
351  Matrix<T, 2, 2> operator+(const Matrix<T, 2, 2>& a, const T b);
352 
354  template <typename T>
355  Matrix<T, 2, 2> operator+(const T a, const Matrix<T, 2, 2>& b);
356 
358  template <typename T>
360 
362  template <typename T>
364 
366  template <typename T>
368 
370  template <typename T>
372 
374  template <typename T>
376 
378  template <typename T>
379  Vector2<T> operator*(const Matrix<T, 2, 2>& a, const Vector2<T>& b);
380 
382  template <typename T>
384 
386  template <typename T>
387  Matrix2x2<T> operator/(const Matrix2x2<T>& a, T b);
388 
390  template <typename T>
391  Matrix2x2<T> operator/(const T& a, const Matrix2x2<T>& b);
392 
395 
398 }
399 
401 
402 #endif
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
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
2-D vector class.
Definition: Vector2.h:26
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
2-D matrix class.
Definition: Matrix2x2.h:28
void SetColumn(size_t j, const VectorExpression< T, E > &col)
Sets j-th column with input vector.
Definition: Matrix-Impl.h:130