1 | // tMatrix2.h  |
2 | //  |
3 | // A 2x2 matrix class with the expected member functions and overloads. Backs off of the tMat2 POD type and the  |
4 | // tLinearAlgebra library functions.  |
5 | //  |
6 | // Copyright (c) 2004-2006, 2015, 2017 Tristan Grimmer.  |
7 | // Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby  |
8 | // granted, provided that the above copyright notice and this permission notice appear in all copies.  |
9 | //  |
10 | // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL  |
11 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,  |
12 | // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  |
13 | // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR  |
14 | // PERFORMANCE OF THIS SOFTWARE.  |
15 |   |
16 | #pragma once  |
17 | #include "Math/tLinearAlgebra.h"  |
18 | #include "Math/tVector2.h"  |
19 | namespace tMath  |
20 | {  |
21 |   |
22 |   |
23 | struct tMatrix2 : public tMat2  |
24 | {  |
25 | tMatrix2() { }  |
26 | tMatrix2(const tMat2& m) { tSet(*this, m); }  |
27 | tMatrix2(const tVec2& c1, const tVec2& c2) { tSet(*this, c1, c2); }  |
28 | tMatrix2(const float* a) { tSet(*this, a); }  |
29 | tMatrix2(float a11, float a21, float a12, float a22) { tSet(*this, a11, a21, a12, a22); }  |
30 | const tMat2& Pod() const { return *this; }  |
31 |   |
32 | void Set(const tMat2& m) { tSet(*this, m); }  |
33 | void Set(const tVec2& c1, const tVec2& c2) { tSet(*this, c1, c2); }  |
34 | void Set(float a11, float a21, float a12, float a22) { tSet(*this, a11, a21, a12, a22); }  |
35 | void Set(const float* a) { tSet(*this, a); }  |
36 | void Get(float* a) const { tGet(a, *this); }  |
37 | tVector2& Col1() { return *((tVector2*)&C1); }  |
38 | tVector2& Col2() { return *((tVector2*)&C2); }  |
39 |   |
40 | void Zero() { tZero(*this); }  |
41 | void Zero(tComponent c) { tZero(*this, c); }  |
42 | bool IsZero() const { return tIsZero(*this); }  |
43 | bool IsZero(tComponent c) const { return tIsZero(*this, c); }  |
44 | bool ApproxEqual(const tMat2& m, float e = Epsilon) const { return tApproxEqual(*this, m, e); }  |
45 | bool ApproxEqual(const tMat2& m, tComponent c, float e = Epsilon) const { return tApproxEqual(*this, m, c, e); }  |
46 |   |
47 | void Identity() { tIdentity(*this); }  |
48 | void Transpose() { tTranspose(*this); }  |
49 | float Determinant() const { return tDeterminant(*this); }  |
50 | float Trace() const { return tTrace(*this); }  |
51 | void Invert() { tInvert(*this); }  |
52 | void MakeRotateZ(float angle) { tMakeRotateZ(*this, angle); }  |
53 |   |
54 | tMatrix2& operator=(const tMat2& m) { tSet(*this, m); return *this; }  |
55 | tMatrix2& operator+=(const tMat2& a) { tAdd(*this, a); return *this; }  |
56 | const tMatrix2 operator+(const tMat2& a) const { tMatrix2 d; tAdd(d, *this, a); return d; }  |
57 | tMatrix2& operator-=(const tMat2& a) { tSub(*this, a); return *this; }  |
58 | const tMatrix2 operator-(const tMat2& a) const { tMatrix2 d; tSub(d, *this, a); return d; }  |
59 | const tMatrix2 operator-() const { tMatrix2 d; tNeg(d, *this); return d; }  |
60 |   |
61 | // The multiplication by a vector is not declared as a friend since we treat vectors as column vectors only,  |
62 | // therefore the vector multiplication is not commutable.  |
63 | tMatrix2& operator*=(float a) { tMul(*this, a); return *this; }  |
64 | tMatrix2& operator*=(const tMat2& a) { tMul(*this, a); return *this; }  |
65 | const tMatrix2 operator*(const tMat2& a) const { tMatrix2 d; tMul(d, *this, a); return d; }  |
66 | const tVec2 operator*(const tVec2& a) const { tVec2 d; tMul(d, *this, a); return d; }  |
67 | inline friend const tMatrix2 operator*(float a, const tMat2& b) { tMatrix2 d; tMul(d, a, b); return d; }  |
68 | inline friend const tMatrix2 operator*(const tMat2& a, float b) { tMatrix2 d; tMul(d, a, b); return d; }  |
69 | tMatrix2& operator/=(float a) { tDiv(*this, a); return *this; }  |
70 | const tMatrix2 operator/(float a) const { tMatrix2 d; tDiv(d, *this, a); return d; }  |
71 |   |
72 | bool operator==(const tMat2& a) const { return tEqual(*this, a); }  |
73 | bool operator!=(const tMat2& a) const { return tNotEqual(*this, a); }  |
74 | operator const float*() { return E; }  |
75 | operator const float*() const { return E; }  |
76 | float& operator[](int i) { return E[i]; }  |
77 | float operator[](int i) const { return E[i]; }  |
78 |   |
79 | const static tMatrix2 zero; // Zero matrix.  |
80 | const static tMatrix2 identity; // Identity matrix.  |
81 | };  |
82 |   |
83 |   |
84 | }  |
85 | |