UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
IntrSegment2Segment2.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Port of WildMagic IntrSegment2Segment2
4
5#pragma once
6
7#include "SegmentTypes.h"
11#include "MathUtil.h"
12#include "VectorTypes.h"
13#include "VectorUtil.h" // for EIntersectionType
14
15namespace UE
16{
17namespace Geometry
18{
19
20using namespace UE::Math;
21
22// ported from WildMagic5
23//
24//
25// RealType IntervalThreshold
26// The intersection testing uses the center-extent form for line segments.
27// If you start with endpoints (Vector2<Real>) and create Segment2<Real>
28// objects, the conversion to center-extent form can contain small
29// numerical round-off errors. Testing for the intersection of two
30// segments that share an endpoint might lead to a failure due to the
31// round-off errors. To allow for this, you may specify a small positive
32// threshold that slightly enlarges the intervals for the segments. The
33// default value is zero.
34//
35// RealType DotThreshold
36// The computation for determining whether the linear components are
37// parallel might contain small floating-point round-off errors. The
38// default threshold is TMathUtil<RealType>::ZeroTolerance. If you set the value,
39// pass in a nonnegative number.
40//
41//
42// The intersection set: Let q = Quantity. The cases are
43//
44// q = 0: The segments do not intersect. Type is Empty
45//
46// q = 1: The segments intersect in a single point. Type is Point
47// Intersection point is Point0.
48//
49// q = 2: The segments are collinear and intersect in a segment.
50// Type is Segment. Points are Point0 and Point1
51
55template<typename RealType>
57{
58protected:
59 // inputs
62 RealType IntervalThreshold = 0;
64
65public:
66 // outputs
67 int Quantity = 0;
70 // these values are all on segment 1, unlike many other tests!!
72 TVector2<RealType> Point1; // only set if Quantity == 2, ie segment overlap
73
74 RealType Parameter0;
75 RealType Parameter1; // only set if Quantity == 2, ie segment overlap
76
77
78
83
84
86 {
87 return Segment1;
88 }
89
95
97 {
98 return Segment2;
99 }
100
106
107 RealType GetIntervalThreshold() const
108 {
109 return IntervalThreshold;
110 }
111
113 {
114 IntervalThreshold = FMath::Max(Value, (RealType)0);
116 }
117
118 RealType GetDotThreshold() const
119 {
120 return DotThreshold;
121 }
122
123 void SetDotThreshold(RealType Value)
124 {
125 DotThreshold = FMath::Max(Value, (RealType)0);
127 }
128
133
134
136 {
137 Find();
138 return *this;
139 }
140
141 // This is implemented in TSegment2::Intersects
142 // bool Test()
143
144 // Note: This implementation is identical to TSegment2::Intersects but also computes the intersection geometry
145 bool Find()
146 {
148 {
150 }
151
152 // Special handling of degenerate segments; by convention if Direction was too small to normalize, Extent and Direction will be zero
153 bool bIsPoint1 = Segment1.Extent == 0;
154 bool bIsPoint2 = Segment2.Extent == 0;
155 int IsPointCount = (int)bIsPoint1 + (int)bIsPoint2;
156 auto SetPointIntersection = [this](TVector2<RealType> Point, RealType Param) -> void
157 {
160 Quantity = 1;
161 Point0 = Point;
162 Parameter0 = Param;
163 };
164 auto SetNoIntersection = [this]() -> void
165 {
166 Quantity = 0;
169 };
170 if (IsPointCount == 2) // both degenerate -- check if they're within IntervalThreshold of each other
171 {
173 if (bIntersects)
174 {
176 }
177 else
178 {
180 }
181 return bIntersects;
182 }
183 else if (IsPointCount == 1) // one degenerate -- check if it's within IntervalThreshold of the non-degenerate segment
184 {
185 RealType DistSq;
186 RealType Param = 0;
188 if (bIsPoint1)
189 {
192 }
193 else // bIsPoint2
194 {
198 }
199 bool bIntersects = DistSq <= IntervalThreshold * IntervalThreshold;
200 if (bIntersects)
201 {
203 }
204 else
205 {
207 }
208 return bIntersects;
209 }
210
211 // If neither segment is zero, directions should always be normalized (will hold as long as segment is created/updated via the standard functions)
213
218
220 {
221 // Test whether the line-line intersection is on the segments.
222 if (FMath::Abs(s[0]) <= Segment1.Extent + IntervalThreshold
223 && FMath::Abs(s[1]) <= Segment2.Extent + IntervalThreshold)
224 {
226 }
227 else
228 {
230 }
231 }
232 else if (Type == EIntersectionType::Line)
233 {
234 // Compute the location of Segment2 endpoints relative to Segment1.
236 RealType t1 = Segment1.Direction.Dot(diff);
237 // Note: IntervalThreshold not used here; this is a bit inconsistent but simplifies the code
238 // If you add it, make sure that segments that intersect without it don't end up with too-wide intersection intervals
239 RealType tmin = t1 - Segment2.Extent;
240 RealType tmax = t1 + Segment2.Extent;
242 calc.Find();
243 Quantity = calc.NumIntersections;
244 if (Quantity == 2)
245 {
247 Parameter0 = calc.GetIntersection(0);
250 Parameter1 = calc.GetIntersection(1);
253 }
254 else if (Quantity == 1)
255 {
257 Parameter0 = calc.GetIntersection(0);
260 }
261 else
262 {
264 }
265 }
266 else
267 {
268 Quantity = 0;
269 }
270
273
274 // for debugging...
275 //SanityCheck();
276
278 }
279
280
281protected:
304};
305
308
309} // end namespace UE::Geometry
310} // end namespace UE
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define check(expr)
Definition AssertionMacros.h:314
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
static EIntersectionType Classify(const TVector2< RealType > &P0, const TVector2< RealType > &D0, const TVector2< RealType > &P1, const TVector2< RealType > &D1, RealType DotThreshold, RealType DistThreshold, TVector2< RealType > &s)
Definition IntrLine2Line2.h:150
Definition IntrSegment2Segment2.h:57
TIntrSegment2Segment2(const TSegment2< RealType > &Segment1In, const TSegment2< RealType > &Segment2In)
Definition IntrSegment2Segment2.h:79
void SanityCheck()
Definition IntrSegment2Segment2.h:282
bool Find()
Definition IntrSegment2Segment2.h:145
RealType DotThreshold
Definition IntrSegment2Segment2.h:63
const TSegment2< RealType > & GetSegment2() const
Definition IntrSegment2Segment2.h:96
void SetSegment1(const TSegment2< RealType > &Value)
Definition IntrSegment2Segment2.h:90
void SetSegment2(const TSegment2< RealType > &Value)
Definition IntrSegment2Segment2.h:101
TVector2< RealType > Point0
Definition IntrSegment2Segment2.h:71
TVector2< RealType > Point1
Definition IntrSegment2Segment2.h:72
bool IsSimpleIntersection() const
Definition IntrSegment2Segment2.h:129
TIntrSegment2Segment2 & Compute()
Definition IntrSegment2Segment2.h:135
EIntersectionType Type
Definition IntrSegment2Segment2.h:69
RealType GetIntervalThreshold() const
Definition IntrSegment2Segment2.h:107
EIntersectionResult Result
Definition IntrSegment2Segment2.h:68
TSegment2< RealType > Segment1
Definition IntrSegment2Segment2.h:60
TSegment2< RealType > Segment2
Definition IntrSegment2Segment2.h:61
RealType Parameter0
Definition IntrSegment2Segment2.h:74
void SetIntervalThreshold(RealType Value)
Definition IntrSegment2Segment2.h:112
RealType GetDotThreshold() const
Definition IntrSegment2Segment2.h:118
int Quantity
Definition IntrSegment2Segment2.h:67
void SetDotThreshold(RealType Value)
Definition IntrSegment2Segment2.h:123
RealType IntervalThreshold
Definition IntrSegment2Segment2.h:62
RealType Parameter1
Definition IntrSegment2Segment2.h:75
const TSegment2< RealType > & GetSegment1() const
Definition IntrSegment2Segment2.h:85
TIntrSegment2Segment2< float > FIntrSegment2Segment2f
Definition IntrSegment2Segment2.h:307
T DistanceSquared(const UE::Math::TVector2< T > &V1, const UE::Math::TVector2< T > &V2)
Definition VectorTypes.h:82
TIntrSegment2Segment2< double > FIntrSegment2Segment2d
Definition IntrSegment2Segment2.h:306
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 SegmentTypes.h:23
T Project(const TVector2< T > &QueryPoint) const
Definition SegmentTypes.h:184
T Extent
Definition SegmentTypes.h:30
TVector2< T > Direction
Definition SegmentTypes.h:28
TVector2< T > Center
Definition SegmentTypes.h:26
T DistanceSquared(const TVector2< T > &Point) const
Definition SegmentTypes.h:132
Definition Vector2D.h:38
T Dot(const TVector2< T > &V2) const
Definition Vector2D.h:1123
static TVector2< T > Zero()
Definition Vector2D.h:79