fix #35; check edge validity;
This commit is contained in:
		| @@ -67,6 +67,14 @@ struct qhFace | |||||||
|  |  | ||||||
| struct qhHalfEdge | struct qhHalfEdge | ||||||
| { | { | ||||||
|  | 	enum State | ||||||
|  | 	{ | ||||||
|  | 		e_used, | ||||||
|  | 		e_deleted | ||||||
|  | 	}; | ||||||
|  | 	 | ||||||
|  | 	State state; | ||||||
|  |  | ||||||
| 	qhHalfEdge* freeNext; | 	qhHalfEdge* freeNext; | ||||||
|  |  | ||||||
| 	qhVertex* tail; | 	qhVertex* tail; | ||||||
|   | |||||||
| @@ -121,11 +121,7 @@ inline void qhFace::ComputeCenterAndPlane() | |||||||
|  |  | ||||||
| // Given a number of points return the required memory size in  | // Given a number of points return the required memory size in  | ||||||
| // bytes for constructing the convex hull of those points.  | // bytes for constructing the convex hull of those points.  | ||||||
| // This function uses constant expression (C++11). Therefore, you can evaluate  | inline u32 qhGetBufferSize(u32 pointCount) | ||||||
| // 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) |  | ||||||
| { | { | ||||||
| 	u32 size = 0; | 	u32 size = 0; | ||||||
|  |  | ||||||
| @@ -179,12 +175,14 @@ inline void qhHull::FreeVertex(qhVertex* v) | |||||||
| inline qhHalfEdge* qhHull::AllocateEdge() | inline qhHalfEdge* qhHull::AllocateEdge() | ||||||
| { | { | ||||||
| 	qhHalfEdge* e = m_freeEdges; | 	qhHalfEdge* e = m_freeEdges; | ||||||
|  | 	e->state = qhHalfEdge::e_used; | ||||||
| 	m_freeEdges = e->freeNext; | 	m_freeEdges = e->freeNext; | ||||||
| 	return e; | 	return e; | ||||||
| } | } | ||||||
|  |  | ||||||
| inline void qhHull::FreeEdge(qhHalfEdge* e) | inline void qhHull::FreeEdge(qhHalfEdge* e) | ||||||
| { | { | ||||||
|  | 	e->state = qhHalfEdge::e_deleted; | ||||||
| 	e->freeNext = m_freeEdges; | 	e->freeNext = m_freeEdges; | ||||||
| 	m_freeEdges = e; | 	m_freeEdges = e; | ||||||
| } | } | ||||||
|   | |||||||
| @@ -172,8 +172,8 @@ void b3QHull::Set(const b3Vec3* points, u32 count) | |||||||
| 	// Create a convex hull. | 	// Create a convex hull. | ||||||
| 	 | 	 | ||||||
| 	// Allocate memory buffer for the worst case. | 	// Allocate memory buffer for the worst case. | ||||||
| 	const u32 qhBufferSize = qhGetBufferSize(B3_MAX_HULL_VERTICES); | 	u32 qhBufferSize = qhGetBufferSize(B3_MAX_HULL_VERTICES); | ||||||
| 	u8 qhBuffer[qhBufferSize]; | 	void* qhBuffer = b3Alloc(qhBufferSize); | ||||||
|  |  | ||||||
| 	// Build | 	// Build | ||||||
| 	qhHull hull; | 	qhHull hull; | ||||||
| @@ -191,6 +191,7 @@ void b3QHull::Set(const b3Vec3* points, u32 count) | |||||||
| 		if (fs_count == B3_MAX_HULL_FACES) | 		if (fs_count == B3_MAX_HULL_FACES) | ||||||
| 		{ | 		{ | ||||||
| 			// Face excess | 			// Face excess | ||||||
|  | 			b3Free(qhBuffer); | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
| 		 | 		 | ||||||
| @@ -206,6 +207,7 @@ void b3QHull::Set(const b3Vec3* points, u32 count) | |||||||
| 			if (iv == B3_MAX_HULL_VERTICES) | 			if (iv == B3_MAX_HULL_VERTICES) | ||||||
| 			{ | 			{ | ||||||
| 				// Vertex excess | 				// Vertex excess | ||||||
|  | 				b3Free(qhBuffer); | ||||||
| 				return; | 				return; | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| @@ -214,6 +216,7 @@ void b3QHull::Set(const b3Vec3* points, u32 count) | |||||||
| 			if (iedge == B3_MAX_HULL_EDGES) | 			if (iedge == B3_MAX_HULL_EDGES) | ||||||
| 			{ | 			{ | ||||||
| 				// Half-edge excess | 				// Half-edge excess | ||||||
|  | 				b3Free(qhBuffer); | ||||||
| 				return; | 				return; | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| @@ -222,6 +225,7 @@ void b3QHull::Set(const b3Vec3* points, u32 count) | |||||||
| 			if (itwin == B3_MAX_HULL_EDGES) | 			if (itwin == B3_MAX_HULL_EDGES) | ||||||
| 			{ | 			{ | ||||||
| 				// Half-edge excess | 				// Half-edge excess | ||||||
|  | 				b3Free(qhBuffer); | ||||||
| 				return; | 				return; | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| @@ -271,6 +275,8 @@ void b3QHull::Set(const b3Vec3* points, u32 count) | |||||||
| 		++iface; | 		++iface; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	b3Free(qhBuffer); | ||||||
|  |  | ||||||
| 	B3_ASSERT(vs.count <= B3_MAX_HULL_VERTICES); | 	B3_ASSERT(vs.count <= B3_MAX_HULL_VERTICES); | ||||||
| 	vertexCount = vs.count; | 	vertexCount = vs.count; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -768,6 +768,8 @@ void qhHull::Validate(const qhFace* face) const | |||||||
| 	const qhHalfEdge* edge = begin; | 	const qhHalfEdge* edge = begin; | ||||||
| 	do | 	do | ||||||
| 	{ | 	{ | ||||||
|  | 		B3_ASSERT(edge->state != qhHalfEdge::e_deleted); | ||||||
|  |  | ||||||
| 		B3_ASSERT(edge->face == face); | 		B3_ASSERT(edge->face == face); | ||||||
|  |  | ||||||
| 		qhHalfEdge* twin = edge->twin; | 		qhHalfEdge* twin = edge->twin; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user