UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
DistLine2AxisAlignedBox2.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Port of gte's DistLine2AlignedBox2 to use GeometryCore data types
4
5#pragma once
6
8#include "LineTypes.h"
9#include "Math/Box2D.h"
10#include "VectorTypes.h"
11
12namespace UE
13{
14namespace Geometry
15{
16
17using namespace UE::Math;
18
22template <typename Real>
24{
25public:
26 // Input
29
30 // Results
34
38
39 Real Get()
40 {
41 return (Real)sqrt(ComputeResult());
42 }
44 {
45 return ComputeResult();
46 }
47
49 {
50 if (ResultDistanceSquared >= 0)
51 {
53 }
54
55 // Translate the line and box so that the box has center at the origin.
60
61 // The query computes 'result' relative to the box with center at the origin.
63
64 // Translate the closest points to the original coordinates.
67
70 }
71
72private:
73
74 // Compute the distance and closest point between a line and an aligned box whose center is the origin. The origin and direction
75 // are not const to allow for reflections that eliminate complicated sign logic in the queries themselves.
77 {
78 // Apply reflections so that the direction has nonnegative
79 // components.
80 Real const zero = static_cast<Real>(0);
82 for (int32_t i = 0; i < 2; ++i)
83 {
84 if (direction[i] < zero)
85 {
86 origin[i] = -origin[i];
87 direction[i] = -direction[i];
88 reflect[i] = true;
89 }
90 }
91
92 // Compute the line-box distance and closest points. The DoQueryND
93 // calls compute LineParameter and BoxClosest. The
94 // LineClosest can be computed after these calls.
95 if (direction[0] > zero)
96 {
97 if (direction[1] > zero)
98 {
99 // The direction signs are (+,+). If the line does not
100 // intersect the box, the only possible closest box points
101 // are K[0] = (-e0,e1) or K[1] = (e0,-e1). If the line
102 // intersects the box, the closest points are the same and
103 // chosen to be the intersection with box edge x0 = e0 or
104 // x1 = e1. For the remaining discussion, define K[2] =
105 // (e0,e1).
106 //
107 // Test where the candidate corners are relative to the
108 // line. If D = (d0,d1), then Perp(D) = (d1,-d0). The
109 // corner K[i] = P + t[i] * D + s[i] * Perp(D), where
110 // s[i] = Dot(K[i]-P,Perp(D))/|D|^2. K[0] is closest when
111 // s[0] >= 0 or K[1] is closest when s[1] <= 0. Otherwise,
112 // the line intersects the box. If s[2] >= 0, the common
113 // closest point is chosen to be (p0+(e1-p1)*d0/d1,e1). If
114 // s[2] < 0, the common closest point is chosen to be
115 // (e0,p1+(e0-p0)*d1/d0).
116 //
117 // It is sufficient to test the signs of Dot(K[i],Perp(D))
118 // and defer the division by |D|^2 until needed for
119 // computing the closest point.
121 }
122 else
123 {
124 // The direction signs are (+,0). The parameter is the
125 // value of t for which P + t * D = (e0, p1).
127 }
128 }
129 else
130 {
131 if (direction[1] > zero)
132 {
133 // The direction signs are (0,+). The parameter is the
134 // value of t for which P + t * D = (p0, e1).
136 }
137 else
138 {
139 // The direction signs are (0,0). The line is degenerate
140 // to a point (its origin). Clamp the origin to the box
141 // to obtain the closest point.
143 }
144 }
145
147
148 // Undo the reflections. The origin and direction are not consumed
149 // by the caller, so these do not need to be reflected. However,
150 // the closest points are consumed.
151 for (int32_t i = 0; i < 2; ++i)
152 {
153 if (reflect[i])
154 {
155 BoxClosest[i] = -BoxClosest[i];
156 LineClosest[i] = -LineClosest[i];
157 }
158 }
159 }
160
162 {
163 Real const zero = static_cast<Real>(0);
164 TVector2<Real> K0{ -extent[0], extent[1] };
167 if (K0dotPerpD >= zero)
168 {
171 BoxClosest = K0;
172 }
173 else
174 {
175 TVector2<Real> K1{ extent[0], -extent[1] };
176 delta = K1 - origin;
178 if (K1dotPerpD <= zero)
179 {
182 BoxClosest = K1;
183 }
184 else
185 {
186 TVector2<Real> K2{ extent[0], extent[1] };
187 delta = K2 - origin;
189 if (K2dotPerpD >= zero)
190 {
191 LineParameter = (extent[1] - origin[1]) / direction[1];
194 BoxClosest[1] = extent[1];
195 }
196 else
197 {
198 LineParameter = (extent[0] - origin[0]) / direction[0];
200 BoxClosest[0] = extent[0];
202 }
203 }
204 }
205 }
206
208 {
213 }
214
216 {
217 LineParameter = static_cast<Real>(0);
219 BoxClosest[0] = FMath::Clamp(origin[0], -extent[0], extent[0]);
220 BoxClosest[1] = FMath::Clamp(origin[1], -extent[1], extent[1]);
221 }
222};
223
226
227
228
229} // end namespace UE::Geometry
230} // end namespace UE
@ InPlace
Definition CoreMiscDefines.h:162
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition StaticArray.h:26
Definition DistLine2AxisAlignedBox2.h:24
TLine2< Real > Line
Definition DistLine2AxisAlignedBox2.h:27
Real ComputeResult()
Definition DistLine2AxisAlignedBox2.h:48
Real LineParameter
Definition DistLine2AxisAlignedBox2.h:33
Real Get()
Definition DistLine2AxisAlignedBox2.h:39
TBox2< Real > AxisAlignedBox
Definition DistLine2AxisAlignedBox2.h:28
Real GetSquared()
Definition DistLine2AxisAlignedBox2.h:43
TDistLine2AxisAlignedBox2(const TLine2< Real > LineIn, const TBox2< Real > &AxisAlignedBoxIn)
Definition DistLine2AxisAlignedBox2.h:35
TVector2< Real > BoxClosest
Definition DistLine2AxisAlignedBox2.h:32
Real ResultDistanceSquared
Definition DistLine2AxisAlignedBox2.h:31
TVector2< Real > LineClosest
Definition DistLine2AxisAlignedBox2.h:32
TDistLine2AxisAlignedBox2< float > FDistLine2AxisAlignedBox2f
Definition DistLine2AxisAlignedBox2.h:224
constexpr T DotPerp(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:19
T DistanceSquared(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:82
TDistLine2AxisAlignedBox2< double > FDistLine2AxisAlignedBox2d
Definition DistLine2AxisAlignedBox2.h:225
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)
Definition UnrealMathUtility.h:592
Definition LineTypes.h:23
TVector2< T > Origin
Definition LineTypes.h:25
TVector2< T > Direction
Definition LineTypes.h:28
Definition Box2D.h:31
TVector2< T > GetExtent() const
Definition Box2D.h:305
TVector2< T > GetCenter() const
Definition Box2D.h:272
Definition Vector2D.h:38