mat2.h
1/*
2** ClanLib SDK
3** Copyright (c) 1997-2020 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#pragma once
32
33#include "../System/cl_platform.h"
34#include "mat3.h"
35#include "mat4.h"
36#include "vec2.h"
37
38namespace clan
39{
42
43 template<typename Type>
44 class Mat2;
45
46 template<typename Type>
47 class Mat3;
48
49 template<typename Type>
50 class Mat4;
51
52 class Angle;
53
57 template<typename Type>
58 class Mat2
59 {
60 public:
63 {
64 for (int i = 0; i < 4; i++)
65 matrix[i] = 0;
66 }
68 Mat2(const Mat2<Type> &copy)
69 {
70 for (int i = 0; i < 4; i++)
71 matrix[i] = copy.matrix[i];
72 }
73
75 explicit Mat2(const Mat3<Type> &copy);
76
78 explicit Mat2(const Mat4<Type> &copy);
79
81 explicit Mat2(const float *init_matrix)
82 {
83 for (int i = 0; i < 4; i++)
84 matrix[i] = (Type)init_matrix[i];
85 }
86
88 explicit Mat2(Type m00, Type m01, Type m10, Type m11)
89 {
90 matrix[0 * 2 + 0] = m00; matrix[0 * 2 + 1] = m01;
91 matrix[1 * 2 + 0] = m10; matrix[1 * 2 + 1] = m11;
92 }
93
95 explicit Mat2(const double *init_matrix)
96 {
97 for (int i = 0; i < 4; i++)
98 matrix[i] = (Type)init_matrix[i];
99 }
100
102 explicit Mat2(const int64_t *init_matrix)
103 {
104 for (int i = 0; i < 4; i++)
105 matrix[i] = (Type)init_matrix[i];
106 }
107
109 explicit Mat2(const int32_t *init_matrix)
110 {
111 for (int i = 0; i < 4; i++)
112 matrix[i] = (Type)init_matrix[i];
113 }
114
116 explicit Mat2(const int16_t *init_matrix)
117 {
118 for (int i = 0; i < 4; i++)
119 matrix[i] = (Type)init_matrix[i];
120 }
121
123 explicit Mat2(const int8_t *init_matrix)
124 {
125 for (int i = 0; i < 4; i++)
126 matrix[i] = (Type)init_matrix[i];
127 }
128
129 static Mat2<Type> null();
130
132
141 static Mat2<Type> multiply(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
142
150 static Mat2<Type> add(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
151
159 static Mat2<Type> subtract(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
160
166 static bool is_equal(const Mat2<Type> &first, const Mat2<Type> &second, Type epsilon)
167 {
168 for (int i = 0; i < 4; i++)
169 {
170 Type diff = second.matrix[i] - first.matrix[i];
171 if (diff < -epsilon || diff > epsilon) return false;
172 }
173 return true;
174 }
175
177 Type matrix[4];
178
183 bool is_equal(const Mat2<Type> &other, Type epsilon) const { return Mat2<Type>::is_equal(*this, other, epsilon); }
184
186 operator Type const*() const { return matrix; }
187
189 operator Type *() { return matrix; }
190
192 Type &operator[](int i) { return matrix[i]; }
193
195 const Type &operator[](int i) const { return matrix[i]; }
196
198 Type &operator[](unsigned int i) { return matrix[i]; }
199
201 const Type &operator[](unsigned int i) const { return matrix[i]; }
202
204 Mat2<Type> &operator =(const Mat2<Type> &copy) { memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
205
208
211
213 Mat2<Type> operator *(const Mat2<Type> &mult) const;
214
216 Mat2<Type> operator +(const Mat2<Type> &add_matrix) const;
217
219 Mat2<Type> operator -(const Mat2<Type> &subtract_matrix) const;
220
222 bool operator==(const Mat2<Type> &other) const
223 {
224 for (int i = 0; i < 4; i++)
225 if (matrix[i] != other.matrix[i]) return false;
226 return true;
227 }
228
230 bool operator!=(const Mat2<Type> &other) const { return !((*this) == other); }
231 };
232
236
238}
2D matrix
Definition vec4.h:52
Mat2(const int64_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 64 bit integers)
Definition mat2.h:102
Type & operator[](unsigned int i)
Operator that returns the matrix cell at the given index.
Definition mat2.h:198
Mat2(const int8_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 8 bit integers)
Definition mat2.h:123
static Mat2< Type > null()
const Type & operator[](int i) const
Operator that returns the matrix cell at the given index.
Definition mat2.h:195
Type & operator[](int i)
Operator that returns the matrix cell at the given index.
Definition mat2.h:192
Mat2(const int32_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 32 bit integers)
Definition mat2.h:109
Mat2(const Mat3< Type > &copy)
Constructs a 2x2 matrix (copied from a 3d matrix)
Mat2< Type > & operator=(const Mat2< Type > &copy)
Copy assignment operator.
Definition mat2.h:204
Mat2(const double *init_matrix)
Constructs a 2x2 matrix (copied from 4 doubles)
Definition mat2.h:95
static Mat2< Type > identity()
Mat2(const float *init_matrix)
Constructs a 2x2 matrix (copied from 4 floats)
Definition mat2.h:81
static Mat2< Type > add(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Add 2 matrices.
Mat2(const Mat4< Type > &copy)
Constructs a 2x2 matrix (copied from a 4d matrix)
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:166
Mat2()
Constructs a 2x2 matrix (zero'ed)
Definition mat2.h:62
bool is_equal(const Mat2< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition mat2.h:183
Mat2(const Mat2< Type > &copy)
Constructs a 2x2 matrix (copied)
Definition mat2.h:68
static Mat2< Type > multiply(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Multiply 2 matrices.
Type matrix[4]
The matrix (in column-major format)
Definition mat2.h:177
Mat2< Type > operator*(const Mat2< Type > &mult) const
Multiplication operator.
Mat2(const int16_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 16 bit integers)
Definition mat2.h:116
Mat2< Type > operator+(const Mat2< Type > &add_matrix) const
Addition operator.
bool operator!=(const Mat2< Type > &other) const
Not-equal operator.
Definition mat2.h:230
Mat2(Type m00, Type m01, Type m10, Type m11)
Constructs a 2x2 matrix (copied from specified values)
Definition mat2.h:88
const Type & operator[](unsigned int i) const
Operator that returns the matrix cell at the given index.
Definition mat2.h:201
static Mat2< Type > subtract(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Subtract 2 matrices.
bool operator==(const Mat2< Type > &other) const
Equality operator.
Definition mat2.h:222
Mat2< Type > operator-(const Mat2< Type > &subtract_matrix) const
Subtract operator.
3D matrix
Definition vec4.h:55
4D matrix
Definition vec4.h:58
Mat2< int > Mat2i
Definition mat2.h:233
Mat2< float > Mat2f
Definition mat2.h:234
Mat2< double > Mat2d
Definition mat2.h:235
Definition clanapp.h:36