NvConvexDecomposition.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. /*
  2. NvConvexDecomposition.cpp : The main interface to the convex decomposition library.
  3. */
  4. /*!
  5. **
  6. ** Copyright (c) 2009 by John W. Ratcliff mailto:[email protected]
  7. **
  8. ** Portions of this source has been released with the PhysXViewer application, as well as
  9. ** Rocket, CreateDynamics, ODF, and as a number of sample code snippets.
  10. **
  11. ** If you find this code useful or you are feeling particularily generous I would
  12. ** ask that you please go to http://www.amillionpixels.us and make a donation
  13. ** to Troy DeMolay.
  14. **
  15. ** DeMolay is a youth group for young men between the ages of 12 and 21.
  16. ** It teaches strong moral principles, as well as leadership skills and
  17. ** public speaking. The donations page uses the 'pay for pixels' paradigm
  18. ** where, in this case, a pixel is only a single penny. Donations can be
  19. ** made for as small as $4 or as high as a $100 block. Each person who donates
  20. ** will get a link to their own site as well as acknowledgement on the
  21. ** donations blog located here http://www.amillionpixels.blogspot.com/
  22. **
  23. ** If you wish to contact me you can use the following methods:
  24. **
  25. ** Skype ID: jratcliff63367
  26. ** Yahoo: jratcliff63367
  27. ** AOL: jratcliff1961
  28. ** email: [email protected]
  29. **
  30. **
  31. ** The MIT license:
  32. **
  33. ** Permission is hereby granted, free of charge, to any person obtaining a copy
  34. ** of this software and associated documentation files (the "Software"), to deal
  35. ** in the Software without restriction, including without limitation the rights
  36. ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  37. ** copies of the Software, and to permit persons to whom the Software is furnished
  38. ** to do so, subject to the following conditions:
  39. **
  40. ** The above copyright notice and this permission notice shall be included in all
  41. ** copies or substantial portions of the Software.
  42. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  43. ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  44. ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  45. ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  46. ** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  47. ** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  48. */
  49. #include <math.h>
  50. #include <float.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <stdlib.h>
  54. #include "NvConvexDecomposition.h"
  55. #include "NvHashMap.h"
  56. #include "NvFloatMath.h"
  57. #include "NvRemoveTjunctions.h"
  58. #include "NvMeshIslandGeneration.h"
  59. #include "NvStanHull.h"
  60. #include "NvConcavityVolume.h"
  61. #include "NvSplitMesh.h"
  62. #include "NvThreadConfig.h"
  63. #pragma warning(disable:4996 4100 4189)
  64. namespace CONVEX_DECOMPOSITION
  65. {
  66. #define GRANULARITY 0.0000000001f
  67. typedef CONVEX_DECOMPOSITION::Array< NxU32 > NxU32Array;
  68. class ConvexHull : public Memalloc
  69. {
  70. public:
  71. ConvexHull(NxU32 vcount,const NxF32 *vertices,NxU32 tcount,const NxU32 *indices)
  72. {
  73. mTested = false;
  74. mVcount = vcount;
  75. mTcount = tcount;
  76. mVertices = 0;
  77. mIndices = 0;
  78. mHullVolume = 0;
  79. if ( vcount )
  80. {
  81. mVertices = (NxF32 *)MEMALLOC_MALLOC(sizeof(NxF32)*3*vcount);
  82. memcpy(mVertices,vertices,sizeof(NxF32)*3*vcount);
  83. }
  84. if ( tcount )
  85. {
  86. mIndices = (NxU32 *)MEMALLOC_MALLOC(sizeof(NxU32)*3*tcount);
  87. memcpy(mIndices,indices,sizeof(NxU32)*3*tcount);
  88. }
  89. if ( mVcount && mTcount )
  90. {
  91. mHullVolume = fm_computeMeshVolume( mVertices, mTcount, mIndices);
  92. }
  93. }
  94. ~ConvexHull(void)
  95. {
  96. reset();
  97. }
  98. void reset(void)
  99. {
  100. MEMALLOC_FREE(mVertices);
  101. MEMALLOC_FREE(mIndices);
  102. mVertices = 0;
  103. mIndices = 0;
  104. mVcount = 0;
  105. mTcount = 0;
  106. mHullVolume = 0;
  107. }
  108. // return true if merging this hull with the 'mergeHull' produces a new convex hull which is no greater in volume than the
  109. // mergeThresholdPercentage
  110. bool canMerge(ConvexHull *mergeHull,NxF32 mergeThresholdPercent,NxU32 maxVertices,NxF32 skinWidth,NxF32 &percent)
  111. {
  112. bool ret = false;
  113. if ( mHullVolume > 0 && mergeHull->mHullVolume > 0 )
  114. {
  115. NxU32 combineVcount = mVcount + mergeHull->mVcount;
  116. NxF32 *vertices = (NxF32 *)MEMALLOC_MALLOC(sizeof(NxF32)*combineVcount*3);
  117. NxF32 *dest = vertices;
  118. const NxF32 *source = mVertices;
  119. for (NxU32 i=0; i<mVcount; i++)
  120. {
  121. dest[0] = source[0];
  122. dest[1] = source[1];
  123. dest[2] = source[2];
  124. dest+=3;
  125. source+=3;
  126. }
  127. source = mergeHull->mVertices;
  128. for (NxU32 i=0; i<mergeHull->mVcount; i++)
  129. {
  130. dest[0] = source[0];
  131. dest[1] = source[1];
  132. dest[2] = source[2];
  133. dest+=3;
  134. source+=3;
  135. }
  136. // create the combined convex hull.
  137. HullDesc hd;
  138. hd.mVcount = combineVcount;
  139. hd.mVertices = vertices;
  140. hd.mVertexStride = sizeof(NxF32)*3;
  141. hd.mMaxVertices = maxVertices;
  142. hd.mSkinWidth = skinWidth;
  143. HullLibrary hl;
  144. HullResult result;
  145. hl.CreateConvexHull(hd,result);
  146. NxF32 combinedVolume = fm_computeMeshVolume(result.mOutputVertices, result.mNumFaces, result.mIndices );
  147. NxF32 seperateVolume = mHullVolume+mergeHull->mHullVolume;
  148. NxF32 percentMerge = 100 - (seperateVolume*100 / combinedVolume );
  149. if ( percentMerge <= mergeThresholdPercent )
  150. {
  151. percent = percentMerge;
  152. ret = true;
  153. }
  154. MEMALLOC_FREE(vertices);
  155. hl.ReleaseResult(result);
  156. }
  157. return ret;
  158. }
  159. void merge(ConvexHull *mergeHull,NxU32 maxVertices,NxF32 skinWidth)
  160. {
  161. NxU32 combineVcount = mVcount + mergeHull->mVcount;
  162. NxF32 *vertices = (NxF32 *)MEMALLOC_MALLOC(sizeof(NxF32)*combineVcount*3);
  163. NxF32 *dest = vertices;
  164. const NxF32 *source = mVertices;
  165. for (NxU32 i=0; i<mVcount; i++)
  166. {
  167. dest[0] = source[0];
  168. dest[1] = source[1];
  169. dest[2] = source[2];
  170. dest+=3;
  171. source+=3;
  172. }
  173. source = mergeHull->mVertices;
  174. for (NxU32 i=0; i<mergeHull->mVcount; i++)
  175. {
  176. dest[0] = source[0];
  177. dest[1] = source[1];
  178. dest[2] = source[2];
  179. dest+=3;
  180. source+=3;
  181. }
  182. // create the combined convex hull.
  183. HullDesc hd;
  184. hd.mVcount = combineVcount;
  185. hd.mVertices = vertices;
  186. hd.mVertexStride = sizeof(NxF32)*3;
  187. hd.mMaxVertices = maxVertices;
  188. hd.mSkinWidth = skinWidth;
  189. HullLibrary hl;
  190. HullResult result;
  191. hl.CreateConvexHull(hd,result);
  192. reset();
  193. mergeHull->reset();
  194. mergeHull->mTested = true; // it's been tested.
  195. mVcount = result.mNumOutputVertices;
  196. mVertices = (NxF32 *)MEMALLOC_MALLOC(sizeof(NxF32)*3*mVcount);
  197. memcpy(mVertices,result.mOutputVertices,sizeof(NxF32)*3*mVcount);
  198. mTcount = result.mNumFaces;
  199. mIndices = (NxU32 *)MEMALLOC_MALLOC(sizeof(NxU32)*mTcount*3);
  200. memcpy(mIndices, result.mIndices, sizeof(NxU32)*mTcount*3);
  201. MEMALLOC_FREE(vertices);
  202. hl.ReleaseResult(result);
  203. }
  204. void setTested(bool state)
  205. {
  206. mTested = state;
  207. }
  208. bool beenTested(void) const { return mTested; };
  209. bool mTested;
  210. NxF32 mHullVolume;
  211. NxU32 mVcount;
  212. NxF32 *mVertices;
  213. NxU32 mTcount;
  214. NxU32 *mIndices;
  215. };
  216. typedef Array< ConvexHull *> ConvexHullVector;
  217. class ConvexDecomposition : public iConvexDecomposition, public CONVEX_DECOMPOSITION::Memalloc, public ThreadInterface
  218. {
  219. public:
  220. ConvexDecomposition(void)
  221. {
  222. mVertexIndex = 0;
  223. mComplete = false;
  224. mCancel = false;
  225. mThread = 0;
  226. }
  227. ~ConvexDecomposition(void)
  228. {
  229. wait();
  230. reset();
  231. if ( mThread )
  232. {
  233. tc_releaseThread(mThread);
  234. }
  235. }
  236. void wait(void) const
  237. {
  238. while ( mThread && !mComplete );
  239. }
  240. virtual void reset(void) // reset the input mesh data.
  241. {
  242. wait();
  243. if ( mVertexIndex )
  244. {
  245. fm_releaseVertexIndex(mVertexIndex);
  246. mVertexIndex = 0;
  247. }
  248. mIndices.clear();
  249. ConvexHullVector::Iterator i;
  250. for (i=mHulls.begin(); i!=mHulls.end(); ++i)
  251. {
  252. ConvexHull *ch = (*i);
  253. delete ch;
  254. }
  255. mHulls.clear();
  256. }
  257. virtual bool addTriangle(const NxF32 *p1,const NxF32 *p2,const NxF32 *p3)
  258. {
  259. bool ret = true;
  260. wait();
  261. if ( mVertexIndex == 0 )
  262. {
  263. mVertexIndex = fm_createVertexIndex(GRANULARITY,false);
  264. }
  265. bool newPos;
  266. NxU32 i1 = mVertexIndex->getIndex(p1,newPos);
  267. NxU32 i2 = mVertexIndex->getIndex(p2,newPos);
  268. NxU32 i3 = mVertexIndex->getIndex(p3,newPos);
  269. if ( i1 == i2 || i1 == i3 || i2 == i3 )
  270. {
  271. ret = false; // triangle is degenerate
  272. }
  273. else
  274. {
  275. mIndices.pushBack(i1);
  276. mIndices.pushBack(i2);
  277. mIndices.pushBack(i3);
  278. }
  279. return ret;
  280. }
  281. ConvexHull * getNonTested(void) const
  282. {
  283. ConvexHull *ret = 0;
  284. for (NxU32 i=0; i<mHulls.size(); i++)
  285. {
  286. ConvexHull *ch = mHulls[i];
  287. if ( !ch->beenTested() )
  288. {
  289. ret = ch;
  290. break;
  291. }
  292. }
  293. return ret;
  294. }
  295. virtual NxU32 computeConvexDecomposition(NxF32 skinWidth,
  296. NxU32 decompositionDepth,
  297. NxU32 maxHullVertices,
  298. NxF32 concavityThresholdPercent,
  299. NxF32 mergeThresholdPercent,
  300. NxF32 volumeSplitThresholdPercent,
  301. bool useInitialIslandGeneration,
  302. bool useIslandGeneration,
  303. bool useThreads)
  304. {
  305. NxU32 ret = 0;
  306. if ( mThread )
  307. return 0;
  308. if ( mVertexIndex )
  309. {
  310. mSkinWidth = skinWidth;
  311. mDecompositionDepth = decompositionDepth;
  312. mMaxHullVertices = maxHullVertices;
  313. mConcavityThresholdPercent = concavityThresholdPercent;
  314. mMergeThresholdPercent = mergeThresholdPercent;
  315. mVolumeSplitThresholdPercent = volumeSplitThresholdPercent;
  316. mUseInitialIslandGeneration = useInitialIslandGeneration;
  317. mUseIslandGeneration = false; // Not currently supported. useIslandGeneration;
  318. mComplete = false;
  319. mCancel = false;
  320. if ( useThreads )
  321. {
  322. mThread = tc_createThread(this);
  323. }
  324. else
  325. {
  326. threadMain();
  327. ret = getHullCount();
  328. }
  329. }
  330. return ret;
  331. }
  332. void performConvexDecomposition(NxU32 vcount,
  333. const NxF32 *vertices,
  334. NxU32 tcount,
  335. const NxU32 *indices,
  336. NxF32 skinWidth,
  337. NxU32 decompositionDepth,
  338. NxU32 maxHullVertices,
  339. NxF32 concavityThresholdPercent,
  340. NxF32 mergeThresholdPercent,
  341. NxF32 volumeSplitThresholdPercent,
  342. bool useInitialIslandGeneration,
  343. bool useIslandGeneration,
  344. NxU32 depth)
  345. {
  346. if ( mCancel ) return;
  347. if ( depth >= decompositionDepth ) return;
  348. RemoveTjunctionsDesc desc;
  349. desc.mVcount = vcount;
  350. desc.mVertices = vertices;
  351. desc.mTcount = tcount;
  352. desc.mIndices = indices;
  353. #if 0
  354. RemoveTjunctions *rt = createRemoveTjunctions();
  355. rt->removeTjunctions(desc);
  356. #else
  357. desc.mTcountOut = desc.mTcount;
  358. desc.mIndicesOut = desc.mIndices;
  359. #endif
  360. // ok..we now have a clean mesh without any tjunctions.
  361. bool island = (depth == 0 ) ? useInitialIslandGeneration : useIslandGeneration;
  362. if ( island )
  363. {
  364. MeshIslandGeneration *mi = createMeshIslandGeneration();
  365. NxU32 icount = mi->islandGenerate(desc.mTcountOut,desc.mIndicesOut,desc.mVertices);
  366. for (NxU32 i=0; i<icount && !mCancel; i++)
  367. {
  368. NxU32 tcount;
  369. NxU32 *indices = mi->getIsland(i,tcount);
  370. baseConvexDecomposition(desc.mVcount,desc.mVertices,
  371. tcount,indices,
  372. skinWidth,
  373. decompositionDepth,
  374. maxHullVertices,
  375. concavityThresholdPercent,
  376. mergeThresholdPercent,
  377. volumeSplitThresholdPercent,
  378. useInitialIslandGeneration,
  379. useIslandGeneration,depth);
  380. }
  381. releaseMeshIslandGeneration(mi);
  382. }
  383. else
  384. {
  385. baseConvexDecomposition(desc.mVcount,desc.mVertices,desc.mTcountOut,
  386. desc.mIndicesOut,
  387. skinWidth,
  388. decompositionDepth,
  389. maxHullVertices,
  390. concavityThresholdPercent,
  391. mergeThresholdPercent,
  392. volumeSplitThresholdPercent,
  393. useInitialIslandGeneration,
  394. useIslandGeneration,depth);
  395. }
  396. #if 0
  397. releaseRemoveTjunctions(rt);
  398. #endif
  399. }
  400. virtual void baseConvexDecomposition(NxU32 vcount,
  401. const NxF32 *vertices,
  402. NxU32 tcount,
  403. const NxU32 *indices,
  404. NxF32 skinWidth,
  405. NxU32 decompositionDepth,
  406. NxU32 maxHullVertices,
  407. NxF32 concavityThresholdPercent,
  408. NxF32 mergeThresholdPercent,
  409. NxF32 volumeSplitThresholdPercent,
  410. bool useInitialIslandGeneration,
  411. bool useIslandGeneration,
  412. NxU32 depth)
  413. {
  414. if ( mCancel ) return;
  415. bool split = false; // by default we do not split
  416. NxU32 *out_indices = (NxU32 *)MEMALLOC_MALLOC( sizeof(NxU32)*tcount*3 );
  417. NxF32 *out_vertices = (NxF32 *)MEMALLOC_MALLOC( sizeof(NxF32)*3*vcount );
  418. NxU32 out_vcount = fm_copyUniqueVertices( vcount, vertices, out_vertices, tcount, indices, out_indices );
  419. // get a copy of only the unique vertices which are actually being used.
  420. HullDesc hd;
  421. hd.mVcount = out_vcount;
  422. hd.mVertices = out_vertices;
  423. hd.mVertexStride = sizeof(NxF32)*3;
  424. hd.mMaxVertices = maxHullVertices;
  425. hd.mSkinWidth = skinWidth;
  426. HullLibrary hl;
  427. HullResult result;
  428. hl.CreateConvexHull(hd,result);
  429. NxF32 meshVolume = fm_computeMeshVolume(result.mOutputVertices, result.mNumFaces, result.mIndices );
  430. if ( (depth+1) < decompositionDepth )
  431. {
  432. // compute the volume of this mesh...
  433. NxF32 percentVolume = (meshVolume*100)/mOverallMeshVolume; // what percentage of the overall mesh volume are we?
  434. if ( percentVolume > volumeSplitThresholdPercent ) // this piece must be greater thant he volume split threshold percent
  435. {
  436. // ok..now we will compute the concavity...
  437. NxF32 concave_volume = computeConcavityVolume(result.mNumOutputVertices, result.mOutputVertices, result.mNumFaces, result.mIndices, out_vcount, out_vertices, tcount, out_indices );
  438. NxF32 concave_percent = (concave_volume*100) / meshVolume;
  439. if ( concave_percent >= concavityThresholdPercent )
  440. {
  441. // ready to do split here..
  442. split = true;
  443. }
  444. }
  445. }
  446. if ( !split )
  447. {
  448. saveConvexHull(result.mNumOutputVertices,result.mOutputVertices,result.mNumFaces,result.mIndices);
  449. }
  450. // Compute the best fit plane relative to the computed convex hull.
  451. NxF32 plane[4];
  452. bool ok = fm_computeSplitPlane(result.mNumOutputVertices,result.mOutputVertices,result.mNumFaces,result.mIndices,plane);
  453. assert(ok);
  454. hl.ReleaseResult(result);
  455. MEMALLOC_FREE(out_indices);
  456. MEMALLOC_FREE(out_vertices);
  457. if ( split )
  458. {
  459. iSplitMesh *sm = createSplitMesh();
  460. NvSplitMesh n;
  461. n.mVcount = vcount;
  462. n.mVertices = vertices;
  463. n.mTcount = tcount;
  464. n.mIndices = indices;
  465. if ( ok )
  466. {
  467. NvSplitMesh leftMesh;
  468. NvSplitMesh rightMesh;
  469. sm->splitMesh(n,leftMesh,rightMesh,plane,GRANULARITY);
  470. if ( leftMesh.mTcount )
  471. {
  472. performConvexDecomposition(leftMesh.mVcount,
  473. leftMesh.mVertices,
  474. leftMesh.mTcount,
  475. leftMesh.mIndices,
  476. skinWidth,
  477. decompositionDepth,
  478. maxHullVertices,
  479. concavityThresholdPercent,
  480. mergeThresholdPercent,
  481. volumeSplitThresholdPercent,
  482. useInitialIslandGeneration,
  483. useIslandGeneration,
  484. depth+1);
  485. }
  486. if ( rightMesh.mTcount )
  487. {
  488. performConvexDecomposition(rightMesh.mVcount,
  489. rightMesh.mVertices,
  490. rightMesh.mTcount,
  491. rightMesh.mIndices,
  492. skinWidth,
  493. decompositionDepth,
  494. maxHullVertices,
  495. concavityThresholdPercent,
  496. mergeThresholdPercent,
  497. volumeSplitThresholdPercent,
  498. useInitialIslandGeneration,
  499. useIslandGeneration,
  500. depth+1);
  501. }
  502. }
  503. releaseSplitMesh(sm);
  504. }
  505. }
  506. // Copies only the vertices which are actually used.
  507. // Then computes the convex hull around these used vertices.
  508. // Next computes the volume of this convex hull.
  509. // Frees up scratch memory and returns the volume of the convex hull around the source triangle mesh.
  510. NxF32 computeHullMeshVolume(NxU32 vcount,const NxF32 *vertices,NxU32 tcount,const NxU32 *indices,NxU32 maxVertices,NxF32 skinWidth)
  511. {
  512. if ( mCancel ) return 0;
  513. // first thing we should do is compute the overall mesh volume.
  514. NxU32 *out_indices = (NxU32 *)MEMALLOC_MALLOC( sizeof(NxU32)*tcount*3 );
  515. NxF32 *out_vertices = (NxF32 *)MEMALLOC_MALLOC( sizeof(NxF32)*3*vcount );
  516. NxU32 out_vcount = fm_copyUniqueVertices( vcount, vertices, out_vertices, tcount, indices, out_indices );
  517. // get a copy of only the unique vertices which are actually being used.
  518. HullDesc hd;
  519. hd.mVcount = out_vcount;
  520. hd.mVertices = out_vertices;
  521. hd.mVertexStride = sizeof(NxF32)*3;
  522. hd.mMaxVertices = maxVertices;
  523. hd.mSkinWidth = skinWidth;
  524. HullLibrary hl;
  525. HullResult result;
  526. hl.CreateConvexHull(hd,result);
  527. NxF32 volume = fm_computeMeshVolume(result.mOutputVertices, result.mNumFaces, result.mIndices );
  528. hl.ReleaseResult(result);
  529. MEMALLOC_FREE(out_indices);
  530. MEMALLOC_FREE(out_vertices);
  531. return volume;
  532. }
  533. virtual bool isComputeComplete(void) // if building the convex hulls in a background thread, this returns true if it is complete.
  534. {
  535. bool ret = true;
  536. if ( mThread )
  537. {
  538. ret = mComplete;
  539. if ( ret )
  540. {
  541. tc_releaseThread(mThread);
  542. mThread = 0;
  543. }
  544. }
  545. return ret;
  546. }
  547. virtual NxU32 getHullCount(void)
  548. {
  549. NxU32 hullCount = 0;
  550. wait();
  551. if ( mCancel )
  552. {
  553. reset();
  554. }
  555. for (NxU32 i=0; i<mHulls.size(); i++)
  556. {
  557. ConvexHull *ch = mHulls[i];
  558. if ( ch->mTcount )
  559. {
  560. hullCount++;
  561. }
  562. }
  563. return hullCount;
  564. }
  565. virtual bool getConvexHullResult(NxU32 hullIndex,ConvexHullResult &result)
  566. {
  567. bool ret = false;
  568. wait();
  569. NxU32 index = 0;
  570. for (NxU32 i=0; i<mHulls.size(); i++)
  571. {
  572. ConvexHull *ch = mHulls[i];
  573. if ( ch->mTcount )
  574. {
  575. if ( hullIndex == index )
  576. {
  577. ret = true;
  578. result.mVcount = ch->mVcount;
  579. result.mTcount = ch->mTcount;
  580. result.mVertices = ch->mVertices;
  581. result.mIndices = ch->mIndices;
  582. break;
  583. }
  584. index++;
  585. }
  586. }
  587. return ret;
  588. }
  589. void saveConvexHull(NxU32 vcount,const NxF32 *vertices,NxU32 tcount,const NxU32 *indices)
  590. {
  591. ConvexHull *ch = MEMALLOC_NEW(ConvexHull)(vcount,vertices,tcount,indices);
  592. mHulls.pushBack(ch);
  593. }
  594. virtual void threadMain(void)
  595. {
  596. mOverallMeshVolume = computeHullMeshVolume( mVertexIndex->getVcount(),
  597. mVertexIndex->getVerticesFloat(),
  598. mIndices.size()/3,
  599. &mIndices[0],
  600. mMaxHullVertices, mSkinWidth );
  601. performConvexDecomposition(mVertexIndex->getVcount(),mVertexIndex->getVerticesFloat(),
  602. mIndices.size()/3,&mIndices[0],
  603. mSkinWidth,
  604. mDecompositionDepth,
  605. mMaxHullVertices,
  606. mConcavityThresholdPercent,
  607. mMergeThresholdPercent,
  608. mVolumeSplitThresholdPercent,
  609. mUseInitialIslandGeneration,
  610. mUseIslandGeneration,0);
  611. if ( mHulls.size() && !mCancel )
  612. {
  613. // While convex hulls can be merged...
  614. ConvexHull *ch = getNonTested();
  615. while ( ch && !mCancel )
  616. {
  617. // Sort all convex hulls by volume, largest to smallest.
  618. NxU32 hullCount = mHulls.size();
  619. ConvexHull *bestHull = 0;
  620. NxF32 bestPercent = 100;
  621. for (NxU32 i=0; i<hullCount; i++)
  622. {
  623. ConvexHull *mergeHull = mHulls[i];
  624. if ( !mergeHull->beenTested() && mergeHull != ch )
  625. {
  626. NxF32 percent;
  627. if ( ch->canMerge(mergeHull,mMergeThresholdPercent,mMaxHullVertices,mSkinWidth,percent) )
  628. {
  629. if ( percent < bestPercent )
  630. {
  631. bestHull = mergeHull;
  632. bestPercent = percent;
  633. }
  634. }
  635. }
  636. }
  637. if ( bestHull )
  638. {
  639. ch->merge(bestHull,mMaxHullVertices,mSkinWidth);
  640. }
  641. else
  642. {
  643. ch->setTested(true);
  644. }
  645. ch = getNonTested();
  646. }
  647. }
  648. mComplete = true;
  649. }
  650. virtual bool cancelCompute(void) // cause background thread computation to abort early. Will return no results. Use 'isComputeComplete' to confirm the thread is done.
  651. {
  652. bool ret = false;
  653. if ( mThread && !mComplete )
  654. {
  655. mCancel = true;
  656. ret = true;
  657. }
  658. return ret;
  659. }
  660. private:
  661. bool mComplete;
  662. bool mCancel;
  663. fm_VertexIndex *mVertexIndex;
  664. NxU32Array mIndices;
  665. NxF32 mOverallMeshVolume;
  666. ConvexHullVector mHulls;
  667. Thread *mThread;
  668. NxF32 mSkinWidth;
  669. NxU32 mDecompositionDepth;
  670. NxU32 mMaxHullVertices;
  671. NxF32 mConcavityThresholdPercent;
  672. NxF32 mMergeThresholdPercent;
  673. NxF32 mVolumeSplitThresholdPercent;
  674. bool mUseInitialIslandGeneration;
  675. bool mUseIslandGeneration;
  676. };
  677. iConvexDecomposition * createConvexDecomposition(void)
  678. {
  679. ConvexDecomposition *cd = MEMALLOC_NEW(ConvexDecomposition);
  680. return static_cast< iConvexDecomposition *>(cd);
  681. }
  682. void releaseConvexDecomposition(iConvexDecomposition *ic)
  683. {
  684. ConvexDecomposition *cd = static_cast< ConvexDecomposition *>(ic);
  685. delete cd;
  686. }
  687. }; // end of namespace