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

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:
63 Mat3()
64 {
65 for (int i = 0; i < 9; i++)
66 matrix[i] = 0;
67 }
68
70 Mat3(const Mat3<Type> &copy) = default;
71
73 explicit Mat3(const Mat2<Type> &copy);
74
76 explicit Mat3(const Mat4<Type> &copy);
77
79 explicit Mat3(const float *init_matrix)
80 {
81 for (int i = 0; i < 9; i++)
82 matrix[i] = (Type)init_matrix[i];
83 }
84
86 explicit Mat3(Type m00, Type m01, Type m02, Type m10, Type m11, Type m12, Type m20, Type m21, Type m22)
87 {
88 matrix[0 * 3 + 0] = m00; matrix[0 * 3 + 1] = m01; matrix[0 * 3 + 2] = m02;
89 matrix[1 * 3 + 0] = m10; matrix[1 * 3 + 1] = m11; matrix[1 * 3 + 2] = m12;
90 matrix[2 * 3 + 0] = m20; matrix[2 * 3 + 1] = m21; matrix[2 * 3 + 2] = m22;
91 }
92
94 explicit Mat3(const double *init_matrix)
95 {
96 for (int i = 0; i < 9; i++)
97 matrix[i] = (Type)init_matrix[i];
98 }
99
101 explicit Mat3(const int64_t *init_matrix)
102 {
103 for (int i = 0; i < 9; i++)
104 matrix[i] = (Type)init_matrix[i];
105 }
106
108 explicit Mat3(const int32_t *init_matrix)
109 {
110 for (int i = 0; i < 9; i++)
111 matrix[i] = (Type)init_matrix[i];
112 }
113
115 explicit Mat3(const int16_t *init_matrix)
116 {
117 for (int i = 0; i < 9; i++)
118 matrix[i] = (Type)init_matrix[i];
119 }
120
122 explicit Mat3(const int8_t *init_matrix)
123 {
124 for (int i = 0; i < 9; i++)
125 matrix[i] = (Type)init_matrix[i];
126 }
127
128 static Mat3<Type> null();
129
130 static Mat3<Type> identity();
131
141 static Mat3<Type> rotate(const Angle &angle, Type x, Type y, Type z, bool normalize = true);
142
150 static Mat3<Type> rotate(const Angle &angle, Vec3<Type> rotation, bool normalize = true)
151 {
152 return rotate(angle, rotation.x, rotation.y, rotation.z, normalize);
153 }
154
160 static Mat3<Type> rotate(const Angle &angle_x, const Angle &angle_y, const Angle &angle_z, EulerOrder order);
161
167 static Mat3<Type> rotate(const Angle &angle);
168
174 static Mat3<Type> scale(Type x, Type y);
175
180 static Mat3<Type> scale(const Vec3<Type> &xy)
181 {
182 return scale(xy.x, xy.y);
183 }
184
191 static Mat3<Type> translate(Type x, Type y);
192
198 static Mat3<Type> translate(const Vec2<Type> &xy)
199 {
200 return translate(xy.x, xy.y);
201 }
202
211 static Mat3<Type> multiply(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
212
220 static Mat3<Type> add(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
221
229 static Mat3<Type> subtract(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
230
235 static Mat3<Type> adjoint(const Mat3<Type> &matrix);
236
242 static Mat3<Type> inverse(const Mat3<Type> &matrix);
243
248 static Mat3<Type> transpose(const Mat3<Type> &matrix);
249
255 static bool is_equal(const Mat3<Type> &first, const Mat3<Type> &second, Type epsilon)
256 {
257 for (int i = 0; i < 9; i++)
258 {
259 Type diff = second.matrix[i] - first.matrix[i];
260 if (diff < -epsilon || diff > epsilon) return false;
261 }
262 return true;
263 }
264
266 Type matrix[9];
267
269 double det() const;
270
274 Mat3<Type> &adjoint();
275
279 Mat3<Type> &inverse();
280
284 Mat3<Type> &transpose();
285
290 bool is_equal(const Mat3<Type> &other, Type epsilon) const { return Mat3<Type>::is_equal(*this, other, epsilon); }
291
293 operator Type const*() const { return matrix; }
294
296 operator Type *() { return matrix; }
297
299 Type &operator[](int i) { return matrix[i]; }
300
302 const Type &operator[](int i) const { return matrix[i]; }
303
305 Type &operator[](unsigned int i) { return matrix[i]; }
306
308 const Type &operator[](unsigned int i) const { return matrix[i]; }
309
311 Mat3<Type> &operator =(const Mat3<Type> &copy) = default;
312
314 Mat3<Type> &operator =(const Mat4<Type> &copy);
315
317 Mat3<Type> &operator =(const Mat2<Type> &copy);
318
320 Mat3<Type> operator *(const Mat3<Type> &mult) const;
321
323 Mat3<Type> operator +(const Mat3<Type> &add_matrix) const;
324
326 Mat3<Type> operator -(const Mat3<Type> &sub_matrix) const;
327
329 Vec2<Type> operator *(const Vec2<Type> &mult) const;
330
332 bool operator==(const Mat3<Type> &other) const
333 {
334 for (int i = 0; i < 9; i++)
335 if (matrix[i] != other.matrix[i]) return false;
336 return true;
337 }
338
340 bool operator!=(const Mat3<Type> &other) { return !((*this) == other); }
341 };
342
343 template<typename Type>
344 inline Mat3<Type> Mat3<Type>::multiply(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 * matrix_2; }
345
346 template<typename Type>
347 inline Mat3<Type> Mat3<Type>::add(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 + matrix_2; }
348
349 template<typename Type>
350 inline Mat3<Type> Mat3<Type>::subtract(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 - matrix_2; }
351
352 template<typename Type>
353 inline Mat3<Type> Mat3<Type>::adjoint(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.adjoint(); return dest; }
354
355 template<typename Type>
356 inline Mat3<Type> Mat3<Type>::inverse(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.inverse(); return dest; }
357
358 template<typename Type>
359 inline Mat3<Type> Mat3<Type>::transpose(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.transpose(); return dest; }
360
361 template<typename Type>
362 inline Mat3<Type> Mat3<Type>::null() { Mat3<Type> m; memset(m.matrix, 0, sizeof(m.matrix)); return m; }
363
364 template<typename Type>
365 inline Mat3<Type> Mat3<Type>::identity() { Mat3<Type> m = null(); m.matrix[0] = 1; m.matrix[4] = 1; m.matrix[8] = 1; return m; }
366
367 typedef Mat3<int> Mat3i;
368 typedef Mat3<float> Mat3f;
369 typedef Mat3<double> Mat3d;
370
372}
clan::Angle
Angle class.
Definition angle.h:60
clan::Mat2
2D matrix
Definition mat2.h:59
clan::Mat3
3D matrix
Definition mat3.h:60
clan::Mat3::rotate
static Mat3< Type > rotate(const Angle &angle, Vec3< Type > rotation, bool normalize=true)
Create a 3d rotation matrix.
Definition mat3.h:150
clan::Mat3::operator==
bool operator==(const Mat3< Type > &other) const
Equality operator.
Definition mat3.h:332
clan::Mat3::det
double det() const
Calculate the matrix determinant.
clan::Mat3::rotate
static Mat3< Type > rotate(const Angle &angle)
Create a 2d rotation matrix.
clan::Mat3::Mat3
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:86
clan::Mat3::transpose
Mat3< Type > & transpose()
Calculate the transpose of this matrix.
clan::Mat3::operator!=
bool operator!=(const Mat3< Type > &other)
Not-equal operator.
Definition mat3.h:340
clan::Mat3::scale
static Mat3< Type > scale(Type x, Type y)
Create a 2d scale matrix.
clan::Mat3::operator=
Mat3< Type > & operator=(const Mat3< Type > &copy)=default
Copy assignment operator.
clan::Mat3< int >::matrix
int matrix[9]
Definition mat3.h:266
clan::Mat3::operator[]
const Type & operator[](unsigned int i) const
Operator that returns the matrix cell at the given index.
Definition mat3.h:308
clan::Mat3::translate
static Mat3< Type > translate(const Vec2< Type > &xy)
Create a 2d translation matrix.
Definition mat3.h:198
clan::Mat3::operator[]
Type & operator[](unsigned int i)
Operator that returns the matrix cell at the given index.
Definition mat3.h:305
clan::Mat3::is_equal
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:255
clan::Mat3::operator-
Mat3< Type > operator-(const Mat3< Type > &sub_matrix) const
Subtraction operator.
clan::Mat3::Mat3
Mat3(const Mat4< Type > &copy)
Constructs a 3x3 matrix (copied from a 4d matrix)
clan::Mat3::operator*
Mat3< Type > operator*(const Mat3< Type > &mult) const
Multiplication operator.
clan::Mat3::operator+
Mat3< Type > operator+(const Mat3< Type > &add_matrix) const
Addition operator.
clan::Mat3::Mat3
Mat3(const float *init_matrix)
Constructs a 3x3 matrix (copied from 9 floats)
Definition mat3.h:79
clan::Mat3::Mat3
Mat3()
Constructs a 3x3 matrix (zero'ed)
Definition mat3.h:63
clan::Mat3::Mat3
Mat3(const int8_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 8 bit integers)
Definition mat3.h:122
clan::Mat3::operator[]
const Type & operator[](int i) const
Operator that returns the matrix cell at the given index.
Definition mat3.h:302
clan::Mat3::translate
static Mat3< Type > translate(Type x, Type y)
Create a 2d translation matrix.
clan::Mat3::rotate
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.
clan::Mat3::Mat3
Mat3(const int32_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 32 bit integers)
Definition mat3.h:108
clan::Mat3::is_equal
bool is_equal(const Mat3< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition mat3.h:290
clan::Mat3::inverse
Mat3< Type > & inverse()
Create the matrix inverse. (Returns a zero matrix if the determinent = 0)
clan::Mat3::adjoint
Mat3< Type > & adjoint()
Creates the adjoint (or known as adjugate) of the matrix.
clan::Mat3::Mat3
Mat3(const int64_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 64 bit integers)
Definition mat3.h:101
clan::Mat3::Mat3
Mat3(const int16_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 16 bit integers)
Definition mat3.h:115
clan::Mat3::scale
static Mat3< Type > scale(const Vec3< Type > &xy)
Create a 2d scale matrix.
Definition mat3.h:180
clan::Mat3::Mat3
Mat3(const double *init_matrix)
Constructs a 3x3 matrix (copied from 9 doubles)
Definition mat3.h:94
clan::Mat3::rotate
static Mat3< Type > rotate(const Angle &angle, Type x, Type y, Type z, bool normalize=true)
Create a 3d rotation matrix.
clan::Mat3::operator[]
Type & operator[](int i)
Operator that returns the matrix cell at the given index.
Definition mat3.h:299
clan::Mat3::Mat3
Mat3(const Mat3< Type > &copy)=default
Constructs a 3x3 matrix (copied)
clan::Mat3::Mat3
Mat3(const Mat2< Type > &copy)
Constructs a 3x3 matrix (copied from a 2d matrix)
clan::Mat4
4D matrix
Definition mat4.h:78
clan::Vec2
2D vector
Definition vec2.h:76
clan::Vec2::y
Type y
Definition vec2.h:81
clan::Vec2::x
Type x
Definition vec2.h:80
clan::Vec3
3D vector
Definition vec3.h:75
clan::Vec3::z
Type z
Definition vec3.h:81
clan::Vec3::y
Type y
Definition vec3.h:80
clan::Vec3::x
Type x
Definition vec3.h:79
clan::JsonType::null
@ null
Definition json_value.h:49
clan::Mat3d
Mat3< double > Mat3d
Definition mat3.h:369
clan::Mat3::transpose
static Mat3< Type > transpose(const Mat3< Type > &matrix)
Calculate the transpose of a matrix.
Definition mat3.h:359
clan::Mat3::multiply
static Mat3< Type > multiply(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Multiply 2 matrices.
Definition mat3.h:344
clan::Mat3f
Mat3< float > Mat3f
Definition mat3.h:368
clan::Mat3::adjoint
static Mat3< Type > adjoint(const Mat3< Type > &matrix)
Calculate the adjoint (or known as Adjugate or Conjugate Transpose) of a matrix.
Definition mat3.h:353
clan::Mat3::subtract
static Mat3< Type > subtract(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Subtract 2 matrices.
Definition mat3.h:350
clan::Mat3i
Mat3< int > Mat3i
Definition mat3.h:367
clan::Mat3::add
static Mat3< Type > add(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Add 2 matrices.
Definition mat3.h:347
clan::Mat3::null
static Mat3< Type > null()
Definition mat3.h:362
clan::EulerOrder
EulerOrder
Euler angle rotation order.
Definition angle.h:49
clan::Mat3::identity
static Mat3< Type > identity()
Definition mat3.h:365
clan::Mat3::inverse
static Mat3< Type > inverse(const Mat3< Type > &matrix)
Calculate the matrix inverse of a matrix.
Definition mat3.h:356
clan
Definition clanapp.h:36
clan::StyleValueType::angle
@ angle
value is a color
Definition style_value_type.h:44
clan::Key::y
@ y
Definition keys.h:105
clan::Key::m
@ m
Definition keys.h:93
clan::Key::i
@ i
Definition keys.h:89
clan::Key::x
@ x
Definition keys.h:104
clan::Key::z
@ z
Definition keys.h:106