t
Loading...
Searching...
No Matches
Matrix3x3.hpp
1#include "math/Vector3.hpp"
2#include <array>
3
4#ifndef MATRIX3X3_HPP
5#define MATRIX3X3_HPP
6
7namespace t {
8
9#define n11 elements[0]
10#define n12 elements[1]
11#define n13 elements[2]
12#define n21 elements[3]
13#define n22 elements[4]
14#define n23 elements[5]
15#define n31 elements[6]
16#define n32 elements[7]
17#define n33 elements[8]
18
24class Matrix3x3 {
25public:
26 std::array<double, 9> elements;
34 static Matrix3x3 identity() { return Matrix3x3(1, 0, 0, 0, 1, 0, 0, 0, 1); }
35
41 static Matrix3x3 zero() { return Matrix3x3(0, 0, 0, 0, 0, 0, 0, 0, 0); }
42
56 Matrix3x3(double _n11, double _n12, double _n13, double _n21, double _n22,
57 double _n23, double _n31, double _n32, double _n33)
58 : elements({_n11, _n12, _n13, _n21, _n22, _n23, _n31, _n32, _n33}) {}
59
65 Matrix3x3 clone() const {
66 return Matrix3x3(n11, n12, n13, n21, n22, n23, n31, n32, n33);
67 }
68
75 Matrix3x3 &copy(const Matrix3x3 &source) {
76 for (auto i = 0; i < 9; ++i) {
77 elements[i] = source.elements[i];
78 }
79
80 return *this;
81 }
82
97 Matrix3x3 &set(double _n11, double _n12, double _n13, double _n21,
98 double _n22, double _n23, double _n31, double _n32,
99 double _n33) {
100 n11 = _n11;
101 n12 = _n12;
102 n13 = _n13;
103 n21 = _n21;
104 n22 = _n22;
105 n23 = _n23;
106 n31 = _n31;
107 n32 = _n32;
108 n33 = _n33;
109
110 return *this;
111 }
112
124 double get(int row, int column) const {
125 return elements.at(row * 3 + column);
126 }
127
137 Matrix3x3 &set(int row, int column, int value) {
138 elements.at(row * 3 + column) = value;
139 return *this;
140 }
141
147 Matrix3x3 &reset() { return set(1, 0, 0, 0, 1, 0, 0, 0, 1); }
148
155 double determinant() const {
156 return n11 * n22 * n33 + n12 * n23 * n31 + n13 * n21 * n32 -
157 n11 * n23 * n32 - n12 * n21 * n33 - n13 * n22 * n31;
158 }
159
169 return Matrix3x3(n11, n21, n31, n12, n22, n32, n13, n23, n33);
170 }
171
185 double det = determinant();
186
187 if (det == 0) {
188 return Matrix3x3(0, 0, 0, 0, 0, 0, 0, 0, 0);
189 }
190
191 return Matrix3x3(
192 (n22 * n33 - n23 * n32) / det, (n13 * n32 - n12 * n33) / det,
193 (n12 * n23 - n13 * n22) / det, (n23 * n31 - n21 * n33) / det,
194 (n11 * n33 - n13 * n31) / det, (n13 * n21 - n11 * n23) / det,
195 (n21 * n32 - n22 * n31) / det, (n12 * n31 - n11 * n32) / det,
196 (n11 * n22 - n12 * n21) / det);
197 }
198
211 double operator[](std::size_t index) const { return elements[index]; }
212
220 for (int i = 0; i < 9; ++i) {
221 elements[i] += m.elements[i];
222 }
223
224 return *this;
225 }
226
234 for (auto &element : elements) {
235 element += s;
236 }
237
238 return *this;
239 }
240
248 for (int i = 0; i < 9; ++i) {
249 elements[i] -= m.elements[i];
250 }
251
252 return *this;
253 }
254
262 for (auto &element : elements) {
263 element -= s;
264 }
265
266 return *this;
267 }
268
279 Matrix3x3 &operator*=(const Matrix3x3 &m) { return this->copy(*this * m); }
280
288 for (auto &element : elements) {
289 element *= s;
290 }
291
292 return *this;
293 }
294
303 for (int i = 0; i < 9; ++i) {
304 elements[i] /= m[i];
305 }
306
307 return *this;
308 }
309
317 for (auto &element : elements) {
318 element /= s;
319 }
320
321 return *this;
322 }
323
331 friend bool operator==(const Matrix3x3 &a, const Matrix3x3 &b) = default;
332
340 friend Matrix3x3 operator+(const Matrix3x3 &a, const Matrix3x3 &b) {
341 return Matrix3x3(a.n11 + b.n11, a.n12 + b.n12, a.n13 + b.n13, a.n21 + b.n21,
342 a.n22 + b.n22, a.n23 + b.n23, a.n31 + b.n31, a.n32 + b.n32,
343 a.n33 + b.n33);
344 }
345
353 friend Matrix3x3 operator+(const Matrix3x3 &a, double s) {
354 return Matrix3x3(a.n11 + s, a.n12 + s, a.n13 + s, a.n21 + s, a.n22 + s,
355 a.n23 + s, a.n31 + s, a.n32 + s, a.n33 + s);
356 }
357
365 friend Matrix3x3 operator+(double s, const Matrix3x3 &a) { return a + s; }
366
374 friend Matrix3x3 operator-(const Matrix3x3 &a, const Matrix3x3 &b) {
375 return Matrix3x3(a.n11 - b.n11, a.n12 - b.n12, a.n13 - b.n13, a.n21 - b.n21,
376 a.n22 - b.n22, a.n23 - b.n23, a.n31 - b.n31, a.n32 - b.n32,
377 a.n33 - b.n33);
378 }
379
388 friend Matrix3x3 operator-(const Matrix3x3 &a, double s) {
389 return Matrix3x3(a.n11 - s, a.n12 - s, a.n13 - s, a.n21 - s, a.n22 - s,
390 a.n23 - s, a.n31 - s, a.n32 - s, a.n33 - s);
391 }
392
402 friend Matrix3x3 operator-(double s, const Matrix3x3 &a) {
403 return Matrix3x3(s - a.n11, s - a.n12, s - a.n13, s - a.n21, s - a.n22,
404 s - a.n23, s - a.n31, s - a.n32, s - a.n33);
405 }
406
417 friend Matrix3x3 operator*(const Matrix3x3 &a, const Matrix3x3 &b) {
418 return Matrix3x3(a.n11 * b.n11 + a.n12 * b.n21 + a.n13 * b.n31,
419 a.n11 * b.n12 + a.n12 * b.n22 + a.n13 * b.n32,
420 a.n11 * b.n13 + a.n12 * b.n23 + a.n13 * b.n33,
421 a.n21 * b.n11 + a.n22 * b.n21 + a.n23 * b.n31,
422 a.n21 * b.n12 + a.n22 * b.n22 + a.n23 * b.n32,
423 a.n21 * b.n13 + a.n22 * b.n23 + a.n23 * b.n33,
424 a.n31 * b.n11 + a.n32 * b.n21 + a.n33 * b.n31,
425 a.n31 * b.n12 + a.n32 * b.n22 + a.n33 * b.n32,
426 a.n31 * b.n13 + a.n32 * b.n23 + a.n33 * b.n33);
427 }
428
437 friend Matrix3x3 operator*(const Matrix3x3 &a, double s) {
438 return Matrix3x3(a.n11 * s, a.n12 * s, a.n13 * s, a.n21 * s, a.n22 * s,
439 a.n23 * s, a.n31 * s, a.n32 * s, a.n33 * s);
440 }
441
450 friend Matrix3x3 operator*(double s, const Matrix3x3 &a) { return a * s; }
451
459 friend Vector3 operator*(const Matrix3x3 &a, const Vector3 &v) {
460 return Vector3(a.n11 * v.x + a.n12 * v.y + a.n13 * v.z,
461 a.n21 * v.x + a.n22 * v.y + a.n23 * v.z,
462 a.n31 * v.x + a.n32 * v.y + a.n33 * v.z);
463 }
464
472 friend Matrix3x3 operator/(const Matrix3x3 &a, const Matrix3x3 &b) {
473 return Matrix3x3(a.n11 / b.n11, a.n12 / b.n12, a.n13 / b.n13, a.n21 / b.n21,
474 a.n22 / b.n22, a.n23 / b.n23, a.n31 / b.n31, a.n32 / b.n32,
475 a.n33 / b.n33);
476 }
477
485 friend Matrix3x3 operator/(const Matrix3x3 &a, double s) {
486 return Matrix3x3(a.n11 / s, a.n12 / s, a.n13 / s, a.n21 / s, a.n22 / s,
487 a.n23 / s, a.n31 / s, a.n32 / s, a.n33 / s);
488 }
489
498 friend Matrix3x3 operator/(double s, const Matrix3x3 &a) {
499 return Matrix3x3(s / a.n11, s / a.n12, s / a.n13, s / a.n21, s / a.n22,
500 s / a.n23, s / a.n31, s / a.n32, s / a.n33);
501 }
502};
503
504#undef n11
505#undef n12
506#undef n13
507#undef n21
508#undef n22
509#undef n23
510#undef n31
511#undef n32
512#undef n33
513
514} // namespace t
515
516#endif // MATRIX3X3_HPP
The matrix class.
Definition Matrix3x3.hpp:24
Matrix3x3 & operator-=(double s)
Substracts the specified constant from all elements in this matrix.
Definition Matrix3x3.hpp:261
Matrix3x3 & operator*=(double s)
Multiplies the elements of this matrix with the specified constant.
Definition Matrix3x3.hpp:287
double determinant() const
Returns the determinant of this matrix.
Definition Matrix3x3.hpp:155
Matrix3x3 & set(int row, int column, int value)
Sets the element at the specified row and column of this matrix to the specified value.
Definition Matrix3x3.hpp:137
Matrix3x3 & reset()
Resets this matrix to the identity matrix.
Definition Matrix3x3.hpp:147
Matrix3x3 & operator/=(double s)
Divides the elements of this matrix with the specified constant.
Definition Matrix3x3.hpp:316
Matrix3x3 & operator+=(const Matrix3x3 &m)
Adds the specified matrix to this matrix.
Definition Matrix3x3.hpp:219
Matrix3x3(double _n11, double _n12, double _n13, double _n21, double _n22, double _n23, double _n31, double _n32, double _n33)
Creates a new matrix with the specified elements.
Definition Matrix3x3.hpp:56
Matrix3x3 & operator*=(const Matrix3x3 &m)
Multiplies this matrix with another matrix and copies the result into this instance.
Definition Matrix3x3.hpp:279
friend Vector3 operator*(const Matrix3x3 &a, const Vector3 &v)
Multiplies a matrix with a 3D vector.
Definition Matrix3x3.hpp:459
friend Matrix3x3 operator*(double s, const Matrix3x3 &a)
Multiplies all elements of a matrix with a constant.
Definition Matrix3x3.hpp:450
Matrix3x3 & operator+=(double s)
Adds the specified constant to all elements in this matrix.
Definition Matrix3x3.hpp:233
friend Matrix3x3 operator/(const Matrix3x3 &a, const Matrix3x3 &b)
Returns the element-wise division of two matrices.
Definition Matrix3x3.hpp:472
Matrix3x3 & set(double _n11, double _n12, double _n13, double _n21, double _n22, double _n23, double _n31, double _n32, double _n33)
Sets the elements of this matrix.
Definition Matrix3x3.hpp:97
Matrix3x3 clone() const
Returns a copy of this matrix.
Definition Matrix3x3.hpp:65
double operator[](std::size_t index) const
Returns the element at the specified index of this matrix without bounds checking and assuming row-ma...
Definition Matrix3x3.hpp:211
friend Matrix3x3 operator-(double s, const Matrix3x3 &a)
Substracts a matrix from a constant matrix.
Definition Matrix3x3.hpp:402
friend Matrix3x3 operator+(const Matrix3x3 &a, const Matrix3x3 &b)
Adds two matrices.
Definition Matrix3x3.hpp:340
Matrix3x3 transpose() const
Returns the transpose of this matrix.
Definition Matrix3x3.hpp:168
friend Matrix3x3 operator+(double s, const Matrix3x3 &a)
Adds a constant to all elements of a matrix.
Definition Matrix3x3.hpp:365
friend Matrix3x3 operator/(const Matrix3x3 &a, double s)
Divides all elements of a matrix by a constant.
Definition Matrix3x3.hpp:485
friend Matrix3x3 operator-(const Matrix3x3 &a, double s)
Substracts a constant from all elements of a matrix.
Definition Matrix3x3.hpp:388
Matrix3x3 & operator/=(const Matrix3x3 &m)
Divides the elements of this matrix by the corresponding elements of the specified matrix.
Definition Matrix3x3.hpp:302
static Matrix3x3 identity()
Returns a identity matrix.
Definition Matrix3x3.hpp:34
friend Matrix3x3 operator/(double s, const Matrix3x3 &a)
Returns the element-wise division of a constant matrix and a matrix.
Definition Matrix3x3.hpp:498
friend Matrix3x3 operator+(const Matrix3x3 &a, double s)
Adds a constant to all elements of a matrix.
Definition Matrix3x3.hpp:353
std::array< double, 9 > elements
The array containing the elements of this matrix in row-major order.
Definition Matrix3x3.hpp:26
friend Matrix3x3 operator*(const Matrix3x3 &a, const Matrix3x3 &b)
Multiplies two matrices.
Definition Matrix3x3.hpp:417
double get(int row, int column) const
Gets the element at the specified row and column of this matrix with bounds checking.
Definition Matrix3x3.hpp:124
friend Matrix3x3 operator-(const Matrix3x3 &a, const Matrix3x3 &b)
Substracts 2 matrices.
Definition Matrix3x3.hpp:374
Matrix3x3 & operator-=(const Matrix3x3 &m)
Substracts the specified matrix from this matrix.
Definition Matrix3x3.hpp:247
Matrix3x3 & copy(const Matrix3x3 &source)
Copies the elements of another matrix into this matrix.
Definition Matrix3x3.hpp:75
friend Matrix3x3 operator*(const Matrix3x3 &a, double s)
Multiplies all elements of a matrix with a constant.
Definition Matrix3x3.hpp:437
friend bool operator==(const Matrix3x3 &a, const Matrix3x3 &b)=default
Returns whether two matrices are equal.
Matrix3x3 inverse() const
Returns the inverse of this matrix or the zero matrix if this matrix does not have an inverse.
Definition Matrix3x3.hpp:184
static Matrix3x3 zero()
Returns a zero matrix.
Definition Matrix3x3.hpp:41
The 3D vector class.
Definition Vector3.hpp:20
The t software 3D graphics library namespace.
Definition algorithms.hpp:12