-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.hpp
1238 lines (1127 loc) · 49.5 KB
/
vector.hpp
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 <cmath>
#include <algorithm>
#include <iostream>
namespace Common {
template <typename Value>
concept arithmetic = std::integral<Value> || std::floating_point<Value>;
template <arithmetic Value>
class Vector2;
template <arithmetic Value>
class Vector3;
/**
* @class Vector2
* @brief A class representing a 2-dimensional vector.
*
* This class provides various operations and utilities for 2D vectors, including
* basic arithmetic operations, normalization, dot product, and more.
*
* @tparam value_type The type of the vector components.
*/
template <arithmetic Value = float>
class Vector2 {
public:
using value_type = Value;
/**
* @brief A member variable of type value_type representing the x-coordinate.
*/
value_type x;
/**
* @brief A member variable of type value_type representing the y-coordinate.
*/
value_type y;
/**
* @brief A constant static member representing a zero vector.
*
* This member is a Vector2 object with all components set to zero.
* It can be used as a default or initial value for vector operations.
*/
const static Vector2 zero;
/**
* @brief A constant static member representing an one vector.
*
* This instance represents a vector with all components set to one.
* It can be used as a default or reference vector in various mathematical
* operations or transformations where a unit vector is required.
*/
const static Vector2 one;
/**
* @brief Default constructor. Initializes the vector to (0, 0, 0).
*/
constexpr Vector2() noexcept : Vector2(0, 0) {}
/**
* @brief Constructs a Vector2 object with the given x, y, and z coordinates.
*
* @param x The x-coordinate of the vector.
* @param y The y-coordinate of the vector.
*/
constexpr Vector2(value_type x, value_type y) noexcept : x{x}, y{y} {}
/**
* @brief Constructs a Vector2 object from a Vector3 object.
*
* This constructor initializes a Vector2 object using the x and y coordinates
* from the given Vector3 object.
*
* @param other The Vector3 object from which to initialize the Vector2 object.
*/
constexpr Vector2(Vector3<value_type> other) noexcept;
/**
* @brief Calculates the squared length of the vector.
*
* This function returns the squared length of the vector, which is the sum of the squares of its components.
* It is marked as constexpr, meaning it can be evaluated at compile time, and noexcept, indicating that it does not throw exceptions.
*
* @return The squared length of the vector.
*/
constexpr value_type Length() const noexcept { return x * x + y * y; }
/**
* @brief Computes the square root of the length of the vector.
*
* This function calculates the square root of the length (magnitude) of the vector.
* It is a shorthand for computing std::sqrt(Length()).
*
* @return The square root of the length of the vector.
*/
value_type LengthSqrt() const noexcept { return std::sqrt(Length()); }
/**
* @brief Calculates the Euclidean distance between two 2D vectors.
*
* This function computes the distance between two points in 2D space
* represented by the vectors `a` and `b`. The distance is calculated
* using the square root of the sum of the squared differences of their
* respective components.
*
* @param a The first vector (point) in 2D space.
* @param b The second vector (point) in 2D space.
* @return value_type The Euclidean distance between vectors `a` and `b`.
*/
static value_type Distance(const Vector2& a, const Vector2& b) noexcept { return (b - a).LengthSqrt(); }
/**
* @brief Calculates the Euclidean distance between two 2D vectors.
*
* This function computes the distance between two points in 2D space
* represented by the vectors `a` and `b`. The distance is calculated
* using the square root of the sum of the squared differences of their
* respective components.
*
* @param other The other vector to calculate the distance to.
* @return value_type The distance between the two vectors.
*/
value_type Distance(const Vector2& other) const noexcept { return Vector2::Distance(*this, other); }
/**
* @brief Normalizes the vector to have a length of 1.
*
* This function computes the normalized vector by dividing each component
* of the vector by its length. If the length of the vector is zero, it
* returns a zero vector to avoid division by zero.
*
* @return A normalized vector with a length of 1, or a zero vector if the
* original vector has a length of zero.
*/
Vector2 Normalize() const noexcept {
value_type length = LengthSqrt();
if (length == 0)
return Vector2::zero;
return { x / length, y / length };
}
/**
* @brief Normalizes the vector to have a length of 1.
*
* This function modifies the vector in place to make its length equal to 1.
* If the vector's length is zero, the vector components are set to zero.
*
* @return A reference to the normalized vector.
*/
Vector2& Normalize() noexcept {
value_type length = LengthSqrt();
if (length != 0) {
x = 0;
y = 0;
} else {
x /= length;
y /= length;
}
return *this;
}
/**
* @brief Computes the dot product of two 2D vectors.
*
* @param a The first vector.
* @param b The second vector.
* @return value_type The dot product of vectors a and b.
*/
constexpr static value_type Dot(const Vector2& a, const Vector2& b) noexcept {
return a.x * b.x + a.y * b.y;
}
/**
* @brief Computes the dot product of this vector and another vector.
*
* @param other The other vector to compute the dot product with.
* @return value_type The dot product of the two vectors.
*/
constexpr value_type Dot(const Vector2& other) const noexcept {
return Vector2::Dot(*this, other);
}
/**
* @brief Projects a vector onto another vector.
*
* This function projects the given vector onto the specified vector (projectOn).
* If the magnitude of the projectOn vector is too small (close to zero),
* the function returns a zero vector to avoid division by zero.
*
* @param vector The vector to be projected.
* @param projectOn The vector onto which the projection is performed.
* @return The projected vector.
*/
constexpr static Vector2 Project(const Vector2& vector, const Vector2& projectOn) noexcept {
auto projectProduct = projectOn.Dot(projectOn);
if (projectProduct < std::numeric_limits<value_type>::epsilon())
return Vector2::zero;
auto product = vector.Dot(projectOn);
return {
projectOn.x * product / projectProduct,
projectOn.y * product / projectProduct
};
}
/**
* @brief Projects this vector onto another vector.
*
* This function projects the current vector onto the given vector `projectOn`.
*
* @param projectOn The vector onto which the current vector will be projected.
* @return Vector2 The result of the projection.
*/
constexpr Vector2 Project(const Vector2& projectOn) noexcept {
return Vector2::Project(*this, projectOn);
}
/**
* @brief Reflects vector a around vector b.
*
* This function calculates the reflection of vector a around vector b using the formula:
* Reflect(a, b) = a - 2 * (a.Dot(b)) * b
*
* @param a The vector to be reflected.
* @param b The vector around which a is reflected.
* @return The reflected vector.
*/
static Vector2 Reflect(const Vector2& a, const Vector2& b) noexcept {
return a - b * 2 * a.Dot(b);
}
/**
* @brief Reflects this vector around the given vector.
*
* This function calculates the reflection of this vector around the specified vector.
*
* @param other The vector to reflect around.
* @return Vector2 The reflected vector.
*/
Vector2 Reflect(const Vector2& other) const noexcept {
return Vector2::Reflect(*this, other);
}
/**
* @brief Calculates the angle between two 2D vectors.
*
* This function computes the angle between two vectors `a` and `b` using the dot product and magnitudes of the vectors.
*
* @param a The first vector.
* @param b The second vector.
* @return The angle in radians between the two vectors. If either vector has zero length, the function returns 0.0f.
*/
constexpr static value_type Angle(const Vector2& a, const Vector2& b) noexcept {
auto magnitude = a.Length() * b.Length();
if (magnitude == 0)
return 0.0f;
auto product = a.Dot(b);
return std::acos(product / magnitude);
}
/**
* @brief Calculates the angle between this vector and another vector.
*
* @param other The other vector to calculate the angle with.
* @return value_type The angle between the two vectors.
*/
constexpr value_type Angle(const Vector2& other) noexcept {
return Vector2::Angle(*this, other);
}
/**
* @brief Linearly interpolates between two vectors a and b by a factor t.
*
* This function performs a linear interpolation between the vectors a and b,
* clamping the interpolation factor t between 0.0 and 1.0.
*
* @param a The starting vector.
* @param b The ending vector.
* @param t The interpolation factor, clamped between 0.0 and 1.0.
* @return The interpolated vector.
*/
constexpr static Vector2 Lerp (
const Vector2& a,
const Vector2& b,
std::floating_point t
) noexcept {
return LerpUnclamped(a, b, std::clamp(t, 0, 1));
}
/**
* @brief Linearly interpolates between this vector and another vector.
*
* This function performs a linear interpolation between the current vector
* and the provided vector `other` based on the interpolation factor `t`.
*
* @param other The target vector to interpolate towards.
* @param t The interpolation factor, typically in the range [0, 1].
* - When t = 0, the result is the current vector.
* - When t = 1, the result is the target vector `other`.
* - Values between 0 and 1 will interpolate linearly between the two vectors.
* @return A new Vector2 that is the result of the linear interpolation.
*/
constexpr Vector2 Lerp(const Vector2& other, std::floating_point t) noexcept {
return Vector2::Lerp(*this, other, t);
}
/**
* @brief Performs Spherical Linear Interpolation (Slerp) between two vectors.
*
* Slerp is a method of interpolating between two vectors along a great circle path on a sphere.
* This function interpolates between vectors `a` and `b` by a factor of `t`.
*
* @param a The starting vector.
* @param b The ending vector.
* @param t The interpolation factor, typically in the range [0, 1].
* @return The interpolated vector.
*/
static Vector2 Slerp(const Vector2& a, const Vector2& b, std::floating_point t) noexcept {
auto dotProduct = std::clamp(a.Dot(b), -1, 1);
auto theta = std::acos(dotProduct) * std::clamp(t, 0, 1);
Vector2 relativeVec = (b - a * dotProduct).Normalize();
return a * std::cos(theta) + relativeVec * std::sin(theta);
}
/**
* @brief Performs spherical linear interpolation (Slerp) between this vector and another vector.
*
* Slerp is a method of interpolating between two vectors along a great circle on a sphere.
* It is commonly used in computer graphics for smooth transitions between orientations.
*
* @param other The target vector to interpolate towards.
* @param t The interpolation factor, typically in the range [0, 1], where 0 returns this vector
* and 1 returns the other vector.
* @return Vector2 The interpolated vector.
*/
Vector2 Slerp(const Vector2& other, std::floating_point t) const noexcept {
return Vector2::Slerp(*this, other, t);
}
/**
* @brief Linearly interpolates between two vectors without clamping the interpolant.
*
* This function performs a linear interpolation between two vectors `a` and `b`
* based on the interpolant `t`. The result is not clamped, meaning `t` can be
* outside the range [0, 1].
*
* @param a The start vector.
* @param b The end vector.
* @param t The interpolant value.
* @return A vector that is the linear interpolation of `a` and `b` based on `t`.
*/
constexpr static Vector2 LerpUnclamped (
const Vector2& a,
const Vector2& b,
value_type t
) noexcept {
return {
a.x + t * (b.x - a.x),
a.y + t * (b.y - a.y)
};
}
/**
* @brief Linearly interpolates between this vector and another vector without clamping.
*
* This function performs a linear interpolation between the current vector and the
* specified vector 'other' based on the interpolation factor 't'. The interpolation
* is not clamped, meaning that 't' can be outside the range [0, 1].
*
* @param other The target vector to interpolate towards.
* @param t The interpolation factor. A value of 0 returns the current vector,
* a value of 1 returns the 'other' vector, and values outside the range
* [0, 1] will extrapolate accordingly.
* @return Vector2 The interpolated vector.
*/
constexpr Vector2 LerpUnclamped(const Vector2& other, std::floating_point t) noexcept {
return Vector2::LerpUnclamped(*this, other, t);
}
/**
* @brief Adds two Vector2 objects component-wise.
*
* This operator performs a component-wise addition of two Vector2 objects.
*
* @param other The other Vector2 object to add.
* @return A new Vector2 object that is the result of the addition.
*/
constexpr Vector2 operator+(const Vector2& other) const noexcept {
return { x + other.x, y + other.y };
}
/**
* @brief Subtracts another Vector2 from this Vector2.
*
* This operator performs component-wise subtraction of the given Vector2
* from this Vector2 and returns the resulting Vector2.
*
* @param other The Vector2 to subtract from this Vector2.
* @return A new Vector2 resulting from the component-wise subtraction.
*/
constexpr Vector2 operator-(const Vector2& other) const noexcept {
return { x - other.x, y - other.y };
}
/**
* @brief Multiplies two Vector2 objects component-wise.
*
* This operator performs a component-wise multiplication of the current
* Vector2 object with another Vector2 object passed as a parameter.
*
* @param other The Vector2 object to multiply with.
* @return A new Vector2 object resulting from the component-wise multiplication.
*/
constexpr Vector2 operator*(const Vector2& other) const noexcept {
return { x * other.x, y * other.y };
}
/**
* @brief Divides the current Vector2 by another Vector2 element-wise.
*
* This operator performs element-wise division of the current vector by the given vector.
* Each component (x, y, z) of the current vector is divided by the corresponding component
* of the other vector.
*
* @param other The Vector2 to divide by.
* @return A new Vector2 resulting from the element-wise division.
* @note This operation assumes that the components of the other vector are non-zero.
*/
constexpr Vector2 operator/(const Vector2& other) const noexcept {
return { x / other.x, y / other.y };
}
/**
* @brief Adds a scalar value to each component of the vector.
*
* This operator overload allows you to add a scalar value to the x, y, and z components
* of the vector, resulting in a new vector where each component is increased by the scalar.
*
* @param scalar The scalar value to be added to each component of the vector.
* @return A new Vector2 object with each component increased by the scalar value.
*/
constexpr Vector2 operator+(value_type scalar) const noexcept {
return { x + scalar, y + scalar };
}
/**
* @brief Subtracts a scalar value from each component of the vector.
*
* This operator returns a new Vector2 instance where each component
* (x, y, z) of the original vector is reduced by the given scalar value.
*
* @param scalar The scalar value to subtract from each component of the vector.
* @return A new Vector2 instance with each component reduced by the scalar value.
*/
constexpr Vector2 operator-(value_type scalar) const noexcept {
return { x - scalar, y - scalar };
}
/**
* @brief Multiplies the vector by a scalar value.
*
* This operator overload allows for the multiplication of a Vector2 object
* by a scalar value, returning a new Vector2 object with each component
* scaled by the given scalar.
*
* @param scalar The scalar value to multiply with the vector components.
* @return A new Vector2 object with each component scaled by the scalar value.
*/
constexpr Vector2 operator*(value_type scalar) const noexcept {
return { x * scalar, y * scalar };
}
/**
* @brief Divides the vector by a scalar value.
*
* This operator performs element-wise division of the vector components
* by the given scalar value.
*
* @param scalar The scalar value to divide the vector by.
* @return A new Vector2 object with each component divided by the scalar.
* @note This function is marked as noexcept, indicating that it does not throw exceptions.
*/
constexpr Vector2 operator/(value_type scalar) const noexcept {
return { x / scalar, y / scalar };
}
/**
* @brief Adds the components of another Vector2 to this vector.
*
* This operator performs component-wise addition of the given vector to this vector.
*
* @param other The vector to be added.
* @return A reference to this vector after addition.
*/
constexpr Vector2& operator+=(const Vector2& other) noexcept {
this->x += other.x;
this->y += other.y;
return *this;
}
/**
* @brief Subtracts the components of another Vector2 from this vector.
*
* This operator modifies the current vector by subtracting the corresponding
* components of the given vector from it.
*
* @param other The vector to subtract from this vector.
* @return A reference to the modified vector.
*/
constexpr Vector2& operator-=(const Vector2& other) noexcept {
this->x -= other.x;
this->y -= other.y;
return *this;
}
/**
* @brief Multiplies each component of this vector by the corresponding component of another vector.
*
* This operator performs element-wise multiplication of the vector components.
*
* @param other The vector to multiply with.
* @return A reference to the resulting vector after multiplication.
*/
constexpr Vector2& operator*=(const Vector2& other) noexcept {
this->x *= other.x;
this->y *= other.y;
return *this;
}
/**
* @brief Divides the components of this vector by the corresponding components of another vector.
*
* This operator performs element-wise division of the vector components.
*
* @param other The vector to divide by.
* @return A reference to this vector after the division.
*/
constexpr Vector2& operator/=(const Vector2& other) noexcept {
this->x /= other.x;
this->y /= other.y;
return *this;
}
/**
* @brief Compares this Vector2 object with another for equality.
*
* @param other The other Vector2 object to compare with.
* @return true if the x, y, and z components of both vectors are equal.
* @return false otherwise.
*/
constexpr bool operator==(const Vector2& other) const noexcept {
return x == other.x && y == other.y;
}
/**
* @brief Inequality operator for comparing two Vector2 objects.
*
* This operator checks if two Vector2 objects are not equal by
* negating the result of the equality operator.
*
* @param other The other Vector2 object to compare with.
* @return true if the two Vector2 objects are not equal, false otherwise.
*/
constexpr bool operator!=(const Vector2& other) const noexcept {
return !(*this == other);
}
/**
* @brief Compares this vector with another vector.
* @param other The vector to compare.
* @return -1 if this vector is less than the other vector, 1 if greater, 0 if equal.
*/
constexpr std::partial_ordering operator<=>(const Vector2& other) const noexcept {
if (x == other.x && y == other.y)
return std::partial_ordering::equivalent;
const auto l1 = Length();
const auto l2 = other.Length();
if (l1 < l2) return std::partial_ordering::less;
else if (l1 > l2) return std::partial_ordering::greater;
else return std::partial_ordering::unordered;
}
};
template <arithmetic T>
constexpr Vector2<T> Vector2<T>::zero{0, 0};
template <arithmetic T>
constexpr Vector2<T> Vector2<T>::one {1, 1};
/**
* @class Vector3
* @brief A class representing a 3-dimensional vector.
*
* This class provides various operations and utilities for 3D vectors, including
* basic arithmetic operations, normalization, dot product, cross product, and more.
*
* @tparam value_type The type of the vector components.
*/
template <arithmetic Value = float>
class Vector3 {
public:
using value_type = Value;
protected:
/**
* @brief A member variable of type value_type representing the x-coordinate.
*/
value_type x;
/**
* @brief A member variable of type value_type representing the y-coordinate.
*/
value_type y;
/**
* @brief A member variable of type value_type representing the z-coordinate.
*/
value_type z;
public:
/**
* @brief A constant static member representing a zero vector.
*
* This member is a Vector3 object with all components set to zero.
* It can be used as a default or initial value for vector operations.
*/
const static Vector3 zero;
/**
* @brief A constant static member representing an one vector.
*
* This instance represents a vector with all components set to one.
* It can be used as a default or reference vector in various mathematical
* operations or transformations where a unit vector is required.
*/
const static Vector3 one;
/**
* @brief Default constructor. Initializes the vector to (0, 0, 0).
*/
constexpr Vector3() noexcept : Vector3(0, 0, 0) {}
/**
* @brief Constructs a Vector3 object with the given x, y, and z coordinates.
*
* @param x The x-coordinate of the vector.
* @param y The y-coordinate of the vector.
* @param z The z-coordinate of the vector.
*/
constexpr Vector3(value_type x, value_type y, value_type z) noexcept : x{x}, y{y}, z{z} {}
/**
* @brief Constructs a Vector3 object from a Vector2 object.
*
* This constructor initializes a Vector3 object using the x and y coordinates
* from the given Vector2 object and sets the z coordinate to 0.
*
* @param other The Vector2 object from which to initialize the Vector3 object.
*/
constexpr Vector3(Vector2<value_type> other) noexcept;
/**
* @brief Calculates the squared length of the vector.
*
* This function returns the squared length of the vector, which is the sum of the squares of its components.
* It is marked as constexpr, meaning it can be evaluated at compile time, and noexcept, indicating that it does not throw exceptions.
*
* @return The squared length of the vector.
*/
constexpr value_type Length() const noexcept { return x * x + y * y + z * z; }
/**
* @brief Computes the square root of the length of the vector.
*
* This function calculates the square root of the length (magnitude) of the vector.
* It is a shorthand for computing std::sqrt(Length()).
*
* @return The square root of the length of the vector.
*/
value_type LengthSqrt() const noexcept { return std::sqrt(Length()); }
/**
* @brief Calculates the Euclidean distance between two 3D vectors.
*
* This function computes the distance between two points in 3D space
* represented by the vectors `a` and `b`. The distance is calculated
* using the square root of the sum of the squared differences of their
* respective components.
*
* @param a The first vector (point) in 3D space.
* @param b The second vector (point) in 3D space.
* @return value_type The Euclidean distance between vectors `a` and `b`.
*/
static value_type Distance(const Vector3& a, const Vector3& b) noexcept { return (b - a).LengthSqrt(); }
/**
* @brief Calculates the distance between this vector and another vector.
*
* @param other The other vector to calculate the distance to.
* @return value_type The distance between the two vectors.
*/
value_type Distance(const Vector3& other) const noexcept { return Vector3::Distance(*this, other); }
/**
* @brief Normalizes the vector to have a length of 1.
*
* This function computes the normalized vector by dividing each component
* of the vector by its length. If the length of the vector is zero, it
* returns a zero vector to avoid division by zero.
*
* @return A normalized vector with a length of 1, or a zero vector if the
* original vector has a length of zero.
*/
Vector3 Normalize() const noexcept {
value_type length = LengthSqrt();
if (length == 0)
return Vector3::zero;
return { x / length, y / length, z / length };
}
/**
* @brief Normalizes the vector to have a length of 1.
*
* This function modifies the vector in place to make its length equal to 1.
* If the vector's length is zero, the vector components are set to zero.
*
* @return A reference to the normalized vector.
*/
Vector3& Normalize() noexcept {
value_type length = LengthSqrt();
if (length != 0) {
x = 0;
y = 0;
z = 0;
} else {
x /= length;
y /= length;
z /= length;
}
return *this;
}
/**
* @brief Computes the dot product of two 3D vectors.
*
* @param a The first vector.
* @param b The second vector.
* @return value_type The dot product of vectors a and b.
*/
constexpr static value_type Dot(const Vector3& a, const Vector3& b) noexcept {
return a.x * b.x + a.y * b.y + a.z * b.z;
}
/**
* @brief Computes the dot product of this vector and another vector.
*
* @param other The other vector to compute the dot product with.
* @return value_type The dot product of the two vectors.
*/
constexpr value_type Dot(const Vector3& other) const noexcept {
return Vector3::Dot(*this, other);
}
/**
* @brief Projects a vector onto another vector.
*
* This function projects the given vector onto the specified vector (projectOn).
* If the magnitude of the projectOn vector is too small (close to zero),
* the function returns a zero vector to avoid division by zero.
*
* @param vector The vector to be projected.
* @param projectOn The vector onto which the projection is performed.
* @return The projected vector.
*/
constexpr static Vector3 Project(const Vector3& vector, const Vector3& projectOn) noexcept {
auto projectProduct = projectOn.Dot(projectOn);
if (projectProduct < std::numeric_limits<value_type>::epsilon())
return Vector3::zero;
auto product = vector.Dot(projectOn);
return {
projectOn.x * product / projectProduct,
projectOn.y * product / projectProduct,
projectOn.z * product / projectProduct
};
}
/**
* @brief Projects this vector onto another vector.
*
* This function projects the current vector onto the given vector `projectOn`.
*
* @param projectOn The vector onto which the current vector will be projected.
* @return Vector3 The result of the projection.
*/
constexpr Vector3 Project(const Vector3& projectOn) noexcept {
return Vector3::Project(*this, projectOn);
}
/**
* @brief Reflects vector a around vector b.
*
* This function calculates the reflection of vector a around vector b using the formula:
* Reflect(a, b) = a - 2 * (a.Dot(b)) * b
*
* @param a The vector to be reflected.
* @param b The vector around which a is reflected.
* @return The reflected vector.
*/
static Vector3 Reflect(const Vector3& a, const Vector3& b) noexcept {
return a - b * 2 * a.Dot(b);
}
/**
* @brief Reflects this vector around the given vector.
*
* This function calculates the reflection of this vector around the specified vector.
*
* @param other The vector to reflect around.
* @return Vector3 The reflected vector.
*/
Vector3 Reflect(const Vector3& other) const noexcept {
return Vector3::Reflect(*this, other);
}
/**
* @brief Calculates the angle between two 3D vectors.
*
* This function computes the angle between two vectors `a` and `b` using the dot product and magnitudes of the vectors.
*
* @param a The first vector.
* @param b The second vector.
* @return The angle in radians between the two vectors. If either vector has zero length, the function returns 0.0f.
*/
constexpr static value_type Angle(const Vector3& a, const Vector3& b) noexcept {
auto magnitude = a.Length() * b.Length();
if (magnitude == 0)
return 0;
auto product = a.Dot(b);
return std::acos(product / magnitude);
}
/**
* @brief Calculates the angle between this vector and another vector.
*
* @param other The other vector to calculate the angle with.
* @return value_type The angle between the two vectors.
*/
constexpr value_type Angle(const Vector3& other) noexcept {
return Vector3::Angle(*this, other);
}
/**
* @brief Computes the cross product of two 3D vectors.
*
* This function calculates the cross product of two 3D vectors `a` and `b`.
* The cross product is a vector that is perpendicular to both `a` and `b`,
* and it has a magnitude equal to the area of the parallelogram that the vectors span.
*
* @param a The first 3D vector.
* @param b The second 3D vector.
* @return Vector3 The resulting 3D vector from the cross product of `a` and `b`.
*/
constexpr static Vector3 Cross(const Vector3& a, const Vector3& b) noexcept {
return {
a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x
};
}
/**
* @brief Computes the cross product of this vector with another vector.
*
* @param other The vector to compute the cross product with.
* @return A new Vector3 representing the cross product of this vector and the other vector.
*/
constexpr Vector3 Cross(const Vector3& other) noexcept {
return Vector3::Cross(*this, other);
}
/**
* @brief Linearly interpolates between two vectors a and b by a factor t.
*
* This function performs a linear interpolation between the vectors a and b,
* clamping the interpolation factor t between 0.0 and 1.0.
*
* @param a The starting vector.
* @param b The ending vector.
* @param t The interpolation factor, clamped between 0.0 and 1.0.
* @return The interpolated vector.
*/
constexpr static Vector3 Lerp (
const Vector3& a,
const Vector3& b,
std::floating_point t
) noexcept {
return LerpUnclamped(a, b, std::clamp(t, 0, 1));
}
/**
* @brief Linearly interpolates between this vector and another vector.
*
* This function performs a linear interpolation between the current vector
* and the provided vector `other` based on the interpolation factor `t`.
*
* @param other The target vector to interpolate towards.
* @param t The interpolation factor, typically in the range [0, 1].
* - When t = 0, the result is the current vector.
* - When t = 1, the result is the target vector `other`.
* - Values between 0 and 1 will interpolate linearly between the two vectors.
* @return A new Vector3 that is the result of the linear interpolation.
*/
constexpr Vector3 Lerp(const Vector3& other, std::floating_point t) noexcept {
return Vector3::Lerp(*this, other, t);
}
/**
* @brief Performs Spherical Linear Interpolation (Slerp) between two vectors.
*
* Slerp is a method of interpolating between two vectors along a great circle path on a sphere.
* This function interpolates between vectors `a` and `b` by a factor of `t`.
*
* @param a The starting vector.
* @param b The ending vector.
* @param t The interpolation factor, typically in the range [0, 1].
* @return The interpolated vector.
*/
static Vector3 Slerp(const Vector3& a, const Vector3& b, std::floating_point t) noexcept {
auto dotProduct = std::clamp(a.Dot(b), -1.0f, 1.0f);
auto theta = std::acos(dotProduct) * std::clamp(t, 0.0f, 1.0f);
Vector3 relativeVec = (b - a * dotProduct).Normalize();
return a * std::cos(theta) + relativeVec * std::sin(theta);
}
/**
* @brief Performs spherical linear interpolation (Slerp) between this vector and another vector.
*
* Slerp is a method of interpolating between two vectors along a great circle on a sphere.
* It is commonly used in computer graphics for smooth transitions between orientations.
*
* @param other The target vector to interpolate towards.
* @param t The interpolation factor, typically in the range [0, 1], where 0 returns this vector
* and 1 returns the other vector.
* @return Vector3 The interpolated vector.
*/
Vector3 Slerp(const Vector3& other, std::floating_point t) const noexcept {
return Vector3::Slerp(*this, other, t);
}
/**
* @brief Linearly interpolates between two vectors without clamping the interpolant.
*
* This function performs a linear interpolation between two vectors `a` and `b`
* based on the interpolant `t`. The result is not clamped, meaning `t` can be
* outside the range [0, 1].
*
* @param a The start vector.
* @param b The end vector.
* @param t The interpolant value.
* @return A vector that is the linear interpolation of `a` and `b` based on `t`.
*/
constexpr static Vector3 LerpUnclamped (
const Vector3& a,
const Vector3& b,
value_type t
) noexcept {
return {
a.x + t * (b.x - a.x),
a.y + t * (b.y - a.y),
a.z + t * (b.z - a.z)
};
}
/**
* @brief Linearly interpolates between this vector and another vector without clamping.
*
* This function performs a linear interpolation between the current vector and the
* specified vector 'other' based on the interpolation factor 't'. The interpolation
* is not clamped, meaning that 't' can be outside the range [0, 1].
*
* @param other The target vector to interpolate towards.
* @param t The interpolation factor. A value of 0 returns the current vector,
* a value of 1 returns the 'other' vector, and values outside the range
* [0, 1] will extrapolate accordingly.
* @return Vector3 The interpolated vector.
*/
constexpr Vector3 LerpUnclamped(const Vector3& other, value_type t) noexcept {
return Vector3::LerpUnclamped(*this, other, t);
}
/**