TDME2  1.9.200
Matrix3x3.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 
5 #include <tdme/tdme.h>
6 #include <tdme/math/fwd-tdme.h>
7 #include <tdme/math/Math.h>
8 #include <tdme/math/Vector2.h>
9 
10 using std::array;
11 
12 using tdme::math::Math;
14 
15 /**
16  * Matrix3x3 class representing matrix3x3 mathematical structure and operations for 2d space
17  * @author Andreas Drewke
18  */
20 {
21  // see: http://learnwebgl.brown37.net/10_surface_properties/texture_mapping_transforms.html
22 private:
23  array<float, 9> data {
24  0.0f, 0.0f, 0.0f,
25  0.0f, 0.0f, 0.0f,
26  0.0f, 0.0f, 0.0f
27  };
28 
29 public:
30  /**
31  * Public constructor
32  */
33  inline Matrix3x3() {
34  }
35 
36  /**
37  * Public constructor
38  * @param r0c0 row 0, column 0
39  * @param r0c1 row 0, column 1
40  * @param r0c2 row 0, column 2
41  * @param r1c0 row 1, column 0
42  * @param r1c1 row 1, column 1
43  * @param r1c2 row 1, column 2
44  * @param r2c0 row 2, column 0
45  * @param r2c1 row 2, column 1
46  * @param r2c2 row 2, column 2
47  */
48  inline Matrix3x3(
49  float r0c0, float r0c1, float r0c2,
50  float r1c0, float r1c1, float r1c2,
51  float r2c0, float r2c1, float r2c2) {
52  set(
53  r0c0, r0c1, r0c2,
54  r1c0, r1c1, r1c2,
55  r2c0, r2c1, r2c2
56  );
57  }
58 
59  /**
60  * Public constructor
61  * @param matrix matrix as array
62  */
63  inline Matrix3x3(const array<float, 9>& matrix) {
64  data = matrix;
65  }
66 
67  /**
68  * Public constructor
69  * @param matrix matrix
70  */
71  inline Matrix3x3(const Matrix3x3& matrix) {
72  data = matrix.data;
73  }
74 
75  /**
76  * Sets this matrix by its components
77  * @param r0c0 row 0, column 0
78  * @param r0c1 row 0, column 1
79  * @param r0c2 row 0, column 2
80  * @param r1c0 row 1, column 0
81  * @param r1c1 row 1, column 1
82  * @param r1c2 row 1, column 2
83  * @param r2c0 row 2, column 0
84  * @param r2c1 row 2, column 1
85  * @param r2c2 row 2, column 2
86  * @return this matrix
87  */
88  inline Matrix3x3& set(
89  float r0c0, float r0c1, float r0c2,
90  float r1c0, float r1c1, float r1c2,
91  float r2c0, float r2c1, float r2c2) {
92  data[0] = r0c0;
93  data[1] = r0c1;
94  data[2] = r0c2;
95  data[3] = r1c0;
96  data[4] = r1c1;
97  data[5] = r1c2;
98  data[6] = r2c0;
99  data[7] = r2c1;
100  data[8] = r2c2;
101  return *this;
102  }
103 
104  /**
105  * Sets this matrix by array
106  * @param matrix matrix as array
107  * @return this matrix
108  */
109  inline Matrix3x3& set(const array<float, 9>& matrix) {
110  data = matrix;
111  return *this;
112  }
113 
114  /**
115  * Sets this matrix by given matrix
116  * @param matrix matrix
117  * @return this matrix
118  */
119  inline Matrix3x3& set(const Matrix3x3& matrix) {
120  data = matrix.data;
121  return *this;
122  }
123 
124  /**
125  * Creates identity matrix
126  * @return this matrix
127  */
128  inline Matrix3x3& identity() {
129  data[0] = 1.0f;
130  data[1] = 0.0f;
131  data[2] = 0.0f;
132  data[3] = 0.0f;
133  data[4] = 1.0f;
134  data[5] = 0.0f;
135  data[6] = 0.0f;
136  data[7] = 0.0f;
137  data[8] = 1.0f;
138  return *this;
139  }
140 
141  /**
142  * Scales by scalar
143  * @param scalar scalar
144  * @returns this matrix
145  */
146  inline Matrix3x3& scale(float scalar) {
147  data[0] *= scalar;
148  data[1] *= scalar;
149  data[2] *= scalar;
150  data[3] *= scalar;
151  data[4] *= scalar;
152  data[5] *= scalar;
153  return *this;
154  }
155 
156  /**
157  * Scales by vector2
158  * @param vector2 vector2
159  * @return this matrix
160  */
161  inline Matrix3x3& scale(const Vector2& vector2) {
162  data[0] *= vector2.data[0];
163  data[1] *= vector2.data[0];
164  data[2] *= vector2.data[0];
165  data[3] *= vector2.data[1];
166  data[4] *= vector2.data[1];
167  data[5] *= vector2.data[1];
168  return *this;
169  }
170 
171  /**
172  * Multiplies this matrix with given matrix
173  * @param matrix matrix
174  * @return this matrix
175  */
176  inline Matrix3x3& multiply(const Matrix3x3& matrix) {
177  array<float, 9> _data;
178  _data[0] = data[0] * matrix.data[0] + data[3] * matrix.data[1] + data[6] * matrix.data[2];
179  _data[1] = data[1] * matrix.data[0] + data[4] * matrix.data[1] + data[7] * matrix.data[2];
180  _data[2] = data[2] * matrix.data[0] + data[5] * matrix.data[1] + data[8] * matrix.data[2];
181  _data[3] = data[0] * matrix.data[3] + data[3] * matrix.data[4] + data[6] * matrix.data[5];
182  _data[4] = data[1] * matrix.data[3] + data[4] * matrix.data[4] + data[7] * matrix.data[5];
183  _data[5] = data[2] * matrix.data[3] + data[5] * matrix.data[4] + data[8] * matrix.data[5];
184  _data[6] = data[0] * matrix.data[6] + data[3] * matrix.data[7] + data[6] * matrix.data[8];
185  _data[7] = data[1] * matrix.data[6] + data[4] * matrix.data[7] + data[7] * matrix.data[8];
186  _data[8] = data[2] * matrix.data[6] + data[5] * matrix.data[7] + data[8] * matrix.data[8];
187  data = _data;
188  return *this;
189  }
190 
191  /**
192  * Multiplies this matrix with vector2
193  * @param vector2 vector2
194  * @return vector2
195  */
196  inline Vector2 multiply(const Vector2& vector2) const {
197  return Vector2(
198  data[0] * vector2.data[0] + data[3] * vector2.data[1] + data[6],
199  data[1] * vector2.data[0] + data[4] * vector2.data[1] + data[7]
200  );
201  }
202 
203  /**
204  * Compares this matrix with given matrix
205  * @param matrix matrix
206  * @return equality
207  */
208  inline bool equals(const Matrix3x3& matrix) const {
209  return
210  (this == &matrix) ||
211  (
212  Math::abs(data[0] - matrix.data[0]) < Math::EPSILON &&
213  Math::abs(data[1] - matrix.data[1]) < Math::EPSILON &&
214  Math::abs(data[2] - matrix.data[2]) < Math::EPSILON &&
215  Math::abs(data[3] - matrix.data[3]) < Math::EPSILON &&
216  Math::abs(data[4] - matrix.data[4]) < Math::EPSILON &&
217  Math::abs(data[5] - matrix.data[5]) < Math::EPSILON &&
218  Math::abs(data[6] - matrix.data[6]) < Math::EPSILON &&
219  Math::abs(data[7] - matrix.data[7]) < Math::EPSILON &&
220  Math::abs(data[8] - matrix.data[8]) < Math::EPSILON
221  );
222  }
223 
224  /**
225  * Get coordinate system axes
226  * @param xAxis x axis
227  * @param yAxis y axis
228  */
229  inline void getAxes(Vector2& xAxis, Vector2& yAxis) const {
230  xAxis.set(data[0], data[1]);
231  yAxis.set(data[3], data[4]);
232  }
233 
234  /**
235  * Set coordinate system axes
236  * @param xAxis x axis
237  * @param yAxis y axis
238  * @return this matrix
239  */
240  inline Matrix3x3& setAxes(const Vector2& xAxis, const Vector2& yAxis) {
241  data[0] = xAxis.data[0];
242  data[1] = xAxis.data[1];
243  data[2] = 0.0f;
244  data[3] = yAxis.data[0];
245  data[4] = yAxis.data[1];
246  data[5] = 0.0f;
247  return *this;
248  }
249 
250  /**
251  * Set coordinate system axis by rotation
252  * @param angle angle
253  * @return this matrix
254  */
255  inline Matrix3x3& setAxes(float angle) {
256  auto r = angle * 3.1415927f / 180.0f;
257  float c = Math::cos(r);
258  float s = Math::sin(r);
259  data[0] = c;
260  data[1] = s;
261  data[2] = 0.0f;
262  data[3] = -s;
263  data[4] = c;
264  data[5] = 0.0f;
265  return *this;
266  }
267 
268  /**
269  * Get scale
270  * @param scale scale
271  */
272  inline void getScale(Vector2& scale) const {
273  // x axis
274  scale.data[0] = Vector2(data[0], data[1]).computeLength();
275  // y axis
276  scale.data[1] = Vector2(data[3], data[4]).computeLength();
277  }
278 
279  /**
280  * Set scale
281  * @param scale scale
282  * @return this matrix
283  */
284  inline Matrix3x3& setScale(const Vector2& scale) {
285  Vector2 axisVector;
286  // x axis
287  axisVector.set(data[0], data[1]);
288  axisVector.normalize();
289  axisVector.scale(scale.data[0]);
290  data[0] = axisVector.data[0];
291  data[1] = axisVector.data[1];
292  // y axis
293  axisVector.set(data[3], data[4]);
294  axisVector.normalize();
295  axisVector.scale(scale.data[1]);
296  data[4] = axisVector.data[0];
297  data[5] = axisVector.data[1];
298  //
299  return *this;
300  }
301 
302  /**
303  * Get translation
304  * @param vector2 vector2
305  */
306  inline void getTranslation(Vector2& vector2) const {
307  vector2.set(data[6], data[7]);
308  }
309 
310  /**
311  * Sets translation in matrix
312  * @param vector2 vector2
313  * @return this matrix
314  */
315  inline Matrix3x3& setTranslation(const Vector2& vector2) {
316  data[6] = vector2.data[0];
317  data[7] = vector2.data[1];
318  return *this;
319  }
320 
321  /**
322  * Creates rotation matrix that rotates around texture center by given angle
323  * @param angle angle
324  * @return new rotation matrix
325  */
326  static inline Matrix3x3 rotateAroundTextureCenter(float angle) {
327  return rotateAroundPoint(Vector2(0.5f, 0.5f), angle);
328  }
329 
330  /**
331  * Creates rotation matrix that rotates around given point by given angle
332  * @param point point
333  * @param angle angle
334  * @return new rotation matrix
335  */
336  static inline Matrix3x3 rotateAroundPoint(const Vector2& point, float angle) {
337  Matrix3x3 matrix;
338  matrix.identity();
339  matrix.setTranslation(point);
340  matrix.multiply((Matrix3x3()).identity().setAxes(-angle));
341  matrix.multiply((Matrix3x3()).identity().setTranslation(point.clone().scale(-1.0f)));
342  return matrix;
343  }
344 
345  /**
346  * Interpolates between matrix 1 and matrix 2 by 0f<=t<=1f linearly
347  * @param a matrix a
348  * @param b matrix b
349  * @param t t
350  * @return interpolated matrix
351  */
352  inline static Matrix3x3 interpolateLinear(const Matrix3x3& a, const Matrix3x3& b, float t) {
353  return Matrix3x3(
354  (b.data[0] * t) + ((1.0f - t) * a.data[0]),
355  (b.data[1] * t) + ((1.0f - t) * a.data[1]),
356  (b.data[2] * t) + ((1.0f - t) * a.data[2]),
357  (b.data[3] * t) + ((1.0f - t) * a.data[3]),
358  (b.data[4] * t) + ((1.0f - t) * a.data[4]),
359  (b.data[5] * t) + ((1.0f - t) * a.data[5]),
360  (b.data[6] * t) + ((1.0f - t) * a.data[6]),
361  (b.data[7] * t) + ((1.0f - t) * a.data[7]),
362  (b.data[8] * t) + ((1.0f - t) * a.data[8])
363  );
364  }
365 
366  /**
367  * @return matrix as array
368  */
369  inline const array<float, 9>& getArray() const {
370  return data;
371  }
372 
373  /**
374  * Clones this matrix
375  * @return cloned matrix
376  */
377  inline Matrix3x3 clone() const {
378  Matrix3x3 clonedMatrix(*this);
379  return clonedMatrix;
380  }
381 
382  /**
383  * Array access operator
384  * @param i index
385  * @return matrix component
386  */
387  inline float& operator[](int i) {
388  return data[i];
389  }
390 
391  /**
392  * Const array access operator
393  * @param i index
394  * @return matrix component
395  */
396  inline const float& operator[](int i) const {
397  return data[i];
398  }
399 
400  /**
401  * Operator * scalar
402  * @param scalar scalar
403  * @return new matrix (this * scalar)
404  */
405  inline Matrix3x3 operator *(const float scalar) const {
406  auto r = this->clone().scale(scalar);
407  return r;
408  }
409 
410  /**
411  * Operator * matrix
412  * @param matrix matrix
413  * @return new matrix (this * matrix)
414  */
415  inline Matrix3x3 operator *(const Matrix3x3& matrix) const {
416  auto r = this->clone().multiply(matrix);
417  return r;
418  }
419 
420  /*
421  * Operator * vector2
422  * @param vector2 vector2
423  * @return new vector (this * vector2)
424  */
425  inline Vector2 operator *(const Vector2& vector2) const {
426  return this->multiply(vector2);
427  }
428 
429  /**
430  * Operator *= matrix
431  * @param matrix matrix
432  * @return this matrix
433  */
434  inline Matrix3x3& operator *=(const Matrix3x3& matrix) {
435  return this->multiply(matrix);
436  }
437 
438  /**
439  * Equality comparison operator
440  * @param matrix matrix to compare to
441  * @return equality
442  */
443  inline bool operator ==(const Matrix3x3& matrix) const {
444  return this->equals(matrix);
445  }
446 
447  /**
448  * Non equality comparison operator
449  * @param matrix matrix to compare to
450  * @return non equality
451  */
452  inline bool operator !=(const Matrix3x3& matrix) const {
453  return this->equals(matrix) == false;
454  }
455 
456 };
Standard math functions.
Definition: Math.h:19
static float sin(float x)
Returns the sine of x.
Definition: Math.h:183
static float cos(float x)
Returns the cosine of x.
Definition: Math.h:118
static constexpr float EPSILON
Definition: Math.h:22
static auto abs(auto value)
Returns absolute value.
Definition: Math.h:63
Matrix3x3 class representing matrix3x3 mathematical structure and operations for 2d space.
Definition: Matrix3x3.h:20
void getScale(Vector2 &scale) const
Get scale.
Definition: Matrix3x3.h:272
Matrix3x3 & identity()
Creates identity matrix.
Definition: Matrix3x3.h:128
const float & operator[](int i) const
Const array access operator.
Definition: Matrix3x3.h:396
float & operator[](int i)
Array access operator.
Definition: Matrix3x3.h:387
Matrix3x3 operator*(const float scalar) const
Operator * scalar.
Definition: Matrix3x3.h:405
Matrix3x3 & set(float r0c0, float r0c1, float r0c2, float r1c0, float r1c1, float r1c2, float r2c0, float r2c1, float r2c2)
Sets this matrix by its components.
Definition: Matrix3x3.h:88
array< float, 9 > data
Definition: Matrix3x3.h:23
Matrix3x3 & set(const array< float, 9 > &matrix)
Sets this matrix by array.
Definition: Matrix3x3.h:109
Matrix3x3 & set(const Matrix3x3 &matrix)
Sets this matrix by given matrix.
Definition: Matrix3x3.h:119
Matrix3x3 & setAxes(float angle)
Set coordinate system axis by rotation.
Definition: Matrix3x3.h:255
Matrix3x3 & setScale(const Vector2 &scale)
Set scale.
Definition: Matrix3x3.h:284
bool equals(const Matrix3x3 &matrix) const
Compares this matrix with given matrix.
Definition: Matrix3x3.h:208
bool operator!=(const Matrix3x3 &matrix) const
Non equality comparison operator.
Definition: Matrix3x3.h:452
Matrix3x3 & operator*=(const Matrix3x3 &matrix)
Operator *= matrix.
Definition: Matrix3x3.h:434
Matrix3x3(const Matrix3x3 &matrix)
Public constructor.
Definition: Matrix3x3.h:71
Matrix3x3(const array< float, 9 > &matrix)
Public constructor.
Definition: Matrix3x3.h:63
Vector2 multiply(const Vector2 &vector2) const
Multiplies this matrix with vector2.
Definition: Matrix3x3.h:196
bool operator==(const Matrix3x3 &matrix) const
Equality comparison operator.
Definition: Matrix3x3.h:443
const array< float, 9 > & getArray() const
Definition: Matrix3x3.h:369
Matrix3x3()
Public constructor.
Definition: Matrix3x3.h:33
Matrix3x3 & setTranslation(const Vector2 &vector2)
Sets translation in matrix.
Definition: Matrix3x3.h:315
static Matrix3x3 rotateAroundTextureCenter(float angle)
Creates rotation matrix that rotates around texture center by given angle.
Definition: Matrix3x3.h:326
Matrix3x3 clone() const
Clones this matrix.
Definition: Matrix3x3.h:377
Matrix3x3 & setAxes(const Vector2 &xAxis, const Vector2 &yAxis)
Set coordinate system axes.
Definition: Matrix3x3.h:240
Matrix3x3 & scale(const Vector2 &vector2)
Scales by vector2.
Definition: Matrix3x3.h:161
void getTranslation(Vector2 &vector2) const
Get translation.
Definition: Matrix3x3.h:306
void getAxes(Vector2 &xAxis, Vector2 &yAxis) const
Get coordinate system axes.
Definition: Matrix3x3.h:229
Matrix3x3 & scale(float scalar)
Scales by scalar.
Definition: Matrix3x3.h:146
static Matrix3x3 rotateAroundPoint(const Vector2 &point, float angle)
Creates rotation matrix that rotates around given point by given angle.
Definition: Matrix3x3.h:336
static Matrix3x3 interpolateLinear(const Matrix3x3 &a, const Matrix3x3 &b, float t)
Interpolates between matrix 1 and matrix 2 by 0f<=t<=1f linearly.
Definition: Matrix3x3.h:352
Matrix3x3(float r0c0, float r0c1, float r0c2, float r1c0, float r1c1, float r1c2, float r2c0, float r2c1, float r2c2)
Public constructor.
Definition: Matrix3x3.h:48
Matrix3x3 & multiply(const Matrix3x3 &matrix)
Multiplies this matrix with given matrix.
Definition: Matrix3x3.h:176
Vector2 class representing vector2 mathematical structure and operations with x, y components.
Definition: Vector2.h:20
float computeLength() const
Definition: Vector2.h:229
Vector2 & scale(const float scalar)
Scales by scalar.
Definition: Vector2.h:174
Vector2 clone() const
Clones this vector2.
Definition: Vector2.h:265
Vector2 & normalize()
Normalizes this vector2.
Definition: Vector2.h:209
array< float, 2 > data
Definition: Vector2.h:24
Vector2 & set(float x, float y)
Sets this vector2 by its components.
Definition: Vector2.h:65