Added improved convex hull computation utility based on Preparata and Hong

Thanks to Ole Kniemeyer, MAXON, See also http://code.google.com/p/bullet/issues/detail?id=275
The ConvexHullDistanceDemo uses it to visualize the convex hull of a point cloud.
This commit is contained in:
erwin.coumans
2011-03-22 00:53:17 +00:00
parent 5454652e44
commit f44cb95b2a
6 changed files with 2996 additions and 10 deletions

View File

@@ -15,9 +15,13 @@ IF (USE_GLUT)
RagdollDemo ForkLiftDemo BasicDemo FractureDemo Box2dDemo BspDemo MovingConcaveDemo VehicleDemo
UserCollisionAlgorithm CharacterDemo SoftDemo HeightFieldFluidDemo
CollisionInterfaceDemo ConcaveConvexcastDemo SimplexDemo DynamicControlDemo
ConvexHullDistance
DoublePrecisionDemo ConcaveDemo CollisionDemo
ContinuousConvexCollision ConcaveRaycastDemo GjkConvexCastDemo
MultiMaterialDemo SerializeDemo InternalEdgeDemo
MultiThreadedDemo
ParticlesOpenCL
OpenCLClothDemo
)
ELSE()
SET(SharedDemoSubdirs
@@ -29,10 +33,7 @@ IF (USE_GLUT)
${SharedDemoSubdirs}
Benchmarks
ThreadingDemo
MultiThreadedDemo
VectorAdd_OpenCL
ParticlesOpenCL
OpenCLClothDemo
)
ENDIF(GLUT_FOUND)
ELSE (USE_GLUT)

View File

@@ -29,6 +29,24 @@ ELSE()
)
ENDIF()
IF (WIN32)
IF (NOT INTERNAL_CREATE_DISTRIBUTABLE_MSVC_PROJECTFILES)
IF (CMAKE_CL_64)
ADD_CUSTOM_COMMAND(
TARGET AppCollisionDemo
POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/glut64.dll ${CMAKE_CURRENT_BINARY_DIR}
)
ELSE(CMAKE_CL_64)
ADD_CUSTOM_COMMAND(
TARGET AppCollisionDemo
POST_BUILD
COMMAND ${CMAKE_COMMAND} ARGS -E copy_if_different ${BULLET_PHYSICS_SOURCE_DIR}/GLUT32.DLL ${CMAKE_CURRENT_BINARY_DIR}
)
ENDIF(CMAKE_CL_64)
ENDIF (NOT INTERNAL_CREATE_DISTRIBUTABLE_MSVC_PROJECTFILES)
ENDIF(WIN32)
IF (INTERNAL_ADD_POSTFIX_EXECUTABLE_NAMES)
SET_TARGET_PROPERTIES(AppCollisionDemo PROPERTIES DEBUG_POSTFIX "_Debug")
SET_TARGET_PROPERTIES(AppCollisionDemo PROPERTIES MINSIZEREL_POSTFIX "_MinsizeRel")

View File

