UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
WorldPartitionEditorSpatialHash.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "CoreMinimal.h"
6#include "UObject/Object.h"
7#include "Misc/HashBuilder.h"
10#include "WorldPartitionEditorSpatialHash.generated.h"
11
12UCLASS(MinimalAPI)
14{
16
17#if WITH_EDITOR
19
20 struct FCell
21 {
22 public:
23 FCell()
24 : Bounds(ForceInitToZero)
25 {}
26
27 FBox Bounds;
29 };
30
31 struct FCellCoord
32 {
33 FCellCoord(int64 InX, int64 InY, int64 InZ, int32 InLevel)
34 : X(InX)
35 , Y(InY)
36 , Z(InZ)
37 , Level(InLevel)
38 {}
39
40 int64 X;
41 int64 Y;
42 int64 Z;
43 int32 Level;
44
45 inline uint32 GetChildIndex() const
46 {
47 return ((X & 1) << 2) | ((Y & 1) << 1) | (Z & 1);
48 }
49
50 inline FCellCoord GetChildCellCoord(uint32 ChildIndex) const
51 {
52 check(Level);
53 check(ChildIndex < 8);
54
55 return FCellCoord(
56 (X << 1) | (ChildIndex >> 2),
57 (Y << 1) | ((ChildIndex >> 1) & 1),
58 (Z << 1) | (ChildIndex & 1),
59 Level - 1
60 );
61 }
62
63 inline FCellCoord GetParentCellCoord() const
64 {
65 return FCellCoord(X >> 1, Y >> 1, Z >> 1, Level + 1);
66 }
67
68 bool operator==(const FCellCoord& Other) const
69 {
70 return (X == Other.X) && (Y == Other.Y) && (Z == Other.Z) && (Level == Other.Level);
71 }
72
73 friend uint32 GetTypeHash(const FCellCoord& CellCoord)
74 {
75 FHashBuilder HashBuilder;
76 HashBuilder << CellCoord.X << CellCoord.Y << CellCoord.Z << CellCoord.Level;
77 return HashBuilder.GetHash();
78 }
79 };
80
81 inline FCellCoord GetCellCoords(const FVector& InPos, int32 Level) const
82 {
83 check(Level >= 0);
84 const int64 CellSizeForLevel = (int64)CellSize * (1LL << Level);
85 return FCellCoord(
86 FMath::FloorToInt(InPos.X / CellSizeForLevel),
87 FMath::FloorToInt(InPos.Y / CellSizeForLevel),
88 FMath::FloorToInt(InPos.Z / CellSizeForLevel),
89 Level
90 );
91 }
92
93 inline FBox GetCellBounds(const FCellCoord& InCellCoord) const
94 {
95 check(InCellCoord.Level >= 0);
96 const int64 CellSizeForLevel = (int64)CellSize * (1LL << InCellCoord.Level);
97 const FVector Min = FVector(
101 );
102 const FVector Max = Min + FVector(static_cast<double>(CellSizeForLevel));
103 return FBox(Min, Max);
104 }
105
106 inline int32 GetLevelForBox(const FBox& Box) const
107 {
108 const FVector Extent = Box.GetExtent();
109 const FVector::FReal MaxLength = Extent.GetMax() * 2.0;
110 return FMath::CeilToInt32(FMath::Max<FVector::FReal>(FMath::Log2(MaxLength / CellSize), 0));
111 }
112
113 inline int32 ForEachIntersectingCells(const FBox& InBounds, int32 Level, TFunctionRef<void(const FCellCoord&)> InOperation) const
114 {
115 int32 NumCells = 0;
116
117 FCellCoord MinCellCoords(GetCellCoords(InBounds.Min, Level));
118 FCellCoord MaxCellCoords(GetCellCoords(InBounds.Max, Level));
119
120 for (int64 z=MinCellCoords.Z; z<=MaxCellCoords.Z; z++)
121 {
122 for (int64 y=MinCellCoords.Y; y<=MaxCellCoords.Y; y++)
123 {
124 for (int64 x=MinCellCoords.X; x<=MaxCellCoords.X; x++)
125 {
126 InOperation(FCellCoord(x, y, z, Level));
127 NumCells++;
128 }
129 }
130 }
131
132 return NumCells;
133 }
134
135 struct FCellNode
136 {
137 FCellNode()
138 : ChildNodesMask(0)
139 {}
140
141 inline bool HasChildNodes() const
142 {
143 return !!ChildNodesMask;
144 }
145
146 inline bool HasChildNode(uint32 ChildIndex) const
147 {
148 check(ChildIndex < 8);
149 return !!(ChildNodesMask & (1 << ChildIndex));
150 }
151
152 inline void AddChildNode(uint32 ChildIndex)
153 {
154 check(ChildIndex < 8);
155 uint32 ChildMask = 1 << ChildIndex;
156 check(!(ChildNodesMask & ChildMask));
157 ChildNodesMask |= ChildMask;
158 }
159
160 inline void RemoveChildNode(uint32 ChildIndex)
161 {
162 check(ChildIndex < 8);
163 uint32 ChildMask = 1 << ChildIndex;
164 check(ChildNodesMask & ChildMask);
166 }
167
168 inline void ForEachChild(TFunctionRef<void(uint32 ChildIndex)> InOperation) const
169 {
171
172 while(CurChildNodesMask)
173 {
174 const int32 ChildIndex = FMath::CountTrailingZeros(CurChildNodesMask);
175
176 check(CurChildNodesMask & (1 << ChildIndex));
177 CurChildNodesMask &= ~(1 << ChildIndex);
178
179 InOperation(ChildIndex);
180 }
181 }
182
184 };
185
186public:
188
189 // UWorldPartitionEditorHash interface begin
190 ENGINE_API virtual void Initialize() override;
191 ENGINE_API virtual void SetDefaultValues() override;
192 ENGINE_API virtual FName GetWorldPartitionEditorName() const override;
193 ENGINE_API virtual FBox GetEditorWorldBounds() const override;
194 ENGINE_API virtual FBox GetRuntimeWorldBounds() const override;
195 ENGINE_API virtual FBox GetNonSpatialBounds() const override;
196 ENGINE_API virtual void Tick(float DeltaSeconds) override;
197
200
201 UE_DEPRECATED(5.4, "Use ForEachIntersectingActor with FWorldPartitionActorDescInstance")
203
205 // UWorldPartitionEditorHash interface end
206#endif
207
208#if WITH_EDITORONLY_DATA
209private:
211 ENGINE_API int32 ForEachIntersectingCellInner(const FBox& Box, const FCellCoord& CellCoord, TFunctionRef<void(FCell*)> InOperation, int32 MinimumLevel = 0);
212
213 UPROPERTY(Config)
214 int32 CellSize;
215
216 // Dynamic sparse octree structure
220
221 TSet<FCell*> Cells;
223
227 bool bBoundsDirty;
228
229#if DO_CHECK
231#endif
232
233public:
234 UPROPERTY(Config, meta = (AllowedClasses = "/Script/Engine.Texture2D, /Script/Engine.MaterialInterface"))
236
237 UPROPERTY(Config)
239
240 UPROPERTY(Config)
242#endif
243};
#define check(expr)
Definition AssertionMacros.h:314
@ ForceInitToZero
Definition CoreMiscDefines.h:156
#define UE_DEPRECATED(Version, Message)
Definition CoreMiscDefines.h:302
FPlatformTypes::int64 int64
A 64-bit signed integer.
Definition Platform.h:1127
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
#define X(Name, Desc)
Definition FormatStringSan.h:47
#define FVector
Definition IOSSystemIncludes.h:8
UE::Math::TBox< double > FBox
Definition MathFwd.h:55
#define UPROPERTY(...)
UObject definition macros.
Definition ObjectMacros.h:744
#define GENERATED_UCLASS_BODY(...)
Definition ObjectMacros.h:768
#define UCLASS(...)
Definition ObjectMacros.h:776
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition HashBuilder.h:18
UE_FORCEINLINE_HINT uint32 GetHash() const
Definition HashBuilder.h:87
Definition NameTypes.h:617
Definition WorldPartitionActorDescInstance.h:18
Definition WorldPartitionActorDesc.h:282
Definition Array.h:670
Definition AssetRegistryState.h:50
Definition UnrealString.h.inl:34
Definition UniquePtr.h:107
Definition WorldPartitionEditorHash.h:13
Definition WorldPartitionEditorSpatialHash.h:14
static float Log2(float Value)
Definition UnrealMathUtility.h:722
Definition SoftObjectPath.h:56
Definition Tuple.h:652
T GetMax() const
Definition Vector.h:1674
double FReal
Definition Vector.h:55