UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
PointHashGrid2.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// Port of geometry3Sharp PointHashGrid2
4
5#pragma once
6
7#include "CoreMinimal.h"
8#include "Misc/ScopeLock.h"
10
11namespace UE
12{
13namespace Geometry
14{
15
16using namespace UE::Math;
17
30template<typename PointDataType, typename RealType>
32{
33private:
35 FCriticalSection CriticalSection;
37 PointDataType InvalidValue;
38
39public:
40
46 TPointHashGrid2(RealType cellSize, PointDataType InvalidValue) : Indexer(cellSize), InvalidValue(InvalidValue)
47 {
48 }
49
55 {
56 Hash.Reserve(Num);
57 }
58
61 {
62 return InvalidValue;
63 }
64
71 {
72 FVector2i idx = Indexer.ToGrid(Position);
73 {
74 FScopeLock Lock(&CriticalSection);
75 Hash.Add(idx, Value);
76 }
77 }
78
85 {
86 FVector2i idx = Indexer.ToGrid(Position);
87 Hash.Add(idx, Value);
88 }
89
90
98 {
99 FVector2i idx = Indexer.ToGrid(Position);
100 {
101 FScopeLock Lock(&CriticalSection);
102 return Hash.RemoveSingle(idx, Value) > 0;
103 }
104 }
105
113 {
114 FVector2i idx = Indexer.ToGrid(Position);
115 return Hash.RemoveSingle(idx, Value) > 0;
116 }
117
118
126 {
127 FVector2i old_idx = Indexer.ToGrid(OldPosition);
129 if (old_idx == new_idx)
130 {
131 return;
132 }
133 bool bWasAtOldPos;
134 {
135 FScopeLock Lock(&CriticalSection);
136 bWasAtOldPos = Hash.RemoveSingle(old_idx, Value) > 0;
137 }
139 {
140 FScopeLock Lock(&CriticalSection);
141 Hash.Add(new_idx, Value);
142 }
143 return;
144 }
145
146
154 {
155 FVector2i old_idx = Indexer.ToGrid(OldPosition);
157 if (old_idx == new_idx)
158 {
159 return;
160 }
161 bool bWasAtOldPos = Hash.RemoveSingle(old_idx, Value) > 0;
163 Hash.Add(new_idx, Value);
164 return;
165 }
166
167
177 const TVector2<RealType>& QueryPoint, RealType Radius,
178 TFunctionRef<RealType(const PointDataType&)> DistanceSqFunc,
179 TFunctionRef<bool(const PointDataType&)> IgnoreFunc = [](const PointDataType& data) { return false; }) const
180 {
181 if (!Hash.Num())
182 {
184 }
185
186 FVector2i min_idx = Indexer.ToGrid(QueryPoint - Radius * TVector2<RealType>::One());
187 FVector2i max_idx = Indexer.ToGrid(QueryPoint + Radius * TVector2<RealType>::One());
188
191 RealType RadiusSquared = Radius * Radius;
192
194 for (int yi = min_idx.Y; yi <= max_idx.Y; yi++)
195 {
196 for (int xi = min_idx.X; xi <= max_idx.X; xi++)
197 {
198 FVector2i idx(xi, yi);
199 Values.Reset();
200 Hash.MultiFind(idx, Values);
201 for (PointDataType Value : Values)
202 {
203 if (IgnoreFunc(Value))
204 {
205 continue;
206 }
207 RealType distsq = DistanceSqFunc(Value);
209 {
210 nearest = Value;
212 }
213 }
214 }
215 }
216
218 }
219};
220
221template <typename PointDataType> using TPointHashGrid2d = TPointHashGrid2<PointDataType, double>;
222template <typename PointDataType> using TPointHashGrid2f = TPointHashGrid2<PointDataType, float>;
223
224} // end namespace UE::Geometry
225} // 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
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
@ Num
Definition MetalRHIPrivate.h:234
FRWLock Lock
Definition UnversionedPropertySerialization.cpp:921
Definition ScopeLock.h:141
Definition Array.h:670
void Reset(SizeType NewSize=0)
Definition Array.h:2246
Definition AssetRegistryState.h:50
Definition PointHashGrid2.h:32
void InsertPointUnsafe(const PointDataType &Value, const TVector2< RealType > &Position)
Definition PointHashGrid2.h:84
bool RemovePoint(const PointDataType &Value, const TVector2< RealType > &Position)
Definition PointHashGrid2.h:97
PointDataType GetInvalidValue() const
Definition PointHashGrid2.h:60
bool RemovePointUnsafe(const PointDataType &Value, const TVector2< RealType > &Position)
Definition PointHashGrid2.h:112
TPointHashGrid2(RealType cellSize, PointDataType InvalidValue)
Definition PointHashGrid2.h:46
void Reserve(int32 Num)
Definition PointHashGrid2.h:54
void UpdatePoint(const PointDataType &Value, const TVector2< RealType > &OldPosition, const TVector2< RealType > &NewPosition)
Definition PointHashGrid2.h:125
void UpdatePointUnsafe(const PointDataType &Value, const TVector2< RealType > &OldPosition, const TVector2< RealType > &NewPosition)
Definition PointHashGrid2.h:153
TPair< PointDataType, RealType > FindNearestInRadius(const TVector2< RealType > &QueryPoint, RealType Radius, TFunctionRef< RealType(const PointDataType &)> DistanceSqFunc, TFunctionRef< bool(const PointDataType &)> IgnoreFunc=[](const PointDataType &data) { return false;}) const
Definition PointHashGrid2.h:176
void InsertPoint(const PointDataType &Value, const TVector2< RealType > &Position)
Definition PointHashGrid2.h:70
Definition Sphere.cpp:10
Definition AdvancedWidgetsModule.cpp:13
Definition NumericLimits.h:41
Definition Tuple.h:652
Definition IntVectorTypes.h:20
Definition GridIndexing2.h:23
FVector2i ToGrid(const TVector2< RealType > &P) const
Definition GridIndexing2.h:38
Definition Vector2D.h:38