SelfIntersectMesh.h 38 KB

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