mat3.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 "mat2.h"
35 #include "mat4.h"
36 #include "vec3.h"
37 #include "../System/cl_platform.h"
38 #include "angle.h"
39 
40 namespace clan
41 {
44 
45 template<typename Type>
46 class Mat2;
47 
48 template<typename Type>
49 class Mat3;
50 
51 template<typename Type>
52 class Mat4;
53 
54 class Angle;
55 
59 template<typename Type>
60 class Mat3
61 {
64 
65 public:
67  Mat3()
68  {
69  for (auto & elem : matrix)
70  elem = 0;
71  }
72 
74  Mat3(const Mat3<Type> &copy)
75  {
76  for (int i=0; i<9; i++)
77  matrix[i] = copy.matrix[i];
78  }
79 
81  explicit Mat3(const Mat2<Type> &copy);
82 
84  explicit Mat3(const Mat4<Type> &copy);
85 
87  explicit Mat3(const float *init_matrix)
88  {
89  for (int i=0; i<9; i++)
90  matrix[i] = (Type) init_matrix[i];
91  }
92 
94  explicit Mat3(Type m00, Type m01, Type m02, Type m10, Type m11, Type m12, Type m20, Type m21, Type m22)
95  {
96  matrix[0 * 3 + 0] = m00; matrix[0 * 3 + 1] = m01; matrix[0 * 3 + 2] = m02;
97  matrix[1 * 3 + 0] = m10; matrix[1 * 3 + 1] = m11; matrix[1 * 3 + 2] = m12;
98  matrix[2 * 3 + 0] = m20; matrix[2 * 3 + 1] = m21; matrix[2 * 3 + 2] = m22;
99  }
100 
102  explicit Mat3(const double *init_matrix)
103  {
104  for (int i=0; i<9; i++)
105  matrix[i] = (Type) init_matrix[i];
106  }
107 
109  explicit Mat3(const int64_t *init_matrix)
110  {
111  for (int i=0; i<9; i++)
112  matrix[i] = (Type) init_matrix[i];
113  }
114 
116  explicit Mat3(const int32_t *init_matrix)
117  {
118  for (int i=0; i<9; i++)
119  matrix[i] = (Type) init_matrix[i];
120  }
121 
123  explicit Mat3(const int16_t *init_matrix)
124  {
125  for (int i=0; i<9; i++)
126  matrix[i] = (Type) init_matrix[i];
127  }
128 
130  explicit Mat3(const int8_t *init_matrix)
131  {
132  for (int i=0; i<9; i++)
133  matrix[i] = (Type) init_matrix[i];
134  }
135 
136  static Mat3<Type> null();
137 
139 
149  static Mat3<Type> rotate(const Angle &angle, Type x, Type y, Type z, bool normalize = true);
150 
158  static Mat3<Type> rotate(const Angle &angle, Vec3<Type> rotation, bool normalize = true)
159  {
160  return rotate(angle, rotation.x, rotation.y, rotation.z, normalize);
161  }
162 
168  static Mat3<Type> rotate(const Angle &angle_x, const Angle &angle_y, const Angle &angle_z, EulerOrder order);
169 
175  static Mat3<Type> rotate(const Angle &angle);
176 
182  static Mat3<Type> scale(Type x, Type y);
183 
188  static Mat3<Type> scale(const Vec3<Type> &xy)
189  {
190  return scale(xy.x, xy.y);
191  }
192 
199  static Mat3<Type> translate(Type x, Type y);
200 
206  static Mat3<Type> translate(const Vec2<Type> &xy)
207  {
208  return translate(xy.x, xy.y);
209  }
210 
219  static Mat3<Type> multiply(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
220 
228  static Mat3<Type> add(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
229 
237  static Mat3<Type> subtract(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
238 
244 
251 
257 
263  static bool is_equal(const Mat3<Type> &first, const Mat3<Type> &second, Type epsilon)
264  {
265  for (int i=0; i<9; i++)
266  {
267  Type diff = second.matrix[i] - first.matrix[i];
268  if (diff < -epsilon || diff > epsilon) return false;
269  }
270  return true;
271  }
272 
276 
277 public:
278  Type matrix[9];
279 
283 
284 public:
286  double det() const;
287 
292 
297 
302 
307  bool is_equal(const Mat3<Type> &other, Type epsilon) const { return Mat3<Type>::is_equal(*this, other, epsilon); }
308 
312 
313 public:
315  operator Type const*() const { return matrix; }
316 
318  operator Type *() { return matrix; }
319 
321  Type &operator[](int i) { return matrix[i]; }
322 
324  const Type &operator[](int i) const { return matrix[i]; }
325 
327  Type &operator[](unsigned int i) { return matrix[i]; }
328 
330  const Type &operator[](unsigned int i) const { return matrix[i]; }
331 
333  Mat3<Type> &operator =(const Mat3<Type> &copy) {memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
334 
337 
340 
342  Mat3<Type> operator *(const Mat3<Type> &mult) const;
343 
345  Mat3<Type> operator +(const Mat3<Type> &add_matrix) const;
346 
348  Mat3<Type> operator -(const Mat3<Type> &sub_matrix) const;
349 
351  Vec2<Type> operator *(const Vec2<Type> &mult) const;
352 
354  bool operator==(const Mat3<Type> &other) const
355  {
356  for (int i=0; i<9; i++)
357  if (matrix[i] != other.matrix[i]) return false;
358  return true;
359  }
360 
362  bool operator!=(const Mat3<Type> &other) { return !((*this) == other); }
363 
367 
368 private:
370 };
371 
372 template<typename Type>
373 inline Mat3<Type> Mat3<Type>::multiply(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 * matrix_2; }
374 
375 template<typename Type>
376 inline Mat3<Type> Mat3<Type>::add(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 + matrix_2; }
377 
378 template<typename Type>
379 inline Mat3<Type> Mat3<Type>::subtract(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 - matrix_2; }
380 
381 template<typename Type>
382 inline Mat3<Type> Mat3<Type>::adjoint(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.adjoint(); return dest; }
383 
384 template<typename Type>
385 inline Mat3<Type> Mat3<Type>::inverse(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.inverse(); return dest; }
386 
387 template<typename Type>
388 inline Mat3<Type> Mat3<Type>::transpose(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.transpose(); return dest; }
389 
390 template<typename Type>
391 inline Mat3<Type> Mat3<Type>::null() { Mat3<Type> m; memset(m.matrix, 0, sizeof(m.matrix)); return m; }
392 
393 template<typename Type>
394 inline Mat3<Type> Mat3<Type>::identity() { Mat3<Type> m = null(); m.matrix[0] = 1; m.matrix[4] = 1; m.matrix[8] = 1; return m; }
395 
396 typedef Mat3<int> Mat3i;
399 
400 }
401 
Mat3< Type > & adjoint()
Creates the adjoint (or known as adjugate) of the matrix.
static Mat3< Type > subtract(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Subtract 2 matrices.
Definition: mat3.h:379
Mat3(const int64_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 64 bit integers)
Definition: mat3.h:109
Mat3< Type > operator-(const Mat3< Type > &sub_matrix) const
Subtraction operator.
Mat3< Type > operator+(const Mat3< Type > &add_matrix) const
Addition operator.
Type y
Definition: vec2.h:81
static Mat3< Type > inverse(const Mat3< Type > &matrix)
Calculate the matrix inverse of a matrix.
Definition: mat3.h:385
static Mat3< Type > null()
Definition: mat3.h:391
Mat3(const int8_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 8 bit integers)
Definition: mat3.h:130
static Mat3< Type > scale(Type x, Type y)
Create a 2d scale matrix.
static Mat3< Type > adjoint(const Mat3< Type > &matrix)
Calculate the adjoint (or known as Adjugate or Conjugate Transpose) of a matrix.
Definition: mat3.h:382
static Mat3< Type > rotate(const Angle &angle, Vec3< Type > rotation, bool normalize=true)
Create a 3d rotation matrix.
Definition: mat3.h:158
double det() const
Calculate the matrix determinant.
Mat3(const int16_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 16 bit integers)
Definition: mat3.h:123
Mat3(const Mat3< Type > &copy)
Constructs a 3x3 matrix (copied)
Definition: mat3.h:74
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:263
Type & operator[](int i)
Operator that returns the matrix cell at the given index.
Definition: mat3.h:321
Mat3< int > Mat3i
Definition: mat3.h:396
Type & operator[](unsigned int i)
Operator that returns the matrix cell at the given index.
Definition: mat3.h:327
static Mat3< Type > scale(const Vec3< Type > &xy)
Create a 2d scale matrix.
Definition: mat3.h:188
Type x
Definition: vec2.h:80
Mat3< double > Mat3d
Definition: mat3.h:398
2D matrix
Definition: mat2.h:45
Mat3< Type > & transpose()
Calculate the transpose of this matrix.
Mat3< Type > & operator=(const Mat3< Type > &copy)
Copy assignment operator.
Definition: mat3.h:333
Angle class.
Definition: angle.h:63
Mat3(const double *init_matrix)
Constructs a 3x3 matrix (copied from 9 doubles)
Definition: mat3.h:102
2D vector
Definition: line.h:48
@ angle
value is a color
EulerOrder
Euler angle rotation order.
Definition: angle.h:51
Mat3(const Mat4< Type > &copy)
Constructs a 3x3 matrix (copied from a 4d matrix)
bool is_equal(const Mat3< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: mat3.h:307
4D matrix
Definition: mat2.h:51
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:94
static Mat3< Type > identity()
Definition: mat3.h:394
const Type & operator[](unsigned int i) const
Operator that returns the matrix cell at the given index.
Definition: mat3.h:330
Mat3(const float *init_matrix)
Constructs a 3x3 matrix (copied from 9 floats)
Definition: mat3.h:87
Definition: clanapp.h:36
Mat3()
Constructs a 3x3 matrix (zero'ed)
Definition: mat3.h:67
static Mat3< Type > rotate(const Angle &angle)
Create a 2d rotation matrix.
Type matrix[9]
Definition: mat3.h:278
static Mat3< Type > multiply(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Multiply 2 matrices.
Definition: mat3.h:373
bool operator==(const Mat3< Type > &other) const
Equality operator.
Definition: mat3.h:354
static Mat3< Type > add(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Add 2 matrices.
Definition: mat3.h:376
static Mat3< Type > translate(Type x, Type y)
Create a 2d translation matrix.
static Mat3< Type > transpose(const Mat3< Type > &matrix)
Calculate the transpose of a matrix.
Definition: mat3.h:388
3D matrix
Definition: mat2.h:48
Mat3(const Mat2< Type > &copy)
Constructs a 3x3 matrix (copied from a 2d matrix)
const Type & operator[](int i) const
Operator that returns the matrix cell at the given index.
Definition: mat3.h:324
Mat3(const int32_t *init_matrix)
Constructs a 3x3 matrix (copied from 9, 32 bit integers)
Definition: mat3.h:116
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.
bool operator!=(const Mat3< Type > &other)
Not-equal operator.
Definition: mat3.h:362
3D vector
Definition: line_ray.h:48
Type x
Definition: vec3.h:80
Mat3< Type > operator*(const Mat3< Type > &mult) const
Multiplication operator.
Mat3< float > Mat3f
Definition: mat3.h:397
Type z
Definition: vec3.h:82
static Mat3< Type > rotate(const Angle &angle, Type x, Type y, Type z, bool normalize=true)
Create a 3d rotation matrix.
Mat3< Type > & inverse()
Create the matrix inverse. (Returns a zero matrix if the determinent = 0)
Type y
Definition: vec3.h:81
static Mat3< Type > translate(const Vec2< Type > &xy)
Create a 2d translation matrix.
Definition: mat3.h:206