UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
IntrLine2Triangle2.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Port of WildMagic TIntrLine2Triangle2
4
5#pragma once
6
7#include "VectorTypes.h"
8#include "IntVectorTypes.h"
9#include "LineTypes.h"
10#include "TriangleTypes.h"
11#include "VectorUtil.h"
12
14
15namespace UE
16{
17namespace Geometry
18{
19
20using namespace UE::Math;
21
25template <typename Real>
27{
28protected:
29 // Input
32
33public:
34 // Output
35 int Quantity = 0;
38
41 double Param0;
42 double Param1;
43
45 {
46 return Line;
47 }
49 {
50 return Triangle;
51 }
62
67
73
74
76 {
77 Find();
78 return this;
79 }
80
81
82 bool Find()
83 {
85 {
87 }
88
89 // if either Line Direction is not a normalized vector,
90 // results are garbage, so fail query
91 if (IsNormalized(Line.Direction) == false)
92 {
95 return false;
96 }
97
98 TVector<Real> Dist;
100 int Positive = 0, Negative = 0, Zero = 0;
102
103 if (Positive == 3 || Negative == 3)
104 {
105 // No intersections.
106 Quantity = 0;
108 }
109 else
110 {
113
115 intr.Find();
116
117 Quantity = intr.NumIntersections;
118 if (Quantity == 2)
119 {
120 // Segment intersection.
122 Param0 = intr.GetIntersection(0);
124 Param1 = intr.GetIntersection(1);
126 }
127 else if (Quantity == 1)
128 {
129 // Point intersection.
131 Param0 = intr.GetIntersection(0);
133 }
134 else
135 {
136 // No intersections.
138 }
139 }
140
144 }
145
146
147
149 const TVector2<Real>& Origin, const TVector2<Real>& Direction, const TTriangle2<Real>& Tri,
150 TVector<Real>& Dist, FVector3i& Sign, int& Positive, int& Negative, int& Zero, Real Tolerance = TMathUtil<Real>::ZeroTolerance)
151 {
152 Positive = 0;
153 Negative = 0;
154 Zero = 0;
155 for (int i = 0; i < 3; ++i)
156 {
157 TVector2<Real> diff = Tri.V[i] - Origin;
158 Dist[i] = DotPerp(diff, Direction);
159 if (Dist[i] > Tolerance)
160 {
161 Sign[i] = 1;
162 ++Positive;
163 }
164 else if (Dist[i] < -Tolerance)
165 {
166 Sign[i] = -1;
167 ++Negative;
168 }
169 else
170 {
171 Dist[i] = 0.0;
172 Sign[i] = 0;
173 ++Zero;
174 }
175 }
176 }
177
178
179 static bool GetInterval(const TVector2<Real>& Origin, const TVector2<Real>& Direction, const TTriangle2<Real>& Tri,
180 const TVector<Real>& Dist, const FVector3i& Sign, TVector2<Real>& param)
181 {
182 // Project Triangle onto Line.
184 int i;
185 for (i = 0; i < 3; ++i)
186 {
187 TVector2<Real> diff = Tri.V[i] - Origin;
188 proj[i] = Direction.Dot(diff);
189 }
190
191 // Compute transverse intersections of Triangle edges with Line.
192 double numer, denom;
193 int i0, i1;
194 int quantity = 0;
195 for (i0 = 2, i1 = 0; i1 < 3; i0 = i1++)
196 {
197 if (Sign[i0] * Sign[i1] < 0)
198 {
199 numer = Dist[i0] * proj[i1] - Dist[i1] * proj[i0];
200 denom = Dist[i0] - Dist[i1];
201 param[quantity++] = numer / denom;
202 }
203 }
204
205 // Check for grazing contact.
206 if (quantity < 2)
207 {
208 for (i = 0; i < 3; i++)
209 {
210 if (Sign[i] == 0)
211 {
212 if (quantity == 2) // all sign==0 case
213 {
214 // Sort
215 if (param[0] > param[1])
216 {
217 Swap(param[0], param[1]);
218 }
219 // Expand range as needed with new param
220 double extraparam = proj[i];
221 if (extraparam < param[0])
222 {
223 param[0] = extraparam;
224 }
225 else if (extraparam > param[1])
226 {
227 param[1] = extraparam;
228 }
229 }
230 else
231 {
232 param[quantity++] = proj[i];
233 }
234 }
235 }
236 }
237
238
239 // (we expect GetInterval only to be called if there was some intersection)
240 if (!ensureMsgf(quantity > 0, TEXT("TIntrLine2Triangle2.GetInterval: need at least one intersection")))
241 {
242 return false;
243 }
244
245 // Sort.
246 if (quantity == 2)
247 {
248 if (param[0] > param[1])
249 {
250 double save = param[0];
251 param[0] = param[1];
252 param[1] = save;
253 }
254 }
255 else
256 {
257 param[1] = param[0];
258 }
259
260 return true;
261 }
262
263
264
265
266};
267
270
271} // end namespace UE::Geometry
272} // end namespace UE
#define ensureMsgf( InExpression, InFormat,...)
Definition AssertionMacros.h:465
#define TEXT(x)
Definition Platform.h:1272
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
EIntersectionResult
Definition VectorUtil.h:10
EIntersectionType
Definition VectorUtil.h:18
Definition MathUtil.h:150
Definition Intersector1.h:25
Definition IntrLine2Triangle2.h:27
TLine2< Real > GetLine() const
Definition IntrLine2Triangle2.h:44
bool Find()
Definition IntrLine2Triangle2.h:82
TTriangle2< Real > GetTriangle() const
Definition IntrLine2Triangle2.h:48
void SetLine(const TLine2< Real > &LineIn)
Definition IntrLine2Triangle2.h:52
TIntrLine2Triangle2()
Definition IntrLine2Triangle2.h:68
int Quantity
Definition IntrLine2Triangle2.h:35
double Param1
Definition IntrLine2Triangle2.h:42
TIntrLine2Triangle2(TLine2< Real > l, TTriangle2< Real > t)
Definition IntrLine2Triangle2.h:70
static void TriangleLineRelations(const TVector2< Real > &Origin, const TVector2< Real > &Direction, const TTriangle2< Real > &Tri, TVector< Real > &Dist, FVector3i &Sign, int &Positive, int &Negative, int &Zero, Real Tolerance=TMathUtil< Real >::ZeroTolerance)
Definition IntrLine2Triangle2.h:148
TTriangle2< Real > Triangle
Definition IntrLine2Triangle2.h:31
TVector2< Real > Point1
Definition IntrLine2Triangle2.h:40
double Param0
Definition IntrLine2Triangle2.h:41
TVector2< Real > Point0
Definition IntrLine2Triangle2.h:39
EIntersectionType Type
Definition IntrLine2Triangle2.h:37
TIntrLine2Triangle2 * Compute()
Definition IntrLine2Triangle2.h:75
EIntersectionResult Result
Definition IntrLine2Triangle2.h:36
static bool GetInterval(const TVector2< Real > &Origin, const TVector2< Real > &Direction, const TTriangle2< Real > &Tri, const TVector< Real > &Dist, const FVector3i &Sign, TVector2< Real > &param)
Definition IntrLine2Triangle2.h:179
bool IsSimpleIntersection()
Definition IntrLine2Triangle2.h:63
void SetTriangle(const TTriangle2< Real > &TriangleIn)
Definition IntrLine2Triangle2.h:57
TLine2< Real > Line
Definition IntrLine2Triangle2.h:30
constexpr T DotPerp(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:19
TIntrLine2Triangle2< float > FIntrLine2Triangle2f
Definition IntrLine2Triangle2.h:268
TIntrLine2Triangle2< double > FIntrLine2Triangle2d
Definition IntrLine2Triangle2.h:269
constexpr bool IsNormalized(const UE::Math::TVector2< T > &Vector, const T Tolerance=TMathUtil< T >::ZeroTolerance)
Definition VectorTypes.h:40
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
Definition IntVectorTypes.h:252
Definition LineTypes.h:23
TVector2< T > Origin
Definition LineTypes.h:25
TVector2< T > Direction
Definition LineTypes.h:28
Definition TriangleTypes.h:39
TVector2< RealType > V[3]
Definition TriangleTypes.h:40
Definition Vector2D.h:38
T Dot(const TVector2< T > &V2) const
Definition Vector2D.h:1123
Definition Vector.h:51