UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Ray.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Math/MathFwd.h" // IWYU pragma: export
6#include "Math/Vector.h"
7
8
12namespace UE
13{
14namespace Math
15{
16
17template<typename T>
18struct TRay
19{
20public:
21 using FReal = T;
22
25
28
29public:
30
33 {
34 Init();
35 }
36
41 [[nodiscard]] explicit TRay<T>( EForceInit )
42 {
43 Init();
44 }
45
46
55 {
56 this->Origin = Origin;
57 this->Direction = Direction;
58 if (bDirectionIsNormalized == false)
59 {
60 this->Direction.Normalize(); // is this a full-accuracy sqrt?
61 }
62 }
63
64
68 void Init()
69 {
71 Direction = TVector<T>(0, 0, 1);
72 }
73
80 [[nodiscard]] bool operator==( const TRay<T>& Other ) const
81 {
82 return (Origin == Other.Origin) && (Direction == Other.Direction);
83 }
84
91 [[nodiscard]] bool operator!=( const TRay<T>& Other ) const
92 {
93 return !(*this == Other);
94 }
95
96
97public:
98
105 [[nodiscard]] TVector<T> PointAt(T RayParameter) const
106 {
107 return Origin + RayParameter * Direction;
108 }
109
117 {
119 }
120
128 {
129 T RayParameter = TVector<T>::DotProduct((Point - Origin), Direction);
130 if (RayParameter < 0)
131 {
133 }
134 else
135 {
136 TVector<T> ProjectionPt = Origin + RayParameter * Direction;
138 }
139 }
140
147 [[nodiscard]] T Dist(const TVector<T>& Point) const
148 {
149 return FMath::Sqrt(DistSquared(Point));
150 }
151
158 {
159 T RayParameter = TVector<T>::DotProduct((Point - Origin), Direction);
160 if (RayParameter < 0)
161 {
162 return Origin;
163 }
164 else
165 {
166 return Origin + RayParameter * Direction;
167 }
168 }
169
170
171
172public:
173
179 [[nodiscard]] FString ToString() const
180 {
181 return FString::Printf(TEXT("Origin=(%s), Direction=(%s)"), *Origin.ToString(), *Direction.ToString());
182 }
183
192 friend FArchive& operator<<( FArchive& Ar, TRay<T>& Ray )
193 {
194 return Ar << Ray.Origin << Ray.Direction;
195 }
196
197 // Note: TRay is usually written via binary serialization. This function exists for SerializeFromMismatchedTag conversion usage.
199 {
200 Ar << *this;
201 return true;
202 }
203
205 {
206 if constexpr (std::is_same_v<T, float>)
207 {
209 }
210 else if constexpr (std::is_same_v<T, double>)
211 {
213 }
214 else
215 {
216 static_assert(sizeof(T) == 0, "Unimplemented");
217 return false;
218 }
219 }
220
221 // Conversion to other type.
222 template<typename FArg UE_REQUIRES(!std::is_same_v<T, FArg>)>
223 explicit TRay(const TRay<FArg>& From)
224 : TRay<T>(TVector<T>(From.Origin), TVector<T>(From.Direction), true)
225 {
226 }
227};
228
229} // namespace UE::Math
230} // namespace UE
231
233
234template<> struct TIsPODType<FRay3f> { enum { Value = true }; };
235template<> struct TIsPODType<FRay3d> { enum { Value = true }; };
236template<> struct TIsUECoreVariant<FRay3f> { enum { Value = true }; };
237template<> struct TIsUECoreVariant<FRay3d> { enum { Value = true }; };
238template<> struct TCanBulkSerialize<FRay3f> { enum { Value = true }; };
239template<> struct TCanBulkSerialize<FRay3d> { enum { Value = true }; };
EForceInit
Definition CoreMiscDefines.h:154
#define TEXT(x)
Definition Platform.h:1272
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
return true
Definition ExternalRpcRegistry.cpp:601
#define UE_SERIALIZE_VARIANT_FROM_MISMATCHED_TAG(AR_OR_SLOT, ALIAS, TYPE, ALT_TYPE)
Definition LargeWorldCoordinatesSerializer.h:9
#define UE_DECLARE_LWC_TYPE(...)
Definition LargeWorldCoordinates.h:27
Definition Archive.h:1208
Definition NameTypes.h:617
Definition AdvancedWidgetsModule.cpp:13
Definition Array.h:45
@ Value
Definition Array.h:46
Definition IsPODType.h:12
@ Value
Definition IsPODType.h:13
Definition IsUECoreType.h:19
@ Value
Definition IsUECoreType.h:20
Definition Ray.h:19
T FReal
Definition Ray.h:21
TRay(const TRay< FArg > &From)
Definition Ray.h:223
bool operator==(const TRay< T > &Other) const
Definition Ray.h:80
TVector< T > Origin
Definition Ray.h:24
TVector< T > ClosestPoint(const TVector< T > &Point) const
Definition Ray.h:157
bool Serialize(FArchive &Ar)
Definition Ray.h:198
bool operator!=(const TRay< T > &Other) const
Definition Ray.h:91
TVector< T > Direction
Definition Ray.h:27
void Init()
Definition Ray.h:68
bool SerializeFromMismatchedTag(FName StructTag, FArchive &Ar)
Definition Ray.h:204
T Dist(const TVector< T > &Point) const
Definition Ray.h:147
T GetParameter(const TVector< T > &Point) const
Definition Ray.h:116
friend FArchive & operator<<(FArchive &Ar, TRay< T > &Ray)
Definition Ray.h:192
FString ToString() const
Definition Ray.h:179
TRay()
Definition Ray.h:32
TVector< T > PointAt(T RayParameter) const
Definition Ray.h:105
T DistSquared(const TVector< T > &Point) const
Definition Ray.h:127
TRay(const TVector< T > &Origin, const TVector< T > &Direction, bool bDirectionIsNormalized=false)
Definition Ray.h:54
Definition Vector.h:51
static UE_FORCEINLINE_HINT T DotProduct(const TVector< T > &A, const TVector< T > &B)
Definition Vector.h:1559
bool Normalize(T Tolerance=UE_SMALL_NUMBER)
Definition Vector.h:1767
static UE_FORCEINLINE_HINT T DistSquared(const TVector< T > &V1, const TVector< T > &V2)
Definition Vector.h:2478