cleanup quickhull

This commit is contained in:
Irlan
2018-04-17 01:53:36 -03:00
parent fd04c35886
commit 5e0a010881
2 changed files with 29 additions and 26 deletions

View File

@ -32,7 +32,6 @@ struct qhList
u32 count; u32 count;
}; };
// Half-edge data structure definition used by qhHull.
struct qhHalfEdge; struct qhHalfEdge;
struct qhVertex; struct qhVertex;
@ -90,16 +89,6 @@ struct qhVertex
qhFace* conflictFace; qhFace* conflictFace;
}; };
// todo
// Snapshots of the algorithm for debug drawing.
struct qhDraw
{
//DrawIteration* iter; // current iteration
//b3Array<DrawIteration> iterations;
};
class b3Draw;
// Given a number of points return the required memory size in bytes for constructing the // Given a number of points return the required memory size in bytes for constructing the
// convex hull of those points. Use this function before allocating the memory buffer passed // convex hull of those points. Use this function before allocating the memory buffer passed
// as argument to Construct. // as argument to Construct.
@ -119,15 +108,23 @@ public:
// Use qhGetMemorySize to see how many free bytes should be available in the buffer. // Use qhGetMemorySize to see how many free bytes should be available in the buffer.
void Construct(void* memory, const b3Array<b3Vec3>& vertices); void Construct(void* memory, const b3Array<b3Vec3>& vertices);
// Output of qhHull. // Get the list of faces in this hull.
// todo const qhList<qhFace>& GetFaceList() const
// Output a cleaner data structure. Maybe similar to b3Hull but storing larger hulls? {
qhList<qhFace> m_faceList; // convex hull return m_faceList;
u32 m_iteration; // number of quickhull iterations }
// Get the number of iterations this algorithm ran.
u32 GetIterations() const
{
return m_iteration;
}
// Return if this hull is valid.
bool IsConsistent() const; bool IsConsistent() const;
void Draw(b3Draw* draw) const; // Draw this hull.
void Draw() const;
private: private:
bool BuildInitialHull(const b3Array<b3Vec3>& vertices); bool BuildInitialHull(const b3Array<b3Vec3>& vertices);
@ -154,6 +151,12 @@ private:
// Coplanarity tolerance // Coplanarity tolerance
float32 m_tolerance; float32 m_tolerance;
// List of faces
qhList<qhFace> m_faceList; // list of faces
// Number of Quickhull iterations
u32 m_iteration;
// Memory // Memory
qhVertex* AllocateVertex(); qhVertex* AllocateVertex();
void FreeVertex(qhVertex* p); void FreeVertex(qhVertex* p);
@ -167,6 +170,7 @@ private:
qhVertex* m_freeVertices; qhVertex* m_freeVertices;
qhHalfEdge* m_freeEdges; qhHalfEdge* m_freeEdges;
qhFace* m_freeFaces; qhFace* m_freeFaces;
}; };
#include <bounce/quickhull/qh_hull.inl> #include <bounce/quickhull/qh_hull.inl>

View File

@ -20,7 +20,7 @@
#include <bounce/common/template/stack.h> #include <bounce/common/template/stack.h>
#include <bounce/common/draw.h> #include <bounce/common/draw.h>
float32 qhFindAABB(u32 iMin[3], u32 iMax[3], const b3Array<b3Vec3>& vertices) static float32 qhFindAABB(u32 iMin[3], u32 iMax[3], const b3Array<b3Vec3>& vertices)
{ {
b3Vec3 min(B3_MAX_FLOAT, B3_MAX_FLOAT, B3_MAX_FLOAT); b3Vec3 min(B3_MAX_FLOAT, B3_MAX_FLOAT, B3_MAX_FLOAT);
iMin[0] = 0; iMin[0] = 0;
@ -65,6 +65,8 @@ qhHull::~qhHull()
void qhHull::Construct(void* memory, const b3Array<b3Vec3>& vs) void qhHull::Construct(void* memory, const b3Array<b3Vec3>& vs)
{ {
// Euler's formula
// V - E + F = 2
u32 V = vs.Count(); u32 V = vs.Count();
u32 E = 3 * V - 6; u32 E = 3 * V - 6;
u32 HE = 2 * E; u32 HE = 2 * E;
@ -73,9 +75,6 @@ void qhHull::Construct(void* memory, const b3Array<b3Vec3>& vs)
HE *= 2; HE *= 2;
F *= 2; F *= 2;
// Euler's formula
// V - E + F = 2
m_freeVertices = NULL; m_freeVertices = NULL;
qhVertex* vertices = (qhVertex*)memory; qhVertex* vertices = (qhVertex*)memory;
for (u32 i = 0; i < V; ++i) for (u32 i = 0; i < V; ++i)
@ -739,7 +738,7 @@ bool qhHull::IsConsistent() const
return true; return true;
} }
void qhHull::Draw(b3Draw* draw) const void qhHull::Draw() const
{ {
qhFace* face = m_faceList.head; qhFace* face = m_faceList.head;
while (face) while (face)
@ -757,17 +756,17 @@ void qhHull::Draw(b3Draw* draw) const
edge = edge->next; edge = edge->next;
} while (edge != begin); } while (edge != begin);
draw->DrawSolidPolygon(n, vs.Begin(), vs.Count(), b3Color(1.0f, 1.0f, 1.0f, 0.5f)); b3Draw_draw->DrawSolidPolygon(n, vs.Begin(), vs.Count(), b3Color(1.0f, 1.0f, 1.0f, 0.5f));
qhVertex* v = face->conflictList.head; qhVertex* v = face->conflictList.head;
while (v) while (v)
{ {
draw->DrawPoint(v->position, 4.0f, b3Color(1.0f, 1.0f, 0.0f)); b3Draw_draw->DrawPoint(v->position, 4.0f, b3Color(1.0f, 1.0f, 0.0f));
draw->DrawSegment(c, v->position, b3Color(1.0f, 1.0f, 0.0f)); b3Draw_draw->DrawSegment(c, v->position, b3Color(1.0f, 1.0f, 0.0f));
v = v->next; v = v->next;
} }
draw->DrawSegment(c, c + n, b3Color(1.0f, 1.0f, 1.0f)); b3Draw_draw->DrawSegment(c, c + n, b3Color(1.0f, 1.0f, 1.0f));
face = face->next; face = face->next;
} }