mat2.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 "../System/cl_platform.h"
36 #include "mat3.h"
37 #include "mat4.h"
38 #include "vec2.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 Mat2
61 {
64 
65 public:
67  Mat2() { }
68 
70  Mat2(const Mat2<Type> &copy)
71  {
72  for (int i=0; i<4; i++)
73  matrix[i] = copy.matrix[i];
74  }
75 
77  explicit Mat2(const Mat3<Type> &copy);
78 
80  explicit Mat2(const Mat4<Type> &copy);
81 
83  explicit Mat2(const float *init_matrix)
84  {
85  for (int i=0; i<4; i++)
86  matrix[i] = (Type) init_matrix[i];
87  }
88 
90  explicit Mat2(Type m00, Type m01, Type m10, Type m11)
91  {
92  matrix[0 * 2 + 0] = m00; matrix[0 * 2 + 1] = m01;
93  matrix[1 * 2 + 0] = m10; matrix[1 * 2 + 1] = m11;
94  }
95 
97  explicit Mat2(const double *init_matrix)
98  {
99  for (int i=0; i<4; i++)
100  matrix[i] = (Type) init_matrix[i];
101  }
102 
104  explicit Mat2(const byte64 *init_matrix)
105  {
106  for (int i=0; i<4; i++)
107  matrix[i] = (Type) init_matrix[i];
108  }
109 
111  explicit Mat2(const byte32 *init_matrix)
112  {
113  for (int i=0; i<4; i++)
114  matrix[i] = (Type) init_matrix[i];
115  }
116 
118  explicit Mat2(const byte16 *init_matrix)
119  {
120  for (int i=0; i<4; i++)
121  matrix[i] = (Type) init_matrix[i];
122  }
123 
125  explicit Mat2(const byte8 *init_matrix)
126  {
127  for (int i=0; i<4; i++)
128  matrix[i] = (Type) init_matrix[i];
129  }
130 
131  static Mat2<Type> null();
132 
133  static Mat2<Type> identity();
134 
143  static Mat2<Type> multiply(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
144 
152  static Mat2<Type> add(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
153 
161  static Mat2<Type> subtract(const Mat2<Type> &matrix_1, const Mat2<Type> &matrix_2);
162 
168  static bool is_equal(const Mat2<Type> &first, const Mat2<Type> &second, Type epsilon)
169  {
170  for (int i=0; i<4; i++)
171  {
172  Type diff = second.matrix[i] - first.matrix[i];
173  if (diff < -epsilon || diff > epsilon) return false;
174  }
175  return true;
176  }
177 
181 
182 public:
183  Type matrix[4];
184 
188 
189 public:
194  bool is_equal(const Mat2<Type> &other, Type epsilon) const { return Mat2<Type>::is_equal(*this, other, epsilon); }
195 
199 
200 public:
202  operator Type const*() const { return matrix; }
203 
205  operator Type *() { return matrix; }
206 
208  Type &operator[](int i) { return matrix[i]; }
209 
211  const Type &operator[](int i) const { return matrix[i]; }
212 
214  Type &operator[](unsigned int i) { return matrix[i]; }
215 
217  const Type &operator[](unsigned int i) const { return matrix[i]; }
218 
220  Mat2<Type> &operator =(const Mat2<Type> &copy) {memcpy(matrix, copy.matrix, sizeof(matrix)); return *this; }
221 
223  Mat2<Type> &operator =(const Mat4<Type> &copy);
224 
226  Mat2<Type> &operator =(const Mat3<Type> &copy);
227 
229  Mat2<Type> operator *(const Mat2<Type> &mult) const;
230 
232  Mat2<Type> operator +(const Mat2<Type> &add_matrix) const;
233 
235  Mat2<Type> operator -(const Mat2<Type> &subtract_matrix) const;
236 
238  bool operator==(const Mat2<Type> &other) const
239  {
240  for (int i=0; i<4; i++)
241  if (matrix[i] != other.matrix[i]) return false;
242  return true;
243  }
244 
246  bool operator!=(const Mat2<Type> &other) const { return !((*this) == other); }
247 
251 
252 private:
254 };
255 
256 typedef Mat2<int> Mat2i;
259 
260 }
261 
Mat2< float > Mat2f
Definition: mat2.h:257
static Mat2< Type > identity()
Mat2< double > Mat2d
Definition: mat2.h:258
Angle class.
Definition: angle.h:63
static Mat2< Type > subtract(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Subtract 2 matrices.
Mat2< Type > operator+(const Mat2< Type > &add_matrix) const
Addition operator.
static Mat2< Type > null()
Mat2< Type > & operator=(const Mat2< Type > &copy)
Copy assignment operator.
Definition: mat2.h:220
char byte8
Definition: cl_platform.h:59
bool is_equal(const Mat2< Type > &other, Type epsilon) const
Returns true if equal within the bounds of an epsilon.
Definition: mat2.h:194
Mat2()
Constructs a 2x2 matrix (uninitialised)
Definition: mat2.h:67
Mat2< Type > operator*(const Mat2< Type > &mult) const
Multiplication operator.
Mat2(const byte64 *init_matrix)
Constructs a 2x2 matrix (copied from 4, 64 bit integers)
Definition: mat2.h:104
Mat2(const byte16 *init_matrix)
Constructs a 2x2 matrix (copied from 4, 16 bit integers)
Definition: mat2.h:118
Mat2(const double *init_matrix)
Constructs a 2x2 matrix (copied from 4 doubles)
Definition: mat2.h:97
Type matrix[4]
Definition: mat2.h:183
const Type & operator[](int i) const
Operator that returns the matrix cell at the given index.
Definition: mat2.h:211
Mat2(const Mat2< Type > &copy)
Constructs a 2x2 matrix (copied)
Definition: mat2.h:70
Mat2(Type m00, Type m01, Type m10, Type m11)
Constructs a 2x2 matrix (copied from specified values)
Definition: mat2.h:90
static bool is_equal(const Mat2< Type > &first, const Mat2< Type > &second, Type epsilon)
Returns true if equal within the bounds of an epsilon.
Definition: mat2.h:168
2D matrix
Definition: mat2.h:46
Mat2(const byte32 *init_matrix)
Constructs a 2x2 matrix (copied from 4, 32 bit integers)
Definition: mat2.h:111
Mat2< Type > operator-(const Mat2< Type > &subtract_matrix) const
Subtract operator.
Type & operator[](int i)
Operator that returns the matrix cell at the given index.
Definition: mat2.h:208
3D matrix
Definition: mat2.h:49
Mat2(const byte8 *init_matrix)
Constructs a 2x2 matrix (copied from 4, 8 bit integers)
Definition: mat2.h:125
int byte32
Definition: cl_platform.h:63
long long byte64
Definition: cl_platform.h:65
Mat2< int > Mat2i
Definition: mat2.h:256
Mat2(const float *init_matrix)
Constructs a 2x2 matrix (copied from 4 floats)
Definition: mat2.h:83
static Mat2< Type > add(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Add 2 matrices.
Type & operator[](unsigned int i)
Operator that returns the matrix cell at the given index.
Definition: mat2.h:214
4D matrix
Definition: mat2.h:52
bool operator==(const Mat2< Type > &other) const
Equality operator.
Definition: mat2.h:238
const Type & operator[](unsigned int i) const
Operator that returns the matrix cell at the given index.
Definition: mat2.h:217
short byte16
Definition: cl_platform.h:61
bool operator!=(const Mat2< Type > &other) const
Not-equal operator.
Definition: mat2.h:246
static Mat2< Type > multiply(const Mat2< Type > &matrix_1, const Mat2< Type > &matrix_2)
Multiply 2 matrices.