SelfIntersectMesh.h 29 KB

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