SelfIntersectMesh.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. #ifndef IGL_SELFINTERSECTMESH_H
  2. #define IGL_SELFINTERSECTMESH_H
  3. #include "CGAL_includes.hpp"
  4. #include "selfintersect.h"
  5. #include <Eigen/Dense>
  6. #include <list>
  7. #include <map>
  8. #include <vector>
  9. #ifndef IGL_FIRST_HIT_EXCEPTION
  10. #define IGL_FIRST_HIT_EXCEPTION 10
  11. #endif
  12. // The easiest way to keep track of everything is to use a class
  13. namespace igl
  14. {
  15. // Kernel is a CGAL kernel like:
  16. // CGAL::Exact_predicates_inexact_constructions_kernel
  17. // or
  18. // CGAL::Exact_predicates_exact_constructions_kernel
  19. template <typename Kernel>
  20. class SelfIntersectMesh
  21. {
  22. public:
  23. // 3D Primitives
  24. typedef CGAL::Point_3<Kernel> Point_3;
  25. typedef CGAL::Segment_3<Kernel> Segment_3;
  26. typedef CGAL::Triangle_3<Kernel> Triangle_3;
  27. typedef CGAL::Plane_3<Kernel> Plane_3;
  28. typedef CGAL::Tetrahedron_3<Kernel> Tetrahedron_3;
  29. typedef CGAL::Polyhedron_3<Kernel> Polyhedron_3;
  30. typedef CGAL::Nef_polyhedron_3<Kernel> Nef_polyhedron_3;
  31. // 2D Primitives
  32. typedef CGAL::Point_2<Kernel> Point_2;
  33. typedef CGAL::Segment_2<Kernel> Segment_2;
  34. typedef CGAL::Triangle_2<Kernel> Triangle_2;
  35. // 2D Constrained Delaunay Triangulation types
  36. typedef CGAL::Triangulation_vertex_base_2<Kernel> TVB_2;
  37. typedef CGAL::Constrained_triangulation_face_base_2<Kernel> CTFB_2;
  38. typedef CGAL::Triangulation_data_structure_2<TVB_2,CTFB_2> TDS_2;
  39. typedef CGAL::Exact_intersections_tag Itag;
  40. typedef CGAL::Constrained_Delaunay_triangulation_2<Kernel,TDS_2,Itag>
  41. CDT_2;
  42. typedef CGAL::Constrained_triangulation_plus_2<CDT_2> CDT_plus_2;
  43. // Axis-align boxes for all-pairs self-intersection detection
  44. typedef std::vector<Triangle_3> Triangles;
  45. typedef typename Triangles::iterator TrianglesIterator;
  46. typedef typename Triangles::const_iterator TrianglesConstIterator;
  47. typedef
  48. CGAL::Box_intersection_d::Box_with_handle_d<double,3,TrianglesIterator>
  49. Box;
  50. // Input mesh
  51. const Eigen::MatrixXd & V;
  52. const Eigen::MatrixXi & F;
  53. // Number of self-intersecting triangle pairs
  54. int count;
  55. std::vector<std::list<CGAL::Object> > F_objects;
  56. Triangles T;
  57. std::list<int> lIF;
  58. std::vector<bool> offensive;
  59. std::vector<int> offending_index;
  60. std::vector<int> offending;
  61. // Make a short name for the edge map's key
  62. typedef std::pair<int,int> EMK;
  63. // Make a short name for the type stored at each edge, the edge map's
  64. // value
  65. typedef std::list<int> EMV;
  66. // Make a short name for the edge map
  67. typedef std::map<EMK,EMV> EdgeMap;
  68. EdgeMap edge2faces;
  69. public:
  70. SelfintersectParam params;
  71. public:
  72. // Constructs (VV,FF) a new mesh with self-intersections of (V,F)
  73. // subdivided
  74. inline SelfIntersectMesh(
  75. const Eigen::MatrixXd & V,
  76. const Eigen::MatrixXi & F,
  77. const SelfintersectParam & params,
  78. Eigen::MatrixXd & VV,
  79. Eigen::MatrixXi & FF,
  80. Eigen::MatrixXi & IF);
  81. private:
  82. // Helper function to mark a face as offensive
  83. //
  84. // Inputs:
  85. // f index of face in F
  86. inline void mark_offensive(const int f);
  87. // Helper function to count intersections between faces
  88. //
  89. // Input:
  90. // fa index of face A in F
  91. // fb index of face B in F
  92. inline void count_intersection(const int fa,const int fb);
  93. // Helper function for box_intersect. Intersect two triangles A and B,
  94. // append the intersection object (point,segment,triangle) to a running
  95. // list for A and B
  96. //
  97. // Inputs:
  98. // A triangle in 3D
  99. // B triangle in 3D
  100. // fa index of A in F (and F_objects)
  101. // fb index of A in F (and F_objects)
  102. // Returns true only if A intersects B
  103. //
  104. inline bool intersect(
  105. const Triangle_3 & A,
  106. const Triangle_3 & B,
  107. const int fa,
  108. const int fb);
  109. // Helper function for box_intersect. In the case where A and B have
  110. // already been identified to share a vertex, then we only want to add
  111. // possible segment intersections. Assumes truly duplicate triangles are
  112. // not given as input
  113. //
  114. // Inputs:
  115. // A triangle in 3D
  116. // B triangle in 3D
  117. // fa index of A in F (and F_objects)
  118. // fb index of B in F (and F_objects)
  119. // va index of shared vertex in A (and F_objects)
  120. // vb index of shared vertex in B (and F_objects)
  121. //// Returns object of intersection (should be Segment or point)
  122. // Returns true if intersection (besides shared point)
  123. //
  124. inline bool single_shared_vertex(
  125. const Triangle_3 & A,
  126. const Triangle_3 & B,
  127. const int fa,
  128. const int fb,
  129. const int va,
  130. const int vb);
  131. // Helper handling one direction
  132. inline bool single_shared_vertex(
  133. const Triangle_3 & A,
  134. const Triangle_3 & B,
  135. const int fa,
  136. const int fb,
  137. const int va);
  138. // Helper function for box_intersect. In the case where A and B have
  139. // already been identified to share two vertices, then we only want to add
  140. // a possible coplanar (Triangle) intersection. Assumes truly degenerate
  141. // facets are not givine as input.
  142. inline bool double_shared_vertex(
  143. const Triangle_3 & A,
  144. const Triangle_3 & B,
  145. const int fa,
  146. const int fb);
  147. public:
  148. // Callback function called during box self intersections test. Means
  149. // boxes a and b intersect. This method then checks if the triangles in
  150. // each box intersect and if so, then processes the intersections
  151. //
  152. // Inputs:
  153. // a box containing a triangle
  154. // b box containing a triangle
  155. inline void box_intersect(const Box& a, const Box& b);
  156. private:
  157. // Compute 2D delaunay triangulation of a given 3d triangle and a list of
  158. // intersection objects (points,segments,triangles). CGAL uses an affine
  159. // projection rather than an isometric projection, so we're not
  160. // guaranteed that the 2D delaunay triangulation here will be a delaunay
  161. // triangulation in 3D.
  162. //
  163. // Inputs:
  164. // A triangle in 3D
  165. // A_objects_3 updated list of intersection objects for A
  166. // Outputs:
  167. // cdt Contrained delaunay triangulation in projected 2D plane
  168. inline void projected_delaunay(
  169. const Triangle_3 & A,
  170. const std::list<CGAL::Object> & A_objects_3,
  171. CDT_plus_2 & cdt);
  172. // Getters:
  173. public:
  174. //const std::list<int>& get_lIF() const{ return lIF;}
  175. static inline void box_intersect(
  176. SelfIntersectMesh * SIM,
  177. const SelfIntersectMesh::Box &a,
  178. const SelfIntersectMesh::Box &b);
  179. };
  180. }
  181. // Implementation
  182. #include "mesh_to_cgal_triangle_list.h"
  183. #include <igl/REDRUM.h>
  184. #include <igl/C_STR.h>
  185. #include <boost/function.hpp>
  186. #include <boost/bind.hpp>
  187. #include <algorithm>
  188. #include <exception>
  189. #include <cassert>
  190. #include <iostream>
  191. // References:
  192. // http://minregret.googlecode.com/svn/trunk/skyline/src/extern/CGAL-3.3.1/examples/Polyhedron/polyhedron_self_intersection.cpp
  193. // http://www.cgal.org/Manual/3.9/examples/Boolean_set_operations_2/do_intersect.cpp
  194. // Q: Should we be using CGAL::Polyhedron_3?
  195. // A: No! Input is just a list of unoriented triangles. Polyhedron_3 requires
  196. // a 2-manifold.
  197. // A: But! It seems we could use CGAL::Triangulation_3. Though it won't be easy
  198. // to take advantage of functions like insert_in_facet because we want to
  199. // constrain segments. Hmmm. Actualy Triangulation_3 doesn't look right...
  200. //static void box_intersect(SelfIntersectMesh * SIM,const Box & A, const Box & B)
  201. //{
  202. // return SIM->box_intersect(A,B);
  203. //}
  204. // CGAL's box_self_intersection_d uses C-style function callbacks without
  205. // userdata. This is a leapfrog method for calling a member function. It should
  206. // be bound as if the prototype was:
  207. // static void box_intersect(const Box &a, const Box &b)
  208. // using boost:
  209. // boost::function<void(const Box &a,const Box &b)> cb
  210. // = boost::bind(&::box_intersect, this, _1,_2);
  211. //
  212. template <typename Kernel>
  213. inline void igl::SelfIntersectMesh<Kernel>::box_intersect(
  214. igl::SelfIntersectMesh<Kernel> * SIM,
  215. const typename igl::SelfIntersectMesh<Kernel>::Box &a,
  216. const typename igl::SelfIntersectMesh<Kernel>::Box &b)
  217. {
  218. SIM->box_intersect(a,b);
  219. }
  220. template <typename Kernel>
  221. inline igl::SelfIntersectMesh<Kernel>::SelfIntersectMesh(
  222. const Eigen::MatrixXd & V,
  223. const Eigen::MatrixXi & F,
  224. const SelfintersectParam & params,
  225. Eigen::MatrixXd & VV,
  226. Eigen::MatrixXi & FF,
  227. Eigen::MatrixXi & IF):
  228. V(V),
  229. F(F),
  230. count(0),
  231. F_objects(F.rows()),
  232. T(),
  233. lIF(),
  234. offensive(F.rows(),false),
  235. offending_index(F.rows(),-1),
  236. offending(),
  237. edge2faces(),
  238. params(params)
  239. {
  240. using namespace std;
  241. using namespace Eigen;
  242. // Compute and process self intersections
  243. mesh_to_cgal_triangle_list(V,F,T);
  244. // http://www.cgal.org/Manual/latest/doc_html/cgal_manual/Box_intersection_d/Chapter_main.html#Section_63.5
  245. // Create the corresponding vector of bounding boxes
  246. std::vector<Box> boxes;
  247. boxes.reserve(T.size());
  248. for (
  249. TrianglesIterator tit = T.begin();
  250. tit != T.end();
  251. ++tit)
  252. {
  253. boxes.push_back(Box(tit->bbox(), tit));
  254. }
  255. // Leapfrog callback
  256. boost::function<void(const Box &a,const Box &b)> cb
  257. = boost::bind(&box_intersect, this, _1,_2);
  258. // Run the self intersection algorithm with all defaults
  259. try{
  260. CGAL::box_self_intersection_d(boxes.begin(), boxes.end(),cb);
  261. }catch(int e)
  262. {
  263. // Rethrow if not IGL_FIRST_HIT_EXCEPTION
  264. if(e != IGL_FIRST_HIT_EXCEPTION)
  265. {
  266. throw e;
  267. }
  268. // Otherwise just fall through
  269. }
  270. // Convert lIF to Eigen matrix
  271. assert(lIF.size()%2 == 0);
  272. IF.resize(lIF.size()/2,2);
  273. {
  274. int i=0;
  275. for(
  276. typename list<int>::const_iterator ifit = lIF.begin();
  277. ifit!=lIF.end();
  278. )
  279. {
  280. IF(i,0) = (*ifit);
  281. ifit++;
  282. IF(i,1) = (*ifit);
  283. ifit++;
  284. i++;
  285. }
  286. }
  287. if(params.detect_only)
  288. {
  289. return;
  290. }
  291. int NF_count = 0;
  292. // list of new faces, we'll fix F later
  293. vector<MatrixXi> NF(offending.size());
  294. // list of new vertices
  295. list<Point_3> NV;
  296. int NV_count = 0;
  297. vector<CDT_plus_2> cdt(offending.size());
  298. vector<Plane_3> P(offending.size());
  299. // Use map for *all* faces
  300. map<typename CDT_plus_2::Vertex_handle,int> v2i;
  301. // Loop over offending triangles
  302. for(int o = 0;o<(int)offending.size();o++)
  303. {
  304. // index in F
  305. const int f = offending[o];
  306. projected_delaunay(T[f],F_objects[f],cdt[o]);
  307. // Q: Is this also delaunay in 3D?
  308. // A: No, because the projection is affine and delaunay is not affine
  309. // invariant.
  310. // Q: Then, can't we first get the 2D delaunay triangulation, then lift it
  311. // to 3D and flip any offending edges?
  312. // Plane of projection (also used by projected_delaunay)
  313. P[o] = Plane_3(T[f].vertex(0),T[f].vertex(1),T[f].vertex(2));
  314. // Build index map
  315. {
  316. int i=0;
  317. for(
  318. typename CDT_plus_2::Finite_vertices_iterator vit = cdt[o].finite_vertices_begin();
  319. vit != cdt[o].finite_vertices_end();
  320. ++vit)
  321. {
  322. if(i<3)
  323. {
  324. //cout<<T[f].vertex(i)<<
  325. // (T[f].vertex(i) == P[o].to_3d(vit->point())?" == ":" != ")<<
  326. // P[o].to_3d(vit->point())<<endl;
  327. #ifndef NDEBUG
  328. // I want to be sure that the original corners really show up as the
  329. // original corners of the CDT. I.e. I don't trust CGAL to maintain the
  330. // order
  331. assert(T[f].vertex(i) == P[o].to_3d(vit->point()));
  332. #endif
  333. // For first three, use original index in F
  334. v2i[vit] = F(f,i);
  335. }else
  336. {
  337. const Point_3 vit_point_3 = P[o].to_3d(vit->point());
  338. // First look up each edge's neighbors to see if exact point has
  339. // already been added (This makes everything a bit quadratic)
  340. bool found = false;
  341. for(int e = 0; e<3 && !found;e++)
  342. {
  343. // Index of F's eth edge in V
  344. int i = F(f,(e+1)%3);
  345. int j = F(f,(e+2)%3);
  346. // Be sure that i<j
  347. if(i>j)
  348. {
  349. swap(i,j);
  350. }
  351. assert(edge2faces.count(EMK(i,j))==1);
  352. // loop over neighbors
  353. for(
  354. list<int>::const_iterator nit = edge2faces[EMK(i,j)].begin();
  355. nit != edge2faces[EMK(i,j)].end() && !found;
  356. nit++)
  357. {
  358. // don't consider self
  359. if(*nit == f)
  360. {
  361. continue;
  362. }
  363. // index of neighbor in offending (to find its cdt)
  364. int no = offending_index[*nit];
  365. // Loop over vertices of that neighbor's cdt (might not have been
  366. // processed yet, but then it's OK because it'll just be empty)
  367. for(
  368. typename CDT_plus_2::Finite_vertices_iterator uit = cdt[no].finite_vertices_begin();
  369. uit != cdt[no].finite_vertices_end() && !found;
  370. ++uit)
  371. {
  372. if(vit_point_3 == P[no].to_3d(uit->point()))
  373. {
  374. assert(v2i.count(uit) == 1);
  375. v2i[vit] = v2i[uit];
  376. found = true;
  377. }
  378. }
  379. }
  380. }
  381. if(!found)
  382. {
  383. v2i[vit] = V.rows()+NV_count;
  384. NV.push_back(vit_point_3);
  385. NV_count++;
  386. }
  387. }
  388. i++;
  389. }
  390. }
  391. {
  392. int i = 0;
  393. // Resize to fit new number of triangles
  394. NF[o].resize(cdt[o].number_of_faces(),3);
  395. NF_count+=NF[o].rows();
  396. // Append new faces to NF
  397. for(
  398. typename CDT_plus_2::Finite_faces_iterator fit = cdt[o].finite_faces_begin();
  399. fit != cdt[o].finite_faces_end();
  400. ++fit)
  401. {
  402. NF[o](i,0) = v2i[fit->vertex(0)];
  403. NF[o](i,1) = v2i[fit->vertex(1)];
  404. NF[o](i,2) = v2i[fit->vertex(2)];
  405. i++;
  406. }
  407. }
  408. }
  409. assert(NV_count == (int)NV.size());
  410. // Build output
  411. #ifndef NDEBUG
  412. {
  413. int off_count = 0;
  414. for(int f = 0;f<F.rows();f++)
  415. {
  416. off_count+= (offensive[f]?1:0);
  417. }
  418. assert(off_count==(int)offending.size());
  419. }
  420. #endif
  421. // Append faces
  422. FF.resize(F.rows()-offending.size()+NF_count,3);
  423. // First append non-offending original faces
  424. // There's an Eigen way to do this in one line but I forget
  425. int off = 0;
  426. for(int f = 0;f<F.rows();f++)
  427. {
  428. if(!offensive[f])
  429. {
  430. FF.row(off++) = F.row(f);
  431. }
  432. }
  433. assert(off == (int)(F.rows()-offending.size()));
  434. // Now append replacement faces for offending faces
  435. for(int o = 0;o<(int)offending.size();o++)
  436. {
  437. FF.block(off,0,NF[o].rows(),3) = NF[o];
  438. off += NF[o].rows();
  439. }
  440. // Append vertices
  441. VV.resize(V.rows()+NV_count,3);
  442. VV.block(0,0,V.rows(),3) = V;
  443. {
  444. int i = 0;
  445. for(
  446. typename list<Point_3>::const_iterator nvit = NV.begin();
  447. nvit != NV.end();
  448. nvit++)
  449. {
  450. for(int d = 0;d<3;d++)
  451. {
  452. const Point_3 & p = *nvit;
  453. VV(V.rows()+i,d) = CGAL::to_double(p[d]);
  454. // This distinction does not seem necessary:
  455. //#ifdef INEXACT_CONSTRUCTION
  456. // VV(V.rows()+i,d) = CGAL::to_double(p[d]);
  457. //#else
  458. // VV(V.rows()+i,d) = CGAL::to_double(p[d].exact());
  459. //#endif
  460. }
  461. i++;
  462. }
  463. }
  464. // Q: Does this give the same result as TETGEN?
  465. // A: For the cow and beast, yes.
  466. // Q: Is tetgen faster than this CGAL implementation?
  467. // A: Well, yes. But Tetgen is only solving the detection (predicates)
  468. // problem. This is also appending the intersection objects (construction).
  469. // But maybe tetgen is still faster for the detection part. For example, this
  470. // CGAL implementation on the beast takes 98 seconds but tetgen detection
  471. // takes 14 seconds
  472. }
  473. template <typename Kernel>
  474. inline void igl::SelfIntersectMesh<Kernel>::mark_offensive(const int f)
  475. {
  476. using namespace std;
  477. lIF.push_back(f);
  478. if(!offensive[f])
  479. {
  480. offensive[f]=true;
  481. offending_index[f]=offending.size();
  482. offending.push_back(f);
  483. // Add to edge map
  484. for(int e = 0; e<3;e++)
  485. {
  486. // Index of F's eth edge in V
  487. int i = F(f,(e+1)%3);
  488. int j = F(f,(e+2)%3);
  489. // Be sure that i<j
  490. if(i>j)
  491. {
  492. swap(i,j);
  493. }
  494. // Create new list if there is no entry
  495. if(edge2faces.count(EMK(i,j))==0)
  496. {
  497. edge2faces[EMK(i,j)] = list<int>();
  498. }
  499. // append to list
  500. edge2faces[EMK(i,j)].push_back(f);
  501. }
  502. }
  503. }
  504. template <typename Kernel>
  505. inline void igl::SelfIntersectMesh<Kernel>::count_intersection(
  506. const int fa,
  507. const int fb)
  508. {
  509. mark_offensive(fa);
  510. mark_offensive(fb);
  511. this->count++;
  512. // We found the first intersection
  513. if(params.first_only && this->count >= 1)
  514. {
  515. throw IGL_FIRST_HIT_EXCEPTION;
  516. }
  517. }
  518. template <typename Kernel>
  519. inline bool igl::SelfIntersectMesh<Kernel>::intersect(
  520. const Triangle_3 & A,
  521. const Triangle_3 & B,
  522. const int fa,
  523. const int fb)
  524. {
  525. // Determine whether there is an intersection
  526. if(!CGAL::do_intersect(A,B))
  527. {
  528. return false;
  529. }
  530. if(!params.detect_only)
  531. {
  532. // Construct intersection
  533. CGAL::Object result = CGAL::intersection(A,B);
  534. F_objects[fa].push_back(result);
  535. F_objects[fb].push_back(result);
  536. }
  537. count_intersection(fa,fb);
  538. return true;
  539. }
  540. template <typename Kernel>
  541. inline bool igl::SelfIntersectMesh<Kernel>::single_shared_vertex(
  542. const Triangle_3 & A,
  543. const Triangle_3 & B,
  544. const int fa,
  545. const int fb,
  546. const int va,
  547. const int vb)
  548. {
  549. ////using namespace std;
  550. //CGAL::Object result = CGAL::intersection(A,B);
  551. //if(CGAL::object_cast<Segment_3 >(&result))
  552. //{
  553. // // Append to each triangle's running list
  554. // F_objects[fa].push_back(result);
  555. // F_objects[fb].push_back(result);
  556. // count_intersection(fa,fb);
  557. //}else
  558. //{
  559. // // Then intersection must be at point
  560. // // And point must be at shared vertex
  561. // assert(CGAL::object_cast<Point_3>(&result));
  562. //}
  563. if(single_shared_vertex(A,B,fa,fb,va))
  564. {
  565. return true;
  566. }
  567. return single_shared_vertex(B,A,fb,fa,vb);
  568. }
  569. template <typename Kernel>
  570. inline bool igl::SelfIntersectMesh<Kernel>::single_shared_vertex(
  571. const Triangle_3 & A,
  572. const Triangle_3 & B,
  573. const int fa,
  574. const int fb,
  575. const int va)
  576. {
  577. // This was not a good idea. It will not handle coplanar triangles well.
  578. using namespace std;
  579. Segment_3 sa(
  580. A.vertex((va+1)%3),
  581. A.vertex((va+2)%3));
  582. if(CGAL::do_intersect(sa,B))
  583. {
  584. CGAL::Object result = CGAL::intersection(sa,B);
  585. if(const Point_3 * p = CGAL::object_cast<Point_3 >(&result))
  586. {
  587. if(!params.detect_only)
  588. {
  589. // Single intersection --> segment from shared point to intersection
  590. CGAL::Object seg = CGAL::make_object(Segment_3(
  591. A.vertex(va),
  592. *p));
  593. F_objects[fa].push_back(seg);
  594. F_objects[fb].push_back(seg);
  595. }
  596. count_intersection(fa,fb);
  597. return true;
  598. }else if(CGAL::object_cast<Segment_3 >(&result))
  599. {
  600. //cerr<<REDRUM("Coplanar at: "<<fa<<" & "<<fb<<" (single shared).")<<endl;
  601. // Must be coplanar
  602. if(params.detect_only)
  603. {
  604. count_intersection(fa,fb);
  605. }else
  606. {
  607. // WRONG:
  608. //// Segment intersection --> triangle from shared point to intersection
  609. //CGAL::Object tri = CGAL::make_object(Triangle_3(
  610. // A.vertex(va),
  611. // s->vertex(0),
  612. // s->vertex(1)));
  613. //F_objects[fa].push_back(tri);
  614. //F_objects[fb].push_back(tri);
  615. //count_intersection(fa,fb);
  616. // Need to do full test. Intersection could be a general poly.
  617. bool test = intersect(A,B,fa,fb);
  618. ((void)test);
  619. assert(test);
  620. }
  621. return true;
  622. }else
  623. {
  624. cerr<<REDRUM("Segment ∩ triangle neither point nor segment?")<<endl;
  625. assert(false);
  626. }
  627. }
  628. return false;
  629. }
  630. template <typename Kernel>
  631. inline bool igl::SelfIntersectMesh<Kernel>::double_shared_vertex(
  632. const Triangle_3 & A,
  633. const Triangle_3 & B,
  634. const int fa,
  635. const int fb)
  636. {
  637. using namespace std;
  638. // Cheaper way to do this than calling do_intersect?
  639. if(
  640. // Can only have an intersection if co-planar
  641. (A.supporting_plane() == B.supporting_plane() ||
  642. A.supporting_plane() == B.supporting_plane().opposite()) &&
  643. CGAL::do_intersect(A,B))
  644. {
  645. // Construct intersection
  646. try
  647. {
  648. CGAL::Object result = CGAL::intersection(A,B);
  649. if(result)
  650. {
  651. if(CGAL::object_cast<Segment_3 >(&result))
  652. {
  653. // not coplanar
  654. return false;
  655. } else if(CGAL::object_cast<Point_3 >(&result))
  656. {
  657. // this "shouldn't" happen but does for inexact
  658. return false;
  659. } else
  660. {
  661. if(!params.detect_only)
  662. {
  663. // Triangle object
  664. F_objects[fa].push_back(result);
  665. F_objects[fb].push_back(result);
  666. }
  667. count_intersection(fa,fb);
  668. //cerr<<REDRUM("Coplanar at: "<<fa<<" & "<<fb<<" (double shared).")<<endl;
  669. return true;
  670. }
  671. }else
  672. {
  673. // CGAL::intersection is disagreeing with do_intersect
  674. return false;
  675. }
  676. }catch(...)
  677. {
  678. // This catches some cgal assertion:
  679. // CGAL error: assertion violation!
  680. // Expression : is_finite(d)
  681. // File : /opt/local/include/CGAL/GMP/Gmpq_type.h
  682. // Line : 132
  683. // Explanation:
  684. // But only if NDEBUG is not defined, otherwise there's an uncaught
  685. // "Floating point exception: 8" SIGFPE
  686. return false;
  687. }
  688. }
  689. // Shouldn't get here either
  690. assert(false);
  691. return false;
  692. }
  693. template <typename Kernel>
  694. inline void igl::SelfIntersectMesh<Kernel>::box_intersect(
  695. const Box& a,
  696. const Box& b)
  697. {
  698. using namespace std;
  699. // index in F and T
  700. int fa = a.handle()-T.begin();
  701. int fb = b.handle()-T.begin();
  702. const Triangle_3 & A = *a.handle();
  703. const Triangle_3 & B = *b.handle();
  704. // I'm not going to deal with degenerate triangles, though at some point we
  705. // should
  706. assert(!a.handle()->is_degenerate());
  707. assert(!b.handle()->is_degenerate());
  708. // Number of combinatorially shared vertices
  709. int comb_shared_vertices = 0;
  710. // Number of geometrically shared vertices (*not* including combinatorially
  711. // shared)
  712. int geo_shared_vertices = 0;
  713. // Keep track of shared vertex indices (we only handles single shared
  714. // vertices as a special case, so just need last/first/only ones)
  715. int va=-1,vb=-1;
  716. int ea,eb;
  717. for(ea=0;ea<3;ea++)
  718. {
  719. for(eb=0;eb<3;eb++)
  720. {
  721. if(F(fa,ea) == F(fb,eb))
  722. {
  723. comb_shared_vertices++;
  724. va = ea;
  725. vb = eb;
  726. }else if(A.vertex(ea) == B.vertex(eb))
  727. {
  728. geo_shared_vertices++;
  729. va = ea;
  730. vb = eb;
  731. }
  732. }
  733. }
  734. const int total_shared_vertices = comb_shared_vertices + geo_shared_vertices;
  735. if(comb_shared_vertices== 3)
  736. {
  737. // Combinatorially duplicate face, these should be removed by preprocessing
  738. cerr<<REDRUM("Facets "<<fa<<" and "<<fb<<" are combinatorial duplicates")<<endl;
  739. goto done;
  740. }
  741. if(total_shared_vertices== 3)
  742. {
  743. // Geometrically duplicate face, these should be removed by preprocessing
  744. cerr<<REDRUM("Facets "<<fa<<" and "<<fb<<" are geometrical duplicates")<<endl;
  745. goto done;
  746. }
  747. //// SPECIAL CASES ARE BROKEN FOR COPLANAR TRIANGLES
  748. //if(total_shared_vertices > 0)
  749. //{
  750. // bool coplanar =
  751. // CGAL::coplanar(A.vertex(0),A.vertex(1),A.vertex(2),B.vertex(0)) &&
  752. // CGAL::coplanar(A.vertex(0),A.vertex(1),A.vertex(2),B.vertex(1)) &&
  753. // CGAL::coplanar(A.vertex(0),A.vertex(1),A.vertex(2),B.vertex(2));
  754. // if(coplanar)
  755. // {
  756. // cerr<<MAGENTAGIN("Facets "<<fa<<" and "<<fb<<
  757. // " are coplanar and share vertices")<<endl;
  758. // goto full;
  759. // }
  760. //}
  761. if(total_shared_vertices == 2)
  762. {
  763. // Q: What about coplanar?
  764. //
  765. // o o
  766. // |\ /|
  767. // | \/ |
  768. // | /\ |
  769. // |/ \|
  770. // o----o
  771. double_shared_vertex(A,B,fa,fb);
  772. goto done;
  773. }
  774. assert(total_shared_vertices<=1);
  775. if(total_shared_vertices==1)
  776. {
  777. assert(va>=0 && va<3);
  778. assert(vb>=0 && vb<3);
  779. //#ifndef NDEBUG
  780. // CGAL::Object result =
  781. //#endif
  782. single_shared_vertex(A,B,fa,fb,va,vb);
  783. //#ifndef NDEBUG
  784. // if(!CGAL::object_cast<Segment_3 >(&result))
  785. // {
  786. // const Point_3 * p = CGAL::object_cast<Point_3 >(&result);
  787. // assert(p);
  788. // for(int ea=0;ea<3;ea++)
  789. // {
  790. // for(int eb=0;eb<3;eb++)
  791. // {
  792. // if(F(fa,ea) == F(fb,eb))
  793. // {
  794. // assert(*p==A.vertex(ea));
  795. // assert(*p==B.vertex(eb));
  796. // }
  797. // }
  798. // }
  799. // }
  800. //#endif
  801. }else
  802. {
  803. //full:
  804. // No geometrically shared vertices, do general intersect
  805. intersect(*a.handle(),*b.handle(),fa,fb);
  806. }
  807. done:
  808. return;
  809. }
  810. // Compute 2D delaunay triangulation of a given 3d triangle and a list of
  811. // intersection objects (points,segments,triangles). CGAL uses an affine
  812. // projection rather than an isometric projection, so we're not guaranteed
  813. // that the 2D delaunay triangulation here will be a delaunay triangulation
  814. // in 3D.
  815. //
  816. // Inputs:
  817. // A triangle in 3D
  818. // A_objects_3 updated list of intersection objects for A
  819. // Outputs:
  820. // cdt Contrained delaunay triangulation in projected 2D plane
  821. template <typename Kernel>
  822. inline void igl::SelfIntersectMesh<Kernel>::projected_delaunay(
  823. const Triangle_3 & A,
  824. const std::list<CGAL::Object> & A_objects_3,
  825. CDT_plus_2 & cdt)
  826. {
  827. using namespace std;
  828. // http://www.cgal.org/Manual/3.2/doc_html/cgal_manual/Triangulation_2/Chapter_main.html#Section_2D_Triangulations_Constrained_Plus
  829. // Plane of triangle A
  830. Plane_3 P(A.vertex(0),A.vertex(1),A.vertex(2));
  831. // Insert triangle into vertices
  832. typename CDT_plus_2::Vertex_handle corners[3];
  833. for(int i = 0;i<3;i++)
  834. {
  835. corners[i] = cdt.insert(P.to_2d(A.vertex(i)));
  836. }
  837. // Insert triangle edges as constraints
  838. for(int i = 0;i<3;i++)
  839. {
  840. cdt.insert_constraint( corners[(i+1)%3], corners[(i+2)%3]);
  841. }
  842. // Insert constraints for intersection objects
  843. for(
  844. typename list<CGAL::Object>::const_iterator lit = A_objects_3.begin();
  845. lit != A_objects_3.end();
  846. lit++)
  847. {
  848. CGAL::Object obj = *lit;
  849. if(const Point_3 *ipoint = CGAL::object_cast<Point_3 >(&obj))
  850. {
  851. // Add point
  852. cdt.insert(P.to_2d(*ipoint));
  853. } else if(const Segment_3 *iseg = CGAL::object_cast<Segment_3 >(&obj))
  854. {
  855. // Add segment constraint
  856. cdt.insert_constraint(P.to_2d(iseg->vertex(0)),P.to_2d(iseg->vertex(1)));
  857. } else if(const Triangle_3 *itri = CGAL::object_cast<Triangle_3 >(&obj))
  858. {
  859. // Add 3 segment constraints
  860. cdt.insert_constraint(P.to_2d(itri->vertex(0)),P.to_2d(itri->vertex(1)));
  861. cdt.insert_constraint(P.to_2d(itri->vertex(1)),P.to_2d(itri->vertex(2)));
  862. cdt.insert_constraint(P.to_2d(itri->vertex(2)),P.to_2d(itri->vertex(0)));
  863. } else if(const std::vector<Point_3 > *polyp =
  864. CGAL::object_cast< std::vector<Point_3 > >(&obj))
  865. {
  866. //cerr<<REDRUM("Poly...")<<endl;
  867. const std::vector<Point_3 > & poly = *polyp;
  868. const int m = poly.size();
  869. assert(m>=2);
  870. for(int p = 0;p<m;p++)
  871. {
  872. const int np = (p+1)%m;
  873. cdt.insert_constraint(P.to_2d(poly[p]),P.to_2d(poly[np]));
  874. }
  875. }else
  876. {
  877. cerr<<REDRUM("What is this object?!")<<endl;
  878. assert(false);
  879. }
  880. }
  881. }
  882. #endif