6#include "Math/Vector2D.h"
7#include "Math/Vector.h"
12
13
14
15
16
17
18
19
20
21
22
23 template<
typename VectorType,
typename Allocator>
24 void ComputeConvexHull(
const TArray<VectorType, Allocator>& Points, TArray<int32, Allocator>& OutIndices)
27 int32 PointsNum = Points.Num();
30 for (int32 Index = 0; Index < PointsNum; ++Index)
32 OutIndices.Add(Index);
39 TArray<uint32, Allocator> SortedIndices;
40 SortedIndices.SetNumUninitialized(PointsNum);
41 for (int32 Index = 0; Index < PointsNum; ++Index)
43 SortedIndices[Index] = Index;
49 [P = Points.GetData()](uint32 a, uint32 b)
52 return P[a].X == P[b].X ? P[a].Y < P[b].Y : P[a].X < P[b].X;
57 [](
const VectorType& O,
const VectorType& A,
const VectorType& B)
59 return ((A.X - O.X) * (B.Y - O.Y) - (A.Y - O.Y) * (B.X - O.X)) <= 0;
63 OutIndices.SetNum(PointsNum*2);
66 for (int32 Index = 0; Index < PointsNum; ++Index)
68 const VectorType& B = Points[SortedIndices[Index]];
69 while (HullIndex >= 2 && IsClockwise(Points[OutIndices[HullIndex - 2]], Points[OutIndices[HullIndex - 1]], B))
74 OutIndices[HullIndex++] = SortedIndices[Index];
78 for (int32 Index = PointsNum - 1, StartIndex = HullIndex + 1; Index > 0; --Index)
80 const VectorType& B = Points[SortedIndices[Index - 1]];
81 while (HullIndex >= StartIndex && IsClockwise(Points[OutIndices[HullIndex - 2]], Points[OutIndices[HullIndex - 1]], B))
86 OutIndices[HullIndex++] = SortedIndices[Index - 1];
89 OutIndices.SetNum(HullIndex - 1,
false);
93 inline FVector::FReal ComputeDeterminant(
const FVector& A,
const FVector& B,
const FVector& C)
95 const FVector::FReal u1 = B.X - A.X;
96 const FVector::FReal v1 = B.Y - A.Y;
97 const FVector::FReal u2 = C.X - A.X;
98 const FVector::FReal v2 = C.Y - A.Y;
100 return u1 * v2 - v1 * u2;
104 inline bool ComparePoints(
const FVector& A,
const FVector& B)
130
131
132
133
134
135 template<
typename Allocator>
136 void ComputeConvexHullLegacy(
const TArray<FVector, Allocator>& Points, TArray<int32, Allocator>& OutIndices)
138 if (Points.Num() == 0)
148 for (int32 i = 1; i < Points.Num(); ++i)
150 if (ComparePoints(Points[i], Points[HullStart]))
154 if (ComparePoints(Points[HullEnd], Points[i]))
160 OutIndices.Add(HullStart);
162 if(HullStart == HullEnd)
169 int32 Hull = HullStart;
170 int LocalEnd = HullEnd;
171 bool bGoRight =
true;
172 bool bFinished =
false;
175 while (OutIndices.Num() <= Points.Num())
177 int32 NextPoint = LocalEnd;
179 for (
int j = 0; j < Points.Num(); ++j)
181 if(j == NextPoint || j == Hull)
186 const FVector & A = Points.GetData()[Hull];
187 const FVector & B = Points.GetData()[NextPoint];
188 const FVector & C = Points.GetData()[j];
189 FVector::FReal Deter = ComputeDeterminant(A, B, C);
197 else if(Deter < 0.001)
201 if(ComparePoints(B, C))
209 if(ComparePoints(C, B))
222 if(NextPoint == HullEnd)
226 LocalEnd = HullStart;
229 if(NextPoint == HullStart)
236 OutIndices.Add(NextPoint);
249 inline FVector2D::FReal ComputeDeterminant2D(
const FVector2D& A,
const FVector2D& B,
const FVector2D& C)
251 const FVector2D::FReal u1 = B.X - A.X;
252 const FVector2D::FReal v1 = B.Y - A.Y;
253 const FVector2D::FReal u2 = C.X - A.X;
254 const FVector2D::FReal v2 = C.Y - A.Y;
256 return u1 * v2 - v1 * u2;
260
261
262
263
264 template<
typename Allocator>
265 void ComputeConvexHullLegacy2(
const TArray<FVector2D, Allocator>& Points, TArray<int32, Allocator>& OutIndices)
267 if (Points.Num() == 0)
273 int32 LeftmostIndex = -1;
274 FVector2D Leftmost(FLT_MAX, FLT_MAX);
276 for (int32 PointIndex = 0; PointIndex < Points.Num(); PointIndex++)
278 if (Points[PointIndex].X < Leftmost.X
279 || (Points[PointIndex].X == Leftmost.X && Points[PointIndex].Y < Leftmost.Y))
281 LeftmostIndex = PointIndex;
282 Leftmost = Points[PointIndex];
286 int32 PointOnHullIndex = LeftmostIndex;
291 OutIndices.Add(PointOnHullIndex);
295 for (int32 j = 1; j < Points.Num(); j++)
297 if (EndPointIndex == PointOnHullIndex
298 || ComputeDeterminant2D(Points[EndPointIndex], Points[OutIndices.Last()], Points[j]) < 0)
304 PointOnHullIndex = EndPointIndex;
306 while (EndPointIndex != LeftmostIndex);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417