UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SparseGrid2.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreMinimal.h"
6#include "BoxTypes.h"
7#include "IntBoxTypes.h"
8
9// 2d version of TSparseGrid3
10
11namespace UE
12{
13namespace Geometry
14{
15
25template<typename ElemType>
27{
28protected:
33
34public:
35
43
48 {
49 FreeAll();
50 }
51
52
56 {
57 Other.Elements.Reset();
58 }
60 {
61 if (this != &Other)
62 {
63 Elements = MoveTemp(Other.Elements);
64 Bounds = MoveTemp(Other.Bounds);
65 Other.Elements.Reset();
66 }
67 return *this;
68 }
69
74 bool Has(const FVector2i& Index) const
75 {
76 return Elements.Contains(Index);
77 }
78
79
85 const ElemType* Get(const FVector2i& Index) const
86 {
87 return Elements.FindRef(Index);
88 }
89
90
97 ElemType* Get(const FVector2i& Index, bool bAllocateIfMissing)
98 {
99 ElemType* result = Elements.FindRef(Index);
100 if (result != nullptr)
101 {
102 return result;
103 }
104 if (bAllocateIfMissing == true)
105 {
106 return Allocate(Index);
107 }
108 return nullptr;
109 }
110
116 bool Free(const FVector2i& Index)
117 {
118 if (Elements.Contains(Index))
119 {
120 delete Elements[Index];
121 Elements.Remove(Index);
122 return true;
123 }
124 return false;
125 }
126
130 void FreeAll()
131 {
132 for (auto pair : Elements)
133 {
134 delete pair.Value;
135 }
136 Elements.Reset();
137 }
138
142 int GetCount() const
143 {
144 return Elements.Num();
145 }
146
150 float GetDensity() const
151 {
152 return (float)Elements.Num() / (float)Bounds.Area();
153 }
154
159 {
160 return Bounds;
161 }
162
167 {
168 return Bounds.Diagonal() + FVector2i::One();
169 }
170
171
175 template<typename Func>
177 {
178 for (auto Pair : Elements)
179 {
180 ElementFunc(Pair.Value);
181 }
182 }
183
184
189 template<typename Func>
190 void RangeIteration(FVector2i MinIndex, FVector2i MaxIndex, Func ElementFunc) const
191 {
192 for (int32 yi = MinIndex.Y; yi <= MaxIndex.Y; ++yi)
193 {
194 for (int32 xi = MinIndex.X; xi <= MaxIndex.X; ++xi)
195 {
196 const ElemType* Found = Elements.FindRef(FVector2i(xi,yi));
197 if (Found != nullptr)
198 {
200 }
201 }
202 }
203 }
204
206 {
207 return Elements.GetAllocatedSize() + GetCount() * sizeof(ElemType);
208 }
209
210protected:
211
212 ElemType* Allocate(const FVector2i& index)
213 {
214 ElemType* NewElem = new ElemType();
215 Elements.Add(index, NewElem);
216 Bounds.Contain(index);
217 return NewElem;
218 }
219};
220
221} // end namespace UE::Geometry
222} // end namespace UE
FPlatformTypes::SIZE_T SIZE_T
An unsigned integer the same size as a pointer, the same as UPTRINT.
Definition Platform.h:1150
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
USkinnedMeshComponent float
Definition SkinnedMeshComponent.h:60
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
Definition UnrealString.h.inl:34
Definition SparseGrid2.h:27
ElemType * Allocate(const FVector2i &index)
Definition SparseGrid2.h:212
TSparseGrid2(TSparseGrid2 &&Other) noexcept
Definition SparseGrid2.h:55
bool Free(const FVector2i &Index)
Definition SparseGrid2.h:116
TSparseGrid2()
Definition SparseGrid2.h:39
TSparseGrid2 & operator=(const TSparseGrid2 &Other)=delete
TSparseGrid2 & operator=(TSparseGrid2 &&Other) noexcept
Definition SparseGrid2.h:59
~TSparseGrid2()
Definition SparseGrid2.h:47
void RangeIteration(FVector2i MinIndex, FVector2i MaxIndex, Func ElementFunc) const
Definition SparseGrid2.h:190
float GetDensity() const
Definition SparseGrid2.h:150
TMap< FVector2i, ElemType * > Elements
Definition SparseGrid2.h:30
bool Has(const FVector2i &Index) const
Definition SparseGrid2.h:74
SIZE_T GetAllocatedSize() const
Definition SparseGrid2.h:205
ElemType * Get(const FVector2i &Index, bool bAllocateIfMissing)
Definition SparseGrid2.h:97
void AllocatedIteration(Func ElementFunc) const
Definition SparseGrid2.h:176
FAxisAlignedBox2i GetBoundsInclusive() const
Definition SparseGrid2.h:158
TSparseGrid2(const TSparseGrid2 &Other)=delete
void FreeAll()
Definition SparseGrid2.h:130
const ElemType * Get(const FVector2i &Index) const
Definition SparseGrid2.h:85
FAxisAlignedBox2i Bounds
Definition SparseGrid2.h:32
FVector2i GetDimensions() const
Definition SparseGrid2.h:166
int GetCount() const
Definition SparseGrid2.h:142
Definition AdvancedWidgetsModule.cpp:13
U16 Index
Definition radfft.cpp:71
Definition IntBoxTypes.h:75
static FAxisAlignedBox2i Empty()
Definition IntBoxTypes.h:98
FVector2i Diagonal() const
Definition IntBoxTypes.h:147
void Contain(const FVector2i &V)
Definition IntBoxTypes.h:157
int32 Area() const
Definition IntBoxTypes.h:140
Definition IntVectorTypes.h:20
int32 X
Definition IntVectorTypes.h:25
int32 Y
Definition IntVectorTypes.h:25
static constexpr FVector2i One()
Definition IntVectorTypes.h:103