UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
MeshSurfacePointSampling.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "MathUtil.h"
6#include "VectorTypes.h"
7#include "FrameTypes.h"
9
10class FProgressCancel;
11
12namespace UE
13{
14namespace Geometry
15{
16
17class FDynamicMesh3;
18
19// class for efficiently drawing weighted samples (when you need to draw more than a very small amount of them)
20// note this is a more flexible version of the algorithm in Engine/WeightedRandomSampler.h,
21// with support for cases where some IDs cannot be sampled (e.g. when sampling triangles of non-compact meshes)
22template<typename RealType>
24{
25public:
35
42 {
43 bool bHasInvalidWeight = false;
44 RealType WtSum = (RealType)0;
45 for (RealType Wt : Weights)
46 {
47 if (Wt < 0)
48 {
49 bHasInvalidWeight = true;
50 continue;
51 }
52 WtSum += Wt;
53 }
54 Init(Weights, WtSum, bHasInvalidWeight);
55 }
56
57 inline int32 Num() const
58 {
59 return Probability.Num();
60 }
61
62 inline bool IsValid() const
63 {
64 return !Probability.IsEmpty();
65 }
66
67 // UniformRandom1, UniformRandom2 must be in the [0,1) range
68 inline int32 Sample(RealType UniformRandom1, RealType UniformRandom2) const
69 {
70 checkSlow(IsValid() && UniformRandom1 < (RealType)1);
71 int32 Idx = FMath::TruncToInt32(UniformRandom1 * (RealType)Probability.Num());
72 return UniformRandom2 < Probability[Idx] ? Idx : Alias[Idx];
73 }
74
75private:
76 // Table of probability that you should keep a given index chosen by unweighted random sampling (can be MaxReal if should always pick the index)
77 TArray<RealType> Probability;
78 // Table indicating the alternative index you should choose, if you don't keep the initial index
79 TArray<int32> Alias;
80};
81
82// Class to help draw uniform-random surface samples from a (possibly non-compact) triangle mesh.
83// TMeshType must implement: IsTriangle, MaxTriangleID, and GetTriVertices. RealType must be float or double.
84template<typename TMeshType, typename RealType>
86{
87public:
88
94
95 // @return true on successful initialization
96 bool Init(const TMeshType& Mesh)
97 {
98 const int32 MaxTID = Mesh.MaxTriangleID();
100 RealType WtSum = (RealType)0;
101 bool bHasInvalidWeights = false;
102 for (int32 TID = 0; TID < MaxTID; ++TID)
103 {
104 if (Mesh.IsTriangle(TID))
105 {
107 Mesh.GetTriVertices(TID, A, B, C);
108 RealType Area = (RealType)VectorUtil::Area(A, B, C);
109 WtSum += Area;
110 Wts.Add(Area);
111 }
112 else
113 {
114 bHasInvalidWeights = true;
115 Wts.Add(-1);
116 }
117 }
118 return AliasTable.Init(Wts, WtSum, bHasInvalidWeights);
119 }
120
121 // @return true if we can generate samples (e.g., if the class has been initialized with a non-empty mesh)
122 bool IsValid() const
123 {
124 return AliasTable.IsValid();
125 }
126
135 {
136 return AliasTable.Sample(UniformRandom1, UniformRandom2);
137 }
138
139private:
141};
142
146
147
153{
154public:
155 //
156 // Basic sampling parameters
157 //
158
160 double SampleRadius = 10.0;
161
164
166 double SubSampleDensity = 10.0;
167
170
173
182
183 //
184 // Parameters for non-uniform / variable-radius sampling.
185 // Disabled if MaxSampleRadius <= SampleRadius
186 //
187
189 double MaxSampleRadius = 0.0;
190
193 {
195 Uniform = 0,
197 Smaller = 1,
199 Larger = 2
200 };
203
206
207
209 bool bUseVertexWeights = false;
210
213
216 {
218 RadiusInterp = 0,
223 };
226
228 bool bInvertWeights = false;
229
232
233 //
234 // TODO: when MaxSamples is set, it would be useful to be able to use Weight to modulate
235 // positional distribution instead of radius, (or both!)
236 //
237
238public:
239 //
240 // Outputs
241 //
242
245
248
251
254
257
258public:
259
283
284
285 // ability to incrementally increase existing sample sizes to more tightly pack them?
286 // ability to iteratively redistribute samples to improve uniformity (ie potential field / mass-spring type method, point smoothing, ?)
287};
288
289
290
291
292
293
294} // end namespace UE::Geometry
295} // end namespace UE
296
#define checkSlow(expr)
Definition AssertionMacros.h:332
@ INDEX_NONE
Definition CoreMiscDefines.h:150
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
void Init()
Definition LockFreeList.h:4
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition ProgressCancel.h:187
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_REWRITE bool IsEmpty() const
Definition Array.h:1133
Definition DynamicMesh3.h:108
Definition MeshSurfacePointSampling.h:153
int32 SamplingMethodVersion
Definition MeshSurfacePointSampling.h:181
TArray< double > VertexWeights
Definition MeshSurfacePointSampling.h:212
int32 MaxSubSamplePoints
Definition MeshSurfacePointSampling.h:172
double MaxSampleRadius
Definition MeshSurfacePointSampling.h:189
ESizeDistribution SizeDistribution
Definition MeshSurfacePointSampling.h:202
uint32 MaxSamples
Definition MeshSurfacePointSampling.h:163
double SizeDistributionPower
Definition MeshSurfacePointSampling.h:205
double SubSampleDensity
Definition MeshSurfacePointSampling.h:166
TArray< FFrame3d > Samples
Definition MeshSurfacePointSampling.h:247
FGeometryResult Result
Definition MeshSurfacePointSampling.h:244
EInterpretWeightMode
Definition MeshSurfacePointSampling.h:216
double SampleRadius
Definition MeshSurfacePointSampling.h:160
bool bUseVertexWeights
Definition MeshSurfacePointSampling.h:209
int32 RandomSeed
Definition MeshSurfacePointSampling.h:169
bool bComputeBarycentrics
Definition MeshSurfacePointSampling.h:231
TArray< int32 > TriangleIDs
Definition MeshSurfacePointSampling.h:253
EInterpretWeightMode InterpretWeightMode
Definition MeshSurfacePointSampling.h:225
TArray< double > Radii
Definition MeshSurfacePointSampling.h:250
bool bInvertWeights
Definition MeshSurfacePointSampling.h:228
GEOMETRYCORE_API void ComputePoissonSampling(const FDynamicMesh3 &Mesh, FProgressCancel *Progress=nullptr)
Definition MeshSurfacePointSampling.cpp:718
TArray< FVector3d > BarycentricCoords
Definition MeshSurfacePointSampling.h:256
ESizeDistribution
Definition MeshSurfacePointSampling.h:193
Definition MeshSurfacePointSampling.h:86
TMeshUniformSurfaceSampling(const TMeshType &Mesh)
Definition MeshSurfacePointSampling.h:90
bool IsValid() const
Definition MeshSurfacePointSampling.h:122
bool Init(const TMeshType &Mesh)
Definition MeshSurfacePointSampling.h:96
int32 DrawTriangleIDSample(RealType UniformRandom1, RealType UniformRandom2) const
Definition MeshSurfacePointSampling.h:134
Definition MeshSurfacePointSampling.h:24
TWeightedSamplingAliasTable(TConstArrayView< RealType > Weights)
Definition MeshSurfacePointSampling.h:41
TWeightedSamplingAliasTable(TConstArrayView< RealType > Weights, RealType SumOfValidWeights, bool bAllowInvalidWeights)
Definition MeshSurfacePointSampling.h:37
int32 Num() const
Definition MeshSurfacePointSampling.h:57
bool IsValid() const
Definition MeshSurfacePointSampling.h:62
int32 Sample(RealType UniformRandom1, RealType UniformRandom2) const
Definition MeshSurfacePointSampling.h:68
RealType Area(const TVector< RealType > &V0, const TVector< RealType > &V1, const TVector< RealType > &V2)
Definition VectorUtil.h:93
@ Area
Definition FitOrientedBox2.h:17
Definition AdvancedWidgetsModule.cpp:13
Definition NumericLimits.h:41
Definition ProgressCancel.h:99
Definition Vector.h:51