UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
VectorTypes.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Math/Vector.h"
6#include "Math/Vector4.h"
7#include "MathUtil.h"
10#include <sstream>
11
12namespace UE {
13namespace Geometry {
14
15
16
18template <typename T>
19constexpr T DotPerp(const UE::Math::TVector2<T>& V1, const UE::Math::TVector2<T>& V2)
20{
21 return V1.X * V2.Y - V1.Y * V2.X;
22}
23
25template <typename T>
27{
28 return UE::Math::TVector2<T>(V.Y, -V.X);
29}
30
32template<typename T>
34{
35 return DotPerp((B - A), (C - A));
36}
37
38
39template <typename T>
40constexpr bool IsNormalized(const UE::Math::TVector2<T>& Vector, const T Tolerance = TMathUtil<T>::ZeroTolerance)
41{
42 return TMathUtil<T>::Abs((Vector.X*Vector.X + Vector.Y*Vector.Y) - 1) < Tolerance;
43}
44
45template <typename T>
46T Normalize(UE::Math::TVector2<T>& Vector, const T Epsilon = 0)
47{
48 T length = Vector.Length();
49 if (length > Epsilon)
50 {
51 T invLength = ((T)1) / length;
52 Vector.X *= invLength;
53 Vector.Y *= invLength;
54 return length;
55 }
56 Vector.X = Vector.Y = (T)0;
57 return (T)0;
58}
59
60template <typename T>
62{
63 T length = Vector.Length();
64 if (length > Epsilon)
65 {
66 T invLength = ((T)1) / length;
68 }
69 return UE::Math::TVector2<T>((T)0, (T)0);
70}
71
72
73template<typename T>
75{
76 T dx = V2.X - V1.X;
77 T dy = V2.Y - V1.Y;
78 return TMathUtil<T>::Sqrt(dx * dx + dy * dy);
79}
80
81template<typename T>
83{
84 T dx = V2.X - V1.X;
85 T dy = V2.Y - V1.Y;
86 return dx * dx + dy * dy;
87}
88
89
90// Angle in Degrees
91template <typename T>
93{
94 T DotVal = V1.Dot(V2);
95 T ClampedDot = (DotVal < (T)-1) ? (T)-1 : ((DotVal > (T)1) ? (T)1 : DotVal);
97}
98
99// Angle in Radians
100template <typename T>
102{
103 T DotVal = V1.Dot(V2);
104 T ClampedDot = (DotVal < (T)-1) ? (T)-1 : ((DotVal > (T)1) ? (T)1 : DotVal);
106}
107
108// Angle in Radians
109template <typename T>
111{
112 T DotVal = V1.Dot(V2);
113 T ClampedDot = (DotVal < (T)-1) ? (T)-1 : ((DotVal > (T)1) ? (T)1 : DotVal);
114 T Direction = DotPerp(V1, V2);
115 if (Direction * Direction < TMathUtil<T>::ZeroTolerance)
116 {
117 return (DotVal < 0) ? TMathUtil<T>::Pi : (T)0;
118 }
119 else
120 {
121 T Sign = Direction < 0 ? (T)-1 : (T)1;
123 }
124}
125
126template <typename T>
133
134
135
136
138template <typename T>
140{
141 UE::Math::TVector<T> UnitVec((T)0, (T)0, (T)0);
142 UnitVec[FMath::Clamp(Axis, 0, 2)] = (T)1;
143 return UnitVec;
144}
145
146
147template<typename T>
149{
150 return TMathUtil<T>::Sqrt(V.X*V.X + V.Y*V.Y + V.Z*V.Z);
151}
152
153template<typename T>
155{
156 return V.X*V.X + V.Y*V.Y + V.Z*V.Z;
157}
158
159
160template <typename T>
161constexpr bool IsNormalized(const UE::Math::TVector<T>& Vector, const T Tolerance = TMathUtil<T>::ZeroTolerance)
162{
163 return TMathUtil<T>::Abs((Vector.X*Vector.X + Vector.Y*Vector.Y + Vector.Z*Vector.Z) - 1) < Tolerance;
164}
165
166
167template<typename T>
168T Normalize(UE::Math::TVector<T>& Vector, const T Epsilon = 0)
169{
170 T length = Vector.Length();
171 if (length > Epsilon)
172 {
173 T invLength = ((T)1) / length;
174 Vector.X *= invLength;
175 Vector.Y *= invLength;
176 Vector.Z *= invLength;
177 return length;
178 }
179 Vector.X = Vector.Y = Vector.Z = (T)0;
180 return (T)0;
181}
182
183template<typename T>
184constexpr UE::Math::TVector<T> Normalized(const UE::Math::TVector<T>& Vector, const T Epsilon = 0)
185{
186 T length = Vector.Length();
187 if (length > Epsilon)
188 {
189 T invLength = ((T)1) / length;
191 }
192 return UE::Math::TVector<T>((T)0, (T)0, (T)0);
193}
194
195template<typename T>
197{
198 T dx = V2.X - V1.X;
199 T dy = V2.Y - V1.Y;
200 T dz = V2.Z - V1.Z;
201 return TMathUtil<T>::Sqrt(dx * dx + dy * dy + dz * dz);
202}
203
204template<typename T>
206{
207 T dx = V2.X - V1.X;
208 T dy = V2.Y - V1.Y;
209 T dz = V2.Z - V1.Z;
210 return dx * dx + dy * dy + dz * dz;
211}
212
213
214
215template<typename T>
217{
218 return V1.X * V2.X + V1.Y * V2.Y + V1.Z * V2.Z;
219}
220
221template<typename T>
223{
225 V1.Y * V2.Z - V1.Z * V2.Y,
226 V1.Z * V2.X - V1.X * V2.Z,
227 V1.X * V2.Y - V1.Y * V2.X);
228}
229
230template<typename T>
232{
233 UE::Math::TVector<T> N = V1.Cross(V2);
234 return Normalized(N);
235}
236
241template <typename T>
243{
244 T DotVal = V1.Dot(V2);
245 T ClampedDot = (DotVal < (T)-1) ? (T)-1 : ((DotVal > (T)1) ? (T)1 : DotVal);
247}
248
253template <typename T>
255{
256 T DotVal = V1.Dot(V2);
257 T ClampedDot = (DotVal < (T)-1) ? (T)-1 : ((DotVal > (T)1) ? (T)1 : DotVal);
259}
260
261template <typename T>
263{
264 return UE::Math::TVector2<T>(V.X, V.Y);
265}
266
267template <typename T>
269{
270 return UE::Math::TVector2<T>(V.X, V.Z);
271}
272
273template <typename T>
275{
276 return UE::Math::TVector2<T>(V.Y, V.Z);
277}
278
279
280template<typename T>
282{
283 return UE::Math::TVector<T>(TMathUtil<T>::Min(V0.X, V1.X),
284 TMathUtil<T>::Min(V0.Y, V1.Y),
285 TMathUtil<T>::Min(V0.Z, V1.Z));
286}
287
288template<typename T>
290{
291 return UE::Math::TVector<T>(TMathUtil<T>::Max(V0.X, V1.X),
292 TMathUtil<T>::Max(V0.Y, V1.Y),
293 TMathUtil<T>::Max(V0.Z, V1.Z));
294}
295
296
297template<typename T>
299{
300 return TMathUtil<T>::Max3(Vector.X, Vector.Y, Vector.Z);
301}
302
304template<typename T>
309
310template<typename T>
312{
313 return TMathUtil<T>::Min3(Vector.X, Vector.Y, Vector.Z);
314}
315
317template<typename T>
322
323template<typename T>
328
330template<typename T>
335
336template<typename T>
341
343template<typename T>
348
349template<typename T>
351{
352 return FLinearColor((float)Vector.X, (float)Vector.Y, (float)Vector.Z);
353}
354
355template<typename T>
357{
358 T OneMinusAlpha = (T)1 - Alpha;
359 return UE::Math::TVector<T>(OneMinusAlpha * A.X + Alpha * B.X,
360 OneMinusAlpha * A.Y + Alpha * B.Y,
361 OneMinusAlpha * A.Z + Alpha * B.Z);
362}
363
364template<typename T>
366{
368 WeightA * A.X + WeightB * B.X + WeightC * C.X,
369 WeightA * A.Y + WeightB * B.Y + WeightC * C.Y,
370 WeightA * A.Z + WeightB * B.Z + WeightC * C.Z);
371}
372
373
374template <typename RealType>
375std::ostream& operator<<(std::ostream& os, const UE::Math::TVector<RealType>& Vec)
376{
377 os << Vec.X << " " << Vec.Y << " " << Vec.Z;
378 return os;
379}
380
381
382
383
384
385template<typename T>
387{
388 return FLinearColor((float)V.X, (float)V.Y, (float)V.Z, (float)V.W);
389}
390
391template<typename T>
393{
394 return UE::Math::TVector4<T>( (T)Color.R, (T)Color.G, (T)Color.B, (T)Color.A );
395}
396
397template<typename T>
399{
400 return FColor(
401 FMathf::Clamp((int)((float)Vector.X * 255.0f), 0, 255),
402 FMathf::Clamp((int)((float)Vector.Y * 255.0f), 0, 255),
403 FMathf::Clamp((int)((float)Vector.Z * 255.0f), 0, 255),
404 FMathf::Clamp((int)((float)Vector.W * 255.0f), 0, 255));
405}
406
407
408template<typename T>
410{
411 T OneMinusAlpha = (T)1 - Alpha;
413 OneMinusAlpha * A.X + Alpha * B.X,
414 OneMinusAlpha * A.Y + Alpha * B.Y,
415 OneMinusAlpha * A.Z + Alpha * B.Z,
416 OneMinusAlpha * A.W + Alpha * B.W);
417}
418
419} // end namespace UE::Geometry
420
421namespace Math
422{
423
424template <typename RealType>
425std::ostream& operator<<(std::ostream& os, const TVector2<RealType>& Vec)
426{
427 os << Vec.X << " " << Vec.Y;
428 return os;
429}
430
431template <typename RealType>
432std::ostream& operator<<(std::ostream& os, const TVector4<RealType>& Vec)
433{
434 os << Vec.X << " " << Vec.Y << " " << Vec.Z << " " << Vec.W;
435 return os;
436}
437
438} // end namespace UE::Math
439
440} // end namespace UE
441
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition MathUtil.h:150
static RealType Min3(const RealType A, const RealType B, const RealType C)
Definition MathUtil.h:277
static RealType Clamp(const RealType Value, const RealType ClampMin, const RealType ClampMax)
Definition MathUtil.h:222
static int32 Min3Index(const RealType A, const RealType B, const RealType C)
Definition MathUtil.h:283
static int32 Max3Index(const RealType A, const RealType B, const RealType C)
Definition MathUtil.h:258
static RealType Max3(const RealType A, const RealType B, const RealType C)
Definition MathUtil.h:252
static RealType Sqrt(const RealType Value)
Definition MathUtil.h:342
static RealType Abs(const RealType Value)
Definition MathUtil.h:215
static RealType ACos(const RealType Value)
Definition MathUtil.h:378
constexpr UE::Math::TVector< T > MakeUnitVector3(int32 Axis)
Definition VectorTypes.h:139
constexpr T MaxElement(const UE::Math::TVector< T > &Vector)
Definition VectorTypes.h:298
T AngleR(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:101
constexpr T MaxAbsElement(const UE::Math::TVector< T > &Vector)
Definition VectorTypes.h:324
constexpr UE::Math::TVector2< T > PerpCW(const UE::Math::TVector2< T > &V)
Definition VectorTypes.h:26
T AngleD(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:92
constexpr UE::Math::TVector2< T > GetXZ(const UE::Math::TVector< T > &V)
Definition VectorTypes.h:268
constexpr FLinearColor ToLinearColor(const UE::Math::TVector< T > &Vector)
Definition VectorTypes.h:350
UE::Math::TVector< T > UnitCross(const UE::Math::TVector< T > &V1, const UE::Math::TVector< T > &V2)
Definition VectorTypes.h:231
constexpr int32 MaxAbsElementIndex(const UE::Math::TVector< T > &Vector)
Definition VectorTypes.h:331
constexpr T DotPerp(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:19
UE::Math::TVector< T > Blend3(const UE::Math::TVector< T > &A, const UE::Math::TVector< T > &B, const UE::Math::TVector< T > &C, const T &WeightA, const T &WeightB, const T &WeightC)
Definition VectorTypes.h:365
constexpr FColor ToFColor(const UE::Math::TVector4< T > &Vector)
Definition VectorTypes.h:398
constexpr int32 MaxElementIndex(const UE::Math::TVector< T > &Vector)
Definition VectorTypes.h:305
T DistanceSquared(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:82
constexpr UE::Math::TVector2< T > GetYZ(const UE::Math::TVector< T > &V)
Definition VectorTypes.h:274
UE::Math::TVector4< T > ToVector4(const FLinearColor &Color)
Definition VectorTypes.h:392
constexpr int32 MinAbsElementIndex(const UE::Math::TVector< T > &Vector)
Definition VectorTypes.h:344
T SquaredLength(const UE::Math::TVector< T > &V)
Definition VectorTypes.h:154
T SignedAngleR(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:110
constexpr UE::Math::TVector2< T > GetXY(const UE::Math::TVector< T > &V)
Definition VectorTypes.h:262
T Orient(const UE::Math::TVector2< T > &A, const UE::Math::TVector2< T > &B, const UE::Math::TVector2< T > &C)
Definition VectorTypes.h:33
constexpr int32 MinElementIndex(const UE::Math::TVector< T > &Vector)
Definition VectorTypes.h:318
T Normalize(UE::Math::TVector2< T > &Vector, const T Epsilon=0)
Definition VectorTypes.h:46
constexpr T MinAbsElement(const UE::Math::TVector< T > &Vector)
Definition VectorTypes.h:337
constexpr T MinElement(const UE::Math::TVector< T > &Vector)
Definition VectorTypes.h:311
constexpr bool IsNormalized(const UE::Math::TVector2< T > &Vector, const T Tolerance=TMathUtil< T >::ZeroTolerance)
Definition VectorTypes.h:40
FArchive & operator<<(FArchive &Ar, TBoxSphereBounds< float, float > &Bounds)
Definition BoxSphereBounds.h:396
Definition AdvancedWidgetsModule.cpp:13
Definition Color.h:486
Definition Color.h:48
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)
Definition UnrealMathUtility.h:592
Definition Vector2D.h:38
UE_FORCEINLINE_HINT T Length() const
Definition Vector2D.h:497
T Y
Definition Vector2D.h:52
T X
Definition Vector2D.h:49
Definition Vector4.h:30
T Y
Definition Vector4.h:46
T Z
Definition Vector4.h:49
T W
Definition Vector4.h:52
T X
Definition Vector4.h:43
Definition Vector.h:51
T Z
Definition Vector.h:68
T Y
Definition Vector.h:65
T Length() const
Definition Vector.h:1722
T X
Definition Vector.h:62