UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
GeneralPolygon2.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// port of geometry3Sharp Polygon
4
5#pragma once
6
8#include "Math/UnrealMath.h"
9#include "VectorTypes.h"
10#include "Polygon2.h"
11#include "BoxTypes.h"
12#include "MatrixTypes.h"
13#include "MathUtil.h"
14
15
16namespace UE
17{
18namespace Geometry
19{
20
21using namespace UE::Math;
22
26template<typename T>
28{
29protected:
30 /* The Outer boundary of the polygon */
32
33 /* if true, Outer polygon winding is clockwise */
35
38
39public:
40
42 {
43 }
44
52
54 {
55 this->Outer = ToSetOuter;
57 }
58
60 {
61 checkSlow(ToSetOuter.IsClockwise() == bToSetOuterIsCW);
62 this->Outer = ToSetOuter;
63 this->bOuterIsCW = bToSetOuterIsCW;
64 }
65
66 const TPolygon2<T>& GetOuter() const
67 {
68 return this->Outer;
69 }
70
72 {
73 return Holes;
74 }
75
76
78 {
80 {
81 if (!Outer.Contains(Hole))
82 {
83 return false;
84 }
85
86 for (const TPolygon2<T>& ExistingHole : Holes)
87 {
88 if (Hole.Overlaps(ExistingHole))
89 {
90 return false;
91 }
92 }
93 }
94
95
97 {
98 bool bHoleIsClockwise = Hole.IsClockwise();
100 {
101 return false;
102 }
103 }
104
105 Holes.Add(Hole);
106 return true;
107 }
108
110 {
111 Holes.Empty();
112 }
113
114
115 bool HasHoles() const
116 {
117 return Holes.Num() > 0;
118 }
119
120
125 {
126 int32 NumHoles = Holes.Num();
127 for (int32 k = 0; k < NumHoles; ++k)
128 {
130 {
131 Holes.RemoveAtSwap(k);
132 k--; // need to reconsider index k
133 NumHoles--;
134 }
135 }
136 }
137
138
139 double SignedArea() const
140 {
141 double Sign = (bOuterIsCW) ? -1.0 : 1.0;
142 double AreaSum = Sign * Outer.SignedArea();
143 for (const TPolygon2<T>& Hole : Holes)
144 {
145 AreaSum += Sign * Hole.SignedArea();
146 }
147 return AreaSum;
148 }
149
150
151 double HoleUnsignedArea() const
152 {
153 double AreaSum = 0;
154 for (const TPolygon2<T>& Hole : Holes)
155 {
156 AreaSum += FMath::Abs(Hole.SignedArea());
157 }
158 return AreaSum;
159 }
160
161
162 double Perimeter() const
163 {
164 double PerimSum = Outer.Perimeter();
165 for (const TPolygon2<T> &Hole : Holes)
166 {
167 PerimSum += Hole.Perimeter();
168 }
169 return PerimSum;
170 }
171
172
174 {
175 TAxisAlignedBox2<T> Box = Outer.Bounds();
176 for (const TPolygon2<T> Hole : Holes)
177 {
178 Box.Contain(Hole.Bounds());
179 }
180 return Box;
181 }
182
183
184 void Translate(TVector2<T> translate)
185 {
186 Outer.Translate(translate);
187 for (TPolygon2<T>& Hole : Holes)
188 {
189 Hole.Translate(translate);
190 }
191 }
192
193
195 Outer.Scale(scale, origin);
196 for (TPolygon2<T>& Hole : Holes)
197 {
198 Hole.Scale(scale, origin);
199 }
200 }
201
203 {
204 Outer.Transform(TransformFunc);
205 for (TPolygon2<T>& Hole : Holes)
206 {
207 Hole.Transform(TransformFunc);
208 }
209 }
210
211 void Reverse()
212 {
213 Outer.Reverse();
215 for (TPolygon2<T>& Hole : Holes)
216 {
217 Hole.Reverse();
218 }
219 }
220
221 bool OuterIsClockwise() const
222 {
223 return bOuterIsCW;
224 }
225
227 {
228 if (Outer.Contains(vTest) == false)
229 {
230 return false;
231 }
232 for (const TPolygon2<T>& Hole : Holes)
233 {
234 if (Hole.Contains(vTest))
235 {
236 return false;
237 }
238 }
239 return true;
240 }
241
243 if (Outer.Contains(Poly) == false)
244 {
245 return false;
246 }
247 for (const TPolygon2<T>& Hole : Holes)
248 {
249 if (Hole.Overlaps(Poly))
250 {
251 return false;
252 }
253 }
254 return true;
255 }
256
257
259 {
260 if (Outer.Intersects(Poly))
261 {
262 return true;
263 }
264 for (const TPolygon2<T>& Hole : Holes)
265 {
266 if (Hole.Intersects(Poly))
267 {
268 return true;
269 }
270 }
271 return false;
272 }
273
274
276 {
277 if (iHoleIndex == -1)
278 {
279 return Outer.GetSegmentPoint(iSegment, fSegT);
280 }
281 return Holes[iHoleIndex].GetSegmentPoint(iSegment, fSegT);
282 }
283
285 {
286 if (iHoleIndex == -1)
287 {
288 return Outer.Segment(iSegment);
289 }
290 return Holes[iHoleIndex].Segment(iSegment);
291 }
292
293 TVector2<T> GetNormal(int iSegment, double segT, int iHoleIndex = -1) const
294 {
295 if (iHoleIndex == -1)
296 {
297 return Outer.GetNormal(iSegment, segT);
298 }
299 return Holes[iHoleIndex].GetNormal(iSegment, segT);
300 }
301
302 // this should be more efficient when there are Holes...
303 double DistanceSquared(TVector2<T> p, int &iHoleIndex, int &iNearSeg, double &fNearSegT) const
304 {
305 iNearSeg = iHoleIndex = -1;
307 double dist = Outer.DistanceSquared(p, iNearSeg, fNearSegT);
308 for (int i = 0; i < Holes.Num(); ++i )
309 {
310 int seg; double segt;
311 double holedist = Holes[i].DistanceSquared(p, seg, segt);
312 if (holedist < dist)
313 {
314 dist = holedist;
315 iHoleIndex = i;
316 iNearSeg = seg;
317 fNearSegT = segt;
318 }
319 }
320 return dist;
321 }
322
323
324 void Simplify(double ClusterTol = 0.0001, double LineDeviationTol = 0.01)
325 {
326 // [TODO] should make sure that Holes stay inside Outer!!
328 for (TPolygon2<T>& Hole : Holes)
329 {
331 }
332 }
333
334
341 {
342 Outer.VtxNormalOffset(OffsetDistance, bUseFaceAvg);
343 for (TPolygon2<T>& Hole : Holes)
344 {
345 Hole.VtxNormalOffset(OffsetDistance, bUseFaceAvg);
346 }
347 }
348
349};
350
353
354
355} // end namespace UE::Geometry
356} // end namespace UE
#define checkSlow(expr)
Definition AssertionMacros.h:332
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
Definition Array.h:670
Definition AssetRegistryState.h:50
Definition AndroidPlatformMisc.h:14
Definition MathUtil.h:150
Definition GeneralPolygon2.h:28
bool Contains(TVector2< T > vTest) const
Definition GeneralPolygon2.h:226
bool Contains(TPolygon2< T > Poly) const
Definition GeneralPolygon2.h:242
void ClearHoles()
Definition GeneralPolygon2.h:109
TGeneralPolygon2(const TPolygon2< T > &ToSetOuter)
Definition GeneralPolygon2.h:48
double SignedArea() const
Definition GeneralPolygon2.h:139
TPolygon2< T > Outer
Definition GeneralPolygon2.h:31
const TArray< TPolygon2< T > > & GetHoles() const
Definition GeneralPolygon2.h:71
bool AddHole(TPolygon2< T > Hole, bool bCheckContainment=true, bool bCheckOrientation=true)
Definition GeneralPolygon2.h:77
bool bOuterIsCW
Definition GeneralPolygon2.h:34
double HoleUnsignedArea() const
Definition GeneralPolygon2.h:151
void SetOuter(const TPolygon2< T > &ToSetOuter)
Definition GeneralPolygon2.h:53
TGeneralPolygon2()
Definition GeneralPolygon2.h:41
TVector2< T > GetSegmentPoint(int iSegment, double fSegT, int iHoleIndex=-1) const
Definition GeneralPolygon2.h:275
void SetOuterWithOrientation(const TPolygon2< T > &ToSetOuter, bool bToSetOuterIsCW)
Definition GeneralPolygon2.h:59
void Translate(TVector2< T > translate)
Definition GeneralPolygon2.h:184
bool Intersects(TPolygon2< T > Poly) const
Definition GeneralPolygon2.h:258
void VtxNormalOffset(T OffsetDistance, bool bUseFaceAvg=false)
Definition GeneralPolygon2.h:340
void FilterHoles(TFunctionRef< bool(const TPolygon2< T > &)> RemoveHolePredicateFunc)
Definition GeneralPolygon2.h:124
void Transform(const TFunction< TVector2< T >(const TVector2< T > &)> &TransformFunc)
Definition GeneralPolygon2.h:202
double Perimeter() const
Definition GeneralPolygon2.h:162
double DistanceSquared(TVector2< T > p, int &iHoleIndex, int &iNearSeg, double &fNearSegT) const
Definition GeneralPolygon2.h:303
TVector2< T > GetNormal(int iSegment, double segT, int iHoleIndex=-1) const
Definition GeneralPolygon2.h:293
const TPolygon2< T > & GetOuter() const
Definition GeneralPolygon2.h:66
TAxisAlignedBox2< T > Bounds() const
Definition GeneralPolygon2.h:173
bool OuterIsClockwise() const
Definition GeneralPolygon2.h:221
TArray< TPolygon2< T > > Holes
Definition GeneralPolygon2.h:37
void Simplify(double ClusterTol=0.0001, double LineDeviationTol=0.01)
Definition GeneralPolygon2.h:324
bool HasHoles() const
Definition GeneralPolygon2.h:115
void Scale(TVector2< T > scale, TVector2< T > origin)
Definition GeneralPolygon2.h:194
void Reverse()
Definition GeneralPolygon2.h:211
TSegment2< T > Segment(int iSegment, int iHoleIndex=-1) const
Definition GeneralPolygon2.h:284
Definition Polygon2.h:30
bool IsClockwise() const
Definition Polygon2.h:270
TGeneralPolygon2< double > FGeneralPolygon2d
Definition GeneralPolygon2.h:351
TGeneralPolygon2< float > FGeneralPolygon2f
Definition GeneralPolygon2.h:352
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
Definition BoxTypes.h:637
void Contain(const TVector2< RealType > &V)
Definition BoxTypes.h:738
Definition SegmentTypes.h:23
Definition Vector2D.h:38