forked from FlaxEngine/FlaxEngine
/
Vector3.h
1055 lines (906 loc) · 34 KB
/
Vector3.h
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
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#pragma once
#include "Math.h"
#include "Mathd.h"
#include "Engine/Core/Formatting.h"
#include "Engine/Core/Templates.h"
/// <summary>
/// Represents a three dimensional mathematical vector.
/// </summary>
template<typename T>
API_STRUCT(Template) struct Vector3Base
{
typedef T Real;
static FLAXENGINE_API struct ScriptingTypeInitializer TypeInitializer;
union
{
struct
{
/// <summary>
/// The X component.
/// </summary>
API_FIELD() T X;
/// <summary>
/// The Y component.
/// </summary>
API_FIELD() T Y;
/// <summary>
/// The Z component.
/// </summary>
API_FIELD() T Z;
};
/// <summary>
/// The raw vector values (in XYZ order).
/// </summary>
T Raw[3];
};
public:
// Vector with all components equal zero (0, 0, 0).
static FLAXENGINE_API const Vector3Base<T> Zero;
// Vector with all components equal one (1, 1, 1).
static FLAXENGINE_API const Vector3Base<T> One;
// Vector with all components equal half (0.5, 0.5, 0.5).
static FLAXENGINE_API const Vector3Base<T> Half;
// The X unit vector (1, 0, 0).
static FLAXENGINE_API const Vector3Base<T> UnitX;
// The Y unit vector (0, 1, 0).
static FLAXENGINE_API const Vector3Base<T> UnitY;
// The Z unit vector (0, 0, 1).
static FLAXENGINE_API const Vector3Base<T> UnitZ;
// A unit vector designating up (0, 1, 0).
static FLAXENGINE_API const Vector3Base<T> Up;
// A unit vector designating down (0, -1, 0).
static FLAXENGINE_API const Vector3Base<T> Down;
// A unit vector designating a (-1, 0, 0).
static FLAXENGINE_API const Vector3Base<T> Left;
// A unit vector designating b (1, 0, 0).
static FLAXENGINE_API const Vector3Base<T> Right;
// A unit vector designating forward in a a-handed coordinate system (0, 0, 1).
static FLAXENGINE_API const Vector3Base<T> Forward;
// A unit vector designating backward in a a-handed coordinate system (0, 0, -1).
static FLAXENGINE_API const Vector3Base<T> Backward;
// Vector with all components equal maximum value.
static FLAXENGINE_API const Vector3Base<T> Minimum;
// Vector with all components equal minimum value.
static FLAXENGINE_API const Vector3Base<T> Maximum;
public:
/// <summary>
/// Empty constructor.
/// </summary>
Vector3Base()
{
}
FORCE_INLINE Vector3Base(T xyz)
: X(xyz)
, Y(xyz)
, Z(xyz)
{
}
FORCE_INLINE explicit Vector3Base(const T* xyz)
: X(xyz[0])
, Y(xyz[1])
, Z(xyz[2])
{
}
FORCE_INLINE Vector3Base(T x, T y, T z)
: X(x)
, Y(y)
, Z(z)
{
}
template<typename U = T, typename TEnableIf<TNot<TIsTheSame<T, U>>::Value>::Type...>
FORCE_INLINE Vector3Base(const Vector3Base<U>& xyz)
: X((T)xyz.X)
, Y((T)xyz.Y)
, Z((T)xyz.Z)
{
}
FLAXENGINE_API Vector3Base(const Float2& xy, T z = 0);
FLAXENGINE_API Vector3Base(const Double2& xy, T z = 0);
FLAXENGINE_API Vector3Base(const Int2& xy, T z = 0);
FLAXENGINE_API Vector3Base(const Int3& xyz);
FLAXENGINE_API explicit Vector3Base(const Float4& xyz);
FLAXENGINE_API explicit Vector3Base(const Double4& xyz);
FLAXENGINE_API explicit Vector3Base(const Int4& xyz);
FLAXENGINE_API explicit Vector3Base(const Color& color);
public:
FLAXENGINE_API String ToString() const;
public:
// Gets a value indicting whether this instance is normalized.
bool IsNormalized() const
{
return Math::IsOne(X * X + Y * Y + Z * Z);
}
// Gets a value indicting whether this vector is zero.
bool IsZero() const
{
return Math::IsZero(X) && Math::IsZero(Y) && Math::IsZero(Z);
}
// Gets a value indicting whether any vector component is zero.
bool IsAnyZero() const
{
return Math::IsZero(X) || Math::IsZero(Y) || Math::IsZero(Z);
}
// Gets a value indicting whether this vector is one.
bool IsOne() const
{
return Math::IsOne(X) && Math::IsOne(Y) && Math::IsOne(Z);
}
// Calculates the length of the vector.
T Length() const
{
return Math::Sqrt(X * X + Y * Y + Z * Z);
}
// Calculates the squared length of the vector.
T LengthSquared() const
{
return X * X + Y * Y + Z * Z;
}
// Calculates inverted length of the vector (1 / length).
T InvLength() const
{
return 1.0f / Length();
}
/// <summary>
/// Returns the average arithmetic of all the components.
/// </summary>
T AverageArithmetic() const
{
return (X + Y + Z) * 0.333333334f;
}
/// <summary>
/// Gets the sum of all vector components values.
/// </summary>
T SumValues() const
{
return X + Y + Z;
}
/// <summary>
/// Returns the minimum value of all the components.
/// </summary>
T MinValue() const
{
return Math::Min(X, Y, Z);
}
/// <summary>
/// Returns the maximum value of all the components.
/// </summary>
T MaxValue() const
{
return Math::Max(X, Y, Z);
}
/// <summary>
/// Returns true if vector has one or more components is not a number (NaN).
/// </summary>
bool IsNaN() const
{
return isnan(X) || isnan(Y) || isnan(Z);
}
/// <summary>
/// Returns true if vector has one or more components equal to +/- infinity.
/// </summary>
bool IsInfinity() const
{
return isinf(X) || isinf(Y) || isinf(Z);
}
/// <summary>
/// Returns true if vector has one or more components equal to +/- infinity or NaN.
/// </summary>
bool IsNanOrInfinity() const
{
return IsInfinity() || IsNaN();
}
/// <summary>
/// Calculates a vector with values being absolute values of that vector.
/// </summary>
Vector3Base GetAbsolute() const
{
return Vector3Base(Math::Abs(X), Math::Abs(Y), Math::Abs(Z));
}
/// <summary>
/// Calculates a vector with values being opposite to values of that vector.
/// </summary>
Vector3Base GetNegative() const
{
return Vector3Base(-X, -Y, -Z);
}
/// <summary>
/// Calculates a normalized vector that has length equal to 1.
/// </summary>
Vector3Base GetNormalized() const
{
Vector3Base result(X, Y, Z);
result.Normalize();
return result;
}
public:
/// <summary>
/// Performs vector normalization (scales vector up to unit length).
/// </summary>
void Normalize()
{
const T length = Math::Sqrt(X * X + Y * Y + Z * Z);
if (Math::Abs(length) >= ZeroTolerance)
{
const T inv = 1.0f / length;
X *= inv;
Y *= inv;
Z *= inv;
}
}
/// <summary>
/// Performs fast vector normalization (scales vector up to unit length).
/// </summary>
void NormalizeFast()
{
const T inv = 1.0f / Math::Sqrt(X * X + Y * Y + Z * Z);
X *= inv;
Y *= inv;
Z *= inv;
}
public:
Vector3Base operator+(const Vector3Base& b) const
{
return Vector3Base(X + b.X, Y + b.Y, Z + b.Z);
}
Vector3Base operator-(const Vector3Base& b) const
{
return Vector3Base(X - b.X, Y - b.Y, Z - b.Z);
}
Vector3Base operator*(const Vector3Base& b) const
{
return Vector3Base(X * b.X, Y * b.Y, Z * b.Z);
}
Vector3Base operator/(const Vector3Base& b) const
{
return Vector3Base(X / b.X, Y / b.Y, Z / b.Z);
}
Vector3Base operator-() const
{
return Vector3Base(-X, -Y, -Z);
}
Vector3Base operator+(T b) const
{
return Vector3Base(X + b, Y + b, Z + b);
}
Vector3Base operator-(T b) const
{
return Vector3Base(X - b, Y - b, Z - b);
}
Vector3Base operator*(T b) const
{
return Vector3Base(X * b, Y * b, Z * b);
}
Vector3Base operator/(T b) const
{
return Vector3Base(X / b, Y / b, Z / b);
}
Vector3Base operator+(typename TOtherFloat<T>::Type a) const
{
T b = (T)a;
return Vector3Base(X + b, Y + b, Z + b);
}
Vector3Base operator-(typename TOtherFloat<T>::Type a) const
{
T b = (T)a;
return Vector3Base(X - b, Y - b, Z - b);
}
Vector3Base operator*(typename TOtherFloat<T>::Type a) const
{
T b = (T)a;
return Vector3Base(X * (T)b, Y * b, Z * b);
}
Vector3Base operator/(typename TOtherFloat<T>::Type a) const
{
T b = (T)a;
return Vector3Base(X / b, Y / b, Z / b);
}
Vector3Base operator^(const Vector3Base& b) const
{
return Cross(*this, b);
}
T operator|(const Vector3Base& b) const
{
return Dot(*this, b);
}
Vector3Base& operator+=(const Vector3Base& b)
{
X += b.X;
Y += b.Y;
Z += b.Z;
return *this;
}
Vector3Base& operator-=(const Vector3Base& b)
{
X -= b.X;
Y -= b.Y;
Z -= b.Z;
return *this;
}
Vector3Base& operator*=(const Vector3Base& b)
{
X *= b.X;
Y *= b.Y;
Z *= b.Z;
return *this;
}
Vector3Base& operator/=(const Vector3Base& b)
{
X /= b.X;
Y /= b.Y;
Z /= b.Z;
return *this;
}
Vector3Base& operator+=(T b)
{
X += b;
Y += b;
Z += b;
return *this;
}
Vector3Base& operator-=(T b)
{
X -= b;
Y -= b;
Z -= b;
return *this;
}
Vector3Base& operator*=(T b)
{
X *= b;
Y *= b;
Z *= b;
return *this;
}
Vector3Base& operator/=(T b)
{
X /= b;
Y /= b;
Z /= b;
return *this;
}
bool operator==(const Vector3Base& b) const
{
return X == b.X && Y == b.Y && Z == b.Z;
}
bool operator!=(const Vector3Base& b) const
{
return X != b.X || Y != b.Y || Z != b.Z;
}
bool operator>(const Vector3Base& b) const
{
return X > b.X && Y > b.Y && Z > b.Z;
}
bool operator>=(const Vector3Base& b) const
{
return X >= b.X && Y >= b.Y && Z >= b.Z;
}
bool operator<(const Vector3Base& b) const
{
return X < b.X && Y < b.Y && Z < b.Z;
}
bool operator<=(const Vector3Base& b) const
{
return X <= b.X && Y <= b.Y && Z <= b.Z;
}
public:
static bool NearEqual(const Vector3Base& a, const Vector3Base& b)
{
return Math::NearEqual(a.X, b.X) && Math::NearEqual(a.Y, b.Y) && Math::NearEqual(a.Z, b.Z);
}
static bool NearEqual(const Vector3Base& a, const Vector3Base& b, T epsilon)
{
return Math::NearEqual(a.X, b.X, epsilon) && Math::NearEqual(a.Y, b.Y, epsilon) && Math::NearEqual(a.Z, b.Z, epsilon);
}
public:
static void Add(const Vector3Base& a, const Vector3Base& b, Vector3Base& result)
{
result = Vector3Base(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
}
static void Subtract(const Vector3Base& a, const Vector3Base& b, Vector3Base& result)
{
result = Vector3Base(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
}
static void Multiply(const Vector3Base& a, const Vector3Base& b, Vector3Base& result)
{
result = Vector3Base(a.X * b.X, a.Y * b.Y, a.Z * b.Z);
}
static void Divide(const Vector3Base& a, const Vector3Base& b, Vector3Base& result)
{
result = Vector3Base(a.X / b.X, a.Y / b.Y, a.Z / b.Z);
}
static void Min(const Vector3Base& a, const Vector3Base& b, Vector3Base& result)
{
result = Vector3Base(a.X < b.X ? a.X : b.X, a.Y < b.Y ? a.Y : b.Y, a.Z < b.Z ? a.Z : b.Z);
}
static void Max(const Vector3Base& a, const Vector3Base& b, Vector3Base& result)
{
result = Vector3Base(a.X > b.X ? a.X : b.X, a.Y > b.Y ? a.Y : b.Y, a.Z > b.Z ? a.Z : b.Z);
}
public:
static Vector3Base Min(const Vector3Base& a, const Vector3Base& b)
{
return Vector3Base(a.X < b.X ? a.X : b.X, a.Y < b.Y ? a.Y : b.Y, a.Z < b.Z ? a.Z : b.Z);
}
static Vector3Base Max(const Vector3Base& a, const Vector3Base& b)
{
return Vector3Base(a.X > b.X ? a.X : b.X, a.Y > b.Y ? a.Y : b.Y, a.Z > b.Z ? a.Z : b.Z);
}
static Vector3Base Mod(const Vector3Base& a, const Vector3Base& b)
{
return Vector3Base(Math::Mod(a.X, b.X), Math::Mod(a.Y, b.Y), Math::Mod(a.Z, b.Z));
}
static Vector3Base Floor(const Vector3Base& v)
{
return Vector3Base(Math::Floor(v.X), Math::Floor(v.Y), Math::Floor(v.Z));
}
static Vector3Base Frac(const Vector3Base& v)
{
return Vector3Base(v.X - (int32)v.X, v.Y - (int32)v.Y, v.Z - (int32)v.Z);
}
static Vector3Base Round(const Vector3Base& v)
{
return Vector3Base(Math::Round(v.X), Math::Round(v.Y), Math::Round(v.Z));
}
static Vector3Base Ceil(const Vector3Base& v)
{
return Vector3Base(Math::Ceil(v.X), Math::Ceil(v.Y), Math::Ceil(v.Z));
}
static Vector3Base Abs(const Vector3Base& v)
{
return Vector3Base(Math::Abs(v.X), Math::Abs(v.Y), Math::Abs(v.Z));
}
public:
// Restricts a value to be within a specified range
// @param v The value to clamp
// @param min The minimum value,
// @param max The maximum value
// @returns Clamped value
static Vector3Base Clamp(const Vector3Base& v, const Vector3Base& min, const Vector3Base& max)
{
Vector3Base result;
Clamp(v, min, max, result);
return result;
}
// Restricts a value to be within a specified range
// @param v The value to clamp
// @param min The minimum value,
// @param max The maximum value
// @param result When the method completes, contains the clamped value
static void Clamp(const Vector3Base& v, const Vector3Base& min, const Vector3Base& max, Vector3Base& result)
{
result = Vector3Base(Math::Clamp(v.X, min.X, max.X), Math::Clamp(v.Y, min.Y, max.Y), Math::Clamp(v.Z, min.Z, max.Z));
}
/// <summary>
/// Makes sure that Length of the output vector is always below max and above 0.
/// </summary>
/// <param name="vector">Input Vector.</param>
/// <param name="max">Max Length</param>
static Vector3Base ClampLength(const Vector3Base& v, float max)
{
return ClampLength(v, 0, max);
}
/// <summary>
/// Makes sure that Length of the output vector is always below max and above min.
/// </summary>
/// <param name="vector">Input Vector.</param>
/// <param name="min">Min Length</param>
/// <param name="max">Max Length</param>
static Vector3Base ClampLength(const Vector3Base& v, float min, float max)
{
Vector3Base result;
ClampLength(v, min, max, result);
return result;
}
/// <summary>
/// Makes sure that Length of the output vector is always below max and above min.
/// </summary>
/// <param name="vector">Input Vector.</param>
/// <param name="min">Min Length</param>
/// <param name="max">Max Length</param>
/// <param name="result">The result vector.</param>
static void ClampLength(const Vector3Base& v, float min, float max, Vector3Base& result)
{
result.X = v.X;
result.Y = v.Y;
result.Z = v.Z;
auto lenSq = result.LengthSquared();
if (lenSq > max * max)
{
auto scaleFactor = max / (float)Math::Sqrt(lenSq);
result.X *= scaleFactor;
result.Y *= scaleFactor;
result.Z *= scaleFactor;
}
if (lenSq < min * min)
{
auto scaleFactor = min / (float)Math::Sqrt(lenSq);
result.X *= scaleFactor;
result.Y *= scaleFactor;
result.Z *= scaleFactor;
}
}
// Calculates the distance between two vectors
// @param a The first vector
// @param b The second vector
// @returns The distance between the two vectors
static T Distance(const Vector3Base& a, const Vector3Base& b)
{
const T x = a.X - b.X;
const T y = a.Y - b.Y;
const T z = a.Z - b.Z;
return Math::Sqrt(x * x + y * y + z * z);
}
// Calculates the squared distance between two vectors
// @param a The first vector
// @param b The second vector
// @returns The squared distance between the two vectors
static T DistanceSquared(const Vector3Base& a, const Vector3Base& b)
{
const T x = a.X - b.X;
const T y = a.Y - b.Y;
const T z = a.Z - b.Z;
return x * x + y * y + z * z;
}
// Performs vector normalization (scales vector up to unit length).
static Vector3Base Normalize(const Vector3Base& v)
{
Vector3Base r = v;
const T length = Math::Sqrt(r.X * r.X + r.Y * r.Y + r.Z * r.Z);
if (Math::Abs(length) >= ZeroTolerance)
{
const T inv = 1.0f / length;
r.X *= inv;
r.Y *= inv;
r.Z *= inv;
}
return r;
}
// Performs vector normalization (scales vector up to unit length). This is a faster version that does not performs check for length equal 0 (it assumes that input vector is not empty).
// @param inout Input vector to normalize (cannot be zero).
// @returns Output vector that is normalized (has unit length)
static Vector3Base NormalizeFast(const Vector3Base& v)
{
const T inv = 1.0f / v.Length();
return Vector3Base(v.X * inv, v.Y * inv, v.Z * inv);
}
// Performs vector normalization (scales vector up to unit length)
// @param inout Input vector to normalize
// @param output Output vector that is normalized (has unit length)
static FORCE_INLINE void Normalize(const Vector3Base& input, Vector3Base& result)
{
result = Normalize(input);
}
// dot product with another vector
FORCE_INLINE static T Dot(const Vector3Base& a, const Vector3Base& b)
{
return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
}
// Calculates the cross product of two vectors
// @param a First source vector
// @param b Second source vector
// @param result When the method completes, contains the cross product of the two vectors
static void Cross(const Vector3Base& a, const Vector3Base& b, Vector3Base& result)
{
result = Vector3Base(a.Y * b.Z - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X);
}
// Calculates the cross product of two vectors
// @param a First source vector
// @param b Second source vector
// @returns Cross product of the two vectors
static Vector3Base Cross(const Vector3Base& a, const Vector3Base& b)
{
return Vector3Base(a.Y * b.Z - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X);
}
// Performs a linear interpolation between two vectors
// @param start Start vector
// @param end End vector
// @param amount Value between 0 and 1 indicating the weight of end
// @param result When the method completes, contains the linear interpolation of the two vectors
static void Lerp(const Vector3Base& start, const Vector3Base& end, T amount, Vector3Base& result)
{
result.X = Math::Lerp(start.X, end.X, amount);
result.Y = Math::Lerp(start.Y, end.Y, amount);
result.Z = Math::Lerp(start.Z, end.Z, amount);
}
// <summary>
// Performs a linear interpolation between two vectors.
// </summary>
static Vector3Base Lerp(const Vector3Base& start, const Vector3Base& end, T amount)
{
Vector3Base result;
Lerp(start, end, amount, result);
return result;
}
// Performs a cubic interpolation between two vectors
// @param start Start vector
// @param end End vector
// @param amount Value between 0 and 1 indicating the weight of end
// @param result When the method completes, contains the cubic interpolation of the two vectors
static void SmoothStep(const Vector3Base& start, const Vector3Base& end, T amount, Vector3Base& result)
{
amount = Math::SmoothStep(amount);
Lerp(start, end, amount, result);
}
// Performs a Hermite spline interpolation.
// @param value1 First source position vector
// @param tangent1 First source tangent vector
// @param value2 Second source position vector
// @param tangent2 Second source tangent vector
// @param amount Weighting factor,
// @param result When the method completes, contains the result of the Hermite spline interpolation,
static FLAXENGINE_API void Hermite(const Vector3Base& value1, const Vector3Base& tangent1, const Vector3Base& value2, const Vector3Base& tangent2, T amount, Vector3Base& result);
// Returns the reflection of a vector off a surface that has the specified normal
// @param vector The source vector
// @param normal Normal of the surface
// @param result When the method completes, contains the reflected vector
static FLAXENGINE_API void Reflect(const Vector3Base& vector, const Vector3Base& normal, Vector3Base& result);
// Transforms a 3D vector by the given Quaternion rotation
// @param vector The vector to rotate
// @param rotation The Quaternion rotation to apply
// @param result When the method completes, contains the transformed Vector3
static FLAXENGINE_API void Transform(const Vector3Base& vector, const Quaternion& rotation, Vector3Base& result);
// Transforms a 3D vector by the given Quaternion rotation
// @param vector The vector to rotate
// @param rotation The Quaternion rotation to apply
// @returns The transformed Vector3
static FLAXENGINE_API Vector3Base Transform(const Vector3Base& vector, const Quaternion& rotation);
// Transforms a 3D vector by the given matrix
// @param vector The source vector
// @param transform The transformation matrix
// @param result When the method completes, contains the transformed Vector3
static FLAXENGINE_API void Transform(const Vector3Base& vector, const Matrix& transform, Vector3Base& result);
// Transforms a 3D vector by the given matrix
// @param vector The source vector
// @param transform The transformation matrix
// @param result When the method completes, contains the transformed Vector3
static FLAXENGINE_API void Transform(const Vector3Base& vector, const Matrix3x3& transform, Vector3Base& result);
// Transforms a 3D vector by the given transformation
// @param vector The source vector
// @param transform The transformation
// @param result When the method completes, contains the transformed Vector3
static FLAXENGINE_API void Transform(const Vector3Base& vector, const ::Transform& transform, Vector3Base& result);
// Transforms a 3D vector by the given matrix
// @param vector The source vector
// @param transform The transformation matrix
// @returns Transformed Vector3
static FLAXENGINE_API Vector3Base Transform(const Vector3Base& vector, const Matrix& transform);
// Transforms a 3D vector by the given transformation
// @param vector The source vector
// @param transform The transformation
// @returns Transformed Vector3
static FLAXENGINE_API Vector3Base Transform(const Vector3Base& vector, const ::Transform& transform);
// Transforms a 3D vector by the given matrix
// @param vector The source vector
// @param transform The transformation matrix
// @param result When the method completes, contains the transformed Vector4
static FLAXENGINE_API void Transform(const Vector3Base& vector, const Matrix& transform, Vector4Base<T>& result);
// Performs a coordinate transformation using the given matrix
// @param coordinate The coordinate vector to transform
// @param transform The transformation matrix
// @param result When the method completes, contains the transformed coordinates
static FLAXENGINE_API void TransformCoordinate(const Vector3Base& coordinate, const Matrix& transform, Vector3Base& result);
// Performs a normal transformation using the given matrix
// @param normal The normal vector to transform
// @param transform The transformation matrix
// @param result When the method completes, contains the transformed normal
static FLAXENGINE_API void TransformNormal(const Vector3Base& normal, const Matrix& transform, Vector3Base& result);
/// <summary>
/// Projects a vector onto another vector.
/// </summary>
/// <param name="vector">The vector to project.</param>
/// <param name="onNormal">The projection normal vector.</param>
/// <returns>The projected vector.</returns>
static FLAXENGINE_API Vector3Base Project(const Vector3Base& vector, const Vector3Base& onNormal);
/// <summary>
/// Projects a vector onto a plane defined by a normal orthogonal to the plane.
/// </summary>
/// <param name="vector">The vector to project.</param>
/// <param name="planeNormal">The plane normal vector.</param>
/// <returns>The projected vector.</returns>
static Vector3Base ProjectOnPlane(const Vector3Base& vector, const Vector3Base& planeNormal)
{
return vector - Project(vector, planeNormal);
}
// Projects a 3D vector from object space into screen space
// @param vector The vector to project
// @param x The X position of the viewport
// @param y The Y position of the viewport
// @param width The width of the viewport
// @param height The height of the viewport
// @param minZ The minimum depth of the viewport
// @param maxZ The maximum depth of the viewport
// @param worldViewProjection The combined world-view-projection matrix
// @param result When the method completes, contains the vector in screen space
static FLAXENGINE_API void Project(const Vector3Base& vector, float x, float y, float width, float height, float minZ, float maxZ, const Matrix& worldViewProjection, Vector3Base& result);
// Projects a 3D vector from object space into screen space
// @param vector The vector to project
// @param x The X position of the viewport
// @param y The Y position of the viewport
// @param width The width of the viewport
// @param height The height of the viewport
// @param minZ The minimum depth of the viewport
// @param maxZ The maximum depth of the viewport
// @param worldViewProjection The combined world-view-projection matrix
// @returns The vector in screen space
static Vector3Base Project(const Vector3Base& vector, float x, float y, float width, float height, float minZ, float maxZ, const Matrix& worldViewProjection)
{
Vector3Base result;
Project(vector, x, y, width, height, minZ, maxZ, worldViewProjection, result);
return result;
}
// Projects a 3D vector from screen space into object space
// @param vector The vector to project
// @param x The X position of the viewport
// @param y The Y position of the viewport
// @param width The width of the viewport
// @param height The height of the viewport
// @param minZ The minimum depth of the viewport
// @param maxZ The maximum depth of the viewport
// @param worldViewProjection The combined world-view-projection matrix
// @param result When the method completes, contains the vector in object space
static FLAXENGINE_API void Unproject(const Vector3Base& vector, float x, float y, float width, float height, float minZ, float maxZ, const Matrix& worldViewProjection, Vector3Base& result);
// Projects a 3D vector from screen space into object space
// @param vector The vector to project
// @param x The X position of the viewport
// @param y The Y position of the viewport
// @param width The width of the viewport
// @param height The height of the viewport
// @param minZ The minimum depth of the viewport
// @param maxZ The maximum depth of the viewport
// @param worldViewProjection The combined world-view-projection matrix
// @returns The vector in object space
static Vector3Base Unproject(const Vector3Base& vector, float x, float y, float width, float height, float minZ, float maxZ, const Matrix& worldViewProjection)
{
Vector3Base result;
Unproject(vector, x, y, width, height, minZ, maxZ, worldViewProjection, result);
return result;
}
/// <summary>
/// Creates an orthonormal basis from a basis with at least two orthogonal vectors.
/// </summary>
/// <param name="xAxis">The X axis.</param>
/// <param name="yAxis">The y axis.</param>
/// <param name="zAxis">The z axis.</param>
static FLAXENGINE_API void CreateOrthonormalBasis(Vector3Base& xAxis, Vector3Base& yAxis, Vector3Base& zAxis);
/// <summary>
/// Finds the best arbitrary axis vectors to represent U and V axes of a plane, by using this vector as the normal of the plane.
/// </summary>
/// <param name="firstAxis">The reference to first axis.</param>
/// <param name="secondAxis">The reference to second axis.</param>
FLAXENGINE_API void FindBestAxisVectors(Vector3Base& firstAxis, Vector3Base& secondAxis) const;
/// <summary>
/// Calculates the area of the triangle.
/// </summary>
/// <param name="v0">The first triangle vertex.</param>
/// <param name="v1">The second triangle vertex.</param>
/// <param name="v2">The third triangle vertex.</param>
/// <returns>The triangle area.</returns>
static FLAXENGINE_API T TriangleArea(const Vector3Base& v0, const Vector3Base& v1, const Vector3Base& v2);
/// <summary>
/// Calculates the angle (in radians) between from and to. This is always the smallest value.
/// </summary>
/// <param name="from">The first vector.</param>
/// <param name="to">The second vector.</param>
/// <returns>The angle (in radians).</returns>
static FLAXENGINE_API T Angle(const Vector3Base& from, const Vector3Base& to);
};
template<typename T>
inline Vector3Base<T> operator+(T a, const Vector3Base<T>& b)
{
return b + a;
}
template<typename T>
inline Vector3Base<T> operator-(T a, const Vector3Base<T>& b)
{
return Vector3Base<T>(a) - b;
}
template<typename T>
inline Vector3Base<T> operator*(T a, const Vector3Base<T>& b)
{
return b * a;
}
template<typename T>
inline Vector3Base<T> operator/(T a, const Vector3Base<T>& b)
{
return Vector3Base<T>(a) / b;
}
template<typename T>
inline Vector3Base<T> operator+(typename TOtherFloat<T>::Type a, const Vector3Base<T>& b)
{
return b + a;
}
template<typename T>
inline Vector3Base<T> operator-(typename TOtherFloat<T>::Type a, const Vector3Base<T>& b)
{
return Vector3Base<T>(a) - b;
}
template<typename T>
inline Vector3Base<T> operator*(typename TOtherFloat<T>::Type a, const Vector3Base<T>& b)
{
return b * a;
}
template<typename T>
inline Vector3Base<T> operator/(typename TOtherFloat<T>::Type a, const Vector3Base<T>& b)
{
return Vector3Base<T>(a) / b;
}
namespace Math
{
template<typename T>
FORCE_INLINE static bool NearEqual(const Vector3Base<T>& a, const Vector3Base<T>& b)
{
return Vector3Base<T>::NearEqual(a, b);
}
template<typename T>
FORCE_INLINE static Vector3Base<T> UnwindDegrees(const Vector3Base<T>& v)
{
return Vector3Base<T>(UnwindDegrees(v.X), UnwindDegrees(v.Y), UnwindDegrees(v.Z));
}
}
template<>
struct TIsPODType<Float3>
{
enum { Value = true };
};
DEFINE_DEFAULT_FORMATTING(Float3, "X:{0} Y:{1} Z:{2}", v.X, v.Y, v.Z);
template<>
struct TIsPODType<Double3>
{
enum { Value = true };
};
DEFINE_DEFAULT_FORMATTING(Double3, "X:{0} Y:{1} Z:{2}", v.X, v.Y, v.Z);
template<>
struct TIsPODType<Int3>
{