SelfIntersectMesh.h 32 KB

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