UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
PointHashGrid3.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Port of geometry3Sharp PointHashGrid3
4
5#pragma once
6
7#include "CoreMinimal.h"
9#include "Misc/ScopeLock.h"
10#include "Util/GridIndexing3.h"
11
12namespace UE
13{
14namespace Geometry
15{
16
17using namespace UE::Math;
18
31template<typename PointDataType, typename RealType>
33{
34private:
38 PointDataType InvalidValue;
39
40public:
41
47 TPointHashGrid3(RealType CellSize, PointDataType InvalidValue) : Indexer(FMath::Max(TMathUtil<RealType>::ZeroTolerance, CellSize)), InvalidValue(InvalidValue)
48 {
49 }
50
56 {
57 Hash.Reserve(Num);
58 }
59
64 void Reset(RealType NewCellSize)
65 {
66 Hash.Reset();
68 }
69
72 {
73 return InvalidValue;
74 }
75
82 {
83 FVector3i idx = Indexer.ToGrid(Position);
84 {
85 UE::TScopeLock Lock(Mutex);
86 Hash.Add(idx, Value);
87 }
88 }
89
96 {
97 FVector3i idx = Indexer.ToGrid(Position);
98 Hash.Add(idx, Value);
99 }
100
101
109 {
110 FVector3i idx = Indexer.ToGrid(Position);
111 {
112 UE::TScopeLock Lock(Mutex);
113 return Hash.RemoveSingle(idx, Value) > 0;
114 }
115 }
116
124 {
125 FVector3i idx = Indexer.ToGrid(Position);
126 return Hash.RemoveSingle(idx, Value) > 0;
127 }
128
129
137 {
138 FVector3i Idx = Indexer.ToGrid(Position);
139 {
140 UE::TScopeLock Lock(Mutex);
141 return !Hash.Contains(Idx);
142 }
143 }
144
145
153 {
154 FVector3i Idx = Indexer.ToGrid(Position);
155 return !Hash.Contains(Idx);
156 }
157
158
166 {
167 FVector3i old_idx = Indexer.ToGrid(OldPosition);
169 if (old_idx == new_idx)
170 {
171 return;
172 }
173 bool bWasAtOldPos;
174 {
175 UE::TScopeLock Lock(Mutex);
176 bWasAtOldPos = Hash.RemoveSingle(old_idx, Value) > 0;
177 }
179 {
180 UE::TScopeLock Lock(Mutex);
181 Hash.Add(new_idx, Value);
182 }
183 return;
184 }
185
186
194 {
195 FVector3i old_idx = Indexer.ToGrid(OldPosition);
197 if (old_idx == new_idx)
198 {
199 return;
200 }
201 bool bWasAtOldPos = Hash.RemoveSingle(old_idx, Value) > 0;
203 Hash.Add(new_idx, Value);
204 return;
205 }
206
223
225 const TVector<RealType>& QueryPoint, RealType Radius,
226 TFunctionRef<RealType(const PointDataType&)> DistanceSqFunc) const
227 {
228 return FindInRadiusHelper<false>(QueryPoint, Radius, DistanceSqFunc, [](const PointDataType& data) { return false; });
229 }
230
247
249 const TVector<RealType>& QueryPoint, RealType Radius,
250 TFunctionRef<RealType(const PointDataType&)> DistanceSqFunc) const
251 {
252 return FindInRadiusHelper<true>(QueryPoint, Radius, DistanceSqFunc, [](const PointDataType& data) { return false; });
253 }
254
255
267 const TVector<RealType>& QueryPoint, RealType Radius,
268 TFunctionRef<RealType(const PointDataType&)> DistanceSqFunc,
270 TFunctionRef<bool(const PointDataType&)> IgnoreFunc = [](const PointDataType& data) { return false; }) const
271 {
272 if (!Hash.Num())
273 {
274 return 0;
275 }
276 int32 InitialNum = ResultsOut.Num();
277
278 FVector3i min_idx = Indexer.ToGrid(QueryPoint - Radius * TVector<RealType>::One());
279 FVector3i max_idx = Indexer.ToGrid(QueryPoint + Radius * TVector<RealType>::One());
280
281 RealType RadiusSquared = Radius * Radius;
282
283 for (int zi = min_idx.Z; zi <= max_idx.Z; zi++)
284 {
285 for (int yi = min_idx.Y; yi <= max_idx.Y; yi++)
286 {
287 for (int xi = min_idx.X; xi <= max_idx.X; xi++)
288 {
289 FVector3i idx(xi, yi, zi);
290 for (typename TMultiMap<FVector3i, PointDataType>::TConstKeyIterator It = Hash.CreateConstKeyIterator(idx); It; ++It)
291 {
292 const PointDataType& Value = It.Value();
293 if (IgnoreFunc(Value))
294 {
295 continue;
296 }
297 RealType distsq = DistanceSqFunc(Value);
298 if (distsq < RadiusSquared)
299 {
300 ResultsOut.Add(Value);
301 }
302 }
303 }
304 }
305 }
306
307 return ResultsOut.Num() - InitialNum;
308 }
309
320 const TVector<RealType>& QueryPoint, RealType Radius,
321 TFunctionRef<RealType(const PointDataType&)> DistanceSqFunc,
323 TFunctionRef<bool(const PointDataType&)> IgnoreFunc = [](const PointDataType& data) { return false; }) const
324 {
325 if (!Hash.Num())
326 {
327 return;
328 }
329
330 FVector3i min_idx = Indexer.ToGrid(QueryPoint - Radius * TVector<RealType>::One());
331 FVector3i max_idx = Indexer.ToGrid(QueryPoint + Radius * TVector<RealType>::One());
332
333 RealType RadiusSquared = Radius * Radius;
334
335 for (int zi = min_idx.Z; zi <= max_idx.Z; zi++)
336 {
337 for (int yi = min_idx.Y; yi <= max_idx.Y; yi++)
338 {
339 for (int xi = min_idx.X; xi <= max_idx.X; xi++)
340 {
341 FVector3i idx(xi, yi, zi);
342 for (typename TMultiMap<FVector3i, PointDataType>::TConstKeyIterator It = Hash.CreateConstKeyIterator(idx); It; ++It)
343 {
344 const PointDataType& Value = It.Value();
345 if (IgnoreFunc(Value))
346 {
347 continue;
348 }
349 RealType DistSq = DistanceSqFunc(Value);
350 if (DistSq <= RadiusSquared)
351 {
353 {
354 return;
355 }
356 }
357 }
358 }
359 }
360 }
361 }
362
363private:
364 template<bool bEarlyOut = false>
365 TPair<PointDataType, RealType> FindInRadiusHelper(
366 const TVector<RealType>& QueryPoint, RealType Radius,
367 TFunctionRef<RealType(const PointDataType&)> DistanceSqFunc,
368 TFunctionRef<bool(const PointDataType&)> IgnoreFunc) const
369 {
370 if (!Hash.Num())
371 {
373 }
374
375 RealType MinDistSq = Radius * Radius;
377
378 auto SearchCell = [this, &Nearest, &MinDistSq, &DistanceSqFunc, &IgnoreFunc](FVector3i CellIdx)
379 {
380 bool bFound = false;
381 for (typename TMultiMap<FVector3i, PointDataType>::TConstKeyIterator It = Hash.CreateConstKeyIterator(CellIdx); It; ++It)
382 {
383 const PointDataType& Value = It.Value();
384 if (IgnoreFunc(Value))
385 {
386 continue;
387 }
388 RealType DistSq = DistanceSqFunc(Value);
389 if (DistSq < MinDistSq)
390 {
391 Nearest = Value;
393 if (bEarlyOut) {
394 return true;
395 }
396 bFound = true;
397 }
398 }
399 return bFound;
400 };
401
402 FVector3i CenterIdx = Indexer.ToGrid(QueryPoint);
403 RealType SearchRadius = Radius;
404
406 {
407 if (bEarlyOut)
408 {
410 }
411 SearchRadius = FMath::Sqrt(MinDistSq);
412 }
413
414 Indexer.IterateAcrossBounds(
415 QueryPoint - SearchRadius * TVector<RealType>::One(),
416 QueryPoint + SearchRadius * TVector<RealType>::One(), [&CenterIdx, &SearchCell](const FVector3i& Idx)
417 {
418 if (Idx == CenterIdx)
419 {
420 return true; // continue
421 }
422 bool bFound = SearchCell(Idx);
423 return !(bEarlyOut && bFound);
424 });
425
426 if (Nearest == GetInvalidValue())
427 {
429 }
431 }
432
433};
434
435template <typename PointDataType> using TPointHashGrid3d = TPointHashGrid3<PointDataType, double>;
436template <typename PointDataType> using TPointHashGrid3f = TPointHashGrid3<PointDataType, float>;
437
438} // end namespace UE::Geometry
439} // end namespace UE
#define check(expr)
Definition AssertionMacros.h:314
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
@ Num
Definition MetalRHIPrivate.h:234
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
Definition Array.h:670
Definition AssetRegistryState.h:50
Definition MathUtil.h:150
Definition Mutex.h:18
Definition PointHashGrid3.h:33
bool IsCellEmpty(const TVector< RealType > &Position)
Definition PointHashGrid3.h:136
void Reserve(int32 Num)
Definition PointHashGrid3.h:55
void Reset(RealType NewCellSize)
Definition PointHashGrid3.h:64
void InsertPointUnsafe(const PointDataType &Value, const TVector< RealType > &Position)
Definition PointHashGrid3.h:95
void UpdatePoint(const PointDataType &Value, const TVector< RealType > &OldPosition, const TVector< RealType > &NewPosition)
Definition PointHashGrid3.h:165
bool RemovePointUnsafe(const PointDataType &Value, const TVector< RealType > &Position)
Definition PointHashGrid3.h:123
void EnumeratePointsInBall(const TVector< RealType > &QueryPoint, RealType Radius, TFunctionRef< RealType(const PointDataType &)> DistanceSqFunc, TFunctionRef< bool(PointDataType, double)> ProcessPointFunc, TFunctionRef< bool(const PointDataType &)> IgnoreFunc=[](const PointDataType &data) { return false;}) const
Definition PointHashGrid3.h:319
int FindPointsInBall(const TVector< RealType > &QueryPoint, RealType Radius, TFunctionRef< RealType(const PointDataType &)> DistanceSqFunc, TArray< PointDataType > &ResultsOut, TFunctionRef< bool(const PointDataType &)> IgnoreFunc=[](const PointDataType &data) { return false;}) const
Definition PointHashGrid3.h:266
bool IsCellEmptyUnsafe(const TVector< RealType > &Position)
Definition PointHashGrid3.h:152
TPair< PointDataType, RealType > FindAnyInRadius(const TVector< RealType > &QueryPoint, RealType Radius, TFunctionRef< RealType(const PointDataType &)> DistanceSqFunc) const
Definition PointHashGrid3.h:248
TPair< PointDataType, RealType > FindAnyInRadius(const TVector< RealType > &QueryPoint, RealType Radius, TFunctionRef< RealType(const PointDataType &)> DistanceSqFunc, TFunctionRef< bool(const PointDataType &)> IgnoreFunc) const
Definition PointHashGrid3.h:240
TPair< PointDataType, RealType > FindNearestInRadius(const TVector< RealType > &QueryPoint, RealType Radius, TFunctionRef< RealType(const PointDataType &)> DistanceSqFunc) const
Definition PointHashGrid3.h:224
bool RemovePoint(const PointDataType &Value, const TVector< RealType > &Position)
Definition PointHashGrid3.h:108
PointDataType GetInvalidValue() const
Definition PointHashGrid3.h:71
TPointHashGrid3(RealType CellSize, PointDataType InvalidValue)
Definition PointHashGrid3.h:47
void UpdatePointUnsafe(const PointDataType &Value, const TVector< RealType > &OldPosition, const TVector< RealType > &NewPosition)
Definition PointHashGrid3.h:193
void InsertPoint(const PointDataType &Value, const TVector< RealType > &Position)
Definition PointHashGrid3.h:81
TPair< PointDataType, RealType > FindNearestInRadius(const TVector< RealType > &QueryPoint, RealType Radius, TFunctionRef< RealType(const PointDataType &)> DistanceSqFunc, TFunctionRef< bool(const PointDataType &)> IgnoreFunc) const
Definition PointHashGrid3.h:216
Definition ScopeLock.h:21
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
Definition UnrealMathUtility.h:270
Definition NumericLimits.h:41
Definition Tuple.h:652
Definition IntVectorTypes.h:252
Definition GridIndexing3.h:66
RealType CellSize
Definition GridIndexing3.h:68
FVector3i ToGrid(const TVector< RealType > &P) const
Definition GridIndexing3.h:84
void IterateAcrossBounds(const TVector< RealType > &RealMin, const TVector< RealType > &RealMax, TFunctionRef< bool(const FVector3i &Index)> ShouldContinue) const
Definition GridIndexing3.h:115
Definition Vector.h:51