philsupertramp/game-math
Loading...
Searching...
No Matches
mat3.h
Go to the documentation of this file.
1#pragma once
2
3#include "../vec/vec3.h"
4
9template<class T>
10struct mat3 {
11public:
13 T values[3][3];
14
18 mat3() {
19 // clang-format off
20 values[0][0] = static_cast<T>(0); values[0][1] = static_cast<T>(0); values[0][2] = static_cast<T>(0);
21 values[1][0] = static_cast<T>(0); values[1][1] = static_cast<T>(0); values[1][2] = static_cast<T>(0);
22 values[2][0] = static_cast<T>(0); values[2][1] = static_cast<T>(0); values[2][2] = static_cast<T>(0); // clang-format on
23 }
24
25 // clang-format off
38 mat3(const T& _a, const T& _b, const T& _c,
39 const T& _d, const T& _e, const T& _f,
40 const T& _g, const T& _h, const T& _i)
41 {
42 values[0][0] = _a; values[0][1] = _b; values[0][2] =_c;
43 values[1][0] = _d; values[1][1] = _e; values[1][2] =_f;
44 values[2][0] = _g; values[2][1] = _h; values[2][2] =_i; // clang-format on
45 }
46
53 mat3(const vec3<T>& A, const vec3<T>& B, const vec3<T>& C) {
54 // clang-format off
55 values[0][0] = A.x; values[0][1] = A.y; values[0][2] = A.z;
56 values[1][0] = B.x; values[1][1] = B.y; values[1][2] = B.z;
57 values[2][0] = C.x; values[2][1] = C.y; values[2][2] = C.z; // clang-format on
58 }
59
64 explicit mat3(T* vals) {
65 values[0][0] = vals[0][0];
66 values[0][2] = vals[0][1];
67 values[0][1] = vals[0][2];
68 values[1][0] = vals[1][0];
69 values[1][2] = vals[1][1];
70 values[1][1] = vals[1][2];
71 values[2][0] = vals[2][0];
72 values[2][2] = vals[2][1];
73 values[2][1] = vals[2][2];
74 }
75
79 ~mat3() = default;
80
85 static inline mat3<T> Unit() {
86 return mat3<T>( // clang-format off
87 static_cast<T>(1), static_cast<T>(0), static_cast<T>(0),
88 static_cast<T>(0), static_cast<T>(1), static_cast<T>(0),
89 static_cast<T>(0), static_cast<T>(0), static_cast<T>(1)); // clang-format on
90 }
91
96 inline mat3<T> Transpose() {
97 // clang-format off
98 return mat3<T>(
99 values[0][0], values[1][0], values[2][0],
100 values[0][1], values[1][1], values[2][1],
101 values[0][2], values[1][2], values[2][2]); // clang-format on
102 }
103
108 inline mat3<T> Inverse() {
109 auto invDet = 1.0f / Determinant();
110 return invDet * Transpose();
111 }
112
117 inline bool IsSymmetric() {
118 return values[0][1] == values[1][0] && values[0][2] == values[2][0] && values[1][2] == values[2][1];
119 }
120
125 inline float Determinant() {
126 return // clang-format off
127 values[0][0] * values[1][1] * values[2][2] + values[0][1] * values[1][2] * values[2][0]
128 + values[0][2] * values[1][0] * values[2][1] - values[0][2] * values[1][1] * values[2][0]
129 - values[2][1] * values[1][0] * values[2][2] - values[0][0] * values[1][2] * values[2][1]; // clang-format on
130 }
131
138 friend mat3<T> operator+(mat3<T> lhs, const mat3<T>& rhs) { return lhs += rhs; }
139
146 friend mat3<T> operator-(mat3<T> lhs, const mat3<T>& rhs) { return lhs -= rhs; }
153 friend mat3<T> operator*(mat3<T> lhs, const T& rhs) { return lhs *= rhs; }
154
161 friend vec3<T> operator*(mat3<T> lhs, const vec3<T>& rhs) {
162 return vec3<T>( // clang-format off
163 lhs.values[0][0] * rhs.x + lhs.values[0][1] * rhs.y + lhs.values[0][2] * rhs.z,
164 lhs.values[1][0] * rhs.x + lhs.values[1][1] * rhs.y + lhs.values[1][2] * rhs.z,
165 lhs.values[2][0] * rhs.x + lhs.values[2][1] * rhs.y + lhs.values[2][2] * rhs.z // clang-format on
166 );
167 }
168
175 friend mat3<T> operator*(mat3<T> lhs, const mat3<T>& rhs) { return lhs *= rhs; }
176
183 friend mat3<T> operator/(mat3<T> lhs, const T& rhs) { return lhs /= rhs; }
184
185 /* compound assignment */
192 // clang-format off
193 values[0][0]+=rhs[0][0]; values[0][1]+=rhs[0][1]; values[0][2]+=rhs[0][2];
194 values[1][0]+=rhs[1][0]; values[1][1]+=rhs[1][1]; values[1][2]+=rhs[1][2];
195 values[2][0]+=rhs[2][0]; values[2][1]+=rhs[2][1]; values[2][2]+=rhs[2][2]; // clang-format on
196 return *this;
197 }
198
205 // clang-format off
206 values[0][0]-=rhs[0][0]; values[0][1]-=rhs[0][1]; values[0][2]-=rhs[0][2];
207 values[1][0]-=rhs[1][0]; values[1][1]-=rhs[1][1]; values[1][2]-=rhs[1][2];
208 values[2][0]-=rhs[2][0]; values[2][1]-=rhs[2][1]; values[2][2]-=rhs[2][2]; // clang-format on
209 return *this;
210 }
211
218 // clang-format off
219 T _a = values[0][0], _b = values[0][1], _c = values[0][2],
220 _d = values[1][0], _e = values[1][1], _f = values[1][2],
221 _g = values[2][0], _h = values[2][1], _i = values[2][2];
222
223 values[0][0] = _a * rhs[0][0] + _b * rhs[1][0] + _c * rhs[2][0];
224 values[0][1] = _a * rhs[0][1] + _b * rhs[1][1] + _c * rhs[2][1];
225 values[0][2] = _a * rhs[0][2] + _b * rhs[1][2] + _c * rhs[2][2];
226
227 values[1][0] = _d * rhs[0][0] + _e * rhs[1][0] + _f * rhs[2][0];
228 values[1][1] = _d * rhs[0][1] + _e * rhs[1][1] + _f * rhs[2][1];
229 values[1][2] = _d * rhs[0][2] + _e * rhs[1][2] + _f * rhs[2][2];
230
231 values[2][0] = _g * rhs[0][0] + _h * rhs[1][0] + _i * rhs[2][0];
232 values[2][1] = _g * rhs[0][1] + _h * rhs[1][1] + _i * rhs[2][1];
233 values[2][2] = _g * rhs[0][2] + _h * rhs[1][2] + _i * rhs[2][2]; // clang-format on
234 return *this;
235 }
236
242 mat3<T>& operator*=(const T& rhs) {
243 // clang-format off
244 values[0][0] *= rhs; values[0][1] *= rhs; values[0][2] *= rhs;
245 values[1][0] *= rhs; values[1][1] *= rhs; values[1][2] *= rhs;
246 values[2][0] *= rhs; values[2][1] *= rhs; values[2][2] *= rhs; // clang-format on
247 return *this;
248 }
249
255 mat3<T>& operator/=(const T& rhs) {
256 // clang-format off
257 values[0][0] /= rhs; values[0][1] /= rhs; values[0][2] /= rhs;
258 values[1][0] /= rhs; values[1][1] /= rhs; values[1][2] /= rhs;
259 values[2][0] /= rhs; values[2][1] /= rhs; values[2][2] /= rhs; // clang-format on
260 return *this;
261 }
262
263 /* Member access */
269 T* operator[](int index) { return values[index]; }
275 const T* operator[](int index) const { return values[index]; }
276
277 /* stream operators */
283 template<class U>
284 friend std::ostream& operator<<(std::ostream&, const mat3<U>&);
285};
286
287template<class U>
288std::ostream& operator<<(std::ostream& out, const mat3<U>& mat) {
289 out.precision(17);
290 out << "[\n\t" // clang-format off
291 << mat[0][0] << ", " << mat[0][1] << ", " << mat[0][2] << ";\n\t"
292 << mat[1][0] << ", " << mat[1][1] << ", " << mat[1][2] << ";\n\t"
293 << mat[2][0] << ", " << mat[2][1] << ", " << mat[2][2] << "\n]\n"; // clang-format on
294 return out;
295}
296
std::ostream & operator<<(std::ostream &out, const mat3< U > &mat)
Definition: mat3.h:288
Definition: mat3.h:10
mat3(T *vals)
Definition: mat3.h:64
const T * operator[](int index) const
Definition: mat3.h:275
mat3< T > Inverse()
Definition: mat3.h:108
T values[3][3]
loosely packed data
Definition: mat3.h:13
friend mat3< T > operator*(mat3< T > lhs, const mat3< T > &rhs)
Definition: mat3.h:175
T * operator[](int index)
Definition: mat3.h:269
mat3< T > & operator+=(const mat3< T > &rhs)
Definition: mat3.h:191
friend mat3< T > operator*(mat3< T > lhs, const T &rhs)
Definition: mat3.h:153
static mat3< T > Unit()
Definition: mat3.h:85
friend vec3< T > operator*(mat3< T > lhs, const vec3< T > &rhs)
Definition: mat3.h:161
friend mat3< T > operator+(mat3< T > lhs, const mat3< T > &rhs)
Definition: mat3.h:138
friend mat3< T > operator/(mat3< T > lhs, const T &rhs)
Definition: mat3.h:183
mat3(const T &_a, const T &_b, const T &_c, const T &_d, const T &_e, const T &_f, const T &_g, const T &_h, const T &_i)
Definition: mat3.h:38
mat3< T > & operator-=(const mat3< T > &rhs)
Definition: mat3.h:204
mat3< T > & operator*=(const mat3< T > &rhs)
Definition: mat3.h:217
float Determinant()
Definition: mat3.h:125
mat3< T > Transpose()
Definition: mat3.h:96
mat3(const vec3< T > &A, const vec3< T > &B, const vec3< T > &C)
Definition: mat3.h:53
~mat3()=default
bool IsSymmetric()
Definition: mat3.h:117
mat3< T > & operator/=(const T &rhs)
Definition: mat3.h:255
mat3< T > & operator*=(const T &rhs)
Definition: mat3.h:242
friend std::ostream & operator<<(std::ostream &, const mat3< U > &)
Definition: mat3.h:288
friend mat3< T > operator-(mat3< T > lhs, const mat3< T > &rhs)
Definition: mat3.h:146
mat3()
Definition: mat3.h:18
Definition: vec3.h:14
T x
Definition: vec3.h:33
T y
Definition: vec3.h:33
T z
Definition: vec3.h:33