Fixes 08 (#98)
- fix hull builder face and edge leak - fix #95 - samples app will no longer crash if data folder is not found
This commit is contained in:
parent
23861418d8
commit
c52908c9a9
11 changed files with 307 additions and 153 deletions
115
src/hull.c
115
src/hull.c
|
|
@ -26,8 +26,8 @@
|
|||
#define B3_MARK_VISIBLE 0
|
||||
#define B3_MARK_DELETE 1
|
||||
|
||||
// Final hull is index-encoded with uint8_t, so vertex/edge/face counts are capped at UINT8_MAX.
|
||||
#define B3_HULL_LIMIT UINT8_MAX
|
||||
// Final hull is index-encoded with uint8_t, so vertex/edge/face counts are capped at 256.
|
||||
#define B3_HULL_MAX_COUNT ( UINT8_MAX + 1 )
|
||||
|
||||
typedef struct b3QHListNode
|
||||
{
|
||||
|
|
@ -120,12 +120,16 @@ typedef struct b3HullBuilder
|
|||
b3QHHalfEdge* edgeBase;
|
||||
int edgeCapacity;
|
||||
int edgeCount;
|
||||
b3QHHalfEdge* edgeFreeHead; // LIFO free list; overlays edge->next
|
||||
|
||||
// LIFO free list. Built with edge->next.
|
||||
b3QHHalfEdge* edgeFreeHead;
|
||||
|
||||
b3QHFace* faceBase;
|
||||
int faceCapacity;
|
||||
int faceCount;
|
||||
b3QHFace* faceFreeHead; // LIFO free list; overlays face->link.next
|
||||
|
||||
// LIFO free list. Built with face->link.next.
|
||||
b3QHFace* faceFreeHead;
|
||||
|
||||
// Reusable scratch buffers.
|
||||
b3QHHalfEdge** horizon;
|
||||
|
|
@ -145,7 +149,6 @@ typedef struct b3HullBuilder
|
|||
int horizonStackCapacity;
|
||||
|
||||
// Final counts of the constructed hull (vertexList / faceList / half-edges around faces).
|
||||
// Populated by CleanHull; zero until then.
|
||||
int finalVertexCount;
|
||||
int finalHalfEdgeCount;
|
||||
int finalFaceCount;
|
||||
|
|
@ -300,17 +303,18 @@ static b3QHFace* b3HullBuilder_NewFace( b3HullBuilder* b, b3QHVertex* v1, b3QHVe
|
|||
return face;
|
||||
}
|
||||
|
||||
// Remove face from faceList if still linked, clear its edge pointer, then push onto faceFreeHead.
|
||||
// Uses face->link.next as the free-list next pointer (link.prev stays NULL, so b3QHList_Contains
|
||||
// returns false on a free slot, as required by the retire-guard in ResolveFaces).
|
||||
// Remove face and add to free list.
|
||||
static void b3HullBuilder_RetireFace( b3HullBuilder* b, b3QHFace* face )
|
||||
{
|
||||
// Sometimes a cone face gets merged and never added to the list.
|
||||
if ( b3QHList_Contains( &face->link ) )
|
||||
{
|
||||
b3QHList_Remove( &face->link );
|
||||
}
|
||||
|
||||
face->edge = NULL;
|
||||
// link.prev is already NULL after Remove (or was never set). link.next holds free-list ptr.
|
||||
// link.prev is already NULL after Remove (or was never set).
|
||||
B3_VALIDATE( face->link.prev == NULL );
|
||||
face->link.next = (b3QHListNode*)b->faceFreeHead;
|
||||
b->faceFreeHead = face;
|
||||
}
|
||||
|
|
@ -1232,7 +1236,7 @@ static void b3HullBuilder_ResolveVertices( b3HullBuilder* b )
|
|||
static void b3HullBuilder_ResolveFaces( b3HullBuilder* b )
|
||||
{
|
||||
// Splice deleted faces out of the face list. Faces already retired by AbsorbFaces are no
|
||||
// longer on faceList, so we guard with b3QHList_Contains before removing.
|
||||
// longer on faceList, so guard with b3QHList_Contains before removing.
|
||||
b3QHListNode* node = b->faceList.link.next;
|
||||
while ( node != &b->faceList.link )
|
||||
{
|
||||
|
|
@ -1242,7 +1246,21 @@ static void b3HullBuilder_ResolveFaces( b3HullBuilder* b )
|
|||
if ( face->mark == B3_MARK_DELETE && b3QHList_Contains( &face->link ) )
|
||||
{
|
||||
B3_ASSERT( B3_LIST_EMPTY( &face->conflictListHead.link ) );
|
||||
b3QHList_Remove( &face->link );
|
||||
|
||||
// Each half-edge is owned by exactly one face, so ring walks over the
|
||||
// dead region retire every interior edge exactly once. Merge deleted
|
||||
// faces are already off the face list.
|
||||
b3QHHalfEdge* start = face->edge;
|
||||
b3QHHalfEdge* edge = start;
|
||||
do
|
||||
{
|
||||
b3QHHalfEdge* next = edge->next;
|
||||
b3HullBuilder_RetireEdge( b, edge );
|
||||
edge = next;
|
||||
}
|
||||
while ( edge != start );
|
||||
|
||||
b3HullBuilder_RetireFace( b, face );
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1427,7 +1445,7 @@ static bool b3HullBuilder_Construct( b3HullBuilder* b, const b3Vec3* points, int
|
|||
return false;
|
||||
}
|
||||
|
||||
int budget = b3ClampInt( maxVertexCount - 4, 0, B3_HULL_LIMIT - 4 );
|
||||
int budget = b3ClampInt( maxVertexCount - 4, 0, B3_HULL_MAX_COUNT - 4 );
|
||||
|
||||
b3QHVertex* vertex = b3HullBuilder_NextConflictVertex( b );
|
||||
while ( vertex && budget > 0 )
|
||||
|
|
@ -1448,8 +1466,10 @@ static bool b3HullBuilder_Construct( b3HullBuilder* b, const b3Vec3* points, int
|
|||
|
||||
typedef struct b3HullWorkSizes
|
||||
{
|
||||
int N; // pointCount
|
||||
int M; // clamped maxVertexCount, in [4, B3_HULL_LIMIT]
|
||||
// Input point count
|
||||
int N;
|
||||
// Output point limit
|
||||
int M;
|
||||
int vertexCapacity;
|
||||
int edgeCapacity;
|
||||
int faceCapacity;
|
||||
|
|
@ -1479,39 +1499,19 @@ static b3HullWorkSizes b3ComputeHullWorkSizes( int pointCount, int clampedMaxCou
|
|||
s.vertexCapacity = pointCount + 4;
|
||||
|
||||
// Edges and faces use free-list recycling; capacity is proportional to live hull size.
|
||||
// edgeCapacity: peak is ~twice live edges plus cone edges; floor 48.
|
||||
s.edgeCapacity = 24 * s.M - 48;
|
||||
if ( s.edgeCapacity < 48 )
|
||||
{
|
||||
s.edgeCapacity = 48;
|
||||
}
|
||||
// edgeCapacity: peak is ~twice live edges plus cone edges. Minimum 48.
|
||||
s.edgeCapacity = b3MaxInt( 48, 24 * s.M - 48 );
|
||||
|
||||
// faceCapacity: peak intermediate state live faces (<=2*M-4) plus full cone (<=3*M-6); floor 16.
|
||||
s.faceCapacity = 5 * s.M - 10;
|
||||
if ( s.faceCapacity < 16 )
|
||||
{
|
||||
s.faceCapacity = 16;
|
||||
}
|
||||
// faceCapacity: peak intermediate state live faces (<=2*M-4) plus full cone (<=3*M-6). Minimum 16.
|
||||
s.faceCapacity = b3MaxInt( 16, 5 * s.M - 10 );
|
||||
|
||||
// Horizon/cone bounded by current half-edge count; mergedFaces by face count.
|
||||
s.horizonCapacity = 3 * s.M - 6;
|
||||
if ( s.horizonCapacity < 6 )
|
||||
{
|
||||
s.horizonCapacity = 6;
|
||||
}
|
||||
// Horizon/cone bounded by current half-edge count. Merged faces by face count.
|
||||
s.horizonCapacity = b3MaxInt( 6, 3 * s.M - 6 );
|
||||
s.coneCapacity = s.horizonCapacity;
|
||||
s.mergedFacesCapacity = 2 * s.M - 4;
|
||||
if ( s.mergedFacesCapacity < 4 )
|
||||
{
|
||||
s.mergedFacesCapacity = 4;
|
||||
}
|
||||
s.mergedFacesCapacity = b3MaxInt( 4, 2 * s.M - 4 );
|
||||
|
||||
// Horizon DFS depth is bounded by the number of live faces (Euler: <=2*M-4).
|
||||
s.horizonStackCapacity = 2 * s.M - 4;
|
||||
if ( s.horizonStackCapacity < 4 )
|
||||
{
|
||||
s.horizonStackCapacity = 4;
|
||||
}
|
||||
s.horizonStackCapacity = b3MaxInt( 4, 2 * s.M - 4 );
|
||||
|
||||
size_t offset = 0;
|
||||
|
||||
|
|
@ -2065,14 +2065,14 @@ b3HullData* b3CreateHull( const b3Vec3* points, int pointCount, int maxVertexCou
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if ( builder.finalVertexCount >= B3_MAX_HULL_VERTICES )
|
||||
if ( builder.finalVertexCount > B3_MAX_HULL_VERTICES )
|
||||
{
|
||||
b3Log( "hull final vertex count of %d exceeds limit of %d", builder.finalVertexCount, B3_MAX_HULL_VERTICES );
|
||||
b3Free( work, sizes.totalBytes );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ( builder.finalFaceCount >= B3_MAX_HULL_FACES )
|
||||
if ( builder.finalFaceCount > B3_MAX_HULL_FACES )
|
||||
{
|
||||
b3Log( "hull final face count of %d exceeds limit of %d", builder.finalFaceCount, B3_MAX_HULL_FACES );
|
||||
b3Free( work, sizes.totalBytes );
|
||||
|
|
@ -2080,20 +2080,20 @@ b3HullData* b3CreateHull( const b3Vec3* points, int pointCount, int maxVertexCou
|
|||
}
|
||||
|
||||
int maxHalfEdgeCount = 2 * B3_MAX_HULL_EDGES;
|
||||
if ( builder.finalHalfEdgeCount >= maxHalfEdgeCount )
|
||||
if ( builder.finalHalfEdgeCount > maxHalfEdgeCount )
|
||||
{
|
||||
b3Log( "hull final half edge count of %d exceeds limit of %d", builder.finalHalfEdgeCount, maxHalfEdgeCount );
|
||||
b3Free( work, sizes.totalBytes );
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Walk lists into temp arrays bounded by B3_HULL_LIMIT, stamping finalIndex on each node so
|
||||
// Walk lists into temp arrays bounded by B3_HULL_MAX_COUNT, stamping finalIndex on each node so
|
||||
// the resolution pass below is O(E + F) instead of O(E^2 + F^2).
|
||||
const b3QHVertex* tempVertices[B3_HULL_LIMIT];
|
||||
const b3QHVertex* tempVertices[B3_HULL_MAX_COUNT];
|
||||
int vertexCount = 0;
|
||||
for ( b3QHListNode* node = builder.vertexList.link.next; node != &builder.vertexList.link; node = node->next )
|
||||
{
|
||||
B3_ASSERT( vertexCount <= B3_HULL_LIMIT - 1 );
|
||||
B3_ASSERT( vertexCount < B3_HULL_MAX_COUNT );
|
||||
|
||||
b3QHVertex* vertex = (b3QHVertex*)node;
|
||||
vertex->finalIndex = vertexCount;
|
||||
|
|
@ -2101,15 +2101,14 @@ b3HullData* b3CreateHull( const b3Vec3* points, int pointCount, int maxVertexCou
|
|||
}
|
||||
|
||||
// Collect edges in twin-paired order (i, i+1) by stamping each pair as we discover it.
|
||||
// Replaces b3SortEdges' O(E^2) twin pairing.
|
||||
const b3QHFace* tempFaces[B3_HULL_LIMIT];
|
||||
const b3QHHalfEdge* tempEdges[B3_HULL_LIMIT];
|
||||
const b3QHFace* tempFaces[B3_HULL_MAX_COUNT];
|
||||
const b3QHHalfEdge* tempEdges[B3_HULL_MAX_COUNT];
|
||||
int faceCount = 0;
|
||||
int edgeCount = 0;
|
||||
|
||||
for ( b3QHListNode* faceNode = builder.faceList.link.next; faceNode != &builder.faceList.link; faceNode = faceNode->next )
|
||||
{
|
||||
B3_ASSERT( faceCount <= B3_HULL_LIMIT - 1 );
|
||||
B3_ASSERT( faceCount < B3_HULL_MAX_COUNT );
|
||||
|
||||
b3QHFace* face = (b3QHFace*)faceNode;
|
||||
face->finalIndex = faceCount;
|
||||
|
|
@ -2120,7 +2119,7 @@ b3HullData* b3CreateHull( const b3Vec3* points, int pointCount, int maxVertexCou
|
|||
{
|
||||
if ( edge->finalIndex < 0 )
|
||||
{
|
||||
B3_ASSERT( edgeCount + 1 <= B3_HULL_LIMIT - 1 );
|
||||
B3_ASSERT( edgeCount + 1 < B3_HULL_MAX_COUNT );
|
||||
|
||||
edge->finalIndex = edgeCount;
|
||||
tempEdges[edgeCount++] = edge;
|
||||
|
|
@ -2349,9 +2348,9 @@ b3HullData* b3CloneAndTransformHull( const b3HullData* original, b3Transform tra
|
|||
{
|
||||
const b3HullFace* face = faces + i;
|
||||
|
||||
uint8_t startEdgeIndex = face->edge;
|
||||
uint8_t currentEdgeIndex = startEdgeIndex;
|
||||
uint8_t prevEdgeIndex = UINT8_MAX;
|
||||
int startEdgeIndex = face->edge;
|
||||
int currentEdgeIndex = startEdgeIndex;
|
||||
int prevEdgeIndex = B3_NULL_INDEX;
|
||||
|
||||
do
|
||||
{
|
||||
|
|
@ -2367,7 +2366,7 @@ b3HullData* b3CloneAndTransformHull( const b3HullData* original, b3Transform tra
|
|||
}
|
||||
while ( currentEdgeIndex != startEdgeIndex );
|
||||
|
||||
B3_ASSERT( prevEdgeIndex != UINT8_MAX );
|
||||
B3_ASSERT( prevEdgeIndex != B3_NULL_INDEX );
|
||||
|
||||
currentEdgeIndex = startEdgeIndex;
|
||||
|
||||
|
|
@ -2375,7 +2374,7 @@ b3HullData* b3CloneAndTransformHull( const b3HullData* original, b3Transform tra
|
|||
{
|
||||
b3HullHalfEdge* edge = edges + currentEdgeIndex;
|
||||
uint8_t nextIndex = edge->next;
|
||||
edge->next = prevEdgeIndex;
|
||||
edge->next = (uint8_t)prevEdgeIndex;
|
||||
|
||||
if ( currentEdgeIndex < edge->twin )
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue