mat2.h Source File - ClanLib SDK
Description: mat2.h Source File

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:
62 Mat2()
63 {
64 for (int i = 0; i < 4; i++)
65 matrix[i] = 0;
66 }
67
68 Mat2(const Mat2<Type> &copy) = default;
69
71 explicit Mat2(const Mat3<Type> &copy);
72
74 explicit Mat2(const Mat4<Type> &copy);
75
77 explicit Mat2(const float *init_matrix)
78 {
79 for (int i = 0; i < 4; i++)
80 matrix[i] = (Type)init_matrix[i];
81 }
82
84 explicit Mat2(Type m00, Type m01, Type m10, Type m11)
85 {
86 matrix[0 * 2 + 0] = m00; matrix[0 * 2 + 1] = m01;
87 matrix[1 * 2 + 0] = m10; matrix[1 * 2 + 1] = m11;
88 }
89
91 explicit Mat2(const double *init_matrix)
92 {
93 for (int i = 0; i < 4; i++)
94 matrix[i] = (Type)init_matrix[i];
95 }
96
98 explicit Mat2(const int64_t *init_matrix)
99 {
100 for (int i = 0; i < 4; i++)
101 matrix[i] = (Type)init_matrix[i];
102 }
103
105 explicit Mat2(const int32_t *init_matrix)
106 {
107 for (int i = 0; i < 4; i++)
108 matrix[i] = (Type)init_matrix[i];
109 }
110
112 explicit Mat2(const int16_t *init_matrix)
113 {
114 for (int i = 0; i < 4; i++)
115 matrix[i] = (Type)init_matrix[i];
116 }
117
119 explicit Mat2(const int8_t *init_matrix)
120 {
121 for (int i = 0; i < 4; i++)
122 matrix[i] = (Type)init_matrix[i];
123 }
124
125 static Mat2<Type> null();
126
127 static Mat2<Type> identity();
128
137 static Mat2<Type> multiply(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
138
146 static Mat2<Type> add(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
147
155 static Mat2<Type> subtract(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
156
162 static bool is_equal(const Mat2<Type> &first, const Mat2<Type> &second, Type epsilon)
163 {
164 for (int i = 0; i < 4; i++)
165 {
166 Type diff = second.matrix[i] - first.matrix[i];
167 if (diff < -epsilon || diff > epsilon) return false;
168 }
169 return true;
170 }
171
173 Type matrix[4];
174
179 bool is_equal(const Mat2<Type> &other, Type epsilon) const { return Mat2<Type>::is_equal(*this, other, epsilon); }
180
182 operator Type const*() const { return matrix; }
183
185 operator Type *() { return matrix; }
186
188 Type &operator[](int i) { return matrix[i]; }
189
191 const Type &operator[](int i) const { return matrix[i]; }
192
194 Type &operator[](unsigned int i) { return matrix[i]; }
195
197 const Type &operator[](unsigned int i) const { return matrix[i]; }
198
200 Mat2<Type> &operator =(const Mat2<Type> &copy) = default;
201
203 Mat2<Type> &operator =(const Mat4<Type> &copy);
204
206 Mat2<Type> &operator =(const Mat3<Type> &copy);
207
209 Mat2<Type> operator *(const Mat2<Type> &mult) const;
210
212 Mat2<Type> operator +(const Mat2<Type> &add_matrix) const;
213
215 Mat2<Type> operator -(const Mat2<Type> &subtract_matrix) const;
216
218 bool operator==(const Mat2<Type> &other) const
219 {
220 for (int i = 0; i < 4; i++)
221 if (matrix[i] != other.matrix[i]) return false;
222 return true;
223 }
224
226 bool operator!=(const Mat2<Type> &other) const { return !((*this) == other); }
227 };
228
229 typedef Mat2<int> Mat2i;
230 typedef Mat2<float> Mat2f;
231 typedef Mat2<double> Mat2d;
232
234}
clan::Angle
Angle class.
Definition angle.h:60
clan::Mat2
2D matrix
Definition mat2.h:59
clan::Mat2::Mat2
Mat2(const int64_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 64 bit integers)
Definition mat2.h:98
clan::Mat2::operator[]
Type & operator[](unsigned int i)
Operator that returns the matrix cell at the given index.
Definition mat2.h:194
clan::Mat2::Mat2
Mat2(const int8_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 8 bit integers)
Definition mat2.h:119
clan::Mat2::null
static Mat2< Type > null()
clan::Mat2::operator[]
const Type & operator[](int i) const
Operator that returns the matrix cell at the given index.
Definition mat2.h:191
clan::Mat2::operator=
Mat2< Type > & operator=(const Mat2< Type > &copy)=default
Copy assignment operator.
clan::Mat2::operator[]
Type & operator[](int i)
Operator that returns the matrix cell at the given index.
Definition mat2.h:188
clan::Mat2::Mat2
Mat2(const int32_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 32 bit integers)
Definition mat2.h:105
clan::Mat2::Mat2
Mat2(const Mat3< Type > &copy)
Constructs a 2x2 matrix (copied from a 3d matrix)
clan::Mat2::Mat2
Mat2(const Mat2< Type > &copy)=default
Constructs a 2x2 matrix (copied)
clan::Mat2::Mat2
Mat2(const double *init_matrix)
Constructs a 2x2 matrix (copied from 4 doubles)
Definition mat2.h:91
clan::Mat2::identity
static Mat2< Type > identity()
clan::Mat2::Mat2
Mat2(const float *init_matrix)
Constructs a 2x2 matrix (copied from 4 floats)
Definition mat2.h:77
clan::Mat2::add
static Mat2< Type > add(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Add 2 matrices.
clan::Mat2::Mat2
Mat2(const Mat4< Type > &copy)
Constructs a 2x2 matrix (copied from a 4d matrix)
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:162
clan::Mat2::Mat2
Mat2()
Constructs a 2x2 matrix (zero'ed)
Definition mat2.h:62
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:179
clan::Mat2::multiply
static Mat2< Type > multiply(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Multiply 2 matrices.
clan::Mat2< int >::matrix
int matrix[4]
Definition mat2.h:173
clan::Mat2::operator*
Mat2< Type > operator*(const Mat2< Type > &mult) const
Multiplication operator.
clan::Mat2::Mat2
Mat2(const int16_t *init_matrix)
Constructs a 2x2 matrix (copied from 4, 16 bit integers)
Definition mat2.h:112
clan::Mat2::operator+
Mat2< Type > operator+(const Mat2< Type > &add_matrix) const
Addition operator.
clan::Mat2::operator!=
bool operator!=(const Mat2< Type > &other) const
Not-equal operator.
Definition mat2.h:226
clan::Mat2::Mat2
Mat2(Type m00, Type m01, Type m10, Type m11)
Constructs a 2x2 matrix (copied from specified values)
Definition mat2.h:84
clan::Mat2::operator[]
const Type & operator[](unsigned int i) const
Operator that returns the matrix cell at the given index.
Definition mat2.h:197
clan::Mat2::subtract
static Mat2< Type > subtract(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Subtract 2 matrices.
clan::Mat2::operator==
bool operator==(const Mat2< Type > &other) const
Equality operator.
Definition mat2.h:218
clan::Mat2::operator-
Mat2< Type > operator-(const Mat2< Type > &subtract_matrix) const
Subtract operator.
clan::Mat3
3D matrix
Definition mat3.h:60
clan::Mat4
4D matrix
Definition mat4.h:78
clan::Mat2i
Mat2< int > Mat2i
Definition mat2.h:229
clan::Mat2f
Mat2< float > Mat2f
Definition mat2.h:230
clan::Mat2d
Mat2< double > Mat2d
Definition mat2.h:231
clan
Definition clanapp.h:36
clan::Key::i
@ i
Definition keys.h:89