ClanLib  2.3.7
mat2.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 "../System/cl_platform.h"
38 #include "mat3.h"
39 #include "mat4.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_Mat2
58 {
61 
62 public:
64  CL_Mat2() { }
65 
67  CL_Mat2(const CL_Mat2<Type> &copy)
68  {
69  for (int i=0; i<4; i++)
70  matrix[i] = copy.matrix[i];
71  }
72 
74  CL_Mat2(const CL_Mat3<Type> &copy);
75 
77  CL_Mat2(const CL_Mat4<Type> &copy);
78 
80  CL_Mat2(const float *init_matrix)
81  {
82  for (int i=0; i<4; i++)
83  matrix[i] = (Type) init_matrix[i];
84  }
85 
87  CL_Mat2(Type m00, Type m01, Type m10, Type m11)
88  {
89  matrix[0 * 2 + 0] = m00; matrix[0 * 2 + 1] = m01;
90  matrix[1 * 2 + 0] = m10; matrix[1 * 2 + 1] = m11;
91  }
92 
94  CL_Mat2(const double *init_matrix)
95  {
96  for (int i=0; i<4; i++)
97  matrix[i] = (Type) init_matrix[i];
98  }
99 
101  CL_Mat2(const cl_byte64 *init_matrix)
102  {
103  for (int i=0; i<4; i++)
104  matrix[i] = (Type) init_matrix[i];
105  }
106 
108  CL_Mat2(const cl_byte32 *init_matrix)
109  {
110  for (int i=0; i<4; i++)
111  matrix[i] = (Type) init_matrix[i];
112  }
113 
115  CL_Mat2(const cl_byte16 *init_matrix)
116  {
117  for (int i=0; i<4; i++)
118  matrix[i] = (Type) init_matrix[i];
119  }
120 
122  CL_Mat2(const cl_byte8 *init_matrix)
123  {
124  for (int i=0; i<4; i++)
125  matrix[i] = (Type) init_matrix[i];
126  }
127 
128  static CL_Mat2<Type> null();
129 
130  static CL_Mat2<Type> identity();
131 
139  static CL_Mat2<Type> multiply(const CL_Mat2<Type> &matrix_1, const CL_Mat2<Type> &matrix_2);
140 
148  static CL_Mat2<Type> add(const CL_Mat2<Type> &matrix_1, const CL_Mat2<Type> &matrix_2);
149 
157  static CL_Mat2<Type> subtract(const CL_Mat2<Type> &matrix_1, const CL_Mat2<Type> &matrix_2);
158 
162 
163 public:
164  Type matrix[4];
165 
169 
170 public:
178  CL_Mat2<Type> &multiply(const CL_Mat2<Type> &mult);
179 
187  CL_Mat2<Type> &add(const CL_Mat2<Type> &add_matrix);
188 
196  CL_Mat2<Type> &subtract(const CL_Mat2<Type> &subtract_matrix);
197 
201 
202 public:
204  operator Type const*() const { return matrix; }
205 
207  operator Type *() { return matrix; }
208 
210  Type &operator[](int i) { return matrix[i]; }
211 
213  const Type &operator[](int i) const { return matrix[i]; }
214 
216  Type &operator[](unsigned int i) { return matrix[i]; }
217 
219  const Type &operator[](unsigned int i) const { return matrix[i]; }
220 
222  CL_Mat2<Type> &operator =(const CL_Mat2<Type> &copy) {memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
223 
225  CL_Mat2<Type> &operator =(const CL_Mat4<Type> &copy);
226 
228  CL_Mat2<Type> &operator =(const CL_Mat3<Type> &copy);
229 
231  CL_Mat2<Type> operator *(const CL_Mat2<Type> &mult) const { CL_Mat2<Type> result = mult; result.multiply(*this); return result; }
232 
234  CL_Mat2<Type> operator +(const CL_Mat2<Type> &add_matrix) const { CL_Mat2<Type> result = add_matrix; result.add(*this); return result; }
235 
237  CL_Mat2<Type> operator -(const CL_Mat2<Type> &subtract_matrix) const { CL_Mat2<Type> result = subtract_matrix; result.subtract(*this); return result; }
238 
240  bool operator==(const CL_Mat2<Type> &other)
241  {
242  for (int i=0; i<4; i++)
243  if (matrix[i] != other.matrix[i]) return false;
244  return true;
245  }
246 
248  bool operator!=(const CL_Mat2<Type> &other) { return !((*this) == other); }
249 
253 
254 private:
256 };
257 
261