Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
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/Vector.h"
6
7
8/**
9 * 3D Ray represented by Origin and (normalized) Direction
10 */
11namespace UE
12{
13namespace Math
14{
15
16template<typename T>
17struct TRay
18{
19public:
20 using FReal = T;
21
22 /** Ray origin point */
23 TVector<T> Origin;
24
25 /** Ray direction vector (always normalized) */
26 TVector<T> Direction;
27
28public:
29
30 /** Default constructor initializes ray to Zero origin and Z-axis direction */
31 TRay()
32 {
33 Init();
34 }
35
36 /**
37 * Creates and initializes a new Ray to Zero Origin and Z-axis Direction.
38 * @param EForceInit Force Init Enum.
39 */
40 explicit TRay<T>( EForceInit )
41 {
42 Init();
43 }
44
45
46 /**
47 * Initialize Ray with origin and direction
48 *
49 * @param Origin Ray Origin Point
50 * @param Direction Ray Direction Vector
51 * @param bDirectionIsNormalized Direction will be normalized unless this is passed as true (default false)
52 */
53 TRay(const TVector<T>& Origin, const TVector<T>& Direction, bool bDirectionIsNormalized = false)
54 {
55 this->Origin = Origin;
56 this->Direction = Direction;
57 if (bDirectionIsNormalized == false)
58 {
59 this->Direction.Normalize(); // is this a full-accuracy sqrt?
60 }
61 }
62
63
64 /**
65 * Set the initial values of the Ray to Zero Origin and Z-axis Direction.
66 */
67 void Init()
68 {
69 Origin = TVector<T>::ZeroVector;
70 Direction = TVector<T>(0, 0, 1);
71 }
72
73 /**
74 * Compares two Rays for equality.
75 *
76 * @param Other The other Ray to compare with.
77 * @return true if the Rays are equal, false otherwise.
78 */
79 bool operator==( const TRay<T>& Other ) const
80 {
81 return (Origin == Other.Origin) && (Direction == Other.Direction);
82 }
83
84 /**
85 * Compares two Rays for inequality.
86 *
87 * @param Other The other Ray to compare with.
88 * @return true if the Rays are not equal, false otherwise.
89 */
90 bool operator!=( const TRay<T>& Other ) const
91 {
92 return !(*this == Other);
93 }
94
95
96public:
97
98 /**
99 * Calculate position on ray at given distance/parameter
100 *
101 * @param RayParameter Scalar distance along Ray
102 * @return Point on Ray
103 */
104 TVector<T> PointAt(T RayParameter) const
105 {
106 return Origin + RayParameter * Direction;
107 }
108
109 /**
110 * Calculate ray parameter (distance from origin to closest point) for query Point
111 *
112 * @param Point query Point
113 * @return distance along ray from origin to closest point
114 */
115 T GetParameter(const TVector<T>& Point) const
116 {
117 return TVector<T>::DotProduct((Point - Origin), Direction);
118 }
119
120 /**
121 * Find minimum squared distance from query point to ray
122 *
123 * @param Point query Point
124 * @return squared distance to Ray
125 */
126 T DistSquared(const TVector<T>& Point) const
127 {
128 T RayParameter = TVector<T>::DotProduct((Point - Origin), Direction);
129 if (RayParameter < 0)
130 {
131 return TVector<T>::DistSquared(Origin, Point);
132 }
133 else
134 {
135 TVector<T> ProjectionPt = Origin + RayParameter * Direction;
136 return TVector<T>::DistSquared(ProjectionPt, Point);
137 }
138 }
139
140 /**
141 * Find minimum distance from query point to ray
142 *
143 * @param Point query Point
144 * @return distance to Ray
145 */
146 T Dist(const TVector<T>& Point) const
147 {
148 return FMath::Sqrt(DistSquared(Point));
149 }
150
151 /**
152 * Find closest point on ray to query point
153 * @param Point query point
154 * @return closest point on Ray
155 */
156 TVector<T> ClosestPoint(const TVector<T>& Point) const
157 {
158 T RayParameter = TVector<T>::DotProduct((Point - Origin), Direction);
159 if (RayParameter < 0)
160 {
161 return Origin;
162 }
163 else
164 {
165 return Origin + RayParameter * Direction;
166 }
167 }
168
169
170
171public:
172
173 /**
174 * Get a textual representation of the Ray.
175 *
176 * @return Text describing the Ray.
177 */
178 FString ToString() const
179 {
180 return FString::Printf(TEXT("Origin=(%s), Direction=(%s)"), *Origin.ToString(), *Direction.ToString());
181 }
182
183 /**
184 * Serializes the Ray.
185 *
186 * @param Ar The archive to serialize into.
187 * @param Box The box to serialize.
188 *
189 * @return Reference to the Archive after serialization.
190 */
191 friend FArchive& operator<<( FArchive& Ar, TRay<T>& Ray )
192 {
193 return Ar << Ray.Origin << Ray.Direction;
194 }
195
196 // Note: TRay is usually written via binary serialization. This function exists for SerializeFromMismatchedTag conversion usage.
197 bool Serialize(FArchive& Ar)
198 {
199 Ar << *this;
200 return true;
201 }
202
203 bool SerializeFromMismatchedTag(FName StructTag, FArchive& Ar);
204
205 // Conversion to other type.
206 template<typename FArg, TEMPLATE_REQUIRES(!std::is_same_v<T, FArg>)>
207 explicit TRay(const TRay<FArg>& From) : TRay<T>(TVector<T>(From.Origin), TVector<T>(From.Direction), true) {}
208};
209
210} // namespace UE::Math
211} // namespace UE
212
214
215template<> struct TIsPODType<FRay3f> { enum { Value = true }; };
216template<> struct TIsPODType<FRay3d> { enum { Value = true }; };
217template<> struct TIsUECoreVariant<FRay3f> { enum { Value = true }; };
218template<> struct TIsUECoreVariant<FRay3d> { enum { Value = true }; };
219template<> struct TCanBulkSerialize<FRay3f> { enum { Value = true }; };
220template<> struct TCanBulkSerialize<FRay3d> { enum { Value = true }; };
221
222template<>
223inline bool FRay3f::SerializeFromMismatchedTag(FName StructTag, FArchive& Ar)
224{
225 return UE_SERIALIZE_VARIANT_FROM_MISMATCHED_TAG(Ar, Ray, Ray3f, Ray3d);
226}
227
228template<>
229inline bool FRay3d::SerializeFromMismatchedTag(FName StructTag, FArchive& Ar)
230{
231 return UE_SERIALIZE_VARIANT_FROM_MISMATCHED_TAG(Ar, Ray, Ray3d, Ray3f);
232}
#define UE_DECLARE_LWC_TYPE(...)
#define UE_SERIALIZE_VARIANT_FROM_MISMATCHED_TAG(AR_OR_SLOT, ALIAS, TYPE, ALT_TYPE)
#define TEXT(x)
Definition Platform.h:1108
#define TEMPLATE_REQUIRES(...)