Fx2D
A C++20 2D rigid-body physics engine
Loading...
Searching...
No Matches
Math.h
Go to the documentation of this file.
1#pragma once
2#include <algorithm>
3#include <cmath>
4#include <concepts>
5#include <cstddef>
6#include <cstdint>
7#include <initializer_list>
8#include <iostream>
9#include <limits>
10#include <memory>
11#include <new>
12#include <numbers>
13#include <stdexcept>
14#include <string>
15#include <type_traits>
16#include <utility>
17#include <vector>
18
19// Constant representing positive infinity
20static constexpr float FxInfinityf = std::numeric_limits<float>::infinity();
21static constexpr double FxInfinityd = std::numeric_limits<double>::infinity();
22
23// Constant representing pi
24static constexpr float FxPif = std::numbers::pi_v<float>;
25static constexpr double FxPid = std::numbers::pi_v<double>;
26
27// Wrap into [-pi, pi). In-range values pass through untouched (avoids float floor near pi).
28static inline float FxAngleWrap(float angle) {
29 if (angle >= -FxPif && angle < FxPif) return angle;
30 angle = std::fmod(angle + FxPif, 2.0f * FxPif);
31 if (angle < 0.0f) angle += 2.0f * FxPif;
32 return angle - FxPif;
33}
34
35// Add inc to a float accumulator in double, keeping whatever did not survive the rounding in
36// carry so the next call folds it back in. Returns the delta that actually landed.
37static inline float FxCarryAdd(float& dst, double inc, double& carry) {
38 double s = (double)dst + inc + carry;
39 float f = (float)s;
40 carry = s - (double)f;
41 float d = f - dst;
42 dst = f;
43 return d;
44}
45
46// sin and cos of one angle from + - * / alone, so every platform computes the same bits; C
47// libraries' sin and cos differ in the last bit. Taylor series about the nearest multiple of
48// pi/2, accurate to the last bit of a double.
49static inline void FxSinCos(double angle, double& s, double& c) {
50 if (!std::isfinite(angle)) {
51 s = c = std::numeric_limits<double>::quiet_NaN();
52 return;
53 }
54 if (std::fabs(angle) > 1e9) angle = std::fmod(angle, 2.0 * FxPid);
55 // pi/2 in two parts: k * kPio2Hi is exact for any wrapped angle, so reduction loses nothing.
56 constexpr double kTwoOverPi = 0.63661977236758134308;
57 constexpr double kPio2Hi = 1.57079632673412561417;
58 constexpr double kPio2Lo = 6.07710050650619224932e-11;
59 // Taylor coefficients (-1)^k / n!: odd n for sin from r^3, even n for cos from r^2.
60 // clang-format off
61 constexpr double kSin[] = {-1.0 / 6, 1.0 / 120, -1.0 / 5040, 1.0 / 362880, -1.0 / 39916800,
62 1.0 / 6227020800, -1.0 / 1307674368000, 1.0 / 355687428096000};
63 constexpr double kCos[] = {-1.0 / 2, 1.0 / 24, -1.0 / 720, 1.0 / 40320, -1.0 / 3628800,
64 1.0 / 479001600, -1.0 / 87178291200, 1.0 / 20922789888000,
65 -1.0 / 6402373705728000};
66 // clang-format on
67 const double t = angle * kTwoOverPi;
68 const long long n = static_cast<long long>(t >= 0.0 ? t + 0.5 : t - 0.5);
69 const double k = static_cast<double>(n);
70 const double r = (angle - k * kPio2Hi) - k * kPio2Lo;
71 const double r2 = r * r;
72 double ps = kSin[7];
73 for (int i = 6; i >= 0; --i)
74 ps = kSin[i] + r2 * ps;
75 double pc = kCos[8];
76 for (int i = 7; i >= 0; --i)
77 pc = kCos[i] + r2 * pc;
78 const double sin_r = r2 == 0.0 ? r : r + r * r2 * ps; // keeps sin(-0) = -0
79 const double cos_r = 1.0 + r2 * pc;
80 switch (n & 3) {
81 case 0:
82 s = sin_r;
83 c = cos_r;
84 break;
85 case 1:
86 s = cos_r;
87 c = -sin_r;
88 break;
89 case 2:
90 s = -sin_r;
91 c = -cos_r;
92 break;
93 default:
94 s = -cos_r;
95 c = sin_r;
96 break;
97 }
98}
99
100// The float version rounds the double result, so it is right to the last bit of a float.
101static inline void FxSinCos(float angle, float& s, float& c) {
102 double sd, cd;
103 FxSinCos(static_cast<double>(angle), sd, cd);
104 s = static_cast<float>(sd);
105 c = static_cast<float>(cd);
106}
107
108// concept to capture float, double, long double, int etc..
109template<typename T>
110concept Numeric = std::integral<T> || std::floating_point<T>;
111
112// ────────────────────────────────────────────────────────────────────────────
113// FxVec / FxMat: fixed-size vectors and matrices
114// ────────────────────────────────────────────────────────────────────────────
115
116// Every operation is written out element by element, so the arithmetic and the order sums are
117// taken in are fixed here, the same on every compiler and CPU.
118namespace fx_detail {
119
120// Sum of a[i] * b[i]: p0 + p1, p0 + (p1 + p2), (p0 + p2) + (p1 + p3). These are the orders
121// Eigen 3.4 produced on x86-64, so dropping Eigen moved no result.
122template<typename T, int N>
123constexpr T dot(const T* a, const T* b) noexcept {
124 if constexpr (N == 2) {
125 return a[0] * b[0] + a[1] * b[1];
126 } else if constexpr (N == 3) {
127 return a[0] * b[0] + (a[1] * b[1] + a[2] * b[2]);
128 } else {
129 static_assert(N == 4, "dot is defined for 2, 3 and 4 elements");
130 return (a[0] * b[0] + a[2] * b[2]) + (a[1] * b[1] + a[3] * b[3]);
131 }
132}
133
134// Calls f(0) ... f(N-1) as straight-line code. Element-wise operations use this, not a loop:
135// under the raised vectoriser budget GCC wraps even a two-element loop in runtime alias checks.
136template<int N, typename F>
137constexpr void unrolled(F&& f) noexcept {
138 [&]<std::size_t... I>(std::index_sequence<I...>) {
139 (f(I), ...);
140 }(std::make_index_sequence<N>{});
141}
142
143} // namespace fx_detail
144
145// N-element vector; default construction leaves it uninitialised, like a plain float. Sizes that
146// are a multiple of 16 bytes keep the 16-byte alignment Eigen gave them, so layouts are unchanged.
147template<typename T, int N>
148class alignas((sizeof(T) * N) % 16 == 0 ? 16 : alignof(T)) FxVec {
149 public:
150 FxVec() = default;
151 FxVec(const FxVec&) = default;
152 // Assigning to a temporary (pose.xy() = v) would be a lost write, so it does not compile.
153 FxVec& operator=(const FxVec&) & = default;
154
155 // Constructor from a single value (fills every component)
156 constexpr explicit FxVec(T a) noexcept {
157 fx_detail::unrolled<N>([&](std::size_t i) { m_v[i] = a; });
158 }
159
160 // Constructor from one value per component
161 template<Numeric... A>
162 requires(sizeof...(A) == N)
163 constexpr FxVec(A... values) noexcept : m_v{static_cast<T>(values)...} {}
164
165 // Component access
166 constexpr T& operator[](std::size_t i) noexcept { return m_v[i]; }
167 constexpr const T& operator[](std::size_t i) const noexcept { return m_v[i]; }
168 constexpr T* data() noexcept { return m_v; }
169 constexpr const T* data() const noexcept { return m_v; }
170
171 // Getters.
172 constexpr T& x() noexcept { return m_v[0]; }
173 constexpr T& y() noexcept { return m_v[1]; }
174 constexpr T& z() noexcept
175 requires(N >= 3)
176 {
177 return m_v[2];
178 }
179 // to use the third value as orientation
180 constexpr T& theta() noexcept
181 requires(N == 3)
182 {
183 return m_v[2];
184 }
185 constexpr T& a() noexcept
186 requires(N == 4)
187 {
188 return m_v[3];
189 }
190
191 // Const getters.
192 constexpr T x() const noexcept { return m_v[0]; }
193 constexpr T y() const noexcept { return m_v[1]; }
194 constexpr T z() const noexcept
195 requires(N >= 3)
196 {
197 return m_v[2];
198 }
199 constexpr T theta() const noexcept
200 requires(N == 3)
201 {
202 return m_v[2];
203 }
204 constexpr T a() const noexcept
205 requires(N == 4)
206 {
207 return m_v[3];
208 }
209
210 // Setters.
211 constexpr void set_x(T val) noexcept { m_v[0] = val; }
212 constexpr void set_y(T val) noexcept { m_v[1] = val; }
213 constexpr void set_z(T val) noexcept
214 requires(N >= 3)
215 {
216 m_v[2] = val;
217 }
218 // when used as orientation
219 constexpr void set_theta(T val) noexcept
220 requires(N == 3)
221 {
222 m_v[2] = val;
223 }
224 constexpr void set_a(T val) noexcept
225 requires(N == 4)
226 {
227 m_v[3] = val;
228 }
229
230 // The position part of a pose, as a copy; write it back with set_xy().
231 constexpr FxVec<T, 2> xy() const noexcept
232 requires(N == 3)
233 {
234 return FxVec<T, 2>(m_v[0], m_v[1]);
235 }
236 constexpr FxVec<T, 2> get_xy() const noexcept
237 requires(N == 3)
238 {
239 return xy();
240 }
241 constexpr void set_xy(const FxVec<T, 2>& v2) noexcept
242 requires(N == 3)
243 {
244 m_v[0] = v2.x();
245 m_v[1] = v2.y();
246 }
247
248 // Dot product, squared length and length
249 constexpr T dot(const FxVec& other) const noexcept {
250 return fx_detail::dot<T, N>(m_v, other.m_v);
251 }
252 constexpr T squaredNorm() const noexcept { return dot(*this); }
253 T norm() const noexcept
254 requires std::floating_point<T>
255 {
256 return std::sqrt(squaredNorm());
257 }
258
259 // Unit vector in the same direction; a zero vector comes back unchanged
260 FxVec normalized() const noexcept
261 requires std::floating_point<T>
262 {
263 const T sq = squaredNorm();
264 if (sq > T(0)) return *this / std::sqrt(sq);
265 return *this;
266 }
267
268 // Cross product with another 2D vector (returns scalar)
269 constexpr T cross(const FxVec& other) const noexcept
270 requires(N == 2)
271 {
272 return x() * other.y() - y() * other.x();
273 }
274
275 // Perpendicular vectors
276 constexpr FxVec perp() const noexcept
277 requires(N == 2)
278 {
279 return FxVec(-y(), x()); // CCW perpendicular
280 }
281 constexpr FxVec perpCW() const noexcept
282 requires(N == 2)
283 {
284 return FxVec(y(), -x()); // CW perpendicular
285 }
286
287 // Radian‐based rotate (suffix "_rad")
289 requires(N == 2 && std::floating_point<T>)
290 {
291 T s, c;
292 FxSinCos(theta, s, c);
293 T xi = x(), yi = y();
294 set_x(xi * c - yi * s);
295 set_y(xi * s + yi * c);
296 return *this;
297 }
298
299 // Degree‐based rotate (default "rotate_inplace" uses degrees)
300 FxVec& rotate_inplace(T degrees) & noexcept
301 requires(N == 2 && std::floating_point<T>)
302 {
303 constexpr T FX_DEG2RAD = std::numbers::pi_v<T> / T(180);
304 return rotate_inplace_rad(degrees * FX_DEG2RAD);
305 }
306
307 // — non-mutating rotation: returns a rotated copy
308 FxVec rotate(T degrees) const noexcept
309 requires(N == 2 && std::floating_point<T>)
310 {
311 FxVec r = *this;
312 r.rotate_inplace(degrees);
313 return r;
314 }
315 FxVec rotate_rad(T theta) const noexcept
316 requires(N == 2 && std::floating_point<T>)
317 {
318 FxVec r = *this;
320 return r;
321 }
322
323 // In-place operations with another vector (component-wise)
324 constexpr FxVec& operator+=(const FxVec& rhs) & noexcept {
325 fx_detail::unrolled<N>([&](std::size_t i) { m_v[i] += rhs.m_v[i]; });
326 return *this;
327 }
328 constexpr FxVec& operator-=(const FxVec& rhs) & noexcept {
329 fx_detail::unrolled<N>([&](std::size_t i) { m_v[i] -= rhs.m_v[i]; });
330 return *this;
331 }
332 constexpr FxVec& operator*=(const FxVec& rhs) & noexcept {
333 fx_detail::unrolled<N>([&](std::size_t i) { m_v[i] *= rhs.m_v[i]; });
334 return *this;
335 }
336 constexpr FxVec& operator/=(const FxVec& rhs) & noexcept {
337 fx_detail::unrolled<N>([&](std::size_t i) { m_v[i] /= rhs.m_v[i]; });
338 return *this;
339 }
340
341 // In-place scalar operations with generic Numeric s
342 template<Numeric S>
343 constexpr FxVec& operator+=(S s) & noexcept {
344 fx_detail::unrolled<N>([&](std::size_t i) { m_v[i] += static_cast<T>(s); });
345 return *this;
346 }
347 template<Numeric S>
348 constexpr FxVec& operator-=(S s) & noexcept {
349 fx_detail::unrolled<N>([&](std::size_t i) { m_v[i] -= static_cast<T>(s); });
350 return *this;
351 }
352 template<Numeric S>
353 constexpr FxVec& operator*=(S s) & noexcept {
354 fx_detail::unrolled<N>([&](std::size_t i) { m_v[i] *= static_cast<T>(s); });
355 return *this;
356 }
357 template<Numeric S>
358 constexpr FxVec& operator/=(S s) & noexcept {
359 fx_detail::unrolled<N>([&](std::size_t i) { m_v[i] /= static_cast<T>(s); });
360 return *this;
361 }
362
363 // Vector operations. There is no vector * vector: use dot(), cross() or *=.
364 friend constexpr FxVec operator+(FxVec lhs, const FxVec& rhs) noexcept { return lhs += rhs; }
365 friend constexpr FxVec operator-(FxVec lhs, const FxVec& rhs) noexcept { return lhs -= rhs; }
366 friend constexpr FxVec operator-(FxVec v) noexcept {
367 fx_detail::unrolled<N>([&](std::size_t i) { v.m_v[i] = -v.m_v[i]; });
368 return v;
369 }
370 friend constexpr bool operator==(const FxVec& lhs, const FxVec& rhs) noexcept {
371 bool equal = true;
372 fx_detail::unrolled<N>([&](std::size_t i) { equal = equal && lhs.m_v[i] == rhs.m_v[i]; });
373 return equal;
374 }
375
376 // Scalar operations with generic Numeric s
377 template<Numeric S>
378 friend constexpr FxVec operator*(FxVec v, S s) noexcept {
379 return v *= s;
380 }
381 template<Numeric S>
382 friend constexpr FxVec operator*(S s, FxVec v) noexcept {
383 return v *= s;
384 }
385 template<Numeric S>
386 friend constexpr FxVec operator/(FxVec v, S s) noexcept {
387 return v /= s;
388 }
389 template<Numeric S>
390 friend constexpr FxVec operator+(FxVec v, S s) noexcept {
391 return v += s;
392 }
393 template<Numeric S>
394 friend constexpr FxVec operator+(S s, FxVec v) noexcept {
395 return v += s;
396 }
397 template<Numeric S>
398 friend constexpr FxVec operator-(FxVec v, S s) noexcept {
399 return v -= s;
400 }
401 template<Numeric S>
402 friend constexpr FxVec operator-(S s, FxVec v) noexcept {
403 fx_detail::unrolled<N>([&](std::size_t i) { v.m_v[i] = static_cast<T>(s) - v.m_v[i]; });
404 return v;
405 }
406 template<Numeric S>
407 friend constexpr FxVec operator/(S s, FxVec v) noexcept {
408 fx_detail::unrolled<N>([&](std::size_t i) { v.m_v[i] = static_cast<T>(s) / v.m_v[i]; });
409 return v;
410 }
411
412 private:
413 T m_v[N];
414};
415
423
424static_assert(std::is_trivially_copyable_v<FxVec2f> &&
425 std::is_trivially_default_constructible_v<FxVec2f>);
426static_assert(sizeof(FxVec3f) == 12 && alignof(FxVec4f) == 16);
427
428// N x N matrix, stored column by column; default construction leaves it uninitialised.
429template<typename T, int N>
430class alignas((sizeof(T) * N * N) % 16 == 0 ? 16 : alignof(T)) FxMat {
431 public:
432 FxMat() = default;
433 FxMat(const FxMat&) = default;
434 FxMat& operator=(const FxMat&) & = default;
435
436 constexpr T& operator()(int row, int col) noexcept { return m_a[col * N + row]; }
437 constexpr const T& operator()(int row, int col) const noexcept { return m_a[col * N + row]; }
438 constexpr T* data() noexcept { return m_a; }
439 constexpr const T* data() const noexcept { return m_a; }
440
441 // Products: each element is a fixed-order dot of a row and a column (see fx_detail::dot)
442 friend constexpr FxVec<T, N> operator*(const FxMat& m, const FxVec<T, N>& v) noexcept {
443 FxVec<T, N> out;
444 for (int row = 0; row < N; ++row)
445 out.data()[row] = fx_detail::dot<T, N>(m.row(row).data(), v.data());
446 return out;
447 }
448 friend constexpr FxMat operator*(const FxMat& lhs, const FxMat& rhs) noexcept {
449 FxMat out;
450 for (int row = 0; row < N; ++row) {
451 const FxVec<T, N> r = lhs.row(row);
452 for (int col = 0; col < N; ++col)
453 out(row, col) = fx_detail::dot<T, N>(r.data(), &rhs.m_a[col * N]);
454 }
455 return out;
456 }
457
458 protected:
459 constexpr FxVec<T, N> row(int r) const noexcept {
460 FxVec<T, N> out;
461 for (int col = 0; col < N; ++col)
462 out.data()[col] = (*this)(r, col);
463 return out;
464 }
465
466 private:
467 T m_a[N * N];
468};
469
470class FxMat2f : public FxMat<float, 2> {
471 public:
472 using FxMat<float, 2>::FxMat;
473 FxMat2f() = default;
474 FxMat2f(const FxMat<float, 2>& m) : FxMat<float, 2>(m) {}
475
476 float& a() { return (*this)(0, 0); }
477 float& b() { return (*this)(0, 1); }
478 float& c() { return (*this)(1, 0); }
479 float& d() { return (*this)(1, 1); }
480
481 // Const getters.
482 float a() const { return (*this)(0, 0); }
483 float b() const { return (*this)(0, 1); }
484 float c() const { return (*this)(1, 0); }
485 float d() const { return (*this)(1, 1); }
486
487 void set_a(float val) { (*this)(0, 0) = val; }
488 void set_b(float val) { (*this)(0, 1) = val; }
489 void set_c(float val) { (*this)(1, 0) = val; }
490 void set_d(float val) { (*this)(1, 1) = val; }
491
492 // The inverse (transpose) of the rotation matrix.
494 FxMat2f t;
495 t.set_a(a());
496 t.set_b(c());
497 t.set_c(b());
498 t.set_d(d());
499 return t;
500 }
501};
502
503class FxMat3f : public FxMat<float, 3> {
504 public:
505 using FxMat<float, 3>::FxMat;
506 FxMat3f() = default;
507 FxMat3f(const FxMat<float, 3>& m) : FxMat<float, 3>(m) {}
508
509 // Accessors for each element of the 3x3 matrix
510 // Row 0
511 float& a() { return (*this)(0, 0); }
512 float& b() { return (*this)(0, 1); }
513 float& c() { return (*this)(0, 2); }
514 // Row 1
515 float& d() { return (*this)(1, 0); }
516 float& e() { return (*this)(1, 1); }
517 float& f() { return (*this)(1, 2); }
518 // Row 2
519 float& g() { return (*this)(2, 0); }
520 float& h() { return (*this)(2, 1); }
521 float& i() { return (*this)(2, 2); }
522
523 // Const getters.
524 float a() const { return (*this)(0, 0); }
525 float b() const { return (*this)(0, 1); }
526 float c() const { return (*this)(0, 2); }
527 float d() const { return (*this)(1, 0); }
528 float e() const { return (*this)(1, 1); }
529 float f() const { return (*this)(1, 2); }
530 float g() const { return (*this)(2, 0); }
531 float h() const { return (*this)(2, 1); }
532 float i() const { return (*this)(2, 2); }
533
534 // Setters for each element of the 3x3 matrix
535 // Row 0
536 void set_a(float val) { (*this)(0, 0) = val; }
537 void set_b(float val) { (*this)(0, 1) = val; }
538 void set_c(float val) { (*this)(0, 2) = val; }
539 // Row 1
540 void set_d(float val) { (*this)(1, 0) = val; }
541 void set_e(float val) { (*this)(1, 1) = val; }
542 void set_f(float val) { (*this)(1, 2) = val; }
543 // Row 2
544 void set_g(float val) { (*this)(2, 0) = val; }
545 void set_h(float val) { (*this)(2, 1) = val; }
546 void set_i(float val) { (*this)(2, 2) = val; }
547
548 // Upper-left 2x2 rotation of a homogeneous transform [R t; 0 1].
549 FxMat2f Rot() const {
550 FxMat2f R;
551 R.set_a(a());
552 R.set_b(b());
553 R.set_c(d());
554 R.set_d(e());
555 return R;
556 }
557 // Extract the translation vector (first two elements of the third column) as an FxVec2f
558 FxVec2f t() const { return FxVec2f((*this)(0, 2), (*this)(1, 2)); }
559
560 // set the rotation part from an FxMat2f
561 void set_Rot(const FxMat2f& R) {
562 (*this)(0, 0) = R(0, 0);
563 (*this)(0, 1) = R(0, 1);
564 (*this)(1, 0) = R(1, 0);
565 (*this)(1, 1) = R(1, 1);
566 }
567
568 // set the translation part from an FxVec2f
569 void set_t(const FxVec2f& trans) {
570 (*this)(0, 2) = trans.x();
571 (*this)(1, 2) = trans.y();
572 }
573
574 // Compute and return the inverse transformation.
576 FxMat3f inv; // This will store the inverse.
577
578 // Compute the rotation transpose (which is the inverse of a rotation).
579 inv(0, 0) = (*this)(0, 0);
580 inv(0, 1) = (*this)(1, 0);
581 inv(1, 0) = (*this)(0, 1);
582 inv(1, 1) = (*this)(1, 1);
583
584 // Compute the new translation vector as -Rᵀt.
585 float t0 = (*this)(0, 2); // original translation x value.
586 float t1 = (*this)(1, 2); // original translation y value.
587 inv(0, 2) = -(inv(0, 0) * t0 + inv(0, 1) * t1);
588 inv(1, 2) = -(inv(1, 0) * t0 + inv(1, 1) * t1);
589
590 // Set the homogeneous row.
591 inv(2, 0) = 0;
592 inv(2, 1) = 0;
593 inv(2, 2) = 1;
594
595 return inv;
596 }
597};
598
599static_assert(alignof(FxMat2f) == 16 && sizeof(FxMat3f) == 36);
600
601// ────────────────────────────────────────────────────────────────────────────
602// FxArray: numpy style array
603// ────────────────────────────────────────────────────────────────────────────
604
605// 1) define the concepts
606template<typename T>
608 std::integral<T> // all integer types
609 || std::floating_point<T> // float, double, long double
610 || std::same_as<T, FxVec2f> || std::same_as<T, FxVec3f> || std::same_as<T, FxVec4f>;
611// || std::same_as<T, FxVecXf>;
612
613template<typename T>
614concept FxVecT = std::same_as<T, FxVec2f> || std::same_as<T, FxVec3f> || std::same_as<T, FxVec4f>;
615
616template<typename U, typename T>
617concept ConvertibleOrNumeric = std::convertible_to<U, T> || Numeric<U>;
618
619// Custom deleter for aligned arrays
620template<class T>
622 std::size_t align;
623 void operator()(T* p) const noexcept { ::operator delete[](p, std::align_val_t(align)); }
624};
625
626// Helper function to create aligned arrays
627template<typename T>
628static std::unique_ptr<T[], FxArrayAlignedDelete<T>> FxArray_make_aligned(std::size_t n,
629 std::size_t align = 32) {
630 if (n == 0) return {nullptr, FxArrayAlignedDelete<T>{align}};
631 // allocation (ctors run)
632 T* p = static_cast<T*>(::operator new[](n * sizeof(T), std::align_val_t(align)));
633 return std::unique_ptr<T[], FxArrayAlignedDelete<T>>(p, FxArrayAlignedDelete<T>{align});
634}
635
636// define the FxArray class template
637template<NumericOrFxVec T>
638class FxArray {
639 private:
640 static constexpr std::size_t kAlign = 32; // compile-time alignment
641 std::size_t m_size = 0;
642 std::unique_ptr<T[], FxArrayAlignedDelete<T>> m_arr;
643
644 protected:
645 // throws if index is out of bounds
646 template<std::integral I>
647 std::size_t checkIndex(I idx) const {
648 auto i = static_cast<long long>(idx); // signed capture for error text
649 auto u = static_cast<std::size_t>(i); // cast for bounds test
650 if (i < 0 || u >= m_size) {
651 throw std::out_of_range("FxArray::[] index " + std::to_string(i) +
652 " out of range for [0," + std::to_string(m_size) + ")");
653 }
654 return u;
655 }
656
657 // throws if empty
658 void throw_if_empty(char const* what) const {
659 if (m_size == 0)
660 throw std::runtime_error(std::string("FxArray::") + what + " on empty array");
661 }
662
663 // throws if size mismatch
664 void throw_if_size_mismatch(char const* what, size_t o_size) const {
665 if (m_size != o_size)
666 throw std::invalid_argument(std::string("FxArray::operator") + what + " size mismatch");
667 }
668
669 // single scan to find (index, value) of best element
670 template<typename Compare>
671 std::pair<std::size_t, T> best_pair(Compare cmp, const char* name) const {
672 throw_if_empty(name);
673 T const* __restrict p = aligned_data();
674 std::size_t bestIdx = 0;
675 T bestVal = p[0];
676 for (std::size_t i = 1; i < m_size; ++i) {
677 if (cmp(p[i], bestVal)) {
678 bestVal = p[i];
679 bestIdx = i;
680 }
681 }
682 return {bestIdx, bestVal};
683 }
684
685 public:
686 // 1) default (empty)
687 FxArray() : m_arr(nullptr, FxArrayAlignedDelete<T>{kAlign}) {}
688 // 2) n sized ctor (all zeros)
689 explicit FxArray(std::size_t n) : m_size(n), m_arr(FxArray_make_aligned<T>(n, kAlign)) {}
690
691 // 3) Dedicated init_list ctor — for braced lists
692 FxArray(std::initializer_list<T> init) : FxArray(init.size()) {
693 std::copy(init.begin(), init.end(), aligned_data());
694 }
695
696 // 4) one ctor for std::vector
697 FxArray(std::vector<T> const& v) : FxArray(v.size()) {
698 std::copy(v.begin(), v.end(), aligned_data());
699 }
700
701 // 5) one overload for c style array
702 template<std::size_t N>
703 FxArray(T const (&arr)[N]) : FxArray(N) {
704 std::copy_n(arr, N, aligned_data());
705 }
706
707 // deep‐copy copy‐ctor using copy_n
708 FxArray(FxArray const& o) : FxArray(o.m_size) {
709 std::copy_n(o.aligned_data(), m_size, aligned_data());
710 }
711
712 // move-assignment (O(1) swap of pointers and size)
713 FxArray(FxArray&&) noexcept = default;
714
715 // Default destructor is sufficient now
716 ~FxArray() = default;
717
718 // = move-assignment
719 FxArray& operator=(FxArray const& o) {
720 if (&o == this) return *this;
721 FxArray tmp(o);
722 swap(tmp);
723 return *this;
724 }
725
726 // assign from std::vector<T>
727 FxArray& operator=(std::vector<T> const& v) {
728 // reuse your vector‐ctor + swap
729 FxArray tmp(v);
730 swap(tmp);
731 return *this;
732 }
733
734 // assign from initializer_list<T>
735 FxArray& operator=(std::initializer_list<T> init) {
736 FxArray tmp(init);
737 swap(tmp);
738 return *this;
739 }
740
741 FxArray& operator=(FxArray&&) noexcept = default;
742
743 // Aligned raw-data helpers
744 T* aligned_data() noexcept { return std::assume_aligned<kAlign>(m_arr.get()); }
745 T const* aligned_data() const noexcept { return std::assume_aligned<kAlign>(m_arr.get()); }
746
747 // 1) UNCHECKED, inlined, noexcept operator[] for hot loops
748 T& operator[](size_t i) noexcept { return aligned_data()[i]; }
749 T const& operator[](size_t i) const noexcept { return aligned_data()[i]; }
750
751 template<std::integral I>
752 T& operator()(I i) noexcept {
753 return aligned_data()[static_cast<size_t>(i)];
754 }
755
756 template<std::integral I>
757 T const& operator()(I i) const noexcept {
758 return aligned_data()[static_cast<size_t>(i)];
759 }
760
761 // 2) BOUNDS-CHECKED at(), still accepts *any* integral index
762 template<std::integral I>
763 T& at(I idx) {
764 return aligned_data()[checkIndex(idx)];
765 }
766 template<std::integral I>
767 T const& at(I idx) const {
768 return aligned_data()[checkIndex(idx)];
769 }
770
771 // Iterators use aligned_data (helps vectorizers in range-for)
772 T* begin() noexcept { return aligned_data(); }
773 T const* begin() const noexcept { return aligned_data(); }
774 T* end() noexcept { return aligned_data() + m_size; }
775 T const* end() const noexcept { return aligned_data() + m_size; }
776
777 // size & raw data
778 size_t size() const noexcept { return m_size; }
779 bool empty() const noexcept { return m_size == 0; }
780 T* data() noexcept { return m_arr.get(); }
781 T const* data() const noexcept { return m_arr.get(); }
782
783 void swap(FxArray& o) noexcept {
784 std::swap(m_size, o.m_size);
785 std::swap(m_arr, o.m_arr);
786 }
787
788 template<typename U>
789 requires(Numeric<T> && Numeric<U>)
790 FxArray<U> as() const {
791 FxArray<U> result(m_size);
792 T const* __restrict src = aligned_data();
793 U* __restrict dst = result.aligned_data();
794 for (std::size_t i = 0; i < m_size; ++i)
795 dst[i] = static_cast<U>(src[i]);
796 return result;
797 }
798
799 template<typename Compare>
800 T min(Compare cmp) const {
801 return best_pair(cmp, "min").second;
802 }
803
804 template<typename Compare>
805 T max(Compare cmp) const {
806 return best_pair([&](const T& a, const T& b) { return cmp(b, a); }, "max").second;
807 }
808
809 // default operator< versions
810 T min() const { return min(std::less<T>{}); }
811 T max() const { return max(std::less<T>{}); }
812
813 // ---------- index-only -> reuse best_pair ----------
814 template<typename Compare>
815 std::pair<std::size_t, T> argmin(Compare cmp) const {
816 return best_pair(cmp, "argmin");
817 }
818
819 template<typename Compare>
820 std::pair<std::size_t, T> argmax(Compare cmp) const {
821 return best_pair([&](const T& a, const T& b) { return cmp(b, a); }, "argmax");
822 }
823
824 // default operator< versions
825 std::pair<std::size_t, T> argmin() const { return argmin(std::less<T>{}); }
826 std::pair<std::size_t, T> argmax() const { return argmax(std::less<T>{}); }
827
828 // --- mean: requires T() + T+= U + T /= scalar ---
829 T mean() const {
830 throw_if_empty("mean");
831 T const* __restrict p = aligned_data();
832 T sum = p[0]; // initialize with first element
833 for (std::size_t i = 1; i < m_size; ++i)
834 sum += p[i];
835 return sum / m_size;
836 }
837
838 // mean as float (casts each element to double for accuracy, returns float)
839 float meanf() const
840 requires Numeric<T>
841 {
842 throw_if_empty("meanf");
843 T const* __restrict p = aligned_data();
844 double sum = 0.0;
845 for (std::size_t i = 0; i < m_size; ++i)
846 sum += static_cast<double>(p[i]);
847 return static_cast<float>(sum / m_size);
848 }
849
850 // population standard deviation as float
851 float stddev() const
852 requires Numeric<T>
853 {
854 throw_if_empty("stddev");
855 double m = meanf();
856 T const* __restrict p = aligned_data();
857 double acc = 0.0;
858 for (std::size_t i = 0; i < m_size; ++i) {
859 double d = static_cast<double>(p[i]) - m;
860 acc += d * d;
861 }
862 return static_cast<float>(std::sqrt(acc / m_size));
863 }
864
865 // --- unary minus (element‐wise negate) ---
867 FxArray result(m_size);
868 T const* __restrict src = aligned_data();
869 T* __restrict dst = result.aligned_data();
870 for (std::size_t i = 0; i < m_size; ++i)
871 dst[i] = -src[i];
872 return result;
873 }
874
875 // --- in-place with a single T ---
876 template<typename U>
878 FxArray& operator+=(U const& v) {
879 T* __restrict p = aligned_data();
880 const T vv = static_cast<T>(v);
881 for (std::size_t i = 0; i < m_size; ++i)
882 p[i] += vv;
883 return *this;
884 }
885
886 template<typename U>
888 FxArray& operator-=(U const& v) {
889 T* __restrict p = aligned_data();
890 const T vv = static_cast<T>(v);
891 for (std::size_t i = 0; i < m_size; ++i)
892 p[i] -= vv;
893 return *this;
894 }
895
896 template<typename U>
898 FxArray& operator*=(U const& v) {
899 T* __restrict p = aligned_data();
900 const T vv = static_cast<T>(v);
901 for (std::size_t i = 0; i < m_size; ++i)
902 p[i] *= vv;
903 return *this;
904 }
905
906 template<typename U>
908 FxArray& operator/=(U const& v) {
909 T* __restrict p = aligned_data();
910 const T vv = static_cast<T>(v);
911 for (std::size_t i = 0; i < m_size; ++i)
912 p[i] /= vv;
913 return *this;
914 }
915
916 // --- in-place element‐wise with another FxArray ---
917 template<typename U>
920 throw_if_size_mismatch("+=", o.size());
921 T* __restrict p = aligned_data();
922 U const* __restrict op = o.aligned_data();
923 for (std::size_t i = 0; i < m_size; ++i)
924 p[i] += op[i];
925 return *this;
926 }
927
928 template<typename U>
931 throw_if_size_mismatch("-=", o.size());
932 T* __restrict p = aligned_data();
933 U const* __restrict op = o.aligned_data();
934 for (std::size_t i = 0; i < m_size; ++i)
935 p[i] -= op[i];
936 return *this;
937 }
938
939 template<typename U>
942 throw_if_size_mismatch("*=", o.size());
943 T* __restrict p = aligned_data();
944 U const* __restrict op = o.aligned_data();
945 for (std::size_t i = 0; i < m_size; ++i)
946 p[i] *= op[i];
947 return *this;
948 }
949
950 template<typename U>
953 throw_if_size_mismatch("/=", o.size());
954 T* __restrict p = aligned_data();
955 U const* __restrict op = o.aligned_data();
956 for (std::size_t i = 0; i < m_size; ++i)
957 p[i] /= op[i];
958 return *this;
959 }
960
961 // dot with one FxVec2f → returns FxArray<float>
962 FxArray<float> dot(T const& v) const
963 requires FxVecT<T>
964 {
965 FxArray<float> out(this->size());
966 T const* __restrict p = aligned_data();
967 float* __restrict result = out.aligned_data();
968 for (size_t i = 0; i < this->size(); ++i)
969 result[i] = p[i].dot(v);
970 return out;
971 }
972
973 // element-wise dot with another FxVec2fArray
975 requires FxVecT<T>
976 {
977 this->throw_if_size_mismatch("dot", o.size());
978 FxArray<float> out(this->size());
979 T const* __restrict p = aligned_data();
980 T const* __restrict op = o.aligned_data();
981 float* __restrict result = out.aligned_data();
982 for (size_t i = 0; i < this->size(); ++i)
983 result[i] = p[i].dot(op[i]);
984 return out;
985 }
986
987 // 1) In-place rotate by radians
988 FxArray& rotate_inplace_rad(float theta_rad) noexcept
989 requires std::same_as<T, FxVec2f>
990 {
991 float s, c;
992 FxSinCos(theta_rad, s, c);
993 T* __restrict p = aligned_data();
994 for (std::size_t i = 0; i < m_size; ++i) {
995 float xi = p[i].x(), yi = p[i].y();
996 p[i].x() = xi * c - yi * s;
997 p[i].y() = xi * s + yi * c;
998 }
999 return *this;
1000 }
1001
1002 // 2) In-place rotate by degrees
1003 FxArray& rotate_inplace(float degrees) noexcept
1004 requires std::same_as<T, FxVec2f>
1005 {
1006 constexpr float FX_DEG2RAD = FxPif / 180.0f;
1007 return rotate_inplace_rad(degrees * FX_DEG2RAD);
1008 }
1009
1010 // 3) In-place rotate by degrees
1012 requires std::same_as<T, FxVec2f>
1013 {
1014 T* __restrict p = aligned_data();
1015 for (std::size_t i = 0; i < m_size; ++i)
1016 p[i] = p[i].perp();
1017 return *this;
1018 }
1019
1020 // 4) Non-mutating perp → new array
1022 requires std::same_as<T, FxVec2f>
1023 {
1024 FxArray tmp = *this;
1025 tmp.perp_inplace();
1026 return tmp;
1027 }
1028
1029 // 5) Non-mutating rotate by radians → new array
1030 FxArray rotate_rad(float theta_rad) const
1031 requires std::same_as<T, FxVec2f>
1032 {
1033 FxArray tmp = *this;
1034 tmp.rotate_inplace_rad(theta_rad);
1035 return tmp;
1036 }
1037
1038 // 6) Non-mutating rotate by degrees → new array
1039 FxArray rotate(float degrees) const
1040 requires std::same_as<T, FxVec2f>
1041 {
1042 FxArray tmp = *this;
1043 tmp.rotate_inplace(degrees);
1044 return tmp;
1045 }
1046
1048 requires std::same_as<T, FxVec2f>
1049 {
1050 this->throw_if_empty("extrema");
1051 T const* __restrict p = aligned_data();
1052 const auto& v0 = p[0];
1053 float minx = v0.x(), maxx = v0.x();
1054 float miny = v0.y(), maxy = v0.y();
1055 for (size_t i = 1, n = this->size(); i < n; ++i) {
1056 const auto& v = p[i];
1057 float x = v.x(), y = v.y();
1058 if (x < minx) minx = x;
1059 else if (x > maxx) maxx = x;
1060 if (y < miny) miny = y;
1061 else if (y > maxy) maxy = y;
1062 }
1063 return {minx, miny, maxx, maxy};
1064 }
1065};
1066
1067template<NumericOrFxVec T>
1068void swap(FxArray<T>& a, FxArray<T>& b) noexcept {
1069 a.swap(b);
1070}
1071
1072// ────────────────────────────────────────────────────────────────────────────
1073// FxVec2fArray: fixed‐size array of FxVec2f
1074// ────────────────────────────────────────────────────────────────────────────
1076
1077// Scalar–Array
1078template<typename T, typename U>
1080inline FxArray<T> operator+(FxArray<T> a, U const& scalar) {
1081 return a += scalar;
1082}
1083
1084template<typename T, typename U>
1086inline FxArray<T> operator-(FxArray<T> a, U const& scalar) {
1087 return a -= scalar;
1088}
1089
1090template<typename T, typename U>
1092inline FxArray<T> operator*(FxArray<T> a, U const& scalar) {
1093 return a *= scalar;
1094}
1095
1096template<typename T, typename U>
1098inline FxArray<T> operator/(FxArray<T> a, U const& scalar) {
1099 return a /= scalar;
1100}
1101
1102// Scalar on the left
1103template<typename T, typename U>
1105inline FxArray<T> operator+(U const& scalar, FxArray<T> a) {
1106 return a += scalar;
1107}
1108
1109template<typename T, typename U>
1111inline FxArray<T> operator*(U const& scalar, FxArray<T> a) {
1112 return a *= scalar;
1113}
1114
1115template<typename T, typename U>
1117inline FxArray<T> operator-(U const& scalar, FxArray<T> a) {
1118 return -a + scalar;
1119}
1120
1121// Scalar–Array division: scalar / array
1122template<typename T, Numeric U>
1123inline FxArray<T> operator/(U const& scalar, FxArray<T> const& a) {
1124 FxArray<T> result(a.size());
1125 T const* __restrict src = a.aligned_data();
1126 T* __restrict dst = result.aligned_data();
1127 for (size_t i = 0; i < result.size(); ++i)
1128 dst[i] = scalar / src[i];
1129 return result;
1130}
1131
1132template<typename T>
1133inline FxArray<T> operator/(T const& scalar, FxArray<T> const& a) {
1134 FxArray<T> result(a.size());
1135 T const* __restrict src = a.aligned_data();
1136 T* __restrict dst = result.aligned_data();
1137 for (size_t i = 0; i < result.size(); ++i)
1138 dst[i] = scalar / src[i];
1139 return result;
1140}
1141
1142// Array–Array
1143template<typename T>
1145 return a += b;
1146}
1147
1148template<typename T>
1150 return a -= b;
1151}
1152
1153template<typename T>
1155 return a *= b;
1156}
1157
1158template<typename T>
1160 return a /= b;
1161}
1162
1163template<Numeric T>
1164inline std::ostream& operator<<(std::ostream& os, FxArray<T> const& a) {
1165 os << "FxArray { ";
1166 for (size_t i = 0; i < a.size(); ++i)
1167 os << a[i] << ", ";
1168 os << "}";
1169 return os;
1170}
1171
1172inline std::ostream& operator<<(std::ostream& os, FxVec2fArray const& a) {
1173 os << "FxVec2fArray { ";
1174 for (size_t i = 0; i < a.size(); ++i)
1175 os << "(" << a[i].x() << " " << a[i].y() << "), ";
1176 os << "} ";
1177 return os;
1178}
1179
1180inline std::ostream& operator<<(std::ostream& os, FxVec2f const& a) {
1181 os << "FxVec2f { ";
1182 os << a.x() << " " << a.y();
1183 os << " }";
1184 return os;
1185}
1186
1187inline std::ostream& operator<<(std::ostream& os, FxVec3f const& a) {
1188 os << "FxVec3f { ";
1189 os << a.x() << " " << a.y() << " " << a.z();
1190 os << " }";
1191 return os;
1192}
@ U
Definition Input.h:16
@ S
Definition Input.h:16
@ A
Definition Input.h:15
@ F
Definition Input.h:15
@ N
Definition Input.h:16
@ T
Definition Input.h:16
@ I
Definition Input.h:15
@ R
Definition Input.h:16
FxArray< T > operator/(FxArray< T > a, U const &scalar)
Definition Math.h:1098
FxVec< float, 2 > FxVec2f
Definition Math.h:416
FxArray< T > operator*(FxArray< T > a, U const &scalar)
Definition Math.h:1092
void swap(FxArray< T > &a, FxArray< T > &b) noexcept
Definition Math.h:1068
std::ostream & operator<<(std::ostream &os, FxArray< T > const &a)
Definition Math.h:1164
FxArray< T > operator-(FxArray< T > a, U const &scalar)
Definition Math.h:1086
FxArray< FxVec2f > FxVec2fArray
Definition Math.h:1075
FxArray< T > operator+(FxArray< T > a, U const &scalar)
Definition Math.h:1080
FxVec< unsigned int, 2 > FxVec2ui
Definition Math.h:421
FxVec< uint8_t, 4 > FxVec4ui8
Definition Math.h:422
Fixed-size, aligned value array.
Definition Math.h:638
T * end() noexcept
Definition Math.h:774
T const & at(I idx) const
Definition Math.h:767
FxArray & operator=(std::vector< T > const &v)
Definition Math.h:727
FxArray & operator*=(FxArray< U > const &o)
Definition Math.h:941
FxArray< float > dot(FxArray< T > const &o) const
Definition Math.h:974
T * data() noexcept
Definition Math.h:780
T & operator()(I i) noexcept
Definition Math.h:752
void throw_if_empty(char const *what) const
Definition Math.h:658
FxArray & rotate_inplace_rad(float theta_rad) noexcept
Definition Math.h:988
T const & operator[](size_t i) const noexcept
Definition Math.h:749
FxArray & operator-=(FxArray< U > const &o)
Definition Math.h:930
std::pair< std::size_t, T > best_pair(Compare cmp, const char *name) const
Definition Math.h:671
T max() const
Definition Math.h:811
FxArray & operator=(FxArray &&) noexcept=default
T & at(I idx)
Definition Math.h:763
FxArray & operator-=(U const &v)
Definition Math.h:888
FxArray operator-() const
Definition Math.h:866
FxArray & perp_inplace() noexcept
Definition Math.h:1011
void throw_if_size_mismatch(char const *what, size_t o_size) const
Definition Math.h:664
FxArray rotate(float degrees) const
Definition Math.h:1039
T & operator[](size_t i) noexcept
Definition Math.h:748
T mean() const
Definition Math.h:829
T * begin() noexcept
Definition Math.h:772
T const * end() const noexcept
Definition Math.h:775
FxArray & operator/=(FxArray< U > const &o)
Definition Math.h:952
FxArray rotate_rad(float theta_rad) const
Definition Math.h:1030
bool empty() const noexcept
Definition Math.h:779
FxArray(FxArray &&) noexcept=default
FxArray(std::size_t n)
Definition Math.h:689
T min(Compare cmp) const
Definition Math.h:800
std::pair< std::size_t, T > argmin() const
Definition Math.h:825
FxArray & operator*=(U const &v)
Definition Math.h:898
T const * begin() const noexcept
Definition Math.h:773
FxArray perp() const
Definition Math.h:1021
T * aligned_data() noexcept
Definition Math.h:744
T const & operator()(I i) const noexcept
Definition Math.h:757
FxArray & operator/=(U const &v)
Definition Math.h:908
FxArray & operator+=(FxArray< U > const &o)
Definition Math.h:919
FxArray< float > dot(T const &v) const
Definition Math.h:962
void swap(FxArray &o) noexcept
Definition Math.h:783
std::size_t checkIndex(I idx) const
Definition Math.h:647
FxArray(T const (&arr)[N])
Definition Math.h:703
std::pair< std::size_t, T > argmin(Compare cmp) const
Definition Math.h:815
FxArray & operator=(std::initializer_list< T > init)
Definition Math.h:735
T const * data() const noexcept
Definition Math.h:781
T max(Compare cmp) const
Definition Math.h:805
size_t size() const noexcept
Definition Math.h:778
FxArray & rotate_inplace(float degrees) noexcept
Definition Math.h:1003
std::pair< std::size_t, T > argmax() const
Definition Math.h:826
FxArray(std::initializer_list< T > init)
Definition Math.h:692
float meanf() const
Definition Math.h:839
FxArray(std::vector< T > const &v)
Definition Math.h:697
FxArray & operator+=(U const &v)
Definition Math.h:878
T const * aligned_data() const noexcept
Definition Math.h:745
FxArray(FxArray const &o)
Definition Math.h:708
FxArray< U > as() const
Definition Math.h:790
std::pair< std::size_t, T > argmax(Compare cmp) const
Definition Math.h:820
FxArray< float > bounds() const
Definition Math.h:1047
T min() const
Definition Math.h:810
FxArray()
Definition Math.h:687
float stddev() const
Definition Math.h:851
2×2 floating-point matrix.
Definition Math.h:470
float & d()
Definition Math.h:479
void set_a(float val)
Definition Math.h:487
float & b()
Definition Math.h:477
void set_d(float val)
Definition Math.h:490
FxMat2f()=default
float & a()
Definition Math.h:476
float & c()
Definition Math.h:478
FxMat2f(const FxMat< float, 2 > &m)
Definition Math.h:474
float d() const
Definition Math.h:485
void set_b(float val)
Definition Math.h:488
float c() const
Definition Math.h:484
float b() const
Definition Math.h:483
FxMat2f inv_rotation() const
Definition Math.h:493
void set_c(float val)
Definition Math.h:489
float a() const
Definition Math.h:482
3×3 floating-point matrix.
Definition Math.h:503
float & g()
Definition Math.h:519
float & b()
Definition Math.h:512
void set_e(float val)
Definition Math.h:541
float h() const
Definition Math.h:531
FxMat3f inv_transform() const
Definition Math.h:575
float & f()
Definition Math.h:517
FxMat2f Rot() const
Definition Math.h:549
FxVec2f t() const
Definition Math.h:558
float & e()
Definition Math.h:516
float & c()
Definition Math.h:513
float & a()
Definition Math.h:511
void set_g(float val)
Definition Math.h:544
float & h()
Definition Math.h:520
float i() const
Definition Math.h:532
float g() const
Definition Math.h:530
void set_Rot(const FxMat2f &R)
Definition Math.h:561
float c() const
Definition Math.h:526
float b() const
Definition Math.h:525
float & d()
Definition Math.h:515
void set_b(float val)
Definition Math.h:537
void set_f(float val)
Definition Math.h:542
FxMat3f(const FxMat< float, 3 > &m)
Definition Math.h:507
float a() const
Definition Math.h:524
float f() const
Definition Math.h:529
float & i()
Definition Math.h:521
float d() const
Definition Math.h:527
float e() const
Definition Math.h:528
void set_h(float val)
Definition Math.h:545
void set_d(float val)
Definition Math.h:540
FxMat3f()=default
void set_a(float val)
Definition Math.h:536
void set_t(const FxVec2f &trans)
Definition Math.h:569
void set_i(float val)
Definition Math.h:546
void set_c(float val)
Definition Math.h:538
constexpr const T & operator()(int row, int col) const noexcept
Definition Math.h:437
friend constexpr FxMat operator*(const FxMat &lhs, const FxMat &rhs) noexcept
Definition Math.h:448
FxMat(const FxMat &)=default
constexpr FxVec< T, N > row(int r) const noexcept
Definition Math.h:459
constexpr const T * data() const noexcept
Definition Math.h:439
FxMat & operator=(const FxMat &) &=default
constexpr T * data() noexcept
Definition Math.h:438
FxMat()=default
constexpr T & operator()(int row, int col) noexcept
Definition Math.h:436
friend constexpr FxVec< T, N > operator*(const FxMat &m, const FxVec< T, N > &v) noexcept
Definition Math.h:442
Double-precision 2D vector.
Single-precision 2D vector.
Double-precision three-component vector.
Single-precision three-component vector, including poses.
Four-component floating-point vector.
Definition Math.h:148
constexpr T x() const noexcept
Definition Math.h:192
constexpr FxVec & operator+=(S s) &noexcept
Definition Math.h:343
constexpr FxVec & operator/=(S s) &noexcept
Definition Math.h:358
constexpr T theta() const noexcept
Definition Math.h:199
constexpr void set_y(T val) noexcept
Definition Math.h:212
friend constexpr FxVec operator*(FxVec v, S s) noexcept
Definition Math.h:378
FxVec normalized() const noexcept
Definition Math.h:260
constexpr T a() const noexcept
Definition Math.h:204
constexpr FxVec & operator/=(const FxVec &rhs) &noexcept
Definition Math.h:336
constexpr void set_x(T val) noexcept
Definition Math.h:211
constexpr T y() const noexcept
Definition Math.h:193
constexpr FxVec & operator-=(const FxVec &rhs) &noexcept
Definition Math.h:328
constexpr FxVec & operator+=(const FxVec &rhs) &noexcept
Definition Math.h:324
FxVec rotate(T degrees) const noexcept
Definition Math.h:308
constexpr void set_z(T val) noexcept
Definition Math.h:213
constexpr T & y() noexcept
Definition Math.h:173
friend constexpr FxVec operator-(FxVec v) noexcept
Definition Math.h:366
FxVec & rotate_inplace(T degrees) &noexcept
Definition Math.h:300
FxVec & operator=(const FxVec &) &=default
friend constexpr FxVec operator+(FxVec lhs, const FxVec &rhs) noexcept
Definition Math.h:364
friend constexpr FxVec operator-(FxVec lhs, const FxVec &rhs) noexcept
Definition Math.h:365
friend constexpr bool operator==(const FxVec &lhs, const FxVec &rhs) noexcept
Definition Math.h:370
constexpr void set_theta(T val) noexcept
Definition Math.h:219
friend constexpr FxVec operator-(S s, FxVec v) noexcept
Definition Math.h:402
constexpr T & x() noexcept
Definition Math.h:172
constexpr FxVec & operator*=(S s) &noexcept
Definition Math.h:353
constexpr void set_a(T val) noexcept
Definition Math.h:224
friend constexpr FxVec operator-(FxVec v, S s) noexcept
Definition Math.h:398
constexpr T & z() noexcept
Definition Math.h:174
constexpr FxVec & operator*=(const FxVec &rhs) &noexcept
Definition Math.h:332
T norm() const noexcept
Definition Math.h:253
constexpr T & theta() noexcept
Definition Math.h:180
friend constexpr FxVec operator/(FxVec v, S s) noexcept
Definition Math.h:386
FxVec & rotate_inplace_rad(T theta) &noexcept
Definition Math.h:288
constexpr FxVec(T a) noexcept
Definition Math.h:156
constexpr T * data() noexcept
Definition Math.h:168
constexpr T z() const noexcept
Definition Math.h:194
FxVec rotate_rad(T theta) const noexcept
Definition Math.h:315
friend constexpr FxVec operator+(FxVec v, S s) noexcept
Definition Math.h:390
constexpr T dot(const FxVec &other) const noexcept
Definition Math.h:249
constexpr T squaredNorm() const noexcept
Definition Math.h:252
constexpr float & a() noexcept
Definition Math.h:185
friend constexpr FxVec operator*(S s, FxVec v) noexcept
Definition Math.h:382
constexpr FxVec perp() const noexcept
Definition Math.h:276
constexpr FxVec & operator-=(S s) &noexcept
Definition Math.h:348
FxVec(const FxVec &)=default
constexpr const T & operator[](std::size_t i) const noexcept
Definition Math.h:167
constexpr FxVec perpCW() const noexcept
Definition Math.h:281
constexpr FxVec< T, 2 > xy() const noexcept
Definition Math.h:231
FxVec()=default
constexpr T cross(const FxVec &other) const noexcept
Definition Math.h:269
constexpr FxVec(A... values) noexcept
Definition Math.h:163
constexpr T & operator[](std::size_t i) noexcept
Definition Math.h:166
constexpr void set_xy(const FxVec< T, 2 > &v2) noexcept
Definition Math.h:241
friend constexpr FxVec operator/(S s, FxVec v) noexcept
Definition Math.h:407
constexpr FxVec< T, 2 > get_xy() const noexcept
Definition Math.h:236
friend constexpr FxVec operator+(S s, FxVec v) noexcept
Definition Math.h:394
constexpr const T * data() const noexcept
Definition Math.h:169
Definition Math.h:614
constexpr void unrolled(F &&f) noexcept
Definition Math.h:137
constexpr T dot(const T *a, const T *b) noexcept
Definition Math.h:123
std::size_t align
Definition Math.h:622
void operator()(T *p) const noexcept
Definition Math.h:623