UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
IntrRay3AxisAlignedBox3.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Port of WildMagic IntrRay3Box3, simplified for Axis-Aligned Bounding Box
4
5#pragma once
6
7#include "BoxTypes.h"
8#include "VectorUtil.h"
9
10namespace UE
11{
12namespace Geometry
13{
14
15using namespace UE::Math;
16
20template <typename RealType>
22{
23public:
24
25 // @todo port object version
26
27
28
36 static bool TestIntersection(const TRay<RealType>& Ray, const TAxisAlignedBox3<RealType>& Box, RealType ExpandExtents = 0)
37 {
42 RealType RHS;
43
44 TVector<RealType> diff = Ray.Origin - Box.Center();
46
47 WdU.X = Ray.Direction.X;
48 AWdU.X = FMath::Abs(WdU.X);
49 DdU.X = diff.X;
50 ADdU.X = FMath::Abs(DdU.X);
51 if (ADdU.X > extent.X && DdU.X * WdU.X >= (RealType)0)
52 {
53 return false;
54 }
55
56 WdU.Y = Ray.Direction.Y;
57 AWdU.Y = FMath::Abs(WdU.Y);
58 DdU.Y = diff.Y;
59 ADdU.Y = FMath::Abs(DdU.Y);
60 if (ADdU.Y > extent.Y && DdU.Y * WdU.Y >= (RealType)0)
61 {
62 return false;
63 }
64
65 WdU.Z = Ray.Direction.Z;
66 AWdU.Z = FMath::Abs(WdU.Z);
67 DdU.Z = diff.Z;
68 ADdU.Z = FMath::Abs(DdU.Z);
69 if (ADdU.Z > extent.Z && DdU.Z * WdU.Z >= (RealType)0)
70 {
71 return false;
72 }
73
76
77 AWxDdU.X = FMath::Abs(WxD.X);
78 RHS = extent.Y * AWdU.Z + extent.Z * AWdU.Y;
79 if (AWxDdU.X > RHS)
80 {
81 return false;
82 }
83
84 AWxDdU.Y = FMath::Abs(WxD.Y);
85 RHS = extent.X * AWdU.Z + extent.Z * AWdU.X;
86 if (AWxDdU.Y > RHS)
87 {
88 return false;
89 }
90
91 AWxDdU.Z = FMath::Abs(WxD.Z);
92 RHS = extent.X * AWdU.Y + extent.Y * AWdU.X;
93 if (AWxDdU.Z > RHS)
94 {
95 return false;
96 }
97
98 return true;
99 }
100
101
102
110 inline static bool FindIntersection(const TRay<RealType>& Ray, const TAxisAlignedBox3<RealType>& Box, RealType& RayParamOut)
111 {
113 }
122 static bool FindIntersection(const TVector<RealType>& Origin, const TVector<RealType>& Direction, const TAxisAlignedBox3<RealType>& Box, RealType& RayParamOut)
123 {
124 RealType RayParam0 = 0.0;
126 int Quantity = 0;
130 DoClipping(RayParam0, RayParam1, Origin, Direction, Box,
131 true, Quantity, Point0, Point1, Type);
132
133 if (Type != EIntersectionType::Empty)
134 {
136 return true;
137 }
138 else
139 {
141 return false;
142 }
143 }
144
145
146
147
148protected:
149
150 // internal functions
151
152 static inline bool DoClipping(RealType& t0, RealType& t1,
153 const TVector<RealType>& RayOrigin, const TVector<RealType>& RayDirection,
156 {
157 TVector<RealType> BOrigin = RayOrigin - Box.Center();
158 TVector<RealType> extent = Box.Extents();
159
160 RealType saveT0 = t0, saveT1 = t1;
161 bool notAllClipped =
162 Clip(+RayDirection.X, -BOrigin.X - extent.X, t0, t1) &&
163 Clip(-RayDirection.X, +BOrigin.X - extent.X, t0, t1) &&
164 Clip(+RayDirection.Y, -BOrigin.Y - extent.Y, t0, t1) &&
165 Clip(-RayDirection.Y, +BOrigin.Y - extent.Y, t0, t1) &&
166 Clip(+RayDirection.Z, -BOrigin.Z - extent.Z, t0, t1) &&
167 Clip(-RayDirection.Z, +BOrigin.Z - extent.Z, t0, t1);
168
169 if (notAllClipped && (solid || t0 != saveT0 || t1 != saveT1))
170 {
171 if (t1 > t0)
172 {
174 quantity = 2;
175 Point0 = RayOrigin + t0 * RayDirection;
176 Point1 = RayOrigin + t1 * RayDirection;
177 }
178 else
179 {
181 quantity = 1;
182 Point0 = RayOrigin + t0 * RayDirection;
183 }
184 }
185 else
186 {
187 quantity = 0;
189 }
190
192 }
193
194 static inline bool Clip(RealType denom, RealType numer, RealType& t0, RealType& t1)
195 {
196 // Return value is 'true' if line segment intersects the current test
197 // plane. Otherwise 'false' is returned in which case the line segment
198 // is entirely clipped.
199
200 if (denom > (RealType)0)
201 {
202 if (numer > denom*t1)
203 {
204 return false;
205 }
206 if (numer > denom*t0)
207 {
208 t0 = numer / denom;
209 }
210 return true;
211 }
212 else if (denom < (RealType)0)
213 {
214 if (numer > denom*t0)
215 {
216 return false;
217 }
218 if (numer > denom*t1)
219 {
220 t1 = numer / denom;
221 }
222 return true;
223 }
224 else
225 {
226 return numer <= (RealType)0;
227 }
228 }
229
230
231};
232
235
236} // end namespace UE::Geometry
237} // end namespace UE
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
EIntersectionType
Definition VectorUtil.h:18
Definition IntrRay3AxisAlignedBox3.h:22
static bool DoClipping(RealType &t0, RealType &t1, const TVector< RealType > &RayOrigin, const TVector< RealType > &RayDirection, const TAxisAlignedBox3< RealType > &Box, bool solid, int &quantity, TVector< RealType > &Point0, TVector< RealType > &Point1, EIntersectionType &intrType)
Definition IntrRay3AxisAlignedBox3.h:152
static bool FindIntersection(const TVector< RealType > &Origin, const TVector< RealType > &Direction, const TAxisAlignedBox3< RealType > &Box, RealType &RayParamOut)
Definition IntrRay3AxisAlignedBox3.h:122
static bool FindIntersection(const TRay< RealType > &Ray, const TAxisAlignedBox3< RealType > &Box, RealType &RayParamOut)
Definition IntrRay3AxisAlignedBox3.h:110
static bool Clip(RealType denom, RealType numer, RealType &t0, RealType &t1)
Definition IntrRay3AxisAlignedBox3.h:194
static bool TestIntersection(const TRay< RealType > &Ray, const TAxisAlignedBox3< RealType > &Box, RealType ExpandExtents=0)
Definition IntrRay3AxisAlignedBox3.h:36
TIntrRay3AxisAlignedBox3< double > FIntrRay3AxisAlignedBox3d
Definition IntrRay3AxisAlignedBox3.h:234
TIntrRay3AxisAlignedBox3< float > FIntrRay3AxisAlignedBox3f
Definition IntrRay3AxisAlignedBox3.h:233
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
Definition NumericLimits.h:41
Definition BoxTypes.h:247
Definition Ray.h:19
TVector< T > Origin
Definition Ray.h:24
TVector< T > Direction
Definition Ray.h:27
Definition Vector.h:51
T Z
Definition Vector.h:68
static TVector< T > Zero()
Definition Vector.h:112
T Y
Definition Vector.h:65
T X
Definition Vector.h:62