NvRemoveTjunctions.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*
  2. NvRemoveTjunctions.cpp : A code snippet to remove tjunctions from a triangle mesh. This version is currently disabled as it appears to have a bug.
  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 <stdio.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <assert.h>
  53. #pragma warning(disable:4702)
  54. #pragma warning(disable:4127) //conditional expression is constant (because _HAS_EXCEPTIONS=0)
  55. #include <vector>
  56. #ifdef __APPLE__
  57. #include <ext/hash_map>
  58. #elif LINUX
  59. #include <hash_map>
  60. #elif _MSC_VER < 1500
  61. #include <hash_map>
  62. #elif _MSC_VER > 1800
  63. #include <unordered_map>
  64. #endif
  65. #include "NvUserMemAlloc.h"
  66. #include "NvHashMap.h"
  67. #include "NvRemoveTjunctions.h"
  68. #include "NvFloatMath.h"
  69. #ifdef LINUX
  70. #include <climits>
  71. #endif
  72. #pragma warning(disable:4189)
  73. using namespace CONVEX_DECOMPOSITION;
  74. namespace CONVEX_DECOMPOSITION
  75. {
  76. class AABB
  77. {
  78. public:
  79. NxF32 mMin[3];
  80. NxF32 mMax[3];
  81. };
  82. bool gDebug=false;
  83. NxU32 gCount=0;
  84. typedef CONVEX_DECOMPOSITION::Array< NxU32 > NxU32Vector;
  85. class Triangle
  86. {
  87. public:
  88. Triangle(void)
  89. {
  90. mPending = false;
  91. mSplit = false;
  92. mI1 = mI2 = mI3 = 0xFFFFFFFF;
  93. mId = 0;
  94. }
  95. Triangle(NxU32 i1,NxU32 i2,NxU32 i3,const float *vertices,NxU32 id)
  96. {
  97. mPending = false;
  98. init(i1,i2,i3,vertices,id);
  99. mSplit = false;
  100. }
  101. void init(NxU32 i1,NxU32 i2,NxU32 i3,const float *vertices,NxU32 id)
  102. {
  103. mSplit = false;
  104. mI1 = i1;
  105. mI2 = i2;
  106. mI3 = i3;
  107. mId = id;
  108. const float *p1 = &vertices[mI1*3];
  109. const float *p2 = &vertices[mI2*3];
  110. const float *p3 = &vertices[mI3*3];
  111. initMinMax(p1,p2,p3);
  112. }
  113. void initMinMax(const float *p1,const float *p2,const float *p3)
  114. {
  115. fm_copy3(p1,mBmin);
  116. fm_copy3(p1,mBmax);
  117. fm_minmax(p2,mBmin,mBmax);
  118. fm_minmax(p3,mBmin,mBmax);
  119. }
  120. void init(const NxU32 *idx,const float *vertices,NxU32 id)
  121. {
  122. mSplit = false;
  123. mI1 = idx[0];
  124. mI2 = idx[1];
  125. mI3 = idx[2];
  126. mId = id;
  127. const float *p1 = &vertices[mI1*3];
  128. const float *p2 = &vertices[mI2*3];
  129. const float *p3 = &vertices[mI3*3];
  130. initMinMax(p1,p2,p3);
  131. }
  132. bool intersects(const float *pos,const float *p1,const float *p2,float epsilon) const
  133. {
  134. bool ret = false;
  135. float sect[3];
  136. LineSegmentType type;
  137. float dist = fm_distancePointLineSegment(pos,p1,p2,sect,type,epsilon);
  138. if ( type == LS_MIDDLE && dist < epsilon )
  139. {
  140. ret = true;
  141. }
  142. return ret;
  143. }
  144. bool intersects(NxU32 i,const float *vertices,NxU32 &edge,float epsilon) const
  145. {
  146. bool ret = true;
  147. const float *pos = &vertices[i*3];
  148. const float *p1 = &vertices[mI1*3];
  149. const float *p2 = &vertices[mI2*3];
  150. const float *p3 = &vertices[mI3*3];
  151. if ( intersects(pos,p1,p2,epsilon) )
  152. {
  153. edge = 0;
  154. }
  155. else if ( intersects(pos,p2,p3,epsilon) )
  156. {
  157. edge = 1;
  158. }
  159. else if ( intersects(pos,p3,p1,epsilon) )
  160. {
  161. edge = 2;
  162. }
  163. else
  164. {
  165. ret = false;
  166. }
  167. return ret;
  168. }
  169. bool intersects(const Triangle *t,const float *vertices,NxU32 &intersection_index,NxU32 &edge,float epsilon)
  170. {
  171. bool ret = false;
  172. if ( fm_intersectAABB(mBmin,mBmax,t->mBmin,t->mBmax) ) // only if the AABB's of the two triangles intersect...
  173. {
  174. if ( t->intersects(mI1,vertices,edge,epsilon) )
  175. {
  176. intersection_index = mI1;
  177. ret = true;
  178. }
  179. if ( t->intersects(mI2,vertices,edge,epsilon) )
  180. {
  181. intersection_index = mI2;
  182. ret = true;
  183. }
  184. if ( t->intersects(mI3,vertices,edge,epsilon) )
  185. {
  186. intersection_index = mI3;
  187. ret = true;
  188. }
  189. }
  190. return ret;
  191. }
  192. bool mSplit:1;
  193. bool mPending:1;
  194. NxU32 mI1;
  195. NxU32 mI2;
  196. NxU32 mI3;
  197. NxU32 mId;
  198. float mBmin[3];
  199. float mBmax[3];
  200. };
  201. class RtEdge
  202. {
  203. public:
  204. RtEdge(void)
  205. {
  206. mNextEdge = 0;
  207. mTriangle = 0;
  208. mHash = 0;
  209. }
  210. NxU32 init(Triangle *t,NxU32 i1,NxU32 i2)
  211. {
  212. mTriangle = t;
  213. mNextEdge = 0;
  214. NX_ASSERT( i1 < 65536 );
  215. NX_ASSERT( i2 < 65536 );
  216. if ( i1 < i2 )
  217. {
  218. mHash = (i1<<16)|i2;
  219. }
  220. else
  221. {
  222. mHash = (i2<<16)|i1;
  223. }
  224. return mHash;
  225. }
  226. RtEdge *mNextEdge;
  227. Triangle *mTriangle;
  228. NxU32 mHash;
  229. };
  230. typedef CONVEX_DECOMPOSITION::Array< Triangle * > TriangleVector;
  231. typedef CONVEX_DECOMPOSITION::HashMap< NxU32, RtEdge * > EdgeMap;
  232. class MyRemoveTjunctions : public RemoveTjunctions
  233. {
  234. public:
  235. MyRemoveTjunctions(void)
  236. {
  237. mInputTriangles = 0;
  238. mEdges = 0;
  239. mVcount = 0;
  240. mVertices = 0;
  241. mEdgeCount = 0;
  242. }
  243. ~MyRemoveTjunctions(void)
  244. {
  245. release();
  246. }
  247. virtual NxU32 removeTjunctions(RemoveTjunctionsDesc &desc)
  248. {
  249. NxU32 ret = 0;
  250. mEpsilon = desc.mEpsilon;
  251. size_t TcountOut;
  252. desc.mIndicesOut = removeTjunctions(desc.mVcount, desc.mVertices, desc.mTcount, desc.mIndices, TcountOut, desc.mIds);
  253. #ifdef WIN32
  254. # pragma warning(push)
  255. # pragma warning(disable:4267)
  256. #endif
  257. NX_ASSERT( TcountOut < UINT_MAX );
  258. desc.mTcountOut = TcountOut;
  259. #ifdef WIN32
  260. # pragma warning(pop)
  261. #endif
  262. if ( !mIds.empty() )
  263. {
  264. desc.mIdsOut = &mIds[0];
  265. }
  266. ret = desc.mTcountOut;
  267. bool check = ret != desc.mTcount;
  268. #if 0
  269. while ( check )
  270. {
  271. NxU32 tcount = ret;
  272. NxU32 *indices = new NxU32[tcount*3];
  273. NxU32 *ids = new NxU32[tcount];
  274. memcpy(indices,desc.mIndicesOut,sizeof(NxU32)*ret*3);
  275. memcpy(ids,desc.mIdsOut,sizeof(NxU32)*ret);
  276. desc.mIndicesOut = removeTjunctions(desc.mVcount, desc.mVertices, tcount, indices, desc.mTcountOut, ids );
  277. if ( !mIds.empty() )
  278. {
  279. desc.mIdsOut = &mIds[0];
  280. }
  281. ret = desc.mTcountOut;
  282. delete []indices;
  283. delete []ids;
  284. check = ret != tcount;
  285. }
  286. #endif
  287. return ret;
  288. }
  289. RtEdge * addEdge(Triangle *t,RtEdge *e,NxU32 i1,NxU32 i2)
  290. {
  291. NxU32 hash = e->init(t,i1,i2);
  292. const EdgeMap::Entry *found = mEdgeMap.find(hash);
  293. if ( found == NULL )
  294. {
  295. mEdgeMap[hash] = e;
  296. }
  297. else
  298. {
  299. RtEdge *old_edge = (*found).second;
  300. e->mNextEdge = old_edge;
  301. mEdgeMap.erase(hash);
  302. mEdgeMap[hash] = e;
  303. }
  304. e++;
  305. mEdgeCount++;
  306. return e;
  307. }
  308. RtEdge * init(Triangle *t,const NxU32 *indices,const float *vertices,RtEdge *e,NxU32 id)
  309. {
  310. t->init(indices,vertices,id);
  311. e = addEdge(t,e,t->mI1,t->mI2);
  312. e = addEdge(t,e,t->mI2,t->mI3);
  313. e = addEdge(t,e,t->mI3,t->mI1);
  314. return e;
  315. }
  316. void release(void)
  317. {
  318. mIds.clear();
  319. mEdgeMap.clear();
  320. mIndices.clear();
  321. mSplit.clear();
  322. delete []mInputTriangles;
  323. delete []mEdges;
  324. mInputTriangles = 0;
  325. mEdges = 0;
  326. mVcount = 0;
  327. mVertices = 0;
  328. mEdgeCount = 0;
  329. }
  330. virtual NxU32 * removeTjunctions(NxU32 vcount,
  331. const float *vertices,
  332. size_t tcount,
  333. const NxU32 *indices,
  334. size_t &tcount_out,
  335. const NxU32 * ids)
  336. {
  337. NxU32 *ret = 0;
  338. release();
  339. mVcount = vcount;
  340. mVertices = vertices;
  341. mTcount = (NxU32)tcount;
  342. tcount_out = 0;
  343. mTcount = (NxU32)tcount;
  344. mMaxTcount = (NxU32)tcount*2;
  345. mInputTriangles = new Triangle[mMaxTcount];
  346. Triangle *t = mInputTriangles;
  347. mEdges = new RtEdge[mMaxTcount*3];
  348. mEdgeCount = 0;
  349. NxU32 id = 0;
  350. RtEdge *e = mEdges;
  351. for (NxU32 i=0; i<tcount; i++)
  352. {
  353. if ( ids ) id = *ids++;
  354. e =init(t,indices,vertices,e,id);
  355. indices+=3;
  356. t++;
  357. }
  358. {
  359. TriangleVector test;
  360. for (EdgeMap::Iterator i = mEdgeMap.getIterator(); !i.done(); ++i)
  361. {
  362. RtEdge *e = (*i).second;
  363. if ( e->mNextEdge == 0 ) // open edge!
  364. {
  365. Triangle *t = e->mTriangle;
  366. if ( !t->mPending )
  367. {
  368. test.pushBack(t);
  369. t->mPending = true;
  370. }
  371. }
  372. }
  373. if ( !test.empty() )
  374. {
  375. TriangleVector::Iterator i;
  376. for (i=test.begin(); i!=test.end(); ++i)
  377. {
  378. Triangle *t = (*i);
  379. locateIntersection(t);
  380. }
  381. }
  382. }
  383. while ( !mSplit.empty() )
  384. {
  385. TriangleVector scan = mSplit;
  386. mSplit.clear();
  387. TriangleVector::Iterator i;
  388. for (i=scan.begin(); i!=scan.end(); ++i)
  389. {
  390. Triangle *t = (*i);
  391. locateIntersection(t);
  392. }
  393. }
  394. mIndices.clear();
  395. mIds.clear();
  396. t = mInputTriangles;
  397. for (NxU32 i=0; i<mTcount; i++)
  398. {
  399. mIndices.pushBack(t->mI1);
  400. mIndices.pushBack(t->mI2);
  401. mIndices.pushBack(t->mI3);
  402. mIds.pushBack(t->mId);
  403. t++;
  404. }
  405. mEdgeMap.clear();
  406. delete []mEdges;
  407. mEdges = 0;
  408. delete []mInputTriangles;
  409. mInputTriangles = 0;
  410. tcount_out = mIndices.size()/3;
  411. ret = tcount_out ? &mIndices[0] : 0;
  412. #ifdef _DEBUG
  413. if ( ret )
  414. {
  415. const NxU32 *scan = ret;
  416. for (NxU32 i=0; i<tcount_out; i++)
  417. {
  418. NxU32 i1 = scan[0];
  419. NxU32 i2 = scan[1];
  420. NxU32 i3 = scan[2];
  421. assert( i1 != i2 && i1 != i3 && i2 != i3 );
  422. scan+=3;
  423. }
  424. }
  425. #endif
  426. return ret;
  427. }
  428. Triangle * locateIntersection(Triangle *scan,Triangle *t)
  429. {
  430. Triangle *ret = 0;
  431. NxU32 t1 = (NxU32)(scan-mInputTriangles);
  432. NxU32 t2 = (NxU32)(t-mInputTriangles);
  433. NX_ASSERT( t1 < mTcount );
  434. NX_ASSERT( t2 < mTcount );
  435. NX_ASSERT( scan->mI1 < mVcount );
  436. NX_ASSERT( scan->mI2 < mVcount );
  437. NX_ASSERT( scan->mI3 < mVcount );
  438. NX_ASSERT( t->mI1 < mVcount );
  439. NX_ASSERT( t->mI2 < mVcount );
  440. NX_ASSERT( t->mI3 < mVcount );
  441. NxU32 intersection_index;
  442. NxU32 edge;
  443. if ( scan != t && scan->intersects(t,mVertices,intersection_index,edge,mEpsilon) )
  444. {
  445. if ( t->mI1 == intersection_index || t->mI2 == intersection_index || t->mI3 == intersection_index )
  446. {
  447. }
  448. else
  449. {
  450. // here is where it intersects!
  451. NxU32 i1,i2,i3;
  452. NxU32 j1,j2,j3;
  453. NxU32 id = t->mId;
  454. switch ( edge )
  455. {
  456. case 0:
  457. i1 = t->mI1;
  458. i2 = intersection_index;
  459. i3 = t->mI3;
  460. j1 = intersection_index;
  461. j2 = t->mI2;
  462. j3 = t->mI3;
  463. break;
  464. case 1:
  465. i1 = t->mI2;
  466. i2 = intersection_index;
  467. i3 = t->mI1;
  468. j1 = intersection_index;
  469. j2 = t->mI3;
  470. j3 = t->mI1;
  471. break;
  472. case 2:
  473. i1 = t->mI3;
  474. i2 = intersection_index;
  475. i3 = t->mI2;
  476. j1 = intersection_index;
  477. j2 = t->mI1;
  478. j3 = t->mI2;
  479. break;
  480. default:
  481. NX_ASSERT(0);
  482. i1 = i2 = i3 = 0;
  483. j1 = j2 = j3 = 0;
  484. break;
  485. }
  486. if ( mTcount < mMaxTcount )
  487. {
  488. t->init(i1,i2,i3,mVertices,id);
  489. Triangle *newt = &mInputTriangles[mTcount];
  490. newt->init(j1,j2,j3,mVertices,id);
  491. mTcount++;
  492. t->mSplit = true;
  493. newt->mSplit = true;
  494. mSplit.pushBack(t);
  495. mSplit.pushBack(newt);
  496. ret = scan;
  497. }
  498. }
  499. }
  500. return ret;
  501. }
  502. Triangle * testIntersection(Triangle *scan,Triangle *t)
  503. {
  504. Triangle *ret = 0;
  505. NxU32 t1 = (NxU32)(scan-mInputTriangles);
  506. NxU32 t2 = (NxU32)(t-mInputTriangles);
  507. NX_ASSERT( t1 < mTcount );
  508. NX_ASSERT( t2 < mTcount );
  509. NX_ASSERT( scan->mI1 < mVcount );
  510. NX_ASSERT( scan->mI2 < mVcount );
  511. NX_ASSERT( scan->mI3 < mVcount );
  512. NX_ASSERT( t->mI1 < mVcount );
  513. NX_ASSERT( t->mI2 < mVcount );
  514. NX_ASSERT( t->mI3 < mVcount );
  515. NxU32 intersection_index;
  516. NxU32 edge;
  517. assert( scan != t );
  518. if ( scan->intersects(t,mVertices,intersection_index,edge,mEpsilon) )
  519. {
  520. // here is where it intersects!
  521. NxU32 i1,i2,i3;
  522. NxU32 j1,j2,j3;
  523. NxU32 id = t->mId;
  524. switch ( edge )
  525. {
  526. case 0:
  527. i1 = t->mI1;
  528. i2 = intersection_index;
  529. i3 = t->mI3;
  530. j1 = intersection_index;
  531. j2 = t->mI2;
  532. j3 = t->mI3;
  533. break;
  534. case 1:
  535. i1 = t->mI2;
  536. i2 = intersection_index;
  537. i3 = t->mI1;
  538. j1 = intersection_index;
  539. j2 = t->mI3;
  540. j3 = t->mI1;
  541. break;
  542. case 2:
  543. i1 = t->mI3;
  544. i2 = intersection_index;
  545. i3 = t->mI2;
  546. j1 = intersection_index;
  547. j2 = t->mI1;
  548. j3 = t->mI2;
  549. break;
  550. default:
  551. NX_ASSERT(0);
  552. i1 = i2 = i3 = 0;
  553. j1 = j2 = j3 = 0;
  554. break;
  555. }
  556. if ( mTcount < mMaxTcount )
  557. {
  558. t->init(i1,i2,i3,mVertices,id);
  559. Triangle *newt = &mInputTriangles[mTcount];
  560. newt->init(j1,j2,j3,mVertices,id);
  561. mTcount++;
  562. t->mSplit = true;
  563. newt->mSplit = true;
  564. mSplit.pushBack(t);
  565. mSplit.pushBack(newt);
  566. ret = scan;
  567. }
  568. }
  569. return ret;
  570. }
  571. Triangle * locateIntersection(Triangle *t)
  572. {
  573. Triangle *ret = 0;
  574. Triangle *scan = mInputTriangles;
  575. for (NxU32 i=0; i<mTcount; i++)
  576. {
  577. ret = locateIntersection(scan,t);
  578. if ( ret )
  579. break;
  580. scan++;
  581. }
  582. return ret;
  583. }
  584. Triangle *mInputTriangles;
  585. NxU32 mVcount;
  586. NxU32 mMaxTcount;
  587. NxU32 mTcount;
  588. const float *mVertices;
  589. NxU32Vector mIndices;
  590. NxU32Vector mIds;
  591. TriangleVector mSplit;
  592. NxU32 mEdgeCount;
  593. RtEdge *mEdges;
  594. EdgeMap mEdgeMap;
  595. NxF32 mEpsilon;
  596. };
  597. RemoveTjunctions * createRemoveTjunctions(void)
  598. {
  599. MyRemoveTjunctions *m = new MyRemoveTjunctions;
  600. return static_cast< RemoveTjunctions *>(m);
  601. }
  602. void releaseRemoveTjunctions(RemoveTjunctions *tj)
  603. {
  604. MyRemoveTjunctions *m = static_cast< MyRemoveTjunctions *>(tj);
  605. delete m;
  606. }
  607. }; // end of namespace