UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
SpatialHash.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Chaos/Box.h"
5#include "Chaos/Vector.h"
6
7namespace Chaos
8{
9template<class T>
11{
12 public:
13 TSpatialHash(const TArray<TVec3<T>>& Particles, const T Radius)
14 : MParticles(Particles)
15 {
16 Init(Radius);
17 }
18
19 TSpatialHash(const TArray<TVec3<T>>& Particles)
20 : MParticles(Particles)
21 {
22 Init();
23 }
24
26
27 void Update(const TArray<TVec3<T>>& Particles, const T Radius);
28 void Update(const TArray<TVec3<T>>& Particles);
29 void Update(const T Radius);
30
31 // Returns all the points in MaxRadius, result not sorted
33 // Returns all the points in MaxRadius, no more than MaxCount, result always sorted
34 TArray<int32> GetClosestPoints(const TVec3<T>& Particle, const T MaxRadius, const int32 MaxPoints);
35 int32 GetClosestPoint(const TVec3<T>& Particle);
36
37private:
38 void Init(const T Radius);
39 void Init();
40
41 int32 SmallestAxis() const
42 {
43 TVec3<T> Extents = MBoundingBox.Extents();
44
45 if (Extents[0] < Extents[1] && Extents[0] < Extents[2])
46 {
47 return 0;
48 }
49 else if (Extents[1] < Extents[2])
50 {
51 return 1;
52 }
53 else
54 {
55 return 2;
56 }
57 }
58
59 int32 ComputeMaxN(const TVec3<T>& Particle, const T Radius);
60 TSet<int32> GetNRing(const TVec3<T>& Particle, const int32 N);
61 void ComputeGridXYZ(const TVec3<T>& Particle, int32& XIndex, int32& YIndex, int32& ZIndex);
62
63 int32 HashFunction(const TVec3<T>& Particle);
64 int32 HashFunction(const int32 XIndex, const int32 YIndex, const int32 ZIndex);
65
66private:
67 TArray<TVec3<T>> MParticles;
68 T MCellSize;
69 TAABB<T, 3> MBoundingBox;
70 int32 MNumberOfCellsX, MNumberOfCellsY, MNumberOfCellsZ;
71 TMap<int32, TArray<int32>> MHashTable;
72};
73}
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
FORCEINLINE TVector< T, d > Extents() const
Definition AABB.h:453
Definition SpatialHash.h:11
void Update(const TArray< TVec3< T > > &Particles, const T Radius)
void Update(const TArray< TVec3< T > > &Particles)
TSpatialHash(const TArray< TVec3< T > > &Particles, const T Radius)
Definition SpatialHash.h:13
~TSpatialHash()
Definition SpatialHash.h:25
TSpatialHash(const TArray< TVec3< T > > &Particles)
Definition SpatialHash.h:19
TArray< int32 > GetClosestPoints(const TVec3< T > &Particle, const T MaxRadius)
int32 GetClosestPoint(const TVec3< T > &Particle)
Definition SpatialHash.cpp:235
TArray< int32 > GetClosestPoints(const TVec3< T > &Particle, const T MaxRadius, const int32 MaxPoints)
Definition Vector.h:1000
Definition Array.h:670
Definition UnrealString.h.inl:34
Definition SkeletalMeshComponent.h:307
@ Update
Definition PendingSpatialData.h:19