Copperfield Engine 0.1
C++ Game Engine
Loading...
Searching...
No Matches
matrix_3.h
1//--------------------------------------------------------------//
2// Math Library
3// Matrix 3 Class Definition.
4//--------------------------------------------------------------//
5//
6// m0 m1 m2
7// m3 m4 m5
8// m6 m7 m8
9//
10//--------------------------------------------------------------//
11#ifndef __MATRIX3_H__
12#define __MATRIX3_H__ 1
13
14#include "math/vector_2.h"
15#include "math/vector_3.h"
16namespace coma {
18class Mat3 {
19 public:
20 Mat3();
21 Mat3(float* values_array);
22 Mat3(float value);
23 Mat3(Vec3 a, Vec3 b, Vec3 c);
24
25 Mat3(const Mat3& copy);
26 ~Mat3();
27
30 static Mat3 Identity();
31
35 Mat3 Multiply(const Mat3& other) const;
36
39 float Determinant() const;
40
43 Mat3 Adjoint() const;
44
48 bool GetInverse(Mat3& out) const;
49
52 bool Inverse();
53
56 Mat3 Transpose() const;
57
61 static Mat3 Translate(const Vec2& position);
66 static Mat3 Translate(float x, float y);
67
71 Vec3 GetColum(int colum) const;
72
76 Vec3 GetLine(int line) const;
77
78 inline Mat3 operator+(const Mat3& other) const;
79 inline Mat3& operator+=(const Mat3& other);
80 inline Mat3 operator+(float value) const;
81 inline Mat3& operator+=(float value);
82 inline Mat3 operator-(const Mat3& other) const;
83 inline Mat3& operator-=(const Mat3& other);
84 inline Mat3 operator-(float value) const;
85 inline Mat3& operator-=(float value);
86 inline Mat3 operator*(float value) const;
87 inline Mat3& operator*=(float value);
88 inline Mat3 operator/(float value) const;
89 inline Mat3& operator/=(float value);
90 bool operator==(const Mat3& other) const;
91 bool operator!=(const Mat3& other) const;
92 inline void operator=(const Mat3& other);
93
94 float m[9];
95};
96
97inline Mat3 Mat3::operator+(const Mat3& other) const {
98 Mat3 res;
99 for (int i = 0; i < 9; i++) {
100 res.m[i] = m[i] + other.m[i];
101 }
102 return res;
103}
104
105inline Mat3& Mat3::operator+=(const Mat3& other) {
106 for (int i = 0; i < 9; i++) {
107 m[i] += other.m[i];
108 }
109 return *this;
110}
111
112inline Mat3 Mat3::operator+(float value) const {
113 Mat3 res;
114 for (int i = 0; i < 9; i++) {
115 res.m[i] = m[i] + value;
116 }
117 return res;
118}
119
120inline Mat3& Mat3::operator+=(float value) {
121 for (int i = 0; i < 9; i++) {
122 m[i] += value;
123 }
124 return *this;
125}
126
127inline Mat3 Mat3::operator-(const Mat3& other) const {
128 Mat3 res;
129 for (int i = 0; i < 9; i++) {
130 res.m[i] = m[i] - other.m[i];
131 }
132 return res;
133}
134
135inline Mat3& Mat3::operator-=(const Mat3& other) {
136 for (int i = 0; i < 9; i++) {
137 m[i] -= other.m[i];
138 }
139 return *this;
140}
141
142inline Mat3 Mat3::operator-(float value) const {
143 Mat3 res;
144 for (int i = 0; i < 9; i++) {
145 res.m[i] = m[i] - value;
146 }
147 return res;
148}
149
150inline Mat3& Mat3::operator-=(float value) {
151 for (int i = 0; i < 9; i++) {
152 m[i] -= value;
153 }
154 return *this;
155}
156
157inline Mat3 Mat3::operator*(float value) const {
158 Mat3 res;
159 for (int i = 0; i < 9; i++) {
160 res.m[i] = m[i] * value;
161 }
162 return res;
163}
164
165inline Mat3& Mat3::operator*=(float value) {
166 for (int i = 0; i < 9; i++) {
167 m[i] *= value;
168 }
169 return *this;
170}
171
172inline Mat3 Mat3::operator/(float value) const {
173 Mat3 res;
174 for (int i = 0; i < 9; i++) {
175 res.m[i] = m[i] / value;
176 }
177 return res;
178}
179
180inline Mat3& Mat3::operator/=(float value) {
181 for (int i = 0; i < 9; i++) {
182 m[i] /= value;
183 }
184 return *this;
185}
186
187inline bool Mat3::operator==(const Mat3& other) const {
188 for (int i = 0; i < 9; i++) {
189 if (m[i] != other.m[i]) return false;
190 }
191 return true;
192}
193
194inline bool Mat3::operator!=(const Mat3& other) const {
195 for (int i = 0; i < 9; i++) {
196 if (m[i] != other.m[i]) return true;
197 }
198 return false;
199}
200
201inline void Mat3::operator=(const Mat3& other) {
202 for (int i = 0; i < 9; i++) {
203 m[i] = other.m[i];
204 }
205}
206
208 return Mat3(Vec3(1, 0, 0), Vec3(0, 1, 0), Vec3(0, 0, 1));
209}
210
211inline float Mat3::Determinant() const {
212 return (m[0] * m[4] * m[8]) + (m[1] * m[5] * m[6]) + (m[2] * m[3] * m[7]) -
213 (m[2] * m[4] * m[6]) - (m[1] * m[3] * m[8]) - (m[0] * m[5] * m[7]);
214}
215
216inline bool Mat3::GetInverse(Mat3& out) const {
217 float det = this->Determinant();
218 if (det == 0) return false;
219 out = this->Adjoint() / det;
220 return true;
221}
222
223inline bool Mat3::Inverse() {
224 float det = this->Determinant();
225 if (det == 0) return false;
226 operator=(this->Adjoint().Transpose() / det);
227 return true;
228}
229
230inline Mat3 Mat3::Translate(const Vec2& mov_vector) {
231 Mat3 res = Mat3::Identity();
232 res.m[2] = mov_vector.x;
233 res.m[5] = mov_vector.y;
234 return res;
235}
236
237inline Mat3 Mat3::Translate(float x, float y) {
238 Mat3 res = Mat3::Identity();
239 res.m[2] = x;
240 res.m[5] = y;
241 return res;
242}
243
244inline Mat3 Mat3::Multiply(const Mat3& other) const {
245 Mat3 res;
246 for (int row = 0; row < 3; row++) {
247 for (int col = 0; col < 3; col++) {
248 res.m[row * 3 + col] = m[row * 3 + 0] * other.m[0 * 3 + col] +
249 m[row * 3 + 1] * other.m[1 * 3 + col] +
250 m[row * 3 + 2] * other.m[2 * 3 + col];
251 }
252 }
253 return res;
254}
255
256inline Mat3 Mat3::Adjoint() const {
257 return Mat3(Vec3(m[4] * m[8] - m[5] * m[7], -(m[3] * m[8] - m[5] * m[6]),
258 m[3] * m[7] - m[4] * m[6]),
259 Vec3(-(m[1] * m[8] - m[2] * m[7]), m[0] * m[8] - m[2] * m[6],
260 -(m[0] * m[7] - m[1] * m[6])),
261 Vec3(m[1] * m[5] - m[2] * m[4], -(m[0] * m[5] - m[2] * m[3]),
262 m[0] * m[4] - m[1] * m[3]));
263}
264
265inline Mat3 Mat3::Transpose() const {
266 return Mat3(Vec3(m[0], m[3], m[6]), Vec3(m[1], m[4], m[7]),
267 Vec3(m[2], m[5], m[8]));
268}
269
270inline Vec3 Mat3::GetColum(int colum) const {
271 return Vec3(m[colum], m[3 + colum], m[6 + colum]);
272}
273
274inline Vec3 Mat3::GetLine(int line) const {
275 return Vec3(m[line * 3 + 0], m[line * 3 + 1], m[line * 3 + 2]);
276}
277} // namespace coma
278#endif
represents mathematical matrix with 3 cols and 3 rows
Definition: matrix_3.h:18
Mat3 Transpose() const
Definition: matrix_3.h:265
Mat3 Adjoint() const
Definition: matrix_3.h:256
Mat3 Multiply(const Mat3 &other) const
Definition: matrix_3.h:244
float Determinant() const
Definition: matrix_3.h:211
Vec3 GetLine(int line) const
Definition: matrix_3.h:274
static Mat3 Identity()
Definition: matrix_3.h:207
bool Inverse()
Definition: matrix_3.h:223
static Mat3 Translate(const Vec2 &position)
Definition: matrix_3.h:230
Vec3 GetColum(int colum) const
Definition: matrix_3.h:270
bool GetInverse(Mat3 &out) const
Definition: matrix_3.h:216
represents mathematical vector with 2 components
Definition: vector_2.h:7
represents mathematical vector with 3 components
Definition: vector_3.h:12
copperdielf Math Library
Definition: buffer.hpp:5