UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
RenderingSpatialHash.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
8{
9
15template <typename ScalarType>
17{
21
22 inline TLocation() {};
23
24 template <typename ScalarBType>
25 inline explicit TLocation(const TLocation< ScalarBType>& InLoc)
26 : Coord(UE::Math::TIntVector3<ScalarType>(InLoc.Coord))
28 {
29 }
30
31 template <typename ScalarBType>
33 : Coord(UE::Math::TIntVector3<ScalarType>(InCoord))
34 , Level(InLevel)
35 {
36 }
37
39 {
40 using UScalarType = std::make_unsigned_t<ScalarType>;
41
42 return uint32((UScalarType)CellLocation.Coord.X * 1150168907 + (UScalarType)CellLocation.Coord.Y * 1235029793 + (UScalarType)CellLocation.Coord.Z * 1282581571 + (UScalarType)CellLocation.Level * 1264559321);
43 }
44
45 inline bool operator == (const TLocation& Other) const
46 {
47 return Level == Other.Level && Coord == Other.Coord;
48 }
49
50 inline bool operator!=(const TLocation& Other) const
51 {
52 return Level != Other.Level || Coord != Other.Coord;
53 }
54
55 inline TLocation operator+(const TLocation& RHS) const
56 {
57 checkSlow(Level == RHS.Level);
58 TLocation Result;
59 Result.Coord = Coord + RHS.Coord;
60 Result.Level = Level;
61 return Result;
62 }
63
64
65 inline TLocation operator-(const TLocation& RHS) const
66 {
67 checkSlow(Level == RHS.Level);
68 TLocation Result;
69 Result.Coord = Coord - RHS.Coord;
70 Result.Level = Level;
71 return Result;
72 }
73};
74
78
79inline int32 CalcLevel(double Size)
80{
81 // TODO: using integer Log2 breaks down for small scales, where the level would need to go negative. It is however far faster and not expected to be useful.
82 return int32(FMath::FloorLog2(uint32(Size)));
83};
84
85inline int32 CalcLevel(float Size)
86{
87 // TODO: using integer Log2 breaks down for small scales, where the level would need to go negative. It is however far faster and not expected to be useful.
88 return int32(FMath::FloorLog2(uint32(Size)));
89};
90
91inline int32 CalcLevelFromRadius(double Radius)
92{
93 return CalcLevel(Radius * 2.0);
94};
95
96inline double GetCellSize(int32 Level)
97{
98 checkSlow(Level >= 0);
99 return double(1ull << uint32(Level + 1));
100};
101
102inline double GetRecCellSize(int32 Level)
103{
104 return 1.0 / GetCellSize(Level);
105}
106
108{
109 FLocation64 Result;
110 double RecLevelCellSize = GetRecCellSize(Level);
111 Result.Level = Level;
113 Result.Coord = FLocation64::FIntVector3(FMath::FloorToInt(LevelGridPos.X), FMath::FloorToInt(LevelGridPos.Y), FMath::FloorToInt(LevelGridPos.Z));
114 return Result;
115};
116
118{
119 // Can't be lower than this, or the footprint might be larger than 2x2x2, globally the same, can pre-calc.
120 int32 Level = CalcLevelFromRadius(BoxSphereBounds.SphereRadius);
121 return ToCellLoc(Level, BoxSphereBounds.Origin);
122};
123
125{
126 // Can't be lower than this, or the footprint might be larger than 2x2x2, globally the same, can pre-calc.
128 return ToCellLoc(Level, FVector(Sphere));
129};
130
131inline FLocation64 CalcLevelAndLocationClamped(const FVector3d& Center, double Radius, int32 FirstLevel)
132{
133 // Can't be lower than this, or the footprint might be larger than 2x2x2, globally the same, can pre-calc.
134 int32 Level = CalcLevelFromRadius(Radius);
135 Level = FMath::Max(Level, FirstLevel);
136 return ToCellLoc(Level, Center);
137};
138
140{
141 return FVector3d(Loc.Coord) * GetCellSize(Loc.Level);
142}
143
145{
146 int32 LevelDelta = Level - Loc.Level;
148 ResLoc.Level = Level;
149 if (LevelDelta > 0)
150 {
151 ResLoc.Coord = ResLoc.Coord >> LevelDelta;
152 }
153 else if (LevelDelta < 0)
154 {
155 ResLoc.Coord = ResLoc.Coord << -LevelDelta;
156 }
157 return ResLoc;
158};
159
160};
#define checkSlow(expr)
Definition AssertionMacros.h:332
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 FVector
Definition IOSSystemIncludes.h:8
UE::Math::TVector< double > FVector3d
Definition MathFwd.h:60
uint32 Size
Definition VulkanMemory.cpp:4034
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition InstanceDataUpdateUtils.h:439
int32 CalcLevelFromRadius(double Radius)
Definition RenderingSpatialHash.h:91
FLocation64 ToCellLoc(int32 Level, const FVector &WorldPos)
Definition RenderingSpatialHash.h:107
FLocation64 ToLevel(const FLocation64 &Loc, int32 Level)
Definition RenderingSpatialHash.h:144
FVector3d CalcWorldPosition(const FLocation64 &Loc)
Definition RenderingSpatialHash.h:139
FLocation64 CalcLevelAndLocation(const FBoxSphereBounds &BoxSphereBounds)
Definition RenderingSpatialHash.h:117
int32 CalcLevel(double Size)
Definition RenderingSpatialHash.h:79
double GetRecCellSize(int32 Level)
Definition RenderingSpatialHash.h:102
FLocation64 CalcLevelAndLocationClamped(const FVector3d &Center, double Radius, int32 FirstLevel)
Definition RenderingSpatialHash.h:131
double GetCellSize(int32 Level)
Definition RenderingSpatialHash.h:96
Definition AdvancedWidgetsModule.cpp:13
Definition RenderingSpatialHash.h:17
UE::Math::TIntVector3< ScalarType > FIntVector3
Definition RenderingSpatialHash.h:18
FIntVector3 Coord
Definition RenderingSpatialHash.h:19
bool operator!=(const TLocation &Other) const
Definition RenderingSpatialHash.h:50
TLocation(const UE::Math::TIntVector3< ScalarBType > &InCoord, int32 InLevel)
Definition RenderingSpatialHash.h:32
TLocation()
Definition RenderingSpatialHash.h:22
TLocation operator+(const TLocation &RHS) const
Definition RenderingSpatialHash.h:55
TLocation(const TLocation< ScalarBType > &InLoc)
Definition RenderingSpatialHash.h:25
friend uint32 GetTypeHash(const TLocation &CellLocation)
Definition RenderingSpatialHash.h:38
TLocation operator-(const TLocation &RHS) const
Definition RenderingSpatialHash.h:65
bool operator==(const TLocation &Other) const
Definition RenderingSpatialHash.h:45
int32 Level
Definition RenderingSpatialHash.h:20
Definition BoxSphereBounds.h:25