UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Geometry.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 "Geo/GeoEnum.h"
6#include "Math/Aabb.h"
7#include "Math/MathConst.h"
8#include "Math/MatrixH.h"
9#include "Math/Point.h"
10
11namespace UE::CADKernel
12{
21
22namespace IntersectionTool
23{
24CADKERNEL_API void SetTolerance(const double Tolerance);
25}
26
27
33{
36
37 // D = 2(BuCv - BvCu)
38 double D = 2. * Segment_P0_P1 ^ Segment_P0_P2;
40 {
42 }
43
44 // CenterU = 1/D * (Cv.|B|.|B| - By.|C|.|C|) = 1/D * CBv ^ SquareNorms
45 // CenterV = 1/D * (Bu.|B|.|B| - Cu.|C|.|C|) = -1/D * SquareNorms ^ CBu
47 double SquareNormC = Segment_P0_P2.SquaredLength();
50
52}
53
54CADKERNEL_API inline FVector ComputeCircumCircleCenter(const FVector& Point0, const FVector& Point1, const FVector& Point2)
55{
56 FVector Test = Point1 - Point0;
57 FVector AxisZ = (Point1 - Point0) ^ (Point2 - Point0);
59
60 FVector AxisX = Point1 - Point0;
63
64 FMatrixH Matrix(Point0, AxisX, AxisY, AxisZ);
65 FMatrixH MatrixInverse = Matrix;
66 MatrixInverse.Inverse();
67
68 FVector2d D2Point1 = FVectorUtil::FromVector(MatrixInverse * Point1);
69 FVector2d D2Point2 = FVectorUtil::FromVector(MatrixInverse * Point2);
70
71 double D = 2. * D2Point1 ^ D2Point2;
73 {
75 }
76
78 double SquareNormC = D2Point2.SquaredLength();
79
80 double CenterU = (SquareNormB * D2Point2.Y - SquareNormC * D2Point1.Y) / D;
81 double CenterV = (SquareNormC * D2Point1.X - SquareNormB * D2Point2.X) / D;
82
84 return Matrix * Center2D;
85}
86
87
110
115template<class PointType>
116inline PointType ProjectPointOnSegment(const PointType& Point, const PointType& InSegmentA, const PointType& InSegmentB, double& OutCoordinate, bool bRestrictCoodinateToInside = true)
117{
118 PointType Segment = InSegmentB - InSegmentA;
119
120 double SquaredLength = Segment | Segment;
121
122 if (SquaredLength <= 0.0)
123 {
124 OutCoordinate = 0.0;
125 return InSegmentA;
126 }
127 else
128 {
129 PointType APoint = Point - InSegmentA;
130 OutCoordinate = (APoint | Segment) / SquaredLength;
131
133 {
134 if (OutCoordinate < 0.0)
135 {
136 OutCoordinate = 0.0;
137 return InSegmentA;
138 }
139
140 if (OutCoordinate > 1.0)
141 {
142 OutCoordinate = 1.0;
143 return InSegmentB;
144 }
145 }
146
149 return ProjectedPoint;
150 }
151}
152
153inline FVector ProjectPointOnPlane(const FVector& Point, const FVector& Origin, const FVector& InNormal, double& OutDistance)
154{
157 Normal.Normalize();
158
159 FVector OP = Point - Origin;
160 OutDistance = OP | Normal;
161
162 return Point - (Normal * OutDistance);
163}
164
168template<class PointType>
169inline PointType PointOnSegment(const PointType& InSegmentA, const PointType& InSegmentB, double InCoordinate)
170{
171 PointType Segment = InSegmentB - InSegmentA;
173}
174
179template<class PointType>
180inline double DistanceOfPointToSegment(const PointType& Point, const PointType& SegmentPoint1, const PointType& SegmentPoint2)
181{
182 double Coordinate;
183 return PointType::Distance(ProjectPointOnSegment<PointType>(Point, SegmentPoint1, SegmentPoint2, Coordinate, /*bRestrictCoodinateToInside*/ true), Point);
184}
185
190template<class PointType>
191inline double SquareDistanceOfPointToSegment(const PointType& Point, const PointType& SegmentPoint1, const PointType& SegmentPoint2)
192{
193 double Coordinate;
194 return FVector2d::DistSquared(ProjectPointOnSegment<PointType>(Point, SegmentPoint1, SegmentPoint2, Coordinate, /*bRestrictCoodinateToInside*/ true), Point);
195}
196
200template<class PointType>
201inline double DistanceOfPointToLine(const PointType& Point, const PointType& LinePoint1, const PointType& LineDirection)
202{
203 double Coordinate;
204 return ProjectPointOnSegment<PointType>(Point, LinePoint1, LinePoint1 + LineDirection, Coordinate, /*bRestrictCoodinateToInside*/ false).Distance(Point);
205}
206
207CADKERNEL_API double ComputeCurvature(const FVector& Gradient, const FVector& Laplacian);
208CADKERNEL_API double ComputeCurvature(const FVector& normal, const FVector& Gradient, const FVector& Laplacian);
209
213template<class PointType>
214inline double CoordinateOfProjectedPointOnSegment(const PointType& Point, const PointType& InSegmentA, const PointType& InSegmentB, bool bRestrictCoodinateToInside = true)
215{
216 PointType Segment = InSegmentB - InSegmentA;
217
218 double SquaredLength = Segment | Segment;
219
220 if (SquaredLength <= 0.0)
221 {
222 return 0.0;
223 }
224 else
225 {
226 PointType APoint = Point - InSegmentA;
227 double Coordinate = (APoint | Segment) / SquaredLength;
228
230 {
231 if (Coordinate < 0.0)
232 {
233 return 0.0;
234 }
235
236 if (Coordinate > 1.0)
237 {
238 return 1.0;
239 }
240 }
241
242 return Coordinate;
243 }
244}
245
247
248template<class PointType>
250{
251 const PointType& Point0;
252 const PointType& Point1;
253 const PointType Dummy = PointType::ZeroVector;
254
255 TSegment(const PointType& InPoint0, const PointType& InPoint1)
256 : Point0(InPoint0)
257 , Point1(InPoint1)
258 {
259 }
260
261 constexpr const PointType& operator[](int32 Index) const
262 {
264 switch (Index)
265 {
266 case 0:
267 return Point0;
268 case 1:
269 return Point1;
270 default:
271 return Dummy;
272 }
273 }
274
275 double SquaredLength() const
276 {
277 return FVector2d::DistSquared(Point0, Point1);
278 }
279
280 PointType GetVector() const
281 {
282 return Point1 - Point0;
283 }
284};
285
288
289template<class PointType>
291{
292 const PointType& Point0;
293 const PointType& Point1;
294 const PointType& Point2;
295
296 TTriangle(const PointType& InPoint0, const PointType& InPoint1, const PointType& InPoint2)
297 : Point0(InPoint0)
298 , Point1(InPoint1)
299 , Point2(InPoint2)
300 {
301 }
302
303 constexpr const PointType& operator[](int32 Index) const
304 {
306 switch (Index)
307 {
308 case 0:
309 return Point0;
310 case 1:
311 return Point1;
312 case 2:
313 return Point2;
314 default:
315 return PointType::ZeroVector;
316 }
317 }
318
319 virtual inline PointType ProjectPoint(const PointType& InPoint, FVector2d& OutCoordinate)
320 {
321 PointType Segment01 = Point1 - Point0;
322 PointType Segment02 = Point2 - Point0;
323 double SquareLength01 = Segment01.SquaredLength();
324 double SquareLength02 = Segment02.SquaredLength();
325 double Seg01Seg02 = Segment01 | Segment02;
327
329 // If the 3 points are aligned
331 {
332 double MaxLength = SquareLength01;
334 if (SquareLength02 > MaxLength)
335 {
336 MaxLength = SquareLength02;
338 }
339
340 PointType Segment12 = Point2 - Point1;
341 if (Segment12.SquaredLength() > MaxLength)
342 {
344 }
345 }
346 else
347 {
348 // Resolve
349 PointType Segment1Point = InPoint - Point0;
352
355
356 // tester la solution pour choisir parmi 4 possibilites
357 if (OutCoordinate.X < 0.0)
358 {
359 // the project point is on the segment 02
361 }
362 else if (OutCoordinate.Y < 0.0)
363 {
364 // the project point is on the segment 01
366 }
367 else if ((OutCoordinate.X + OutCoordinate.Y) > 1.0)
368 {
369 // the project point is on the segment 12
371 }
372 else {
373 // the project point is inside the Segment
376 PointType ProjectedPoint = Segment01 + Segment02;
378 return ProjectedPoint;
379 }
380 }
381
382 // projects the point on the nearest side
383 PointType ProjectedPoint;
384 double SegmentCoordinate;
385 switch (SideIndex)
386 {
387 case Side01:
390 OutCoordinate.Y = 0.0;
391 break;
392 case Side20:
394 OutCoordinate.X = 0.0;
396 break;
397 case Side12:
401 break;
402 }
403 return ProjectedPoint;
404 }
405
406 virtual PointType CircumCircleCenter() const
407 {
408 return ComputeCircumCircleCenter(Point0, Point1, Point2);
409 }
410};
411
412struct CADKERNEL_API FTriangle : public TTriangle<FVector>
413{
418
419 virtual FVector ComputeNormal() const
420 {
421 FVector Normal = (Point1 - Point0) ^ (Point2 - Point0);
423 return Normal;
424 }
425};
426
427struct CADKERNEL_API FTriangle2D : public TTriangle<FVector2d>
428{
433
435 {
436 return ComputeCircumCircleCenterAndSquareRadius(this->Point0, this->Point1, this->Point2, SquareRadius);
437 }
438};
439
444{
445 const FVector2d AB = SegmentAB[1] - SegmentAB[0];
446 const FVector2d DC = SegmentCD[0] - SegmentCD[1];
447 const FVector2d AC = SegmentCD[0] - SegmentAB[0];
448
449 double ParallelCoef = DC ^ AB;
451 {
452 const double SquareAB = AB | AB;
453 double CCoordinate = (AB | AC) / SquareAB;
454
455 const FVector2d AD = SegmentCD[1] - SegmentAB[0];
456 double DCoordinate = (AB | AD) / SquareAB;
457
459 {
461 {
463 return (SegmentCD[0] + SegmentCD[1]) * 0.5;
464 }
465
468 return SegmentCD[0];
469 }
471 {
474 return SegmentCD[1];
475 }
476 else
477 {
479 return (SegmentAB[0] + SegmentAB[1]) * 0.5;
480 }
481 }
482
485
486 return SegmentAB[0] + AB * OutABIntersectionCoordinate;
487}
488
492inline FVector2d FindIntersectionOfSegments2D(const FSegment2D& SegmentAB, const FSegment2D& SegmentCD)
493{
495 return FindIntersectionOfSegments2D(SegmentAB, SegmentCD, ABIntersectionCoordinate);
496}
497
502{
503 constexpr const double Min = -DOUBLE_SMALL_NUMBER;
504 constexpr const double Max = 1. + DOUBLE_SMALL_NUMBER;
505
506 const FVector2d AB = LineAB[1] - LineAB[0];
507 const FVector2d DC = LineCD[0] - LineCD[1];
508 const FVector2d AC = LineCD[0] - LineAB[0];
509
510 double ParallelCoef = DC ^ AB;
512 {
513 return false;
514 }
515
518 return true;
519}
520
525CADKERNEL_API bool DoIntersect(const FSegment2D& SegmentAB, const FSegment2D& SegmentCD);
526CADKERNEL_API bool DoIntersectInside(const FSegment2D& SegmentAB, const FSegment2D& SegmentCD);
527
528inline bool AreParallel(const FSegment2D& SegmentAB, const FSegment2D& SegmentCD)
529{
530 const FVector2d AB = SegmentAB.GetVector().GetSafeNormal();
531 const FVector2d CD = SegmentCD.GetVector().GetSafeNormal();
532 const double ParallelCoef = AB ^ CD;
534};
535
536} // namespace UE::CADKernel
537
@ Normal
Definition AndroidInputInterface.h:116
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
#define SMALL_NUMBER_SQUARE
Definition Types.h:10
UE::Math::TVector2< double > FVector2d
Definition MathFwd.h:61
#define DOUBLE_KINDA_SMALL_NUMBER
Definition UnrealMathUtility.h:73
#define DOUBLE_SMALL_NUMBER
Definition UnrealMathUtility.h:72
uint8_t uint8
Definition binka_ue_file_header.h:8
Definition Array.h:670
Definition MatrixH.h:14
void Inverse()
Definition MatrixH.cpp:114
static FVector2d FromVector(const FVector &Src)
Definition Point.h:18
Definition TestUtils.cpp:8
void SetTolerance(const double Tolerance)
Definition Geometry.cpp:17
Definition CADEntity.cpp:23
PointType ProjectPointOnSegment(const PointType &Point, const PointType &InSegmentA, const PointType &InSegmentB, double &OutCoordinate, bool bRestrictCoodinateToInside=true)
Definition Geometry.h:116
FVector ProjectPointOnPlane(const FVector &Point, const FVector &Origin, const FVector &InNormal, double &OutDistance)
Definition Geometry.h:153
EPolygonSide
Definition Geometry.h:14
@ Side20
Definition Geometry.h:17
@ Side30
Definition Geometry.h:19
@ Side01
Definition Geometry.h:15
@ Side12
Definition Geometry.h:16
@ Side23
Definition Geometry.h:18
FVector2d FindIntersectionOfSegments2D(const FSegment2D &SegmentAB, const FSegment2D &SegmentCD, double &OutABIntersectionCoordinate)
Definition Geometry.h:443
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
bool DoIntersectInside(const FSegment2D &SegmentAB, const FSegment2D &SegmentCD)
Definition Geometry.cpp:252
PointType PointOnSegment(const PointType &InSegmentA, const PointType &InSegmentB, double InCoordinate)
Definition Geometry.h:169
CADKERNEL_API FVector2d ComputeCircumCircleCenterAndSquareRadius(const FVector2d &InPoint0, const FVector2d &InPoint1, const FVector2d &InPoint2, double &OutSquareRadius)
Definition Geometry.h:88
bool FindIntersectionOfLines2D(const FSegment2D &LineAB, const FSegment2D &LineCD, FVector2d &OutIntersectionPoint)
Definition Geometry.h:501
double DistanceOfPointToLine(const PointType &Point, const PointType &LinePoint1, const PointType &LineDirection)
Definition Geometry.h:201
double DistanceOfPointToSegment(const PointType &Point, const PointType &SegmentPoint1, const PointType &SegmentPoint2)
Definition Geometry.h:180
double ComputeCurvature(const FVector &Gradient, const FVector &Laplacian)
Definition Geometry.cpp:92
TSegment< FVector2d > FSegment2D
Definition Geometry.h:286
EIso
Definition GeoEnum.h:66
CADKERNEL_API FVector2d ComputeCircumCircleCenter(const FVector2d &InPoint0, const FVector2d &InPoint1, const FVector2d &InPoint2)
Definition Geometry.h:32
double CoordinateOfProjectedPointOnSegment(const PointType &Point, const PointType &InSegmentA, const PointType &InSegmentB, bool bRestrictCoodinateToInside=true)
Definition Geometry.h:214
bool AreParallel(const FSegment2D &SegmentAB, const FSegment2D &SegmentCD)
Definition Geometry.h:528
@ Iso
Definition Visu.h:20
@ Point
Definition Visu.h:17
void FindLoopIntersectionsWithIso(const EIso Iso, const double IsoParameter, const TArray< TArray< FVector2d > > &Loops, TArray< double > &OutIntersections)
Definition Geometry.cpp:109
double SquareDistanceOfPointToSegment(const PointType &Point, const PointType &SegmentPoint1, const PointType &SegmentPoint2)
Definition Geometry.h:191
U16 Index
Definition radfft.cpp:71
static constexpr UE_FORCEINLINE_HINT T Square(const T A)
Definition UnrealMathUtility.h:578
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)
Definition UnrealMathUtility.h:592
static UE_FORCEINLINE_HINT bool IsNearlyZero(float Value, float ErrorTolerance=UE_SMALL_NUMBER)
Definition UnrealMathUtility.h:407
Definition Geometry.h:428
FTriangle2D(const FVector2d &InPoint0, const FVector2d &InPoint1, const FVector2d &InPoint2)
Definition Geometry.h:429
FVector2d CircumCircleCenterWithSquareRadius(double &SquareRadius) const
Definition Geometry.h:434
Definition Geometry.h:413
virtual FVector ComputeNormal() const
Definition Geometry.h:419
FTriangle(const FVector &InPoint0, const FVector &InPoint1, const FVector &InPoint2)
Definition Geometry.h:414
Definition Geometry.h:250
TSegment(const PointType &InPoint0, const PointType &InPoint1)
Definition Geometry.h:255
PointType GetVector() const
Definition Geometry.h:280
double SquaredLength() const
Definition Geometry.h:275
constexpr const PointType & operator[](int32 Index) const
Definition Geometry.h:261
const PointType & Point1
Definition Geometry.h:252
const PointType & Point0
Definition Geometry.h:251
Definition Geometry.h:291
const PointType & Point0
Definition Geometry.h:292
const PointType & Point1
Definition Geometry.h:293
constexpr const PointType & operator[](int32 Index) const
Definition Geometry.h:303
virtual PointType ProjectPoint(const PointType &InPoint, FVector2d &OutCoordinate)
Definition Geometry.h:319
virtual PointType CircumCircleCenter() const
Definition Geometry.h:406
TTriangle(const PointType &InPoint0, const PointType &InPoint1, const PointType &InPoint2)
Definition Geometry.h:296
const PointType & Point2
Definition Geometry.h:294
UE_FORCEINLINE_HINT T SquaredLength() const
Definition Vector2D.h:516
static UE_FORCEINLINE_HINT double DistSquared(const TVector2< double > &V1, const TVector2< double > &V2)
Definition Vector2D.h:935
static CORE_API const TVector2< double > ZeroVector
Definition Vector2D.h:63
static CORE_API const TVector< double > ZeroVector
Definition Vector.h:79
bool Normalize(T Tolerance=UE_SMALL_NUMBER)
Definition Vector.h:1767
T SquaredLength() const
Definition Vector.h:1734