mat3.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2013 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 "../api_core.h"
35 #include "mat2.h"
36 #include "mat4.h"
37 #include "vec3.h"
38 #include "../System/cl_platform.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 
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 byte64 *init_matrix)
106  {
107  for (int i=0; i<9; i++)
108  matrix[i] = (Type) init_matrix[i];
109  }
110 
112  explicit Mat3(const byte32 *init_matrix)
113  {
114  for (int i=0; i<9; i++)
115  matrix[i] = (Type) init_matrix[i];
116  }
117 
119  explicit Mat3(const byte16 *init_matrix)
120  {
121  for (int i=0; i<9; i++)
122  matrix[i] = (Type) init_matrix[i];
123  }
124 
126  explicit Mat3(const byte8 *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 
134  static Mat3<Type> identity();
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 
167  static Mat3<Type> multiply(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
168 
176  static Mat3<Type> add(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
177 
185  static Mat3<Type> subtract(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2);
186 
191  static Mat3<Type> adjoint(const Mat3<Type> &matrix);
192 
198  static Mat3<Type> inverse(const Mat3<Type> &matrix);
199 
204  static Mat3<Type> transpose(const Mat3<Type> &matrix);
205 
211  static bool is_equal(const Mat3<Type> &first, const Mat3<Type> &second, Type epsilon)
212  {
213  for (int i=0; i<9; i++)
214  {
215  Type diff = second.matrix[i] - first.matrix[i];
216  if (diff < -epsilon || diff > epsilon) return false;
217  }
218  return true;
219  }
220 
224 
225 public:
226  Type matrix[9];
227 
231 
232 public:
234  double det() const;
235 
239  Mat3<Type> &adjoint();
240 
244  Mat3<Type> &inverse();
245 
250 
255  bool is_equal(const Mat3<Type> &other, Type epsilon) const { return Mat3<Type>::is_equal(*this, other, epsilon); }
256 
260 
261 public:
263  operator Type const*() const { return matrix; }
264 
266  operator Type *() { return matrix; }
267 
269  Type &operator[](int i) { return matrix[i]; }
270 
272  const Type &operator[](int i) const { return matrix[i]; }
273 
275  Type &operator[](unsigned int i) { return matrix[i]; }
276 
278  const Type &operator[](unsigned int i) const { return matrix[i]; }
279 
281  Mat3<Type> &operator =(const Mat3<Type> &copy) {memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
282 
284  Mat3<Type> &operator =(const Mat4<Type> &copy);
285 
287  Mat3<Type> &operator =(const Mat2<Type> &copy);
288 
290  Mat3<Type> operator *(const Mat3<Type> &mult) const;
291 
293  Mat3<Type> operator +(const Mat3<Type> &add_matrix) const;
294 
296  Mat3<Type> operator -(const Mat3<Type> &sub_matrix) const;
297 
299  bool operator==(const Mat3<Type> &other) const
300  {
301  for (int i=0; i<9; i++)
302  if (matrix[i] != other.matrix[i]) return false;
303  return true;
304  }
305 
307  bool operator!=(const Mat3<Type> &other) { return !((*this) == other); }
308 
312 
313 private:
315 };
316 
317 
318 
319 template<typename Type>
320 inline Mat3<Type> Mat3<Type>::multiply(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 * matrix_2; }
321 
322 template<typename Type>
323 inline Mat3<Type> Mat3<Type>::add(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 + matrix_2; }
324 
325 template<typename Type>
326 inline Mat3<Type> Mat3<Type>::subtract(const Mat3<Type> &matrix_1, const Mat3<Type> &matrix_2) { return matrix_1 - matrix_2; }
327 
328 template<typename Type>
329 inline Mat3<Type> Mat3<Type>::adjoint(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.adjoint(); return dest; }
330 
331 template<typename Type>
332 inline Mat3<Type> Mat3<Type>::inverse(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.inverse(); return dest; }
333 
334 template<typename Type>
335 inline Mat3<Type> Mat3<Type>::transpose(const Mat3<Type> &matrix) { Mat3<Type> dest(matrix); dest.transpose(); return dest; }
336 
337 template<typename Type>
338 inline Mat3<Type> Mat3<Type>::null() { Mat3<Type> m; memset(m.matrix, 0, sizeof(m.matrix)); return m; }
339 
340 template<typename Type>
341 inline Mat3<Type> Mat3<Type>::identity() { Mat3<Type> m = null(); m.matrix[0] = 1; m.matrix[4] = 1; m.matrix[8] = 1; return m; }
342 
343 typedef Mat3<int> Mat3i;
346 
347 }
348 
static Mat3< Type > identity()
Definition: mat3.h:341
Mat3< double > Mat3d
Definition: mat3.h:345
Type x
Definition: vec3.h:81
Mat3< float > Mat3f
Definition: mat3.h:344
static Mat3< Type > rotate(const Angle &angle, Vec3< Type > rotation, bool normalize=true)
Create a rotation matrix.
Definition: mat3.h:154
static Mat3< Type > rotate(const Angle &angle, Type x, Type y, Type z, bool normalize=true)
Create a rotation matrix.
Angle class.
Definition: angle.h:63
Mat3< int > Mat3i
Definition: mat3.h:343
Mat3< Type > & operator=(const Mat3< Type > &copy)
Copy assignment operator.
Definition: mat3.h:281
Type y
Definition: vec3.h:82
static Mat3< Type > null()
Definition: mat3.h:338
static Mat3< Type > subtract(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Subtract 2 matrices.
Definition: mat3.h:326
char byte8
Definition: cl_platform.h:59
bool operator!=(const Mat3< Type > &other)
Not-equal operator.
Definition: mat3.h:307
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:211
Mat3(const byte16 *init_matrix)
Constructs a 3x3 matrix (copied from 9, 16 bit integers)
Definition: mat3.h:119
bool operator==(const Mat3< Type > &other) const
Equality operator.
Definition: mat3.h:299
Type & operator[](int i)
Operator that returns the matrix cell at the given index.
Definition: mat3.h:269
Mat3< Type > operator*(const Mat3< Type > &mult) const
Multiplication operator.
Mat3(const byte64 *init_matrix)
Constructs a 3x3 matrix (copied from 9, 64 bit integers)
Definition: mat3.h:105
const Type & operator[](unsigned int i) const
Operator that returns the matrix cell at the given index.
Definition: mat3.h:278
Mat3< Type > & transpose()
Calculate the transpose of this matrix.
bool is_equal(const Mat3< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: mat3.h:255
Mat3< Type > operator-(const Mat3< Type > &sub_matrix) const
Subtraction operator.
2D matrix
Definition: mat2.h:46
Mat3(const byte8 *init_matrix)
Constructs a 3x3 matrix (copied from 9, 8 bit integers)
Definition: mat3.h:126
Type & operator[](unsigned int i)
Operator that returns the matrix cell at the given index.
Definition: mat3.h:275
3D matrix
Definition: mat2.h:49
Mat3(const Mat3< Type > &copy)
Constructs a 3x3 matrix (copied)
Definition: mat3.h:70
int byte32
Definition: cl_platform.h:63
long long byte64
Definition: cl_platform.h:65
Mat3()
Constructs a 3x3 matrix (uninitialised)
Definition: mat3.h:67
3D vector
Definition: line_ray.h:49
Mat3< Type > operator+(const Mat3< Type > &add_matrix) const
Addition operator.
const Type & operator[](int i) const
Operator that returns the matrix cell at the given index.
Definition: mat3.h:272
static Mat3< Type > add(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Add 2 matrices.
Definition: mat3.h:323
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
double det() const
Calculate the matrix determinant.
Mat3(const byte32 *init_matrix)
Constructs a 3x3 matrix (copied from 9, 32 bit integers)
Definition: mat3.h:112
4D matrix
Definition: mat2.h:52
Mat3(const double *init_matrix)
Constructs a 3x3 matrix (copied from 9 doubles)
Definition: mat3.h:98
Mat3< Type > & adjoint()
Creates the adjoint (or known as adjugate) of the matrix.
static Mat3< Type > multiply(const Mat3< Type > &matrix_1, const Mat3< Type > &matrix_2)
Multiply 2 matrices.
Definition: mat3.h:320
Mat3< Type > & inverse()
Create the matrix inverse. (Returns a zero matrix if the determinent = 0)
Type matrix[9]
Definition: mat3.h:226
Mat3(const float *init_matrix)
Constructs a 3x3 matrix (copied from 9 floats)
Definition: mat3.h:83
Type z
Definition: vec3.h:83
short byte16
Definition: cl_platform.h:61