UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SimpleCellGrid.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"
8
10{
13
16 {
17 }
18
19 bool operator==(const FGridSize2D& Other) const
20 {
21 return Width == Other.Width && Height == Other.Height;
22 }
23};
24
26template<typename CellType, int InvalidCellValue = 0>
28{
30
36
37protected:
39
40public:
48
54 bool Init(const float InCellSize, const FBox& Bounds)
55 {
56 if (InCellSize <= 0.0f || !Bounds.IsValid)
57 {
58 return false;
59 }
60
62
63 const FVector RealBoundsSize = Bounds.GetSize();
66 Origin = FVector(Bounds.Min.X, Bounds.Min.Y, (Bounds.Min.Z + Bounds.Max.Z) * 0.5f);
68
71 ensureMsgf(CellCount == TempCellCount, TEXT("Grid width and height are too big."));
73
74 return true;
75 }
76
84 bool Init(const float InCellSize, const FGridSize2D& InGridSize, const FVector& InOrigin, const float VerticalBoundSize)
85 {
86 if (InCellSize < 0 || InGridSize.Height == 0 || InGridSize.Width == 0 || VerticalBoundSize < 0)
87 {
88 return false;
89 }
90
96
99 ensureMsgf(CellCount == TempCellCount, TEXT("Grid width and height are too big."));
101
102 return true;
103 }
104
107 {
108 if (VerticalInterval.IsValid())
109 {
111 Origin.Z = VerticalInterval.Interpolate(0.5);
113 }
114 }
115
120
121 inline bool IsValid() const
122 {
123 return Cells.Num() && GridCellSize > 0;
124 }
125
126 inline bool IsValidIndex(const int32 CellIndex) const
127 {
128 return Cells.IsValidIndex(CellIndex);
129 }
130
132 {
133 return (LocationX >= 0) && (LocationX < (int32)GridSize.Width) && (LocationY >= 0) && (LocationY < (int32)GridSize.Height);
134 }
135
136 inline bool IsValidCoord(const FIntVector& CellCoords) const
137 {
139 }
140
142 {
144 }
145
147 inline FIntVector GetCellCoordsUnsafe(const FVector& WorldLocation) const
148 {
149 return FIntVector(
150 IntCastChecked<int32>(FMath::TruncToInt((WorldLocation.X - Origin.X) / GridCellSize)),
151 IntCastChecked<int32>(FMath::TruncToInt((WorldLocation.Y - Origin.Y) / GridCellSize)),
152 0);
153 }
154
156 FIntVector GetCellCoords(const FVector& WorldLocation) const
157 {
158 const FIntVector UnsafeCoords = GetCellCoordsUnsafe(WorldLocation);
160 }
161
163 inline int32 GetCellCoordX(int32 CellIndex) const
164 {
165 return CellIndex / GridSize.Height;
166 }
167
169 inline int32 GetCellCoordY(int32 CellIndex) const
170 {
171 return CellIndex % GridSize.Height;
172 }
173
175 inline FIntVector GetCellCoords(int32 CellIndex) const
176 {
177 return FIntVector(GetCellCoordX(CellIndex), GetCellCoordY(CellIndex), 0);
178 }
179
181 int32 GetCellIndexUnsafe(const FVector& WorldLocation) const
182 {
183 const FIntVector CellCoords = GetCellCoordsUnsafe(WorldLocation);
185 }
186
189 {
191 }
192
198
204
206 int32 GetCellIndex(const FVector& WorldLocation) const
207 {
208 const FIntVector CellCoords = GetCellCoordsUnsafe(WorldLocation);
210 }
211
213 inline FBox GetWorldCellBox(int32 CellIndex) const
214 {
215 return GetWorldCellBox(GetCellCoordX(CellIndex), GetCellCoordY(CellIndex));
216 }
217
226
228 inline FBox2D GetWorldCellBox2D(int32 CellIndex) const
229 {
230 return GetWorldCellBox2D(GetCellCoordX(CellIndex), GetCellCoordY(CellIndex));
231 }
232
241
244 {
245 return FBox(
246 Origin + FVector(CellRect.Min.X * GridCellSize, CellRect.Min.Y * GridCellSize, -BoundsSize.Z * 0.5f),
247 Origin + FVector((CellRect.Max.X+1) * GridCellSize, (CellRect.Max.Y+1) * GridCellSize, BoundsSize.Z * 0.5f)
248 );
249 }
250
253 {
254 if (!WorldBox.IsValid)
255 {
256 return FIntRect();
257 }
258
261 return FIntRect(CellMin.X, CellMin.Y, CellMax.X, CellMax.Y);
262 }
263
266 {
267 return FIntRect(0, 0, GridSize.Width-1, GridSize.Height-1);
268 }
269
270 inline FVector GetWorldCellCenter(int32 CellIndex) const
271 {
272 return GetWorldCellCenter(GetCellCoordX(CellIndex), GetCellCoordY(CellIndex));
273 }
274
276 {
277 return Origin + FVector((LocationX + 0.5f) * GridCellSize, (LocationY + 0.5f) * GridCellSize, 0);
278 }
279
280 const FCellType& GetCellAtWorldLocationUnsafe(const FVector& WorldLocation) const
281 {
282 const int32 CellIndex = GetCellIndexUnsafe(WorldLocation);
283 return Cells[CellIndex];
284 }
285
286 const FCellType& GetCellAtWorldLocation(const FVector& WorldLocation) const
287 {
289 const int32 CellIndex = GetCellIndex(WorldLocation);
290 return (CellIndex == INDEX_NONE) ? InvalidCellInstance : Cells[CellIndex];
291 }
292
293 inline FCellType& operator[](int32 CellIndex) { return Cells[CellIndex]; }
294 inline const FCellType& operator[](int32 CellIndex) const { return Cells[CellIndex]; }
295
296 inline FCellType& GetCellAtIndexUnsafe(int32 CellIndex) { return Cells.GetData()[CellIndex]; }
297 inline const FCellType& GetCellAtIndexUnsafe(int32 CellIndex) const { return Cells.GetData()[CellIndex]; }
298
301
302 inline int32 GetCellsCount() const
303 {
304 return Cells.Num();
305 }
306
307 inline int32 Num() const
308 {
309 return Cells.Num();
310 }
311
313 {
314 uint32 VersionNum = MAX_uint32;
315 Ar << VersionNum;
316
317 if (Ar.IsLoading())
318 {
319 if (VersionNum == MAX_uint32)
320 {
321 Ar << GridCellSize;
322 }
323 else
324 {
325 GridCellSize = VersionNum * 1.0f;
326 }
327 }
328 else
329 {
330 Ar << GridCellSize;
331 }
332
333 Ar << Origin;
334 Ar << BoundsSize;
335 Ar << GridSize.Width << GridSize.Height;
336
338
339 if (VersionNum == MAX_uint32)
340 {
341 Ar << Cells;
342 }
343 else
344 {
346 Ar << DataBytesCount;
347
348 if (DataBytesCount > 0)
349 {
350 if (Ar.IsLoading())
351 {
354 ensureMsgf(CellCount == TempCellCount, TEXT("Grid width and height are too big."));
356
358 }
359
361 }
362 }
363 }
364
369
371 {
372 Cells.Empty();
373 }
374
375 void Zero()
376 {
377 Cells.Reset();
379 }
380
381 void CleanUp()
382 {
383 Cells.Empty();
384 GridCellSize = 0;
386 }
387};
#define ensureMsgf( InExpression, InFormat,...)
Definition AssertionMacros.h:465
@ INDEX_NONE
Definition CoreMiscDefines.h:150
@ ForceInitToZero
Definition CoreMiscDefines.h:156
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define FVector
Definition IOSSystemIncludes.h:8
UE::Math::TIntRect< int32 > FIntRect
Definition MathFwd.h:133
UE::Math::TBox< double > FBox
Definition MathFwd.h:55
UE::Math::TVector2< double > FVector2D
Definition MathFwd.h:48
FInt32Vector3 FIntVector
Definition MathFwd.h:115
UE::Math::TBox2< double > FBox2D
Definition MathFwd.h:56
#define MAX_uint32
Definition NumericLimits.h:21
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Archive.h:1208
virtual void Serialize(void *V, int64 Length)
Definition Archive.h:1689
UE_FORCEINLINE_HINT bool IsLoading() const
Definition Archive.h:236
Definition Array.h:670
UE_REWRITE SizeType Num() const
Definition Array.h:1144
void Reset(SizeType NewSize=0)
Definition Array.h:2246
UE_NODEBUG UE_FORCEINLINE_HINT ElementType * GetData() UE_LIFETIMEBOUND
Definition Array.h:1027
SizeType AddDefaulted()
Definition Array.h:2795
typename InAllocatorType::SizeType SizeType
Definition Array.h:675
void SetNum(SizeType NewNum, EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:2308
UE_NODEBUG UE_FORCEINLINE_HINT bool IsValidIndex(SizeType Index) const
Definition Array.h:1122
SizeType AddZeroed()
Definition Array.h:2755
UE_NODEBUG UE_FORCEINLINE_HINT SIZE_T GetAllocatedSize(void) const
Definition Array.h:1059
void Empty(SizeType Slack=0)
Definition Array.h:2273
Definition SimpleCellGrid.h:10
FGridSize2D(uint32 InWidth=0, uint32 InHeight=0)
Definition SimpleCellGrid.h:14
uint32 Width
Definition SimpleCellGrid.h:11
bool operator==(const FGridSize2D &Other) const
Definition SimpleCellGrid.h:19
uint32 Height
Definition SimpleCellGrid.h:12
static constexpr UE_FORCEINLINE_HINT T Clamp(const T X, const T MinValue, const T MaxValue)
Definition UnrealMathUtility.h:592
Definition NumericLimits.h:41
Definition SimpleCellGrid.h:28
const FCellType & operator[](int32 CellIndex) const
Definition SimpleCellGrid.h:294
int32 Num() const
Definition SimpleCellGrid.h:307
FCellType & GetCellAtIndexUnsafe(int32 CellIndex)
Definition SimpleCellGrid.h:296
FVector GetWorldCellCenter(int32 LocationX, int32 LocationY) const
Definition SimpleCellGrid.h:275
bool IsValidCoord(int32 LocationX, int32 LocationY) const
Definition SimpleCellGrid.h:131
void UpdateWorldBounds()
Definition SimpleCellGrid.h:116
FVector BoundsSize
Definition SimpleCellGrid.h:34
FBox2D GetWorldCellBox2D(int32 LocationX, int32 LocationY) const
Definition SimpleCellGrid.h:234
int32 GetCellIndex(const FVector &WorldLocation) const
Definition SimpleCellGrid.h:206
TSimpleCellGrid()
Definition SimpleCellGrid.h:41
const FCellType & GetCellAtIndexUnsafe(int32 CellIndex) const
Definition SimpleCellGrid.h:297
FIntRect GetGridRectangle() const
Definition SimpleCellGrid.h:265
FVector GetWorldCellCenter(int32 CellIndex) const
Definition SimpleCellGrid.h:270
int32 GetCellIndexUnsafe(int32 LocationX, int32 LocationY) const
Definition SimpleCellGrid.h:194
void Serialize(FArchive &Ar)
Definition SimpleCellGrid.h:312
bool IsValid() const
Definition SimpleCellGrid.h:121
bool Init(const float InCellSize, const FGridSize2D &InGridSize, const FVector &InOrigin, const float VerticalBoundSize)
Definition SimpleCellGrid.h:84
FIntVector GetCellCoords(const FVector &WorldLocation) const
Definition SimpleCellGrid.h:156
int32 GetCellCoordY(int32 CellIndex) const
Definition SimpleCellGrid.h:169
FCellType & GetCellAtCoordsUnsafe(int32 LocationX, int32 LocationY)
Definition SimpleCellGrid.h:299
FBox GetWorldCellRectangleBox(const FIntRect &CellRect) const
Definition SimpleCellGrid.h:243
const FCellType & GetCellAtWorldLocationUnsafe(const FVector &WorldLocation) const
Definition SimpleCellGrid.h:280
int32 GetCellsCount() const
Definition SimpleCellGrid.h:302
void CleanUp()
Definition SimpleCellGrid.h:381
void Zero()
Definition SimpleCellGrid.h:375
bool IsValidCoord(const FIntVector &CellCoords) const
Definition SimpleCellGrid.h:136
FVector Origin
Definition SimpleCellGrid.h:33
int32 GetCellIndex(int32 LocationX, int32 LocationY) const
Definition SimpleCellGrid.h:200
float GridCellSize
Definition SimpleCellGrid.h:31
FBox GetWorldCellBox(int32 LocationX, int32 LocationY) const
Definition SimpleCellGrid.h:219
FBox GetWorldCellBox(int32 CellIndex) const
Definition SimpleCellGrid.h:213
const FCellType & GetCellAtCoordsUnsafe(int32 LocationX, int32 LocationY) const
Definition SimpleCellGrid.h:300
TArray< FCellType > Cells
Definition SimpleCellGrid.h:38
FGridSize2D GridSize
Definition SimpleCellGrid.h:35
uint32 GetAllocatedSize() const
Definition SimpleCellGrid.h:141
int32 GetCellIndexUnsafe(const FVector &WorldLocation) const
Definition SimpleCellGrid.h:181
int32 GetCellIndexUnsafe(const FIntVector &CellCoords) const
Definition SimpleCellGrid.h:188
FBox2D GetWorldCellBox2D(int32 CellIndex) const
Definition SimpleCellGrid.h:228
FIntRect GetCellRectangleFromBox(const FBox &WorldBox) const
Definition SimpleCellGrid.h:252
bool Init(const float InCellSize, const FBox &Bounds)
Definition SimpleCellGrid.h:54
CellType FCellType
Definition SimpleCellGrid.h:29
void FreeMemory()
Definition SimpleCellGrid.h:370
FBox WorldBounds
Definition SimpleCellGrid.h:32
FIntVector GetCellCoordsUnsafe(const FVector &WorldLocation) const
Definition SimpleCellGrid.h:147
const FCellType & GetCellAtWorldLocation(const FVector &WorldLocation) const
Definition SimpleCellGrid.h:286
void AllocateMemory()
Definition SimpleCellGrid.h:365
void SetVerticalInterval(const FFloatInterval &VerticalInterval)
Definition SimpleCellGrid.h:106
FIntVector GetCellCoords(int32 CellIndex) const
Definition SimpleCellGrid.h:175
bool IsValidIndex(const int32 CellIndex) const
Definition SimpleCellGrid.h:126
int32 GetCellCoordX(int32 CellIndex) const
Definition SimpleCellGrid.h:163
FCellType & operator[](int32 CellIndex)
Definition SimpleCellGrid.h:293
uint8 IsValid
Definition Box.h:45
TVector< T > GetSize() const
Definition Box.h:420
TVector< T > Min
Definition Box.h:39
TVector< T > Max
Definition Box.h:42
T Z
Definition Vector.h:68
T Y
Definition Vector.h:65
T X
Definition Vector.h:62