1 #ifndef CONVEX_DECOMPOSITION_H
3 #define CONVEX_DECOMPOSITION_H
5 /*----------------------------------------------------------------------
6 Copyright (c) 2004 Open Dynamics Framework Group
10 Redistribution and use in source and binary forms, with or without modification, are permitted provided
11 that the following conditions are met:
13 Redistributions of source code must retain the above copyright notice, this list of conditions
14 and the following disclaimer.
16 Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
20 Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
21 be used to endorse or promote products derived from this software without specific prior written permission.
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25 DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 -----------------------------------------------------------------------*/
32 // http://codesuppository.blogspot.com
34 // mailto: jratcliff@infiniplex.net
36 // http://www.amillionpixels.us
41 #include <memory.h> //memcpy
45 #include "LinearMath/btAlignedObjectArray.h"
49 extern unsigned int MAXDEPTH ;
50 extern float CONCAVE_PERCENT ;
51 extern float MERGE_PERCENT ;
54 typedef btAlignedObjectArray< unsigned int > UintVector;
58 namespace ConvexDecomposition
72 ConvexResult(unsigned int hvcount,const float *hvertices,unsigned int htcount,const unsigned int *hindices)
74 mHullVcount = hvcount;
77 mHullVertices = new float[mHullVcount*sizeof(float)*3];
78 memcpy(mHullVertices, hvertices, sizeof(float)*3*mHullVcount );
85 mHullTcount = htcount;
89 mHullIndices = new unsigned int[sizeof(unsigned int)*mHullTcount*3];
90 memcpy(mHullIndices,hindices, sizeof(unsigned int)*mHullTcount*3 );
99 ConvexResult(const ConvexResult &r)
101 mHullVcount = r.mHullVcount;
104 mHullVertices = new float[mHullVcount*sizeof(float)*3];
105 memcpy(mHullVertices, r.mHullVertices, sizeof(float)*3*mHullVcount );
111 mHullTcount = r.mHullTcount;
114 mHullIndices = new unsigned int[sizeof(unsigned int)*mHullTcount*3];
115 memcpy(mHullIndices, r.mHullIndices, sizeof(unsigned int)*mHullTcount*3 );
125 delete [] mHullVertices;
126 delete [] mHullIndices;
130 unsigned int mHullVcount;
131 float * mHullVertices;
132 unsigned int mHullTcount;
133 unsigned int *mHullIndices;
135 float mHullVolume; // the volume of the convex hull.
137 float mOBBSides[3]; // the width, height and breadth of the best fit OBB
138 float mOBBCenter[3]; // the center of the OBB
139 float mOBBOrientation[4]; // the quaternion rotation of the OBB.
140 float mOBBTransform[16]; // the 4x4 transform of the OBB.
141 float mOBBVolume; // the volume of the OBB
143 float mSphereRadius; // radius and center of best fit sphere
144 float mSphereCenter[3];
145 float mSphereVolume; // volume of the best fit sphere
151 class ConvexDecompInterface
154 virtual ~ConvexDecompInterface() {};
155 virtual void ConvexDebugTri(const float *p1,const float *p2,const float *p3,unsigned int color) { };
156 virtual void ConvexDebugPoint(const float *p,float dist,unsigned int color) { };
157 virtual void ConvexDebugBound(const float *bmin,const float *bmax,unsigned int color) { };
158 virtual void ConvexDebugOBB(const float *sides, const float *matrix,unsigned int color) { };
160 virtual void ConvexDecompResult(ConvexResult &result) = 0;
166 // just to avoid passing a zillion parameters to the method the
167 // options are packed into this descriptor.
185 // describes the input triangle.
186 unsigned int mVcount; // the number of vertices in the source mesh.
187 const float *mVertices; // start of the vertex position array. Assumes a stride of 3 floats.
188 unsigned int mTcount; // the number of triangles in the source mesh.
189 unsigned int *mIndices; // the indexed triangle list array (zero index based)
192 unsigned int mDepth; // depth to split, a maximum of 10, generally not over 7.
193 float mCpercent; // the concavity threshold percentage. 0=20 is reasonable.
194 float mPpercent; // the percentage volume conservation threshold to collapse hulls. 0-30 is reasonable.
196 // hull output limits.
197 unsigned int mMaxVertices; // maximum number of vertices in the output hull. Recommended 32 or less.
198 float mSkinWidth; // a skin width to apply to the output hulls.
200 ConvexDecompInterface *mCallback; // the interface to receive back the results.
204 // perform approximate convex decomposition on a mesh.
205 unsigned int performConvexDecomposition(const DecompDesc &desc); // returns the number of hulls produced.
208 void calcConvexDecomposition(unsigned int vcount,
209 const float *vertices,
211 const unsigned int *indices,
212 ConvexDecompInterface *callback,