14 return Extent.
X * Extent.
Y + Extent.
X * Extent.
Z + Extent.
Y * Extent.
Z;
24 template<
typename FFuncType >
50 template<
typename FFuncType >
79 template<
typename FFuncType >
95 template<
typename T >
101 template<
typename T >
114 template<
typename T >
117 constexpr double TileSize = 1024.0 * 1024.0;
131 if(
Root ==
nullptr )
135 Root->FirstChild = ~0u;
162 template<
typename FFuncType >
174 return ( x < 2 ) ? 0 : 1 +
ConstLog2( x / 2 );
193 ZeroCostNodes[ NumZeros++ ] = NodeIndex;
205 NodeIndex = ZeroCostNodes[ --NumZeros ];
211 while( CandidateHead <
Num && Candidates[ CandidateHead ].Key >=
BestCost )
213 if( CandidateHead ==
Num )
219 for(
int32 i = CandidateHead + 1; i <
Num; i++ )
221 float Cost = Candidates[i].Key;
243 static constexpr uint32 MaxZeros = 32;
245 uint32 ZeroCostNodes[ MaxZeros ];
257 using FRoot =
typename FRootPolicy::FRoot;
270 template<
typename T >
272 template<
typename T >
282 template<
typename T,
typename FFuncType >
284 template<
typename FPredicate,
typename FFuncType >
286 template<
typename FFuncType >
289 template<
typename T,
typename FFuncType >
302 float TotalCost = 0.0f;
303 for(
auto& Node :
Nodes )
305 for(
uint32 i = 0; i < Node.NumChildren; i++ )
307 if( ( Node.ChildIndexes[i] & 1 ) == 0 )
308 TotalCost += CostMetric( Node.ChildBounds[i] );
395 Leaves[ FirstChild >> 1 ] = NodeIndex;
420template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
423 static_assert(
MaxChildren > 1,
"Must at least be binary tree." );
427template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
428template<
typename T >
438 FRoot&
Root = Roots.FindOrAdd( Bounds );
440 if(
Root.FirstChild == ~0u )
442 Root.FirstChild = AllocNode();
443 GetNode(
Root.FirstChild ).ParentIndex = ~0u;
444 GetNode(
Root.FirstChild ).NumChildren = 0;
453template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
454template<
typename T >
461template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
467 Extract( Leaves[
Index ] );
468 Leaves[
Index ] = ~0u;
471template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
492template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
516 for(
uint32 i = 0; i < Node.NumChildren; i += 4 )
532 for(
uint32 j = 0; j < 4 && i + j < Node.NumChildren; j++ )
542 uint32 FirstChild = Node.ChildIndexes[ i + j ];
543 bool bIsLeaf = FirstChild & 1;
546 Candidates.Add(
ChildCost[j], FirstChild );
558 return Node.ParentIndex;
575template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
597 for(
uint32 i = 0; i < Node.NumChildren; i += 4 )
610 for(
uint32 j = 0; j < 4 && i + j < Node.NumChildren; j++ )
638 bool bIsLeaf = FirstChild & 1;
643 NodeIndex = FirstChild;
649 return Node.ParentIndex;
658 while( NodeIndex != ~0u );
663template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
666 FNode& RootNode = GetNode(
Root.FirstChild );
671 Root.Bounds += Bounds;
680 bool bIsLeaf = NodeIndex & 1;
681 bool bAddLevel = bIsLeaf || GetNode( NodeIndex ).IsFull();
701 FNode& Children = GetNode( NodeIndex );
704 NodeIndex |= Children.NumChildren++;
725template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
728 FNode& Node = GetNode( NodeIndex );
731 RemoveAndSwap( NodeIndex );
736 uint32 RootIndex = NodeIndex;
747 Roots.FindChecked( RootIndex & ~ChildMask ).Bounds =
PathBounds;
755 FreeNode( NodeIndex );
759 Roots.Remove( NodeIndex & ~ChildMask );
760 FreeNode( NodeIndex );
764 RecursivePromoteChild( NodeIndex );
768template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
771 FNode& Node = GetNode( NodeIndex );
774 if( ( NodeIndex & ChildMask ) < LastChild )
785template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
801 float Cost = CostMetric(
PathNode.ChildBounds[i] );
816 while( NodeIndex != ~0u );
821template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
824 FNode& Node = GetNode( NodeIndex );
829 FNode& Children = GetNode( FirstChild );
836 for(
uint32 i = 0; i < Children.NumChildren; i++ )
838 uint32 j = Children.NumChildren - 1 - i;
843 Forward += Children.ChildBounds[i];
844 Back += Children.ChildBounds[j];
850 for(
uint32 i = 0; i < Children.NumChildren; i++ )
852 float Cost = CostMetric( Excluded[i] );
864 SetBounds( NodeIndex, Excluded[
BestIndex & ChildMask ] );
874 uint32 LastChild = --Children.NumChildren;
875 if( Children.NumChildren == 1 )
887 else if( (
BestIndex & ChildMask ) != LastChild )
891 Children.GetBounds( LastChild ),
892 Children.GetFirstChild( LastChild ) );
898template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
901 FNode& Node = GetNode( NodeIndex );
909 if( i != (NodeIndex & ChildMask) )
942template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
945 if( FreeHead != ~0u )
947 uint32 NodeIndex = FreeHead;
948 uint32& NextIndex = GetNode( NodeIndex ).ParentIndex;
949 FreeHead = NextIndex;
955 DirtyPolicy.Add( Nodes.Num() );
956 return Nodes.AddUninitialized() << IndexShift;
960template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
964 GetNode( NodeIndex ).ParentIndex = FreeHead;
965 GetNode( NodeIndex ).NumChildren = 0;
969template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
972 const FNode& Node = GetNode( NodeIndex );
985 check( Leaves[ FirstChild >> 1 ] == NodeIndex );
989 check( ( FirstChild & ChildMask ) == 0 );
991 const FNode& Children = GetNode( FirstChild );
992 for(
uint32 i = 0; i < Children.NumChildren; i++ )
994 check( Children.ParentIndex == NodeIndex );
999template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
1000template<
typename T,
typename FFuncType >
1006 [
this, &Stack, &Bounds, &Func ](
const FRoot&
Root )
1019 for(
uint32 i = 0; i < Node.NumChildren; i++ )
1024 uint32 FirstChild = Node.ChildIndexes[i];
1025 if( FirstChild & 1 )
1028 Func( FirstChild >> 1 );
1032 Stack.
Push( FirstChild );
1037 if( Stack.
Num() == 0 )
1045template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
1046template<
typename FPredicate,
typename FFuncType >
1052 [
this, &Stack, &Predicate, &Func ](
const FRoot&
Root )
1054 if( !Predicate(
Root.ToAbsolute(
Root.Bounds ) ) )
1063 for(
uint32 i = 0; i < Node.NumChildren; i++ )
1065 if( Predicate(
Root.ToAbsolute( Node.ChildBounds[i] ) ) )
1067 uint32 FirstChild = Node.ChildIndexes[i];
1068 if( FirstChild & 1 )
1071 Func( FirstChild >> 1 );
1075 Stack.
Push( FirstChild );
1080 if( Stack.
Num() == 0 )
1088template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
1089template<
typename FFuncType >
1095 Func(
Index, GetNode(
Index << IndexShift ) );
1100template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
1101template<
typename T,
typename FFuncType >
1121 for(
uint32 i = 0; i < Node.NumChildren; i += 4 )
1132 for(
uint32 j = 0; j < 4 && i + j < Node.NumChildren; j++ )
1136 uint32 FirstChild = Node.ChildIndexes[ i + j ];
1137 if( FirstChild & 1 )
1181 void RegenerateCodes(
const FRange& Range );
1202 RegenerateCodes( Range );
1204 Code0 = Sorted[ Range.Begin ].Code;
1205 Code1 = Sorted[ Range.End - 1 ].Code;
1209 return ( Range.Begin + Range.End ) >> 1;
1220 if( Sorted[ Mid ].Code &
Mask )
1229template< u
int32 MaxChildren,
typename FRootPolicy,
typename FDirtyPolicy,
typename FCostMetric >
1245 Root.FirstChild = 0;
1254 uint32 ParentIndex = ~0u;
1259 uint32 NodeIndex = AllocNode();
1260 FNode& Node = GetNode( NodeIndex );
1263 if( ParentIndex != ~0u )
1264 SetFirstChild( ParentIndex, NodeIndex );
1266 check( Range.Begin < Range.End );
1295 if( Stack.
Num() == 0 )
1298 ParentIndex = Stack.
Last().ParentIndex;
1299 Range = Stack.
Last().Range;
1306 Children[0] = Range;
1308 int32 NumChildren = 1;
1320 Children[ NumChildren ].Begin = Middle;
1321 Children[ NumChildren ].End =
Child.End;
1326 for(
int32 i = 0; i < NumChildren; i++ )
1355 for(
int32 i = NumChildren - 1; i >= 0; i-- )
1357 bool bIsLeaf = Children[i].Num() == 1;
1370 Stack.
Push( { NodeIndex + i, Children[i] } );
1373 ParentIndex = NodeIndex +
Last;
1374 Range = Children[
Last ];
#define checkSlow(expr)
Definition AssertionMacros.h:332
#define check(expr)
Definition AssertionMacros.h:314
TBounds< float > FBounds3f
Definition Bounds.h:154
#define CA_SUPPRESS(WarningNumber)
Definition CoreMiscDefines.h:125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
constexpr uint32 ConstLog2(uint32 x)
Definition DynamicBVH.h:172
bool operator<(const FTextFormatString &LHS, const FTextFormatString &RHS)
Definition ITextFormatArgumentModifier.h:147
UE::Math::TVector< float > FVector3f
Definition MathFwd.h:73
#define MAX_flt
Definition NumericLimits.h:29
#define Split(a, ahi, alo)
Definition Predicates.inl:204
@ Four
Definition PropertyPathHelpersTest.h:19
#define UE_KINDA_SMALL_NUMBER
Definition UnrealMathUtility.h:131
Definition DynamicBVH.h:256
uint32 FindBestInsertion_BranchAndBound(uint32 NodeIndex, const FBounds3f &RESTRICT Bounds)
Definition DynamicBVH.h:493
float GetTotalCost() const
Definition DynamicBVH.h:300
TArray< uint32 > Leaves
Definition DynamicBVH.h:362
void RemoveAndSwap(uint32 NodeIndex)
Definition DynamicBVH.h:769
const FNode & GetNode(uint32 NodeIndex) const
Definition DynamicBVH.h:369
uint32 Insert(FRoot &RESTRICT Root, const FBounds3f &RESTRICT Bounds, uint32 NodeIndex)
Definition DynamicBVH.h:664
void Add(const TBounds< T > &Bounds, uint32 Index)
void SetBounds(uint32 NodeIndex, const FBounds3f &Bounds)
Definition DynamicBVH.h:382
int32 GetNumLeaves() const
Definition DynamicBVH.h:267
FNode & GetNode(uint32 NodeIndex)
Definition DynamicBVH.h:368
bool Check() const
Definition DynamicBVH.h:315
void Update(const TBounds< T > &Bounds, uint32 Index)
int32 GetNumDirty() const
Definition DynamicBVH.h:268
void MarkDirty(uint32 NodeIndex)
Definition DynamicBVH.h:371
static constexpr uint32 ChildMask
Definition DynamicBVH.h:332
uint32 AllocNode()
Definition DynamicBVH.h:943
uint32 FindBestInsertion_Greedy(uint32 NodeIndex, const FBounds3f &RESTRICT Bounds)
Definition DynamicBVH.h:576
FDynamicBVH()
Definition DynamicBVH.h:421
void FreeNode(uint32 NodeIndex)
Definition DynamicBVH.h:961
FLowestCostList Candidates
Definition DynamicBVH.h:365
void Rotate(uint32 NodeIndex)
Definition DynamicBVH.h:899
void ForAll(const TBounds< T > &Bounds, const FFuncType &Func) const
Definition DynamicBVH.h:1001
const FBounds3f & GetBounds(uint32 Index) const
Definition DynamicBVH.h:293
void SetFirstChild(uint32 NodeIndex, uint32 FirstChild)
Definition DynamicBVH.h:388
uint32 FreeHead
Definition DynamicBVH.h:363
void Build(const TArray< FBounds3f > &BoundsArray, uint32 FirstIndex)
Definition DynamicBVH.h:1230
void AddDefaulted()
Definition DynamicBVH.h:277
void Set(uint32 NodeIndex, const FBounds3f &Bounds, uint32 FirstChild)
Definition DynamicBVH.h:376
static constexpr uint32 IndexShift
Definition DynamicBVH.h:331
void CheckNode(uint32 NodeIndex) const
Definition DynamicBVH.h:970
void ForAllDirty(const FFuncType &Func)
Definition DynamicBVH.h:1090
void SwapIndexes(uint32 Index0, uint32 Index1)
Definition DynamicBVH.h:472
uint32 NumTested
Definition DynamicBVH.h:328
bool IsPresent(uint32 Index) const
Definition DynamicBVH.h:276
uint32 PromoteChild(uint32 NodeIndex)
Definition DynamicBVH.h:822
bool RecursivePromoteChild(uint32 NodeIndex)
Definition DynamicBVH.h:786
TArray< FNode > Nodes
Definition DynamicBVH.h:361
int32 GetNumNodes() const
Definition DynamicBVH.h:266
void Extract(uint32 NodeIndex)
Definition DynamicBVH.h:726
void Remove(uint32 Index)
Definition DynamicBVH.h:462
static constexpr uint32 MaxChildren4
Definition DynamicBVH.h:333
uint32 FindClosest(const UE::Math::TVector< T > &Position, const FFuncType &LeafDistSqr)
Definition DynamicBVH.h:1102
Definition DynamicBVH.h:178
void Reset()
Definition DynamicBVH.h:182
bool GetNext(float BestCost, float &NodeCost, uint32 &NodeIndex)
Definition DynamicBVH.h:201
void Add(float NodeCost, uint32 NodeIndex)
Definition DynamicBVH.h:189
Definition DynamicBVH.h:1164
uint32 Split(const FRange &Range)
Definition DynamicBVH.h:1195
uint32 GetIndex(int32 i) const
Definition DynamicBVH.h:1177
UE_REWRITE SizeType Num() const
Definition Array.h:1144
UE_NODEBUG UE_FORCEINLINE_HINT ElementType & Last(SizeType IndexFromTheEnd=0) UE_LIFETIMEBOUND
Definition Array.h:1263
UE_NODEBUG UE_FORCEINLINE_HINT void Push(ElementType &&Item)
Definition Array.h:1224
void Reset(SizeType NewSize=0)
Definition Array.h:2246
UE_NODEBUG UE_FORCEINLINE_HINT SizeType Add(ElementType &&Item)
Definition Array.h:2696
ElementType Pop(EAllowShrinking AllowShrinking=UE::Core::Private::AllowShrinkingByDefault< AllocatorType >())
Definition Array.h:1196
int32 Add(const bool Value)
Definition BitArray.h:615
U16 Index
Definition radfft.cpp:71
Definition DynamicBVH.h:336
bool IsLeaf(uint32 NodeIndex) const
Definition DynamicBVH.h:347
bool IsRoot() const
Definition DynamicBVH.h:346
uint32 GetFirstChild(uint32 NodeIndex) const
Definition DynamicBVH.h:343
uint32 ChildIndexes[MaxChildren]
Definition DynamicBVH.h:340
FBounds3f UnionBounds() const
Definition DynamicBVH.h:350
const FBounds3f & GetBounds(uint32 NodeIndex) const
Definition DynamicBVH.h:344
uint32 ParentIndex
Definition DynamicBVH.h:338
uint32 NumChildren
Definition DynamicBVH.h:339
FBounds3f ChildBounds[MaxChildren]
Definition DynamicBVH.h:341
bool IsFull() const
Definition DynamicBVH.h:348
Definition DynamicBVH.h:19
void Mark(uint32 Index)
Definition DynamicBVH.h:22
int32 Num() const
Definition DynamicBVH.h:20
void Add(uint32 Index)
Definition DynamicBVH.h:21
void ForAll(const FFuncType &Func)
Definition DynamicBVH.h:25
static UE_FORCEINLINE_HINT float RoundToZero(float F)
Definition UnrealMathUtility.h:2209
static UE_FORCEINLINE_HINT void * Memset(void *Dest, uint8 Char, SIZE_T Count)
Definition UnrealMemory.h:119
Definition DynamicBVH.h:1167
int32 Num() const
Definition DynamicBVH.h:1171
int32 Begin
Definition DynamicBVH.h:1168
int32 End
Definition DynamicBVH.h:1169
Definition DynamicBVH.h:90
uint32 FirstChild
Definition DynamicBVH.h:93
FBounds3d ToAbsolute(const FBounds3f &Other) const
Definition DynamicBVH.h:107
FVector3d Offset
Definition DynamicBVH.h:91
FBounds3f ToRelative(const TBounds< T > &Other) const
Definition DynamicBVH.h:102
FVector3f ToRelative(const UE::Math::TVector< T > &Position) const
Definition DynamicBVH.h:96
FBounds3f Bounds
Definition DynamicBVH.h:92
Definition DynamicBVH.h:88
FRoot & FindChecked(uint32 RootFirstChild)
Definition DynamicBVH.h:141
FRoot & FindOrAdd(const TBounds< T > &Bounds)
Definition DynamicBVH.h:115
TArray< FRoot > Roots
Definition DynamicBVH.h:112
void Remove(uint32 RootFirstChild)
Definition DynamicBVH.h:152
void ForAll(const FFuncType &Func) const
Definition DynamicBVH.h:163
Definition DynamicBVH.h:65
const FBounds3f & ToRelative(const FBounds3f &Other) const
Definition DynamicBVH.h:70
const FVector3f & ToRelative(const FVector3f &Position) const
Definition DynamicBVH.h:69
uint32 FirstChild
Definition DynamicBVH.h:67
FBounds3f Bounds
Definition DynamicBVH.h:66
const FBounds3f & ToAbsolute(const FBounds3f &Other) const
Definition DynamicBVH.h:71
Definition DynamicBVH.h:63
FRoot Root
Definition DynamicBVH.h:73
FRoot & FindOrAdd(const FBounds3f &Bounds)
Definition DynamicBVH.h:75
FRoot & FindChecked(uint32 RootFirstChild)
Definition DynamicBVH.h:76
void ForAll(const FFuncType &Func) const
Definition DynamicBVH.h:80
void Remove(uint32 RootFirstChild)
Definition DynamicBVH.h:77
Definition DynamicBVH.h:10
float operator()(const FBounds3f &Bounds) const
Definition DynamicBVH.h:11
Definition DynamicBVH.h:29
TBitArray NodeIsDirty
Definition DynamicBVH.h:30
void Mark(uint32 Index)
Definition DynamicBVH.h:41
int32 Num() const
Definition DynamicBVH.h:33
TArray< uint32 > DirtyNodes
Definition DynamicBVH.h:31
void ForAll(const FFuncType &Func)
Definition DynamicBVH.h:51
void Add(uint32 Index)
Definition DynamicBVH.h:35
TVector4< T > Max
Definition Bounds.h:19
TBounds< float > ToRelative(const TVector< U > &Offset) const
Definition Bounds.h:105
T DistSqr(const TVector< T > &Point) const
Definition Bounds.h:63
TVector4< T > Min
Definition Bounds.h:18
UE_FORCEINLINE_HINT TVector< T > GetCenter() const
Definition Bounds.h:72
auto ToAbsolute(const TVector< U > &Offset) const
Definition Bounds.h:94
T X
Definition Vector4.h:43
T Z
Definition Vector.h:68
T Y
Definition Vector.h:65
static CORE_API const TVector< float > ZeroVector
Definition Vector.h:79
T X
Definition Vector.h:62
TVector< T > GetAbs() const
Definition Vector.h:1710