-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstructs.h
More file actions
4660 lines (4043 loc) · 277 KB
/
structs.h
File metadata and controls
4660 lines (4043 loc) · 277 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#include <iostream>
#include <vector>
#include <array>
#include <numeric>
#include <functional>
#include <initializer_list>
#include <optional>
#include <cassert>
#include "constants.h"
#include "hash.h"
namespace vim::math3d {
namespace mathOps {
template <typename T = double>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
percentage(T denominator, T numerator) { return (numerator / denominator) * 100; }
/// <summary>
/// Calculate the nearest power of 2 from the input number
/// </summary>
template <typename T = std::int32_t>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
toNearestPowOf2(T x) { return (T)(std::pow((T)2, std::round(std::log(x) / std::log(2)))); }
/// <summary>
/// Performs a Catmull-Rom interpolation using the specified positions.
/// </summary>
template <typename T = float>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
catmullRom(T value1, T value2, T value3, T value4, T amount) {
// Using formula from http://www.mvps.org/directx/articles/catmull/
// Internally using doubles not to lose precision
auto amountSquared = amount * amount;
auto amountCubed = amountSquared * amount;
return (T)(0.5 * (2.0 * value2 +
(value3 - value1) * amount +
(2.0 * value1 - 5.0 * value2 + 4.0 * value3 - value4) * amountSquared +
(3.0 * value2 - value1 - 3.0 * value3 + value4) * amountCubed));
}
/// <summary>
/// Performs a Hermite spline interpolation.
/// </summary>
template <typename T = float>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
hermite(T value1, T tangent1, T value2, T tangent2, T amount) {
// All transformed to double not to lose precision
// Otherwise, for high numbers of param:amount the result is NaN instead of Infinity
T v1 = value1, v2 = value2, t1 = tangent1, t2 = tangent2, s = amount, result;
auto sCubed = s * s * s;
auto sSquared = s * s;
if (amount == 0) { return value1; }
if (amount == 1) { return value2; }
return (2 * v1 - 2 * v2 + t2 + t1) * sCubed + (3 * v2 - 3 * v1 - 2 * t1 - t2) * sSquared + t1 * s + v1;
}
/// <summary>
/// Interpolates between two values using a cubic equation (Hermite),
/// clamping the amount to 0 to 1
/// </summary>
template <typename T = double>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
smoothStep(T value1, T value2, T amount) {
return hermite(value1, (T)0, value2, (T)0, std::max(std::min(amount, (T)1), (T)0));
}
/// <summary>
/// Reduces a given angle to a value between π and -π.
/// </summary>
/// <param name="angle">The angle to reduce, in radians.</param>
/// <returns>The new angle, in radians.</returns>
template <typename T = float>
inline typename std::enable_if<std::is_arithmetic<T>::value, T>::type
wrapAngle(T angle) {
if ((angle > -constants::pi) && (angle <= constants::pi))
return angle;
angle %= constants::twoPi;
if (angle <= -constants::pi)
return angle + constants::twoPi;
if (angle > constants::pi)
return angle - constants::twoPi;
return angle;
}
template <typename T = double>
inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
isNonZeroAndValid(T self, float tolerance = constants::tolerance) { return !std::isinf(self) && !std::isnan(self) && std::fabs(self) > tolerance; }
}
template<typename TContainer, typename TPart>
struct Mappable {
virtual TContainer map(std::function<TPart(const TPart&)> f) const = 0;
};
template<typename TVector>
struct Points {
virtual std::size_t numPoints() const = 0;
virtual TVector getPoint(std::size_t n) const = 0;
};
//template<typename TSelf, typename TMatrix4x4>
//struct Transformable3D {
// virtual TSelf transform(const TMatrix4x4& mat) = 0;
//};
template <typename T = float>
struct Stats final {
std::size_t Count;
T Min;
T Max;
T Sum;
const Stats(std::size_t count, T min, T max, T sum) : Count(count), Min(min), Max(max), Sum(sum) {}
inline T average() const { return Sum / Count; }
inline T extents() const { return Max - Min; }
inline T middle() const { return extents() / 2 + Min; }
inline static Stats<T> stats(const std::initializer_list<T>& self) {
Stats<T> result = Stats<T>(0, T(), T(), T());
for (const auto& elem : self) {
result.Count += 1;
result.Min = min(result.Min, elem);
result.Max = max(result.Max, elem);
result.Sum += elem;
}
return result;
}
inline static T sum(const std::initializer_list<T>& self) { return stats<T>(self).Sum; }
inline static T average(const std::initializer_list<T>& self) { return stats<T>(self).average(); }
inline static T extents(const std::initializer_list<T>& self) { return stats<T>(self).extents(); }
inline static T middle(const std::initializer_list<T>& self) { return stats<T>(self).middle(); }
inline std::size_t hash() const { return hash::combine(Count, Min, Max, Sum); }
inline friend bool operator==(const Stats<T>& s, const Stats<T>& other) { return s.Count == other.Count && s.Min == other.Min && s.Max == other.Max && s.Sum == other.Sum; }
inline friend bool operator!=(const Stats<T>& s, const Stats<T>& other) { return !(s == other); }
inline friend std::ostream& operator<<(std::ostream& out, const Stats<T>& v) { return (out << "Stats(Count = " << v.Count << ", Min = " << v.Min << ", Max = " << v.Max << ", Sum = " << v.Sum << ")"); }
};
enum class ContainmentType { disjoint, contains, intersects };
enum class PlaneIntersectionType { front, back, intersecting };
struct Byte2 final {
std::uint8_t X;
std::uint8_t Y;
const Byte2(std::uint8_t x, std::uint8_t y) : X(x), Y(y) {}
const Byte2(std::uint8_t value) : X(value), Y(value) {}
inline static const Byte2 zero() { return Byte2(0); }
inline static const Byte2 minValue() { return Byte2(std::numeric_limits<std::uint8_t>::min()); }
inline static const Byte2 maxValue() { return Byte2(std::numeric_limits<std::uint8_t>::max()); }
inline Byte2 setX(std::uint8_t x) const { return Byte2(x, Y); }
inline Byte2 setY(std::uint8_t y) const { return Byte2(X, y); }
inline std::size_t hash() const { return hash::combine(X, Y); } //(Y)
inline bool almostEquals(const Byte2& x, float tolerance = constants::tolerance) const {
return std::fabs(X - x.X) < tolerance && std::fabs(Y - x.Y) < tolerance;
}
inline friend bool operator==(const Byte2& b, const Byte2& other) { return b.X == other.X && b.Y == other.Y; }
inline friend bool operator!=(const Byte2& b, const Byte2& other) { return !(b == other); }
inline friend std::ostream& operator<<(std::ostream& out, const Byte2& v) { return (out << "Byte2(X = " << v.X << ", Y = " << v.Y << ")"); }
};
struct Byte3 final {
std::uint8_t X;
std::uint8_t Y;
std::uint8_t Z;
const Byte3(std::uint8_t x, std::uint8_t y, std::uint8_t z) : X(x), Y(y), Z(z) {}
const Byte3(std::uint8_t value) : X(value), Y(value), Z(value) {}
inline static const Byte3 zero() { return Byte3(0); }
inline static const Byte3 minValue() { return Byte3(std::numeric_limits<std::uint8_t>::min()); }
inline static const Byte3 maxValue() { return Byte3(std::numeric_limits<std::uint8_t>::max()); }
inline Byte3 setX(std::uint8_t x) const { return Byte3(x, Y, Z); }
inline Byte3 setY(std::uint8_t y) const { return Byte3(X, y, Z); }
inline Byte3 setZ(std::uint8_t z) const { return Byte3(X, Y, z); }
inline std::size_t hash() const { return hash::combine(X, Y, Z); }
inline bool almostEquals(const Byte3& x, float tolerance = constants::tolerance) const {
return std::fabs(X - x.X) < tolerance && std::fabs(Y - x.Y) < tolerance && std::fabs(Z - x.Z) < tolerance;
}
inline friend bool operator==(const Byte3& b, const Byte3& other) { return b.X == other.X && b.Y == other.Y && b.Z == other.Z; }
inline friend bool operator!=(const Byte3& b, const Byte3& other) { return !(b == other); }
inline friend std::ostream& operator<<(std::ostream& out, const Byte3& v) { return (out << "Byte3(X = " << v.X << ", Y = " << v.Y << ", Z = " << v.Z << ")"); }
};
struct Byte4 final {
std::uint8_t X;
std::uint8_t Y;
std::uint8_t Z;
std::uint8_t W;
const Byte4(std::uint8_t x, std::uint8_t y, std::uint8_t z, std::uint8_t w) : X(x), Y(y), Z(z), W(w) {}
const Byte4(std::uint8_t value) : X(value), Y(value), Z(value), W(value) {}
inline static const Byte4 zero() { return Byte4(0); }
inline static const Byte4 minValue() { return Byte4(std::numeric_limits<std::uint8_t>::min()); }
inline static const Byte4 maxValue() { return Byte4(std::numeric_limits<std::uint8_t>::max()); }
inline Byte4 setX(std::uint8_t x) const { return Byte4(x, Y, Z, W); }
inline Byte4 setY(std::uint8_t y) const { return Byte4(X, y, Z, W); }
inline Byte4 setZ(std::uint8_t z) const { return Byte4(X, Y, z, W); }
inline Byte4 setW(std::uint8_t w) const { return Byte4(X, Y, Z, w); }
inline std::size_t hash() const { return hash::combine(X, Y, Z, W); }
inline bool almostEquals(const Byte4& x, float tolerance = constants::tolerance) const {
return std::fabs(X - x.X) < tolerance && std::fabs(Y - x.Y) < tolerance && std::fabs(Z - x.Z) < tolerance && std::fabs(W - x.W) < tolerance;
}
inline friend bool operator==(const Byte4& b, const Byte4& other) { return b.X == other.X && b.Y == other.Y && b.Z == other.Z && b.W == other.W; }
inline friend bool operator!=(const Byte4& b, const Byte4& other) { return !(b == other); }
inline friend std::ostream& operator<<(std::ostream& out, const Byte4& v) { return (out << "Byte4(X = " << v.X << ", Y = " << v.Y << ", Z = " << v.Z << ", W = " << v.W << ")"); }
};
struct ColorHDR final {
float R;
float G;
float B;
float A;
const ColorHDR(float r, float g, float b, float a) : R(r), G(g), B(b), A(a) {}
const ColorHDR(float value) : R(value), G(value), B(value), A(value) {}
inline static const ColorHDR zero() { return ColorHDR(0); }
inline static const ColorHDR minValue() { return ColorHDR(std::numeric_limits<float>::min()); }
inline static const ColorHDR maxValue() { return ColorHDR(std::numeric_limits<float>::max()); }
inline ColorHDR setR(float r) const { return ColorHDR(r, G, B, A); }
inline ColorHDR setG(float g) const { return ColorHDR(R, g, B, A); }
inline ColorHDR setB(float b) const { return ColorHDR(R, G, b, A); }
inline ColorHDR setA(float a) const { return ColorHDR(R, G, B, a); }
inline std::size_t hash() const { return hash::combine(R, G, B, A); }
inline bool almostEquals(const ColorHDR& x, float tolerance = constants::tolerance) const {
return std::fabs(R - x.R) < tolerance && std::fabs(G - x.G) < tolerance
&& std::fabs(B - x.B) < tolerance && std::fabs(A - x.A) < tolerance;
}
inline friend bool operator==(const ColorHDR& o, const ColorHDR& other) { return o.R == other.R && o.G == other.G && o.B == other.B && o.A == other.A; }
inline friend bool operator!=(const ColorHDR& o, const ColorHDR& other) { return !(o == other); }
inline friend std::ostream& operator<<(std::ostream& out, const ColorHDR& v) { return (out << "ColorHDR(R = " << v.R << ", G = " << v.G << ", B = " << v.B << ", A = " << v.A << ")"); }
};
struct ColorRGB final {
std::uint8_t R;
std::uint8_t G;
std::uint8_t B;
const ColorRGB(std::uint8_t r, std::uint8_t g, std::uint8_t b) : R(r), G(g), B(b) {}
const ColorRGB(std::uint8_t value) : R(value), G(value), B(value) {}
inline static const ColorRGB zero() { return ColorRGB(0); }
inline static const ColorRGB minValue() { return ColorRGB(std::numeric_limits<std::uint8_t>::min()); }
inline static const ColorRGB maxValue() { return ColorRGB(std::numeric_limits<std::uint8_t>::max()); }
inline ColorRGB setR(std::uint8_t r) const { return ColorRGB(r, G, B); }
inline ColorRGB setG(std::uint8_t g) const { return ColorRGB(R, g, B); }
inline ColorRGB setB(std::uint8_t b) const { return ColorRGB(R, G, b); }
inline std::size_t hash() const { return hash::combine(R, G, B); }
inline bool almostEquals(const ColorRGB& x, float tolerance = constants::tolerance) const {
return std::fabs(R - x.R) < tolerance && std::fabs(G - x.G) < tolerance && std::fabs(B - x.B) < tolerance;
}
inline friend bool operator==(const ColorRGB& o, const ColorRGB& other) { return o.R == other.R && o.G == other.G && o.B == other.B; }
inline friend bool operator!=(const ColorRGB& o, const ColorRGB& other) { return !(o == other); }
inline friend std::ostream& operator<<(std::ostream& out, const ColorRGB& v) { return (out << "ColorRGB(R = " << v.R << ", G = " << v.G << ", B = " << v.B << ")"); }
};
struct ColorRGBA final {
std::uint8_t R;
std::uint8_t G;
std::uint8_t B;
std::uint8_t A;
const ColorRGBA(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t a) : R(r), G(g), B(b), A(a) {}
const ColorRGBA(std::uint8_t value = 0) : R(value), G(value), B(value), A(value) {}
inline static const ColorRGBA zero() { return ColorRGBA(0); }
inline static const ColorRGBA minValue() { return ColorRGBA(std::numeric_limits<std::uint8_t>::min()); }
inline static const ColorRGBA maxValue() { return ColorRGBA(std::numeric_limits<std::uint8_t>::max()); }
inline static const ColorRGBA lightRed() { return ColorRGBA(255, 128, 128, 255); }
inline static const ColorRGBA darkRed() { return ColorRGBA(255, 0, 0, 255); }
inline static const ColorRGBA lightGreen() { return ColorRGBA(128, 255, 128, 255); }
inline static const ColorRGBA darkGreen() { return ColorRGBA(0, 255, 0, 255); }
inline static const ColorRGBA lightBlue() { return ColorRGBA(128, 128, 255, 255); }
inline static const ColorRGBA darkBlue() { return ColorRGBA(0, 0, 255, 255); }
inline ColorRGBA setR(std::uint8_t r) const { return ColorRGBA(r, G, B, A); }
inline ColorRGBA setG(std::uint8_t g) const { return ColorRGBA(R, g, B, A); }
inline ColorRGBA setB(std::uint8_t b) const { return ColorRGBA(R, G, b, A); }
inline ColorRGBA setA(std::uint8_t a) const { return ColorRGBA(R, G, B, a); }
inline std::size_t hash() const { return hash::combine(R, G, B, A); }
inline bool almostEquals(const ColorRGBA& x, float tolerance = constants::tolerance) const {
return std::fabs(R - x.R) < tolerance && std::fabs(G - x.G) < tolerance
&& std::fabs(B - x.B) < tolerance && std::fabs(A - x.A) < tolerance;
}
inline friend bool operator==(const ColorRGBA& c, const ColorRGBA& other) { return c.R == other.R && c.G == other.G && c.B == other.B && c.A == other.A; }
inline friend bool operator!=(const ColorRGBA& c, const ColorRGBA& other) { return !(c == other); }
inline friend std::ostream& operator<<(std::ostream& out, const ColorRGBA& v) { return (out << "ColorRGBA(R = " << v.R << ", G = " << v.G << ", B = " << v.B << ", A = " << v.A << ")"); }
};
struct Complex final {
double Real;
double Imaginary;
const Complex(double real, double imaginary) : Real(real), Imaginary(imaginary) {}
const Complex(double value) : Real(value), Imaginary(value) {}
inline static const Complex zero() { return Complex(0.0); }
inline static const Complex minValue() { return Complex(std::numeric_limits<double>::min()); }
inline static const Complex maxValue() { return Complex(std::numeric_limits<double>::max()); }
inline static const Complex one() { return Complex(1); }
inline static const Complex unitReal() { return Complex(1.0, 0.0); }
inline static const Complex unitImaginary() { return Complex(0.0, 1.0); }
inline Complex setReal(double real) const { return Complex(real, Imaginary); }
inline Complex setImaginary(double imaginary) const { return Complex(Real, imaginary); }
inline std::size_t hash() const { return hash::combine(Real, Imaginary); }
inline bool almostEquals(const Complex& x, float tolerance = constants::tolerance) const {
return std::fabs(Real - x.Real) <= tolerance && std::fabs(Imaginary - x.Imaginary) <= tolerance;
}
inline static double dot(const Complex& value1, const Complex& value2) { return value1.Real * value2.Real + value1.Imaginary * value2.Imaginary; }
inline double dot(const Complex& value) const { return dot(*this, value); }
inline bool almostZero(float tolerance = constants::tolerance) const {
return std::fabs(Real) < tolerance
&& std::fabs(Imaginary) < tolerance;
}
inline double minComponent() const { return std::min(Real, Imaginary); }
inline double maxComponent() const { return std::max(Real, Imaginary); }
inline double sumComponents() const { return Real + Imaginary; }
inline double sumSqrComponents() const { return Real * Real + Imaginary * Imaginary; }
inline double productComponents() const { return Real * Imaginary; }
inline double getComponent(int n) const { return n == 0 ? Real : Imaginary; }
inline bool anyComponentNegative() const { return minComponent() < 0; }
inline double magnitudeSquared() const { return sumSqrComponents(); }
inline double magnitude() const { return std::sqrt(magnitudeSquared()); }
inline bool isnan() const { return std::isnan(Real) || std::isnan(Imaginary); }
inline bool isinf() const { return std::isinf(Real) || std::isinf(Imaginary); }
inline int compare(const Complex& x) const { return std::signbit(magnitudeSquared() - x.magnitudeSquared()); }
inline friend Complex operator-(const Complex& o) { return { -o.Real, -o.Imaginary }; }
inline friend bool operator==(const Complex& o, const Complex& other) { return o.Real == other.Real && o.Imaginary == other.Imaginary; }
inline friend bool operator!=(const Complex& o, const Complex& other) { return !(o == other); }
inline friend std::ostream& operator<<(std::ostream& out, const Complex& v) { return (out << "Complex(Real = " << v.Real << ", Imaginary = " << v.Imaginary << ")"); }
inline friend Complex operator+(const Complex& lhs, const Complex& rhs) { return Complex(lhs.Real + rhs.Real, lhs.Imaginary + rhs.Imaginary); }
inline friend Complex operator+(const Complex& lhs, float rhs) { return Complex(lhs.Real + rhs, lhs.Imaginary + rhs); }
inline friend Complex operator+(float lhs, const Complex& rhs) { return Complex(lhs + rhs.Real, lhs + rhs.Imaginary); }
inline friend Complex operator-(const Complex& lhs, const Complex& rhs) { return Complex(lhs.Real - rhs.Real, lhs.Imaginary - rhs.Imaginary); }
inline friend Complex operator-(const Complex& lhs, float rhs) { return Complex(lhs.Real - rhs, lhs.Imaginary - rhs); }
inline friend Complex operator-(float lhs, const Complex& rhs) { return Complex(lhs - rhs.Real, lhs - rhs.Imaginary); }
inline friend Complex operator*(const Complex& lhs, const Complex& rhs) { return Complex(lhs.Real * rhs.Real, lhs.Imaginary * rhs.Imaginary); }
inline friend Complex operator*(const Complex& lhs, float rhs) { return Complex(lhs.Real * rhs, lhs.Imaginary * rhs); }
inline friend Complex operator*(float lhs, const Complex& rhs) { return Complex(lhs * rhs.Real, lhs * rhs.Imaginary); }
inline friend Complex operator/(const Complex& lhs, const Complex& rhs) { return Complex(lhs.Real / rhs.Real, lhs.Imaginary / rhs.Imaginary); }
inline friend Complex operator/(const Complex& lhs, float rhs) { return Complex(lhs.Real / rhs, lhs.Imaginary / rhs); }
inline friend Complex operator/(float lhs, const Complex& rhs) { return Complex(lhs / rhs.Real, lhs / rhs.Imaginary); }
inline friend bool operator<(const Complex& x0, const Complex& x1) { return x0.compare(x1) < 0; }
inline friend bool operator<=(const Complex& x0, const Complex& x1) { return x0.compare(x1) <= 0; }
inline friend bool operator>(const Complex& x0, const Complex& x1) { return x0.compare(x1) > 0; }
inline friend bool operator>=(const Complex& x0, const Complex& x1) { return x0.compare(x1) >= 0; }
};
template <typename T = float>
struct Euler final {
T Yaw;
T Pitch;
T Roll;
const Euler(T yaw, T pitch, T roll) : Yaw(yaw), Pitch(pitch), Roll(roll) {}
const Euler(T value) : Yaw(value), Pitch(value), Roll(value) {}
inline static const Euler zero() { return Euler(0); }
inline static const Euler minValue() { return Euler(std::numeric_limits<T>::min()); }
inline static const Euler maxValue() { return Euler(std::numeric_limits<T>::max()); }
inline static const Euler one() { return Euler(1); }
inline static const Euler unitYaw() { return Euler(1, 0, 0); }
inline static const Euler unitPitch() { return Euler(0, 1, 0); }
inline static const Euler unitRoll() { return Euler(0, 0, 1); }
inline Euler setYaw(T x) const { return Euler(x, Pitch, Roll); }
inline Euler setPitch(T y) const { return Euler(Yaw, y, Roll); }
inline Euler setRoll(T z) const { return Euler(Yaw, Pitch, z); }
inline std::size_t hash() const { return hash::combine((Yaw), (Pitch), (Roll)); }
inline bool almostEquals(const Euler& x, float tolerance = constants::tolerance) const {
return std::fabs(Yaw - x.Yaw) <= tolerance
&& std::fabs(Pitch - x.Pitch) <= tolerance
&& std::fabs(Roll - x.Roll) <= tolerance;
}
inline static T dot(const Euler& value1, const Euler& value2) {
return value1.Yaw * value2.Yaw + value1.Pitch * value2.Pitch + value1.Roll * value2.Roll;
}
inline T dot(const Euler& value) const { return dot(*this, value); }
inline bool almostZero(float tolerance = constants::tolerance) const {
return std::fabs(Yaw) < tolerance
&& std::fabs(Pitch) < tolerance
&& std::fabs(Roll) < tolerance;
}
inline T minComponent() const { return std::min(std::min(Yaw, Pitch), Roll); }
inline T maxComponent() const { return std::max(std::max(Yaw, Pitch), Roll); }
inline T sumComponents() const { return Yaw + Pitch + Roll; }
inline T sumSqrComponents() const { return Yaw * Yaw + Pitch * Pitch + Roll * Roll; }
inline T productComponents() const { return Yaw * Pitch * Roll; }
inline T getComponent(int n) const { return n == 0 ? Yaw : n == 1 ? Pitch : Roll; }
inline bool anyComponentNegative() const { return minComponent() < 0.0; }
inline T magnitudeSquared() const { return sumSqrComponents(); }
inline T magnitude() const { return std::sqrt(magnitudeSquared()); }
inline bool isnan() const { return std::isnan(Yaw) || std::isnan(Pitch) || std::isnan(Roll); }
inline bool isinf() const { return std::isinf(Yaw) || std::isinf(Pitch) || std::isinf(Roll); }
inline int compare(const Euler& x) const { return std::signbit(magnitudeSquared() - x.magnitudeSquared()); }
inline friend Euler operator-(const Euler& o) { return { -o.Yaw, -o.Pitch, -o.Roll }; }
inline friend bool operator==(const Euler& o, const Euler& other) { return o.Yaw == other.Yaw && o.Pitch == other.Pitch && o.Roll == other.Roll; }
inline friend bool operator!=(const Euler& o, const Euler& other) { return !(o == other); }
inline friend std::ostream& operator<<(std::ostream& out, const Euler& v) { return (out << "Euler(Yaw = " << v.Yaw << ", Pitch = " << v.Pitch << ", Roll = " << v.Roll << ")"); }
inline friend Euler operator+(const Euler& lhs, const Euler& rhs) { return Euler(lhs.Yaw + rhs.Yaw, lhs.Pitch + rhs.Pitch, lhs.Roll + rhs.Roll); }
inline friend Euler operator+(const Euler& lhs, T rhs) { return Euler(lhs.Yaw + rhs, lhs.Pitch + rhs, lhs.Roll + rhs); }
inline friend Euler operator+(T lhs, const Euler& rhs) { return Euler(lhs + rhs.Yaw, lhs + rhs.Pitch, lhs + rhs.Roll); }
//inline friend Euler operator-(const Vector3<T>& rhs) { return Euler(lhs.Yaw - rhs.X, lhs.Pitch - rhs.Y, lhs.Roll - rhs.Z); }
inline Euler operator-(T rhs) { return Euler(Yaw - rhs, Pitch - rhs, Roll - rhs); }
inline friend Euler operator-(T lhs, const Euler& rhs) { return Euler(lhs - rhs.Yaw, lhs - rhs.Pitch, lhs - rhs.Roll); }
inline friend Euler operator *(const Euler& value1, const Euler& value2) { return Euler(value1.Yaw * value2.Yaw, value1.Pitch * value2.Pitch, value1.Roll * value2.Roll); }
inline friend Euler operator *(const Euler& value1, T value2) { return Euler(value1.Yaw * value2, value1.Pitch * value2, value1.Roll * value2); }
inline friend Euler operator *(T value1, Euler& value2) { return Euler(value1 * value2.Yaw, value1 * value2.Pitch, value1 * value2.Roll); }
inline friend Euler operator/(const Euler& lhs, const Euler& rhs) { return Euler(lhs.Yaw / rhs.Yaw, lhs.Pitch / rhs.Pitch, lhs.Roll / rhs.Roll); }
inline friend Euler operator/(const Euler& lhs, T rhs) { return Euler(lhs.Yaw / rhs, lhs.Pitch / rhs, lhs.Roll / rhs); }
inline friend Euler operator/(T lhs, const Euler& rhs) { return Euler(lhs / rhs.Yaw, lhs / rhs.Pitch, lhs / rhs.Roll); }
inline friend bool operator<(const Euler& x0, const Euler& x1) { return x0.compare(x1) < 0; }
inline friend bool operator<=(const Euler& x0, const Euler& x1) { return x0.compare(x1) <= 0; }
inline friend bool operator>(const Euler& x0, const Euler& x1) { return x0.compare(x1) > 0; }
inline friend bool operator>=(const Euler& x0, const Euler& x1) { return x0.compare(x1) >= 0; }
};
#define FEuler Euler<float>
#define DEuler Euler<double>
struct Int2 final {
std::int32_t X;
std::int32_t Y;
const Int2(std::int32_t x, std::int32_t y) : X(x), Y(y) {}
const Int2(std::int32_t value = 0) : X(value), Y(value) {}
inline static const Int2 zero() { return Int2(0); }
inline static const Int2 minValue() { return Int2(std::numeric_limits<std::int32_t>::min()); }
inline static const Int2 maxValue() { return Int2(std::numeric_limits<std::int32_t>::max()); }
inline static const Int2 one() { return Int2(1); }
inline static const Int2 unitX() { return Int2(1, 0); }
inline static const Int2 unitY() { return Int2(0, 1); }
inline Int2 setX(std::int32_t x) const { return Int2(x, Y); }
inline Int2 setY(std::int32_t y) const { return Int2(X, y); }
inline std::size_t hash() const { return hash::combine(X, Y); }
inline bool almostEquals(const Int2& x, float tolerance = constants::tolerance) const {
return std::fabs(X - x.X) <= tolerance && std::fabs(Y - x.Y) <= tolerance;
}
inline static std::int32_t dot(const Int2& value1, const Int2& value2) { return value1.X * value2.X + value1.Y * value2.Y; }
inline std::int32_t dot(const Int2& value) const { return dot(*this, value); }
inline bool almostZero(float tolerance = constants::tolerance) const {
return std::fabs(X) < tolerance && std::fabs(Y) < tolerance;
}
inline std::int32_t minComponent() const { return std::min(X, Y); }
inline std::int32_t maxComponent() const { return std::max(X, Y); }
inline std::int32_t sumComponents() const { return X + Y; }
inline std::int32_t sumSqrComponents() const { return X * X + Y * Y; }
inline std::int32_t productComponents() const { return X * Y; }
inline std::int32_t getComponent(int n) const { return n == 0 ? X : Y; }
inline double magnitudeSquared() const { return sumSqrComponents(); }
inline double magnitude() const { return std::sqrt(magnitudeSquared()); }
inline bool anyComponentNegative() const { return minComponent() < 0.0; }
/* inline bool isnan() const { return std::isnan(X) || std::isnan(Y); }
inline bool isinf() const { return std::isinf(X) || std::isinf(Y); }*/
inline int compare(const Int2& x) const { return std::signbit(magnitudeSquared() - x.magnitudeSquared()); }
inline friend Int2 operator-(const Int2& l) { return Int2(- l.X, -l.Y); }
inline friend bool operator==(const Int2& o, const Int2& other) { return o.X == other.X && o.Y == other.Y; }
inline friend bool operator!=(const Int2& o, const Int2& other) { return !(o == other); }
inline friend std::ostream& operator<<(std::ostream& out, const Int2& v) { return (out << "Int2(X = " << v.X << ", Y = " << v.Y << ")"); }
inline friend Int2 operator+(const Int2& lhs, const Int2& rhs) { return Int2(lhs.X + rhs.X, lhs.Y + rhs.Y); }
inline friend Int2 operator+(const Int2& lhs, std::int32_t rhs) { return Int2(lhs.X + rhs, lhs.Y + rhs); }
inline friend Int2 operator+(std::int32_t lhs, const Int2& rhs) { return Int2(lhs + rhs.X, lhs + rhs.Y); }
inline friend Int2 operator-(const Int2& lhs, const Int2& rhs) { return Int2(lhs.X - rhs.X, lhs.Y - rhs.Y); }
inline friend Int2 operator-(const Int2& lhs, std::int32_t rhs) { return Int2(lhs.X - rhs, lhs.Y - rhs); }
inline friend Int2 operator-(std::int32_t lhs, const Int2& rhs) { return Int2(lhs - rhs.X, lhs - rhs.Y); }
inline friend Int2 operator*(const Int2& lhs, const Int2& rhs) { return Int2(lhs.X * rhs.X, lhs.Y * rhs.Y); }
inline friend Int2 operator*(const Int2& lhs, std::int32_t rhs) { return Int2(lhs.X * rhs, lhs.Y * rhs); }
inline friend Int2 operator*(std::int32_t lhs, const Int2& rhs) { return Int2(lhs * rhs.X, lhs * rhs.Y); }
inline friend Int2 operator/(const Int2& lhs, const Int2& rhs) { return Int2(lhs.X / rhs.X, lhs.Y / rhs.Y); }
inline friend Int2 operator/(const Int2& lhs, std::int32_t rhs) { return Int2(lhs.X / rhs, lhs.Y / rhs); }
inline friend Int2 operator/(std::int32_t lhs, const Int2& rhs) { return Int2(lhs / rhs.X, lhs / rhs.Y); }
inline friend bool operator<(const Int2& x0, const Int2& x1) { return x0.compare(x1) < 0; }
inline friend bool operator<=(const Int2& x0, const Int2& x1) { return x0.compare(x1) <= 0; }
inline friend bool operator>(const Int2& x0, const Int2& x1) { return x0.compare(x1) > 0; }
inline friend bool operator>=(const Int2& x0, const Int2& x1) { return x0.compare(x1) >= 0; }
};
struct Int3 final {
std::int32_t X;
std::int32_t Y;
std::int32_t Z;
const Int3(std::int32_t x, std::int32_t y, std::int32_t z) : X(x), Y(y), Z(z) {}
const Int3(std::int32_t value) : X(value), Y(value), Z(value) {}
inline static const Int3 zero() { return Int3(0); }
inline static const Int3 minValue() { return Int3(std::numeric_limits<std::int32_t>::min()); }
inline static const Int3 maxValue() { return Int3(std::numeric_limits<std::int32_t>::max()); }
inline static const Int3 one() { return Int3(1); }
inline static const Int3 unitX() { return Int3(1, 0, 0); }
inline static const Int3 unitY() { return Int3(0, 1, 0); }
inline static const Int3 unitZ() { return Int3(0, 0, 1); }
inline Int3 setX(std::int32_t x) const { return Int3(x, Y, Z); }
inline Int3 setY(std::int32_t y) const { return Int3(X, y, Z); }
inline Int3 setZ(std::int32_t z) const { return Int3(X, Y, z); }
inline std::size_t hash() const { return hash::combine(X, Y, Z); }
inline bool almostEquals(const Int3& x, float tolerance = constants::tolerance) const {
return std::fabs(X - x.X) <= tolerance
&& std::fabs(Y - x.Y) <= tolerance
&& std::fabs(Z - x.Z) <= tolerance;
}
inline static std::int32_t dot(const Int3& value1, const Int3& value2) {
return value1.X * value2.X + value1.Y * value2.Y + value1.Z * value2.Z;
}
inline std::int32_t dot(const Int3& value) const { return dot(*this, value); }
inline bool almostZero(float tolerance = constants::tolerance) const {
return std::fabs(X) < tolerance
&& std::fabs(Y) < tolerance
&& std::fabs(Z) < tolerance;
}
inline std::int32_t minComponent() const { return std::min(std::min(X, Y), Z); }
inline std::int32_t maxComponent() const { return std::max(std::max(X, Y), Z); }
inline std::int32_t sumComponents() const { return X + Y + Z; }
inline std::int32_t sumSqrComponents() const { return X * X + Y * Y + Z * Z; }
inline std::int32_t productComponents() const { return X * Y * Z; }
inline std::int32_t getComponent(int n) const { return n == 0 ? X : n == 1 ? Y : Z; }
inline bool anyComponentNegative() const { return minComponent() < 0.0; }
inline double magnitudeSquared() const { return sumSqrComponents(); }
inline double magnitude() const { return std::sqrt(magnitudeSquared()); }
/* inline bool isnan() const { return std::isnan(X) || std::isnan(Y) || std::isnan(Z); }
inline bool isinf() const { return std::isinf(X) || std::isinf(Y) || std::isinf(Z); }*/
inline int compare(const Int3& x) const { return std::signbit(magnitudeSquared() - x.magnitudeSquared()); }
inline friend Int3 operator-(const Int3& l) { return { -l.X, -l.Y, -l.Z }; }
inline friend bool operator==(const Int3& l, const Int3& other) { return l.X == other.X && l.Y == other.Y && l.Z == other.Z; }
inline friend bool operator!=(const Int3& l, const Int3& other) { return !(l == other); }
inline friend std::ostream& operator<<(std::ostream& out, const Int3& v) { return (out << "Int3(X = " << v.X << ", Y = " << v.Y << ", Z = " << v.Z << ")"); }
inline friend Int3 operator+(const Int3& lhs, const Int3& rhs) { return Int3(lhs.X + rhs.X, lhs.Y + rhs.Y, lhs.Z + rhs.Z); }
inline friend Int3 operator+(const Int3& lhs, std::int32_t rhs) { return Int3(lhs.X + rhs, lhs.Y + rhs, lhs.Z + rhs); }
inline friend Int3 operator+(std::int32_t lhs, const Int3& rhs) { return Int3(lhs + rhs.X, lhs + rhs.Y, lhs + rhs.Z); }
inline friend Int3 operator-(const Int3& lhs, const Int3& rhs) { return Int3(lhs.X - rhs.X, lhs.Y - rhs.Y, lhs.Z - rhs.Z); }
inline friend Int3 operator-(const Int3& lhs, std::int32_t rhs) { return Int3(lhs.X - rhs, lhs.Y - rhs, lhs.Z - rhs); }
inline friend Int3 operator-(std::int32_t lhs, const Int3& rhs) { return Int3(lhs - rhs.X, lhs - rhs.Y, lhs - rhs.Z); }
inline friend Int3 operator*(const Int3& lhs, const Int3& rhs) { return Int3(lhs.X * rhs.X, lhs.Y * rhs.Y, lhs.Z * rhs.Z); }
inline friend Int3 operator*(const Int3& lhs, std::int32_t rhs) { return Int3(lhs.X * rhs, lhs.Y * rhs, lhs.Z * rhs); }
inline friend Int3 operator*(std::int32_t lhs, const Int3& rhs) { return Int3(lhs * rhs.X, lhs * rhs.Y, lhs * rhs.Z); }
inline friend Int3 operator/(const Int3& lhs, const Int3& rhs) { return Int3(lhs.X / rhs.X, lhs.Y / rhs.Y, lhs.Z / rhs.Z); }
inline friend Int3 operator/(const Int3& lhs, std::int32_t rhs) { return Int3(lhs.X / rhs, lhs.Y / rhs, lhs.Z / rhs); }
inline friend Int3 operator/(std::int32_t lhs, const Int3& rhs) { return Int3(lhs / rhs.X, lhs / rhs.Y, lhs / rhs.Z); }
inline friend bool operator<(const Int3& x0, const Int3& x1) { return x0.compare(x1) < 0; }
inline friend bool operator<=(const Int3& x0, const Int3& x1) { return x0.compare(x1) <= 0; }
inline friend bool operator>(const Int3& x0, const Int3& x1) { return x0.compare(x1) > 0; }
inline friend bool operator>=(const Int3& x0, const Int3& x1) { return x0.compare(x1) >= 0; }
};
struct Int4 final {
std::int32_t X;
std::int32_t Y;
std::int32_t Z;
std::int32_t W;
const Int4(std::int32_t x, std::int32_t y, std::int32_t z, std::int32_t w) : X(x), Y(y), Z(z), W(w) {}
const Int4(std::int32_t value) : X(value), Y(value), Z(value), W(value) {}
inline static const Int4 zero() { return Int4(0); }
inline static const Int4 minValue() { return Int4(std::numeric_limits<std::int32_t>::min()); }
inline static const Int4 maxValue() { return Int4(std::numeric_limits<std::int32_t>::max()); }
inline static const Int4 one() { return Int4(1); }
inline static const Int4 unitX() { return Int4(1, 0, 0, 0); }
inline static const Int4 unitY() { return Int4(0, 1, 0, 0); }
inline static const Int4 unitZ() { return Int4(0, 0, 1, 0); }
inline static const Int4 unitW() { return Int4(0, 0, 0, 1); }
inline Int4 setX(std::int32_t x) const { return Int4(x, Y, Z, W); }
inline Int4 setY(std::int32_t y) const { return Int4(X, y, Z, W); }
inline Int4 setZ(std::int32_t z) const { return Int4(X, Y, z, W); }
inline Int4 setW(std::int32_t w) const { return Int4(X, Y, Z, w); }
inline std::size_t hash() const { return hash::combine(X, Y, Z, W); }
inline bool almostEquals(const Int4& x, float tolerance = constants::tolerance) const {
return std::fabs(X - x.X) <= tolerance
&& std::fabs(Y - x.Y) <= tolerance
&& std::fabs(Z - x.Z) <= tolerance
&& std::fabs(W - x.W) <= tolerance;
}
inline static std::int32_t dot(const Int4& value1, const Int4& value2) {
return value1.X * value2.X + value1.Y * value2.Y + value1.Z * value2.Z + value1.W * value2.W;
}
inline std::int32_t dot(const Int4& value) const { return dot(*this, value); }
inline bool almostZero(float tolerance = constants::tolerance) const {
return std::fabs(X) < tolerance
&& std::fabs(Y) < tolerance
&& std::fabs(Z) < tolerance
&& std::fabs(W) < tolerance;
}
inline std::int32_t minComponent() const { return std::min(std::min(std::min(X, Y), Z), W); }
inline std::int32_t maxComponent() const { return std::max(std::max(std::max(X, Y), Z), W); }
inline std::int32_t sumComponents() const { return X + Y + Z + W; }
inline std::int32_t sumSqrComponents() const { return X * X + Y * Y + Z * Z + W * W; }
inline std::int32_t productComponents() const { return X * Y * Z * W; }
inline std::int32_t getComponent(int n) const { return n == 0 ? X : n == 1 ? Y : n == 2 ? Z : W; }
inline bool anyComponentNegative() const { return minComponent() < 0.0; }
inline double magnitudeSquared() const { return sumSqrComponents(); }
inline double magnitude() const { return std::sqrt(magnitudeSquared()); }
inline int compare(const Int4& x) const { return std::signbit(magnitudeSquared() - x.magnitudeSquared()); }
inline friend Int4 operator-(const Int4& l) { return { -l.X, -l.Y, -l.Z, -l.W }; }
inline friend bool operator==(const Int4& l, const Int4& other) { return l.X == other.X && l.Y == other.Y && l.Z == other.Z && l.W == other.W; }
inline friend bool operator!=(const Int4& l, const Int4& other) { return !(l == other); }
inline friend std::ostream& operator<<(std::ostream& out, const Int4& v) { return (out << "Int4(X = " << v.X << ", Y = " << v.Y << ", Z = " << v.Z << ", W = " << v.W << ")"); }
inline friend Int4 operator+(const Int4& lhs, const Int4& rhs) { return Int4(lhs.X + rhs.X, lhs.Y + rhs.Y, lhs.Z + rhs.Z, lhs.W + rhs.W); }
inline friend Int4 operator+(const Int4& lhs, std::int32_t rhs) { return Int4(lhs.X + rhs, lhs.Y + rhs, lhs.Z + rhs, lhs.W + rhs); }
inline friend Int4 operator+(std::int32_t lhs, const Int4& rhs) { return Int4(lhs + rhs.X, lhs + rhs.Y, lhs + rhs.Z, lhs + rhs.W); }
inline friend Int4 operator-(const Int4& lhs, const Int4& rhs) { return Int4(lhs.X - rhs.X, lhs.Y - rhs.Y, lhs.Z - rhs.Z, lhs.W - rhs.W); }
inline friend Int4 operator-(const Int4& lhs, std::int32_t rhs) { return Int4(lhs.X - rhs, lhs.Y - rhs, lhs.Z - rhs, lhs.W - rhs); }
inline friend Int4 operator-(std::int32_t lhs, const Int4& rhs) { return Int4(lhs - rhs.X, lhs - rhs.Y, lhs - rhs.Z, lhs - rhs.W); }
inline friend Int4 operator*(const Int4& lhs, const Int4& rhs) { return Int4(lhs.X * rhs.X, lhs.Y * rhs.Y, lhs.Z * rhs.Z, lhs.W * rhs.W); }
inline friend Int4 operator*(const Int4& lhs, std::int32_t rhs) { return Int4(lhs.X * rhs, lhs.Y * rhs, lhs.Z * rhs, lhs.W * rhs); }
inline friend Int4 operator*(std::int32_t lhs, const Int4& rhs) { return Int4(lhs * rhs.X, lhs * rhs.Y, lhs * rhs.Z, lhs * rhs.W); }
inline friend Int4 operator/(const Int4& lhs, const Int4& rhs) { return Int4(lhs.X / rhs.X, lhs.Y / rhs.Y, lhs.Z / rhs.Z, lhs.W / rhs.W); }
inline friend Int4 operator/(const Int4& lhs, std::int32_t rhs) { return Int4(lhs.X / rhs, lhs.Y / rhs, lhs.Z / rhs, lhs.W / rhs); }
inline friend Int4 operator/(std::int32_t lhs, const Int4& rhs) { return Int4(lhs / rhs.X, lhs / rhs.Y, lhs / rhs.Z, lhs / rhs.W); }
inline friend bool operator<(const Int4& x0, const Int4& x1) { return x0.compare(x1) < 0; }
inline friend bool operator<=(const Int4& x0, const Int4& x1) { return x0.compare(x1) <= 0; }
inline friend bool operator>(const Int4& x0, const Int4& x1) { return x0.compare(x1) > 0; }
inline friend bool operator>=(const Int4& x0, const Int4& x1) { return x0.compare(x1) >= 0; }
};
template <typename T = float>
struct Interval final {
T Min;
T Max;
const Interval(T min, T max) : Min(min), Max(max) {}
const Interval(T value) : Min(value), Max(value) {}
inline static const Interval zero() { return Interval(0); }
inline static const Interval minValue() { return Interval(std::numeric_limits<T>::min()); }
inline static const Interval maxValue() { return Interval(std::numeric_limits<T>::max()); }
inline static const Interval empty() { return Interval(std::numeric_limits<T>::max(), std::numeric_limits<T>::min()); }
inline Interval setMin(T x) const { return Interval(x, Max); }
inline Interval setMax(T x) const { return Interval(Min, x); }
inline std::size_t hash() const { return hash::combine((Min), (Max)); }
inline bool almostEquals(const Interval& x, float tolerance = constants::tolerance) const {
return std::fabs(Min - x.Min) <= tolerance
&& std::fabs(Max - x.Max) <= tolerance;
}
inline T extent() const { return Max - Min; };
inline T center() const { return (Max + Min) * 0.5; };
inline T magnitudeSquared() const { T ext = extent(); return ext * ext; }
inline T magnitude() const { return std::sqrt(magnitudeSquared()); }
inline bool isnan() const { return std::isnan(Min) || std::isnan(Max); }
inline bool isinf() const { return std::isinf(Min) || std::isinf(Max); }
inline int compare(const Interval& x) const { return std::signbit(magnitudeSquared() - x.magnitudeSquared()); }
inline Interval merge(const Interval& other) const { return { std::min(Min, other.Min), std::max(Max, other.Max) }; }
inline Interval merge(T other) const { return { std::min(Min, other), std::max(Max, other) }; }
inline Interval intersection(const Interval& other) const { return { std::max(Min, other.Min), std::min(Max, other.Max) }; }
inline friend bool operator==(const Interval& o, const Interval& other) { return o.Min == other.Min && o.Max == other.Max; }
inline friend bool operator!=(const Interval& o, const Interval& other) { return !(o == other); }
inline friend std::ostream& operator<<(std::ostream& out, const Interval& v) { return (out << "Interval(Min = " << v.Min << ", Max = " << v.Max << ")"); }
inline friend Interval operator+(const Interval& value1, const Interval& value2) { return value1.merge(value2); }
inline friend Interval operator+(const Interval& value1, const T value2) { return value1.merge(value2); }
inline friend Interval operator-(const Interval& value1, const T value2) { return value1.intersection(value2); }
inline friend bool operator<(const Interval& x0, const Interval& x1) { return x0.compare(x1) < 0; }
inline friend bool operator<=(const Interval& x0, const Interval& x1) { return x0.compare(x1) <= 0; }
inline friend bool operator>(const Interval& x0, const Interval& x1) { return x0.compare(x1) > 0; }
inline friend bool operator>=(const Interval& x0, const Interval& x1) { return x0.compare(x1) >= 0; }
};
#define FInterval Interval<float>
#define DInterval Interval<double>
template <typename T = float>
struct Vector2 final {
T X;
T Y;
const Vector2(T x, T y) : X(x), Y(y) {}
const Vector2(T value = 0) : X(value), Y(value) {}
inline static const Vector2<T> minValue() { return Vector2<T>(std::numeric_limits<T>::min()); }
inline static const Vector2<T> maxValue() { return Vector2<T>(std::numeric_limits<T>::max()); }
inline static const Vector2<T> zero() { return Vector2<T>(0); }
inline static const Vector2<T> one() { return Vector2<T>(1); }
inline static const Vector2<T> unitX() { return Vector2<T>(1, 0); }
inline static const Vector2<T> unitY() { return Vector2<T>(0, 1); }
inline T minComponent() const { return std::min(X, Y); }
inline T maxComponent() const { return std::max(X, Y); }
inline T sumComponents() const { return X + Y; }
inline T sumSqrComponents() const { return X * X + Y * Y; }
inline T productComponents() const { return X * Y; }
inline T getComponent(int n) const { return n == 0 ? X : Y; }
inline T magnitudeSquared() const { return sumSqrComponents(); }
inline T magnitude() const { return std::sqrt(magnitudeSquared()); }
inline bool anyComponentNegative() const { return minComponent() < 0.0; }
inline bool isnan() const { return std::isnan(X) || std::isnan(Y); }
inline bool isinf() const { return std::isinf(X) || std::isinf(Y); }
inline Vector2<T> abs() const { return Vector2<T>(std::abs(X), std::abs(Y)); }
inline Vector2<T> acos() const { return Vector2<T>(std::acos(X), std::acos(Y)); }
inline Vector2<T> asin() const { return Vector2<T>(std::asin(X), std::asin(Y)); }
inline Vector2<T> atan() const { return Vector2<T>(std::atan(X), std::atan(Y)); }
inline Vector2<T> cos() const { return Vector2<T>(std::cos(X), std::cos(Y)); }
inline Vector2<T> cosh() const { return Vector2<T>(std::cosh(X), std::cosh(Y)); }
inline Vector2<T> exp() const { return Vector2<T>(std::exp(X), std::exp(Y)); }
inline Vector2<T> log() const { return Vector2<T>(std::log(X), std::log(Y)); }
inline Vector2<T> log10() const { return Vector2<T>(std::log10(X), std::log10(Y)); }
inline Vector2<T> sin() const { return Vector2<T>(std::sin(X), std::sin(Y)); }
inline Vector2<T> sinh() const { return Vector2<T>(std::sinh(X), std::sinh(Y)); }
inline Vector2<T> sqrt() const { return Vector2<T>(std::sqrt(X), std::sqrt(Y)); }
inline Vector2<T> tan() const { return Vector2<T>(std::tan(X), std::tan(Y)); }
inline Vector2<T> tanh() const { return Vector2<T>(std::tanh(X), std::tanh(Y)); }
inline Vector2<T> inverse() const { return Vector2<T>((1 / X), (1 / Y)); }
inline Vector2<T> ceiling() const { return Vector2<T>(std::ceil(X), std::ceil(Y)); }
inline Vector2<T> floor() const { return Vector2<T>(std::floor(X), std::floor(Y)); }
inline Vector2<T> round() const { return Vector2<T>(std::round(X), std::round(Y)); }
inline Vector2<T> truncate() const { return Vector2<T>(std::trunc(X), std::trunc(Y)); }
inline Vector2<T> sqr() const { return Vector2<T>(X * X, Y * Y); }
inline Vector2<T> cube() const { return Vector2<T>(X * X * X, Y * Y * Y); }
inline Vector2<T> toRadians() const { return Vector2<T>(X * constants::degreesToRadians, Y * constants::degreesToRadians); }
inline Vector2<T> toDegrees() const { return Vector2<T>(X * constants::radiansToDegrees, Y * constants::radiansToDegrees); }
inline T lengthSquared() const { return sumSqrComponents(); }
inline T length() const { return std::sqrt(lengthSquared()); }
inline T distance(const Vector2<T>& v2) const { return (*this - v2).length(); }
inline static T distance(const Vector2<T>& v1, const Vector2<T>& v2) { return v1.distance(v2); }
inline T distanceSquared(const Vector2<T>& v2) const { return (*this - v2).lengthSquared(); }
inline static T distanceSquared(const Vector2<T>& v1, const Vector2<T>& v2) { return v1.distanceSquared(v2); }
inline Vector2<T> normalize() const { return *this / length(); }
inline Vector2<T> safeNormalize() const { auto l = length(); return (l != 0) ? (*this / l) : (*this); }
inline T dot(const Vector2<T>& v2) const { return X * v2.X + Y * v2.Y; }
inline static T dot(const Vector2<T>& v1, const Vector2<T>& v2) { return v1.dot(v2); }
inline Vector2<T> min(const Vector2<T>& v2) const { return Vector2<T>(std::min(X, v2.X), std::min(Y, v2.Y)); }
inline Vector2<T> max(const Vector2<T>& v2) const { return Vector2<T>(std::max(X, v2.X), std::max(Y, v2.Y)); }
inline Vector2<T> squareRoot() const { return sqrt(); }
inline Vector2<T> lerp(const Vector2<T>& v2, T t) const { return (*this) + (v2 - (*this)) * t; }
inline Vector2<T> inverseLerp(const Vector2<T>& a, const Vector2<T>& b) const { return ((*this) - a) / (b - a); }
inline Vector2<T> lerpPrecise(const Vector2<T>& v2, T t) const { return ((1 - t) * (*this)) + (v2 * t); }
inline Vector2<T> clampLower(const Vector2<T>& m) const { return max(m); }
inline Vector2<T> clampUpper(const Vector2<T>& m) const { return min(m); }
inline Vector2<T> clamp(const Vector2<T>& mn, const Vector2<T>& mx) const { return min(mx).max(mn); }
inline Vector2<T> average(const Vector2<T>& v2) const { return lerp(v2, (T)0.5); }
inline Vector2<T> barycentric(const Vector2<T>& v2, const Vector2<T>& v3, T u, T v) const { return (*this) + (v2 - (*this)) * u + (v3 - (*this)) * v; }
inline Vector2<T> add(const Vector2<T>& v2) { return (*this) + v2; }
inline Vector2<T> subtract(const Vector2<T>& v2) { return (*this) - v2; }
inline Vector2<T> multiply(const Vector2<T>& v2) { return (*this) * v2; }
inline Vector2<T> divide(const Vector2<T>& v2) { return (*this) / v2; }
inline Vector2<T> negate() { return -(*this); }
inline T pointCrossProduct(const Vector2<T>& other) { return X * other.Y - other.X * Y; }
inline T cross(const Vector2<T>& v2) { return X * v2.Y - Y * v2.X; }
inline Vector2<T> reflect(const Vector2<T>& normal) { return (*this) - (2 * (dot(normal) * normal)); }
inline Vector2<T> setX(T x) const { return Vector2<T>(x, Y); }
inline Vector2<T> setY(T y) const { return Vector2<T>(X, y); }
inline std::size_t hash() const { return hash::combine(X, Y); }
inline bool almostEquals(const Vector2<T>& x, float tolerance = constants::tolerance) const {
return std::fabs(X - x.X) <= tolerance && std::fabs(Y - x.Y) <= tolerance;
}
inline bool almostZero(float tolerance = constants::tolerance) const {
return std::fabs(X) < tolerance && std::fabs(Y) < tolerance;
}
inline int compare(const Vector2<T>& x) const { return std::signbit(magnitudeSquared() - x.magnitudeSquared()); }
inline friend Vector2<T> operator-(const Vector2<T>& l) { return Vector2<T>(-l.X, -l.Y); }
inline friend bool operator==(const Vector2<T>& l, const Vector2<T>& other) { return l.X == other.X && l.Y == other.Y; }
inline friend bool operator!=(const Vector2<T>& l, const Vector2<T>& other) { return !(l == other); }
inline friend std::ostream& operator<<(std::ostream& out, const Vector2<T>& v) { return (out << "Vector2<T>(X = " << v.X << ", Y = " << v.Y << ")"); }
inline friend Vector2<T> operator+(const Vector2<T>& lhs, const Vector2<T>& rhs) { return Vector2<T>(lhs.X + rhs.X, lhs.Y + rhs.Y); }
inline friend Vector2<T> operator+(const Vector2<T>& lhs, T rhs) { return Vector2<T>(lhs.X + rhs, lhs.Y + rhs); }
inline friend Vector2<T> operator+(T lhs, const Vector2<T>& rhs) { return Vector2<T>(lhs + rhs.X, lhs + rhs.Y); }
inline friend Vector2<T> operator-(const Vector2<T>& lhs, const Vector2<T>& rhs) { return Vector2<T>(lhs.X - rhs.X, lhs.Y - rhs.Y); }
inline friend Vector2<T> operator-(const Vector2<T>& lhs, T rhs) { return Vector2<T>(lhs.X - rhs, lhs.Y - rhs); }
inline friend Vector2<T> operator-(T lhs, const Vector2<T>& rhs) { return Vector2<T>(lhs - rhs.X, lhs - rhs.Y); }
inline friend Vector2<T> operator*(const Vector2<T>& lhs, const Vector2<T>& rhs) { return Vector2<T>(lhs.X * rhs.X, lhs.Y * rhs.Y); }
inline friend Vector2<T> operator*(const Vector2<T>& lhs, T rhs) { return Vector2<T>(lhs.X * rhs, lhs.Y * rhs); }
inline friend Vector2<T> operator*(T lhs, const Vector2<T>& rhs) { return Vector2<T>(lhs * rhs.X, lhs * rhs.Y); }
inline friend Vector2<T> operator/(const Vector2<T>& lhs, const Vector2<T>& rhs) { return Vector2<T>(lhs.X / rhs.X, lhs.Y / rhs.Y); }
inline friend Vector2<T> operator/(const Vector2<T>& lhs, T rhs) { return Vector2<T>(lhs.X / rhs, lhs.Y / rhs); }
inline friend Vector2<T> operator/(T lhs, const Vector2<T>& rhs) { return Vector2<T>(lhs / rhs.X, lhs / rhs.Y); }
inline friend bool operator<(const Vector2<T>& x0, const Vector2<T>& x1) { return x0.compare(x1) < 0; }
inline friend bool operator<=(const Vector2<T>& x0, const Vector2<T>& x1) { return x0.compare(x1) <= 0; }
inline friend bool operator>(const Vector2<T>& x0, const Vector2<T>& x1) { return x0.compare(x1) > 0; }
inline friend bool operator>=(const Vector2<T>& x0, const Vector2<T>& x1) { return x0.compare(x1) >= 0; }
};
#define FVector2 Vector2<float>
#define DVector2 Vector2<double>
template <typename T = float>
struct Vector3 final {
T X;
T Y;
T Z;
const Vector3(T x, T y, T z = 0) : X(x), Y(y), Z(z) {}
const Vector3(T value = 0) : X(value), Y(value), Z(value) {}
const Vector3(const Vector2<T>& v, T z = 0) : X(v.X), Y(v.Y), Z(z) {}
inline static const Vector3<T> minValue() { return Vector3<T>(std::numeric_limits<T>::min()); }
inline static const Vector3<T> maxValue() { return Vector3<T>(std::numeric_limits<T>::max()); }
inline static const Vector3<T> zero() { return Vector3<T>(0); }
inline static const Vector3<T> one() { return Vector3<T>(1); }
inline static const Vector3<T> unitX() { return Vector3<T>(1, 0, 0); }
inline static const Vector3<T> unitY() { return Vector3<T>(0, 1, 0); }
inline static const Vector3<T> unitZ() { return Vector3<T>(0, 0, 1); }
inline static Vector3<T> alongX(T self) { return unitX() * self; }
inline static Vector3<T> alongY(T self) { return unitY() * self; }
inline static Vector3<T> alongZ(T self) { return unitX() * self; }
inline T minComponent() const { return std::min(std::min(X, Y), Z); }
inline T maxComponent() const { return std::max(std::max(X, Y), Z); }
inline T sumComponents() const { return X + Y + Z; }
inline T sumSqrComponents() const { return X * X + Y * Y + Z * Z; }
inline T productComponents() const { return X * Y * Z; }
inline T getComponent(int n) const { return n == 0 ? X : n == 1 ? Y : Z; }
inline bool anyComponentNegative() const { return minComponent() < 0.0; }
inline T magnitudeSquared() const { return sumSqrComponents(); }
inline T magnitude() const { return std::sqrt(magnitudeSquared()); }
inline bool isnan() const { return std::isnan(X) || std::isnan(Y) || std::isnan(Z); }
inline bool isinf() const { return std::isinf(X) || std::isinf(Y) || std::isinf(Z); }
inline Vector3<T> abs() const { return Vector3<T>(std::abs(X), std::abs(Y), std::abs(Z)); }
inline Vector3<T> acos() const { return Vector3<T>(std::acos(X), std::acos(Y), std::acos(Z)); }
inline Vector3<T> asin() const { return Vector3<T>(std::asin(X), std::asin(Y), std::asin(Z)); }
inline Vector3<T> atan() const { return Vector3<T>(std::atan(X), std::atan(Y), std::atan(Z)); }
inline Vector3<T> cos() const { return Vector3<T>(std::cos(X), std::cos(Y), std::cos(Z)); }
inline Vector3<T> cosh() const { return Vector3<T>(std::cosh(X), std::cosh(Y), std::cosh(Z)); }
inline Vector3<T> exp() const { return Vector3<T>(std::exp(X), std::exp(Y), std::exp(Z)); }
inline Vector3<T> log() const { return Vector3<T>(std::log(X), std::log(Y), std::log(Z)); }
inline Vector3<T> log10() const { return Vector3<T>(std::log10(X), std::log10(Y), std::log10(Z)); }
inline Vector3<T> sin() const { return Vector3<T>(std::sin(X), std::sin(Y), std::sin(Z)); }
inline Vector3<T> sinh() const { return Vector3<T>(std::sinh(X), std::sinh(Y), std::sinh(Z)); }
inline Vector3<T> sqrt() const { return Vector3<T>(std::sqrt(X), std::sqrt(Y), std::sqrt(Z)); }
inline Vector3<T> tan() const { return Vector3<T>(std::tan(X), std::tan(Y), std::tan(Z)); }
inline Vector3<T> tanh() const { return Vector3<T>(std::tanh(X), std::tanh(Y), std::tanh(Z)); }
inline Vector3<T> inverse() const { return Vector3<T>((1 / X), (1 / Y), (1 / Z)); }
inline Vector3<T> ceiling() const { return Vector3<T>(std::ceil(X), std::ceil(Y), std::ceil(Z)); }
inline Vector3<T> floor() const { return Vector3<T>(std::floor(X), std::floor(Y), std::floor(Z)); }
inline Vector3<T> round() const { return Vector3<T>(std::round(X), std::round(Y), std::round(Z)); }
inline Vector3<T> truncate() const { return Vector3<T>(std::trunc(X), std::trunc(Y), std::trunc(Z)); }
inline Vector3<T> sqr() const { return Vector3<T>(X * X, Y * Y, Z * Z); }
inline Vector3<T> cube() const { return Vector3<T>(X * X * X, Y * Y * Y, Z * Z * Z); }
inline Vector3<T> toRadians() const { return Vector3<T>(X * constants::degreesToRadians, Y * constants::degreesToRadians, Z * constants::degreesToRadians); }
inline Vector3<T> toDegrees() const { return Vector3<T>(X * constants::radiansToDegrees, Y * constants::radiansToDegrees, Z * constants::radiansToDegrees); }
inline T lengthSquared() const { return sumSqrComponents(); }
inline T length() const { return std::sqrt(lengthSquared()); }
inline T distance(const Vector3<T>& v2) const { return (*this - v2).length(); }
inline T distanceSquared(const Vector3<T>& v2) const { return (*this - v2).lengthSquared(); }
inline Vector3<T> normalize() const { return *this / length(); }
inline Vector3<T> safeNormalize() const { auto l = length(); return (l != 0) ? (*this / l) : (*this); }
inline T dot(const Vector3<T>& v2) const { return X * v2.X + Y * v2.Y + Z * v2.Z; }
inline static T dot(const Vector3<T>& v1, const Vector3<T>& v2) { return v1.dot(v2); }
inline Vector3<T> min(const Vector3<T>& v2) const { return Vector3<T>(std::min(X, v2.X), std::min(Y, v2.Y), std::min(Z, v2.Z)); }
inline Vector3<T> max(const Vector3<T>& v2) const { return Vector3<T>(std::max(X, v2.X), std::max(Y, v2.Y), std::max(Z, v2.Z)); }
inline Vector3<T> squareRoot() const { return sqrt(); }
inline Vector3<T> lerp(const Vector3<T>& v2, T t) const { return (*this) + (v2 - (*this)) * t; }
inline Vector3<T> inverseLerp(const Vector3<T>& a, const Vector3<T>& b) const { return ((*this) - a) / (b - a); }
inline Vector3<T> lerpPrecise(const Vector3<T>& v2, T t) const { return ((1 - t) * (*this)) + (v2 * t); }
inline Vector3<T> clampLower(const Vector3<T>& m) const { return max(m); }
inline Vector3<T> clampUpper(const Vector3<T>& m) const { return min(m); }
inline Vector3<T> clamp(const Vector3<T>& mn, const Vector3<T>& mx) const { return min(mx).max(mn); }
inline Vector3<T> average(const Vector3<T>& v2) const { return lerp(v2, (T)0.5); }
inline Vector3<T> barycentric(const Vector3<T>& v2, const Vector3<T>& v3, T u, T v) const { return (*this) + (v2 - (*this)) * u + (v3 - (*this)) * v; }
inline Vector3<T> add(const Vector3<T>& v2) { return (*this) + v2; }
inline Vector3<T> subtract(const Vector3<T>& v2) { return (*this) - v2; }
inline Vector3<T> multiply(const Vector3<T>& v2) { return (*this) * v2; }
inline Vector3<T> divide(const Vector3<T>& v2) { return (*this) / v2; }
inline Vector3<T> negate() { return -(*this); }
inline Vector3<T> cross(const Vector3<T>& x) const { return Vector3<T>(Y * x.Z - Z * x.Y, Z * x.X - X * x.Z, X * x.Y - Y * x.X); }
inline T mixedProduct(const Vector3<T>& v1, const Vector3<T>& v2) { return cross(v1).dot(v2); }
inline Vector3<T> reflect(const Vector3<T>& normal) { return (*this) - (2 * (dot(normal) * normal)); }
inline bool isNonZeroAndValid(float tolerance = constants::tolerance) const { T self = lengthSquared(); return !std::isinf(self) && !std::isnan(self) && std::fabs(self) > tolerance; }
inline bool isZeroOrInvalid(float tolerance = constants::tolerance) const { return !isNonZeroAndValid(tolerance); }
inline bool isPerpendicular(const Vector3<T>& v1, const Vector3<T>& v2, float tolerance = constants::tolerance) const { auto z = zero(); return v1 != z && v2 != z && (std::fabs(v1.dot(v2)) < tolerance); }
inline Vector3<T> projection(const Vector3<T>& v2) const { return v2 * (dot(v2) / v2.lengthSquared()); }
inline Vector3<T> rejection(const Vector3<T>& v2) const { return *this - projection(v2); }
inline T angle(const Vector3<T>& v2, float tolerance = constants::tolerance) const
{
auto d = lengthSquared() * std::sqrt(v2.lengthSquared());
//std::clamp(dot(v2) / d, (T)-1, (T)1);
return d < tolerance ? 0 : std::acos(std::max(std::min(dot(v2) / d, (T)1), (T)-1));
}
inline T signedAngle(const Vector3<T>& to, const Vector3<T>& axis) const { return angle(to) * (std::int8_t)std::signbit(axis.dot(cross(to))); }
inline T signedAngle(const Vector3<T>& to) const { return signedAngle(to, unitZ()); }
inline bool colinear(const Vector3<T>& v2, float tolerance = constants::tolerance) const { return !isnan() && !v2.isnan() && signedAngle(v2) <= tolerance; }
inline bool isBackFace(const Vector3<T>& lineOfSight) const { return dot(lineOfSight) < 0; }
inline Vector3<T> along(T d) const { return normalize() * d; }
inline Vector3<T> catmullRom(const Vector3<T>& value2, const Vector3<T>& value3, const Vector3<T>& value4, T amount) const {
return Vector3<T>(
mathOps::catmullRom(X, value2.X, value3.X, value4.X, amount),
mathOps::catmullRom(Y, value2.Y, value3.Y, value4.Y, amount),
mathOps::catmullRom(Z, value2.Z, value3.Z, value4.Z, amount));
}
inline Vector3<T> hermite(const Vector3<T>& tangent1, const Vector3<T>& value2, const Vector3<T>& tangent2, T amount) const {
return Vector3<T>(
mathOps::hermite(X, tangent1.X, value2.X, tangent2.X, amount),
mathOps::hermite(Y, tangent1.Y, value2.Y, tangent2.Y, amount),
mathOps::hermite(Z, tangent1.Z, value2.Z, tangent2.Z, amount));
}
inline Vector3<T> smoothStep(const Vector3<T>& value2, T amount) const {
return Vector3<T>(
mathOps::smoothStep(X, value2.X, amount),
mathOps::smoothStep(Y, value2.Y, amount),
mathOps::smoothStep(Z, value2.Z, amount));
}
inline bool coplanar(const Vector3<T>& v2, const Vector3<T>& v3, const Vector3<T>& v4, float epsilon = constants::tolerance) const { return std::fabs((v3 - (*this)).dot((v2 - (*this)).cross(v4 - (*this)))) < epsilon; }
inline Vector3<T> xzy() const { return Vector3<T>(X, Z, Y); }
inline Vector3<T> zxy() const { return Vector3<T>(Z, X, Y); }
inline Vector3<T> zyx() const { return Vector3<T>(Z, Y, Z); }
inline Vector3<T> yxz() const { return Vector3<T>(Y, X, Z); }
inline Vector3<T> yzx() const { return Vector3<T>(Y, Z, X); }
inline Vector2<T> xy() const { return Vector2<T>(X, Y); }
inline Vector2<T> xz() const { return Vector2<T>(X, Z); }
inline Vector2<T> yz() const { return Vector2<T>(Y, Z); }
inline Vector3<T> setX(T x) const { return Vector3<T>(x, Y, Z); }
inline Vector3<T> setY(T y) const { return Vector3<T>(X, y, Z); }
inline Vector3<T> setZ(T z) const { return Vector3<T>(X, Y, z); }
inline std::size_t hash() const { return hash::combine(X, Y, Z); }
inline bool almostEquals(const Vector3<T>& x, float tolerance = constants::tolerance) const { return std::fabs(X - x.X) <= tolerance && std::fabs(Y - x.Y) <= tolerance && std::fabs(Z - x.Z) <= tolerance; }
inline bool almostZero(float tolerance = constants::tolerance) const { return std::fabs(X) < tolerance && std::fabs(Y) < tolerance && std::fabs(Z) < tolerance; }
inline int compare(const Vector3<T>& x) const { return std::signbit(magnitudeSquared() - x.magnitudeSquared()); }