mat3.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 "mat2.h"
34#include "mat4.h"
35#include "vec3.h"
36#include "../System/cl_platform.h"
37#include "angle.h"
38
39namespace 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 Mat3
60 {
61 public:
64 {
65 for (int i = 0; i < 9; i++)
66 matrix[i] = 0;
67 }
68
70 Mat3(const Mat3<Type> &copy)
71 {
72 for (int i = 0; i < 9; i++)
73 matrix[i] = copy.matrix[i];
74 }
75
77 explicit Mat3(const Mat2<Type> &copy);
78
80 explicit Mat3(const Mat4<Type> &copy);
81
83 explicit Mat3(const float *init_matrix)
84 {
85 for (int i = 0; i < 9; i++)
86 matrix[i] = (Type)init_matrix[i];
87 }
88
90 explicit Mat3(Type m00, Type m01, Type m02, Type m10, Type m11, Type m12, Type m20, Type m21, Type m22)
91 {
92 matrix[0 * 3 + 0] = m00; matrix[0 * 3 + 1] = m01; matrix[0 * 3 + 2] = m02;
93 matrix[1 * 3 + 0] = m10; matrix[1 * 3 + 1] = m11; matrix[1 * 3 + 2] = m12;
94 matrix[2 * 3 + 0] = m20; matrix[2 * 3 + 1] = m21; matrix[2 * 3 + 2] = m22;
95 }
96
98 explicit Mat3(const double *init_matrix)
99 {
100 for (int i = 0; i < 9; i++)
101 matrix[i] = (Type)init_matrix[i];
102 }
103
105 explicit Mat3(const int64_t *init_matrix)
106 {
107 for (int i = 0; i < 9; i++)
108 matrix[i] = (Type)init_matrix[i];
109 }
110
112 explicit Mat3(const int32_t *init_matrix)
113 {
114 for (int i = 0; i < 9; i++)
115 matrix[i] = (Type)init_matrix[i];
116 }
117
119 explicit Mat3(const int16_t *init_matrix)
120 {
121 for (int i = 0; i < 9; i++)
122 matrix[i] = (Type)init_matrix[i];
123 }
124
126 explicit Mat3(const int8_t *init_matrix)
127 {
128 for (int i = 0; i < 9; i++)
129 matrix[i] = (Type)init_matrix[i];
130 }
131
132 static Mat3<Type> null();
133
135
145 static Mat3<Type> rotate(const Angle &angle, Type x, Type y, Type z, bool normalize = true);
146
154 static Mat3<Type> rotate(const Angle &angle, Vec3<Type> rotation, bool normalize = true)
155 {
156 return rotate(angle, rotation.x, rotation.y, rotation.z, normalize);
157 }
158
164 static Mat3<Type> rotate(const Angle &angle_x, const Angle &angle_y, const Angle &angle_z, EulerOrder order);
165
171 static Mat3<Type> rotate(const Angle &angle);
172
178 static Mat3<Type> scale(Type x, Type y);
179
184 static Mat3<Type> scale(const Vec3<Type> &xy)
185 {
186 return scale(xy.x, xy.y);
187 }
188
195 static Mat3<Type> translate(Type x, Type y);
196
203 {
204 return translate(xy.x, xy.y);
205 }
206
215 static Mat3<Type> multiply(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
216
224 static Mat3<Type> add(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
225
233 static Mat3<Type> subtract(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
234
240
247
253
259 static bool is_equal(const Mat3<Type> &first, const Mat3<Type> &second, Type epsilon)
260 {
261 for (int i = 0; i < 9; i++)
262 {
263 Type diff = second.matrix[i] - first.matrix[i];
264 if (diff < -epsilon || diff > epsilon) return false;
265 }
266 return true;
267 }
268
270 Type matrix[9];
271
273 double det() const;
274
279
284
289
294 bool is_equal(const Mat3<Type> &other, Type epsilon) const { return Mat3<Type>::is_equal(*this, other, epsilon); }
295
297 operator Type const*() const { return matrix; }
298
300 operator Type *() { return matrix; }
301
303 Type &operator[](int i) { return matrix[i]; }
304
306 const Type &operator[](int i) const { return matrix[i]; }
307
309 Type &operator[](unsigned int i) { return matrix[i]; }
310
312 const Type &operator[](unsigned int i) const { return matrix[i]; }
313
315 Mat3<Type> &operator =(const Mat3<Type> &copy) { memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
316
319
322
324 Mat3<Type> operator *(const Mat3<Type> &mult) const;
325
327 Mat3<Type> operator +(const Mat3<Type> &add_matrix) const;
328
330 Mat3<Type> operator -(const Mat3<Type> &sub_matrix) const;
331
333 Vec2<Type> operator *(const Vec2<Type> &mult) const;
334
336 bool operator==(const Mat3<Type> &other) const
337 {
338 for (int i = 0; i < 9; i++)
339 if (matrix[i] != other.matrix[i]) return false;
340 return true;
341 }
342
344 bool operator!=(const Mat3<Type> &other) { return !((*this) == other); }
345 };
346
347 template<typename Type>
348 inline Mat3<Type> Mat3<Type>::multiply(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 * matrix_2; }
349
350 template<typename Type>
351 inline Mat3<Type> Mat3<Type>::add(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 + matrix_2; }
352
353 template<typename Type>
354 inline Mat3<Type> Mat3<Type>::subtract(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 - matrix_2; }
355
356 template<typename Type>
357 inline Mat3<Type> Mat3<Type>::adjoint(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.adjoint(); return dest; }
358
359 template<typename Type>
360 inline Mat3<Type> Mat3<Type>::inverse(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.inverse(); return dest; }
361
362 template<typename Type>
363 inline Mat3<Type> Mat3<Type>::transpose(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.transpose(); return dest; }
364
365 template<typename Type>
366 inline Mat3<Type> Mat3<Type>::null() { Mat3<Type> m; memset(m.matrix, 0, sizeof(m.matrix)); return m; }
367
368 template<typename Type>
369 inline Mat3<Type> Mat3<Type>::identity() { Mat3<Type> m = null(); m.matrix[0] = 1; m.matrix[4] = 1; m.matrix[8] = 1; return m; }
370
374
376}
Angle class.
Definition angle.h:60
2D matrix
Definition vec4.h:52
3D matrix
Definition vec4.h:55
static Mat3< Type > rotate(const Angle &angle, Vec3< Type > rotation, bool normalize=true)
Create a 3d rotation matrix.
Definition mat3.h:154
bool operator==(const Mat3< Type > &other) const
Equality operator.
Definition mat3.h:336
double det() const
Calculate the matrix determinant.
static Mat3< Type > rotate(const Angle &angle)
Create a 2d rotation matrix.
Mat3(Type m00, Type m01, Type m02, Type m10, Type m11, Type m12, Type m20, Type m21, Type m22)
Constructs a 3x3 matrix (copied from specified values)
Definition mat3.h:90
Mat3< Type > & transpose()
Calculate the transpose of this matrix.
bool operator!=(const Mat3< Type > &other)
Not-equal operator.
Definition mat3.h:344
static Mat3< Type > scale(Type x, Type y)
Create a 2d scale matrix.
Type matrix[9]
The matrix (in column-major format)
Definition mat3.h:270
const Type & operator[](unsigned int i) const
Operator that returns the matrix cell at the given index.
Definition mat3.h:312
static Mat3< Type > translate(const Vec2< Type > &xy)
Create a 2d translation matrix.
Definition mat3.h:202
Type & operator[](unsigned int i)
Operator that returns the matrix cell at the given index.
Definition mat3.h:309
static bool is_equal(const Mat3< Type > &first, const Mat3< Type > &second, Type epsilon)
Returns true if equal within the bounds of an epsilon.
Definition mat3.h:259
Mat3< Type > operator-(const Mat3< Type > &sub_matrix) const
Subtraction operator.
Mat3(const Mat4< Type > &copy)
Constructs a 3x3 matrix (copied from a 4d matrix)
Mat3< Type > operator*(const Mat3< Type > &mult) const
Multiplication operator.
Mat3< Type > operator+(const Mat3< Type > &add_matrix) const
Addition operator.
Mat3< Type > & operator=(const Mat3< Type > &copy)
Copy assignment operator.
Definition mat3.h:315
Mat3(const float *init_matrix)
Constructs a 3x3 matrix (copied from 9 floats)
Definition mat3.h:83
Mat3()
Constructs a 3x3 matrix (zero'ed)
Definition mat3.h:63
Mat3(const int8_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 8 bit integers)
Definition mat3.h:126
const Type & operator[](int i) const
Operator that returns the matrix cell at the given index.
Definition mat3.h:306
static Mat3< Type > translate(Type x, Type y)
Create a 2d translation matrix.
Mat3(const Mat3< Type > &copy)
Constructs a 3x3 matrix (copied)
Definition mat3.h:70
static Mat3< Type > rotate(const Angle &angle_x, const Angle &angle_y, const Angle &angle_z, EulerOrder order)
Create a 3d rotation matrix using euler angles.
Mat3(const int32_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 32 bit integers)
Definition mat3.h:112
bool is_equal(const Mat3< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition mat3.h:294
Mat3< Type > & inverse()
Create the matrix inverse. (Returns a zero matrix if the determinent = 0)
Mat3< Type > & adjoint()
Creates the adjoint (or known as adjugate) of the matrix.
Mat3(const int64_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 64 bit integers)
Definition mat3.h:105
Mat3(const int16_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 16 bit integers)
Definition mat3.h:119
static Mat3< Type > scale(const Vec3< Type > &xy)
Create a 2d scale matrix.
Definition mat3.h:184
Mat3(const double *init_matrix)
Constructs a 3x3 matrix (copied from 9 doubles)
Definition mat3.h:98
static Mat3< Type > rotate(const Angle &angle, Type x, Type y, Type z, bool normalize=true)
Create a 3d rotation matrix.
Type & operator[](int i)
Operator that returns the matrix cell at the given index.
Definition mat3.h:303
Mat3(const Mat2< Type > &copy)
Constructs a 3x3 matrix (copied from a 2d matrix)
4D matrix
Definition vec4.h:58
2D vector
Definition vec4.h:43
Type y
Definition vec2.h:81
Type x
Definition vec2.h:80
3D vector
Definition vec4.h:46
Type z
Definition vec3.h:81
Type y
Definition vec3.h:80
Type x
Definition vec3.h:79
Mat3< double > Mat3d
Definition mat3.h:373
static Mat3< Type > transpose(const Mat3< Type > &matrix)
Calculate the transpose of a matrix.
Definition mat3.h:363
static Mat3< Type > multiply(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Multiply 2 matrices.
Definition mat3.h:348
Mat3< float > Mat3f
Definition mat3.h:372
static Mat3< Type > adjoint(const Mat3< Type > &matrix)
Calculate the adjoint (or known as Adjugate or Conjugate Transpose) of a matrix.
Definition mat3.h:357
static Mat3< Type > subtract(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Subtract 2 matrices.
Definition mat3.h:354
Mat3< int > Mat3i
Definition mat3.h:371
static Mat3< Type > add(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Add 2 matrices.
Definition mat3.h:351
static Mat3< Type > null()
Definition mat3.h:366
EulerOrder
Euler angle rotation order.
Definition angle.h:49
static Mat3< Type > identity()
Definition mat3.h:369
static Mat3< Type > inverse(const Mat3< Type > &matrix)
Calculate the matrix inverse of a matrix.
Definition mat3.h:360
Definition clanapp.h:36
@ angle
value is a color