UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
IntersectionSegmentTool.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Core/Types.h"
5#include "Math/Boundary.h"
6#include "Math/Geometry.h"
7#include "UI/Visu.h"
8
9namespace UE::CADKernel
10{
11class FGrid;
12class FIsoNode;
13class FIsoInnerNode;
14class FIsoSegment;
15
23
24namespace IntersectionToolBase
25{
27{
29
34
39 double AxisMin;
40 double AxisMax;
41
45 FSegment(const double Tolerance, const FVector2d& StartPoint, const FVector2d& EndPoint)
46 : Segment2D(StartPoint, EndPoint)
47 , Boundary(StartPoint, EndPoint, Tolerance)
48 {
51 }
52
53 virtual ~FSegment() = default;
54
55 virtual bool IsValid() const = 0;
56
57 virtual const FIsoNode* GetFirstNode() const = 0;
58 virtual const FIsoNode* GetSecondNode() const = 0;
59
60 virtual const FIsoSegment* GetIsoSegment() const
61 {
62 return nullptr;
63 }
64
65 static EConnectionType IsSuperimposed(const FSegment2D& SegmentAB, const FSegment2D& SegmentCD, bool bSameOrientation)
66 {
67 const FVector2d AB = SegmentAB.GetVector().GetSafeNormal();
68 const FVector2d CD = SegmentCD.GetVector().GetSafeNormal();
69 const double ParallelCoef = AB ^ CD;
71 {
72 const double OrientationCoef = AB | CD;
74 {
76 }
77 }
79 };
80
82 {
83 if (GetFirstNode() == StartNode)
84 {
85 constexpr bool bSameOrientation = true;
87 }
88 if (GetSecondNode() == StartNode)
89 {
90 constexpr bool bNotSameOrientation = false;
92 }
93
95 }
96
98 {
100 }
101
103 {
104 if (GetFirstNode() == StartNode)
105 {
106 if (GetSecondNode() == EndNode)
107 {
109 }
110 return IsSuperimposed(Segment2D, InSegment, true);
111 }
112
113 if (GetSecondNode() == EndNode)
114 {
115 return IsSuperimposed(Segment2D, InSegment, true);
116 }
117
118 if (GetFirstNode() == EndNode)
119 {
120 if (GetSecondNode() == StartNode)
121 {
123 }
124 return IsSuperimposed(Segment2D, InSegment, false);
125 }
126
127 if (GetSecondNode() == StartNode)
128 {
129 return IsSuperimposed(Segment2D, InSegment, false);
130 }
131
133 }
134
135 bool IsFullyBefore(const FSegment& Segment) const
136 {
137 return AxisMax < Segment.AxisMin;
138 }
139
140 bool IsFullyAfter(const FSegment& Segment) const
141 {
142 return Segment.AxisMax < AxisMin;
143 }
144
146 {
148 {
149 return false;
150 }
151
153 {
154 return false;
155 }
156 return true;
157 }
158
159 virtual bool DoesItIntersect(const FSegment& Segment) const
160 {
161 if (!CouldItIntersect(Segment.Boundary))
162 {
163 return false;
164 }
165
166 return DoIntersect(Segment2D, Segment.Segment2D);
167 }
168
169 bool IsParallelWith(const FSegment& Segment) const
170 {
171 return AreParallel(Segment2D, Segment.Segment2D);
172 }
173};
174}
175
176namespace IntersectionSegmentTool
177{
178
180{
182
183 FSegment(const FGrid& Grid, const double Tolerance, const FIsoSegment& InSegment);
184 FSegment(const FGrid& Grid, const double Tolerance, const FIsoNode& StartNode, const FIsoNode& EndNode);
185 FSegment(const FGrid& Grid, const double Tolerance, const FIsoNode& StartNode, const FVector2d& EndPoint);
186 FSegment(const FGrid& Grid, const double Tolerance, const FVector2d& StartPoint, const FVector2d& EndPoint)
187 : IntersectionToolBase::FSegment(Tolerance, StartPoint, EndPoint)
188 , IsoSegment(nullptr)
189 {
190 }
191
192 virtual bool IsValid() const override;
193
194 virtual const FIsoNode* GetFirstNode() const override;
195 virtual const FIsoNode* GetSecondNode() const override;
196
197 virtual const FIsoSegment* GetIsoSegment() const override
198 {
199 return IsoSegment;
200 }
201
202};
203
204}
205
206template<typename SegmentType>
208{
209protected:
210 const FGrid& Grid;
211
214
215 const double Tolerance;
216
217public:
224
225 virtual ~TIntersectionSegmentTool() = default;
226
228 {
230 bSegmentsAreSorted = false;
231 }
232
237
244
250
252 {
253 return Segments.Num();
254 }
255
259 void Sort()
260 {
261 Algo::Sort(Segments, [](const SegmentType& Segment1, const SegmentType& Segment2) { return Segment1.AxisMin < Segment2.AxisMin; });
262 bSegmentsAreSorted = true;
263 }
264
265 template<typename ExtremityType1, typename ExtremityType2>
267 {
268 using namespace IntersectionSegmentTool;
269 const SegmentType InSegment(Grid, Tolerance, *StartExtremity, *EndExtremity);
270
271 for (const SegmentType& Segment : Segments)
272 {
273 if (!Segment.IsValid())
274 {
275 continue;
276 }
277
279 {
280 if (Segment.IsFullyBefore(InSegment))
281 {
282 continue;
283 }
284
285 if (Segment.IsFullyAfter(InSegment))
286 {
287 break;
288 }
289 }
290
291 switch (Segment.DoesItStartFromAndSuperimposed(StartExtremity, EndExtremity, InSegment.Segment2D))
292 {
295 continue;
297 return &Segment;
299 default:
300 break;
301 }
302
303 if (Segment.DoesItIntersect(InSegment))
304 {
305 return &Segment;
306 }
307 }
308
309 return nullptr;
310 }
311
312 template<typename ExtremityType1, typename ExtremityType2>
314 {
317 {
318 OutIntersectedSegments->Reset(10);
319 }
320
322 for (const SegmentType& Segment : Segments)
323 {
324 if (!Segment.IsValid())
325 {
326 continue;
327 }
328
330 {
331 if (Segment.IsFullyBefore(InSegment))
332 {
333 continue;
334 }
335
336 if (Segment.IsFullyAfter(InSegment))
337 {
338 break;
339 }
340 }
341
342 switch (Segment.DoesItStartFromAndSuperimposed(StartExtremity, EndExtremity, InSegment.Segment2D))
343 {
346 continue;
347
351 {
352 OutIntersectedSegments->Add(Segment.GetIsoSegment());
353 }
354 continue;
355
357 default:
358 break;
359 }
360
361 if (Segment.DoesItIntersect(InSegment))
362 {
365 {
366 OutIntersectedSegments->Add(Segment.GetIsoSegment());
367 }
368 }
369 }
370
371 return IntersectionCount;
372 }
373
374#ifdef CADKERNEL_DEBUG
375 virtual void Display(bool bDisplay, const TCHAR* Message, EVisuProperty Property = EVisuProperty::BlueCurve) const
376 {
377 if (!bDisplay)
378 {
379 return;
380 }
381
382 int32 Index = 0;
383 Open3DDebugSession(Message);
384 for (const SegmentType& Segment : Segments)
385 {
386 DisplaySegment(Segment.Segment2D[0] * DisplayScale, Segment.Segment2D[1] * DisplayScale, Index++, Property);
387 }
389 }
390#endif
391
392};
393
394class FIntersectionSegmentTool : public TIntersectionSegmentTool<IntersectionSegmentTool::FSegment>
395{
396public:
398 : TIntersectionSegmentTool<IntersectionSegmentTool::FSegment>(InGrid, Tolerance)
399 {
400 }
401
403 {
405 {
406 return SegmentIter.IsoSegment == Segment;
407 });
408
409 if (SegmentIndex != INDEX_NONE)
410 {
411 Segments.RemoveAt(SegmentIndex);
412 Segments.EmplaceAt(SegmentIndex, Grid, Tolerance, *Segment);
413 bSegmentsAreSorted = false;
414 return true;
415 }
416 return false;
417 }
418
429
439
445
446 void AddSegment(const FVector2d& StartPoint, const FVector2d& EndPoint)
447 {
448 bSegmentsAreSorted = false;
449 Segments.Emplace(Grid, Tolerance, StartPoint, EndPoint);
450 }
451
456 {
458 {
459 return SegmentIter.IsoSegment == Segment;
460 });
461
462 if (SegmentIndex != INDEX_NONE)
463 {
464 Segments.RemoveAt(SegmentIndex);
465 return true;
466 }
467 return false;
468 }
469
472
474 {
475 return FindIntersectingSegment(Segment) != nullptr;
476 }
477
481 bool DoesIntersect(const FVector2d& StartPoint, const FVector2d& EndPoint) const
482 {
484 }
485
490 bool DoesIntersect(const FIsoNode& StartNode, const FVector2d& EndPoint) const
491 {
493 }
494
495 bool DoesIntersect(const FIsoNode* StartNode, const FIsoNode* EndNode) const
496 {
498 }
499
500 bool DoesIntersect(const FIsoNode& StartNode, const FIsoNode& EndNode) const
501 {
503 }
504
508 const FIsoSegment* FindIntersectingSegment(const FIsoNode& StartNode, const FIsoNode& EndNode) const
509 {
512 {
513 return IntersectingSegment->IsoSegment;
514 }
515 return nullptr;
516 }
517
518 const FIsoSegment* FindIntersectingSegment(const FIsoNode* StartNode, const FIsoNode* EndNode) const
519 {
522 {
523 return IntersectingSegment->IsoSegment;
524 }
525 return nullptr;
526 }
527
532
537
538 int32 CountIntersections(const FIsoNode* StartNode, const FIsoNode* EndNode) const
539 {
541 }
542};
543
544} // namespace UE::CADKernel
545
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define ensureCADKernel(InExpression)
Definition Types.h:115
DIRECTLINK_API Display
Definition DirectLinkLog.h:8
#define DOUBLE_KINDA_SMALL_NUMBER
Definition UnrealMathUtility.h:73
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
void RemoveAt(SizeType Index, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2083
void Reset(SizeType NewSize=0)
Definition Array.h:2246
UE_FORCEINLINE_HINT SizeType Emplace(ArgsType &&... Args)
Definition Array.h:2561
void SetNum(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2308
SizeType IndexOfByPredicate(Predicate Pred) const
Definition Array.h:1423
UE_FORCEINLINE_HINT void EmplaceAt(SizeType Index, ArgsType &&... Args)
Definition Array.h:2665
UE_FORCEINLINE_HINT void Reserve(SizeType Number)
Definition Array.h:3016
Definition Grid.h:46
Definition IntersectionSegmentTool.h:395
FIntersectionSegmentTool(const FGrid &InGrid, const double Tolerance)
Definition IntersectionSegmentTool.h:397
bool FindIntersectingSegments(const FIsoNode &StartNode, const FIsoNode &EndNode, TArray< const FIsoSegment * > &OutIntersections) const
Definition IntersectionSegmentTool.h:533
bool Remove(const FIsoSegment *Segment)
Definition IntersectionSegmentTool.h:455
bool DoesIntersect(const FVector2d &StartPoint, const FVector2d &EndPoint) const
Definition IntersectionSegmentTool.h:481
const FIsoSegment * FindIntersectingSegment(const FIsoNode &StartNode, const FIsoNode &EndNode) const
Definition IntersectionSegmentTool.h:508
bool DoesIntersect(const FIsoNode &StartNode, const FIsoNode &EndNode) const
Definition IntersectionSegmentTool.h:500
const FIsoSegment * FindIntersectingSegment(const FIsoNode *StartNode, const FIsoNode *EndNode) const
Definition IntersectionSegmentTool.h:518
bool DoesIntersect(const FIsoNode *StartNode, const FIsoNode *EndNode) const
Definition IntersectionSegmentTool.h:495
bool Update(const FIsoSegment *Segment)
Definition IntersectionSegmentTool.h:402
bool FindIntersectingSegments(const FIsoNode *StartNode, const FIsoNode *EndNode, TArray< const FIsoSegment * > &OutIntersections) const
Definition IntersectionSegmentTool.h:528
bool DoesIntersect(const FIsoSegment &Segment) const
Definition IntersectionSegmentTool.h:473
void AddSegments(const TArray< FIsoSegment * > &InNewSegments)
Definition IntersectionSegmentTool.h:430
void AddSegment(const FIsoSegment &Segment)
Definition IntersectionSegmentTool.h:440
void AddSegments(FIsoSegment **InNewSegments, int32 Count)
Definition IntersectionSegmentTool.h:419
bool DoesIntersect(const FIsoNode &StartNode, const FVector2d &EndPoint) const
Definition IntersectionSegmentTool.h:490
void AddSegment(const FVector2d &StartPoint, const FVector2d &EndPoint)
Definition IntersectionSegmentTool.h:446
int32 CountIntersections(const FIsoNode *StartNode, const FIsoNode *EndNode) const
Definition IntersectionSegmentTool.h:538
const FIsoSegment * FindIntersectingSegment(const FIsoSegment &Segment) const
Definition IntersectionSegmentTool.cpp:14
Definition IsoNode.h:62
Definition IsoSegment.h:52
Definition Boundary.h:248
Definition IntersectionSegmentTool.h:208
void RemoveLast()
Definition IntersectionSegmentTool.h:238
void Sort()
Definition IntersectionSegmentTool.h:259
int32 Count()
Definition IntersectionSegmentTool.h:251
void Empty(int32 InMaxNum)
Definition IntersectionSegmentTool.h:227
const FGrid & Grid
Definition IntersectionSegmentTool.h:210
TArray< SegmentType > Segments
Definition IntersectionSegmentTool.h:212
const SegmentType * FindIntersectingSegment(const ExtremityType1 *StartExtremity, const ExtremityType2 *EndExtremity) const
Definition IntersectionSegmentTool.h:266
int32 FindIntersectingSegments(const ExtremityType1 *StartExtremity, const ExtremityType2 *EndExtremity, TArray< const FIsoSegment * > *OutIntersectedSegments) const
Definition IntersectionSegmentTool.h:313
void SetCount(int32 NewCount)
Definition IntersectionSegmentTool.h:245
const double Tolerance
Definition IntersectionSegmentTool.h:215
void Reserve(int32 InMaxNum)
Definition IntersectionSegmentTool.h:233
TIntersectionSegmentTool(const FGrid &InGrid, const double InTolerance)
Definition IntersectionSegmentTool.h:218
bool bSegmentsAreSorted
Definition IntersectionSegmentTool.h:213
UE_REWRITE void Sort(RangeType &&Range)
Definition Sort.h:16
Definition CADEntity.cpp:23
bool DoIntersect(const FSegment2D &SegmentAB, const FSegment2D &SegmentCD, TFunction< bool(double, double, double, double)> DoCoincidentSegmentsIntersect, const double Min, const double Max)
Definition Geometry.cpp:167
void DisplaySegment(const FVector &Point1, const FVector &Point2, FIdent Ident, EVisuProperty Property)
Definition Display.cpp:1268
void Open3DDebugSession(bool bIsDisplayed, FString name, const TArray< FIdent > &idList=TArray< FIdent >())
Definition Display.h:56
void Close3DDebugSession(bool bIsDisplayed=true)
Definition Display.h:58
EConnectionType
Definition IntersectionSegmentTool.h:17
@ IsoV
Definition GeoEnum.h:68
@ IsoU
Definition GeoEnum.h:67
bool AreParallel(const FSegment2D &SegmentAB, const FSegment2D &SegmentCD)
Definition Geometry.h:528
EVisuProperty
Definition Visu.h:15
@ BlueCurve
Definition Visu.h:31
@ false
Definition radaudio_common.h:23
U16 Index
Definition radfft.cpp:71
static UE_FORCEINLINE_HINT bool IsNearlyZero(float Value, float ErrorTolerance=UE_SMALL_NUMBER)
Definition UnrealMathUtility.h:407
Definition IntersectionSegmentTool.h:180
virtual const FIsoNode * GetFirstNode() const override
Definition IntersectionSegmentTool.cpp:50
virtual const FIsoSegment * GetIsoSegment() const override
Definition IntersectionSegmentTool.h:197
virtual const FIsoNode * GetSecondNode() const override
Definition IntersectionSegmentTool.cpp:55
FSegment(const FGrid &Grid, const double Tolerance, const FVector2d &StartPoint, const FVector2d &EndPoint)
Definition IntersectionSegmentTool.h:186
virtual bool IsValid() const override
Definition IntersectionSegmentTool.cpp:44
const FIsoSegment * IsoSegment
Definition IntersectionSegmentTool.h:181
Definition IntersectionSegmentTool.h:27
virtual const FIsoSegment * GetIsoSegment() const
Definition IntersectionSegmentTool.h:60
const FSurfacicBoundary Boundary
Definition IntersectionSegmentTool.h:33
FSegment(const double Tolerance, const FVector2d &StartPoint, const FVector2d &EndPoint)
Definition IntersectionSegmentTool.h:45
EConnectionType DoesItStartFromAndSuperimposed(const FVector2d *StartPoint, const FVector2d *EndPoint, const FSegment2D &InSegment) const
Definition IntersectionSegmentTool.h:97
bool CouldItIntersect(const FSurfacicBoundary &SegmentBoundary) const
Definition IntersectionSegmentTool.h:145
const FSegment2D Segment2D
Definition IntersectionSegmentTool.h:28
bool IsFullyBefore(const FSegment &Segment) const
Definition IntersectionSegmentTool.h:135
bool IsParallelWith(const FSegment &Segment) const
Definition IntersectionSegmentTool.h:169
EConnectionType DoesItStartFromAndSuperimposed(const FIsoNode *StartNode, const FIsoNode *EndNode, const FSegment2D &InSegment) const
Definition IntersectionSegmentTool.h:102
static EConnectionType IsSuperimposed(const FSegment2D &SegmentAB, const FSegment2D &SegmentCD, bool bSameOrientation)
Definition IntersectionSegmentTool.h:65
double AxisMin
Definition IntersectionSegmentTool.h:39
virtual const FIsoNode * GetSecondNode() const =0
EConnectionType DoesItStartFromAndSuperimposed(const FIsoNode *StartNode, const FVector2d *EndPoint, const FSegment2D &InSegment) const
Definition IntersectionSegmentTool.h:81
bool IsFullyAfter(const FSegment &Segment) const
Definition IntersectionSegmentTool.h:140
virtual const FIsoNode * GetFirstNode() const =0
double AxisMax
Definition IntersectionSegmentTool.h:40
virtual bool DoesItIntersect(const FSegment &Segment) const
Definition IntersectionSegmentTool.h:159
Definition Geometry.h:250
PointType GetVector() const
Definition Geometry.h:280