ConvexBuilder.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #include "float_math.h"
  2. #include "ConvexBuilder.h"
  3. #include "meshvolume.h"
  4. #include "bestfit.h"
  5. #include <assert.h>
  6. #include "cd_hull.h"
  7. #include "fitsphere.h"
  8. #include "bestfitobb.h"
  9. unsigned int MAXDEPTH = 8;
  10. float CONCAVE_PERCENT = 1.0f;
  11. float MERGE_PERCENT = 2.0f;
  12. CHull::CHull(const ConvexDecomposition::ConvexResult &result)
  13. {
  14. mResult = new ConvexDecomposition::ConvexResult(result);
  15. mVolume = computeMeshVolume(result.mHullVertices, result.mHullTcount, result.mHullIndices);
  16. mDiagonal = getBoundingRegion(result.mHullVcount, result.mHullVertices, sizeof(float) * 3, mMin, mMax);
  17. float dx = mMax[0] - mMin[0];
  18. float dy = mMax[1] - mMin[1];
  19. float dz = mMax[2] - mMin[2];
  20. dx *= 0.1f; // inflate 1/10th on each edge
  21. dy *= 0.1f; // inflate 1/10th on each edge
  22. dz *= 0.1f; // inflate 1/10th on each edge
  23. mMin[0] -= dx;
  24. mMin[1] -= dy;
  25. mMin[2] -= dz;
  26. mMax[0] += dx;
  27. mMax[1] += dy;
  28. mMax[2] += dz;
  29. }
  30. CHull::~CHull(void)
  31. {
  32. delete mResult;
  33. }
  34. bool CHull::overlap(const CHull &h) const
  35. {
  36. return overlapAABB(mMin, mMax, h.mMin, h.mMax);
  37. }
  38. ConvexBuilder::ConvexBuilder(ConvexDecompInterface *callback)
  39. {
  40. mCallback = callback;
  41. }
  42. ConvexBuilder::~ConvexBuilder(void)
  43. {
  44. int i;
  45. for (i = 0; i < mChulls.size(); i++)
  46. {
  47. CHull *cr = mChulls[i];
  48. delete cr;
  49. }
  50. }
  51. bool ConvexBuilder::isDuplicate(unsigned int i1, unsigned int i2, unsigned int i3,
  52. unsigned int ci1, unsigned int ci2, unsigned int ci3)
  53. {
  54. unsigned int dcount = 0;
  55. assert(i1 != i2 && i1 != i3 && i2 != i3);
  56. assert(ci1 != ci2 && ci1 != ci3 && ci2 != ci3);
  57. if (i1 == ci1 || i1 == ci2 || i1 == ci3) dcount++;
  58. if (i2 == ci1 || i2 == ci2 || i2 == ci3) dcount++;
  59. if (i3 == ci1 || i3 == ci2 || i3 == ci3) dcount++;
  60. return dcount == 3;
  61. }
  62. void ConvexBuilder::getMesh(const ConvexDecomposition::ConvexResult &cr, VertexLookup vc, UintVector &indices)
  63. {
  64. unsigned int *src = cr.mHullIndices;
  65. for (unsigned int i = 0; i < cr.mHullTcount; i++)
  66. {
  67. unsigned int i1 = *src++;
  68. unsigned int i2 = *src++;
  69. unsigned int i3 = *src++;
  70. const float *p1 = &cr.mHullVertices[i1 * 3];
  71. const float *p2 = &cr.mHullVertices[i2 * 3];
  72. const float *p3 = &cr.mHullVertices[i3 * 3];
  73. i1 = Vl_getIndex(vc, p1);
  74. i2 = Vl_getIndex(vc, p2);
  75. i3 = Vl_getIndex(vc, p3);
  76. #if 0
  77. bool duplicate = false;
  78. unsigned int tcount = indices.size()/3;
  79. for (unsigned int j=0; j<tcount; j++)
  80. {
  81. unsigned int ci1 = indices[j*3+0];
  82. unsigned int ci2 = indices[j*3+1];
  83. unsigned int ci3 = indices[j*3+2];
  84. if ( isDuplicate(i1,i2,i3, ci1, ci2, ci3 ) )
  85. {
  86. duplicate = true;
  87. break;
  88. }
  89. }
  90. if ( !duplicate )
  91. {
  92. indices.push_back(i1);
  93. indices.push_back(i2);
  94. indices.push_back(i3);
  95. }
  96. #endif
  97. }
  98. }
  99. CHull *ConvexBuilder::canMerge(CHull *a, CHull *b)
  100. {
  101. if (!a->overlap(*b)) return 0; // if their AABB's (with a little slop) don't overlap, then return.
  102. CHull *ret = 0;
  103. // ok..we are going to combine both meshes into a single mesh
  104. // and then we are going to compute the concavity...
  105. VertexLookup vc = Vl_createVertexLookup();
  106. UintVector indices;
  107. getMesh(*a->mResult, vc, indices);
  108. getMesh(*b->mResult, vc, indices);
  109. unsigned int vcount = Vl_getVcount(vc);
  110. const float *vertices = Vl_getVertices(vc);
  111. unsigned int tcount = indices.size() / 3;
  112. //don't do anything if hull is empty
  113. if (!tcount)
  114. {
  115. Vl_releaseVertexLookup(vc);
  116. return 0;
  117. }
  118. ConvexDecomposition::HullResult hresult;
  119. ConvexDecomposition::HullLibrary hl;
  120. ConvexDecomposition::HullDesc desc;
  121. desc.SetHullFlag(ConvexDecomposition::QF_TRIANGLES);
  122. desc.mVcount = vcount;
  123. desc.mVertices = vertices;
  124. desc.mVertexStride = sizeof(float) * 3;
  125. ConvexDecomposition::HullError hret = hl.CreateConvexHull(desc, hresult);
  126. if (hret == ConvexDecomposition::QE_OK)
  127. {
  128. float combineVolume = computeMeshVolume(hresult.mOutputVertices, hresult.mNumFaces, hresult.mIndices);
  129. float sumVolume = a->mVolume + b->mVolume;
  130. float percent = (sumVolume * 100) / combineVolume;
  131. if (percent >= (100.0f - MERGE_PERCENT))
  132. {
  133. ConvexDecomposition::ConvexResult cr(hresult.mNumOutputVertices, hresult.mOutputVertices, hresult.mNumFaces, hresult.mIndices);
  134. ret = new CHull(cr);
  135. }
  136. }
  137. Vl_releaseVertexLookup(vc);
  138. return ret;
  139. }
  140. bool ConvexBuilder::combineHulls(void)
  141. {
  142. bool combine = false;
  143. sortChulls(mChulls); // sort the convex hulls, largest volume to least...
  144. CHullVector output; // the output hulls...
  145. int i;
  146. for (i = 0; i < mChulls.size() && !combine; ++i)
  147. {
  148. CHull *cr = mChulls[i];
  149. int j;
  150. for (j = 0; j < mChulls.size(); j++)
  151. {
  152. CHull *match = mChulls[j];
  153. if (cr != match) // don't try to merge a hull with itself, that be stoopid
  154. {
  155. CHull *merge = canMerge(cr, match); // if we can merge these two....
  156. if (merge)
  157. {
  158. output.push_back(merge);
  159. ++i;
  160. while (i != mChulls.size())
  161. {
  162. CHull *cr = mChulls[i];
  163. if (cr != match)
  164. {
  165. output.push_back(cr);
  166. }
  167. i++;
  168. }
  169. delete cr;
  170. delete match;
  171. combine = true;
  172. break;
  173. }
  174. }
  175. }
  176. if (combine)
  177. {
  178. break;
  179. }
  180. else
  181. {
  182. output.push_back(cr);
  183. }
  184. }
  185. if (combine)
  186. {
  187. mChulls.clear();
  188. mChulls.copyFromArray(output);
  189. output.clear();
  190. }
  191. return combine;
  192. }
  193. unsigned int ConvexBuilder::process(const ConvexDecomposition::DecompDesc &desc)
  194. {
  195. unsigned int ret = 0;
  196. MAXDEPTH = desc.mDepth;
  197. CONCAVE_PERCENT = desc.mCpercent;
  198. MERGE_PERCENT = desc.mPpercent;
  199. calcConvexDecomposition(desc.mVcount, desc.mVertices, desc.mTcount, desc.mIndices, this, 0, 0);
  200. while (combineHulls())
  201. ; // keep combinging hulls until I can't combine any more...
  202. int i;
  203. for (i = 0; i < mChulls.size(); i++)
  204. {
  205. CHull *cr = mChulls[i];
  206. // before we hand it back to the application, we need to regenerate the hull based on the
  207. // limits given by the user.
  208. const ConvexDecomposition::ConvexResult &c = *cr->mResult; // the high resolution hull...
  209. ConvexDecomposition::HullResult result;
  210. ConvexDecomposition::HullLibrary hl;
  211. ConvexDecomposition::HullDesc hdesc;
  212. hdesc.SetHullFlag(ConvexDecomposition::QF_TRIANGLES);
  213. hdesc.mVcount = c.mHullVcount;
  214. hdesc.mVertices = c.mHullVertices;
  215. hdesc.mVertexStride = sizeof(float) * 3;
  216. hdesc.mMaxVertices = desc.mMaxVertices; // maximum number of vertices allowed in the output
  217. if (desc.mSkinWidth)
  218. {
  219. hdesc.mSkinWidth = desc.mSkinWidth;
  220. hdesc.SetHullFlag(ConvexDecomposition::QF_SKIN_WIDTH); // do skin width computation.
  221. }
  222. ConvexDecomposition::HullError ret = hl.CreateConvexHull(hdesc, result);
  223. if (ret == ConvexDecomposition::QE_OK)
  224. {
  225. ConvexDecomposition::ConvexResult r(result.mNumOutputVertices, result.mOutputVertices, result.mNumFaces, result.mIndices);
  226. r.mHullVolume = computeMeshVolume(result.mOutputVertices, result.mNumFaces, result.mIndices); // the volume of the hull.
  227. // compute the best fit OBB
  228. computeBestFitOBB(result.mNumOutputVertices, result.mOutputVertices, sizeof(float) * 3, r.mOBBSides, r.mOBBTransform);
  229. r.mOBBVolume = r.mOBBSides[0] * r.mOBBSides[1] * r.mOBBSides[2]; // compute the OBB volume.
  230. fm_getTranslation(r.mOBBTransform, r.mOBBCenter); // get the translation component of the 4x4 matrix.
  231. fm_matrixToQuat(r.mOBBTransform, r.mOBBOrientation); // extract the orientation as a quaternion.
  232. r.mSphereRadius = computeBoundingSphere(result.mNumOutputVertices, result.mOutputVertices, r.mSphereCenter);
  233. r.mSphereVolume = fm_sphereVolume(r.mSphereRadius);
  234. mCallback->ConvexDecompResult(r);
  235. }
  236. hl.ReleaseResult(result);
  237. delete cr;
  238. }
  239. ret = mChulls.size();
  240. mChulls.clear();
  241. return ret;
  242. }
  243. void ConvexBuilder::ConvexDebugTri(const float *p1, const float *p2, const float *p3, unsigned int color)
  244. {
  245. mCallback->ConvexDebugTri(p1, p2, p3, color);
  246. }
  247. void ConvexBuilder::ConvexDebugOBB(const float *sides, const float *matrix, unsigned int color)
  248. {
  249. mCallback->ConvexDebugOBB(sides, matrix, color);
  250. }
  251. void ConvexBuilder::ConvexDebugPoint(const float *p, float dist, unsigned int color)
  252. {
  253. mCallback->ConvexDebugPoint(p, dist, color);
  254. }
  255. void ConvexBuilder::ConvexDebugBound(const float *bmin, const float *bmax, unsigned int color)
  256. {
  257. mCallback->ConvexDebugBound(bmin, bmax, color);
  258. }
  259. void ConvexBuilder::ConvexDecompResult(ConvexDecomposition::ConvexResult &result)
  260. {
  261. CHull *ch = new CHull(result);
  262. mChulls.push_back(ch);
  263. }
  264. void ConvexBuilder::sortChulls(CHullVector &hulls)
  265. {
  266. hulls.quickSort(CHullSort());
  267. //hulls.heapSort(CHullSort());
  268. }