@@ -32,6 +32,61 @@ subject to the following restrictions:
#include "BulletCollision/NarrowPhaseCollision/btVoronoiSimplexSolver.h"
#include "BulletCollision/NarrowPhaseCollision/btConvexPenetrationDepthSolver.h"
#include "LinearMath/btIDebugDraw.h"
#include "LinearMath/btConvexHullComputer.h"
#define TaruVtxCount 43
static float TaruVtx[] = {
1.08664f,-1.99237f,0.0f,
0.768369f,-1.99237f,-0.768369f,
1.28852f,1.34412e-007f,-1.28852f,
1.82224f,1.90735e-007f,0.0f,
0.0f,-1.99237f,-1.08664f,
0.0f,0.0f,-1.82224f,
0.0f,-1.99237f,-1.08664f,
-0.768369f,-1.99237f,-0.768369f,
-1.28852f,1.34412e-007f,-1.28852f,
0.0f,0.0f,-1.82224f,
-1.08664f,-1.99237f,1.82086e-007f,
-1.82224f,1.90735e-007f,1.59305e-007f,
-0.768369f,-1.99237f,0.76837f,
-1.28852f,2.47058e-007f,1.28852f,
1.42495e-007f,-1.99237f,1.08664f,
2.38958e-007f,2.70388e-007f,1.82224f,
0.768369f,-1.99237f,0.768369f,
1.28852f,2.47058e-007f,1.28852f,
0.768369f,1.99237f,-0.768369f,
1.08664f,1.99237f,0.0f,
0.0f,1.99237f,-1.08664f,
-0.768369f,1.99237f,-0.768369f,
0.0f,1.99237f,-1.08664f,
-1.08664f,1.99237f,0.0f,
-0.768369f,1.99237f,0.768369f,
1.42495e-007f,1.99237f,1.08664f,
0.768369f,1.99237f,0.768369f,
1.42495e-007f,-1.99237f,1.08664f,
-0.768369f,-1.99237f,0.76837f,
-1.08664f,-1.99237f,1.82086e-007f,
-0.768369f,-1.99237f,-0.768369f,
0.0f,-1.99237f,-1.08664f,
0.768369f,-1.99237f,-0.768369f,
1.08664f,-1.99237f,0.0f,
0.768369f,-1.99237f,0.768369f,
0.768369f,1.99237f,-0.768369f,
0.0f,1.99237f,-1.08664f,
-0.768369f,1.99237f,-0.768369f,
-1.08664f,1.99237f,0.0f,
-0.768369f,1.99237f,0.768369f,
1.42495e-007f,1.99237f,1.08664f,
0.768369f,1.99237f,0.768369f,
1.08664f,1.99237f,0.0f,
};
#define USE_GJK
@@ -72,7 +127,7 @@ void clientResetScene()
tr[1].setOrigin(btVector3(0.0f,9.f,2.f));
}
int debugMode = 0;//btIDebugDraw::DBG_DrawWireframe;
int debugMode = btIDebugDraw::DBG_DrawWireframe;
GL_ShapeDrawer shapeDrawer;
int m_glutScreenWidth=0;
int m_glutScreenHeight=0;
@@ -188,13 +243,12 @@ int main(int argc,char** argv)
tr[1].setBasis(basisB);
btVector3 points0[3]={btVector3(1,0,0),btVector3(0,1,0),btVector3(0,0,1)};
btVector3 points1[5]={btVector3(1,0,0),btVector3(0,1,0),btVector3(0,0,1),btVector3(0,0,-1),btVector3(-1,-1,0)};
//btVector3 points1[5]={btVector3(1,0,0),btVector3(0,1,0),btVector3(0,0,1),btVector3(0,0,-1),btVector3(-1,-1,0)};
btConvexHullShape hullA(&points0[0].getX(),3);
btConvexHullShape hullB(&points1[0].getX(),5);
btCylinderShape cylinder(btVector3(0.3,1,1));
shapePtr[0] = &cylinder;//hullA;
btConvexHullShape hullB(TaruVtx,TaruVtxCount,3*sizeof(float));
shapePtr[0] = &hullA;
shapePtr[1] = &hullB;
@@ -209,6 +263,7 @@ int main(int argc,char** argv)
static btVoronoiSimplexSolver sGjkSimplexSolver;
btSimplexSolverInterface& gGjkSimplexSolver = sGjkSimplexSolver;
#include <stdio.h>
void clientDisplay(void) {
@@ -291,7 +346,65 @@ void clientDisplay(void) {
tr[i].getOpenGLMatrix( m );
shapeDrawer.drawOpenGL(m,shapePtr[i],btVector3(1,1,1),debugMode, worldMin, worldMax);
if (debugMode)
{
/// for polyhedral shapes
if (shapePtr[i]->isPolyhedral())
{
if (!shapePtr[i]->getUserPointer())
{
btConvexHullComputer* convexUtil = new btConvexHullComputer();
shapePtr[i]->setUserPointer(convexUtil);
btPolyhedralConvexShape* polyshape = (btPolyhedralConvexShape*) shapePtr[i];
btAlignedObjectArray<btVector3> vertices;
vertices.resize(polyshape->getNumVertices());
for (int i=0;i<polyshape->getNumVertices();i++)
{
polyshape->getVertex(i,vertices[i]);
}
bool useDoublePrecision = false;
convexUtil->compute(&vertices[0].getX(),sizeof(btVector3), polyshape->getNumVertices(),0,0);
}
if (shapePtr[i]->getUserPointer())
{
btConvexHullComputer* convexUtil = (btConvexHullComputer*)shapePtr[i]->getUserPointer();
//printf("num faces = %d\n",convexUtil->faces.size());
for (int j=0;j<convexUtil->faces.size();j++)
{
int face = convexUtil->faces[j];
//printf("face=%d\n",face);
const btConvexHullComputer::Edge* firstEdge = &convexUtil->edges[face];
const btConvexHullComputer::Edge* edge = firstEdge;
do
{
int src = edge->getSourceVertex();
int targ = edge->getTargetVertex();
//printf("src=%d target = %d\n", src,targ);
btVector3 wa = tr[i] * convexUtil->vertices[src];
btVector3 wb = tr[i] * convexUtil->vertices[targ];
glBegin(GL_LINES);
glColor3f(1, 1, 1);
glVertex3f(wa.getX(),wa.getY(),wa.getZ());
glVertex3f(wb.getX(),wb.getY(),wb.getZ());
glEnd();
edge = edge->getNextEdgeOfFace();
} while (edge!=firstEdge);
}
}
}
} else
{
shapeDrawer.drawOpenGL(m,shapePtr[i],btVector3(1,1,1),debugMode, worldMin, worldMax);
}
}

View File

@@ -6,6 +6,7 @@ INCLUDE_DIRECTORIES(
SET(LinearMath_SRCS
btAlignedAllocator.cpp
btConvexHull.cpp
btConvexHullComputer.cpp
btGeometryUtil.cpp
btQuickprof.cpp
btSerializer.cpp
@@ -16,6 +17,7 @@ SET(LinearMath_HDRS
btAlignedAllocator.h
btAlignedObjectArray.h
btConvexHull.h
btConvexHullComputer.h
btDefaultMotionState.h
btGeometryUtil.h
btHashMap.h

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,103 @@
/*
Copyright (c) 2011 Ole Kniemeyer, MAXON, www.maxon.net
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#ifndef _CONVEX_HULL_COMPUTER_H
#define _CONVEX_HULL_COMPUTER_H
#include "btVector3.h"
#include "btAlignedObjectArray.h"
/// Convex hull implementation based on Preparata and Hong
/// See http://code.google.com/p/bullet/issues/detail?id=275
/// Ole Kniemeyer, MAXON Computer GmbH
class btConvexHullComputer
{
private:
btScalar compute(const void* coords, bool doubleCoords, int stride, int count, btScalar shrink, btScalar shrinkClamp);
public:
class Edge
{
private:
int next;
int reverse;
int targetVertex;
friend class btConvexHullComputer;
public:
int getSourceVertex() const
{
return (this + reverse)->targetVertex;
}
int getTargetVertex() const
{
return targetVertex;
}
const Edge* getNextEdgeOfVertex() const // counter-clockwise list of all edges of a vertex
{
return this + next;
}
const Edge* getNextEdgeOfFace() const // clockwise list of all edges of a face
{
return (this + reverse)->getNextEdgeOfVertex();
}
const Edge* getReverseEdge() const
{
return this + reverse;
}
};
// Vertices of the output hull
btAlignedObjectArray<btVector3> vertices;
// Edges of the output hull
btAlignedObjectArray<Edge> edges;
// Faces of the convex hull. Each entry is an index into the "edges" array pointing to an edge of the face. Faces are planar n-gons
btAlignedObjectArray<int> faces;
/*
Compute convex hull of "count" vertices stored in "coords". "stride" is the difference in bytes
between the addresses of consecutive vertices. If "shrink" is positive, the convex hull is shrunken
by that amount (each face is moved by "shrink" length units towards the center along its normal).
If "shrinkClamp" is positive, "shrink" is clamped to not exceed "shrinkClamp * innerRadius", where "innerRadius"
is the minimum distance of a face to the center of the convex hull.
The returned value is the amount by which the hull has been shrunken. If it is negative, the amount was so large
that the resulting convex hull is empty.
The output convex hull can be found in the member variables "vertices", "edges", "faces".
*/
btScalar compute(const float* coords, int stride, int count, btScalar shrink, btScalar shrinkClamp)
{
return compute(coords, false, stride, count, shrink, shrinkClamp);
}
// same as above, but double precision
btScalar compute(const double* coords, int stride, int count, btScalar shrink, btScalar shrinkClamp)
{
return compute(coords, true, stride, count, shrink, shrinkClamp);
}
};
#endif //_CONVEX_HULL_COMPUTER_H