Improve compound serialization (#183)
- no more pointer fixups - reduced storage 20% by removing leaf proxies
This commit is contained in:
parent
3d9db79ee1
commit
5d83df83ab
25 changed files with 845 additions and 376 deletions
|
|
@ -867,17 +867,17 @@ B3_FORCE_INLINE bool b3TestCategory( uint64_t categoryBits, uint64_t maskBits, b
|
|||
return requireAllBits ? ( categoryBits & maskBits ) == maskBits : ( categoryBits & maskBits ) != 0;
|
||||
}
|
||||
|
||||
b3TreeStats b3DynamicTree_Query( const b3DynamicTree* tree, b3AABB aabb, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryCallbackFcn* callback, void* context )
|
||||
B3_FORCE_INLINE b3TreeStats b3QueryTreeView( const b3TreeView* view, b3AABB aabb, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryCallbackFcn* callback, void* context, bool hasProxies )
|
||||
{
|
||||
b3TreeStats result = { 0 };
|
||||
|
||||
if ( tree->proxyCount == 0 )
|
||||
if ( b3IsEmptyNode( view->nodes + B3_ROOT_NODE ) )
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
const b3TreeNode* nodes = tree->nodes;
|
||||
const b3TreeNode* nodes = view->nodes;
|
||||
|
||||
int stack[B3_TREE_STACK_SIZE];
|
||||
int stackCount = 0;
|
||||
|
|
@ -901,16 +901,23 @@ b3TreeStats b3DynamicTree_Query( const b3DynamicTree* tree, b3AABB aabb, uint64_
|
|||
if ( b3IsLeaf( node ) )
|
||||
{
|
||||
int proxyId = b3GetProxyId( node );
|
||||
const b3TreeProxy* proxy = tree->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) )
|
||||
uint64_t userData = (uint64_t)(uint32_t)node->shapeIndex;
|
||||
if ( hasProxies )
|
||||
{
|
||||
bool proceed = callback( proxyId, proxy->userData, context );
|
||||
result.leafVisits += 1;
|
||||
|
||||
if ( proceed == false )
|
||||
const b3TreeProxy* proxy = view->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
{
|
||||
return result;
|
||||
continue;
|
||||
}
|
||||
userData = proxy->userData;
|
||||
}
|
||||
|
||||
bool proceed = callback( proxyId, userData, context );
|
||||
result.leafVisits += 1;
|
||||
|
||||
if ( proceed == false )
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
else
|
||||
|
|
@ -942,17 +949,18 @@ struct b3QueryClosestItem
|
|||
float distanceToNodeSqr;
|
||||
};
|
||||
|
||||
b3TreeStats b3DynamicTree_QueryClosest( const b3DynamicTree* tree, b3Vec3 point, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryClosestCallbackFcn* callback, void* context, float* minDistanceSqr )
|
||||
B3_FORCE_INLINE b3TreeStats b3QueryClosestTreeView( const b3TreeView* view, b3Vec3 point, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryClosestCallbackFcn* callback, void* context, float* minDistanceSqr,
|
||||
bool hasProxies )
|
||||
{
|
||||
b3TreeStats result = { 0 };
|
||||
|
||||
if ( tree->proxyCount == 0 )
|
||||
if ( b3IsEmptyNode( view->nodes + B3_ROOT_NODE ) )
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
const b3TreeNode* nodes = tree->nodes;
|
||||
const b3TreeNode* nodes = view->nodes;
|
||||
|
||||
float minSqr = *minDistanceSqr;
|
||||
struct b3QueryClosestItem stack[B3_TREE_STACK_SIZE];
|
||||
|
|
@ -1002,14 +1010,19 @@ b3TreeStats b3DynamicTree_QueryClosest( const b3DynamicTree* tree, b3Vec3 point,
|
|||
if ( b3IsLeaf( node ) )
|
||||
{
|
||||
int proxyId = b3GetProxyId( node );
|
||||
const b3TreeProxy* proxy = tree->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
uint64_t userData = (uint64_t)(uint32_t)node->shapeIndex;
|
||||
if ( hasProxies )
|
||||
{
|
||||
continue;
|
||||
const b3TreeProxy* proxy = view->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
userData = proxy->userData;
|
||||
}
|
||||
|
||||
// callback to user code with minimum distance squared so far and proxy id
|
||||
float dd = callback( minSqr, proxyId, proxy->userData, context );
|
||||
float dd = callback( minSqr, proxyId, userData, context );
|
||||
|
||||
if ( dd < minSqr )
|
||||
{
|
||||
|
|
@ -1059,12 +1072,13 @@ b3TreeStats b3DynamicTree_QueryClosest( const b3DynamicTree* tree, b3Vec3 point,
|
|||
|
||||
// A lot of optimization work went into this. The children of a popped pair are both tested
|
||||
// before either is followed, then the survivors are ordered so the closer one narrows first.
|
||||
b3TreeStats b3DynamicTree_RayCast( const b3DynamicTree* tree, const b3RayCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeRayCastCallbackFcn* callback, void* context )
|
||||
B3_FORCE_INLINE b3TreeStats b3RayCastTreeView( const b3TreeView* view, const b3RayCastInput* input, uint64_t maskBits,
|
||||
bool requireAllBits, b3TreeRayCastCallbackFcn* callback, void* context,
|
||||
bool hasProxies )
|
||||
{
|
||||
b3TreeStats result = { 0 };
|
||||
|
||||
if ( tree->proxyCount == 0 )
|
||||
if ( b3IsEmptyNode( view->nodes + B3_ROOT_NODE ) )
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
|
@ -1083,7 +1097,7 @@ b3TreeStats b3DynamicTree_RayCast( const b3DynamicTree* tree, const b3RayCastInp
|
|||
b3AABB segmentAABB = { b3Min( p1, p2 ), b3Max( p1, p2 ) };
|
||||
b3AABBV boxv = b3LoadAABBV( &segmentAABB );
|
||||
|
||||
const b3TreeNode* nodes = tree->nodes;
|
||||
const b3TreeNode* nodes = view->nodes;
|
||||
|
||||
int stack[B3_TREE_STACK_SIZE];
|
||||
int stackCount = 0;
|
||||
|
|
@ -1140,16 +1154,20 @@ b3TreeStats b3DynamicTree_RayCast( const b3DynamicTree* tree, const b3RayCastInp
|
|||
if ( isLeaf[i] )
|
||||
{
|
||||
int proxyId = b3GetProxyId( hit[i] );
|
||||
const b3TreeProxy* proxy = tree->proxies + proxyId;
|
||||
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
uint64_t userData = (uint64_t)(uint32_t)hit[i]->shapeIndex;
|
||||
if ( hasProxies )
|
||||
{
|
||||
continue;
|
||||
const b3TreeProxy* proxy = view->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
userData = proxy->userData;
|
||||
}
|
||||
|
||||
subInput.maxFraction = maxFraction;
|
||||
|
||||
float value = callback( &subInput, proxyId, proxy->userData, context );
|
||||
float value = callback( &subInput, proxyId, userData, context );
|
||||
result.leafVisits += 1;
|
||||
|
||||
// The user may return -1 to indicate this shape should be skipped
|
||||
|
|
@ -1188,12 +1206,13 @@ b3TreeStats b3DynamicTree_RayCast( const b3DynamicTree* tree, const b3RayCastInp
|
|||
}
|
||||
|
||||
// Follows structure of ray cast with small tweaks to handle a swept box.
|
||||
b3TreeStats b3DynamicTree_BoxCast( const b3DynamicTree* tree, const b3BoxCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeBoxCastCallbackFcn* callback, void* context )
|
||||
B3_FORCE_INLINE b3TreeStats b3BoxCastTreeView( const b3TreeView* view, const b3BoxCastInput* input, uint64_t maskBits,
|
||||
bool requireAllBits, b3TreeBoxCastCallbackFcn* callback, void* context,
|
||||
bool hasProxies )
|
||||
{
|
||||
b3TreeStats result = { 0 };
|
||||
|
||||
if ( tree->proxyCount == 0 )
|
||||
if ( b3IsEmptyNode( view->nodes + B3_ROOT_NODE ) )
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
|
@ -1220,7 +1239,7 @@ b3TreeStats b3DynamicTree_BoxCast( const b3DynamicTree* tree, const b3BoxCastInp
|
|||
};
|
||||
b3AABBV boxv = b3LoadAABBV( &totalAABB );
|
||||
|
||||
const b3TreeNode* nodes = tree->nodes;
|
||||
const b3TreeNode* nodes = view->nodes;
|
||||
|
||||
int stack[B3_TREE_STACK_SIZE];
|
||||
int stackCount = 0;
|
||||
|
|
@ -1278,16 +1297,20 @@ b3TreeStats b3DynamicTree_BoxCast( const b3DynamicTree* tree, const b3BoxCastInp
|
|||
if ( isLeaf[i] )
|
||||
{
|
||||
int proxyId = b3GetProxyId( hit[i] );
|
||||
const b3TreeProxy* proxy = tree->proxies + proxyId;
|
||||
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
uint64_t userData = (uint64_t)(uint32_t)hit[i]->shapeIndex;
|
||||
if ( hasProxies )
|
||||
{
|
||||
continue;
|
||||
const b3TreeProxy* proxy = view->proxies + proxyId;
|
||||
if ( b3TestCategory( proxy->categoryBits, maskBits, requireAllBits ) == false )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
userData = proxy->userData;
|
||||
}
|
||||
|
||||
subInput.maxFraction = maxFraction;
|
||||
|
||||
float value = callback( &subInput, proxyId, proxy->userData, context );
|
||||
float value = callback( &subInput, proxyId, userData, context );
|
||||
result.leafVisits += 1;
|
||||
|
||||
if ( value == 0.0f )
|
||||
|
|
@ -1322,6 +1345,97 @@ b3TreeStats b3DynamicTree_BoxCast( const b3DynamicTree* tree, const b3BoxCastInp
|
|||
return result;
|
||||
}
|
||||
|
||||
b3TreeStats b3DynamicTree_Query( const b3DynamicTree* tree, b3AABB aabb, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( tree->proxyCount == 0 )
|
||||
{
|
||||
return B3_LITERAL( b3TreeStats ){ 0 };
|
||||
}
|
||||
|
||||
b3TreeView view = b3MakeTreeView( tree );
|
||||
return b3QueryTreeView( &view, aabb, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3DynamicTree_QueryClosest( const b3DynamicTree* tree, b3Vec3 point, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryClosestCallbackFcn* callback, void* context, float* minDistanceSqr )
|
||||
{
|
||||
if ( tree->proxyCount == 0 )
|
||||
{
|
||||
return B3_LITERAL( b3TreeStats ){ 0 };
|
||||
}
|
||||
|
||||
b3TreeView view = b3MakeTreeView( tree );
|
||||
return b3QueryClosestTreeView( &view, point, maskBits, requireAllBits, callback, context, minDistanceSqr, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3DynamicTree_RayCast( const b3DynamicTree* tree, const b3RayCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeRayCastCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( tree->proxyCount == 0 )
|
||||
{
|
||||
return B3_LITERAL( b3TreeStats ){ 0 };
|
||||
}
|
||||
|
||||
b3TreeView view = b3MakeTreeView( tree );
|
||||
return b3RayCastTreeView( &view, input, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3DynamicTree_BoxCast( const b3DynamicTree* tree, const b3BoxCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeBoxCastCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( tree->proxyCount == 0 )
|
||||
{
|
||||
return B3_LITERAL( b3TreeStats ){ 0 };
|
||||
}
|
||||
|
||||
b3TreeView view = b3MakeTreeView( tree );
|
||||
return b3BoxCastTreeView( &view, input, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3TreeView_Query( const b3TreeView* view, b3AABB aabb, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( view->proxies == NULL )
|
||||
{
|
||||
return b3QueryTreeView( view, aabb, maskBits, requireAllBits, callback, context, false );
|
||||
}
|
||||
|
||||
return b3QueryTreeView( view, aabb, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3TreeView_QueryClosest( const b3TreeView* view, b3Vec3 point, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeQueryClosestCallbackFcn* callback, void* context, float* minDistanceSqr )
|
||||
{
|
||||
if ( view->proxies == NULL )
|
||||
{
|
||||
return b3QueryClosestTreeView( view, point, maskBits, requireAllBits, callback, context, minDistanceSqr, false );
|
||||
}
|
||||
|
||||
return b3QueryClosestTreeView( view, point, maskBits, requireAllBits, callback, context, minDistanceSqr, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3TreeView_RayCast( const b3TreeView* view, const b3RayCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeRayCastCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( view->proxies == NULL )
|
||||
{
|
||||
return b3RayCastTreeView( view, input, maskBits, requireAllBits, callback, context, false );
|
||||
}
|
||||
|
||||
return b3RayCastTreeView( view, input, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
b3TreeStats b3TreeView_BoxCast( const b3TreeView* view, const b3BoxCastInput* input, uint64_t maskBits, bool requireAllBits,
|
||||
b3TreeBoxCastCallbackFcn* callback, void* context )
|
||||
{
|
||||
if ( view->proxies == NULL )
|
||||
{
|
||||
return b3BoxCastTreeView( view, input, maskBits, requireAllBits, callback, context, false );
|
||||
}
|
||||
|
||||
return b3BoxCastTreeView( view, input, maskBits, requireAllBits, callback, context, true );
|
||||
}
|
||||
|
||||
// Median split == 0, Surface area heuristic == 1
|
||||
#define B3_TREE_HEURISTIC 0
|
||||
|
|
|
|||
Loading…
Reference in a new issue