UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Voronoi.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3#include "Containers/Array.h"
5#include "Containers/Map.h"
6#include "CoreMinimal.h"
7#include "Math/Box.h"
10#include "Math/Vector.h"
12#include "Templates/PimplPtr.h"
13#include "Templates/Tuple.h"
15
18
19// All the info you would typically want about a single cell in the Voronoi diagram, in the format that is easiest to compute
27
28// third party library voro++'s container class; stores voronoi sites in a uniform grid accel structure
29namespace voro {
30 class container;
31 template <class ContainerType> class voro_compute;
32}
33
34class FVoronoiDiagram;
35
36// The Voronoi Compute Helper caches information to help compute cells quickly
37// To safely parallelize voronoi diagram construction, create a separate compute helper per-thread
53
55{
56 int32 NumSites;
58 FBox Bounds;
59
60public:
62
82
91
95 FVoronoiDiagram() : NumSites(0), Container(nullptr), Bounds(EForceInit::ForceInit)
96 {}
97
102
113
115
117
118 int32 Num() const
119 {
120 return NumSites;
121 }
122
124
127
129
132
142
148
150
152 {
154 {
155 return FMath::Max(MinDefaultSitesPerThread, NumSites / 64);
156 }
158 }
159
160private:
161 VORONOI_API TArray<int32> GetParallelBlockRanges(int32 ApproxSitesPerThread);
162};
163
174{
175 TArray<FVector> Sites;
176 TArray<TArray<int32>> Neighbors;
177 FVoronoiDiagram Diagram;
178
179public:
181
186
188 {
189 Sites = SitesIn;
190 Diagram.Initialize(Sites, Bounds, 0, SquaredDistSkipPtThreshold);
191 Diagram.ComputeAllNeighbors(Neighbors, true);
192 }
193
198
200 {
201 return Diagram.GetComputeHelper();
202 }
203
204 // @return Distance to the nearest Voronoi cell boundary (ignoring bounding box walls), or InvalidValue if outside of bounds
206 {
208 int32 Cell = Diagram.FindCell(Sample, ComputeHelper, CloseSite);
209 if (Cell < 0)
210 {
211 return InvalidValue;
212 }
213 double BestDistSq = DBL_MAX;
214 int32 BestNbr = -1;
215 for (int32 NbrCell : Neighbors[Cell])
216 {
217 double DistSq = FVector::DistSquared(Sample, Sites[NbrCell]);
218 if (DistSq < BestDistSq)
219 {
222 }
223 }
224 if (BestNbr == -1)
225 {
226 return InvalidValue;
227 }
228 FVector NbrPos = Sites[BestNbr];
229 FVector Mid = (NbrPos + CloseSite) * .5;
232 checkSlow(bNormalizeSuccess); // expect this can't happen because the Voronoi diagram can't be constructed w/ duplicate points
233 // Note: Returns a signed plane distance, but should always be positive because we know Sample is closer to CloseSite
234 return (Mid - Sample).Dot(Normal);
235 }
236
238 {
241 ToRet.Key = Diagram.FindCell(Sample, ComputeHelper, CloseSite);
242 if (ToRet.Key < 0)
243 {
244 return ToRet;
245 }
246 double BestDistSq = DBL_MAX;
247 ToRet.Value = -1;
248 for (int32 NbrCell : Neighbors[ToRet.Key])
249 {
250 double DistSq = FVector::DistSquared(Sample, Sites[NbrCell]);
251 if (DistSq < BestDistSq)
252 {
253 ToRet.Value = NbrCell;
255 }
256 }
257 return ToRet;
258 }
259
260 // @return Distance to the nearest Voronoi site, or InvalidValue if outside of bounds
262 {
264 int32 Cell = Diagram.FindCell(Sample, ComputeHelper, CloseSite);
265 if (Cell < 0)
266 {
267 return InvalidValue;
268 }
269 return FVector::Distance(Sample, CloseSite);
270 }
271
273 {
274 return Diagram.FindCell(Sample, ComputeHelper);
275 }
276};
@ Normal
Definition AndroidInputInterface.h:116
#define checkSlow(expr)
Definition AssertionMacros.h:332
EForceInit
Definition CoreMiscDefines.h:154
@ ForceInit
Definition CoreMiscDefines.h:155
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 UE_DOUBLE_KINDA_SMALL_NUMBER
Definition UnrealMathUtility.h:140
#define UE_KINDA_SMALL_NUMBER
Definition UnrealMathUtility.h:131
bool VORONOI_API VoronoiNeighbors(const TArrayView< const FVector > &Sites, TArray< TArray< int > > &Neighbors, bool bExcludeBounds=true, double SquaredDistSkipPtThreshold=UE_KINDA_SMALL_NUMBER)
Definition Voronoi.cpp:163
bool VORONOI_API GetVoronoiEdges(const TArrayView< const FVector > &Sites, const FBox &Bounds, TArray< TTuple< FVector, FVector > > &Edges, TArray< int32 > &CellMember, double SquaredDistSkipPtThreshold=UE_KINDA_SMALL_NUMBER)
Definition Voronoi.cpp:193
Definition Voronoi.h:39
FVoronoiComputeHelper(const FVoronoiComputeHelper &Other)=delete
FVoronoiComputeHelper(FVoronoiComputeHelper &&Other)=default
VORONOI_API void Init()
FVoronoiComputeHelper & operator=(FVoronoiComputeHelper &&Other)=default
FVoronoiComputeHelper & operator=(const FVoronoiComputeHelper &Other)=delete
FVoronoiComputeHelper()
Definition Voronoi.h:43
Definition Voronoi.h:174
double DistanceToClosest(const FVector &Sample, FVoronoiComputeHelper &ComputeHelper, double InvalidValue=TNumericLimits< double >::Lowest())
Definition Voronoi.h:261
double DistanceToCellWall(const FVector &Sample, FVoronoiComputeHelper &ComputeHelper, double InvalidValue=TNumericLimits< double >::Lowest())
Definition Voronoi.h:205
FVoronoiDiagramField & operator=(FVoronoiDiagramField &&Other)=default
FVoronoiDiagramField(const TArray< FVector > &SitesIn, const FBox &Bounds, double SquaredDistSkipPtThreshold=DBL_EPSILON)
Definition Voronoi.h:182
int32 ClosestID(const FVector &Sample, FVoronoiComputeHelper &ComputeHelper)
Definition Voronoi.h:272
FVoronoiComputeHelper GetComputeHelper() const
Definition Voronoi.h:199
FVoronoiDiagramField & operator=(const FVoronoiDiagramField &Other)=delete
FVoronoiDiagramField()=default
void Initialize(const TArray< FVector > &SitesIn, const FBox &Bounds, double SquaredDistSkipPtThreshold=DBL_EPSILON)
Definition Voronoi.h:187
TPair< int32, int32 > ClosestTwoIDs(const FVector &Sample, FVoronoiComputeHelper &ComputeHelper)
Definition Voronoi.h:237
FVoronoiDiagramField(FVoronoiDiagramField &&Other)=default
FVoronoiDiagramField(const FVoronoiDiagramField &Other)=delete
Definition Voronoi.h:55
VORONOI_API FVoronoiComputeHelper GetComputeHelper() const
Definition Voronoi.cpp:493
FVoronoiDiagram(const FVoronoiDiagram &Other)=delete
FVoronoiDiagram & operator=(FVoronoiDiagram &&Other)=default
VORONOI_API ~FVoronoiDiagram()
int32 ApproxSitesPerThreadWithDefault(int32 ApproxSitesPerThreadIn)
Definition Voronoi.h:151
int32 Num() const
Definition Voronoi.h:118
VORONOI_API void Initialize(const TArrayView< const FVector > &Sites, const FBox &Bounds, double ExtraBoundingSpace, double SquaredDistSkipPtThreshold=UE_KINDA_SMALL_NUMBER)
Definition Voronoi.cpp:154
VORONOI_API void AddSites(const TArrayView< const FVector > &Sites, double SquaredDistSkipPtThreshold=0.0f)
Definition Voronoi.cpp:204
VORONOI_API static const int MinDefaultSitesPerThread
Definition Voronoi.h:61
FVoronoiDiagram(FVoronoiDiagram &&Other)=default
FVoronoiDiagram()
Definition Voronoi.h:95
VORONOI_API void ComputeAllCells(TArray< FVoronoiCellInfo > &AllCells, int32 ApproxSitesPerThread=-1)
Definition Voronoi.cpp:283
VORONOI_API void ComputeAllNeighbors(TArray< TArray< int32 > > &AllNeighbors, bool bExcludeBounds=true, int32 ApproxSitesPerThread=-1)
Definition Voronoi.cpp:324
VORONOI_API void ComputeCellEdgesSerial(TArray< TTuple< FVector, FVector > > &Edges, TArray< int32 > &CellMember)
Definition Voronoi.cpp:356
VORONOI_API void ComputeAllCellsSerial(TArray< FVoronoiCellInfo > &AllCells)
Definition Voronoi.cpp:218
VORONOI_API int32 FindCell(const FVector &Pos, FVoronoiComputeHelper &ComputeHelper, FVector &OutFoundSite) const
Definition Voronoi.cpp:483
VORONOI_API void ComputeCellEdges(TArray< TTuple< FVector, FVector > > &Edges, TArray< int32 > &CellMember, int32 ApproxSitesPerThread=-1)
Definition Voronoi.cpp:404
FVoronoiDiagram & operator=(const FVoronoiDiagram &Other)=delete
int32 FindCell(const FVector &Pos, FVoronoiComputeHelper &ComputeHelper) const
Definition Voronoi.h:143
static VORONOI_API FBox GetBounds(const TArrayView< const FVector > &Sites, double ExtraBoundingSpace=UE_DOUBLE_KINDA_SMALL_NUMBER)
Definition Voronoi.cpp:148
Definition ArrayView.h:139
Definition Array.h:670
Definition c_loops.cc:15
Definition Voronoi.h:21
TArray< int32 > Neighbors
Definition Voronoi.h:24
TArray< FVector > Normals
Definition Voronoi.h:25
TArray< FVector > Vertices
Definition Voronoi.h:22
TArray< int32 > Faces
Definition Voronoi.h:23
Definition NumericLimits.h:41
Definition PimplPtr.h:50
Definition Tuple.h:652
bool Normalize(T Tolerance=UE_SMALL_NUMBER)
Definition Vector.h:1767
static UE_FORCEINLINE_HINT double Distance(const TVector< double > &V1, const TVector< double > &V2)
Definition Vector.h:1018
static UE_FORCEINLINE_HINT double DistSquared(const TVector< double > &V1, const TVector< double > &V2)
Definition Vector.h:2478