1 #include "float_math.h"
2 #include "ConvexBuilder.h"
3 #include "meshvolume.h"
9 #include "bestfitobb.h"
11 unsigned int MAXDEPTH = 8 ;
12 float CONCAVE_PERCENT = 1.0f ;
13 float MERGE_PERCENT = 2.0f ;
15 CHull::CHull(const ConvexResult &result)
17 mResult = new ConvexResult(result);
18 mVolume = computeMeshVolume( result.mHullVertices, result.mHullTcount, result.mHullIndices );
20 mDiagonal = getBoundingRegion( result.mHullVcount, result.mHullVertices, sizeof(float)*3, mMin, mMax );
22 float dx = mMax[0] - mMin[0];
23 float dy = mMax[1] - mMin[1];
24 float dz = mMax[2] - mMin[2];
26 dx*=0.1f; // inflate 1/10th on each edge
27 dy*=0.1f; // inflate 1/10th on each edge
28 dz*=0.1f; // inflate 1/10th on each edge
46 bool CHull::overlap(const CHull &h) const
48 return overlapAABB(mMin,mMax, h.mMin, h.mMax );
54 ConvexBuilder::ConvexBuilder(ConvexDecompInterface *callback)
59 ConvexBuilder::~ConvexBuilder(void)
62 for (i=0;i<mChulls.size();i++)
64 CHull *cr = mChulls[i];
69 bool ConvexBuilder::isDuplicate(unsigned int i1,unsigned int i2,unsigned int i3,
70 unsigned int ci1,unsigned int ci2,unsigned int ci3)
72 unsigned int dcount = 0;
74 assert( i1 != i2 && i1 != i3 && i2 != i3 );
75 assert( ci1 != ci2 && ci1 != ci3 && ci2 != ci3 );
77 if ( i1 == ci1 || i1 == ci2 || i1 == ci3 ) dcount++;
78 if ( i2 == ci1 || i2 == ci2 || i2 == ci3 ) dcount++;
79 if ( i3 == ci1 || i3 == ci2 || i3 == ci3 ) dcount++;
84 void ConvexBuilder::getMesh(const ConvexResult &cr,VertexLookup vc,UintVector &indices)
86 unsigned int *src = cr.mHullIndices;
88 for (unsigned int i=0; i<cr.mHullTcount; i++)
90 unsigned int i1 = *src++;
91 unsigned int i2 = *src++;
92 unsigned int i3 = *src++;
94 const float *p1 = &cr.mHullVertices[i1*3];
95 const float *p2 = &cr.mHullVertices[i2*3];
96 const float *p3 = &cr.mHullVertices[i3*3];
98 i1 = Vl_getIndex(vc,p1);
99 i2 = Vl_getIndex(vc,p2);
100 i3 = Vl_getIndex(vc,p3);
103 bool duplicate = false;
105 unsigned int tcount = indices.size()/3;
106 for (unsigned int j=0; j<tcount; j++)
108 unsigned int ci1 = indices[j*3+0];
109 unsigned int ci2 = indices[j*3+1];
110 unsigned int ci3 = indices[j*3+2];
111 if ( isDuplicate(i1,i2,i3, ci1, ci2, ci3 ) )
120 indices.push_back(i1);
121 indices.push_back(i2);
122 indices.push_back(i3);
129 CHull * ConvexBuilder::canMerge(CHull *a,CHull *b)
132 if ( !a->overlap(*b) ) return 0; // if their AABB's (with a little slop) don't overlap, then return.
136 // ok..we are going to combine both meshes into a single mesh
137 // and then we are going to compute the concavity...
139 VertexLookup vc = Vl_createVertexLookup();
143 getMesh( *a->mResult, vc, indices );
144 getMesh( *b->mResult, vc, indices );
146 unsigned int vcount = Vl_getVcount(vc);
147 const float *vertices = Vl_getVertices(vc);
148 unsigned int tcount = indices.size()/3;
150 //don't do anything if hull is empty
153 Vl_releaseVertexLookup (vc);
161 desc.SetHullFlag(QF_TRIANGLES);
163 desc.mVcount = vcount;
164 desc.mVertices = vertices;
165 desc.mVertexStride = sizeof(float)*3;
167 HullError hret = hl.CreateConvexHull(desc,hresult);
172 float combineVolume = computeMeshVolume( hresult.mOutputVertices, hresult.mNumFaces, hresult.mIndices );
173 float sumVolume = a->mVolume + b->mVolume;
175 float percent = (sumVolume*100) / combineVolume;
176 if ( percent >= (100.0f-MERGE_PERCENT) )
178 ConvexResult cr(hresult.mNumOutputVertices, hresult.mOutputVertices, hresult.mNumFaces, hresult.mIndices);
184 Vl_releaseVertexLookup(vc);
189 bool ConvexBuilder::combineHulls(void)
192 bool combine = false;
194 sortChulls(mChulls); // sort the convex hulls, largest volume to least...
196 CHullVector output; // the output hulls...
201 for (i=0;i<mChulls.size() && !combine; ++i)
203 CHull *cr = mChulls[i];
206 for (j=0;j<mChulls.size();j++)
208 CHull *match = mChulls[j];
210 if ( cr != match ) // don't try to merge a hull with itself, that be stoopid
213 CHull *merge = canMerge(cr,match); // if we can merge these two....
218 output.push_back(merge);
222 while ( i != mChulls.size() )
224 CHull *cr = mChulls[i];
227 output.push_back(cr);
246 output.push_back(cr);
254 mChulls.copyFromArray(output);
262 unsigned int ConvexBuilder::process(const DecompDesc &desc)
265 unsigned int ret = 0;
267 MAXDEPTH = desc.mDepth;
268 CONCAVE_PERCENT = desc.mCpercent;
269 MERGE_PERCENT = desc.mPpercent;
272 calcConvexDecomposition(desc.mVcount, desc.mVertices, desc.mTcount, desc.mIndices,this,0,0);
275 while ( combineHulls() ); // keep combinging hulls until I can't combine any more...
278 for (i=0;i<mChulls.size();i++)
280 CHull *cr = mChulls[i];
282 // before we hand it back to the application, we need to regenerate the hull based on the
283 // limits given by the user.
285 const ConvexResult &c = *cr->mResult; // the high resolution hull...
291 hdesc.SetHullFlag(QF_TRIANGLES);
293 hdesc.mVcount = c.mHullVcount;
294 hdesc.mVertices = c.mHullVertices;
295 hdesc.mVertexStride = sizeof(float)*3;
296 hdesc.mMaxVertices = desc.mMaxVertices; // maximum number of vertices allowed in the output
298 if ( desc.mSkinWidth )
300 hdesc.mSkinWidth = desc.mSkinWidth;
301 hdesc.SetHullFlag(QF_SKIN_WIDTH); // do skin width computation.
304 HullError ret = hl.CreateConvexHull(hdesc,result);
308 ConvexResult r(result.mNumOutputVertices, result.mOutputVertices, result.mNumFaces, result.mIndices);
310 r.mHullVolume = computeMeshVolume( result.mOutputVertices, result.mNumFaces, result.mIndices ); // the volume of the hull.
312 // compute the best fit OBB
313 computeBestFitOBB( result.mNumOutputVertices, result.mOutputVertices, sizeof(float)*3, r.mOBBSides, r.mOBBTransform );
315 r.mOBBVolume = r.mOBBSides[0] * r.mOBBSides[1] *r.mOBBSides[2]; // compute the OBB volume.
317 fm_getTranslation( r.mOBBTransform, r.mOBBCenter ); // get the translation component of the 4x4 matrix.
319 fm_matrixToQuat( r.mOBBTransform, r.mOBBOrientation ); // extract the orientation as a quaternion.
321 r.mSphereRadius = computeBoundingSphere( result.mNumOutputVertices, result.mOutputVertices, r.mSphereCenter );
322 r.mSphereVolume = fm_sphereVolume( r.mSphereRadius );
325 mCallback->ConvexDecompResult(r);
328 hl.ReleaseResult (result);
334 ret = mChulls.size();
342 void ConvexBuilder::ConvexDebugTri(const float *p1,const float *p2,const float *p3,unsigned int color)
344 mCallback->ConvexDebugTri(p1,p2,p3,color);
347 void ConvexBuilder::ConvexDebugOBB(const float *sides, const float *matrix,unsigned int color)
349 mCallback->ConvexDebugOBB(sides,matrix,color);
351 void ConvexBuilder::ConvexDebugPoint(const float *p,float dist,unsigned int color)
353 mCallback->ConvexDebugPoint(p,dist,color);
356 void ConvexBuilder::ConvexDebugBound(const float *bmin,const float *bmax,unsigned int color)
358 mCallback->ConvexDebugBound(bmin,bmax,color);
361 void ConvexBuilder::ConvexDecompResult(ConvexResult &result)
363 CHull *ch = new CHull(result);
364 mChulls.push_back(ch);
367 void ConvexBuilder::sortChulls(CHullVector &hulls)
369 hulls.quickSort(CHullSort());
370 //hulls.heapSort(CHullSort());