mat2.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib 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 ClanLib 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
32 #pragma once
33
34 #include "../System/cl_platform.h"
35 #include "mat3.h"
36 #include "mat4.h"
37 #include "vec2.h"
38
39 namespace clan
40 {
43
44 template<typename Type>
45 class Mat2;
46
47 template<typename Type>
48 class Mat3;
49
50 template<typename Type>
51 class Mat4;
52
53 class Angle;
54
58 template<typename Type>
59 class Mat2
60 {
63
64 public:
66 Mat2()
67 {
68 for (auto & elem : matrix)
69 elem = 0;
70 }
72 Mat2(const Mat2<Type> &copy)
73 {
74 for (int i=0; i<4; i++)
75 matrix[i] = copy.matrix[i];
76 }
77
79 explicit Mat2(const Mat3<Type> &copy);
80
82 explicit Mat2(const Mat4<Type> &copy);
83
85 explicit Mat2(const float *init_matrix)
86 {
87 for (int i=0; i<4; i++)
88 matrix[i] = (Type) init_matrix[i];
89 }
90
92 explicit Mat2(Type m00, Type m01, Type m10, Type m11)
93 {
94 matrix[0 * 2 + 0] = m00; matrix[0 * 2 + 1] = m01;
95 matrix[1 * 2 + 0] = m10; matrix[1 * 2 + 1] = m11;
96 }
97
99 explicit Mat2(const double *init_matrix)
100 {
101 for (int i=0; i<4; i++)
102 matrix[i] = (Type) init_matrix[i];
103 }
104
106 explicit Mat2(const int64_t *init_matrix)
107 {
108 for (int i=0; i<4; i++)
109 matrix[i] = (Type) init_matrix[i];
110 }
111
113 explicit Mat2(const int32_t *init_matrix)
114 {
115 for (int i=0; i<4; i++)
116 matrix[i] = (Type) init_matrix[i];
117 }
118
120 explicit Mat2(const int16_t *init_matrix)
121 {
122 for (int i=0; i<4; i++)
123 matrix[i] = (Type) init_matrix[i];
124 }
125
127 explicit Mat2(const int8_t *init_matrix)
128 {
129 for (int i=0; i<4; i++)
130 matrix[i] = (Type) init_matrix[i];
131 }
132
133 static Mat2<Type> null();
134
135 static Mat2<Type> identity();
136
145 static Mat2<Type> multiply(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
146
154 static Mat2<Type> add(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
155
163 static Mat2<Type> subtract(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
164
170 static bool is_equal(const Mat2<Type> &first, const Mat2<Type> &second, Type epsilon)
171 {
172 for (int i=0; i<4; i++)
173 {
174 Type diff = second.matrix[i] - first.matrix[i];
175 if (diff < -epsilon || diff > epsilon) return false;
176 }
177 return true;
178 }
179
183
184 public:
185 Type matrix[4];
186
190
191 public:
196 bool is_equal(const Mat2<Type> &other, Type epsilon) const { return Mat2<Type>::is_equal(*this, other, epsilon); }
197
201
202 public:
204 operator Type const*() const { return matrix; }
205
207 operator Type *() { return matrix; }
208
210 Type &operator[](int i) { return matrix[i]; }
211
213 const Type &operator[](int i) const { return matrix[i]; }
214
216 Type &operator[](unsigned int i) { return matrix[i]; }
217
219 const Type &operator[](unsigned int i) const { return matrix[i]; }
220
222 Mat2<Type> &operator =(const Mat2<Type> &copy) {memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
223
225 Mat2<Type> &operator =(const Mat4<Type> &copy);
226
228 Mat2<Type> &operator =(const Mat3<Type> &copy);
229
231 Mat2<Type> operator *(const Mat2<Type> &mult) const;
232
234 Mat2<Type> operator +(const Mat2<Type> &add_matrix) const;
235
237 Mat2<Type> operator -(const Mat2<Type> &subtract_matrix) const;
238
240 bool operator==(const Mat2<Type> &other) const
241 {
242 for (int i=0; i<4; i++)
243 if (matrix[i] != other.matrix[i]) return false;
244 return true;
245 }
246
248 bool operator!=(const Mat2<Type> &other) const { return !((*this) == other); }
249
253
254 private:
256 };
257
258 typedef Mat2<int> Mat2i;
259 typedef Mat2<float> Mat2f;
260 typedef Mat2<double> Mat2d;
261
262 }
263
clan::Mat2f
Mat2< float > Mat2f
Definition: mat2.h:259
clan::Mat2::identity
static Mat2< Type > identity()
clan
Definition: clanapp.h:35
clan::Mat2d
Mat2< double > Mat2d
Definition: mat2.h:260
clan::Angle
Angle class.
Definition: angle.h:62
clan::Mat2::Mat2
Mat2(const int64_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 64 bit integers)
Definition: mat2.h:106
clan::Mat2::subtract
static Mat2< Type > subtract(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Subtract 2 matrices.
clan::Mat2::operator+
Mat2< Type > operator+(const Mat2< Type > &add_matrix) const
Addition operator.
clan::Mat2::null
static Mat2< Type > null()
clan::Mat2::Mat2
Mat2(const int32_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 32 bit integers)
Definition: mat2.h:113
clan::Mat2::operator=
Mat2< Type > & operator=(const Mat2< Type > &copy)
Copy assignment operator.
Definition: mat2.h:222
clan::Mat2::is_equal
bool is_equal(const Mat2< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: mat2.h:196
clan::Mat2::Mat2
Mat2()
Constructs a 2x2 matrix (zero'ed)
Definition: mat2.h:66
clan::Mat2::Mat2
Mat2(const int8_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 8 bit integers)
Definition: mat2.h:127
clan::Mat2::operator*
Mat2< Type > operator*(const Mat2< Type > &mult) const
Multiplication operator.
clan::Mat2::Mat2
Mat2(const double *init_matrix)
Constructs a 2x2 matrix (copied from 4 doubles)
Definition: mat2.h:99
clan::Mat2::matrix
Type matrix[4]
Definition: mat2.h:185
clan::Mat2::operator[]
const Type & operator[](int i) const
Operator that returns the matrix cell at the given index.
Definition: mat2.h:213
clan::Mat2::Mat2
Mat2(const Mat2< Type > &copy)
Constructs a 2x2 matrix (copied)
Definition: mat2.h:72
clan::Mat2::Mat2
Mat2(Type m00, Type m01, Type m10, Type m11)
Constructs a 2x2 matrix (copied from specified values)
Definition: mat2.h:92
clan::Mat2::is_equal
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:170
clan::Mat2
2D matrix
Definition: mat2.h:45
clan::Mat2::operator-
Mat2< Type > operator-(const Mat2< Type > &subtract_matrix) const
Subtract operator.
clan::Mat2::operator[]
Type & operator[](int i)
Operator that returns the matrix cell at the given index.
Definition: mat2.h:210
clan::Mat3
3D matrix
Definition: mat2.h:48
clan::Mat2i
Mat2< int > Mat2i
Definition: mat2.h:258
clan::Mat2::Mat2
Mat2(const float *init_matrix)
Constructs a 2x2 matrix (copied from 4 floats)
Definition: mat2.h:85
clan::Key::i
clan::Mat2::add
static Mat2< Type > add(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Add 2 matrices.
clan::Mat2::operator[]
Type & operator[](unsigned int i)
Operator that returns the matrix cell at the given index.
Definition: mat2.h:216
clan::Mat4
4D matrix
Definition: mat2.h:51
clan::Mat2::operator==
bool operator==(const Mat2< Type > &other) const
Equality operator.
Definition: mat2.h:240
clan::Mat2::operator[]
const Type & operator[](unsigned int i) const
Operator that returns the matrix cell at the given index.
Definition: mat2.h:219
clan::Mat2::Mat2
Mat2(const int16_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 16 bit integers)
Definition: mat2.h:120
clan::Mat2::operator!=
bool operator!=(const Mat2< Type > &other) const
Not-equal operator.
Definition: mat2.h:248
clan::Mat2::multiply
static Mat2< Type > multiply(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Multiply 2 matrices.