UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
DistPoint3Triangle3.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Port of gte's GteDistPointTriangle to use GeometryProcessing data types
4
5#pragma once
6
7#include "VectorTypes.h"
8#include "TriangleTypes.h"
9
10
11namespace UE
12{
13namespace Geometry
14{
15
16using namespace UE::Math;
17
21template <typename Real>
23{
24public:
25 // Input
28
29 // Results
31 TVector<Real> ClosestTrianglePoint; // do we need this just use Triangle.BarycentricPoint
32
33
39
40 Real Get()
41 {
42 return (Real)sqrt(ComputeResult());
43 }
45 {
46 return ComputeResult();
47 }
48
50 {
54 Real a00 = edge0.SquaredLength();
55 Real a01 = edge0.Dot(edge1);
56 Real a11 = edge1.SquaredLength();
57 Real b0 = -diff.Dot(edge0);
58 Real b1 = -diff.Dot(edge1);
59
60 Real f00 = b0;
61 Real f10 = b0 + a00;
62 Real f01 = b0 + a01;
63
64 TVector2<Real> p0, p1, p;
65 Real dt1, h0, h1;
66
67 // Compute the endpoints p0 and p1 of the segment. The segment is
68 // parameterized by L(z) = (1-z)*p0 + z*p1 for z in [0,1] and the
69 // directional derivative of half the quadratic on the segment is
70 // H(z) = Dot(p1-p0,gradient[Q](L(z))/2), where gradient[Q]/2 = (F,G).
71 // By design, F(L(z)) = 0 for cases (2), (4), (5), and (6). Cases (1) and
72 // (3) can correspond to no-intersection or intersection of F = 0 with the
73 // Triangle.
74 if (f00 >= (Real)0)
75 {
76 if (f01 >= (Real)0)
77 {
78 // (1) p0 = (0,0), p1 = (0,1), H(z) = G(L(z))
79 GetMinEdge02(a11, b1, p);
80 }
81 else
82 {
83 // (2) p0 = (0,t10), p1 = (t01,1-t01), H(z) = (t11 - t10)*G(L(z))
84 p0[0] = (Real)0;
85 p0[1] = f00 / (f00 - f01);
86 p1[0] = f01 / (f01 - f10);
87 p1[1] = (Real)1 - p1[0];
88 dt1 = p1[1] - p0[1];
89 h0 = dt1 * (a11 * p0[1] + b1);
90 if (h0 >= (Real)0)
91 {
92 GetMinEdge02(a11, b1, p);
93 }
94 else
95 {
96 h1 = dt1 * (a01 * p1[0] + a11 * p1[1] + b1);
97 if (h1 <= (Real)0)
98 {
99 GetMinEdge12(a01, a11, b1, f10, f01, p);
100 }
101 else
102 {
103 GetMinInterior(p0, h0, p1, h1, p);
104 }
105 }
106 }
107 }
108 else if (f01 <= (Real)0)
109 {
110 if (f10 <= (Real)0)
111 {
112 // (3) p0 = (1,0), p1 = (0,1), H(z) = G(L(z)) - F(L(z))
113 GetMinEdge12(a01, a11, b1, f10, f01, p);
114 }
115 else
116 {
117 // (4) p0 = (t00,0), p1 = (t01,1-t01), H(z) = t11*G(L(z))
118 p0[0] = f00 / (f00 - f10);
119 p0[1] = (Real)0;
120 p1[0] = f01 / (f01 - f10);
121 p1[1] = (Real)1 - p1[0];
122 h0 = p1[1] * (a01 * p0[0] + b1);
123 if (h0 >= (Real)0)
124 {
125 p = p0; // GetMinEdge01
126 }
127 else
128 {
129 h1 = p1[1] * (a01 * p1[0] + a11 * p1[1] + b1);
130 if (h1 <= (Real)0)
131 {
132 GetMinEdge12(a01, a11, b1, f10, f01, p);
133 }
134 else
135 {
136 GetMinInterior(p0, h0, p1, h1, p);
137 }
138 }
139 }
140 }
141 else if (f10 <= (Real)0)
142 {
143 // (5) p0 = (0,t10), p1 = (t01,1-t01), H(z) = (t11 - t10)*G(L(z))
144 p0[0] = (Real)0;
145 p0[1] = f00 / (f00 - f01);
146 p1[0] = f01 / (f01 - f10);
147 p1[1] = (Real)1 - p1[0];
148 dt1 = p1[1] - p0[1];
149 h0 = dt1 * (a11 * p0[1] + b1);
150 if (h0 >= (Real)0)
151 {
152 GetMinEdge02(a11, b1, p);
153 }
154 else
155 {
156 h1 = dt1 * (a01 * p1[0] + a11 * p1[1] + b1);
157 if (h1 <= (Real)0)
158 {
159 GetMinEdge12(a01, a11, b1, f10, f01, p);
160 }
161 else
162 {
163 GetMinInterior(p0, h0, p1, h1, p);
164 }
165 }
166 }
167 else
168 {
169 // (6) p0 = (t00,0), p1 = (0,t11), H(z) = t11*G(L(z))
170 p0[0] = f00 / (f00 - f10);
171 p0[1] = (Real)0;
172 p1[0] = (Real)0;
173 p1[1] = f00 / (f00 - f01);
174 h0 = p1[1] * (a01 * p0[0] + b1);
175 if (h0 >= (Real)0)
176 {
177 p = p0; // GetMinEdge01
178 }
179 else
180 {
181 h1 = p1[1] * (a11 * p1[1] + b1);
182 if (h1 <= (Real)0)
183 {
184 GetMinEdge02(a11, b1, p);
185 }
186 else
187 {
188 GetMinInterior(p0, h0, p1, h1, p);
189 }
190 }
191 }
192
193 TriangleBaryCoords = TVector<Real>((Real)1 - p[0] - p[1], p[0], p[1]);
194 ClosestTrianglePoint = Triangle.V[0] + p[0] * edge0 + p[1] * edge1;
196 }
197
198private:
199 void GetMinEdge02(Real const& a11, Real const& b1, TVector2<Real>& p) const
200 {
201 p[0] = (Real)0;
202 if (b1 >= (Real)0)
203 {
204 p[1] = (Real)0;
205 }
206 else if (a11 + b1 <= (Real)0)
207 {
208 p[1] = (Real)1;
209 }
210 else
211 {
212 p[1] = -b1 / a11;
213 }
214 }
215
216 void GetMinEdge12(
217 Real const& a01, Real const& a11, Real const& b1, Real const& f10,
218 Real const& f01, TVector2<Real>& p) const
219 {
220 Real h0 = a01 + b1 - f10;
221 if (h0 >= (Real)0)
222 {
223 p[1] = (Real)0;
224 }
225 else
226 {
227 Real h1 = a11 + b1 - f01;
228 if (h1 <= (Real)0)
229 {
230 p[1] = (Real)1;
231 }
232 else
233 {
234 p[1] = h0 / (h0 - h1);
235 }
236 }
237 p[0] = (Real)1 - p[1];
238 }
239
240 void GetMinInterior(
241 TVector2<Real> const& p0, Real const& h0, TVector2<Real> const& p1,
242 Real const& h1, TVector2<Real>& p) const
243 {
244 Real z = h0 / (h0 - h1);
245 p = ((Real)1 - z) * p0 + z * p1;
246 }
247
248};
249
252
253} // end namespace UE::Geometry
254} // end namespace UE
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
Definition DistPoint3Triangle3.h:23
TVector< Real > TriangleBaryCoords
Definition DistPoint3Triangle3.h:30
TDistPoint3Triangle3(const TVector< Real > &PointIn, const TTriangle3< Real > &TriangleIn)
Definition DistPoint3Triangle3.h:34
TVector< Real > Point
Definition DistPoint3Triangle3.h:26
TTriangle3< Real > Triangle
Definition DistPoint3Triangle3.h:27
Real Get()
Definition DistPoint3Triangle3.h:40
Real GetSquared()
Definition DistPoint3Triangle3.h:44
Real ComputeResult()
Definition DistPoint3Triangle3.h:49
TVector< Real > ClosestTrianglePoint
Definition DistPoint3Triangle3.h:31
T DistanceSquared(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:82
TDistPoint3Triangle3< double > FDistPoint3Triangle3d
Definition DistPoint3Triangle3.h:251
TDistPoint3Triangle3< float > FDistPoint3Triangle3f
Definition DistPoint3Triangle3.h:250
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
Definition TriangleTypes.h:263
TVector< RealType > V[3]
Definition TriangleTypes.h:264
Definition Vector2D.h:38
Definition Vector.h:51
T SquaredLength() const
Definition Vector.h:1734