SelfIntersectMesh.h 28 KB

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