mat2.h
1 /*
2 ** UICore
3 ** Copyright (c) 1997-2015 The UICore Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries UICore may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Mark Page
28 ** Harry Storbacka
29 */
30 
31 #pragma once
32 
33 #include <cstdint>
34 #include "mat3.h"
35 #include "mat4.h"
36 #include "vec2.h"
37 
38 namespace uicore
39 {
40  template<typename Type>
41  class Mat2;
42 
43  template<typename Type>
44  class Mat3;
45 
46  template<typename Type>
47  class Mat4;
48 
52  template<typename Type>
53  class Mat2
54  {
55  public:
57  Mat2()
58  {
59  for (auto & elem : matrix)
60  elem = 0;
61  }
63  Mat2(const Mat2<Type> &copy)
64  {
65  for (int i = 0; i < 4; i++)
66  matrix[i] = copy.matrix[i];
67  }
68 
70  explicit Mat2(const Mat3<Type> &copy);
71 
73  explicit Mat2(const Mat4<Type> &copy);
74 
76  explicit Mat2(const float *init_matrix)
77  {
78  for (int i = 0; i < 4; i++)
79  matrix[i] = (Type)init_matrix[i];
80  }
81 
83  explicit Mat2(Type m00, Type m01, Type m10, Type m11)
84  {
85  matrix[0 * 2 + 0] = m00; matrix[0 * 2 + 1] = m01;
86  matrix[1 * 2 + 0] = m10; matrix[1 * 2 + 1] = m11;
87  }
88 
90  explicit Mat2(const double *init_matrix)
91  {
92  for (int i = 0; i < 4; i++)
93  matrix[i] = (Type)init_matrix[i];
94  }
95 
97  explicit Mat2(const int64_t *init_matrix)
98  {
99  for (int i = 0; i < 4; i++)
100  matrix[i] = (Type)init_matrix[i];
101  }
102 
104  explicit Mat2(const int32_t *init_matrix)
105  {
106  for (int i = 0; i < 4; i++)
107  matrix[i] = (Type)init_matrix[i];
108  }
109 
111  explicit Mat2(const int16_t *init_matrix)
112  {
113  for (int i = 0; i < 4; i++)
114  matrix[i] = (Type)init_matrix[i];
115  }
116 
118  explicit Mat2(const int8_t *init_matrix)
119  {
120  for (int i = 0; i < 4; i++)
121  matrix[i] = (Type)init_matrix[i];
122  }
123 
124  static Mat2<Type> null();
125 
126  static Mat2<Type> identity();
127 
136  static Mat2<Type> multiply(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
137 
145  static Mat2<Type> add(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
146 
154  static Mat2<Type> subtract(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
155 
161  static bool is_equal(const Mat2<Type> &first, const Mat2<Type> &second, Type epsilon)
162  {
163  for (int i = 0; i < 4; i++)
164  {
165  Type diff = second.matrix[i] - first.matrix[i];
166  if (diff < -epsilon || diff > epsilon) return false;
167  }
168  return true;
169  }
170 
172  Type matrix[4];
173 
178  bool is_equal(const Mat2<Type> &other, Type epsilon) const { return Mat2<Type>::is_equal(*this, other, epsilon); }
179 
181  operator Type const*() const { return matrix; }
182 
184  operator Type *() { return matrix; }
185 
187  Type &operator[](int i) { return matrix[i]; }
188 
190  const Type &operator[](int i) const { return matrix[i]; }
191 
193  Type &operator[](unsigned int i) { return matrix[i]; }
194 
196  const Type &operator[](unsigned int i) const { return matrix[i]; }
197 
199  Mat2<Type> &operator =(const Mat2<Type> &copy) { memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
200 
202  Mat2<Type> &operator =(const Mat4<Type> &copy);
203 
205  Mat2<Type> &operator =(const Mat3<Type> &copy);
206 
208  Mat2<Type> operator *(const Mat2<Type> &mult) const;
209 
211  Mat2<Type> operator +(const Mat2<Type> &add_matrix) const;
212 
214  Mat2<Type> operator -(const Mat2<Type> &subtract_matrix) const;
215 
217  bool operator==(const Mat2<Type> &other) const
218  {
219  for (int i = 0; i < 4; i++)
220  if (matrix[i] != other.matrix[i]) return false;
221  return true;
222  }
223 
225  bool operator!=(const Mat2<Type> &other) const { return !((*this) == other); }
226  };
227 
228  typedef Mat2<int> Mat2i;
231 }
Mat2()
Constructs a 2x2 matrix (zero'ed)
Definition: mat2.h:57
2D matrix
Definition: mat2.h:41
bool operator==(const Mat2< Type > &other) const
Equality operator.
Definition: mat2.h:217
Type & operator[](int i)
Operator that returns the matrix cell at the given index.
Definition: mat2.h:187
bool operator!=(const Mat2< Type > &other) const
Not-equal operator.
Definition: mat2.h:225
Mat2< Type > & operator=(const Mat2< Type > &copy)
Copy assignment operator.
Definition: mat2.h:199
Mat2(Type m00, Type m01, Type m10, Type m11)
Constructs a 2x2 matrix (copied from specified values)
Definition: mat2.h:83
3D matrix
Definition: mat2.h:44
Type matrix[4]
The matrix (in column-major format)
Definition: mat2.h:172
Mat2< Type > operator-(const Mat2< Type > &subtract_matrix) const
Subtract operator.
Mat2(const Mat2< Type > &copy)
Constructs a 2x2 matrix (copied)
Definition: mat2.h:63
Mat2(const int16_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 16 bit integers)
Definition: mat2.h:111
Mat2(const int32_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 32 bit integers)
Definition: mat2.h:104
static Mat2< Type > null()
bool is_equal(const Mat2< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: mat2.h:178
Mat2< int > Mat2i
Definition: mat2.h:228
Mat2(const double *init_matrix)
Constructs a 2x2 matrix (copied from 4 doubles)
Definition: mat2.h:90
Mat2(const float *init_matrix)
Constructs a 2x2 matrix (copied from 4 floats)
Definition: mat2.h:76
Mat2< Type > operator+(const Mat2< Type > &add_matrix) const
Addition operator.
static Mat2< Type > multiply(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Multiply 2 matrices.
static Mat2< Type > identity()
Mat2(const int64_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 64 bit integers)
Definition: mat2.h:97
static bool is_equal(const Mat2< Type > &first, const Mat2< Type > &second, Type epsilon)
Returns true if equal within the bounds of an epsilon.
Definition: mat2.h:161
Mat2< Type > operator*(const Mat2< Type > &mult) const
Multiplication operator.
static Mat2< Type > add(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Add 2 matrices.
const Type & operator[](int i) const
Operator that returns the matrix cell at the given index.
Definition: mat2.h:190
Mat2(const int8_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 8 bit integers)
Definition: mat2.h:118
Type & operator[](unsigned int i)
Operator that returns the matrix cell at the given index.
Definition: mat2.h:193
static Mat2< Type > subtract(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Subtract 2 matrices.
Mat2< float > Mat2f
Definition: mat2.h:229
Mat2< double > Mat2d
Definition: mat2.h:230
Definition: Application/application.h:35
4D matrix
Definition: mat2.h:47
const Type & operator[](unsigned int i) const
Operator that returns the matrix cell at the given index.
Definition: mat2.h:196