From 7d71713bea2268c5154b60947ab447028f510d76 Mon Sep 17 00:00:00 2001 From: Irlan <-> Date: Thu, 19 Apr 2018 19:42:37 -0300 Subject: [PATCH] store hull features on the stack --- include/bounce/collision/shapes/qhull.h | 25 ++++++++++++-- src/bounce/collision/shapes/qhull.cpp | 46 +++---------------------- 2 files changed, 28 insertions(+), 43 deletions(-) diff --git a/include/bounce/collision/shapes/qhull.h b/include/bounce/collision/shapes/qhull.h index d71b7cf..c17738a 100644 --- a/include/bounce/collision/shapes/qhull.h +++ b/include/bounce/collision/shapes/qhull.h @@ -21,10 +21,31 @@ #include +// The maximum number of vertices that a hull can have. +#define B3_MAX_HULL_VERTICES (256) + +// The maximum number of half-edges a hull can have. +#define B3_MAX_HULL_EDGES (256) + +// The maximum number of faces and planes a hull can have. +#define B3_MAX_HULL_FACES (256) + +// This hull can be constructed from an array of points. struct b3QHull : public b3Hull { - b3QHull(); - ~b3QHull(); + b3Vec3 hullVertices[B3_MAX_HULL_VERTICES]; + b3HalfEdge hullEdges[B3_MAX_HULL_EDGES]; + b3Face hullFaces[B3_MAX_HULL_FACES]; + b3Plane hullPlanes[B3_MAX_HULL_FACES]; + + // + b3QHull() + { + vertices = hullVertices; + edges = hullEdges; + faces = hullFaces; + planes = hullPlanes; + } // Create a convex hull from an array of points. // If the points define a degenerate polyhedron the hull is not overwritten. diff --git a/src/bounce/collision/shapes/qhull.cpp b/src/bounce/collision/shapes/qhull.cpp index 1ad4083..ed9b2f8 100644 --- a/src/bounce/collision/shapes/qhull.cpp +++ b/src/bounce/collision/shapes/qhull.cpp @@ -19,14 +19,6 @@ #include #include -// Euler's formula -// V - E + F = 2 -//#define B3_MAX_HULL_VERTICES 87 -//#define B3_MAX_HULL_EDGES (3 * B3_MAX_HULL_VERTICES - 6) -//#define B3_MAX_HULL_FACES (2 * B3_MAX_HULL_VERTICES - 4) - -#define B3_MAX_HULL_FEATURES 256 - #define B3_NULL_HULL_FEATURE 0xFF // @@ -61,26 +53,6 @@ struct b3PointerMap b3StackArray m_entries; }; -b3QHull::b3QHull() -{ - centroid.SetZero(); - vertexCount = 0; - vertices = NULL; - edgeCount = 0; - edges = NULL; - faceCount = 0; - faces = NULL; - planes = NULL; -} - -b3QHull::~b3QHull() -{ - b3Free(vertices); - b3Free(edges); - b3Free(faces); - b3Free(planes); -} - // static b3Vec3 b3ComputeCentroid(b3QHull* hull) { @@ -151,7 +123,7 @@ static b3Vec3 b3ComputeCentroid(b3QHull* hull) void b3QHull::Set(const b3Vec3* points, u32 count) { // Copy points into local buffer, remove coincident points. - b3StackArray ps; + b3StackArray ps; for (u32 i = 0; i < count; ++i) { b3Vec3 p = points[i]; @@ -209,27 +181,19 @@ void b3QHull::Set(const b3Vec3* points, u32 count) face = face->next; } - if (V > B3_MAX_HULL_FEATURES || E > B3_MAX_HULL_FEATURES || F > B3_MAX_HULL_FEATURES) + if (V > B3_MAX_HULL_VERTICES || E > B3_MAX_HULL_EDGES || F > B3_MAX_HULL_FACES) { b3Free(qh_memory); return; } - b3Free(vertices); - b3Free(edges); - b3Free(faces); - b3Free(planes); - + // Convert the constructed hull into a run-time hull. vertexCount = 0; - vertices = (b3Vec3*)b3Alloc(V * sizeof(b3Vec3)); edgeCount = 0; - edges = (b3HalfEdge*)b3Alloc(E * sizeof(b3HalfEdge)); faceCount = 0; - faces = (b3Face*)b3Alloc(F * sizeof(b3Face)); - planes = (b3Plane*)b3Alloc(F * sizeof(b3Plane)); - b3PointerMap vertexMap; - b3PointerMap edgeMap; + b3PointerMap vertexMap; + b3PointerMap edgeMap; face = faceList.head; while (face)