Debian Codegraph @

Source/Core/Common/Matrix.h

raw symbols SCIP
1 // Copyright 2019 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <array>
7 #include <cmath>
8 #include <functional>
9 #include <type_traits>
10
11 // Tiny matrix/vector library.
12 // Used for things like Free-Look in the gfx backend.
13
14 namespace Common
15 {
16 template <typename T>
17 union TVec3
18 {
19 constexpr TVec3() = default;
20 constexpr TVec3(T _x, T _y, T _z) : data{_x, _y, _z} {}
21
22 template <typename OtherT>
23 constexpr explicit TVec3(const TVec3<OtherT>& other) : TVec3(other.x, other.y, other.z)
24 {
25 }
26
27 constexpr bool operator==(const TVec3& other) const
28 {
29 return x == other.x && y == other.y && z == other.z;
30 }
31
32 constexpr TVec3 Cross(const TVec3& rhs) const
33 {
34 return {(y * rhs.z) - (rhs.y * z), (z * rhs.x) - (rhs.z * x), (x * rhs.y) - (rhs.x * y)};
35 }
36 constexpr T Dot(const TVec3& other) const { return x * other.x + y * other.y + z * other.z; }
37 constexpr T LengthSquared() const { return Dot(*this); }
38 T Length() const { return std::sqrt(LengthSquared()); }
39 TVec3 Normalized() const { return *this / Length(); }
40
41 constexpr TVec3& operator+=(const TVec3& rhs)
42 {
43 x += rhs.x;
44 y += rhs.y;
45 z += rhs.z;
46 return *this;
47 }
48
49 constexpr TVec3& operator-=(const TVec3& rhs)
50 {
51 x -= rhs.x;
52 y -= rhs.y;
53 z -= rhs.z;
54 return *this;
55 }
56
57 constexpr TVec3& operator*=(const TVec3& rhs)
58 {
59 x *= rhs.x;
60 y *= rhs.y;
61 z *= rhs.z;
62 return *this;
63 }
64
65 constexpr TVec3& operator/=(const TVec3& rhs)
66 {
67 x /= rhs.x;
68 y /= rhs.y;
69 z /= rhs.z;
70 return *this;
71 }
72
73 constexpr TVec3 operator-() const { return {-x, -y, -z}; }
74
75 // Apply function to each element and return the result.
76 template <typename F>
77 auto Map(F&& f) const -> TVec3<decltype(f(T{}))>
78 {
79 return {f(x), f(y), f(z)};
80 }
81
82 template <typename F, typename T2>
83 auto Map(F&& f, const TVec3<T2>& t) const -> TVec3<decltype(f(T{}, t.x))>
84 {
85 return {f(x, t.x), f(y, t.y), f(z, t.z)};
86 }
87
88 template <typename F, typename T2>
89 auto Map(F&& f, T2 scalar) const -> TVec3<decltype(f(T{}, scalar))>
90 {
91 return {f(x, scalar), f(y, scalar), f(z, scalar)};
92 }
93
94 std::array<T, 3> data = {};
95
96 struct
97 {
98 T x;
99 T y;
100 T z;
101 };
102 };
103
104 template <typename T>
105 TVec3<bool> operator<(const TVec3<T>& lhs, const TVec3<T>& rhs)
106 {
107 return lhs.Map(std::less<T>{}, rhs);
108 }
109
110 constexpr TVec3<bool> operator!(const TVec3<bool>& vec)
111 {
112 return {!vec.x, !vec.y, !vec.z};
113 }
114
115 template <typename T>
116 auto operator+(const TVec3<T>& lhs, const TVec3<T>& rhs) -> TVec3<decltype(lhs.x + rhs.x)>
117 {
118 return lhs.Map(std::plus<decltype(lhs.x + rhs.x)>{}, rhs);
119 }
120
121 template <typename T>
122 auto operator-(const TVec3<T>& lhs, const TVec3<T>& rhs) -> TVec3<decltype(lhs.x - rhs.x)>
123 {
124 return lhs.Map(std::minus<decltype(lhs.x - rhs.x)>{}, rhs);
125 }
126
127 template <typename T1, typename T2>
128 auto operator*(const TVec3<T1>& lhs, const TVec3<T2>& rhs) -> TVec3<decltype(lhs.x * rhs.x)>
129 {
130 return lhs.Map(std::multiplies<decltype(lhs.x * rhs.x)>{}, rhs);
131 }
132
133 template <typename T>
134 auto operator/(const TVec3<T>& lhs, const TVec3<T>& rhs) -> TVec3<decltype(lhs.x / rhs.x)>
135 {
136 return lhs.Map(std::divides<decltype(lhs.x / rhs.x)>{}, rhs);
137 }
138
139 template <typename T1, typename T2>
140 auto operator*(const TVec3<T1>& lhs, T2 scalar) -> TVec3<decltype(lhs.x * scalar)>
141 {
142 return lhs.Map(std::multiplies<decltype(lhs.x * scalar)>{}, scalar);
143 }
144
145 template <typename T1, typename T2>
146 auto operator/(const TVec3<T1>& lhs, T2 scalar) -> TVec3<decltype(lhs.x / scalar)>
147 {
148 return lhs.Map(std::divides<decltype(lhs.x / scalar)>{}, scalar);
149 }
150
151 using Vec3 = TVec3<float>;
152 using DVec3 = TVec3<double>;
153
154 template <typename T>
155 union TVec4
156 {
157 constexpr TVec4() = default;
158 constexpr TVec4(TVec3<T> _vec, T _w) : TVec4{_vec.x, _vec.y, _vec.z, _w} {}
159 constexpr TVec4(T _x, T _y, T _z, T _w) : data{_x, _y, _z, _w} {}
160
161 constexpr bool operator==(const TVec4& other) const
162 {
163 return x == other.x && y == other.y && z == other.z && w == other.w;
164 }
165
166 constexpr T Dot(const TVec4& other) const
167 {
168 return x * other.x + y * other.y + z * other.z + w * other.w;
169 }
170
171 constexpr TVec4& operator*=(const TVec4& rhs)
172 {
173 x *= rhs.x;
174 y *= rhs.y;
175 z *= rhs.z;
176 w *= rhs.w;
177 return *this;
178 }
179
180 constexpr TVec4& operator/=(const TVec4& rhs)
181 {
182 x /= rhs.x;
183 y /= rhs.y;
184 z /= rhs.z;
185 w /= rhs.w;
186 return *this;
187 }
188
189 constexpr TVec4& operator*=(T scalar) { return *this *= TVec4{scalar, scalar, scalar, scalar}; }
190 constexpr TVec4& operator/=(T scalar) { return *this /= TVec4{scalar, scalar, scalar, scalar}; }
191
192 std::array<T, 4> data = {};
193
194 struct
195 {
196 T x;
197 T y;
198 T z;
199 T w;
200 };
201 };
202
203 template <typename T>
204 constexpr TVec4<T> operator*(TVec4<T> lhs, std::common_type_t<T> scalar)
205 {
206 return lhs *= scalar;
207 }
208
209 template <typename T>
210 constexpr TVec4<T> operator/(TVec4<T> lhs, std::common_type_t<T> scalar)
211 {
212 return lhs /= scalar;
213 }
214
215 using Vec4 = TVec4<float>;
216 using DVec4 = TVec4<double>;
217
218 template <typename T>
219 union TVec2
220 {
221 constexpr TVec2() = default;
222 constexpr TVec2(T _x, T _y) : data{_x, _y} {}
223
224 template <typename OtherT>
225 constexpr explicit TVec2(const TVec2<OtherT>& other) : TVec2(other.x, other.y)
226 {
227 }
228
229 constexpr bool operator==(const TVec2& other) const { return x == other.x && y == other.y; }
230
231 constexpr T Cross(const TVec2& rhs) const { return (x * rhs.y) - (y * rhs.x); }
232 constexpr T Dot(const TVec2& rhs) const { return (x * rhs.x) + (y * rhs.y); }
233 constexpr T LengthSquared() const { return Dot(*this); }
234 T Length() const { return std::sqrt(LengthSquared()); }
235 TVec2 Normalized() const { return *this / Length(); }
236
237 constexpr TVec2& operator+=(const TVec2& rhs)
238 {
239 x += rhs.x;
240 y += rhs.y;
241 return *this;
242 }
243
244 constexpr TVec2& operator-=(const TVec2& rhs)
245 {
246 x -= rhs.x;
247 y -= rhs.y;
248 return *this;
249 }
250
251 constexpr TVec2& operator*=(const TVec2& rhs)
252 {
253 x *= rhs.x;
254 y *= rhs.y;
255 return *this;
256 }
257
258 constexpr TVec2& operator/=(const TVec2& rhs)
259 {
260 x /= rhs.x;
261 y /= rhs.y;
262 return *this;
263 }
264
265 constexpr TVec2& operator*=(T scalar)
266 {
267 x *= scalar;
268 y *= scalar;
269 return *this;
270 }
271
272 constexpr TVec2& operator/=(T scalar)
273 {
274 x /= scalar;
275 y /= scalar;
276 return *this;
277 }
278
279 constexpr TVec2 operator-() const { return {-x, -y}; }
280
281 std::array<T, 2> data = {};
282
283 struct
284 {
285 T x;
286 T y;
287 };
288 };
289
290 template <typename T>
291 constexpr TVec2<bool> operator<(const TVec2<T>& lhs, const TVec2<T>& rhs)
292 {
293 return {lhs.x < rhs.x, lhs.y < rhs.y};
294 }
295
296 constexpr TVec2<bool> operator!(const TVec2<bool>& vec)
297 {
298 return {!vec.x, !vec.y};
299 }
300
301 template <typename T>
302 constexpr TVec2<T> operator+(TVec2<T> lhs, const TVec2<T>& rhs)
303 {
304 return lhs += rhs;
305 }
306
307 template <typename T>
308 constexpr TVec2<T> operator-(TVec2<T> lhs, const TVec2<T>& rhs)
309 {
310 return lhs -= rhs;
311 }
312
313 template <typename T>
314 constexpr TVec2<T> operator*(TVec2<T> lhs, const TVec2<T>& rhs)
315 {
316 return lhs *= rhs;
317 }
318
319 template <typename T>
320 constexpr TVec2<T> operator/(TVec2<T> lhs, const TVec2<T>& rhs)
321 {
322 return lhs /= rhs;
323 }
324
325 template <typename T, typename T2>
326 constexpr auto operator*(TVec2<T> lhs, T2 scalar)
327 {
328 return TVec2<decltype(lhs.x * scalar)>(lhs) *= scalar;
329 }
330
331 template <typename T, typename T2>
332 constexpr auto operator/(TVec2<T> lhs, T2 scalar)
333 {
334 return TVec2<decltype(lhs.x / scalar)>(lhs) /= scalar;
335 }
336
337 using Vec2 = TVec2<float>;
338 using DVec2 = TVec2<double>;
339
340 class Matrix33;
341
342 class Quaternion
343 {
344 public:
345 static Quaternion Identity();
346
347 static Quaternion RotateX(float rad);
348 static Quaternion RotateY(float rad);
349 static Quaternion RotateZ(float rad);
350
351 // Returns a quaternion with rotations about each axis simultaneously (e.g processing gyroscope
352 // input)
353 static Quaternion RotateXYZ(const Vec3& rads);
354
355 static Quaternion Rotate(float rad, const Vec3& axis);
356
357 Quaternion() = default;
358 Quaternion(float w, float x, float y, float z);
359
360 float Norm() const;
361 Quaternion Normalized() const;
362 Quaternion Conjugate() const;
363 Quaternion Inverted() const;
364
365 Quaternion& operator*=(const Quaternion& rhs);
366
367 Vec4 data;
368 };
369
370 Quaternion operator*(Quaternion lhs, const Quaternion& rhs);
371 Vec3 operator*(const Quaternion& lhs, const Vec3& rhs);
372
373 Vec3 FromQuaternionToEuler(const Quaternion& q);
374
375 class Matrix33
376 {
377 public:
378 static Matrix33 Identity();
379 static Matrix33 FromQuaternion(const Quaternion&);
380
381 // Return a rotation matrix around the x,y,z axis
382 static Matrix33 RotateX(float rad);
383 static Matrix33 RotateY(float rad);
384 static Matrix33 RotateZ(float rad);
385
386 static Matrix33 Rotate(float rad, const Vec3& axis);
387
388 static Matrix33 Scale(const Vec3& vec);
389
390 // set result = a x b
391 static void Multiply(const Matrix33& a, const Matrix33& b, Matrix33* result);
392 static void Multiply(const Matrix33& a, const Vec3& vec, Vec3* result);
393
394 float Determinant() const;
395 Matrix33 Inverted() const;
396 Matrix33 Transposed() const;
397
398 Matrix33& operator*=(const Matrix33& rhs)
399 {
400 Multiply(*this, rhs, this);
401 return *this;
402 }
403
404 // Note: Row-major storage order.
405 std::array<float, 9> data;
406 };
407
408 inline Matrix33 operator*(Matrix33 lhs, const Matrix33& rhs)
409 {
410 return lhs *= rhs;
411 }
412
413 inline Vec3 operator*(const Matrix33& lhs, Vec3 rhs)
414 {
415 Matrix33::Multiply(lhs, rhs, &rhs);
416 return rhs;
417 }
418
419 class Matrix44
420 {
421 public:
422 static Matrix44 Identity();
423 static Matrix44 FromMatrix33(const Matrix33& m33);
424 static Matrix44 FromQuaternion(const Quaternion& q);
425 static Matrix44 FromArray(const std::array<float, 16>& arr);
426
427 static Matrix44 Translate(const Vec3& vec);
428 static Matrix44 Shear(const float a, const float b = 0);
429 static Matrix44 Perspective(float fov_y, float aspect_ratio, float z_near, float z_far);
430
431 static void Multiply(const Matrix44& a, const Matrix44& b, Matrix44* result);
432 static void Multiply(const Matrix44& a, const Vec4& vec, Vec4* result);
433
434 // For when a vec4 isn't needed a multiplication function that takes a Vec3 and w:
435 Vec3 Transform(const Vec3& point, float w) const;
436
437 float Determinant() const;
438 Matrix44 Inverted() const;
439 Matrix44 Transposed() const;
440
441 Matrix44& operator*=(const Matrix44& rhs)
442 {
443 Multiply(*this, rhs, this);
444 return *this;
445 }
446
447 // Note: Row-major storage order.
448 std::array<float, 16> data;
449 };
450
451 inline Matrix44 operator*(Matrix44 lhs, const Matrix44& rhs)
452 {
453 return lhs *= rhs;
454 }
455
456 inline Vec4 operator*(const Matrix44& lhs, Vec4 rhs)
457 {
458 Matrix44::Multiply(lhs, rhs, &rhs);
459 return rhs;
460 }
461
462 } // namespace Common