GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_MATH_HPP
5#define GREM_CORE_MATH_HPP
6
8
15
16#include <cmath> // std::...
17#include <numbers> // std::numbers::...
18#include <type_traits> // std::conditional_t, std::remove_cvref_t, std::is_constant_evaluated
19
20namespace grem {
21
22template <typename T>
23struct Formatter;
24
25namespace numbers {
26
27using std::numbers::e_v;
28using std::numbers::egamma_v;
29using std::numbers::inv_pi_v;
30using std::numbers::inv_sqrt3_v;
31using std::numbers::inv_sqrtpi_v;
32using std::numbers::ln10_v;
33using std::numbers::ln2_v;
34using std::numbers::log10e_v;
35using std::numbers::log2e_v;
36using std::numbers::phi_v;
37using std::numbers::pi_v;
38using std::numbers::sqrt2_v;
39using std::numbers::sqrt3_v;
40
41inline constexpr float E = e_v<float>;
42inline constexpr float LOG2E = log2e_v<float>;
43inline constexpr float LOG10E = log10e_v<float>;
44inline constexpr float PI = pi_v<float>;
45inline constexpr float INV_PI = inv_pi_v<float>;
46inline constexpr float INV_SQRTPI = inv_sqrtpi_v<float>;
47inline constexpr float LN2 = ln2_v<float>;
48inline constexpr float LN10 = ln10_v<float>;
49inline constexpr float SQRT2 = sqrt2_v<float>;
50inline constexpr float SQRT3 = sqrt3_v<float>;
51inline constexpr float INV_SQRT3 = inv_sqrt3_v<float>;
52inline constexpr float EGAMMA = egamma_v<float>;
53inline constexpr float PHI = phi_v<float>;
54
55} // namespace numbers
56
57template <size_t N, typename T>
58struct vec;
59
60template <typename T>
61struct vec<1, T> {
62 using Component = T;
63 static constexpr size_t RANK = 1;
64
65 T x{};
66
67 GREM_ALWAYS_INLINE constexpr vec() noexcept = default;
68
69 template <typename U>
70 GREM_ALWAYS_INLINE constexpr explicit vec(vec<1, U> v) noexcept
71 : x(static_cast<T>(v.x)) {}
72
73 GREM_ALWAYS_INLINE constexpr vec(T value) noexcept
74 : x(value) {}
75
76 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(const vec&) const noexcept = default;
77
78 [[nodiscard]] GREM_ALWAYS_INLINE constexpr Component& operator[](size_t i) {
79 GREM_ASSERT(i < RANK);
80 if (std::is_constant_evaluated()) {
81 switch (i) {
82 case 0: return x;
83 default: unreachable();
84 }
85 } else {
86 return x;
87 }
88 }
89
90 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const Component& operator[](size_t i) const {
91 GREM_ASSERT(i < RANK);
92 if (std::is_constant_evaluated()) {
93 switch (i) {
94 case 0: return x;
95 default: unreachable();
96 }
97 } else {
98 return x;
99 }
100 }
101
102 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_t size() const noexcept {
103 return 1;
104 }
105};
106
107template <typename T>
108struct vec<2, T> {
109 using Component = T;
110 static constexpr size_t RANK = 2;
111
112 T x{};
113 T y{};
114
115 GREM_ALWAYS_INLINE constexpr vec() noexcept = default;
116
117 template <typename U>
118 GREM_ALWAYS_INLINE constexpr explicit vec(vec<2, U> v) noexcept
119 : x(static_cast<T>(v.x))
120 , y(static_cast<T>(v.y)) {}
121
122 GREM_ALWAYS_INLINE constexpr explicit vec(T value) noexcept
123 : x(value)
124 , y(value) {}
125
126 GREM_ALWAYS_INLINE constexpr vec(T x, T y) noexcept
127 : x(x)
128 , y(y) {}
129
130 template <typename U>
131 GREM_ALWAYS_INLINE constexpr explicit operator vec<1, U>() const noexcept {
132 return vec<1, U>{static_cast<U>(x)};
133 }
134
135 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(const vec&) const noexcept = default;
136
137 [[nodiscard]] GREM_ALWAYS_INLINE constexpr Component& operator[](size_t i) {
138 GREM_ASSERT(i < RANK);
139 if (std::is_constant_evaluated()) {
140 switch (i) {
141 case 0: return x;
142 case 1: return y;
143 default: unreachable();
144 }
145 } else {
146 return reinterpret_cast<Component*>(this)[i];
147 }
148 }
149
150 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const Component& operator[](size_t i) const {
151 GREM_ASSERT(i < RANK);
152 if (std::is_constant_evaluated()) {
153 switch (i) {
154 case 0: return x;
155 case 1: return y;
156 default: unreachable();
157 }
158 } else {
159 return reinterpret_cast<const Component*>(this)[i];
160 }
161 }
162
163 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_t size() const noexcept {
164 return 2;
165 }
166};
167
168template <typename T>
169struct vec<3, T> {
170 using Component = T;
171 static constexpr size_t RANK = 3;
172
173 T x{};
174 T y{};
175 T z{};
176
177 GREM_ALWAYS_INLINE constexpr vec() noexcept = default;
178
179 template <typename U>
180 GREM_ALWAYS_INLINE constexpr explicit vec(vec<3, U> v) noexcept
181 : x(static_cast<T>(v.x))
182 , y(static_cast<T>(v.y))
183 , z(static_cast<T>(v.z)) {}
184
185 GREM_ALWAYS_INLINE constexpr explicit vec(T value) noexcept
186 : x(value)
187 , y(value)
188 , z(value) {}
189
190 GREM_ALWAYS_INLINE constexpr vec(T x, T y, T z) noexcept
191 : x(x)
192 , y(y)
193 , z(z) {}
194
195 GREM_ALWAYS_INLINE constexpr vec(vec<2, T> xy, T z) noexcept
196 : x(xy.x)
197 , y(xy.y)
198 , z(z) {}
199
200 GREM_ALWAYS_INLINE constexpr vec(T x, vec<2, T> yz) noexcept
201 : x(x)
202 , y(yz.x)
203 , z(yz.y) {}
204
205 template <typename U>
206 GREM_ALWAYS_INLINE constexpr explicit operator vec<1, U>() const noexcept {
207 return vec<1, U>{static_cast<U>(x)};
208 }
209
210 template <typename U>
211 GREM_ALWAYS_INLINE constexpr explicit operator vec<2, U>() const noexcept {
212 return vec<2, U>{static_cast<U>(x), static_cast<U>(y)};
213 }
214
215 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(const vec&) const noexcept = default;
216
217 [[nodiscard]] GREM_ALWAYS_INLINE constexpr Component& operator[](size_t i) {
218 GREM_ASSERT(i < RANK);
219 if (std::is_constant_evaluated()) {
220 switch (i) {
221 case 0: return x;
222 case 1: return y;
223 case 2: return z;
224 default: unreachable();
225 }
226 } else {
227 return reinterpret_cast<Component*>(this)[i];
228 }
229 }
230
231 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const Component& operator[](size_t i) const {
232 GREM_ASSERT(i < RANK);
233 if (std::is_constant_evaluated()) {
234 switch (i) {
235 case 0: return x;
236 case 1: return y;
237 case 2: return z;
238 default: unreachable();
239 }
240 } else {
241 return reinterpret_cast<const Component*>(this)[i];
242 }
243 }
244
245 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_t size() const noexcept {
246 return 3;
247 }
248};
249
250template <typename T>
251struct vec<4, T> {
252 using Component = T;
253 static constexpr size_t RANK = 4;
254
255 T x{};
256 T y{};
257 T z{};
258 T w{};
259
260 GREM_ALWAYS_INLINE constexpr vec() noexcept = default;
261
262 template <typename U>
263 GREM_ALWAYS_INLINE constexpr explicit vec(vec<4, U> v) noexcept
264 : x(static_cast<T>(v.x))
265 , y(static_cast<T>(v.y))
266 , z(static_cast<T>(v.z))
267 , w(static_cast<T>(v.w)) {}
268
269 GREM_ALWAYS_INLINE constexpr explicit vec(T value) noexcept
270 : x(value)
271 , y(value)
272 , z(value)
273 , w(value) {}
274
275 GREM_ALWAYS_INLINE constexpr vec(T x, T y, T z, T w) noexcept
276 : x(x)
277 , y(y)
278 , z(z)
279 , w(w) {}
280
281 GREM_ALWAYS_INLINE constexpr vec(vec<3, T> xyz, T w) noexcept
282 : x(xyz.x)
283 , y(xyz.y)
284 , z(xyz.z)
285 , w(w) {}
286
287 GREM_ALWAYS_INLINE constexpr vec(T x, vec<3, T> yzw) noexcept
288 : x(x)
289 , y(yzw.x)
290 , z(yzw.y)
291 , w(yzw.z) {}
292
293 GREM_ALWAYS_INLINE constexpr vec(vec<2, T> xy, vec<2, T> zw) noexcept
294 : x(xy.x)
295 , y(xy.y)
296 , z(zw.x)
297 , w(zw.y) {}
298
299 GREM_ALWAYS_INLINE constexpr vec(vec<2, T> xy, T z, T w) noexcept
300 : x(xy.x)
301 , y(xy.y)
302 , z(z)
303 , w(w) {}
304
305 GREM_ALWAYS_INLINE constexpr vec(T x, vec<2, T> yz, T w) noexcept
306 : x(x)
307 , y(yz.x)
308 , z(yz.y)
309 , w(w) {}
310
311 GREM_ALWAYS_INLINE constexpr vec(T x, T y, vec<2, T> zw) noexcept
312 : x(x)
313 , y(y)
314 , z(zw.x)
315 , w(zw.y) {}
316
317 template <typename U>
318 GREM_ALWAYS_INLINE constexpr explicit operator vec<1, U>() const noexcept {
319 return vec<1, U>{static_cast<U>(x)};
320 }
321
322 template <typename U>
323 GREM_ALWAYS_INLINE constexpr explicit operator vec<2, U>() const noexcept {
324 return vec<2, U>{static_cast<U>(x), static_cast<U>(y)};
325 }
326
327 template <typename U>
328 GREM_ALWAYS_INLINE constexpr explicit operator vec<3, U>() const noexcept {
329 return vec<3, U>{static_cast<U>(x), static_cast<U>(y), static_cast<U>(z)};
330 }
331
332 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(const vec&) const noexcept = default;
333
334 [[nodiscard]] GREM_ALWAYS_INLINE constexpr Component& operator[](size_t i) {
335 GREM_ASSERT(i < RANK);
336 if (std::is_constant_evaluated()) {
337 switch (i) {
338 case 0: return x;
339 case 1: return y;
340 case 2: return z;
341 case 3: return w;
342 default: unreachable();
343 }
344 } else {
345 return reinterpret_cast<Component*>(this)[i];
346 }
347 }
348
349 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const Component& operator[](size_t i) const {
350 GREM_ASSERT(i < RANK);
351 if (std::is_constant_evaluated()) {
352 switch (i) {
353 case 0: return x;
354 case 1: return y;
355 case 2: return z;
356 case 3: return w;
357 default: unreachable();
358 }
359 } else {
360 return reinterpret_cast<const Component*>(this)[i];
361 }
362 }
363
364 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_t size() const noexcept {
365 return 4;
366 }
367};
368
473
475 static constexpr size_t RANK = 4;
476
477 uint32_t _private_value{};
478
479 GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm() noexcept = default;
480
482 : _private_value(encode(value)) {}
483
484 template <typename U>
485 GREM_ALWAYS_INLINE constexpr explicit iA2B10G10R10vec4norm(vec<4, U> v) noexcept
487
488 GREM_ALWAYS_INLINE constexpr explicit iA2B10G10R10vec4norm(float value) noexcept
489 : iA2B10G10R10vec4norm(vec4{value}) {}
490
491 GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(float x, float y, float z, float w) noexcept
492 : iA2B10G10R10vec4norm(vec4{x, y, z, w}) {}
493
494 GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(vec3 xyz, float w) noexcept
495 : iA2B10G10R10vec4norm(vec4{xyz, w}) {}
496
497 GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(float x, vec3 yzw) noexcept
498 : iA2B10G10R10vec4norm(vec4{x, yzw}) {}
499
501 : iA2B10G10R10vec4norm(vec4{xy, zw}) {}
502
503 GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(vec2 xy, float z, float w) noexcept
504 : iA2B10G10R10vec4norm(vec4{xy, z, w}) {}
505
506 GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(float x, vec2 yz, float w) noexcept
507 : iA2B10G10R10vec4norm(vec4{x, yz, w}) {}
508
509 GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(float x, float y, vec2 zw) noexcept
510 : iA2B10G10R10vec4norm(vec4{x, y, zw}) {}
511
512 template <typename U>
513 GREM_ALWAYS_INLINE constexpr explicit operator vec<1, U>() const noexcept {
514 return vec<1, U>{vec4{*this}};
515 }
516
517 template <typename U>
518 GREM_ALWAYS_INLINE constexpr explicit operator vec<2, U>() const noexcept {
519 return vec<2, U>{vec4{*this}};
520 }
521
522 template <typename U>
523 GREM_ALWAYS_INLINE constexpr explicit operator vec<3, U>() const noexcept {
524 return vec<3, U>{vec4{*this}};
525 }
526
527 template <typename U>
528 GREM_ALWAYS_INLINE constexpr explicit operator vec<4, U>() const noexcept requires(!same_as<U, float>) {
529 return vec<4, U>{vec4{*this}};
530 }
531
532 GREM_ALWAYS_INLINE constexpr operator vec4() const noexcept {
533 return decode(_private_value);
534 }
535
536 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(const iA2B10G10R10vec4norm&) const noexcept = default;
537
538 [[nodiscard]] GREM_ALWAYS_INLINE constexpr float operator[](size_t i) const {
539 return decode(_private_value)[i];
540 }
541
542 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_t size() const noexcept {
543 return 4;
544 }
545
546private:
547 [[nodiscard]] static constexpr uint32_t encode(vec4 value) noexcept {
548 float x = clamp(value.x, -1.0f, 1.0f) * 511.0f;
549 float y = clamp(value.y, -1.0f, 1.0f) * 511.0f;
550 float z = clamp(value.z, -1.0f, 1.0f) * 511.0f;
551 float w = clamp(value.w, -1.0f, 1.0f);
552 x += (value.x < 0.0f) ? -0.5f : 0.5f;
553 y += (value.y < 0.0f) ? -0.5f : 0.5f;
554 z += (value.z < 0.0f) ? -0.5f : 0.5f;
555 w += (value.w < 0.0f) ? -0.5f : 0.5f;
556 return (bit_cast<uint32_t>(static_cast<int32_t>(x)) & 0b1111111111) | //
557 ((bit_cast<uint32_t>(static_cast<int32_t>(y)) & 0b1111111111) << 10) | //
558 ((bit_cast<uint32_t>(static_cast<int32_t>(z)) & 0b1111111111) << 20) | //
559 ((bit_cast<uint32_t>(static_cast<int32_t>(w)) & 0b11) << 30);
560 }
561
562 [[nodiscard]] static constexpr vec4 decode(uint32_t value) noexcept {
563 uint32_t x = static_cast<uint32_t>(value & 0b1111111111);
564 uint32_t y = static_cast<uint32_t>((value >> 10) & 0b1111111111);
565 uint32_t z = static_cast<uint32_t>((value >> 20) & 0b1111111111);
566 uint32_t w = static_cast<uint32_t>((value >> 30) & 0b11);
567 x |= uint32_t{0} - (x & 0b1000000000);
568 y |= uint32_t{0} - (y & 0b1000000000);
569 z |= uint32_t{0} - (z & 0b1000000000);
570 w |= uint32_t{0} - (w & 0b10);
571 return {
572 static_cast<float>(bit_cast<int32_t>(x)) / 511.0f,
573 static_cast<float>(bit_cast<int32_t>(y)) / 511.0f,
574 static_cast<float>(bit_cast<int32_t>(z)) / 511.0f,
575 static_cast<float>(bit_cast<int32_t>(w)),
576 };
577 }
578};
579
581 static constexpr size_t RANK = 4;
582
583 uint32_t _private_value{};
584
585 GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm() noexcept = default;
586
588 : _private_value(encode(value)) {}
589
590 template <typename U>
591 GREM_ALWAYS_INLINE constexpr explicit uA2B10G10R10vec4norm(vec<4, U> v) noexcept
593
594 GREM_ALWAYS_INLINE constexpr explicit uA2B10G10R10vec4norm(float value) noexcept
595 : uA2B10G10R10vec4norm(vec4{value}) {}
596
597 GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(float x, float y, float z, float w) noexcept
598 : uA2B10G10R10vec4norm(vec4{x, y, z, w}) {}
599
600 GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(vec3 xyz, float w) noexcept
601 : uA2B10G10R10vec4norm(vec4{xyz, w}) {}
602
603 GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(float x, vec3 yzw) noexcept
604 : uA2B10G10R10vec4norm(vec4{x, yzw}) {}
605
607 : uA2B10G10R10vec4norm(vec4{xy, zw}) {}
608
609 GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(vec2 xy, float z, float w) noexcept
610 : uA2B10G10R10vec4norm(vec4{xy, z, w}) {}
611
612 GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(float x, vec2 yz, float w) noexcept
613 : uA2B10G10R10vec4norm(vec4{x, yz, w}) {}
614
615 GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(float x, float y, vec2 zw) noexcept
616 : uA2B10G10R10vec4norm(vec4{x, y, zw}) {}
617
618 GREM_ALWAYS_INLINE constexpr explicit operator vec1() const noexcept {
619 return vec1{vec4{*this}};
620 }
621
622 GREM_ALWAYS_INLINE constexpr explicit operator vec2() const noexcept {
623 return vec2{vec4{*this}};
624 }
625
626 GREM_ALWAYS_INLINE constexpr explicit operator vec3() const noexcept {
627 return vec3{vec4{*this}};
628 }
629
630 GREM_ALWAYS_INLINE constexpr operator vec4() const noexcept {
631 return decode(_private_value);
632 }
633
634 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(const uA2B10G10R10vec4norm&) const noexcept = default;
635
636 [[nodiscard]] GREM_ALWAYS_INLINE constexpr float operator[](size_t i) const {
637 return decode(_private_value)[i];
638 }
639
640 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_t size() const noexcept {
641 return 4;
642 }
643
644private:
645 [[nodiscard]] static constexpr uint32_t encode(vec4 value) noexcept {
646 return (static_cast<uint32_t>(clamp(value.x, 0.0f, 1.0f) * 1023.0f)) | //
647 (static_cast<uint32_t>(clamp(value.y, 0.0f, 1.0f) * 1023.0f) << 10) | //
648 (static_cast<uint32_t>(clamp(value.z, 0.0f, 1.0f) * 1023.0f) << 20) | //
649 (static_cast<uint32_t>(clamp(value.w, 0.0f, 1.0f) * 3.0f) << 30);
650 }
651
652 [[nodiscard]] static constexpr vec4 decode(uint32_t value) noexcept {
653 return {
654 static_cast<float>(value & 0b1111111111) / 1023.0f,
655 static_cast<float>((value >> 10) & 0b1111111111) / 1023.0f,
656 static_cast<float>((value >> 20) & 0b1111111111) / 1023.0f,
657 static_cast<float>((value >> 30) & 0b11) / 3.0f,
658 };
659 }
660};
661
662template <size_t C, size_t R, typename T>
663struct mat {
665 static constexpr size_t RANK = C;
666
667 vec<R, T> _private_value[C]{};
668
669 GREM_ALWAYS_INLINE constexpr mat() noexcept = default;
670
671 template <typename U>
672 GREM_ALWAYS_INLINE constexpr explicit mat(const mat<C, R, U>& m) noexcept {
673 for (size_t i = 0; i < C; ++i) {
674 _private_value[i] = vec<R, T>{m[i]};
675 }
676 }
677
678 GREM_ALWAYS_INLINE constexpr explicit mat(T value) noexcept requires(C == R) {
679 for (size_t i = 0; i < C; ++i) {
680 _private_value[i][i] = value;
681 }
682 }
683
684 GREM_ALWAYS_INLINE constexpr mat(T ix, T iy, T jx, T jy) noexcept requires(C == 2 && R == 2)
685 : _private_value{{ix, iy}, {jx, jy}} {}
686
687 GREM_ALWAYS_INLINE constexpr mat(T ix, T iy, T iz, T jx, T jy, T jz, T kx, T ky, T kz) noexcept requires(C == 3 && R == 3)
688 : _private_value{{ix, iy, iz}, {jx, jy, jz}, {kx, ky, kz}} {}
689
690 GREM_ALWAYS_INLINE constexpr mat(T ix, T iy, T iz, T iw, T jx, T jy, T jz, T jw, T kx, T ky, T kz, T kw, T lx, T ly, T lz, T lw) noexcept requires(C == 4 && R == 4)
691 : _private_value{{ix, iy, iz, iw}, {jx, jy, jz, jw}, {kx, ky, kz, kw}, {lx, ly, lz, lw}} {}
692
693 GREM_ALWAYS_INLINE constexpr explicit mat(vec<1, T> i) noexcept requires(C == 1 && R == 1)
694 : _private_value{i} {}
695
696 GREM_ALWAYS_INLINE constexpr mat(vec<2, T> i, vec<2, T> j) noexcept requires(C == 2 && R == 2)
697 : _private_value{i, j} {}
698
699 GREM_ALWAYS_INLINE constexpr mat(vec<3, T> i, vec<3, T> j, vec<3, T> k) noexcept requires(C == 3 && R == 3)
700 : _private_value{i, j, k} {}
701
702 GREM_ALWAYS_INLINE constexpr mat(vec<4, T> i, vec<4, T> j, vec<4, T> k, vec<4, T> l) noexcept requires(C == 4 && R == 4)
703 : _private_value{i, j, k, l} {}
704
705 template <size_t N>
706 GREM_ALWAYS_INLINE constexpr mat(const mat<N, N, T>& m) noexcept requires(C == R && C > N) {
707 for (size_t y = 0; y < N; ++y) {
708 for (size_t x = 0; x < N; ++x) {
709 _private_value[y][x] = m[y][x];
710 }
711 }
712 for (size_t i = N; i < C; ++i) {
713 _private_value[i][i] = T{1};
714 }
715 }
716
717 GREM_ALWAYS_INLINE constexpr explicit operator mat<1, 1, T>() const noexcept requires(C == R && C > 1) {
718 return mat<1, 1, T>{
719 _private_value[0][0],
720 };
721 }
722
723 GREM_ALWAYS_INLINE constexpr explicit operator mat<2, 2, T>() const noexcept requires(C == R && C > 2) {
724 return mat<2, 2, T>{
725 _private_value[0][0],
726 _private_value[0][1],
727 _private_value[1][0],
728 _private_value[1][1],
729 };
730 }
731
732 GREM_ALWAYS_INLINE constexpr explicit operator mat<3, 3, T>() const noexcept requires(C == R && C > 3) {
733 return mat<3, 3, T>{
734 _private_value[0][0],
735 _private_value[0][1],
736 _private_value[0][2],
737 _private_value[1][0],
738 _private_value[1][1],
739 _private_value[1][2],
740 _private_value[2][0],
741 _private_value[2][1],
742 _private_value[2][2],
743 };
744 }
745
746 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(const mat&) const noexcept = default;
747
748 [[nodiscard]] GREM_ALWAYS_INLINE constexpr Component& operator[](size_t i) {
749 GREM_ASSERT(i < RANK);
750 return _private_value[i];
751 }
752
753 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const Component& operator[](size_t i) const {
754 GREM_ASSERT(i < RANK);
755 return _private_value[i];
756 }
757
758 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_t size() const noexcept {
759 return C;
760 }
761};
762
823
824template <typename T>
825struct qua {
826 using Component = T;
827 static constexpr size_t RANK = 4;
828
829 T x{};
830 T y{};
831 T z{};
832 T w{};
833
834 GREM_ALWAYS_INLINE constexpr qua() noexcept = default;
835
836 template <typename U>
837 GREM_ALWAYS_INLINE constexpr explicit qua(qua<U> q) noexcept
838 : x(static_cast<T>(q.x))
839 , y(static_cast<T>(q.y))
840 , z(static_cast<T>(q.z))
841 , w(static_cast<T>(q.w)) {}
842
843 GREM_ALWAYS_INLINE constexpr qua(T x, T y, T z, T w) noexcept
844 : x(x)
845 , y(y)
846 , z(z)
847 , w(w) {}
848
849 GREM_ALWAYS_INLINE constexpr qua(vec<3, T> xyz, T w) noexcept
850 : x(xyz.x)
851 , y(xyz.y)
852 , z(xyz.z)
853 , w(w) {}
854
855 [[nodiscard]] GREM_ALWAYS_INLINE constexpr bool operator==(const qua&) const noexcept = default;
856
857 [[nodiscard]] GREM_ALWAYS_INLINE constexpr Component& operator[](size_t i) {
858 GREM_ASSERT(i < RANK);
859 if (std::is_constant_evaluated()) {
860 switch (i) {
861 case 0: return x;
862 case 1: return y;
863 case 2: return z;
864 case 3: return w;
865 default: unreachable();
866 }
867 } else {
868 return reinterpret_cast<Component*>(this)[i];
869 }
870 }
871
872 [[nodiscard]] GREM_ALWAYS_INLINE constexpr const Component& operator[](size_t i) const {
873 GREM_ASSERT(i < RANK);
874 if (std::is_constant_evaluated()) {
875 switch (i) {
876 case 0: return x;
877 case 1: return y;
878 case 2: return z;
879 case 3: return w;
880 default: unreachable();
881 }
882 } else {
883 return reinterpret_cast<const Component*>(this)[i];
884 }
885 }
886
887 [[nodiscard]] GREM_ALWAYS_INLINE constexpr size_t size() const noexcept {
888 return 4;
889 }
890};
891
897
898// Vector overloads of functions from fundamentals:
899
908template <size_t N, typename T>
910 vec<N, T> result;
911 for (size_t i = 0; i < N; ++i) {
912 result[i] = midpoint(a[i], b[i]);
913 }
914 return result;
915}
916
926template <typename T>
927[[nodiscard]] GREM_ALWAYS_INLINE constexpr T select(bool condition, T ifTrue, T ifFalse) {
928 // clang-format off
929 using Mask =
930 std::conditional_t<sizeof(T) == sizeof(uint8_t), uint8_t,
931 std::conditional_t<sizeof(T) == sizeof(uint16_t), uint16_t,
932 std::conditional_t<sizeof(T) == sizeof(uint32_t), uint32_t,
933 uint64_t>>>;
934 // clang-format on
935 if constexpr (sizeof(Mask) == sizeof(T)) {
936 const Mask mask = static_cast<Mask>(condition) - Mask{1};
937 const Mask ifTrueMask = bit_cast<Mask>(ifTrue);
938 const Mask ifFalseMask = bit_cast<Mask>(ifFalse);
939 return bit_cast<T>((mask & ifFalseMask) | (~mask & ifTrueMask));
940 } else {
941 return (condition) ? ifTrue : ifFalse;
942 }
943}
944
955template <size_t N, typename T>
956[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> select(vec<N, bool> conditions, vec<N, T> ifTrue, vec<N, T> ifFalse) {
957 // clang-format off
958 using Mask =
959 std::conditional_t<sizeof(T) == sizeof(uint8_t), uint8_t,
960 std::conditional_t<sizeof(T) == sizeof(uint16_t), uint16_t,
961 std::conditional_t<sizeof(T) == sizeof(uint32_t), uint32_t,
962 uint64_t>>>;
963 // clang-format on
964 if constexpr (sizeof(Mask) == sizeof(T)) {
965 const vec<N, Mask> mask = vec<N, Mask>{conditions} - vec<N, Mask>{Mask{1}};
966 const vec<N, Mask> ifTrueMask = bit_cast<vec<N, Mask>>(ifTrue);
967 const vec<N, Mask> ifFalseMask = bit_cast<vec<N, Mask>>(ifFalse);
968 return bit_cast<vec<N, T>>((mask & ifFalseMask) | (~mask & ifTrueMask));
969 } else {
970 vec<N, T> result;
971 for (size_t i = 0; i < N; ++i) {
972 result[i] = select(conditions[i], ifTrue[i], ifFalse[i]);
973 }
974 return result;
975 }
976}
977
987template <size_t N, typename T>
988[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> min(vec<N, T> a, vec<N, T> b) {
989 vec<N, T> result;
990 for (size_t i = 0; i < N; ++i) {
991 result[i] = min(a[i], b[i]);
992 }
993 return result;
994}
995
1005template <size_t N, typename T>
1006[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> max(vec<N, T> a, vec<N, T> b) {
1007 vec<N, T> result;
1008 for (size_t i = 0; i < N; ++i) {
1009 result[i] = max(a[i], b[i]);
1010 }
1011 return result;
1012}
1013
1026template <size_t N, typename T>
1027[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> clamp(vec<N, T> values, vec<N, T> minValues, vec<N, T> maxValues) {
1028 vec<N, T> result;
1029 for (size_t i = 0; i < N; ++i) {
1030 result[i] = clamp(values[i], minValues[i], maxValues[i]);
1031 }
1032 return result;
1033}
1034
1047template <size_t N, typename T>
1048[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> clamp(vec<N, T> values, T minValue, T maxValue) {
1049 vec<N, T> result;
1050 for (size_t i = 0; i < N; ++i) {
1051 result[i] = clamp(values[i], minValue, maxValue);
1052 }
1053 return result;
1054}
1055
1056// Math functions:
1057
1058using std::abs;
1059using std::acos;
1060using std::acosh;
1061using std::asin;
1062using std::asinh;
1063using std::atan;
1064using std::atan2;
1065using std::atanh;
1066using std::cbrt;
1067using std::ceil;
1068using std::cos;
1069using std::cosh;
1070using std::erf;
1071using std::erfc;
1072using std::expm1;
1073using std::fdim;
1074using std::floor;
1075using std::fma;
1076using std::frexp;
1077using std::hypot;
1078using std::ilogb;
1079using std::isfinite;
1080using std::isinf;
1081using std::isnan;
1082using std::isnormal;
1083using std::isunordered;
1084using std::ldexp;
1085using std::lgamma;
1086using std::log;
1087using std::log10;
1088using std::log1p;
1089using std::log2;
1090using std::logb;
1091using std::lround;
1092using std::modf;
1093using std::nextafter;
1094using std::nexttoward;
1095using std::remainder;
1096using std::remquo;
1097using std::round;
1098using std::signbit;
1099using std::sin;
1100using std::sinh;
1101using std::tan;
1102using std::tanh;
1103using std::tgamma;
1104using std::trunc;
1105
1113template <typename T>
1114[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sqrt(T a) requires(floating_point<T>) {
1115 GREM_ASSERT(a >= T{0});
1116 if (std::is_constant_evaluated()) {
1117 T previous{};
1118 T current = a;
1119 while (current != previous) {
1120 previous = current;
1121 current = T{0.5} * (current + a / current);
1122 }
1123 return current;
1124 }
1125 return std::sqrt(a);
1126}
1127
1135template <typename T>
1136[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL inversesqrt(T a) requires(floating_point<T>) {
1137 GREM_ASSERT(a > T{0});
1138 return T{1} / sqrt(a);
1139}
1140
1141template <typename T>
1142[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL fract(T a) requires(floating_point<T>) {
1143 return a - floor(a);
1144}
1145
1153template <typename T>
1154[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL exp(T a) noexcept requires(floating_point<T>) {
1155 return std::exp(a);
1156}
1157
1165template <typename T>
1166[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL exp2(T a) noexcept requires(floating_point<T>) {
1167 return std::exp2(a);
1168}
1169
1178template <typename T>
1179[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL pow(T a, T b) noexcept requires(floating_point<T>) {
1180 return std::pow(a, b);
1181}
1182
1191template <typename T>
1192[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL fmod(T a, T b) noexcept requires(floating_point<T>) {
1193 return std::fmod(a, b);
1194}
1195
1203template <typename T>
1205 return static_cast<T>(T{0} < a) - static_cast<T>(a < T{0});
1206}
1207
1217template <typename T>
1218[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL wrap(T value, T limit) noexcept requires(strict_arithmetic<T>) {
1219 GREM_ASSERT(limit > T{0});
1220 if constexpr (floating_point<T>) {
1221 return value - floor(value / limit) * limit;
1222 } else if constexpr (unsigned_integral<T>) {
1223 return static_cast<T>(value % limit);
1224 } else {
1225 return static_cast<T>((value % limit + limit) % limit);
1226 }
1227}
1228
1229template <typename T>
1230[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL equal(T a, T b) requires(strict_arithmetic<T>) {
1231 return a == b;
1232}
1233
1234template <typename T>
1235[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL notEqual(T a, T b) requires(strict_arithmetic<T>) {
1236 return a != b;
1237}
1238
1239template <typename T>
1240[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL lessThan(T a, T b) requires(strict_arithmetic<T>) {
1241 return a < b;
1242}
1243
1244template <typename T>
1245[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL lessThanEqual(T a, T b) requires(strict_arithmetic<T>) {
1246 return a <= b;
1247}
1248
1249template <typename T>
1250[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL greaterThan(T a, T b) requires(strict_arithmetic<T>) {
1251 return a > b;
1252}
1253
1254template <typename T>
1255[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL greaterThanEqual(T a, T b) requires(strict_arithmetic<T>) {
1256 return a >= b;
1257}
1258
1259[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL not_(bool a) {
1260 return !a;
1261}
1262
1263template <size_t N>
1265 vec<N, bool> result;
1266 for (size_t i = 0; i < N; ++i) {
1267 result[i] = not_(a[i]);
1268 }
1269 return result;
1270}
1271
1272[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL any(bool a) {
1273 return a;
1274}
1275
1276template <size_t N>
1277[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL any(vec<N, bool> a) {
1278 bool result = false;
1279 for (size_t i = 0; i < N; ++i) {
1280 result |= a[i];
1281 }
1282 return result;
1283}
1284
1285[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL all(bool a) {
1286 return a;
1287}
1288
1289template <size_t N>
1290[[nodiscard]] GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL all(vec<N, bool> a) {
1291 bool result = true;
1292 for (size_t i = 0; i < N; ++i) {
1293 result &= a[i];
1294 }
1295 return result;
1296}
1297
1298#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(ResultType, function) \
1299 template <size_t N, typename T> \
1300 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, ResultType> GREM_VECTORCALL function(vec<N, T> a) requires(requires(const vec<N, T> x, const size_t i) { function(x[i]); }) \
1301 { \
1302 vec<N, ResultType> result; \
1303 for (size_t i = 0; i < N; ++i) { \
1304 result[i] = function(a[i]); \
1305 } \
1306 return result; \
1307 }
1308
1309#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(ResultType, function) \
1310 template <size_t N, typename T> \
1311 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, ResultType> GREM_VECTORCALL function(vec<N, T> a, vec<N, T> b) \
1312 requires(requires(const vec<N, T> x, const vec<N, T> y, const size_t i) { function(x[i], y[i]); }) { \
1313 vec<N, ResultType> result; \
1314 for (size_t i = 0; i < N; ++i) { \
1315 result[i] = function(a[i], b[i]); \
1316 } \
1317 return result; \
1318 }
1319
1320#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_SCALAR_OVERLOAD_OF_BINARY_MATH_FUNCTION(ResultType, function) \
1321 template <size_t N, typename T> \
1322 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, ResultType> GREM_VECTORCALL function(vec<N, T> a, T b) \
1323 requires(requires(const vec<N, T> x, const T y, const size_t i) { function(x[i], y); }) { \
1324 vec<N, ResultType> result; \
1325 for (size_t i = 0; i < N; ++i) { \
1326 result[i] = function(a[i], b); \
1327 } \
1328 return result; \
1329 }
1330
1331#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_SCALAR_X_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(ResultType, function) \
1332 template <size_t N, typename T> \
1333 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, ResultType> GREM_VECTORCALL function(T a, vec<N, T> b) \
1334 requires(requires(const T x, const vec<N, T> y, const size_t i) { function(x, y[i]); }) { \
1335 vec<N, ResultType> result; \
1336 for (size_t i = 0; i < N; ++i) { \
1337 result[i] = function(a, b[i]); \
1338 } \
1339 return result; \
1340 }
1341
1342#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_TERNARY_MATH_FUNCTION(ResultType, function) \
1343 template <size_t N, typename T> \
1344 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, ResultType> GREM_VECTORCALL function(vec<N, T> a, vec<N, T> b, vec<N, T> c) \
1345 requires(requires(const vec<N, T> x, const vec<N, T> y, const vec<N, T> z, const size_t i) { function(x[i], y[i], z[i]); }) { \
1346 vec<N, ResultType> result; \
1347 for (size_t i = 0; i < N; ++i) { \
1348 result[i] = function(a[i], b[i], c[i]); \
1349 } \
1350 return result; \
1351 }
1352
1353#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_VECTOR_X_SCALAR_OVERLOAD_OF_TERNARY_MATH_FUNCTION(ResultType, function) \
1354 template <size_t N, typename T> \
1355 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, ResultType> GREM_VECTORCALL function(vec<N, T> a, vec<N, T> b, T c) \
1356 requires(requires(const vec<N, T> x, const vec<N, T> y, const T z, const size_t i) { function(x[i], y[i], z); }) { \
1357 vec<N, ResultType> result; \
1358 for (size_t i = 0; i < N; ++i) { \
1359 result[i] = function(a[i], b[i], c); \
1360 } \
1361 return result; \
1362 }
1363
1364#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_SCALAR_X_VECTOR_OVERLOAD_OF_TERNARY_MATH_FUNCTION(ResultType, function) \
1365 template <size_t N, typename T> \
1366 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, ResultType> GREM_VECTORCALL function(vec<N, T> a, T b, vec<N, T> c) \
1367 requires(requires(const vec<N, T> x, const T y, const vec<N, T> z, const size_t i) { function(x[i], y, z[i]); }) { \
1368 vec<N, ResultType> result; \
1369 for (size_t i = 0; i < N; ++i) { \
1370 result[i] = function(a[i], b, c[i]); \
1371 } \
1372 return result; \
1373 }
1374
1375#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_SCALAR_X_SCALAR_OVERLOAD_OF_TERNARY_MATH_FUNCTION(ResultType, function) \
1376 template <size_t N, typename T> \
1377 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, ResultType> GREM_VECTORCALL function(vec<N, T> a, T b, T c) \
1378 requires(requires(const vec<N, T> x, const T y, const T z, const size_t i) { function(x[i], y, z); }) { \
1379 vec<N, ResultType> result; \
1380 for (size_t i = 0; i < N; ++i) { \
1381 result[i] = function(a[i], b, c); \
1382 } \
1383 return result; \
1384 }
1385
1386GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, abs)
1387GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, sqrt)
1388GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, inversesqrt)
1389GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, cos)
1390GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, cosh)
1391GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, acos)
1392GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, acosh)
1393GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, sin)
1394GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, sinh)
1395GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, asin)
1396GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, asinh)
1397GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, tan)
1398GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, tanh)
1399GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, atan)
1400GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, atanh)
1401GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, cbrt)
1402GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, erf)
1403GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, erfc)
1404GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, lgamma)
1405GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, tgamma)
1406GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, pow)
1407GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_SCALAR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, pow)
1408GREM_PRIVATE_DEFINE_COMPONENT_WISE_SCALAR_X_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, pow)
1409GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, exp)
1410GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, exp2)
1411GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, expm1)
1412GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, log)
1413GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, log10)
1414GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, log1p)
1415GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, log2)
1416GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, logb)
1417GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(int, ilogb)
1418GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, round)
1419GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(long, lround)
1420GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, trunc)
1421GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, floor)
1422GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, ceil)
1423GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, fract)
1424GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION(T, sign)
1425GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, fmod)
1426GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_SCALAR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, fmod)
1427GREM_PRIVATE_DEFINE_COMPONENT_WISE_SCALAR_X_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, fmod)
1428GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, wrap)
1429GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_SCALAR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, wrap)
1430GREM_PRIVATE_DEFINE_COMPONENT_WISE_SCALAR_X_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, wrap)
1431GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_TERNARY_MATH_FUNCTION(T, fma)
1432GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_VECTOR_X_SCALAR_OVERLOAD_OF_TERNARY_MATH_FUNCTION(T, fma)
1433GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_SCALAR_X_VECTOR_OVERLOAD_OF_TERNARY_MATH_FUNCTION(T, fma)
1434GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_SCALAR_X_SCALAR_OVERLOAD_OF_TERNARY_MATH_FUNCTION(T, fma)
1435GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, fdim)
1436GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_SCALAR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, fdim)
1437GREM_PRIVATE_DEFINE_COMPONENT_WISE_SCALAR_X_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(T, fdim)
1438GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(bool, equal)
1439GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(bool, notEqual)
1440GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(bool, lessThan)
1441GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(bool, lessThanEqual)
1442GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(bool, greaterThan)
1443GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION(bool, greaterThanEqual)
1444
1445#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_UNARY_MATH_FUNCTION
1446#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_OVERLOAD_OF_BINARY_MATH_FUNCTION
1447#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_VECTOR_X_SCALAR_OVERLOAD_OF_BINARY_MATH_FUNCTION
1448
1449// Basic operators:
1450
1451#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_VECTOR_OPERATORS(op) \
1452 template <typename T> \
1453 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<1, T> GREM_VECTORCALL operator op(vec<1, T> a) { \
1454 return {static_cast<T>(op a.x)}; \
1455 } \
1456 template <typename T> \
1457 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<2, T> GREM_VECTORCALL operator op(vec<2, T> a) { \
1458 return {static_cast<T>(op a.x), static_cast<T>(op a.y)}; \
1459 } \
1460 template <typename T> \
1461 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<3, T> GREM_VECTORCALL operator op(vec<3, T> a) { \
1462 return {static_cast<T>(op a.x), static_cast<T>(op a.y), static_cast<T>(op a.z)}; \
1463 } \
1464 template <typename T> \
1465 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<4, T> GREM_VECTORCALL operator op(vec<4, T> a) { \
1466 return {static_cast<T>(op a.x), static_cast<T>(op a.y), static_cast<T>(op a.z), static_cast<T>(op a.w)}; \
1467 }
1468
1469#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_OPERATORS(op) \
1470 template <typename T> \
1471 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<1, T> GREM_VECTORCALL operator op(vec<1, T> a, vec<1, T> b) { \
1472 return {static_cast<T>(a.x op b.x)}; \
1473 } \
1474 template <typename T> \
1475 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<2, T> GREM_VECTORCALL operator op(vec<2, T> a, vec<2, T> b) { \
1476 return {static_cast<T>(a.x op b.x), static_cast<T>(a.y op b.y)}; \
1477 } \
1478 template <typename T> \
1479 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<3, T> GREM_VECTORCALL operator op(vec<3, T> a, vec<3, T> b) { \
1480 return {static_cast<T>(a.x op b.x), static_cast<T>(a.y op b.y), static_cast<T>(a.z op b.z)}; \
1481 } \
1482 template <typename T> \
1483 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<4, T> GREM_VECTORCALL operator op(vec<4, T> a, vec<4, T> b) { \
1484 return {static_cast<T>(a.x op b.x), static_cast<T>(a.y op b.y), static_cast<T>(a.z op b.z), static_cast<T>(a.w op b.w)}; \
1485 }
1486
1487#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_X_SCALAR_OPERATORS(op) \
1488 template <typename T> \
1489 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<1, T> GREM_VECTORCALL operator op(vec<1, T> a, T b) requires(strict_arithmetic<T>) { \
1490 return {static_cast<T>(a.x op b)}; \
1491 } \
1492 template <typename T> \
1493 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<2, T> GREM_VECTORCALL operator op(vec<2, T> a, T b) requires(strict_arithmetic<T>) { \
1494 return {static_cast<T>(a.x op b), static_cast<T>(a.y op b)}; \
1495 } \
1496 template <typename T> \
1497 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<3, T> GREM_VECTORCALL operator op(vec<3, T> a, T b) requires(strict_arithmetic<T>) { \
1498 return {static_cast<T>(a.x op b), static_cast<T>(a.y op b), static_cast<T>(a.z op b)}; \
1499 } \
1500 template <typename T> \
1501 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<4, T> GREM_VECTORCALL operator op(vec<4, T> a, T b) requires(strict_arithmetic<T>) { \
1502 return {static_cast<T>(a.x op b), static_cast<T>(a.y op b), static_cast<T>(a.z op b), static_cast<T>(a.w op b)}; \
1503 }
1504
1505#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_VECTOR_OPERATORS(op) \
1506 template <typename T> \
1507 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<1, T> GREM_VECTORCALL operator op(T a, vec<1, T> b) requires(strict_arithmetic<T>) { \
1508 return {static_cast<T>(a op b.x)}; \
1509 } \
1510 template <typename T> \
1511 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<2, T> GREM_VECTORCALL operator op(T a, vec<2, T> b) requires(strict_arithmetic<T>) { \
1512 return {static_cast<T>(a op b.x), static_cast<T>(a op b.y)}; \
1513 } \
1514 template <typename T> \
1515 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<3, T> GREM_VECTORCALL operator op(T a, vec<3, T> b) requires(strict_arithmetic<T>) { \
1516 return {static_cast<T>(a op b.x), static_cast<T>(a op b.y), static_cast<T>(a op b.z)}; \
1517 } \
1518 template <typename T> \
1519 [[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<4, T> GREM_VECTORCALL operator op(T a, vec<4, T> b) requires(strict_arithmetic<T>) { \
1520 return {static_cast<T>(a op b.x), static_cast<T>(a op b.y), static_cast<T>(a op b.z), static_cast<T>(a op b.w)}; \
1521 }
1522
1523#define GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_OPERATORS(op) \
1524 template <typename T> \
1525 GREM_ALWAYS_INLINE constexpr vec<1, T>& GREM_VECTORCALL operator op(vec<1, T>& a, vec<1, T> b) { \
1526 a.x op b.x; \
1527 return a; \
1528 } \
1529 template <typename T> \
1530 GREM_ALWAYS_INLINE constexpr vec<2, T>& GREM_VECTORCALL operator op(vec<2, T>& a, vec<2, T> b) { \
1531 a.x op b.x; \
1532 a.y op b.y; \
1533 return a; \
1534 } \
1535 template <typename T> \
1536 GREM_ALWAYS_INLINE constexpr vec<3, T>& GREM_VECTORCALL operator op(vec<3, T>& a, vec<3, T> b) { \
1537 a.x op b.x; \
1538 a.y op b.y; \
1539 a.z op b.z; \
1540 return a; \
1541 } \
1542 template <typename T> \
1543 GREM_ALWAYS_INLINE constexpr vec<4, T>& GREM_VECTORCALL operator op(vec<4, T>& a, vec<4, T> b) { \
1544 a.x op b.x; \
1545 a.y op b.y; \
1546 a.z op b.z; \
1547 a.w op b.w; \
1548 return a; \
1549 }
1550
1551#define GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_X_SCALAR_OPERATORS(op) \
1552 template <typename T> \
1553 GREM_ALWAYS_INLINE constexpr vec<1, T>& GREM_VECTORCALL operator op(vec<1, T>& a, T b) requires(strict_arithmetic<T>) { \
1554 a.x op b; \
1555 return a; \
1556 } \
1557 template <typename T> \
1558 GREM_ALWAYS_INLINE constexpr vec<2, T>& GREM_VECTORCALL operator op(vec<2, T>& a, T b) requires(strict_arithmetic<T>) { \
1559 a.x op b; \
1560 a.y op b; \
1561 return a; \
1562 } \
1563 template <typename T> \
1564 GREM_ALWAYS_INLINE constexpr vec<3, T>& GREM_VECTORCALL operator op(vec<3, T>& a, T b) requires(strict_arithmetic<T>) { \
1565 a.x op b; \
1566 a.y op b; \
1567 a.z op b; \
1568 return a; \
1569 } \
1570 template <typename T> \
1571 GREM_ALWAYS_INLINE constexpr vec<4, T>& GREM_VECTORCALL operator op(vec<4, T>& a, T b) requires(strict_arithmetic<T>) { \
1572 a.x op b; \
1573 a.y op b; \
1574 a.z op b; \
1575 a.w op b; \
1576 return a; \
1577 }
1578
1579#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_MATRIX_OPERATORS(op) \
1580 template <size_t R, typename T> \
1581 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<1, R, T> GREM_VECTORCALL operator op(const mat<1, R, T>& a) { \
1582 return {op a[0]}; \
1583 } \
1584 template <size_t R, typename T> \
1585 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<2, R, T> GREM_VECTORCALL operator op(const mat<2, R, T>& a) { \
1586 return {op a[0], op a[1]}; \
1587 } \
1588 template <size_t R, typename T> \
1589 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<3, R, T> GREM_VECTORCALL operator op(const mat<3, R, T>& a) { \
1590 return {op a[0], op a[1], op a[2]}; \
1591 } \
1592 template <size_t R, typename T> \
1593 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<4, R, T> GREM_VECTORCALL operator op(const mat<4, R, T>& a) { \
1594 return {op a[0], op a[1], op a[2], op a[3]}; \
1595 }
1596
1597#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_MATRIX_OPERATORS(op) \
1598 template <size_t R, typename T> \
1599 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<1, R, T> GREM_VECTORCALL operator op(const mat<1, R, T>& a, const mat<1, R, T>& b) { \
1600 return {a[0] op b[0]}; \
1601 } \
1602 template <size_t R, typename T> \
1603 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<2, R, T> GREM_VECTORCALL operator op(const mat<2, R, T>& a, const mat<2, R, T>& b) { \
1604 return {a[0] op b[0], a[1] op b[1]}; \
1605 } \
1606 template <size_t R, typename T> \
1607 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<3, R, T> GREM_VECTORCALL operator op(const mat<3, R, T>& a, const mat<3, R, T>& b) { \
1608 return {a[0] op b[0], a[1] op b[1], a[2] op b[2]}; \
1609 } \
1610 template <size_t R, typename T> \
1611 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<4, R, T> GREM_VECTORCALL operator op(const mat<4, R, T>& a, const mat<4, R, T>& b) { \
1612 return {a[0] op b[0], a[1] op b[1], a[2] op b[2], a[3] op b[3]}; \
1613 }
1614
1615#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_MATRIX_X_SCALAR_OPERATORS(op) \
1616 template <size_t R, typename T> \
1617 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<1, R, T> GREM_VECTORCALL operator op(const mat<1, R, T>& a, T b) requires(strict_arithmetic<T>) { \
1618 return {a[0] op b}; \
1619 } \
1620 template <size_t R, typename T> \
1621 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<2, R, T> GREM_VECTORCALL operator op(const mat<2, R, T>& a, T b) requires(strict_arithmetic<T>) { \
1622 return {a[0] op b, a[1] op b}; \
1623 } \
1624 template <size_t R, typename T> \
1625 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<3, R, T> GREM_VECTORCALL operator op(const mat<3, R, T>& a, T b) requires(strict_arithmetic<T>) { \
1626 return {a[0] op b, a[1] op b, a[2] op b}; \
1627 } \
1628 template <size_t R, typename T> \
1629 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<4, R, T> GREM_VECTORCALL operator op(const mat<4, R, T>& a, T b) requires(strict_arithmetic<T>) { \
1630 return {a[0] op b, a[1] op b, a[2] op b, a[3] op b}; \
1631 }
1632
1633#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_MATRIX_OPERATORS(op) \
1634 template <size_t R, typename T> \
1635 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<1, R, T> GREM_VECTORCALL operator op(T a, const mat<1, R, T>& b) requires(strict_arithmetic<T>) { \
1636 return {a op b[0]}; \
1637 } \
1638 template <size_t R, typename T> \
1639 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<2, R, T> GREM_VECTORCALL operator op(T a, const mat<2, R, T>& b) requires(strict_arithmetic<T>) { \
1640 return {a op b[0], a op b[1]}; \
1641 } \
1642 template <size_t R, typename T> \
1643 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<3, R, T> GREM_VECTORCALL operator op(T a, const mat<3, R, T>& b) requires(strict_arithmetic<T>) { \
1644 return {a op b[0], a op b[1], a op b[2]}; \
1645 } \
1646 template <size_t R, typename T> \
1647 [[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<4, R, T> GREM_VECTORCALL operator op(T a, const mat<4, R, T>& b) requires(strict_arithmetic<T>) { \
1648 return {a op b[0], a op b[1], a op b[2], a op b[3]}; \
1649 }
1650
1651#define GREM_PRIVATE_DEFINE_COMPOUND_MATRIX_OPERATORS(op) \
1652 template <size_t R, typename T> \
1653 GREM_ALWAYS_INLINE constexpr mat<1, R, T>& GREM_VECTORCALL operator op(mat<1, R, T>& a, const mat<1, R, T>& b) { \
1654 a[0] op b[0]; \
1655 return a; \
1656 } \
1657 template <size_t R, typename T> \
1658 GREM_ALWAYS_INLINE constexpr mat<2, R, T>& GREM_VECTORCALL operator op(mat<2, R, T>& a, const mat<2, R, T>& b) { \
1659 a[0] op b[0]; \
1660 a[1] op b[1]; \
1661 return a; \
1662 } \
1663 template <size_t R, typename T> \
1664 GREM_ALWAYS_INLINE constexpr mat<3, R, T>& GREM_VECTORCALL operator op(mat<3, R, T>& a, const mat<3, R, T>& b) { \
1665 a[0] op b[0]; \
1666 a[1] op b[1]; \
1667 a[2] op b[2]; \
1668 return a; \
1669 } \
1670 template <size_t R, typename T> \
1671 GREM_ALWAYS_INLINE constexpr mat<4, R, T>& GREM_VECTORCALL operator op(mat<4, R, T>& a, const mat<4, R, T>& b) { \
1672 a[0] op b[0]; \
1673 a[1] op b[1]; \
1674 a[2] op b[2]; \
1675 a[3] op b[3]; \
1676 return a; \
1677 }
1678
1679#define GREM_PRIVATE_DEFINE_COMPOUND_MATRIX_X_SCALAR_OPERATORS(op) \
1680 template <size_t R, typename T> \
1681 GREM_ALWAYS_INLINE constexpr mat<1, R, T>& GREM_VECTORCALL operator op(mat<1, R, T>& a, T b) requires(strict_arithmetic<T>) { \
1682 a[0] op b; \
1683 return a; \
1684 } \
1685 template <size_t R, typename T> \
1686 GREM_ALWAYS_INLINE constexpr mat<2, R, T>& GREM_VECTORCALL operator op(mat<2, R, T>& a, T b) requires(strict_arithmetic<T>) { \
1687 a[0] op b; \
1688 a[1] op b; \
1689 return a; \
1690 } \
1691 template <size_t R, typename T> \
1692 GREM_ALWAYS_INLINE constexpr mat<3, R, T>& GREM_VECTORCALL operator op(mat<3, R, T>& a, T b) requires(strict_arithmetic<T>) { \
1693 a[0] op b; \
1694 a[1] op b; \
1695 a[2] op b; \
1696 return a; \
1697 } \
1698 template <size_t R, typename T> \
1699 GREM_ALWAYS_INLINE constexpr mat<4, R, T>& GREM_VECTORCALL operator op(mat<4, R, T>& a, T b) requires(strict_arithmetic<T>) { \
1700 a[0] op b; \
1701 a[1] op b; \
1702 a[2] op b; \
1703 a[3] op b; \
1704 return a; \
1705 }
1706
1707#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_QUATERNION_OPERATORS(op) \
1708 template <typename T> \
1709 [[nodiscard]] GREM_ALWAYS_INLINE constexpr qua<T> GREM_VECTORCALL operator op(qua<T> a) { \
1710 return {static_cast<T>(op a.x), static_cast<T>(op a.y), static_cast<T>(op a.z), static_cast<T>(op a.w)}; \
1711 }
1712
1713#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_QUATERNION_OPERATORS(op) \
1714 template <typename T> \
1715 [[nodiscard]] GREM_ALWAYS_INLINE constexpr qua<T> GREM_VECTORCALL operator op(qua<T> a, qua<T> b) { \
1716 return {static_cast<T>(a.x op b.x), static_cast<T>(a.y op b.y), static_cast<T>(a.z op b.z), static_cast<T>(a.w op b.w)}; \
1717 }
1718
1719#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_QUATERNION_X_SCALAR_OPERATORS(op) \
1720 template <typename T> \
1721 [[nodiscard]] GREM_ALWAYS_INLINE constexpr qua<T> GREM_VECTORCALL operator op(qua<T> a, T b) requires(floating_point<T>) { \
1722 return {static_cast<T>(a.x op b), static_cast<T>(a.y op b), static_cast<T>(a.z op b), static_cast<T>(a.w op b)}; \
1723 }
1724
1725#define GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_QUATERNION_OPERATORS(op) \
1726 template <typename T> \
1727 [[nodiscard]] GREM_ALWAYS_INLINE constexpr qua<T> GREM_VECTORCALL operator op(T a, qua<T> b) requires(floating_point<T>) { \
1728 return {static_cast<T>(a op b.x), static_cast<T>(a op b.y), static_cast<T>(a op b.z), static_cast<T>(a op b.w)}; \
1729 }
1730
1731#define GREM_PRIVATE_DEFINE_COMPOUND_QUATERNION_OPERATORS(op) \
1732 template <typename T> \
1733 GREM_ALWAYS_INLINE constexpr qua<T>& GREM_VECTORCALL operator op(qua<T>& a, qua<T> b) { \
1734 a.x op b.x; \
1735 a.y op b.y; \
1736 a.z op b.z; \
1737 a.w op b.w; \
1738 return a; \
1739 }
1740
1741#define GREM_PRIVATE_DEFINE_COMPOUND_QUATERNION_X_SCALAR_OPERATORS(op) \
1742 template <typename T> \
1743 GREM_ALWAYS_INLINE constexpr qua<T>& GREM_VECTORCALL operator op(qua<T>& a, T b) requires(floating_point<T>) { \
1744 a.x op b; \
1745 a.y op b; \
1746 a.z op b; \
1747 a.w op b; \
1748 return a; \
1749 }
1750
1751GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_VECTOR_OPERATORS(~)
1752GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_OPERATORS(|)
1753GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_OPERATORS(&)
1754GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_OPERATORS(^)
1755
1756GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_VECTOR_OPERATORS(+)
1757GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_MATRIX_OPERATORS(+)
1758GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_QUATERNION_OPERATORS(+)
1759
1760GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_VECTOR_OPERATORS(-)
1761GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_MATRIX_OPERATORS(-)
1762GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_QUATERNION_OPERATORS(-)
1763
1764GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_OPERATORS(+)
1765GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_MATRIX_OPERATORS(+)
1766GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_QUATERNION_OPERATORS(+)
1767
1768GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_X_SCALAR_OPERATORS(+)
1769GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_VECTOR_OPERATORS(+)
1770GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_MATRIX_X_SCALAR_OPERATORS(+)
1771GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_MATRIX_OPERATORS(+)
1772GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_QUATERNION_X_SCALAR_OPERATORS(+)
1773GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_QUATERNION_OPERATORS(+)
1774
1775GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_OPERATORS(-)
1776GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_MATRIX_OPERATORS(-)
1777GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_QUATERNION_OPERATORS(-)
1778
1779GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_X_SCALAR_OPERATORS(-)
1780GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_VECTOR_OPERATORS(-)
1781GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_MATRIX_X_SCALAR_OPERATORS(-)
1782GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_MATRIX_OPERATORS(-)
1783GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_QUATERNION_X_SCALAR_OPERATORS(-)
1784GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_QUATERNION_OPERATORS(-)
1785
1786GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_OPERATORS(*)
1787
1788GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_X_SCALAR_OPERATORS(*)
1789GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_VECTOR_OPERATORS(*)
1790GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_MATRIX_X_SCALAR_OPERATORS(*)
1791GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_MATRIX_OPERATORS(*)
1792GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_QUATERNION_X_SCALAR_OPERATORS(*)
1793GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_QUATERNION_OPERATORS(*)
1794
1795GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_OPERATORS(/)
1796
1797GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_X_SCALAR_OPERATORS(/)
1798GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_VECTOR_OPERATORS(/)
1799GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_MATRIX_X_SCALAR_OPERATORS(/)
1800GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_MATRIX_OPERATORS(/)
1801GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_QUATERNION_X_SCALAR_OPERATORS(/)
1802GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_QUATERNION_OPERATORS(/)
1803
1804GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_OPERATORS(+=)
1805GREM_PRIVATE_DEFINE_COMPOUND_MATRIX_OPERATORS(+=)
1806GREM_PRIVATE_DEFINE_COMPOUND_QUATERNION_OPERATORS(+=)
1807
1808GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_X_SCALAR_OPERATORS(+=)
1809GREM_PRIVATE_DEFINE_COMPOUND_MATRIX_X_SCALAR_OPERATORS(+=)
1810GREM_PRIVATE_DEFINE_COMPOUND_QUATERNION_X_SCALAR_OPERATORS(+=)
1811
1812GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_OPERATORS(-=)
1813GREM_PRIVATE_DEFINE_COMPOUND_MATRIX_OPERATORS(-=)
1814GREM_PRIVATE_DEFINE_COMPOUND_QUATERNION_OPERATORS(-=)
1815
1816GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_X_SCALAR_OPERATORS(-=)
1817GREM_PRIVATE_DEFINE_COMPOUND_MATRIX_X_SCALAR_OPERATORS(-=)
1818GREM_PRIVATE_DEFINE_COMPOUND_QUATERNION_X_SCALAR_OPERATORS(-=)
1819
1820GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_OPERATORS(*=)
1821
1822GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_X_SCALAR_OPERATORS(*=)
1823GREM_PRIVATE_DEFINE_COMPOUND_MATRIX_X_SCALAR_OPERATORS(*=)
1824GREM_PRIVATE_DEFINE_COMPOUND_QUATERNION_X_SCALAR_OPERATORS(*=)
1825
1826GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_OPERATORS(/=)
1827
1828GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_X_SCALAR_OPERATORS(/=)
1829GREM_PRIVATE_DEFINE_COMPOUND_MATRIX_X_SCALAR_OPERATORS(/=)
1830GREM_PRIVATE_DEFINE_COMPOUND_QUATERNION_X_SCALAR_OPERATORS(/=)
1831
1832#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_VECTOR_OPERATORS
1833#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_OPERATORS
1834#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_VECTOR_X_SCALAR_OPERATORS
1835#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_VECTOR_OPERATORS
1836#undef GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_OPERATORS
1837#undef GREM_PRIVATE_DEFINE_COMPOUND_VECTOR_X_SCALAR_OPERATORS
1838#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_MATRIX_OPERATORS
1839#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_MATRIX_OPERATORS
1840#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_MATRIX_X_SCALAR_OPERATORS
1841#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_MATRIX_OPERATORS
1842#undef GREM_PRIVATE_DEFINE_COMPOUND_MATRIX_OPERATORS
1843#undef GREM_PRIVATE_DEFINE_COMPOUND_MATRIX_X_SCALAR_OPERATORS
1844#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_UNARY_QUATERNION_OPERATORS
1845#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_QUATERNION_OPERATORS
1846#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_QUATERNION_X_SCALAR_OPERATORS
1847#undef GREM_PRIVATE_DEFINE_COMPONENT_WISE_BINARY_SCALAR_X_QUATERNION_OPERATORS
1848#undef GREM_PRIVATE_DEFINE_COMPOUND_QUATERNION_OPERATORS
1849#undef GREM_PRIVATE_DEFINE_COMPOUND_QUATERNION_X_SCALAR_OPERATORS
1850
1851// Sum of components:
1852
1853template <typename T>
1854[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sum(vec<1, T> a) {
1855 return a.x;
1856}
1857
1858template <typename T>
1859[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sum(vec<2, T> a) {
1860 return a.x + a.y;
1861}
1862
1863template <typename T>
1864[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sum(vec<3, T> a) {
1865 return a.x + a.y + a.z;
1866}
1867
1868template <typename T>
1869[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sum(vec<4, T> a) {
1870 return a.x + a.y + a.z + a.w;
1871}
1872
1873// Product of components:
1874
1875template <typename T>
1877 return a.x;
1878}
1879
1880template <typename T>
1882 return a.x * a.y;
1883}
1884
1885template <typename T>
1887 return a.x * a.y * a.z;
1888}
1889
1890template <typename T>
1892 return a.x * a.y * a.z * a.w;
1893}
1894
1895// Minimum component:
1896
1897template <typename T>
1899 return a.x;
1900}
1901
1902template <typename T>
1904 return min(a.x, a.y);
1905}
1906
1907template <typename T>
1909 return min(min(a.x, a.y), a.z);
1910}
1911
1912template <typename T>
1914 return min(min(min(a.x, a.y), a.z), a.w);
1915}
1916
1917// Maximum component:
1918
1919template <typename T>
1921 return a.x;
1922}
1923
1924template <typename T>
1926 return max(a.x, a.y);
1927}
1928
1929template <typename T>
1931 return max(max(a.x, a.y), a.z);
1932}
1933
1934template <typename T>
1936 return max(max(max(a.x, a.y), a.z), a.w);
1937}
1938
1939// Dot product:
1940
1941template <typename T>
1943 return a.x * b.x;
1944}
1945
1946template <typename T>
1948 return a.x * b.x + a.y * b.y;
1949}
1950
1951template <typename T>
1953 return a.x * b.x + a.y * b.y + a.z * b.z;
1954}
1955
1956template <typename T>
1958 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
1959}
1960
1961template <typename T>
1962[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL dot(qua<T> a, qua<T> b) {
1963 return a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
1964}
1965
1966// Squared magnitude:
1967
1968template <typename T>
1969[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length2(T a) requires(strict_arithmetic<T>) {
1970 return a * a;
1971}
1972
1973template <typename T>
1975 return a.x * a.x;
1976}
1977
1978template <typename T>
1980 return a.x * a.x + a.y * a.y;
1981}
1982
1983template <typename T>
1985 return a.x * a.x + a.y * a.y + a.z * a.z;
1986}
1987
1988template <typename T>
1990 return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
1991}
1992
1993template <typename T>
1995 return a.x * a.x + a.y * a.y + a.z * a.z + a.w * a.w;
1996}
1997
1998// Magnitude:
1999
2000template <typename T>
2001[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length(T a) requires(floating_point<T>) {
2002 if constexpr (unsigned_integral<T>) {
2003 return a;
2004 } else {
2005 return abs(a);
2006 }
2007}
2008
2009template <size_t N, typename T>
2011 if constexpr (N == 1) {
2012 return length(a.x);
2013 } else {
2014 return sqrt(length2(a));
2015 }
2016}
2017
2018template <typename T>
2019[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length(qua<T> a) requires(floating_point<T>) {
2020 return sqrt(length2(a));
2021}
2022
2023// Squared distance:
2024
2025template <typename T>
2026[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL distance2(T a, T b) requires(strict_arithmetic<T>) {
2027 return length2(a - b);
2028}
2029
2030template <size_t N, typename T>
2032 return length2(a - b);
2033}
2034
2035// Distance:
2036
2037template <typename T>
2038[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL distance(T a, T b) requires(floating_point<T>) {
2039 return length(a - b);
2040}
2041
2042template <size_t N, typename T>
2044 return length(a - b);
2045}
2046
2047// Normalization:
2048
2056template <size_t N, typename T>
2058 return a / length(a);
2059}
2060
2068template <typename T>
2070 return a / length(a);
2071}
2072
2081template <size_t N, typename T>
2083 const T lengthSquared = length2(a);
2084 if (lengthSquared > length2(Limits<T>::MACHINE_EPSILON)) {
2085 return a / sqrt(lengthSquared);
2086 }
2087 return {};
2088}
2089
2100template <typename T>
2101[[nodiscard]] GREM_ALWAYS_INLINE constexpr Optional<T> GREM_VECTORCALL tryDivide(T a, T b) requires(floating_point<T>) {
2103 return a / b;
2104 }
2105 return {};
2106}
2107
2118template <size_t N, typename T>
2121 return a / b;
2122 }
2123 return {};
2124}
2125
2126// Vector decomposition:
2127
2135template <typename T>
2137 return atan2(a.y, a.x);
2138}
2139
2148template <typename T>
2151 return atan2(a.y, a.x);
2152 }
2153 return {};
2154}
2155
2156template <size_t N, typename T>
2161
2162template <size_t N, typename T>
2164 const T magnitude = length(a);
2165 return {.magnitude = magnitude, .direction = a / magnitude};
2166}
2167
2168template <size_t N, typename T>
2170 const T magnitude = length(a);
2171 if (magnitude > Limits<T>::MACHINE_EPSILON) {
2172 return DecomposeMagnitudeDirectionResult<N, T>{.magnitude = magnitude, .direction = a / magnitude};
2173 }
2174 return {};
2175}
2176
2177template <typename T>
2182
2183template <typename T>
2185 return {.magnitude = length(a), .angle = getAngle(a)};
2186}
2187
2188template <typename T>
2190 const T magnitude = length(a);
2191 if (magnitude > Limits<T>::MACHINE_EPSILON) {
2192 return DecomposeMagnitudeAngleResult<T>{.magnitude = magnitude, .angle = getAngle(a)};
2193 }
2194 return {};
2195}
2196
2197// Vector constructors:
2198
2206template <typename T>
2208 return vec<2, T>{cos(angle), sin(angle)};
2209}
2210
2211// Matrix determinant:
2212
2213template <typename T>
2214[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL determinant(const mat<1, 1, T>& a) {
2215 return a[0][0];
2216}
2217
2218template <typename T>
2219[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL determinant(const mat<2, 2, T>& a) {
2220 return a[0][0] * a[1][1] - a[1][0] * a[0][1];
2221}
2222
2223template <typename T>
2224[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL determinant(const mat<3, 3, T>& a) {
2225 return a[0][0] * (a[1][1] * a[2][2] - a[2][1] * a[1][2]) - //
2226 a[1][0] * (a[0][1] * a[2][2] - a[2][1] * a[0][2]) + //
2227 a[2][0] * (a[0][1] * a[1][2] - a[1][1] * a[0][2]);
2228}
2229
2230template <typename T>
2231[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL determinant(const mat<4, 4, T>& a) {
2232 const T aa0 = a[2][2] * a[3][3] - a[3][2] * a[2][3];
2233 const T aa1 = a[2][1] * a[3][3] - a[3][1] * a[2][3];
2234 const T aa2 = a[2][1] * a[3][2] - a[3][1] * a[2][2];
2235 const T aa3 = a[2][0] * a[3][3] - a[3][0] * a[2][3];
2236 const T aa4 = a[2][0] * a[3][2] - a[3][0] * a[2][2];
2237 const T aa5 = a[2][0] * a[3][1] - a[3][0] * a[2][1];
2238 return a[0][0] * (a[1][1] * aa0 - a[1][2] * aa1 + a[1][3] * aa2) - //
2239 a[0][1] * (a[1][0] * aa0 - a[1][2] * aa3 + a[1][3] * aa4) + //
2240 a[0][2] * (a[1][0] * aa1 - a[1][1] * aa3 + a[1][3] * aa5) - //
2241 a[0][3] * (a[1][0] * aa2 - a[1][1] * aa4 + a[1][2] * aa5);
2242}
2243
2244// Matrix transpose:
2245
2246template <size_t C, size_t R, typename T>
2247[[nodiscard]] GREM_ALWAYS_INLINE constexpr mat<R, C, T> transpose(const mat<C, R, T>& a) {
2248 mat<R, C, T> result;
2249 for (size_t x = 0; x < R; ++x) {
2250 for (size_t y = 0; y < C; ++y) {
2251 result[x][y] = a[y][x];
2252 }
2253 }
2254 return result;
2255}
2256
2257// Quaternion conjugate:
2258
2259template <typename T>
2261 return {-a.x, -a.y, -a.z, a.w};
2262}
2263
2264// Matrix inverse:
2265
2266template <typename T>
2268 return {T{1} / a[0][0]};
2269}
2270
2271template <typename T>
2273 const mat<2, 2, T> adjugate{
2274 +a[1][1],
2275 -a[0][1],
2276 -a[1][0],
2277 +a[0][0],
2278 };
2279 return (T{1} / determinant(a)) * adjugate;
2280}
2281
2282template <typename T>
2284 const mat<3, 3, T> adjugate{
2285 +(a[1][1] * a[2][2] - a[2][1] * a[1][2]),
2286 -(a[0][1] * a[2][2] - a[2][1] * a[0][2]),
2287 +(a[0][1] * a[1][2] - a[1][1] * a[0][2]),
2288 -(a[1][0] * a[2][2] - a[2][0] * a[1][2]),
2289 +(a[0][0] * a[2][2] - a[2][0] * a[0][2]),
2290 -(a[0][0] * a[1][2] - a[1][0] * a[0][2]),
2291 +(a[1][0] * a[2][1] - a[2][0] * a[1][1]),
2292 -(a[0][0] * a[2][1] - a[2][0] * a[0][1]),
2293 +(a[0][0] * a[1][1] - a[1][0] * a[0][1]),
2294 };
2295 return (T{1} / determinant(a)) * adjugate;
2296}
2297
2298template <typename T>
2300 const T aa00 = a[2][2] * a[3][3] - a[3][2] * a[2][3];
2301 const T aa01 = a[2][1] * a[3][3] - a[3][1] * a[2][3];
2302 const T aa02 = a[2][1] * a[3][2] - a[3][1] * a[2][2];
2303 const T aa03 = a[2][0] * a[3][3] - a[3][0] * a[2][3];
2304 const T aa04 = a[2][0] * a[3][2] - a[3][0] * a[2][2];
2305 const T aa05 = a[2][0] * a[3][1] - a[3][0] * a[2][1];
2306 const T aa06 = a[1][2] * a[3][3] - a[3][2] * a[1][3];
2307 const T aa07 = a[1][1] * a[3][3] - a[3][1] * a[1][3];
2308 const T aa08 = a[1][1] * a[3][2] - a[3][1] * a[1][2];
2309 const T aa09 = a[1][0] * a[3][3] - a[3][0] * a[1][3];
2310 const T aa10 = a[1][0] * a[3][2] - a[3][0] * a[1][2];
2311 const T aa11 = a[1][0] * a[3][1] - a[3][0] * a[1][1];
2312 const T aa12 = a[1][2] * a[2][3] - a[2][2] * a[1][3];
2313 const T aa13 = a[1][1] * a[2][3] - a[2][1] * a[1][3];
2314 const T aa14 = a[1][1] * a[2][2] - a[2][1] * a[1][2];
2315 const T aa15 = a[1][0] * a[2][3] - a[2][0] * a[1][3];
2316 const T aa16 = a[1][0] * a[2][2] - a[2][0] * a[1][2];
2317 const T aa17 = a[1][0] * a[2][1] - a[2][0] * a[1][1];
2318 const mat<4, 4, T> adjugate{
2319 +(a[1][1] * aa00 - a[1][2] * aa01 + a[1][3] * aa02),
2320 -(a[0][1] * aa00 - a[0][2] * aa01 + a[0][3] * aa02),
2321 +(a[0][1] * aa06 - a[0][2] * aa07 + a[0][3] * aa08),
2322 -(a[0][1] * aa12 - a[0][2] * aa13 + a[0][3] * aa14),
2323 -(a[1][0] * aa00 - a[1][2] * aa03 + a[1][3] * aa04),
2324 +(a[0][0] * aa00 - a[0][2] * aa03 + a[0][3] * aa04),
2325 -(a[0][0] * aa06 - a[0][2] * aa09 + a[0][3] * aa10),
2326 +(a[0][0] * aa12 - a[0][2] * aa15 + a[0][3] * aa16),
2327 +(a[1][0] * aa01 - a[1][1] * aa03 + a[1][3] * aa05),
2328 -(a[0][0] * aa01 - a[0][1] * aa03 + a[0][3] * aa05),
2329 +(a[0][0] * aa07 - a[0][1] * aa09 + a[0][3] * aa11),
2330 -(a[0][0] * aa13 - a[0][1] * aa15 + a[0][3] * aa17),
2331 -(a[1][0] * aa02 - a[1][1] * aa04 + a[1][2] * aa05),
2332 +(a[0][0] * aa02 - a[0][1] * aa04 + a[0][2] * aa05),
2333 -(a[0][0] * aa08 - a[0][1] * aa10 + a[0][2] * aa11),
2334 +(a[0][0] * aa14 - a[0][1] * aa16 + a[0][2] * aa17),
2335 };
2336 const T determinant = a[0][0] * adjugate[0][0] + a[0][1] * adjugate[1][0] + a[0][2] * adjugate[2][0] + a[0][3] * adjugate[3][0];
2337 return (T{1} / determinant) * adjugate;
2338}
2339
2340// Matrix inverse transpose:
2341
2342template <typename T>
2344 return {T{1} / a[0][0]};
2345}
2346
2347template <typename T>
2349 const mat<2, 2, T> adjugate{
2350 +a[0][0],
2351 -a[0][1],
2352 -a[1][0],
2353 +a[1][1],
2354 };
2355 return (T{1} / determinant(a)) * adjugate;
2356}
2357
2358template <typename T>
2360 const mat<3, 3, T> adjugate{
2361 +(a[1][1] * a[2][2] - a[2][1] * a[1][2]),
2362 -(a[1][0] * a[2][2] - a[2][0] * a[1][2]),
2363 +(a[1][0] * a[2][1] - a[2][0] * a[1][1]),
2364 -(a[0][1] * a[2][2] - a[2][1] * a[0][2]),
2365 +(a[0][0] * a[2][2] - a[2][0] * a[0][2]),
2366 -(a[0][0] * a[2][1] - a[2][0] * a[0][1]),
2367 +(a[0][1] * a[1][2] - a[1][1] * a[0][2]),
2368 -(a[0][0] * a[1][2] - a[1][0] * a[0][2]),
2369 +(a[0][0] * a[1][1] - a[1][0] * a[0][1]),
2370 };
2371 return (T{1} / determinant(a)) * adjugate;
2372}
2373
2374template <typename T>
2376 const T aa00 = a[2][2] * a[3][3] - a[3][2] * a[2][3];
2377 const T aa01 = a[2][1] * a[3][3] - a[3][1] * a[2][3];
2378 const T aa02 = a[2][1] * a[3][2] - a[3][1] * a[2][2];
2379 const T aa03 = a[2][0] * a[3][3] - a[3][0] * a[2][3];
2380 const T aa04 = a[2][0] * a[3][2] - a[3][0] * a[2][2];
2381 const T aa05 = a[2][0] * a[3][1] - a[3][0] * a[2][1];
2382 const T aa06 = a[1][2] * a[3][3] - a[3][2] * a[1][3];
2383 const T aa07 = a[1][1] * a[3][3] - a[3][1] * a[1][3];
2384 const T aa08 = a[1][1] * a[3][2] - a[3][1] * a[1][2];
2385 const T aa09 = a[1][0] * a[3][3] - a[3][0] * a[1][3];
2386 const T aa10 = a[1][0] * a[3][2] - a[3][0] * a[1][2];
2387 const T aa11 = a[1][0] * a[3][1] - a[3][0] * a[1][1];
2388 const T aa12 = a[1][2] * a[2][3] - a[2][2] * a[1][3];
2389 const T aa13 = a[1][1] * a[2][3] - a[2][1] * a[1][3];
2390 const T aa14 = a[1][1] * a[2][2] - a[2][1] * a[1][2];
2391 const T aa15 = a[1][0] * a[2][3] - a[2][0] * a[1][3];
2392 const T aa16 = a[1][0] * a[2][2] - a[2][0] * a[1][2];
2393 const T aa17 = a[1][0] * a[2][1] - a[2][0] * a[1][1];
2394 const mat<4, 4, T> adjugate{
2395 +(a[1][1] * aa00 - a[1][2] * aa01 + a[1][3] * aa02),
2396 -(a[1][0] * aa00 - a[1][2] * aa03 + a[1][3] * aa04),
2397 +(a[1][0] * aa01 - a[1][1] * aa03 + a[1][3] * aa05),
2398 -(a[1][0] * aa02 - a[1][1] * aa04 + a[1][2] * aa05),
2399 -(a[0][1] * aa00 - a[0][2] * aa01 + a[0][3] * aa02),
2400 +(a[0][0] * aa00 - a[0][2] * aa03 + a[0][3] * aa04),
2401 -(a[0][0] * aa01 - a[0][1] * aa03 + a[0][3] * aa05),
2402 +(a[0][0] * aa02 - a[0][1] * aa04 + a[0][2] * aa05),
2403 +(a[0][1] * aa06 - a[0][2] * aa07 + a[0][3] * aa08),
2404 -(a[0][0] * aa06 - a[0][2] * aa09 + a[0][3] * aa10),
2405 +(a[0][0] * aa07 - a[0][1] * aa09 + a[0][3] * aa11),
2406 -(a[0][0] * aa08 - a[0][1] * aa10 + a[0][2] * aa11),
2407 -(a[0][1] * aa12 - a[0][2] * aa13 + a[0][3] * aa14),
2408 +(a[0][0] * aa12 - a[0][2] * aa15 + a[0][3] * aa16),
2409 -(a[0][0] * aa13 - a[0][1] * aa15 + a[0][3] * aa17),
2410 +(a[0][0] * aa14 - a[0][1] * aa16 + a[0][2] * aa17),
2411 };
2412 const T determinant = a[0][0] * adjugate[0][0] + a[0][1] * adjugate[0][1] + a[0][2] * adjugate[0][2] + a[0][3] * adjugate[0][3];
2413 return (T{1} / determinant) * adjugate;
2414}
2415
2416// Quaternion inverse:
2417
2418template <typename T>
2420 return conjugate(a) / length2(a);
2421}
2422
2423// Cross product:
2424
2425template <typename T>
2427 return a.x * b.y - b.x * a.y;
2428}
2429
2430template <typename T>
2432 return {
2433 a.y * b.z - b.y * a.z,
2434 a.z * b.x - b.z * a.x,
2435 a.x * b.y - b.x * a.y,
2436 };
2437}
2438
2439template <typename T>
2441 return {
2442 a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
2443 a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z,
2444 a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x,
2445 a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
2446 };
2447}
2448
2449template <typename T>
2451 return inverse(b) * a;
2452}
2453
2454template <typename T>
2456 return a * b;
2457}
2458
2459// Multiplication of matrices:
2460
2461template <size_t N, size_t C2, size_t R1, typename T>
2463 mat<C2, R1, T> result{};
2464 for (size_t y = 0; y < C2; ++y) {
2465 for (size_t x = 0; x < R1; ++x) {
2466 for (size_t i = 0; i < N; ++i) {
2467 result[y][x] += a[i][x] * b[y][i];
2468 }
2469 }
2470 }
2471 return result;
2472}
2473
2474// Multiplication of matrix and vector:
2475
2476template <size_t N, typename T>
2478 vec<N, T> result{};
2479 for (size_t x = 0; x < N; ++x) {
2480 for (size_t i = 0; i < N; ++i) {
2481 result[x] += a[i][x] * b[i];
2482 }
2483 }
2484 return result;
2485}
2486
2487// Multiplication of quaternions:
2488
2489template <typename T>
2491 return {
2492 a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
2493 a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z,
2494 a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x,
2495 a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
2496 };
2497}
2498
2499// Multiplication of quaternion and vector:
2500
2501template <typename T>
2503 const vec<3, T> xyz{a.x, a.y, a.z};
2504 const vec<3, T> xyzb = cross(xyz, b);
2505 return b + (xyzb * a.w + cross(xyz, xyzb)) * T{2};
2506}
2507
2508template <typename T>
2510 return inverse(b) * a;
2511}
2512
2513// Division of matrices:
2514
2515template <size_t N, size_t C2, size_t R1, typename T>
2517 return a * inverse(b);
2518}
2519
2520// Division of matrix and vector:
2521
2522template <size_t N, typename T>
2524 return inverse(a) * b;
2525}
2526
2527// Quaternion to matrix:
2528
2529template <typename T>
2531 const T xx = a.x * a.x;
2532 const T yy = a.y * a.y;
2533 const T zz = a.z * a.z;
2534 const T xz = a.x * a.z;
2535 const T xy = a.x * a.y;
2536 const T yz = a.y * a.z;
2537 const T wx = a.w * a.x;
2538 const T wy = a.w * a.y;
2539 const T wz = a.w * a.z;
2540 return {
2541 vec<3, T>{T{1} - T{2} * (yy + zz), T{2} * (xy + wz), T{2} * (xz - wy)},
2542 vec<3, T>{T{2} * (xy - wz), T{1} - T{2} * (xx + zz), T{2} * (yz + wx)},
2543 vec<3, T>{T{2} * (xz + wy), T{2} * (yz - wx), T{1} - T{2} * (xx + yy)},
2544 };
2545}
2546
2547template <typename T>
2549 const T xx = a.x * a.x;
2550 const T yy = a.y * a.y;
2551 const T zz = a.z * a.z;
2552 const T xz = a.x * a.z;
2553 const T xy = a.x * a.y;
2554 const T yz = a.y * a.z;
2555 const T wx = a.w * a.x;
2556 const T wy = a.w * a.y;
2557 const T wz = a.w * a.z;
2558 return {
2559 vec<4, T>{T{1} - T{2} * (yy + zz), T{2} * (xy + wz), T{2} * (xz - wy), T{0}},
2560 vec<4, T>{T{2} * (xy - wz), T{1} - T{2} * (xx + zz), T{2} * (yz + wx), T{0}},
2561 vec<4, T>{T{2} * (xz + wy), T{2} * (yz - wx), T{1} - T{2} * (xx + yy), T{0}},
2562 vec<4, T>{T{0}, T{0}, T{0}, T{1}},
2563 };
2564}
2565
2566// Matrix to quaternion:
2567
2568template <typename T>
2570 const T rSquaredTimes4Minus1[4]{
2571 a[0][0] - a[1][1] - a[2][2],
2572 a[1][1] - a[0][0] - a[2][2],
2573 a[2][2] - a[0][0] - a[1][1],
2574 a[0][0] + a[1][1] + a[2][2],
2575 };
2576
2577 size_t maxIndex = 0;
2578 if (rSquaredTimes4Minus1[1] > rSquaredTimes4Minus1[maxIndex]) {
2579 maxIndex = 1;
2580 }
2581 if (rSquaredTimes4Minus1[2] > rSquaredTimes4Minus1[maxIndex]) {
2582 maxIndex = 2;
2583 }
2584 if (rSquaredTimes4Minus1[3] > rSquaredTimes4Minus1[maxIndex]) {
2585 maxIndex = 3;
2586 }
2587
2588 const T r = T{0.5} * sqrt(T{1} + rSquaredTimes4Minus1[maxIndex]);
2589 const T s = T{0.25} / r;
2590 switch (maxIndex) {
2591 case 0: return {r, (a[0][1] + a[1][0]) * s, (a[2][0] + a[0][2]) * s, (a[1][2] - a[2][1]) * s};
2592 case 1: return {(a[0][1] + a[1][0]) * s, r, (a[1][2] + a[2][1]) * s, (a[2][0] - a[0][2]) * s};
2593 case 2: return {(a[2][0] + a[0][2]) * s, (a[1][2] + a[2][1]) * s, r, (a[0][1] - a[1][0]) * s};
2594 case 3: return {(a[1][2] - a[2][1]) * s, (a[2][0] - a[0][2]) * s, (a[0][1] - a[1][0]) * s, r};
2595 default: unreachable();
2596 }
2597}
2598
2599template <typename T>
2603
2604// Matrix constructors:
2605
2614template <size_t N, typename T>
2616 mat<N, N, T> result{};
2617 for (size_t i = 0; i < N; ++i) {
2618 result[i][i] = components[i];
2619 }
2620 return result;
2621}
2622
2630template <typename T>
2632 return {
2633 vec<3, T>{T{1}, T{0}, T{0}},
2634 vec<3, T>{T{0}, T{1}, T{0}},
2635 vec<3, T>{translation.x, translation.y, T{1}},
2636 };
2637}
2638
2646template <typename T>
2648 const T cosine = cos(rotation);
2649 const T sine = sin(rotation);
2650 return {
2651 vec<3, T>{cosine, sine, T{0}},
2652 vec<3, T>{-sine, cosine, T{0}},
2653 vec<3, T>{T{0}, T{0}, T{1}},
2654 };
2655}
2656
2664template <typename T>
2666 return {
2667 vec<3, T>{scale.x, T{0}, T{0}},
2668 vec<3, T>{T{0}, scale.y, T{0}},
2669 vec<3, T>{T{0}, T{0}, T{1}},
2670 };
2671}
2672
2680template <typename T>
2682 return {
2683 vec<3, T>{T{1} + shear.x * shear.y, shear.y, T{0}},
2684 vec<3, T>{shear.x, T{1}, T{0}},
2685 vec<3, T>{T{0}, T{0}, T{1}},
2686 };
2687}
2688
2701template <typename T>
2703 const T cosine = cos(rotation);
2704 const T sine = sin(rotation);
2705 return {
2706 vec<3, T>{cosine * scale.x, sine * scale.x, T{0}},
2707 vec<3, T>{-sine * scale.y, cosine * scale.y, T{0}},
2708 vec<3, T>{translation.x, translation.y, T{1}},
2709 };
2710}
2711
2723template <typename T>
2725 const T cosine = cos(rotation);
2726 const T sine = sin(rotation);
2727 return {
2728 vec<3, T>{cosine, sine, T{0}},
2729 vec<3, T>{-sine, cosine, T{0}},
2730 vec<3, T>{translation.x, translation.y, T{1}},
2731 };
2732}
2733
2745template <typename T>
2747 return {
2748 vec<3, T>{scale.x, T{0}, T{0}},
2749 vec<3, T>{T{0}, scale.y, T{0}},
2750 vec<3, T>{translation.x, translation.y, T{1}},
2751 };
2752}
2753
2764template <typename T>
2766 const T cosine = cos(rotation);
2767 const T sine = sin(rotation);
2768 return {
2769 vec<3, T>{cosine * scale.x, sine * scale.x, T{0}},
2770 vec<3, T>{-sine * scale.y, cosine * scale.y, T{0}},
2771 vec<3, T>{T{0}, T{0}, T{1}},
2772 };
2773}
2774
2782template <typename T>
2784 return {
2785 vec<4, T>{T{1}, T{0}, T{0}, T{0}},
2786 vec<4, T>{T{0}, T{1}, T{0}, T{0}},
2787 vec<4, T>{T{0}, T{0}, T{1}, T{0}},
2788 vec<4, T>{translation.x, translation.y, translation.z, T{1}},
2789 };
2790}
2791
2799template <typename T>
2803
2811template <typename T>
2813 const T cosPitch = cos(pitchYawRollAngles.x);
2814 const T sinPitch = sin(pitchYawRollAngles.x);
2815 const T cosYaw = cos(pitchYawRollAngles.y);
2816 const T sinYaw = sin(pitchYawRollAngles.y);
2817 const T cosRoll = cos(pitchYawRollAngles.z);
2818 const T sinRoll = sin(pitchYawRollAngles.z);
2819 return {
2820 vec<4, T>{
2821 cosYaw * cosRoll + sinYaw * sinPitch * sinRoll,
2822 sinRoll * cosPitch,
2823 -sinYaw * cosRoll + cosYaw * sinPitch * sinRoll,
2824 T{0},
2825 },
2826 vec<4, T>{
2827 -cosYaw * sinRoll + sinYaw * sinPitch * cosRoll,
2828 cosRoll * cosPitch,
2829 sinRoll * sinYaw + cosYaw * sinPitch * cosRoll,
2830 T{0},
2831 },
2832 vec<4, T>{
2833 sinYaw * cosPitch,
2834 -sinPitch,
2835 cosYaw * cosPitch,
2836 T{0},
2837 },
2838 vec<4, T>{T{0}, T{0}, T{0}, T{1}},
2839 };
2840}
2841
2849template <typename T>
2851 return {
2852 vec<4, T>{scale.x, T{0}, T{0}, T{0}},
2853 vec<4, T>{T{0}, scale.y, T{0}, T{0}},
2854 vec<4, T>{T{0}, T{0}, scale.z, T{0}},
2855 vec<4, T>{T{0}, T{0}, T{0}, T{1}},
2856 };
2857}
2858
2871template <typename T>
2873 return {
2874 vec<4, T>{
2875 (T{1} - T{2} * (rotation.y * rotation.y + rotation.z * rotation.z)) * scale.x,
2876 (rotation.x * rotation.y + rotation.z * rotation.w) * scale.x * T{2},
2877 (rotation.x * rotation.z - rotation.y * rotation.w) * scale.x * T{2},
2878 T{0},
2879 },
2880 vec<4, T>{
2881 (rotation.x * rotation.y - rotation.z * rotation.w) * scale.y * T{2},
2882 (T{1} - T{2} * (rotation.x * rotation.x + rotation.z * rotation.z)) * scale.y,
2883 (rotation.y * rotation.z + rotation.x * rotation.w) * scale.y * T{2},
2884 T{0},
2885 },
2886 vec<4, T>{
2887 (rotation.x * rotation.z + rotation.y * rotation.w) * scale.z * T{2},
2888 (rotation.y * rotation.z - rotation.x * rotation.w) * scale.z * T{2},
2889 (T{1} - T{2} * (rotation.x * rotation.x + rotation.y * rotation.y)) * scale.z,
2890 T{0},
2891 },
2892 vec<4, T>{translation.x, translation.y, translation.z, T{1}},
2893 };
2894}
2895
2907template <typename T>
2909 return {
2910 vec<4, T>{
2911 T{1} - T{2} * (rotation.y * rotation.y + rotation.z * rotation.z),
2912 (rotation.x * rotation.y + rotation.z * rotation.w) * T{2},
2913 (rotation.x * rotation.z - rotation.y * rotation.w) * T{2},
2914 T{0},
2915 },
2916 vec<4, T>{
2917 (rotation.x * rotation.y - rotation.z * rotation.w) * T{2},
2918 T{1} - T{2} * (rotation.x * rotation.x + rotation.z * rotation.z),
2919 (rotation.y * rotation.z + rotation.x * rotation.w) * T{2},
2920 T{0},
2921 },
2922 vec<4, T>{
2923 (rotation.x * rotation.z + rotation.y * rotation.w) * T{2},
2924 (rotation.y * rotation.z - rotation.x * rotation.w) * T{2},
2925 T{1} - T{2} * (rotation.x * rotation.x + rotation.y * rotation.y),
2926 T{0},
2927 },
2928 vec<4, T>{translation.x, translation.y, translation.z, T{1}},
2929 };
2930}
2931
2943template <typename T>
2945 return {
2946 vec<4, T>{scale.x, T{0}, T{0}, T{0}},
2947 vec<4, T>{T{0}, scale.y, T{0}, T{0}},
2948 vec<4, T>{T{0}, T{0}, scale.z, T{0}},
2949 vec<4, T>{translation.x, translation.y, translation.z, T{1}},
2950 };
2951}
2952
2963template <typename T>
2965 return {
2966 vec<4, T>{
2967 (T{1} - T{2} * (rotation.y * rotation.y + rotation.z * rotation.z)) * scale.x,
2968 (rotation.x * rotation.y + rotation.z * rotation.w) * scale.x * T{2},
2969 (rotation.x * rotation.z - rotation.y * rotation.w) * scale.x * T{2},
2970 T{0},
2971 },
2972 vec<4, T>{
2973 (rotation.x * rotation.y - rotation.z * rotation.w) * scale.y * T{2},
2974 (T{1} - T{2} * (rotation.x * rotation.x + rotation.z * rotation.z)) * scale.y,
2975 (rotation.y * rotation.z + rotation.x * rotation.w) * scale.y * T{2},
2976 T{0},
2977 },
2978 vec<4, T>{
2979 (rotation.x * rotation.z + rotation.y * rotation.w) * scale.z * T{2},
2980 (rotation.y * rotation.z - rotation.x * rotation.w) * scale.z * T{2},
2981 (T{1} - T{2} * (rotation.x * rotation.x + rotation.y * rotation.y)) * scale.z,
2982 T{0},
2983 },
2984 vec<4, T>{T{0}, T{0}, T{0}, T{1}},
2985 };
2986}
2987
2988template <typename T>
2990 const vec<3, T> viewBackward = normalize(eye - target);
2991 const vec<3, T> viewRight = normalize(cross(up, viewBackward));
2992 const vec<3, T> viewUp = cross(viewBackward, viewRight);
2993 const T viewLocalX = dot(eye, viewRight);
2994 const T viewLocalY = dot(eye, viewUp);
2995 const T viewLocalZ = dot(eye, viewBackward);
2996 return {
2997 vec<4, T>{viewRight.x, viewUp.x, viewBackward.x, T{0}},
2998 vec<4, T>{viewRight.y, viewUp.y, viewBackward.y, T{0}},
2999 vec<4, T>{viewRight.z, viewUp.z, viewBackward.z, T{0}},
3000 vec<4, T>{-viewLocalX, -viewLocalY, -viewLocalZ, T{1}},
3001 };
3002}
3003
3004template <typename T>
3005[[nodiscard]] GREM_ALWAYS_INLINE mat<4, 4, T> GREM_VECTORCALL ortho(T left, T right, T bottom, T top) requires(floating_point<T>) {
3006 return {
3007 vec<4, T>{T{2} / (right - left), T{0}, T{0}, T{0}},
3008 vec<4, T>{T{0}, T{2} / (top - bottom), T{0}, T{0}},
3009 vec<4, T>{T{0}, T{0}, T{-1}, T{0}},
3010 vec<4, T>{(left + right) / (left - right), (bottom + top) / (bottom - top), T{0}, T{1}},
3011 };
3012}
3013
3014template <typename T>
3015[[nodiscard]] GREM_ALWAYS_INLINE mat<4, 4, T> GREM_VECTORCALL ortho(T left, T right, T bottom, T top, T nearZ, T farZ) requires(floating_point<T>) {
3016 return {
3017 vec<4, T>{T{2} / (right - left), T{0}, T{0}, T{0}},
3018 vec<4, T>{T{0}, T{2} / (top - bottom), T{0}, T{0}},
3019 vec<4, T>{T{0}, T{0}, T{1} / (nearZ - farZ), T{0}},
3020 vec<4, T>{(left + right) / (left - right), (bottom + top) / (bottom - top), nearZ / (nearZ - farZ), T{1}},
3021 };
3022}
3023
3024template <typename T>
3025[[nodiscard]] GREM_ALWAYS_INLINE mat<4, 4, T> GREM_VECTORCALL frustum(T left, T right, T bottom, T top, T nearZ, T farZ) requires(floating_point<T>) {
3026 return {
3027 vec<4, T>{(T{2} * nearZ) / (right - left), T{0}, T{0}, T{0}},
3028 vec<4, T>{T{0}, (T{2} * nearZ) / (top - bottom), T{0}, T{0}},
3029 vec<4, T>{(left + right) / (right - left), (bottom + top) / (top - bottom), farZ / (nearZ - farZ), T{-1}},
3030 vec<4, T>{T{0}, T{0}, (nearZ * farZ) / (nearZ - farZ), T{0}},
3031 };
3032}
3033
3034template <typename T>
3035[[nodiscard]] GREM_ALWAYS_INLINE mat<4, 4, T> GREM_VECTORCALL perspective(T verticalFieldOfView, T aspectRatio, T nearZ, T farZ) requires(floating_point<T>) {
3036 const T tanHalfVerticalFOV = tan(T{0.5} * verticalFieldOfView);
3037 return {
3038 vec<4, T>{T{1} / (tanHalfVerticalFOV * aspectRatio), T{0}, T{0}, T{0}},
3039 vec<4, T>{T{0}, T{1} / tanHalfVerticalFOV, T{0}, T{0}},
3040 vec<4, T>{T{0}, T{0}, farZ / (nearZ - farZ), T{-1}},
3041 vec<4, T>{T{0}, T{0}, (nearZ * farZ) / (nearZ - farZ), T{0}},
3042 };
3043}
3044
3045template <typename T>
3046[[nodiscard]] GREM_ALWAYS_INLINE mat<4, 4, T> GREM_VECTORCALL infinitePerspective(T verticalFieldOfView, T aspectRatio, T nearZ) requires(floating_point<T>) {
3047 const T tanHalfVerticalFOV = tan(T{0.5} * verticalFieldOfView);
3048 return {
3049 vec<4, T>{T{1} / (tanHalfVerticalFOV * aspectRatio), T{0}, T{0}, T{0}},
3050 vec<4, T>{T{0}, T{1} / tanHalfVerticalFOV, T{0}, T{0}},
3051 vec<4, T>{T{0}, T{0}, T{-1}, T{-1}},
3052 vec<4, T>{T{0}, T{0}, -nearZ, T{0}},
3053 };
3054}
3055
3056// Matrix decomposition:
3057
3061template <size_t N, typename T>
3063
3064template <typename T>
3070
3071template <typename T>
3077
3089template <typename T>
3092 vec<2, T> x{a[0]};
3093 vec<2, T> y{a[1]};
3094 result.translation = vec<2, T>{a[2]};
3095 result.scale = vec<2, T>{length(x), length(y)};
3096 if (result.scale.x >= Limits<T>::MACHINE_EPSILON) {
3097 result.rotation = atan2(x.y, x.x);
3098 } else if (result.scale.y >= Limits<T>::MACHINE_EPSILON) {
3099 result.rotation = atan2(y.x, -y.y);
3100 } else {
3101 result.rotation = T{0};
3102 }
3103 return result;
3104}
3105
3117template <typename T>
3120 vec<3, T> x{a[0]};
3121 vec<3, T> y{a[1]};
3122 vec<3, T> z{a[2]};
3123 result.translation = vec<3, T>{a[3]};
3124 result.scale = vec<3, T>{length(x), length(y), length(z)};
3125 if (any(lessThan(abs(result.scale), vec<3, T>{Limits<T>::MACHINE_EPSILON}))) {
3126 result.rotation = qua<T>{T{0}, T{0}, T{0}, T{1}};
3127 } else {
3128 x /= result.scale.x;
3129 y /= result.scale.y;
3130 z /= result.scale.z;
3131 if (dot(x, cross(y, z)) < T{0}) {
3132 result.scale = -result.scale;
3133 x = -x;
3134 y = -y;
3135 z = -z;
3136 }
3137 if (x.x + y.y + z.z > T{0}) {
3138 const T s = T{2} * sqrt(T{1} + x.x + y.y + z.z);
3139 result.rotation.x = (y.z - z.y) / s;
3140 result.rotation.y = (z.x - x.z) / s;
3141 result.rotation.z = (x.y - y.x) / s;
3142 result.rotation.w = T{0.25} * s;
3143 } else if (x.x >= y.y && x.x >= z.z) {
3144 const T s = T{2} * sqrt(T{1} + x.x - y.y - z.z);
3145 result.rotation.x = T{0.25} * s;
3146 result.rotation.y = (x.y + y.x) / s;
3147 result.rotation.z = (x.z + z.x) / s;
3148 result.rotation.w = (y.z - z.y) / s;
3149 } else if (y.y >= z.z) {
3150 const T s = T{2} * sqrt(T{1} + y.y - x.x - z.z);
3151 result.rotation.x = (y.x + x.y) / s;
3152 result.rotation.y = T{0.25} * s;
3153 result.rotation.z = (y.z + z.y) / s;
3154 result.rotation.w = (z.x - x.z) / s;
3155 } else {
3156 const T s = T{2} * sqrt(T{1} + z.z - x.x - y.y);
3157 result.rotation.x = (z.x + x.z) / s;
3158 result.rotation.y = (z.y + y.z) / s;
3159 result.rotation.z = T{0.25} * s;
3160 result.rotation.w = (x.y - y.x) / s;
3161 }
3162 }
3163 return result;
3164}
3165
3166// Quaternion decomposition:
3167
3168template <typename T>
3170 constexpr T COS_ONE_HALF = static_cast<T>(0.87758256189037271612);
3171 if (abs(orientation.w) <= COS_ONE_HALF) {
3172 return acos(orientation.w) * T{2};
3173 }
3174 const T result = asin(length(vec<3, T>{orientation.x, orientation.y, orientation.z})) * T{2};
3175 if (orientation.w < T{0}) {
3176 return T{2} * numbers::pi_v<T> - result;
3177 }
3178 return result;
3179}
3180
3181template <typename T>
3183 if (const T wSquared = length2(orientation.w); wSquared < T{1}) {
3184 return vec<3, T>{orientation.x, orientation.y, orientation.z} * (T{1} / sqrt(T{1} - wSquared));
3185 }
3186 return vec<3, T>{T{0}, T{0}, T{1}};
3187}
3188
3189template <typename T>
3191 return asin(T{2} * (orientation.w * orientation.x - orientation.y * orientation.z));
3192}
3193
3194template <typename T>
3195[[nodiscard]] GREM_ALWAYS_INLINE T GREM_VECTORCALL getYaw(qua<T> orientation) requires(floating_point<T>) {
3196 return atan2(T{2} * (orientation.x * orientation.z + orientation.w * orientation.y),
3197 length2(orientation.w) - length(orientation.x) - length2(orientation.y) + length2(orientation.z));
3198}
3199
3200template <typename T>
3202 return atan2(T{2} * (orientation.x * orientation.y + orientation.w * orientation.z),
3203 length2(orientation.w) - length(orientation.x) + length2(orientation.y) - length2(orientation.z));
3204}
3205
3206template <typename T>
3208 return {getPitch(orientation), getYaw(orientation), getRoll(orientation)};
3209}
3210
3211// Quaternion constructors:
3212
3213template <typename T>
3215 const T cosine = dot(from, to);
3216 if (cosine >= T{1} - Limits<T>::MACHINE_EPSILON) {
3217 return {T{0}, T{0}, T{0}, T{1}};
3218 }
3219 if (cosine < T{-1} + Limits<T>::MACHINE_EPSILON) {
3220 if (const Optional<vec<3, T>> axis = tryNormalize(cross(vec<3, T>{T{0}, T{0}, T{1}}, from))) {
3221 return angleAxis(numbers::pi_v<T>, *axis);
3222 }
3223 return angleAxis(numbers::pi_v<T>, normalize(cross(vec<3, T>{T{1}, T{0}, T{0}}, from)));
3224 }
3225 const T s = sqrt(T{2} * (T{1} + cosine));
3226 return {(T{1} / s) * cross(from, to), T{0.5} * s};
3227}
3228
3229template <typename T>
3231 vec<3, T> right = cross(direction, up);
3233 right = cross(direction, vec<3, T>{T{0}, T{1}, T{0}});
3235 right = cross(direction, vec<3, T>{T{1}, T{0}, T{0}});
3236 }
3237 }
3238 const vec<3, T> viewBackward = -direction;
3239 const vec<3, T> viewRight = normalize(right);
3240 const vec<3, T> viewUp = cross(viewRight, direction);
3241 return convert3x3MatrixToQuaternion(mat<3, 3, T>{viewRight, viewUp, viewBackward});
3242}
3243
3244template <typename T>
3246 const T halfAngle = T{0.5} * angle;
3247 return {axis * sin(halfAngle), cos(halfAngle)};
3248}
3249
3250template <typename T>
3252 const T halfAngle = T{0.5} * angle;
3253 return {sin(halfAngle), 0.0f, 0.0f, cos(halfAngle)};
3254}
3255
3256template <typename T>
3258 const T halfAngle = T{0.5} * angle;
3259 return {0.0f, sin(halfAngle), 0.0f, cos(halfAngle)};
3260}
3261
3262template <typename T>
3264 const T halfAngle = T{0.5} * angle;
3265 return {0.0f, 0.0f, sin(halfAngle), cos(halfAngle)};
3266}
3267
3268// Conversions:
3269
3270template <typename T>
3271[[nodiscard]] GREM_ALWAYS_INLINE qua<T> GREM_VECTORCALL convertAnglesToQuaternion(T pitchAngle, T yawAngle, T rollAngle) requires(floating_point<T>) {
3272 const T halfPitch = T{0.5} * pitchAngle;
3273 const T halfYaw = T{0.5} * yawAngle;
3274 const T halfRoll = T{0.5} * rollAngle;
3275 const T cosHalfPitch = cos(halfPitch);
3276 const T cosHalfYaw = cos(halfYaw);
3277 const T cosHalfRoll = cos(halfRoll);
3278 const T sinHalfPitch = sin(halfPitch);
3279 const T sinHalfYaw = sin(halfYaw);
3280 const T sinHalfRoll = sin(halfRoll);
3281 return {
3282 sinHalfPitch * cosHalfYaw * cosHalfRoll - cosHalfPitch * sinHalfYaw * sinHalfRoll,
3283 cosHalfPitch * sinHalfYaw * cosHalfRoll + sinHalfPitch * cosHalfYaw * sinHalfRoll,
3284 cosHalfPitch * cosHalfYaw * sinHalfRoll - sinHalfPitch * sinHalfYaw * cosHalfRoll,
3285 cosHalfPitch * cosHalfYaw * cosHalfRoll + sinHalfPitch * sinHalfYaw * sinHalfRoll,
3286 };
3287}
3288
3289template <typename T>
3291 return convertAnglesToQuaternion(pitchYawRollAngles.x, pitchYawRollAngles.y, pitchYawRollAngles.z);
3292}
3293
3294template <typename T>
3296 GREM_ASSERT(length2(x) >= length2(0.9999f) && length2(x) <= length2(1.0001f));
3297 return acos(x.x);
3298}
3299
3300template <typename T>
3302 GREM_ASSERT(length2(y) >= length2(0.9999f) && length2(y) <= length2(1.0001f));
3303 return acos(y.y);
3304}
3305
3306template <typename T>
3308 GREM_ASSERT(length2(z) >= length2(0.9999f) && length2(z) <= length2(1.0001f));
3309 return {-asin(z.y), atan2(z.x, z.z)};
3310}
3311
3312template <typename T>
3314 const T cosRoll = cos(rollAngle);
3315 const T sinRoll = sin(rollAngle);
3316 return {cosRoll, sinRoll};
3317}
3318
3319template <typename T>
3321 const T cosRoll = cos(rollAngle);
3322 const T sinRoll = sin(rollAngle);
3323 return {-sinRoll, cosRoll};
3324}
3325
3326template <typename T>
3328 const T cosYaw = cos(yawAngle);
3329 const T sinYaw = sin(yawAngle);
3330 const T cosPitch = cos(pitchAngle);
3331 const T sinPitch = sin(pitchAngle);
3332 return {sinYaw * cosPitch, -sinPitch, cosYaw * cosPitch};
3333}
3334
3335template <typename T>
3339
3340template <typename T>
3342 return -convertPitchYawAnglesToZDirection(pitchYawAngles.x, pitchYawAngles.y);
3343}
3344
3345template <typename T>
3347 return convertAnglesToForwardDirection(vec<2, T>{pitchYawRollAngles.x, pitchYawRollAngles.y});
3348}
3349
3350template <typename T>
3354
3355template <typename T>
3359
3369template <typename T>
3370[[nodiscard]] inline T getAngleDifference(T a, T b) {
3371 return wrap((b - a) + numbers::pi_v<T>, T{2} * numbers::pi_v<T>) - numbers::pi_v<T>;
3372}
3373
3384template <typename T>
3385[[nodiscard]] inline T getAngleDifferenceAroundAxis(vec<3, T> axis, vec<3, T> a, vec<3, T> b) {
3386 return atan2(dot(cross(a, b), axis), dot(a, b));
3387}
3388
3389template <typename T>
3390[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL convertDegreesToRadians(T angleInDegrees) requires(floating_point<T>) {
3391 return angleInDegrees * static_cast<T>(0.01745329251994329577);
3392}
3393
3394template <size_t N, typename T>
3396 vec<N, T> result;
3397 for (size_t i = 0; i < N; ++i) {
3398 result[i] = convertDegreesToRadians(angleInDegrees[i]);
3399 }
3400 return result;
3401}
3402
3403template <typename T>
3404[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL convertRadiansToDegrees(T angleInRadians) requires(floating_point<T>) {
3405 return angleInRadians * static_cast<T>(57.2957795130823208768);
3406}
3407
3408template <size_t N, typename T>
3410 vec<N, T> result;
3411 for (size_t i = 0; i < N; ++i) {
3412 result[i] = convertRadiansToDegrees(angleInRadians[i]);
3413 }
3414 return result;
3415}
3416
3417// Classification:
3418
3427template <typename T>
3429 return vec<1, bool>{isfinite(values.x)};
3430}
3431
3440template <typename T>
3442 return vec<2, bool>{isfinite(values.x), isfinite(values.y)};
3443}
3444
3453template <typename T>
3455 return vec<3, bool>{isfinite(values.x), isfinite(values.y), isfinite(values.z)};
3456}
3457
3466template <typename T>
3468 return vec<4, bool>{isfinite(values.x), isfinite(values.y), isfinite(values.z), isfinite(values.w)};
3469}
3470
3479template <typename T>
3481 return vec<1, bool>{isinf(values.x)};
3482}
3483
3492template <typename T>
3494 return vec<2, bool>{isinf(values.x), isinf(values.y)};
3495}
3496
3505template <typename T>
3507 return vec<3, bool>{isinf(values.x), isinf(values.y), isinf(values.z)};
3508}
3509
3518template <typename T>
3520 return vec<4, bool>{isinf(values.x), isinf(values.y), isinf(values.z), isinf(values.w)};
3521}
3522
3531template <typename T>
3533 return vec<1, bool>{isnan(values.x)};
3534}
3535
3544template <typename T>
3546 return vec<2, bool>{isnan(values.x), isnan(values.y)};
3547}
3548
3557template <typename T>
3559 return vec<3, bool>{isnan(values.x), isnan(values.y), isnan(values.z)};
3560}
3561
3570template <typename T>
3572 return vec<4, bool>{isnan(values.x), isnan(values.y), isnan(values.z), isnan(values.w)};
3573}
3574
3583template <size_t N, typename T>
3585 vec<N, bool> result;
3586 for (size_t i = 0; i < N; ++i) {
3587 result[i] = signbit(values[i]);
3588 }
3589 return result;
3590}
3591
3600template <typename T>
3602 if constexpr (same_as<T, float>) {
3603 return std::copysignf(magnitude, sign);
3604 } else if constexpr (same_as<T, double>) {
3605 return std::copysign(magnitude, sign);
3606 } else {
3607 magnitude = abs(magnitude);
3608 return (sign < 0) ? -magnitude : magnitude;
3609 }
3610}
3611
3622template <size_t N, typename T>
3623[[nodiscard]] GREM_ALWAYS_INLINE vec<N, T> copysign(vec<N, T> magnitudes, vec<N, T> signs) requires(requires(const size_t i) { copysign(magnitudes[i], signs[i]); }) {
3624 vec<N, T> result;
3625 for (size_t i = 0; i < N; ++i) {
3626 result[i] = copysign(magnitudes[i], signs[i]);
3627 }
3628 return result;
3629}
3630
3631// Blending:
3632
3641template <typename T>
3642[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL step(T edge, T value) requires(floating_point<T> || strict_signed_integral<T>) {
3643 return mix(T{1}, T{0}, value < edge);
3644}
3645
3655template <size_t N, typename T>
3657 requires(requires(const vec<N, T> x, const vec<N, T> y, const size_t i) { step(x[i], y[i]); }) {
3658 vec<N, T> result;
3659 for (size_t i = 0; i < N; ++i) {
3660 result[i] = step(edges[i], values[i]);
3661 }
3662 return result;
3663}
3664
3674template <size_t N, typename T>
3675[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> GREM_VECTORCALL step(T edge, vec<N, T> values)
3676 requires(requires(const T x, const vec<N, T> y, const size_t i) { step(x, y[i]); }) {
3677 vec<N, T> result;
3678 for (size_t i = 0; i < N; ++i) {
3679 result[i] = step(edge, values[i]);
3680 }
3681 return result;
3682}
3683
3694template <typename T>
3695[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL smoothstep(T edge0, T edge1, T value) requires(floating_point<T>) {
3696 const T t = clamp((value - edge0) / (edge1 - edge0), T{0}, T{1});
3697 return t * t * (T{3} - T{2} * t);
3698}
3699
3711template <size_t N, typename T>
3713 requires(requires(const vec<N, T> x, const vec<N, T> y, const vec<N, T> z, const size_t i) { smoothstep(x[i], y[i], z[i]); }) {
3714 vec<N, T> result;
3715 for (size_t i = 0; i < N; ++i) {
3716 result[i] = smoothstep(edges0[i], edges1[i], values[i]);
3717 }
3718 return result;
3719}
3720
3731template <size_t N, typename T>
3732[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> GREM_VECTORCALL smoothstep(T edge0, T edge1, vec<N, T> values)
3733 requires(requires(const T x, const T y, const vec<N, T> z, const size_t i) { smoothstep(x, y, z[i]); }) {
3734 vec<N, T> result;
3735 for (size_t i = 0; i < N; ++i) {
3736 result[i] = smoothstep(edge0, edge1, values[i]);
3737 }
3738 return result;
3739}
3740
3750template <typename T>
3751[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL lerp(T a, T b, T alpha) requires(floating_point<T>) {
3752 return a * (T{1} - alpha) + b * alpha;
3753}
3754
3765template <size_t N, typename T, typename Alpha>
3766[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> GREM_VECTORCALL lerp(vec<N, T> a, vec<N, T> b, Alpha alpha)
3767 requires(requires(const vec<N, T> x, const vec<N, T> y, const Alpha z, const size_t i) { lerp(x[i], y[i], z); }) {
3768 vec<N, T> result;
3769 for (size_t i = 0; i < N; ++i) {
3770 result[i] = lerp(a[i], b[i], alpha);
3771 }
3772 return result;
3773}
3774
3784template <typename T>
3785[[nodiscard]] GREM_ALWAYS_INLINE constexpr qua<T> lerp(qua<T> a, qua<T> b, T alpha) requires(floating_point<T>) {
3786 return a * (1 - alpha) + b * alpha;
3787}
3788
3798template <typename T>
3799[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL unlerp(T value, T minValue, T maxValue) requires(floating_point<T>) {
3800 return (value - minValue) / (maxValue - minValue);
3801}
3802
3813template <typename T>
3814[[nodiscard]] GREM_ALWAYS_INLINE constexpr qua<T> slerp(qua<T> a, qua<T> b, T alpha) requires(floating_point<T>) {
3815 T cosine = dot(a, b);
3816 if (cosine < T{0}) {
3817 cosine = -cosine;
3818 b = -b;
3819 }
3820 if (cosine > T{1} - Limits<T>::MACHINE_EPSILON) {
3821 return lerp(a, b, alpha);
3822 }
3823 const T angle = acos(cosine);
3824 return (a * sin((T{1} - alpha) * angle) + b * sin(alpha * angle)) / sin(angle);
3825}
3826
3836template <typename T>
3837[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL mix(T a, T b, T alpha) requires(floating_point<T>) {
3838 return lerp(a, b, alpha);
3839}
3840
3851template <size_t N, typename T, typename Alpha>
3852[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> GREM_VECTORCALL mix(vec<N, T> a, vec<N, T> b, Alpha alpha)
3853 requires(requires(const vec<N, T> x, const vec<N, T> y, const Alpha z, const size_t i) { mix(x[i], y[i], z); }) {
3854 vec<N, T> result;
3855 for (size_t i = 0; i < N; ++i) {
3856 result[i] = mix(a[i], b[i], alpha);
3857 }
3858 return result;
3859}
3860
3871template <size_t N, typename T, typename Alpha>
3873 requires(requires(const vec<N, T> x, const vec<N, T> y, const Alpha z, const size_t i) { mix(x[i], y[i], z[i]); }) {
3874 vec<N, T> result;
3875 for (size_t i = 0; i < N; ++i) {
3876 result[i] = mix(a[i], b[i], alphas[i]);
3877 }
3878 return result;
3879}
3880
3891template <typename T>
3892[[nodiscard]] GREM_ALWAYS_INLINE constexpr qua<T> mix(qua<T> a, qua<T> b, T alpha) requires(floating_point<T>) {
3893 return slerp(a, b, alpha);
3894}
3895
3896// Utilities:
3897
3905template <typename T>
3907 return {-a.y, a.x};
3908}
3909
3917template <typename T>
3919 return {a.y, -a.x};
3920}
3921
3929template <size_t N, typename T>
3930[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> flipX(vec<N, T> a) {
3931 a.x = -a.x;
3932 return a;
3933}
3934
3942template <size_t N, typename T>
3943[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> flipY(vec<N, T> a) requires(N >= 2) {
3944 a.y = -a.y;
3945 return a;
3946}
3947
3955template <size_t N, typename T>
3956[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> flipZ(vec<N, T> a) requires(N >= 3) {
3957 a.z = -a.z;
3958 return a;
3959}
3960
3969template <size_t N, typename T>
3970[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> reflect(vec<N, T> a, vec<N, T> normal) {
3971 return a - T{2} * dot(normal, a) * normal;
3972}
3973
3982template <typename T>
3983[[nodiscard]] GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL flipSignIf(T value, bool flip) requires(floating_point<T> || strict_signed_integral<T>) {
3985 return bit_cast<float32_t>((static_cast<uint32_t>(flip) << 31) ^ bit_cast<uint32_t>(value));
3986 } else if constexpr (same_as<T, float64_t> && Limits<float64_t>::IS_IEC60559) {
3987 return bit_cast<float64_t>((static_cast<uint64_t>(flip) << 63) ^ bit_cast<uint64_t>(value));
3988 } else {
3989 return (flip) ? -value : value;
3990 }
3991}
3992
4001template <size_t N, typename T>
4002[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> GREM_VECTORCALL flipSignIf(vec<N, T> value, bool flip)
4003 requires(requires(const vec<N, T> x, const bool y, const size_t i) { flipSignIf(x[i], y); }) {
4004 vec<N, T> result;
4005 for (size_t i = 0; i < N; ++i) {
4006 result[i] = flipSignIf(value[i], flip);
4007 }
4008 return result;
4009}
4010
4024template <typename T>
4025[[nodiscard]] GREM_ALWAYS_INLINE constexpr T clampLength(T value, T maxLength) requires(strict_arithmetic<T>) {
4026 GREM_ASSERT(maxLength >= T{0});
4027 if constexpr (unsigned_integral<T>) {
4028 return min(value, maxLength);
4029 } else {
4030 if constexpr (floating_point<T>) {
4031 if (abs(value) > maxLength) {
4032 value = copysign(maxLength, value);
4033 }
4034 } else {
4035 if (value < -maxLength) {
4036 value = -maxLength;
4037 } else if (value > maxLength) {
4038 value = maxLength;
4039 }
4040 }
4041 return value;
4042 }
4043}
4044
4058template <size_t N, typename T>
4059[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> clampLength(vec<N, T> vector, T maxLength) requires(strict_arithmetic<T>) {
4060 GREM_ASSERT(maxLength >= T{0});
4061 if (const T lengthSquared = length2(vector); lengthSquared > length2(maxLength)) {
4062 vector *= maxLength / sqrt(lengthSquared);
4063 }
4064 return vector;
4065}
4066
4076template <typename T>
4077[[nodiscard]] GREM_ALWAYS_INLINE constexpr T moveTowards(T value, T targetValue, T delta) requires(floating_point<T>) {
4078 GREM_ASSERT(delta >= T{});
4079 const T difference = targetValue - value;
4080 if (abs(difference) <= delta) {
4081 return targetValue;
4082 }
4083 return value + copysign(delta, difference);
4084}
4085
4095template <size_t N, typename T>
4096[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> moveTowards(vec<N, T> value, vec<N, T> targetValue, T delta) requires(floating_point<T>) {
4097 GREM_ASSERT(delta >= T{});
4098 const vec<N, T> difference = targetValue - value;
4099 const T distance = length(difference);
4100 if (distance <= delta) {
4101 return targetValue;
4102 }
4103 return value + difference * (delta / distance);
4104}
4105
4117template <typename T, typename Frequency, typename Duration>
4118[[nodiscard]] GREM_ALWAYS_INLINE constexpr T expDecay(T value, T targetValue, Frequency decayRate, Duration deltaTime) requires(floating_point<T>) {
4119 GREM_ASSERT(decayRate >= Frequency{});
4120 GREM_ASSERT(deltaTime >= Duration{});
4121 value = targetValue + (value - targetValue) * exp(-decayRate * deltaTime);
4122 if (abs(value - targetValue) <= Limits<T>::MACHINE_EPSILON) {
4123 value = targetValue;
4124 }
4125 return value;
4126}
4127
4139template <size_t N, typename T, typename Frequency, typename Duration>
4140[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> expDecay(vec<N, T> value, vec<N, T> targetValue, Frequency decayRate, Duration deltaTime) requires(floating_point<T>) {
4141 GREM_ASSERT(decayRate >= Frequency{});
4142 GREM_ASSERT(deltaTime >= Duration{});
4143 value = targetValue + (value - targetValue) * exp(-decayRate * deltaTime);
4144 if (distance2(value, targetValue) <= length2(Limits<T>::MACHINE_EPSILON)) {
4145 value = targetValue;
4146 }
4147 return value;
4148}
4149
4160template <typename T, typename Frequency, typename Duration>
4161[[nodiscard]] GREM_ALWAYS_INLINE constexpr T expDecay(T value, Frequency decayRate, Duration deltaTime) requires(floating_point<T>) {
4162 GREM_ASSERT(decayRate >= Frequency{});
4163 GREM_ASSERT(deltaTime >= Duration{});
4164 value *= exp(-decayRate * deltaTime);
4165 if (abs(value) <= Limits<T>::MACHINE_EPSILON) {
4166 value = T{0};
4167 }
4168 return value;
4169}
4170
4181template <size_t N, typename T, typename Frequency, typename Duration>
4182[[nodiscard]] GREM_ALWAYS_INLINE constexpr vec<N, T> expDecay(vec<N, T> value, Frequency decayRate, Duration deltaTime) requires(floating_point<T>) {
4183 GREM_ASSERT(decayRate >= Frequency{});
4184 GREM_ASSERT(deltaTime >= Duration{});
4185 value *= exp(-decayRate * deltaTime);
4187 value = {};
4188 }
4189 return value;
4190}
4191
4192} // namespace grem
4193
4194template <grem::size_t N, typename T>
4195struct grem::Formatter<grem::vec<N, T>> : Formatter<T> {
4199
4200 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
4202 if (*p == 'n') {
4203 openingBracket = {};
4204 closingBracket = {};
4205 ++p;
4206 }
4207 if (*p == 's') {
4208 separator = " ";
4209 ++p;
4210 }
4211 return p;
4212 }
4213
4214 void formatTo(auto& output, const vec<N, T>& value) const {
4215 output.append(openingBracket);
4216 if (N > 0) {
4217 Formatter<T>::formatTo(output, value[0]);
4218 for (size_t i = 1; i < N; ++i) {
4219 output.append(separator);
4220 Formatter<T>::formatTo(output, value[i]);
4221 }
4222 }
4223 output.append(closingBracket);
4224 }
4225};
4226
4227template <typename V>
4229struct grem::Formatter<V> : Formatter<std::remove_cvref_t<decltype(V{}[0])>> {
4233
4234 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
4235 p = Formatter<std::remove_cvref_t<decltype(V{}[0])>>::parseFormatSpecification(p);
4236 if (*p == 'n') {
4237 openingBracket = {};
4238 closingBracket = {};
4239 ++p;
4240 }
4241 if (*p == 's') {
4242 separator = " ";
4243 ++p;
4244 }
4245 return p;
4246 }
4247
4248 void formatTo(auto& output, const V& value) const {
4249 output.append(openingBracket);
4250 Formatter<std::remove_cvref_t<decltype(V{}[0])>>::formatTo(output, value[0]);
4251 output.append(separator);
4252 Formatter<std::remove_cvref_t<decltype(V{}[0])>>::formatTo(output, value[1]);
4253 output.append(separator);
4254 Formatter<std::remove_cvref_t<decltype(V{}[0])>>::formatTo(output, value[2]);
4255 output.append(separator);
4256 Formatter<std::remove_cvref_t<decltype(V{}[0])>>::formatTo(output, value[3]);
4257 output.append(closingBracket);
4258 }
4259};
4260
4261template <grem::size_t C, grem::size_t R, typename T>
4262struct grem::Formatter<grem::mat<C, R, T>> : Formatter<T> {
4269
4270 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
4272 if (*p == 'n') {
4273 openingBracket = {};
4274 closingBracket = {};
4275 innerSeparator = " ";
4278 ++p;
4279 }
4280 if (*p == 's') {
4281 innerSeparator = " ";
4282 ++p;
4283 }
4284 return p;
4285 }
4286
4287 void formatTo(auto& output, const mat<C, R, T>& value) const {
4288 output.append(openingBracket);
4289 if (C > 0) {
4290 output.append(innerOpeningBracket);
4291 if (R > 0) {
4292 Formatter<T>::formatTo(output, value[0][0]);
4293 for (size_t i = 1; i < R; ++i) {
4294 output.append(innerSeparator);
4295 Formatter<T>::formatTo(output, value[0][i]);
4296 }
4297 }
4298 output.append(innerClosingBracket);
4299 for (size_t i = 1; i < C; ++i) {
4300 output.append(separator);
4301 output.append(innerOpeningBracket);
4302 if (R > 0) {
4303 Formatter<T>::formatTo(output, value[i][0]);
4304 for (size_t j = 1; j < R; ++j) {
4305 output.append(innerSeparator);
4306 Formatter<T>::formatTo(output, value[i][j]);
4307 }
4308 }
4309 output.append(innerClosingBracket);
4310 }
4311 }
4312 output.append(closingBracket);
4313 }
4314};
4315
4316template <typename T>
4319
4320 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
4321 if (*p == ':') {
4322 ++p;
4323 p = underlying.parseFormatSpecification(p);
4324 }
4325 return p;
4326 }
4327
4328 void formatTo(auto& output, const qua<T>& value) const {
4329 underlying.formatTo(output, value.x);
4330 output.append("i + ");
4331 underlying.formatTo(output, value.y);
4332 output.append("j + ");
4333 underlying.formatTo(output, value.z);
4334 output.append("k + ");
4335 underlying.formatTo(output, value.w);
4336 }
4337};
4338
4339#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
#define GREM_ALWAYS_INLINE
Definition attributes.hpp:10
#define GREM_VECTORCALL
Definition attributes.hpp:51
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Definition concepts.hpp:85
Definition concepts.hpp:35
Definition concepts.hpp:91
Definition concepts.hpp:76
Definition concepts.hpp:79
@ T
't'
Definition keyboard.hpp:90
Definition aliases.hpp:17
constexpr float LN10
Definition math.hpp:48
constexpr float SQRT2
Definition math.hpp:49
constexpr float SQRT3
Definition math.hpp:50
constexpr float LOG2E
Definition math.hpp:42
constexpr float LOG10E
Definition math.hpp:43
constexpr float PHI
Definition math.hpp:53
constexpr float EGAMMA
Definition math.hpp:52
constexpr float LN2
Definition math.hpp:47
constexpr float INV_PI
Definition math.hpp:45
constexpr float INV_SQRTPI
Definition math.hpp:46
constexpr float PI
Definition math.hpp:44
constexpr float E
Definition math.hpp:41
constexpr float INV_SQRT3
Definition math.hpp:51
Definition aliases.hpp:9
GREM_ALWAYS_INLINE qua< T > GREM_VECTORCALL quatLookAt(vec< 3, T > direction, vec< 3, T > up)
Definition math.hpp:3230
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length2(T a)
Definition math.hpp:1969
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL erf(vec< N, T > a)
Definition math.hpp:1402
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL lessThan(T a, T b)
Definition math.hpp:1240
vec< 1, size_t > uzvec1
Definition math.hpp:433
mat< 2, 2, double > dmat2
Definition math.hpp:808
vec< 1, double > dvec1
Definition math.hpp:441
qua< float > quat
Definition math.hpp:892
GREM_ALWAYS_INLINE constexpr T midpoint(T a, T b)
Get the midpoint between two values.
Definition fundamentals.hpp:559
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL lerp(T a, T b, T alpha)
Perform linear interpolation (lerp) between two values.
Definition math.hpp:3751
Clock::duration Duration
Definition Clock.hpp:25
GREM_ALWAYS_INLINE constexpr vec< 2, T > GREM_VECTORCALL angledVector(T angle)
Create a 2-dimensional direction unit vector from an angle.
Definition math.hpp:2207
vec< 4, bool32_t > b32vec4
Definition math.hpp:384
vec< 4, float64_t > f64vec4
Definition math.hpp:456
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL cos(vec< N, T > a)
Definition math.hpp:1389
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL distance(T a, T b)
Definition math.hpp:2038
GREM_ALWAYS_INLINE constexpr mat< N, N, T > GREM_VECTORCALL diagonal(vec< N, T > components)
Create a matrix from its diagonal components.
Definition math.hpp:2615
GREM_ALWAYS_INLINE constexpr mat< 3, 3, T > GREM_VECTORCALL translateScale(vec< 2, T > translation, vec< 2, T > scale)
Create a combined translation and scale matrix describing a 2D transformation.
Definition math.hpp:2746
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL sin(vec< N, T > a)
Definition math.hpp:1393
vec< 4, bool8_t > b8vec4
Definition math.hpp:376
vec< 3, uint16_t > u16vec3
Definition math.hpp:411
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL lessThanEqual(T a, T b)
Definition math.hpp:1245
mat< 4, 4, int8_t > i8mat4
Definition math.hpp:774
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL lgamma(vec< N, T > a)
Definition math.hpp:1404
vec< 4, uint64_t > u64vec4
Definition math.hpp:428
vec< 2, uint16_t > u16vec2
Definition math.hpp:410
vec< 4, bool64_t > b64vec4
Definition math.hpp:388
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL asin(vec< N, T > a)
Definition math.hpp:1395
vec< 4, float > vec4
Definition math.hpp:440
vec< 4, i16norm > i16vec4norm
Definition math.hpp:468
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL flipSignIf(T value, bool flip)
Get a value with its sign flipped if a boolean is true.
Definition math.hpp:3983
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sqrt(T a)
Get the square root of a value.
Definition math.hpp:1114
vec< 3, bool64_t > b64vec3
Definition math.hpp:387
GREM_ALWAYS_INLINE vec< 3, T > GREM_VECTORCALL getPitchYawRoll(qua< T > orientation)
Definition math.hpp:3207
vec< 3, uint32_t > u32vec3
Definition math.hpp:419
mat< 2, 2, int8_t > i8mat2
Definition math.hpp:772
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL notEqual(T a, T b)
Definition math.hpp:1235
mat< 3, 3, int16_t > i16mat3
Definition math.hpp:781
vec< 1, float > vec1
Definition math.hpp:437
GREM_ALWAYS_INLINE qua< T > GREM_VECTORCALL roll(T angle)
Definition math.hpp:3263
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL normalize(vec< N, T > a)
Convert a vector to a unit vector of length 1 in the same direction.
Definition math.hpp:2057
mat< 1, 1, uint8_t > u8mat1
Definition math.hpp:775
vec< 1, float16_t > f16vec1
Definition math.hpp:445
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL lookAt(vec< 3, T > eye, vec< 3, T > target, vec< 3, T > up)
Definition math.hpp:2989
vec< 4, int > ivec4
Definition math.hpp:392
GREM_ALWAYS_INLINE constexpr mat< 3, 3, T > GREM_VECTORCALL translate(vec< 2, T > translation)
Create a translation matrix describing a 2D translation.
Definition math.hpp:2631
GREM_ALWAYS_INLINE constexpr vec< N, T > flipY(vec< N, T > a)
Get a vector flipped along the Y axis.
Definition math.hpp:3943
vec< 2, float64_t > f64vec2
Definition math.hpp:454
mat< 1, 1, float16_t > f16mat1
Definition math.hpp:811
vec< 2, float > vec2
Definition math.hpp:438
vec< 3, i8norm > i8vec3norm
Definition math.hpp:459
vec< 4, bool > bvec4
Definition math.hpp:372
vec< 3, u8norm > u8vec3norm
Definition math.hpp:463
mat< 1, 1, int8_t > i8mat1
Definition math.hpp:771
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL wrap(T value, T limit) noexcept
Wrap a scalar value around a range of non-negative values.
Definition math.hpp:1218
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL floor(vec< N, T > a)
Definition math.hpp:1421
mat< 4, 4, int64_t > i64mat4
Definition math.hpp:798
vec< 1, int16_t > i16vec1
Definition math.hpp:405
mat< 1, 1, float32_t > f32mat1
Definition math.hpp:815
mat< 2, 2, int16_t > i16mat2
Definition math.hpp:780
GREM_ALWAYS_INLINE constexpr mat< 1, 1, T > GREM_VECTORCALL inverseTranspose(const mat< 1, 1, T > &a)
Definition math.hpp:2343
GREM_ALWAYS_INLINE constexpr decltype(auto) clamp(T &&value, Low &&minValue, High &&maxValue)
Clamp a value to a specific interval.
Definition fundamentals.hpp:630
GREM_ALWAYS_INLINE T GREM_VECTORCALL getPitch(qua< T > orientation)
Definition math.hpp:3190
GREM_ALWAYS_INLINE constexpr Optional< T > GREM_VECTORCALL tryDivide(T a, T b)
Try to divide a floating-point value by a scalar and produce a defined result.
Definition math.hpp:2101
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL greaterThan(T a, T b)
Definition math.hpp:1250
vec< 3, int64_t > i64vec3
Definition math.hpp:423
mat< 2, 2, uint64_t > u64mat2
Definition math.hpp:800
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL smoothstep(T edge0, T edge1, T value)
Perform Hermite interpolation between two edge values.
Definition math.hpp:3695
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL determinant(const mat< 1, 1, T > &a)
Definition math.hpp:2214
vec< 4, i8norm > i8vec4norm
Definition math.hpp:460
GREM_ALWAYS_INLINE constexpr vec< N, int > GREM_VECTORCALL ilogb(vec< N, T > a)
Definition math.hpp:1417
vec< 2, i16norm > i16vec2norm
Definition math.hpp:466
vec< 4, ssize_t > izvec4
Definition math.hpp:432
vec< 2, uint64_t > u64vec2
Definition math.hpp:426
vec< 3, uint8_t > u8vec3
Definition math.hpp:403
mat< 4, 4, double > dmat4
Definition math.hpp:810
mat< 4, 4, uint8_t > u8mat4
Definition math.hpp:778
vec< 4, int32_t > i32vec4
Definition math.hpp:416
GREM_ALWAYS_INLINE constexpr qua< T > slerp(qua< T > a, qua< T > b, T alpha)
Perform spherical linear interpolation (slerp) between two quaternions.
Definition math.hpp:3814
constexpr Offset2D operator/(Offset2D a, int32_t b) noexcept
Definition extents.hpp:700
vec< 2, size_t > uzvec2
Definition math.hpp:434
GREM_ALWAYS_INLINE qua< T > GREM_VECTORCALL yaw(T angle)
Definition math.hpp:3257
GREM_ALWAYS_INLINE T copysign(T magnitude, T sign)
Get a value with the sign of a different value.
Definition math.hpp:3601
mat< 1, 1, uint16_t > u16mat1
Definition math.hpp:783
GREM_ALWAYS_INLINE qua< T > GREM_VECTORCALL pitch(T angle)
Definition math.hpp:3251
GREM_ALWAYS_INLINE constexpr mat< R, C, T > transpose(const mat< C, R, T > &a)
Definition math.hpp:2247
mat< 3, 3, uint32_t > u32mat3
Definition math.hpp:793
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL tgamma(vec< N, T > a)
Definition math.hpp:1405
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL cross(vec< 2, T > a, vec< 2, T > b)
Definition math.hpp:2426
vec< 2, int8_t > i8vec2
Definition math.hpp:398
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL pow(T a, T b) noexcept
Calculate the result of raising a given value to a given power.
Definition math.hpp:1179
mat< 2, 2, int > imat2
Definition math.hpp:764
qua< float64_t > f64quat
Definition math.hpp:896
vec< 3, int32_t > i32vec3
Definition math.hpp:415
vec< 2, uint8_t > u8vec2
Definition math.hpp:402
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL abs(vec< N, T > a)
Definition math.hpp:1386
mat< 1, 1, unsigned > umat1
Definition math.hpp:767
GREM_ALWAYS_INLINE vec< 3, T > GREM_VECTORCALL convertPitchYawAnglesToZDirection(T pitchAngle, T yawAngle)
Definition math.hpp:3327
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL exp(T a) noexcept
Calculate the result of Euler's number raised to a given power.
Definition math.hpp:1154
mat< 1, 1, int32_t > i32mat1
Definition math.hpp:787
vec< 1, bool > bvec1
Definition math.hpp:369
GREM_ALWAYS_INLINE constexpr T expDecay(T value, T targetValue, Frequency decayRate, Duration deltaTime)
Exponentially decay a value towards a target value over a given time.
Definition math.hpp:4118
vec< 1, bool16_t > b16vec1
Definition math.hpp:377
GREM_ALWAYS_INLINE qua< T > GREM_VECTORCALL angleAxis(T angle, vec< 3, T > axis)
Definition math.hpp:3245
GREM_ALWAYS_INLINE constexpr T clampLength(T value, T maxLength)
Get a value with the same sign as a given value, but whose magnitude has been clamped to a maximum le...
Definition math.hpp:4025
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL step(T edge, T value)
Evaluate a step function with a given edge value.
Definition math.hpp:3642
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL acosh(vec< N, T > a)
Definition math.hpp:1392
vec< 2, bool8_t > b8vec2
Definition math.hpp:374
vec< 2, bool > bvec2
Definition math.hpp:370
vec< 4, int16_t > i16vec4
Definition math.hpp:408
GREM_ALWAYS_INLINE constexpr vec< N, T > flipX(vec< N, T > a)
Get a vector flipped along the X axis.
Definition math.hpp:3930
vec< 1, bool8_t > b8vec1
Definition math.hpp:373
qua< float16_t > f16quat
Definition math.hpp:894
GREM_ALWAYS_INLINE vec< 2, T > GREM_VECTORCALL convertAnglesToForwardDirection(T rollAngle)
Definition math.hpp:3336
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL product(vec< 1, T > a)
Definition math.hpp:1876
GREM_ALWAYS_INLINE vec< 2, T > GREM_VECTORCALL convertZDirectionToPitchYawAngles(vec< 3, T > z)
Definition math.hpp:3307
vec< 3, float > vec3
Definition math.hpp:439
mat< 4, 4, float64_t > f64mat4
Definition math.hpp:822
mat< 4, 4, uint64_t > u64mat4
Definition math.hpp:802
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL frustum(T left, T right, T bottom, T top, T nearZ, T farZ)
Definition math.hpp:3025
vec< 4, bool16_t > b16vec4
Definition math.hpp:380
vec< 3, unsigned > uvec3
Definition math.hpp:395
mat< 4, 4, float32_t > f32mat4
Definition math.hpp:818
mat< 3, 3, int8_t > i8mat3
Definition math.hpp:773
vec< 4, uint8_t > u8vec4
Definition math.hpp:404
GREM_ALWAYS_INLINE constexpr Optional< DecomposeMagnitudeAngleResult< T > > tryDecomposeMagnitudeAngle(vec< 2, T > a)
Definition math.hpp:2189
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL fma(vec< N, T > a, vec< N, T > b, vec< N, T > c)
Definition math.hpp:1431
vec< 1, ssize_t > izvec1
Definition math.hpp:429
GREM_ALWAYS_INLINE constexpr Optional< DecomposeMagnitudeDirectionResult< N, T > > tryDecomposeMagnitudeDirection(vec< N, T > a)
Definition math.hpp:2169
vec< 4, unsigned > uvec4
Definition math.hpp:396
GREM_ALWAYS_INLINE mat< 3, 3, T > GREM_VECTORCALL translateRotate(vec< 2, T > translation, T rotation)
Create a combined translation and rotation matrix describing a 2D transformation.
Definition math.hpp:2724
vec< 1, bool32_t > b32vec1
Definition math.hpp:381
vec< 2, bool32_t > b32vec2
Definition math.hpp:382
constexpr auto rotate(forward_range auto &&range, auto middle)
Definition algorithms.hpp:641
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL log1p(vec< N, T > a)
Definition math.hpp:1414
vec< 2, int64_t > i64vec2
Definition math.hpp:422
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL minComponent(vec< 1, T > a)
Definition math.hpp:1898
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL atanh(vec< N, T > a)
Definition math.hpp:1400
vec< 4, int64_t > i64vec4
Definition math.hpp:424
GREM_ALWAYS_INLINE T GREM_VECTORCALL getRoll(qua< T > orientation)
Definition math.hpp:3201
qua< float32_t > f32quat
Definition math.hpp:895
mat< 3, 3, float > mat3
Definition math.hpp:805
mat< 4, 4, uint32_t > u32mat4
Definition math.hpp:794
constexpr Offset2D operator*(Offset2D a, int32_t b) noexcept
Definition extents.hpp:692
GREM_ALWAYS_INLINE mat< 3, 3, T > GREM_VECTORCALL translateRotateScale(vec< 2, T > translation, T rotation, vec< 2, T > scale)
Create a combined translation, rotation and scale (TRS) matrix describing a 2D transformation.
Definition math.hpp:2702
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL infinitePerspective(T verticalFieldOfView, T aspectRatio, T nearZ)
Definition math.hpp:3046
mat< 3, 3, float16_t > f16mat3
Definition math.hpp:813
GREM_ALWAYS_INLINE mat< 3, 3, T > GREM_VECTORCALL rotateScale(T rotation, vec< 2, T > scale)
Create a combined rotation and scale matrix describing a 2D transformation.
Definition math.hpp:2765
GREM_ALWAYS_INLINE vec< 2, T > GREM_VECTORCALL convertRollAngleToXDirection(T rollAngle)
Definition math.hpp:3313
GREM_ALWAYS_INLINE vec< 1, bool > GREM_VECTORCALL isinf(const vec< 1, T > &values)
Check if each component of a vector is infinity.
Definition math.hpp:3480
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL distance2(T a, T b)
Definition math.hpp:2026
mat< 1, 1, int > imat1
Definition math.hpp:763
vec< 3, i16norm > i16vec3norm
Definition math.hpp:467
mat< 2, 2, uint16_t > u16mat2
Definition math.hpp:784
vec< 3, bool > bvec3
Definition math.hpp:371
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL erfc(vec< N, T > a)
Definition math.hpp:1403
GREM_ALWAYS_INLINE constexpr vec< N, long > GREM_VECTORCALL lround(vec< N, T > a)
Definition math.hpp:1419
vec< 4, size_t > uzvec4
Definition math.hpp:436
vec< 4, u16norm > u16vec4norm
Definition math.hpp:472
GREM_ALWAYS_INLINE constexpr vec< N, T > reflect(vec< N, T > a, vec< N, T > normal)
Get a vector reflected against a plane with a given normal vector.
Definition math.hpp:3970
vec< 3, int8_t > i8vec3
Definition math.hpp:399
vec< 3, bool16_t > b16vec3
Definition math.hpp:379
vec< 3, float32_t > f32vec3
Definition math.hpp:451
T getAngleDifferenceAroundAxis(vec< 3, T > axis, vec< 3, T > a, vec< 3, T > b)
Get the smallest signed angle around an axis between two relative vectors.
Definition math.hpp:3385
vec< 4, uint16_t > u16vec4
Definition math.hpp:412
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL convertRadiansToDegrees(T angleInRadians)
Definition math.hpp:3404
vec< 1, i16norm > i16vec1norm
Definition math.hpp:465
GREM_ALWAYS_INLINE constexpr DecomposeMagnitudeDirectionResult< N, T > decomposeMagnitudeDirection(vec< N, T > a)
Definition math.hpp:2163
mat< 2, 2, float32_t > f32mat2
Definition math.hpp:816
vec< 2, u16norm > u16vec2norm
Definition math.hpp:470
GREM_ALWAYS_INLINE constexpr Optional< vec< N, T > > GREM_VECTORCALL tryNormalize(vec< N, T > a)
Try to convert a vector to a unit vector of length 1 in the same direction.
Definition math.hpp:2082
vec< 3, float64_t > f64vec3
Definition math.hpp:455
vec< 2, u8norm > u8vec2norm
Definition math.hpp:462
constexpr bool equal(input_range auto &&rangeA, input_range auto &&rangeB)
Definition algorithms.hpp:358
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL mix(T a, T b, T alpha)
Blend between two values based on an alpha value.
Definition math.hpp:3837
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL fract(T a)
Definition math.hpp:1142
vec< 2, bool64_t > b64vec2
Definition math.hpp:386
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL log2(vec< N, T > a)
Definition math.hpp:1415
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL ceil(vec< N, T > a)
Definition math.hpp:1422
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL expm1(vec< N, T > a)
Definition math.hpp:1411
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL atan(vec< N, T > a)
Definition math.hpp:1399
vec< 2, ssize_t > izvec2
Definition math.hpp:430
qua< float > dquat
Definition math.hpp:893
vec< 1, uint16_t > u16vec1
Definition math.hpp:409
vec< 2, bool16_t > b16vec2
Definition math.hpp:378
vec< 4, uint32_t > u32vec4
Definition math.hpp:420
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL trunc(vec< N, T > a)
Definition math.hpp:1420
mat< 3, 3, unsigned > umat3
Definition math.hpp:769
vec< 1, i8norm > i8vec1norm
Definition math.hpp:457
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL perspective(T verticalFieldOfView, T aspectRatio, T nearZ, T farZ)
Definition math.hpp:3035
void unreachable()
Definition assertions.hpp:125
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL ortho(T left, T right, T bottom, T top)
Definition math.hpp:3005
mat< 2, 2, float > mat2
Definition math.hpp:804
mat< 3, 3, float32_t > f32mat3
Definition math.hpp:817
mat< 4, 4, float16_t > f16mat4
Definition math.hpp:814
GREM_ALWAYS_INLINE T GREM_VECTORCALL convertForwardDirectionToAngles(vec< 2, T > forward)
Definition math.hpp:3351
vec< 1, float64_t > f64vec1
Definition math.hpp:453
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL log10(vec< N, T > a)
Definition math.hpp:1413
mat< 2, 2, unsigned > umat2
Definition math.hpp:768
vec< 4, double > dvec4
Definition math.hpp:444
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL fdim(vec< N, T > a, vec< N, T > b)
Definition math.hpp:1435
GREM_ALWAYS_INLINE constexpr decltype(auto) max(A &&a, B &&b)
Get the greater of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:598
vec< 3, bool32_t > b32vec3
Definition math.hpp:383
vec< 3, uint64_t > u64vec3
Definition math.hpp:427
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL unlerp(T value, T minValue, T maxValue)
Linearly convert a value from a specific range to the [0, 1] range.
Definition math.hpp:3799
mat< 1, 1, int64_t > i64mat1
Definition math.hpp:795
mat< 3, 3, int > imat3
Definition math.hpp:765
mat< 3, 3, double > dmat3
Definition math.hpp:809
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL fmod(T a, T b) noexcept
Calculate the floating-point remainder of a scalar division.
Definition math.hpp:1192
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL tanh(vec< N, T > a)
Definition math.hpp:1398
GREM_ALWAYS_INLINE constexpr vec< N, T > flipZ(vec< N, T > a)
Get a vector flipped along the Z axis.
Definition math.hpp:3956
GREM_ALWAYS_INLINE vec< N, bool > GREM_VECTORCALL signbit(vec< N, T > values)
Get the sign bits of the components of a vector.
Definition math.hpp:3584
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL sinh(vec< N, T > a)
Definition math.hpp:1394
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL greaterThanEqual(T a, T b)
Definition math.hpp:1255
vec< 3, double > dvec3
Definition math.hpp:443
vec< 2, int32_t > i32vec2
Definition math.hpp:414
T getAngleDifference(T a, T b)
Get the smallest signed angle between two absolute angles.
Definition math.hpp:3370
vec< 4, u8norm > u8vec4norm
Definition math.hpp:464
DecomposeTranslationRotationScaleResult< 2, T > decomposeTranslationRotationScale(const mat< 3, 3, T > &a)
Decompose a combined translation, rotation and scale (TRS) matrix describing a 2D transformation into...
Definition math.hpp:3090
GREM_ALWAYS_INLINE qua< T > GREM_VECTORCALL convertAnglesToQuaternion(T pitchAngle, T yawAngle, T rollAngle)
Definition math.hpp:3271
mat< 2, 2, int32_t > i32mat2
Definition math.hpp:788
GREM_ALWAYS_INLINE T GREM_VECTORCALL convertXDirectionToRollAngle(vec< 2, T > x)
Definition math.hpp:3295
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL round(vec< N, T > a)
Definition math.hpp:1418
mat< 1, 1, uint32_t > u32mat1
Definition math.hpp:791
vec< 2, float16_t > f16vec2
Definition math.hpp:446
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL tan(vec< N, T > a)
Definition math.hpp:1397
GREM_ALWAYS_INLINE qua< T > GREM_VECTORCALL convert3x3MatrixToQuaternion(const mat< 3, 3, T > &a)
Definition math.hpp:2569
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL asinh(vec< N, T > a)
Definition math.hpp:1396
vec< 3, u16norm > u16vec3norm
Definition math.hpp:471
GREM_ALWAYS_INLINE qua< T > GREM_VECTORCALL rotation(vec< 3, T > from, vec< 3, T > to)
Definition math.hpp:3214
vec< 1, int32_t > i32vec1
Definition math.hpp:413
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL not_(bool a)
Definition math.hpp:1259
vec< 4, float16_t > f16vec4
Definition math.hpp:448
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL exp2(T a) noexcept
Calculate the result of 2 raised to a given power.
Definition math.hpp:1166
GREM_ALWAYS_INLINE constexpr vec< 2, T > rotate90DegreesClockwise(vec< 2, T > a)
Get a 2D vector rotated 90 degrees clockwise.
Definition math.hpp:3918
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL inversesqrt(T a)
Get the reciprocal of the square root of a value.
Definition math.hpp:1136
mat< 4, 4, float > mat4
Definition math.hpp:806
GREM_ALWAYS_INLINE constexpr qua< T > GREM_VECTORCALL conjugate(qua< T > a)
Definition math.hpp:2260
vec< 2, float32_t > f32vec2
Definition math.hpp:450
vec< 1, uint8_t > u8vec1
Definition math.hpp:401
vec< 1, float32_t > f32vec1
Definition math.hpp:449
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL getAngle(vec< 2, T > a)
Get the absolute angle of a 2-dimensional vector, in radians.
Definition math.hpp:2136
mat< 2, 2, float64_t > f64mat2
Definition math.hpp:820
GREM_ALWAYS_INLINE vec< 1, bool > GREM_VECTORCALL isnan(const vec< 1, T > &values)
Check if each component of a vector is NaN (not a number).
Definition math.hpp:3532
mat< 4, 4, int > imat4
Definition math.hpp:766
mat< 4, 4, int32_t > i32mat4
Definition math.hpp:790
GREM_ALWAYS_INLINE constexpr mat< 1, 1, T > GREM_VECTORCALL inverse(const mat< 1, 1, T > &a)
Definition math.hpp:2267
GREM_ALWAYS_INLINE T GREM_VECTORCALL convertYDirectionToRollAngle(vec< 2, T > y)
Definition math.hpp:3301
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL logb(vec< N, T > a)
Definition math.hpp:1416
vec< 1, int64_t > i64vec1
Definition math.hpp:421
GREM_ALWAYS_INLINE vec< 3, T > GREM_VECTORCALL getAxis(qua< T > orientation)
Definition math.hpp:3182
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
mat< 1, 1, double > dmat1
Definition math.hpp:807
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL acos(vec< N, T > a)
Definition math.hpp:1391
mat< 3, 3, int64_t > i64mat3
Definition math.hpp:797
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL cosh(vec< N, T > a)
Definition math.hpp:1390
mat< 2, 2, uint32_t > u32mat2
Definition math.hpp:792
vec< 2, unsigned > uvec2
Definition math.hpp:394
mat< 4, 4, unsigned > umat4
Definition math.hpp:770
GREM_ALWAYS_INLINE constexpr decltype(auto) min(A &&a, B &&b)
Get the smaller of two values, or the first value if they are equivalent.
Definition fundamentals.hpp:585
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sign(T a)
Get the sign multiplier of a scalar.
Definition math.hpp:1204
vec< 1, uint32_t > u32vec1
Definition math.hpp:417
mat< 3, 3, int32_t > i32mat3
Definition math.hpp:789
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL all(bool a)
Definition math.hpp:1285
vec< 3, int > ivec3
Definition math.hpp:391
vec< 3, int16_t > i16vec3
Definition math.hpp:407
vec< 1, int8_t > i8vec1
Definition math.hpp:397
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL length(T a)
Definition math.hpp:2001
vec< 1, u16norm > u16vec1norm
Definition math.hpp:469
mat< 2, 2, float16_t > f16mat2
Definition math.hpp:812
vec< 1, u8norm > u8vec1norm
Definition math.hpp:461
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL cbrt(vec< N, T > a)
Definition math.hpp:1401
vec< 4, float32_t > f32vec4
Definition math.hpp:452
mat< 3, 3, float64_t > f64mat3
Definition math.hpp:821
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL convertDegreesToRadians(T angleInDegrees)
Definition math.hpp:3390
vec< 3, float16_t > f16vec3
Definition math.hpp:447
GREM_ALWAYS_INLINE constexpr Optional< T > GREM_VECTORCALL tryGetAngle(vec< 2, T > a)
Try to get the absolute angle of a 2-dimensional vector, in radians.
Definition math.hpp:2149
GREM_ALWAYS_INLINE mat< 3, 3, T > GREM_VECTORCALL convertQuaternionTo3x3Matrix(qua< T > a)
Definition math.hpp:2530
vec< 1, bool64_t > b64vec1
Definition math.hpp:385
mat< 4, 4, uint16_t > u16mat4
Definition math.hpp:786
mat< 1, 1, float > mat1
Definition math.hpp:803
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL sum(vec< 1, T > a)
Definition math.hpp:1854
GREM_ALWAYS_INLINE constexpr vec< N, T > GREM_VECTORCALL log(vec< N, T > a)
Definition math.hpp:1412
mat< 3, 3, uint8_t > u8mat3
Definition math.hpp:777
mat< 1, 1, int16_t > i16mat1
Definition math.hpp:779
GREM_ALWAYS_INLINE constexpr bool GREM_VECTORCALL any(bool a)
Definition math.hpp:1272
vec< 2, i8norm > i8vec2norm
Definition math.hpp:458
vec< 1, unsigned > uvec1
Definition math.hpp:393
GREM_ALWAYS_INLINE constexpr mat< 3, 3, T > GREM_VECTORCALL scale(vec< 2, T > scale)
Create a scale matrix describing a 2D scale.
Definition math.hpp:2665
mat< 3, 3, uint64_t > u64mat3
Definition math.hpp:801
vec< 3, size_t > uzvec3
Definition math.hpp:435
vec< 2, uint32_t > u32vec2
Definition math.hpp:418
mat< 2, 2, int64_t > i64mat2
Definition math.hpp:796
GREM_ALWAYS_INLINE constexpr DecomposeMagnitudeAngleResult< T > decomposeMagnitudeAngle(vec< 2, T > a)
Definition math.hpp:2184
vec< 2, double > dvec2
Definition math.hpp:442
mat< 1, 1, float64_t > f64mat1
Definition math.hpp:819
vec< 2, int16_t > i16vec2
Definition math.hpp:406
GREM_ALWAYS_INLINE T GREM_VECTORCALL getYaw(qua< T > orientation)
Definition math.hpp:3195
GREM_ALWAYS_INLINE mat< 4, 4, T > GREM_VECTORCALL convertQuaternionTo4x4Matrix(qua< T > a)
Definition math.hpp:2548
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL maxComponent(vec< 1, T > a)
Definition math.hpp:1920
vec< 3, ssize_t > izvec3
Definition math.hpp:431
GREM_ALWAYS_INLINE vec< 1, bool > GREM_VECTORCALL isfinite(const vec< 1, T > &values)
Check if each component of a vector is finite.
Definition math.hpp:3428
vec< 1, uint64_t > u64vec1
Definition math.hpp:425
mat< 4, 4, int16_t > i16mat4
Definition math.hpp:782
GREM_ALWAYS_INLINE vec< 2, T > GREM_VECTORCALL convertRollAngleToYDirection(T rollAngle)
Definition math.hpp:3320
vec< 4, int8_t > i8vec4
Definition math.hpp:400
GREM_ALWAYS_INLINE constexpr mat< 3, 3, T > GREM_VECTORCALL shear(vec< 2, T > shear)
Create a shear matrix describing a 2D shear.
Definition math.hpp:2681
mat< 2, 2, uint8_t > u8mat2
Definition math.hpp:776
GREM_ALWAYS_INLINE constexpr T moveTowards(T value, T targetValue, T delta)
Move a value linearly towards a target by some distance, without overshoot.
Definition math.hpp:4077
vec< 2, int > ivec2
Definition math.hpp:390
mat< 1, 1, uint64_t > u64mat1
Definition math.hpp:799
vec< 1, int > ivec1
Definition math.hpp:389
mat< 3, 3, uint16_t > u16mat3
Definition math.hpp:785
GREM_ALWAYS_INLINE constexpr T select(bool condition, T ifTrue, T ifFalse)
Choose between two values based on a condition.
Definition math.hpp:927
GREM_ALWAYS_INLINE constexpr T GREM_VECTORCALL dot(vec< 1, T > a, vec< 1, T > b)
Definition math.hpp:1942
GREM_ALWAYS_INLINE qua< T > GREM_VECTORCALL convert4x4MatrixToQuaternion(const mat< 4, 4, T > &a)
Definition math.hpp:2600
vec< 3, bool8_t > b8vec3
Definition math.hpp:375
GREM_ALWAYS_INLINE constexpr vec< 2, T > rotate90DegreesCounterclockwise(vec< 2, T > a)
Get a 2D vector rotated 90 degrees counterclockwise.
Definition math.hpp:3906
Definition math.hpp:2178
T angle
Definition math.hpp:2180
T magnitude
Definition math.hpp:2179
vec< N, T > direction
Definition math.hpp:2159
T magnitude
Definition math.hpp:2158
vec< 2, T > translation
Extracted translation vector.
Definition math.hpp:3066
vec< 2, T > scale
Extracted scale vector.
Definition math.hpp:3068
T rotation
Extracted rotation angle.
Definition math.hpp:3067
vec< 3, T > scale
Extracted scale vector.
Definition math.hpp:3075
qua< T > rotation
Extracted rotation quaternion.
Definition math.hpp:3074
vec< 3, T > translation
Extracted translation vector.
Definition math.hpp:3073
Result of the decomposeTranslationRotationScale() function.
Definition math.hpp:3062
void formatTo(auto &output, const V &value) const
Definition math.hpp:4248
constexpr const char * parseFormatSpecification(const char *p)
Definition math.hpp:4234
StringView separator
Definition math.hpp:4230
StringView closingBracket
Definition math.hpp:4232
StringView openingBracket
Definition math.hpp:4231
StringView innerClosingBracket
Definition math.hpp:4268
StringView separator
Definition math.hpp:4263
void formatTo(auto &output, const mat< C, R, T > &value) const
Definition math.hpp:4287
constexpr const char * parseFormatSpecification(const char *p)
Definition math.hpp:4270
StringView closingBracket
Definition math.hpp:4265
StringView openingBracket
Definition math.hpp:4264
StringView innerOpeningBracket
Definition math.hpp:4267
StringView innerSeparator
Definition math.hpp:4266
Formatter< T > underlying
Definition math.hpp:4318
void formatTo(auto &output, const qua< T > &value) const
Definition math.hpp:4328
constexpr const char * parseFormatSpecification(const char *p)
Definition math.hpp:4320
void formatTo(auto &output, const vec< N, T > &value) const
Definition math.hpp:4214
constexpr const char * parseFormatSpecification(const char *p)
Definition math.hpp:4200
StringView openingBracket
Definition math.hpp:4197
StringView separator
Definition math.hpp:4196
StringView closingBracket
Definition math.hpp:4198
Definition formatting.hpp:47
constexpr const char * parseFormatSpecification(const auto &p)
Definition formatting.hpp:50
void formatTo(FormatOutput &output, const auto &value) const
Definition formatting.hpp:56
Definition fundamentals.hpp:27
GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm() noexcept=default
GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(float x, vec2 yz, float w) noexcept
Definition math.hpp:506
GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(vec< 4, U > v) noexcept
Definition math.hpp:485
GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(float value) noexcept
Definition math.hpp:488
GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(vec2 xy, vec2 zw) noexcept
Definition math.hpp:500
GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(vec3 xyz, float w) noexcept
Definition math.hpp:494
GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(float x, float y, vec2 zw) noexcept
Definition math.hpp:509
GREM_ALWAYS_INLINE constexpr size_t size() const noexcept
Definition math.hpp:542
GREM_ALWAYS_INLINE constexpr float operator[](size_t i) const
Definition math.hpp:538
static constexpr size_t RANK
Definition math.hpp:475
GREM_ALWAYS_INLINE constexpr bool operator==(const iA2B10G10R10vec4norm &) const noexcept=default
GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(vec2 xy, float z, float w) noexcept
Definition math.hpp:503
GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(float x, vec3 yzw) noexcept
Definition math.hpp:497
GREM_ALWAYS_INLINE constexpr iA2B10G10R10vec4norm(float x, float y, float z, float w) noexcept
Definition math.hpp:491
Definition math.hpp:663
GREM_ALWAYS_INLINE constexpr mat() noexcept=default
GREM_ALWAYS_INLINE constexpr mat(vec< 4, T > i, vec< 4, T > j, vec< 4, T > k, vec< 4, T > l) noexcept
Definition math.hpp:702
GREM_ALWAYS_INLINE constexpr mat(vec< 3, T > i, vec< 3, T > j, vec< 3, T > k) noexcept
Definition math.hpp:699
GREM_ALWAYS_INLINE constexpr mat(T value) noexcept
Definition math.hpp:678
GREM_ALWAYS_INLINE constexpr mat(T ix, T iy, T iz, T jx, T jy, T jz, T kx, T ky, T kz) noexcept
Definition math.hpp:687
GREM_ALWAYS_INLINE constexpr const Component & operator[](size_t i) const
Definition math.hpp:753
GREM_ALWAYS_INLINE constexpr mat(vec< 1, T > i) noexcept
Definition math.hpp:693
GREM_ALWAYS_INLINE constexpr size_t size() const noexcept
Definition math.hpp:758
GREM_ALWAYS_INLINE constexpr bool operator==(const mat &) const noexcept=default
GREM_ALWAYS_INLINE constexpr mat(const mat< N, N, T > &m) noexcept
Definition math.hpp:706
static constexpr size_t RANK
Definition math.hpp:665
vec< R, T > Component
Definition math.hpp:664
GREM_ALWAYS_INLINE constexpr mat(T ix, T iy, T jx, T jy) noexcept
Definition math.hpp:684
GREM_ALWAYS_INLINE constexpr mat(T ix, T iy, T iz, T iw, T jx, T jy, T jz, T jw, T kx, T ky, T kz, T kw, T lx, T ly, T lz, T lw) noexcept
Definition math.hpp:690
GREM_ALWAYS_INLINE constexpr mat(vec< 2, T > i, vec< 2, T > j) noexcept
Definition math.hpp:696
GREM_ALWAYS_INLINE constexpr Component & operator[](size_t i)
Definition math.hpp:748
Definition math.hpp:825
GREM_ALWAYS_INLINE constexpr Component & operator[](size_t i)
Definition math.hpp:857
float y
Definition math.hpp:830
float w
Definition math.hpp:832
GREM_ALWAYS_INLINE constexpr const Component & operator[](size_t i) const
Definition math.hpp:872
GREM_ALWAYS_INLINE constexpr size_t size() const noexcept
Definition math.hpp:887
float x
Definition math.hpp:829
T Component
Definition math.hpp:826
GREM_ALWAYS_INLINE constexpr qua(vec< 3, T > xyz, T w) noexcept
Definition math.hpp:849
static constexpr size_t RANK
Definition math.hpp:827
float z
Definition math.hpp:831
GREM_ALWAYS_INLINE constexpr qua() noexcept=default
GREM_ALWAYS_INLINE constexpr qua(T x, T y, T z, T w) noexcept
Definition math.hpp:843
GREM_ALWAYS_INLINE constexpr bool operator==(const qua &) const noexcept=default
GREM_ALWAYS_INLINE constexpr bool operator==(const uA2B10G10R10vec4norm &) const noexcept=default
GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(float value) noexcept
Definition math.hpp:594
GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(float x, vec3 yzw) noexcept
Definition math.hpp:603
static constexpr size_t RANK
Definition math.hpp:581
GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(float x, vec2 yz, float w) noexcept
Definition math.hpp:612
GREM_ALWAYS_INLINE constexpr float operator[](size_t i) const
Definition math.hpp:636
GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(vec2 xy, float z, float w) noexcept
Definition math.hpp:609
GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm() noexcept=default
GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(vec< 4, U > v) noexcept
Definition math.hpp:591
GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(float x, float y, float z, float w) noexcept
Definition math.hpp:597
GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(vec3 xyz, float w) noexcept
Definition math.hpp:600
GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(vec2 xy, vec2 zw) noexcept
Definition math.hpp:606
GREM_ALWAYS_INLINE constexpr uA2B10G10R10vec4norm(float x, float y, vec2 zw) noexcept
Definition math.hpp:615
GREM_ALWAYS_INLINE constexpr size_t size() const noexcept
Definition math.hpp:640
T x
Definition math.hpp:65
T Component
Definition math.hpp:62
GREM_ALWAYS_INLINE constexpr vec(T value) noexcept
Definition math.hpp:73
static constexpr size_t RANK
Definition math.hpp:63
GREM_ALWAYS_INLINE constexpr bool operator==(const vec &) const noexcept=default
GREM_ALWAYS_INLINE constexpr vec() noexcept=default
GREM_ALWAYS_INLINE constexpr Component & operator[](size_t i)
Definition math.hpp:78
GREM_ALWAYS_INLINE constexpr const Component & operator[](size_t i) const
Definition math.hpp:90
GREM_ALWAYS_INLINE constexpr size_t size() const noexcept
Definition math.hpp:102
GREM_ALWAYS_INLINE constexpr const Component & operator[](size_t i) const
Definition math.hpp:150
GREM_ALWAYS_INLINE constexpr bool operator==(const vec &) const noexcept=default
GREM_ALWAYS_INLINE constexpr size_t size() const noexcept
Definition math.hpp:163
T y
Definition math.hpp:113
T x
Definition math.hpp:112
T Component
Definition math.hpp:109
GREM_ALWAYS_INLINE constexpr vec(T x, T y) noexcept
Definition math.hpp:126
GREM_ALWAYS_INLINE constexpr vec(T value) noexcept
Definition math.hpp:122
GREM_ALWAYS_INLINE constexpr vec() noexcept=default
GREM_ALWAYS_INLINE constexpr Component & operator[](size_t i)
Definition math.hpp:137
static constexpr size_t RANK
Definition math.hpp:110
GREM_ALWAYS_INLINE constexpr Component & operator[](size_t i)
Definition math.hpp:217
GREM_ALWAYS_INLINE constexpr vec(T x, T y, T z) noexcept
Definition math.hpp:190
T x
Definition math.hpp:173
GREM_ALWAYS_INLINE constexpr vec() noexcept=default
GREM_ALWAYS_INLINE constexpr bool operator==(const vec &) const noexcept=default
static constexpr size_t RANK
Definition math.hpp:171
T y
Definition math.hpp:174
GREM_ALWAYS_INLINE constexpr vec(T value) noexcept
Definition math.hpp:185
GREM_ALWAYS_INLINE constexpr vec(T x, vec< 2, T > yz) noexcept
Definition math.hpp:200
T Component
Definition math.hpp:170
T z
Definition math.hpp:175
GREM_ALWAYS_INLINE constexpr vec(vec< 2, T > xy, T z) noexcept
Definition math.hpp:195
GREM_ALWAYS_INLINE constexpr const Component & operator[](size_t i) const
Definition math.hpp:231
GREM_ALWAYS_INLINE constexpr size_t size() const noexcept
Definition math.hpp:245
GREM_ALWAYS_INLINE constexpr Component & operator[](size_t i)
Definition math.hpp:334
GREM_ALWAYS_INLINE constexpr vec(T x, T y, vec< 2, T > zw) noexcept
Definition math.hpp:311
GREM_ALWAYS_INLINE constexpr bool operator==(const vec &) const noexcept=default
T y
Definition math.hpp:256
GREM_ALWAYS_INLINE constexpr vec(vec< 2, T > xy, T z, T w) noexcept
Definition math.hpp:299
GREM_ALWAYS_INLINE constexpr vec(T x, vec< 2, T > yz, T w) noexcept
Definition math.hpp:305
GREM_ALWAYS_INLINE constexpr vec(T x, T y, T z, T w) noexcept
Definition math.hpp:275
T z
Definition math.hpp:257
GREM_ALWAYS_INLINE constexpr vec() noexcept=default
GREM_ALWAYS_INLINE constexpr vec(vec< 2, T > xy, vec< 2, T > zw) noexcept
Definition math.hpp:293
GREM_ALWAYS_INLINE constexpr vec(T value) noexcept
Definition math.hpp:269
GREM_ALWAYS_INLINE constexpr vec(vec< 3, T > xyz, T w) noexcept
Definition math.hpp:281
GREM_ALWAYS_INLINE constexpr vec(T x, vec< 3, T > yzw) noexcept
Definition math.hpp:287
T Component
Definition math.hpp:252
static constexpr size_t RANK
Definition math.hpp:253
GREM_ALWAYS_INLINE constexpr const Component & operator[](size_t i) const
Definition math.hpp:349
T w
Definition math.hpp:258
GREM_ALWAYS_INLINE constexpr size_t size() const noexcept
Definition math.hpp:364
T x
Definition math.hpp:255
Definition math.hpp:58