ClanLib  2.3.7
mat3.h
Go to the documentation of this file.
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2011 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 
33 
34 #pragma once
35 
36 #include "../api_core.h"
37 #include "mat2.h"
38 #include "mat4.h"
39 #include "../System/cl_platform.h"
40 
41 template<typename Type>
42 class CL_Mat2;
43 
44 template<typename Type>
45 class CL_Mat3;
46 
47 template<typename Type>
48 class CL_Mat4;
49 
50 class CL_Angle;
51 
56 template<typename Type>
57 class CL_Mat3
58 {
61 
62 public:
64  CL_Mat3() { }
65 
67  CL_Mat3(const CL_Mat3<Type> &copy)
68  {
69  for (int i=0; i<9; i++)
70  matrix[i] = copy.matrix[i];
71  }
72 
74  CL_Mat3(const CL_Mat2<Type> &copy);
75 
77  CL_Mat3(const CL_Mat4<Type> &copy);
78 
80  CL_Mat3(const float *init_matrix)
81  {
82  for (int i=0; i<9; i++)
83  matrix[i] = (Type) init_matrix[i];
84  }
85 
87  CL_Mat3(Type m00, Type m01, Type m02, Type m10, Type m11, Type m12, Type m20, Type m21, Type m22)
88  {
89  matrix[0 * 3 + 0] = m00; matrix[0 * 3 + 1] = m01; matrix[0 * 3 + 2] = m02;
90  matrix[1 * 3 + 0] = m10; matrix[1 * 3 + 1] = m11; matrix[1 * 3 + 2] = m12;
91  matrix[2 * 3 + 0] = m20; matrix[2 * 3 + 1] = m21; matrix[2 * 3 + 2] = m22;
92  }
93 
95  CL_Mat3(const double *init_matrix)
96  {
97  for (int i=0; i<9; i++)
98  matrix[i] = (Type) init_matrix[i];
99  }
100 
102  CL_Mat3(const cl_byte64 *init_matrix)
103  {
104  for (int i=0; i<9; i++)
105  matrix[i] = (Type) init_matrix[i];
106  }
107 
109  CL_Mat3(const cl_byte32 *init_matrix)
110  {
111  for (int i=0; i<9; i++)
112  matrix[i] = (Type) init_matrix[i];
113  }
114 
116  CL_Mat3(const cl_byte16 *init_matrix)
117  {
118  for (int i=0; i<9; i++)
119  matrix[i] = (Type) init_matrix[i];
120  }
121 
123  CL_Mat3(const cl_byte8 *init_matrix)
124  {
125  for (int i=0; i<9; i++)
126  matrix[i] = (Type) init_matrix[i];
127  }
128 
129  static CL_Mat3<Type> null();
130 
131  static CL_Mat3<Type> identity();
132 
142  static CL_Mat3<Type> rotate(const CL_Angle &angle, Type x, Type y, Type z, bool normalize = true);
143 
151  static CL_Mat3<Type> multiply(const CL_Mat3<Type> &matrix_1, const CL_Mat3<Type> &matrix_2);
152 
160  static CL_Mat3<Type> add(const CL_Mat3<Type> &matrix_1, const CL_Mat3<Type> &matrix_2);
161 
169  static CL_Mat3<Type> subtract(const CL_Mat3<Type> &matrix_1, const CL_Mat3<Type> &matrix_2);
170 
175  static CL_Mat3<Type> adjoint(const CL_Mat3<Type> &matrix);
176 
182  static CL_Mat3<Type> inverse(const CL_Mat3<Type> &matrix);
183 
189 
193 
194 public:
195  Type matrix[9];
196 
200 
201 public:
209  CL_Mat3<Type> &multiply(const CL_Mat3<Type> &mult);
210 
218  CL_Mat3<Type> &add(const CL_Mat3<Type> &add_matrix);
219 
227  CL_Mat3<Type> &subtract(const CL_Mat3<Type> &sub_matrix);
228 
230  double det() const;
231 
236 
241 
246 
250 
251 public:
253  operator Type const*() const { return matrix; }
254 
256  operator Type *() { return matrix; }
257 
259  Type &operator[](int i) { return matrix[i]; }
260 
262  const Type &operator[](int i) const { return matrix[i]; }
263 
265  Type &operator[](unsigned int i) { return matrix[i]; }
266 
268  const Type &operator[](unsigned int i) const { return matrix[i]; }
269 
271  CL_Mat3<Type> &operator =(const CL_Mat3<Type> &copy) {memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
272 
274  CL_Mat3<Type> &operator =(const CL_Mat4<Type> &copy);
275 
277  CL_Mat3<Type> &operator =(const CL_Mat2<Type> &copy);
278 
280  CL_Mat3<Type> operator *(const CL_Mat4<Type> &mult) const { CL_Mat3<Type> result = mult; result.multiply(*this); return result; }
281 
283  CL_Mat3<Type> operator +(const CL_Mat4<Type> &add_matrix) const { CL_Mat3<Type> result = add_matrix; result.add(*this); return result; }
284 
286  CL_Mat3<Type> operator -(const CL_Mat4<Type> &sub_matrix) const { CL_Mat3<Type> result = sub_matrix; result.subtract(*this); return result; }
287 
289  bool operator==(const CL_Mat3<Type> &other)
290  {
291  for (int i=0; i<9; i++)
292  if (matrix[i] != other.matrix[i]) return false;
293  return true;
294  }
295 
297  bool operator!=(const CL_Mat3<Type> &other) { return !((*this) == other); }
298 
302 
303 private:
305 };
306 
310