UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SlopeUtils.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Core/Types.h"
6#include "Math/Point.h"
7#include "Algo/AllOf.h"
8
9namespace UE::CADKernel
10{
11
51namespace Slope
52{
53
54constexpr double NullSlope = 0.;
55
59constexpr double RightSlope = 2.;
60constexpr double HalfPiSlope = 2.;
61constexpr double NinetySlope = 2.;
62
66constexpr double ThreeRightSlope = 6.;
67
71constexpr double MinusRightSlope = -2.;
72
76constexpr double PiSlope = 4.;
77
81constexpr double TwoPiSlope = 8.;
82
86constexpr double ThirdPiSlope = 1.422649730810374235490851219498;
87constexpr double SixtySlope = 1.422649730810374235490851219498;
88
92constexpr double QuaterPiSlope = 1;
93constexpr double FortyFiveSlope = 0.57735026918962576450914878050196;
94
98constexpr double SixthPiSlope = 0.57735026918962576450914878050196;
99constexpr double ThirtySlope = 0.57735026918962576450914878050196;
100
104constexpr double ThreeQuaterPiSlope = 3;
105
106constexpr double OneDegree = 0.01745506492821758576512889521973;
107constexpr double TwoDegree = 0.03492076949174773050040262577373;
108constexpr double FiveDegree = 0.08748866352592400522201866943496;
109constexpr double TenDegree = 0.17632698070846497347109038686862;
110constexpr double FifteenDegree = 0.26794919243112270647255365849413;
111constexpr double TwentyDegree = 0.36397023426620236135104788277683;
112constexpr double TwentyFiveDegree = 0.46630765815499859283000619479956;
113
114constexpr double Epsilon = 0.001;
115
116}
117
118typedef TFunction<double(const FVector2d&, const FVector2d&, double)> SlopeMethod;
119
124inline double TransformIntoOrientedSlope(double Slope)
125{
127}
128
129inline double TransformIntoClockwiseSlope(double Slope)
130{
131 return Slope::TwoPiSlope - Slope;
132}
133
138inline double TransformIntoUnorientedSlope(double Slope)
139{
140 return FMath::Abs(WrapTo(Slope, -Slope::PiSlope, Slope::PiSlope, Slope::TwoPiSlope));
141}
142
147inline double TransformIntoPositiveSlope(double Slope)
148{
150}
151
158{
159 Slope = TransformIntoUnorientedSlope(Slope);
160
161 if (Slope > Slope::RightSlope)
162 {
163 Slope = Slope::PiSlope - Slope;
164 }
165
166 return Slope;
167}
168
169
174inline double SwapSlopeOrientation(double Slope)
175{
176 const double SwapedSlope = Slope < Slope::PiSlope ? Slope + Slope::PiSlope : Slope - Slope::PiSlope;
178}
179
180inline double ComputeSlope(const FVector2d& StartPoint, const FVector2d& EndPoint)
181{
182 double DeltaU = EndPoint.X - StartPoint.X;
183 double DeltaV = EndPoint.Y - StartPoint.Y;
184 double Delta;
185
186 if (FMath::Abs(DeltaU) < DOUBLE_SMALL_NUMBER && FMath::Abs(DeltaV) < DOUBLE_SMALL_NUMBER)
187 {
188 return 0;
189 }
190
191 if (DeltaU > DOUBLE_SMALL_NUMBER)
192 {
194 {
195 if (DeltaU > DeltaV)
196 {
197 Delta = DeltaV / DeltaU;
198 }
199 else
200 {
201 Delta = 2. - DeltaU / DeltaV;
202 }
203 }
204 else
205 {
206 if (DeltaU > -DeltaV)
207 {
208 Delta = 8. + DeltaV / DeltaU;
209 }
210 else if (fabs(DeltaV) > DOUBLE_SMALL_NUMBER)
211 {
212 Delta = 6. - DeltaU / DeltaV; // deltaU/deltaV <0
213 }
214 else
215 {
216 Delta = 8.;
217 }
218 }
219 }
220 else if (-DeltaU > DOUBLE_SMALL_NUMBER)
221 {
223 {
224 if (-DeltaU > DeltaV)
225 {
226 Delta = 4. + DeltaV / DeltaU;
227 }
228 else
229 {
230 Delta = 2. - DeltaU / DeltaV;
231 }
232 }
233 else
234 {
235 if (-DeltaU > -DeltaV)
236 {
237 Delta = 4. + DeltaV / DeltaU;
238 }
239 else if (fabs(DeltaV) > DOUBLE_SMALL_NUMBER)
240 {
241 Delta = 6. - DeltaU / DeltaV;
242 }
243 else
244 {
245 Delta = 4.;
246 }
247 }
248 }
249 else
250 {
251 if (DeltaV > 0)
252 {
253 Delta = 2.;
254 }
255 else
256 {
257 Delta = 6.;
258 }
259 }
260
261 return Delta;
262}
263
268inline double ComputeSlope(const FVector2d& StartPoint, const FVector2d& EndPoint, double ReferenceSlope)
269{
270 const double Slope = ComputeSlope(StartPoint, EndPoint);
271 return Slope - ReferenceSlope;
272}
273
278inline double ComputeSlope(const FVector2d& StartPoint, const FVector2d& EndPoint1, const FVector2d& EndPoint2)
279{
280 const double ReferenceSlope = ComputeSlope(StartPoint, EndPoint1);
281 const double Slope = ComputeSlope(StartPoint, EndPoint2);
282 return Slope - ReferenceSlope;
283}
284
290inline double ComputePositiveSlope(const FVector2d& StartPoint, const FVector2d& EndPoint, double ReferenceSlope)
291{
292 double Slope = ComputeSlope(StartPoint, EndPoint, ReferenceSlope);
293 return TransformIntoPositiveSlope(Slope);
294}
295
300inline double ComputePositiveSlope(const FVector2d& StartPoint, const FVector2d& EndPoint1, const FVector2d& EndPoint2)
301{
302 double Slope = ComputeSlope(StartPoint, EndPoint1, EndPoint2);
303 return TransformIntoPositiveSlope(Slope);
304}
305
306inline double ClockwiseSlope(const FVector2d& StartPoint, const FVector2d& EndPoint, double ReferenceSlope)
307{
309}
310
311inline double CounterClockwiseSlope(const FVector2d& StartPoint, const FVector2d& EndPoint, double ReferenceSlope)
312{
313 return ComputePositiveSlope(StartPoint, EndPoint, ReferenceSlope);
314}
315
320inline double ComputeOrientedSlope(const FVector2d& StartPoint, const FVector2d& EndPoint, double ReferenceSlope)
321{
322 return TransformIntoOrientedSlope(ComputeSlope(StartPoint, EndPoint, ReferenceSlope));
323}
324
329inline double ComputeOrientedSlope(const FVector2d& StartPoint, const FVector2d& EndPoint1, const FVector2d& EndPoint2)
330{
331 return TransformIntoOrientedSlope(ComputeSlope(StartPoint, EndPoint1, EndPoint2));
332}
333
337inline double ComputeUnorientedSlope(const FVector2d& StartPoint, const FVector2d& EndPoint, double ReferenceSlope)
338{
339 const double Slope = ComputeSlope(StartPoint, EndPoint, ReferenceSlope);
340 return TransformIntoUnorientedSlope(Slope);
341}
342
349inline double ComputeSlopeRelativeToNearestAxis(const FVector2d& StartPoint, const FVector2d& EndPoint)
350{
351 double Slope = TransformIntoUnorientedSlope(ComputeSlope(StartPoint, EndPoint));
352 if (Slope > Slope::RightSlope)
353 {
354 Slope = Slope::PiSlope - Slope;
355 }
356
357 // if slope close to 2 means segment close to IsoU, otherwise segment close to IsoV
358 // Wants a slope between 0 and 1 to manage either IsoU and IsoV
359 // Close to 0 means close to IsoU or IsoV
360 if (Slope > Slope::QuaterPiSlope)
361 {
362 Slope = Slope::RightSlope - Slope;
363 }
364
365 return Slope;
366}
367
373inline double ComputeSlopeRelativeToReferenceAxis(const FVector2d& StartPoint, const FVector2d& EndPoint, double ReferenceAxisSlope)
374{
375 const double Slope = ComputeSlope(StartPoint, EndPoint, ReferenceAxisSlope);
377}
378
382inline double ComputeUnorientedSlope(const FVector2d& StartPoint, const FVector2d& EndPoint1, const FVector2d& EndPoint2)
383{
384 return TransformIntoUnorientedSlope(ComputeSlope(StartPoint, EndPoint1, EndPoint2));
385}
386
401inline bool IsPointPInsideSectorABC(const FVector2d& PointA, const FVector2d& PointB, const FVector2d& PointC, const FVector2d& PointP, const double FlatAngle)
402{
403 double SlopWithNextBoundary = ComputeSlope(PointB, PointC);
407 {
408 return false;
409 }
410 return true;
411}
412
426inline bool ArePointsInsideSectorABC(const FVector2d& PointA, const FVector2d& PointB, const FVector2d& PointC, const TArray<const FVector2d*>& Points, const double FlatAngle = -DOUBLE_SMALL_NUMBER)
427{
428 double SlopWithNextBoundary = ComputeSlope(PointB, PointC);
430
431 return Algo::AllOf(Points, [&](const FVector2d* PointP)
432 {
433 double DeltaU = PointB.X - PointP->X;
434 double DeltaV = PointB.Y - PointP->Y;
435
436 if (FMath::Abs(DeltaU) < DOUBLE_SMALL_NUMBER && FMath::Abs(DeltaV) < DOUBLE_SMALL_NUMBER)
437 {
438 return true;
439 }
440
443 {
444 return false;
445 }
446 return true;
447 });
448}
449
450inline FVector2d SlopeToVector(const double Slope)
451{
452 int32 SlopeStep = (int32)(Slope);
454 switch (SlopeStep)
455 {
456 case 0:
457 // Delta = DeltaV / DeltaU;
458 Vector[0] = 1.;
459 Vector[1] = Slope;
460 break;
461 case 1:
462 // 2 - DeltaU / DeltaV;
463 Vector[0] = 2. - Slope;
464 Vector[1] = 1.;
465 break;
466 case 2:
467 // 2 - DeltaU / DeltaV;
468 Vector[0] = 2. - Slope;
469 Vector[1] = 1.;
470 break;
471 case 3:
472 // 4 + DeltaV / DeltaU;
473 Vector[0] = -1.;
474 Vector[1] = 4. - Slope;
475 break;
476 case 4:
477 // 4 + DeltaV / DeltaU;
478 Vector[0] = -1.;
479 Vector[1] = 4. - Slope;
480 break;
481 case 5:
482 // 6 - DeltaU / DeltaV;
483 Vector[0] = Slope - 6.;
484 Vector[1] = -1.;
485 break;
486 case 6:
487 // 6 - DeltaU / DeltaV // deltaU/deltaV <0
488 Vector[0] = Slope - 6.;
489 Vector[1] = -1.;
490 break;
491 case 7:
492 // 8 + DeltaV / DeltaU; // deltaU/deltaV <0
493 Vector[0] = 1.;
494 Vector[1] = Slope - 8.;
495 break;
496 default:
497 break;
498 }
499
500 return Vector;
501}
502
503} // namespace UE::CADKernel
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 DOUBLE_SMALL_NUMBER
Definition UnrealMathUtility.h:72
Definition Array.h:670
Definition AndroidPlatformMisc.h:14
bool AllOf(const RangeType &Range)
Definition AllOf.h:19
constexpr double TwentyFiveDegree
Definition SlopeUtils.h:112
constexpr double FifteenDegree
Definition SlopeUtils.h:110
constexpr double NullSlope
Definition SlopeUtils.h:54
constexpr double PiSlope
Definition SlopeUtils.h:76
constexpr double Epsilon
Definition SlopeUtils.h:114
constexpr double FortyFiveSlope
Definition SlopeUtils.h:93
constexpr double SixtySlope
Definition SlopeUtils.h:87
constexpr double SixthPiSlope
Definition SlopeUtils.h:98
constexpr double FiveDegree
Definition SlopeUtils.h:108
constexpr double MinusRightSlope
Definition SlopeUtils.h:71
constexpr double OneDegree
Definition SlopeUtils.h:106
constexpr double NinetySlope
Definition SlopeUtils.h:61
constexpr double TenDegree
Definition SlopeUtils.h:109
constexpr double TwentyDegree
Definition SlopeUtils.h:111
constexpr double RightSlope
Definition SlopeUtils.h:59
constexpr double ThirdPiSlope
Definition SlopeUtils.h:86
constexpr double ThirtySlope
Definition SlopeUtils.h:99
constexpr double TwoDegree
Definition SlopeUtils.h:107
constexpr double ThreeRightSlope
Definition SlopeUtils.h:66
constexpr double QuaterPiSlope
Definition SlopeUtils.h:92
constexpr double HalfPiSlope
Definition SlopeUtils.h:60
constexpr double ThreeQuaterPiSlope
Definition SlopeUtils.h:104
constexpr double TwoPiSlope
Definition SlopeUtils.h:81
Definition CADEntity.cpp:23
bool IsPointPInsideSectorABC(const FVector2d &PointA, const FVector2d &PointB, const FVector2d &PointC, const FVector2d &PointP, const double FlatAngle)
Definition SlopeUtils.h:401
bool ArePointsInsideSectorABC(const FVector2d &PointA, const FVector2d &PointB, const FVector2d &PointC, const TArray< const FVector2d * > &Points, const double FlatAngle=-DOUBLE_SMALL_NUMBER)
Definition SlopeUtils.h:426
double TransformIntoOrientedSlope(double Slope)
Definition SlopeUtils.h:124
double TransformIntoUnorientedSlope(double Slope)
Definition SlopeUtils.h:138
double ComputeOrientedSlope(const FVector2d &StartPoint, const FVector2d &EndPoint, double ReferenceSlope)
Definition SlopeUtils.h:320
double ClockwiseSlope(const FVector2d &StartPoint, const FVector2d &EndPoint, double ReferenceSlope)
Definition SlopeUtils.h:306
double SwapSlopeOrientation(double Slope)
Definition SlopeUtils.h:174
double TransformIntoClockwiseSlope(double Slope)
Definition SlopeUtils.h:129
TFunction< double(const FVector2d &, const FVector2d &, double)> SlopeMethod
Definition CycleTriangulator.h:23
double TransformIntoPositiveSlope(double Slope)
Definition SlopeUtils.h:147
double CounterClockwiseSlope(const FVector2d &StartPoint, const FVector2d &EndPoint, double ReferenceSlope)
Definition SlopeUtils.h:311
double ComputeSlopeRelativeToReferenceAxis(const FVector2d &StartPoint, const FVector2d &EndPoint, double ReferenceAxisSlope)
Definition SlopeUtils.h:373
double ComputeSlopeRelativeToNearestAxis(const FVector2d &StartPoint, const FVector2d &EndPoint)
Definition SlopeUtils.h:349
double ComputeSlope(const FVector2d &StartPoint, const FVector2d &EndPoint)
Definition SlopeUtils.h:180
double ComputeUnorientedSlope(const FVector2d &StartPoint, const FVector2d &EndPoint, double ReferenceSlope)
Definition SlopeUtils.h:337
double ComputePositiveSlope(const FVector2d &StartPoint, const FVector2d &EndPoint, double ReferenceSlope)
Definition SlopeUtils.h:290
FVector2d SlopeToVector(const double Slope)
Definition SlopeUtils.h:450
double TransformIntoSlopeRelativeToReferenceAxis(double Slope)
Definition SlopeUtils.h:157
double WrapTo(double Slope, const double StartOfPeriod, const double EndOfPeriod, const double PeriodLength)
Definition MathConst.h:65
T Y
Definition Vector2D.h:52
T X
Definition Vector2D.h:49
static CORE_API const TVector2< double > ZeroVector
Definition Vector2D.h:63