6#include "Containers/Map.h"
14template<
typename PayloadType>
18 struct FPayloadAndBounds
21 int32 BoundsLookupIdx;
28 FHashIndex() =
default;
40 bool operator==(
const FHashIndex&
Other)
const
51 inline uint32 GetTypeHash()
const
73 void PreallocateElementsForConcurrentAdd(
int32 MaxNumElements,
int32 HashSize)
75 HashSize = FMath::RoundUpToPowerOfTwo(HashSize);
76 Hash.SetNumUninitialized(HashSize);
77 for (std::atomic<int32>& H :
Hash)
79 H.store(
INDEX_NONE, std::memory_order_relaxed);
81 Elements.SetNumUninitialized(MaxNumElements);
82 ConcurrentElementAddIdx.store(0);
101 const uint32 HashKey = Element.Key.GetTypeHash() & (
Hash.Num() - 1);
102 Element.NextIndex =
Hash[HashKey].load(std::memory_order_relaxed);
104 Element.Value =
Value;
115 const int32 IndexToWrite = ConcurrentElementAddIdx.fetch_add(1, std::memory_order_relaxed);
117 Element.Key = FHashIndex( CellIdx,
Lod );
118 const uint32 HashKey = Element.Key.GetTypeHash() & (
Hash.Num() - 1);
119 Element.NextIndex =
Hash[HashKey].load(std::memory_order_relaxed);
121 Element.Value =
Value;
127 void ShrinkElementsAfterConcurrentAdd()
135 const uint32 HashKey = Key.GetTypeHash() &(
Hash.Num() - 1);
136 return Hash[HashKey].load(std::memory_order_relaxed);
141 return Elements[
Index].NextIndex;
149 bool KeyMatches(
int32 Index,
const FHashIndex& Key)
const
151 return Elements[
Index].Key == Key;
154 const FPayloadAndBounds& GetValue(
int32 Index)
const
156 return Elements[
Index].Value;
161 return Elements[
Index].Key;
170 FPayloadAndBounds Value;
175 std::atomic<int32> ConcurrentElementAddIdx;
212template<
typename PayloadType,
typename T>
216 using typename Base::FPayloadAndBounds;
221 static int32 LodForCellSize(T CellSize)
224 if (CellSize <= (T).5)
226 return -(
int32)FMath::FloorLog2((
uint32)FMath::FloorToInt32((T)1 / CellSize));
230 return (
int32)FMath::CeilLogTwo((
uint32)FMath::CeilToInt32(CellSize));
234 static T CellSizeForLod(
const int32 Lod)
238 return (T)(1ULL <<
Lod);
240 return (T)1 / (T)(1ULL << (-
Lod));
295 return Extents.
Max();
331 template<
typename ParticlesType>
337 Bounds.SetNumUninitialized(Particles.Num());
338 HashMap.PreallocateElementsForConcurrentAdd(Particles.Num() * 8, Particles.Num() * 2);
353 const auto& Particle = Particles[ParticleIdx];
354 const FVectorAABB ParticleBounds = Particle.VectorAABB();
356 const int32 Lod = FMath::Max(MinAllowableLod, LodForCellSize(ParticleBounds.LongestSideLength()));
357 const T CellSize = CellSizeForLod(Lod);
358 const TVectorRegisterType<T> CellSizeVector = MakeVectorRegister(CellSize, CellSize, CellSize, CellSize);
360 const PayloadType Payload = Particle.template GetPayload<PayloadType>(ParticleIdx);
362 const TVec3<int32> MinCellIdx = Base::CellIdxForPoint(ParticleBounds.GetMin(), CellSizeVector);
363 const TVec3<int32> MaxCellIdx = Base::CellIdxForPoint(ParticleBounds.GetMax(), CellSizeVector);
365 const FPayloadAndBounds Value = { Payload, ParticleIdx };
376 UsedLods.Add(
Lod, CellSize);
382 HashMap.ShrinkElementsAfterConcurrentAdd();
388 UsedLods.Add(
Lod, CellSizeForLod(
Lod));
413 for (
int32 HashIndex = HashMap.First(Key); HashMap.IsValid(HashIndex); HashIndex = HashMap.Next(HashIndex))
415 if (HashMap.KeyMatches(HashIndex, Key))
417 const FPayloadAndBounds&
Value = HashMap.GetValue(HashIndex);
429 Result.AddUnique(
Value.Payload);
456 const FHashIndex Key{ CellIdx,
Lod };
457 for (
int32 HashIndex = HashMap.First(Key); HashMap.IsValid(HashIndex); HashIndex = HashMap.Next(HashIndex))
459 if (HashMap.KeyMatches(HashIndex, Key))
461 const FPayloadAndBounds
Value = HashMap.GetValue(HashIndex);
468 Result.AddUnique(
Value.Payload);
482template<
typename PayloadType,
typename T>
486 using typename Base::FPayloadAndBounds;
500 ParticleIndexToElement.Reset();
503 template<
typename ParticlesType>
509 ParticleIndexToElement.SetNum(Particles.Num());
510 HashMap.PreallocateElementsForConcurrentAdd(Particles.Num(), Particles.Num());
512 [
this, &Particles](
int32 ParticleIdx)
514 const auto& Particle = Particles[ParticleIdx];
515 const PayloadType Payload = Particle.template GetPayload<PayloadType>(ParticleIdx);
516 const auto& ParticleX = Particle.X();
517 const TVec3<int32> CellIdx = Base::CellIdxForPoint(MakeVectorRegister(ParticleX[0], ParticleX[1], ParticleX[2], 0), CellSize);
518 const FPayloadAndBounds Value = { Payload, ParticleIdx };
520 ParticleIndexToElement[ParticleIdx] =
ElementIdx;
524 HashMap.ShrinkElementsAfterConcurrentAdd();
536 const FHashMap& HashMap;
541 std::atomic<int32> ResultIndex;
551 Result.SetNum(Result.Max());
575 Result.SetNum(Result.Max());
585 for (
int32 HashIndex = HashMap.First(
Cell); HashMap.IsValid(HashIndex); HashIndex = HashMap.Next(HashIndex))
587 if (HashMap.KeyMatches(HashIndex,
Cell))
594 void operator()(
int32 ParticleIdx)
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define ensureAlways( InExpression)
Definition AssertionMacros.h:466
#define check(expr)
Definition AssertionMacros.h:314
@ INDEX_NONE
Definition CoreMiscDefines.h:150
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
#define TRACE_CPUPROFILER_EVENT_SCOPE(Name)
Definition CpuProfilerTrace.h:528
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
const bool
Definition NetworkReplayStreaming.h:178
FORCEINLINE VectorRegister4Int MakeVectorRegisterInt(int32 X, int32 Y, int32 Z, int32 W)
Definition UnrealMathFPU.h:282
FORCEINLINE VectorRegister4Float VectorSubtract(const VectorRegister4Float &Vec1, const VectorRegister4Float &Vec2)
Definition UnrealMathFPU.h:731
FORCEINLINE uint32 VectorAnyGreaterThan(const VectorRegister4Float &Vec1, const VectorRegister4Float &Vec2)
Definition UnrealMathFPU.h:1917
FORCEINLINE VectorRegister4Float VectorMin(const VectorRegister4Float &Vec1, const VectorRegister4Float &Vec2)
Definition UnrealMathFPU.h:1686
#define VectorShiftRightImmArithmetic(Vec, ImmAmt)
Definition UnrealMathFPU.h:2659
FORCEINLINE VectorRegister4Float MakeVectorRegister(uint32 X, uint32 Y, uint32 Z, uint32 W)
Definition UnrealMathFPU.h:195
FORCEINLINE void VectorIntStoreAligned(const VectorRegister4Int &A, const void *Ptr)
Definition UnrealMathFPU.h:2578
FORCEINLINE VectorRegister4Int VectorIntCompareNEQ(const VectorRegister4Int &A, const VectorRegister4Int &B)
Definition UnrealMathFPU.h:2365
FORCEINLINE VectorRegister4Float VectorDivide(const VectorRegister4Float &Vec1, const VectorRegister4Float &Vec2)
Definition UnrealMathFPU.h:834
FORCEINLINE VectorRegister4Float VectorMax(const VectorRegister4Float &Vec1, const VectorRegister4Float &Vec2)
Definition UnrealMathFPU.h:1713
FORCEINLINE VectorRegister4Int VectorRoundToIntHalfToEven(const VectorRegister4Float &A)
Definition UnrealMathFPU.h:2175
FORCEINLINE constexpr VectorRegister4Int MakeVectorRegisterIntConstant(int32 X, int32 Y, int32 Z, int32 W)
Definition UnrealMathFPU.h:292
FORCEINLINE int32 VectorMaskBits(const VectorRegister4Float &Vec1)
Definition UnrealMathFPU.h:1075
FORCEINLINE VectorRegister4Float VectorAdd(const VectorRegister4Float &Vec1, const VectorRegister4Float &Vec2)
Definition UnrealMathFPU.h:704
FORCEINLINE void VectorIntStore(const VectorRegister4Int &A, const void *Ptr)
Definition UnrealMathFPU.h:2546
FORCEINLINE VectorRegister4Int VectorIntMultiply(const VectorRegister4Int &A, const VectorRegister4Int &B)
Definition UnrealMathFPU.h:2435
FORCEINLINE VectorRegister4Int VectorIntAdd(const VectorRegister4Int &A, const VectorRegister4Int &B)
Definition UnrealMathFPU.h:2417
FORCEINLINE void VectorStoreFloat3(const VectorRegister4Float &Vec, float *Dst)
Definition UnrealMathFPU.h:594
FORCEINLINE VectorRegister4Float MakeVectorRegisterFloatFromDouble(const VectorRegister4Double &Vec4d)
Definition UnrealMathFPU.h:262
FORCEINLINE uint32 VectorAnyLesserThan(VectorRegister4Float Vec1, VectorRegister4Float Vec2)
Definition UnrealMathVectorCommon.h.inl:450
UE_INTRINSIC_CAST UE_REWRITE constexpr std::remove_reference_t< T > && MoveTemp(T &&Obj) noexcept
Definition UnrealTemplate.h:520
typename UE::Math::VectorRegisterPrivate::TVectorRegisterTypeHelper< T >::Type TVectorRegisterType
Definition VectorRegister.h:49
Definition HierarchicalSpatialHash.h:246
void Thicken(const T Thickness)
Definition HierarchicalSpatialHash.h:283
T LongestSideLength() const
Definition HierarchicalSpatialHash.h:290
bool Intersects(const FVectorAABB &Other) const
Definition HierarchicalSpatialHash.h:301
void GrowToInclude(const TVec3< T > &Point, const T Radius)
Definition HierarchicalSpatialHash.h:275
FVectorAABB(const TVec3< T > &Point)
Definition HierarchicalSpatialHash.h:256
void GrowToInclude(const TVec3< T > &Point)
Definition HierarchicalSpatialHash.h:268
FVectorAABB(const TVec3< T > &Point, const T Radius)
Definition HierarchicalSpatialHash.h:261
const TVectorRegisterType< T > & GetMax() const
Definition HierarchicalSpatialHash.h:299
FVectorAABB(const TAABB< T, 3 > &BBox)
Definition HierarchicalSpatialHash.h:251
const TVectorRegisterType< T > & GetMin() const
Definition HierarchicalSpatialHash.h:298
bool Intersects(const TVectorRegisterType< T > &Point) const
Definition HierarchicalSpatialHash.h:310
Definition HierarchicalSpatialHash.h:214
void Initialize(const ParticlesType &Particles, const T MinLodSize=(T) 0)
Definition HierarchicalSpatialHash.h:332
TArray< PayloadType > FindAllIntersections(const TAABB< T, 3 > &LookupBounds) const
Definition HierarchicalSpatialHash.h:440
TArray< PayloadType > FindAllIntersections(const TVec3< T > &Point) const
Definition HierarchicalSpatialHash.h:445
TArray< PayloadType > FindAllIntersections(const FVectorAABB LookupBounds, TFunctionRef< bool(const int32 Payload)> BroadphaseTest) const
Definition HierarchicalSpatialHash.h:393
void Reset()
Definition HierarchicalSpatialHash.h:325
Definition HierarchicalSpatialHash.h:16
Definition HierarchicalSpatialHash.h:484
TSpatialHashGridPoints(const T InCellSize)
Definition HierarchicalSpatialHash.h:492
void Reset()
Definition HierarchicalSpatialHash.h:497
TArray< TVec2< PayloadType > > FindAllSelfProximities(int32 CellRadius, int32 MaxNumExpectedConnections, TFunctionRef< bool(const int32 Payload0, const int32 Payload1)> NarrowTest, bool bForceSingleThreaded=false) const
Definition HierarchicalSpatialHash.h:527
void InitializePoints(const ParticlesType &Particles, bool bForceSingleThreaded=false)
Definition HierarchicalSpatialHash.h:504
FORCEINLINE T Max() const
Definition Vector.h:1070
Definition AssetRegistryState.h:50
Definition UnrealString.h.inl:34
Definition StaticArray.h:26
Definition SkeletalMeshComponent.h:307
void CHAOS_API PhysicsParallelFor(int32 InNum, TFunctionRef< void(int32)> InCallable, bool bForceSingleThreaded=false)
Definition Parallel.cpp:55
constexpr VectorRegister4Double DoubleOneHalf
Definition UnrealMathVectorConstants.h.inl:68
constexpr VectorRegister4Float FloatOneHalf
Definition UnrealMathVectorConstants.h.inl:50
float v
Definition radaudio_mdct.cpp:62
U16 Index
Definition radfft.cpp:71
static constexpr UE_FORCEINLINE_HINT bool IsPowerOfTwo(T Value)
Definition UnrealMathUtility.h:519
Definition UnrealMathFPU.h:42
Definition UnrealMathFPU.h:20
Definition UnrealMathFPU.h:28