diff --git a/include/bounce/quickhull/qh_hull.h b/include/bounce/quickhull/qh_hull.h index 0385101..35da4bb 100644 --- a/include/bounce/quickhull/qh_hull.h +++ b/include/bounce/quickhull/qh_hull.h @@ -67,6 +67,14 @@ struct qhFace struct qhHalfEdge { + enum State + { + e_used, + e_deleted + }; + + State state; + qhHalfEdge* freeNext; qhVertex* tail; diff --git a/include/bounce/quickhull/qh_hull.inl b/include/bounce/quickhull/qh_hull.inl index fa815c6..6972da7 100644 --- a/include/bounce/quickhull/qh_hull.inl +++ b/include/bounce/quickhull/qh_hull.inl @@ -121,11 +121,7 @@ inline void qhFace::ComputeCenterAndPlane() // Given a number of points return the required memory size in // bytes for constructing the convex hull of those points. -// This function uses constant expression (C++11). Therefore, you can evaluate -// its value at compile-time. That is particularly usefull when you want to -// create a stack buffer from a constant number of vertices. -// Due to the constexpr specifier, this function is automatically inlined. -constexpr u32 qhGetBufferSize(u32 pointCount) +inline u32 qhGetBufferSize(u32 pointCount) { u32 size = 0; @@ -179,12 +175,14 @@ inline void qhHull::FreeVertex(qhVertex* v) inline qhHalfEdge* qhHull::AllocateEdge() { qhHalfEdge* e = m_freeEdges; + e->state = qhHalfEdge::e_used; m_freeEdges = e->freeNext; return e; } inline void qhHull::FreeEdge(qhHalfEdge* e) { + e->state = qhHalfEdge::e_deleted; e->freeNext = m_freeEdges; m_freeEdges = e; } diff --git a/src/bounce/collision/shapes/qhull.cpp b/src/bounce/collision/shapes/qhull.cpp index 583bd66..d67dc7c 100644 --- a/src/bounce/collision/shapes/qhull.cpp +++ b/src/bounce/collision/shapes/qhull.cpp @@ -172,8 +172,8 @@ void b3QHull::Set(const b3Vec3* points, u32 count) // Create a convex hull. // Allocate memory buffer for the worst case. - const u32 qhBufferSize = qhGetBufferSize(B3_MAX_HULL_VERTICES); - u8 qhBuffer[qhBufferSize]; + u32 qhBufferSize = qhGetBufferSize(B3_MAX_HULL_VERTICES); + void* qhBuffer = b3Alloc(qhBufferSize); // Build qhHull hull; @@ -191,6 +191,7 @@ void b3QHull::Set(const b3Vec3* points, u32 count) if (fs_count == B3_MAX_HULL_FACES) { // Face excess + b3Free(qhBuffer); return; } @@ -206,6 +207,7 @@ void b3QHull::Set(const b3Vec3* points, u32 count) if (iv == B3_MAX_HULL_VERTICES) { // Vertex excess + b3Free(qhBuffer); return; } @@ -214,6 +216,7 @@ void b3QHull::Set(const b3Vec3* points, u32 count) if (iedge == B3_MAX_HULL_EDGES) { // Half-edge excess + b3Free(qhBuffer); return; } @@ -222,6 +225,7 @@ void b3QHull::Set(const b3Vec3* points, u32 count) if (itwin == B3_MAX_HULL_EDGES) { // Half-edge excess + b3Free(qhBuffer); return; } @@ -271,6 +275,8 @@ void b3QHull::Set(const b3Vec3* points, u32 count) ++iface; } + b3Free(qhBuffer); + B3_ASSERT(vs.count <= B3_MAX_HULL_VERTICES); vertexCount = vs.count; diff --git a/src/bounce/quickhull/qh_hull.cpp b/src/bounce/quickhull/qh_hull.cpp index e8737bb..f29880f 100644 --- a/src/bounce/quickhull/qh_hull.cpp +++ b/src/bounce/quickhull/qh_hull.cpp @@ -768,6 +768,8 @@ void qhHull::Validate(const qhFace* face) const const qhHalfEdge* edge = begin; do { + B3_ASSERT(edge->state != qhHalfEdge::e_deleted); + B3_ASSERT(edge->face == face); qhHalfEdge* twin = edge->twin;