NvRemoveTjunctions.cpp 15 KB

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