SelfIntersectMesh.h 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187
  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. const size_t noff = offending.size();
  385. // Unfortunately it looks like CGAL has trouble allocating memory when
  386. // multiple openmp threads are running. Crashes durring CDT...
  387. //# pragma omp parallel for if (noff>1000)
  388. for(Index o = 0;o<(Index)noff;o++)
  389. {
  390. // index in F
  391. const Index f = offending[o];
  392. {
  393. projected_delaunay(T[f],F_objects[f],cdt[o]);
  394. }
  395. // Q: Is this also delaunay in 3D?
  396. // A: No, because the projection is affine and delaunay is not affine
  397. // invariant.
  398. // Q: Then, can't we first get the 2D delaunay triangulation, then lift it
  399. // to 3D and flip any offending edges?
  400. // Plane of projection (also used by projected_delaunay)
  401. P[o] = Plane_3(T[f].vertex(0),T[f].vertex(1),T[f].vertex(2));
  402. // Build index map
  403. {
  404. Index i=0;
  405. for(
  406. typename CDT_plus_2::Finite_vertices_iterator vit = cdt[o].finite_vertices_begin();
  407. vit != cdt[o].finite_vertices_end();
  408. ++vit)
  409. {
  410. if(i<3)
  411. {
  412. //cout<<T[f].vertex(i)<<
  413. // (T[f].vertex(i) == P[o].to_3d(vit->point())?" == ":" != ")<<
  414. // P[o].to_3d(vit->point())<<endl;
  415. #ifndef NDEBUG
  416. // I want to be sure that the original corners really show up as the
  417. // original corners of the CDT. I.e. I don't trust CGAL to maintain
  418. // the order
  419. assert(T[f].vertex(i) == P[o].to_3d(vit->point()));
  420. #endif
  421. // For first three, use original index in F
  422. //# pragma omp critical
  423. v2i[vit] = F(f,i);
  424. }else
  425. {
  426. const Point_3 vit_point_3 = P[o].to_3d(vit->point());
  427. // First look up each edge's neighbors to see if exact point has
  428. // already been added (This makes everything a bit quadratic)
  429. bool found = false;
  430. for(int e = 0; e<3 && !found;e++)
  431. {
  432. // Index of F's eth edge in V
  433. Index i = F(f,(e+1)%3);
  434. Index j = F(f,(e+2)%3);
  435. // Be sure that i<j
  436. if(i>j)
  437. {
  438. swap(i,j);
  439. }
  440. assert(edge2faces.count(EMK(i,j))==1);
  441. // loop over neighbors
  442. for(
  443. typename IndexList::const_iterator nit = edge2faces[EMK(i,j)].begin();
  444. nit != edge2faces[EMK(i,j)].end() && !found;
  445. nit++)
  446. {
  447. // don't consider self
  448. if(*nit == f)
  449. {
  450. continue;
  451. }
  452. // index of neighbor in offending (to find its cdt)
  453. Index no = offending_index[*nit];
  454. // Loop over vertices of that neighbor's cdt (might not have been
  455. // processed yet, but then it's OK because it'll just be empty)
  456. for(
  457. typename CDT_plus_2::Finite_vertices_iterator uit = cdt[no].finite_vertices_begin();
  458. uit != cdt[no].finite_vertices_end() && !found;
  459. ++uit)
  460. {
  461. if(vit_point_3 == P[no].to_3d(uit->point()))
  462. {
  463. assert(v2i.count(uit) == 1);
  464. //# pragma omp critical
  465. v2i[vit] = v2i[uit];
  466. found = true;
  467. }
  468. }
  469. }
  470. }
  471. if(!found)
  472. {
  473. //# pragma omp critical
  474. {
  475. v2i[vit] = V.rows()+NV_count;
  476. NV.push_back(vit_point_3);
  477. NV_count++;
  478. }
  479. }
  480. }
  481. i++;
  482. }
  483. }
  484. {
  485. Index i = 0;
  486. // Resize to fit new number of triangles
  487. NF[o].resize(cdt[o].number_of_faces(),3);
  488. //# pragma omp atomic
  489. NF_count+=NF[o].rows();
  490. // Append new faces to NF
  491. for(
  492. typename CDT_plus_2::Finite_faces_iterator fit = cdt[o].finite_faces_begin();
  493. fit != cdt[o].finite_faces_end();
  494. ++fit)
  495. {
  496. NF[o](i,0) = v2i[fit->vertex(0)];
  497. NF[o](i,1) = v2i[fit->vertex(1)];
  498. NF[o](i,2) = v2i[fit->vertex(2)];
  499. i++;
  500. }
  501. }
  502. }
  503. //cout<<"CDT: "<<tictoc()<<" "<<t_proj_del<<endl;
  504. assert(NV_count == (Index)NV.size());
  505. // Build output
  506. #ifndef NDEBUG
  507. {
  508. Index off_count = 0;
  509. for(Index f = 0;f<F.rows();f++)
  510. {
  511. off_count+= (offensive[f]?1:0);
  512. }
  513. assert(off_count==(Index)offending.size());
  514. }
  515. #endif
  516. // Append faces
  517. FF.resize(F.rows()-offending.size()+NF_count,3);
  518. J.resize(FF.rows());
  519. // First append non-offending original faces
  520. // There's an Eigen way to do this in one line but I forget
  521. Index off = 0;
  522. for(Index f = 0;f<F.rows();f++)
  523. {
  524. if(!offensive[f])
  525. {
  526. FF.row(off) = F.row(f);
  527. J(off) = f;
  528. off++;
  529. }
  530. }
  531. assert(off == (Index)(F.rows()-offending.size()));
  532. // Now append replacement faces for offending faces
  533. for(Index o = 0;o<(Index)offending.size();o++)
  534. {
  535. FF.block(off,0,NF[o].rows(),3) = NF[o];
  536. J.block(off,0,NF[o].rows(),1).setConstant(offending[o]);
  537. off += NF[o].rows();
  538. }
  539. // Append vertices
  540. VV.resize(V.rows()+NV_count,3);
  541. VV.block(0,0,V.rows(),3) = V;
  542. {
  543. Index i = 0;
  544. for(
  545. typename Point_3List::const_iterator nvit = NV.begin();
  546. nvit != NV.end();
  547. nvit++)
  548. {
  549. for(Index d = 0;d<3;d++)
  550. {
  551. const Point_3 & p = *nvit;
  552. VV(V.rows()+i,d) = CGAL::to_double(p[d]);
  553. // This distinction does not seem necessary:
  554. //#ifdef INEXACT_CONSTRUCTION
  555. // VV(V.rows()+i,d) = CGAL::to_double(p[d]);
  556. //#else
  557. // VV(V.rows()+i,d) = CGAL::to_double(p[d].exact());
  558. //#endif
  559. }
  560. i++;
  561. }
  562. }
  563. IM.resize(VV.rows(),1);
  564. map<Point_3,Index> vv2i;
  565. // Safe to check for duplicates using double for original vertices: if
  566. // incoming reps are different then the points are unique.
  567. for(Index v = 0;v<V.rows();v++)
  568. {
  569. const Point_3 p(V(v,0),V(v,1),V(v,2));
  570. if(vv2i.count(p)==0)
  571. {
  572. vv2i[p] = v;
  573. }
  574. assert(vv2i.count(p) == 1);
  575. IM(v) = vv2i[p];
  576. }
  577. // Must check for duplicates of new vertices using exact.
  578. {
  579. Index v = V.rows();
  580. for(
  581. typename Point_3List::const_iterator nvit = NV.begin();
  582. nvit != NV.end();
  583. nvit++)
  584. {
  585. const Point_3 & p = *nvit;
  586. if(vv2i.count(p)==0)
  587. {
  588. vv2i[p] = v;
  589. }
  590. assert(vv2i.count(p) == 1);
  591. IM(v) = vv2i[p];
  592. v++;
  593. }
  594. }
  595. //cout<<"Output + dupes: "<<tictoc()<<endl;
  596. // Q: Does this give the same result as TETGEN?
  597. // A: For the cow and beast, yes.
  598. // Q: Is tetgen faster than this CGAL implementation?
  599. // A: Well, yes. But Tetgen is only solving the detection (predicates)
  600. // problem. This is also appending the intersection objects (construction).
  601. // But maybe tetgen is still faster for the detection part. For example, this
  602. // CGAL implementation on the beast takes 98 seconds but tetgen detection
  603. // takes 14 seconds
  604. }
  605. template <
  606. typename Kernel,
  607. typename DerivedV,
  608. typename DerivedF,
  609. typename DerivedVV,
  610. typename DerivedFF,
  611. typename DerivedIF,
  612. typename DerivedJ,
  613. typename DerivedIM>
  614. inline void igl::SelfIntersectMesh<
  615. Kernel,
  616. DerivedV,
  617. DerivedF,
  618. DerivedVV,
  619. DerivedFF,
  620. DerivedIF,
  621. DerivedJ,
  622. DerivedIM>::mark_offensive(const Index f)
  623. {
  624. using namespace std;
  625. lIF.push_back(f);
  626. if(!offensive[f])
  627. {
  628. offensive[f]=true;
  629. offending_index[f]=offending.size();
  630. offending.push_back(f);
  631. // Add to edge map
  632. for(Index e = 0; e<3;e++)
  633. {
  634. // Index of F's eth edge in V
  635. Index i = F(f,(e+1)%3);
  636. Index j = F(f,(e+2)%3);
  637. // Be sure that i<j
  638. if(i>j)
  639. {
  640. swap(i,j);
  641. }
  642. // Create new list if there is no entry
  643. if(edge2faces.count(EMK(i,j))==0)
  644. {
  645. edge2faces[EMK(i,j)] = EMV();
  646. }
  647. // append to list
  648. edge2faces[EMK(i,j)].push_back(f);
  649. }
  650. }
  651. }
  652. template <
  653. typename Kernel,
  654. typename DerivedV,
  655. typename DerivedF,
  656. typename DerivedVV,
  657. typename DerivedFF,
  658. typename DerivedIF,
  659. typename DerivedJ,
  660. typename DerivedIM>
  661. inline void igl::SelfIntersectMesh<
  662. Kernel,
  663. DerivedV,
  664. DerivedF,
  665. DerivedVV,
  666. DerivedFF,
  667. DerivedIF,
  668. DerivedJ,
  669. DerivedIM>::count_intersection(
  670. const Index fa,
  671. const Index fb)
  672. {
  673. mark_offensive(fa);
  674. mark_offensive(fb);
  675. this->count++;
  676. // We found the first intersection
  677. if(params.first_only && this->count >= 1)
  678. {
  679. throw IGL_FIRST_HIT_EXCEPTION;
  680. }
  681. }
  682. template <
  683. typename Kernel,
  684. typename DerivedV,
  685. typename DerivedF,
  686. typename DerivedVV,
  687. typename DerivedFF,
  688. typename DerivedIF,
  689. typename DerivedJ,
  690. typename DerivedIM>
  691. inline bool igl::SelfIntersectMesh<
  692. Kernel,
  693. DerivedV,
  694. DerivedF,
  695. DerivedVV,
  696. DerivedFF,
  697. DerivedIF,
  698. DerivedJ,
  699. DerivedIM>::intersect(
  700. const Triangle_3 & A,
  701. const Triangle_3 & B,
  702. const Index fa,
  703. const Index fb)
  704. {
  705. // Determine whether there is an intersection
  706. if(!CGAL::do_intersect(A,B))
  707. {
  708. return false;
  709. }
  710. if(!params.detect_only)
  711. {
  712. // Construct intersection
  713. CGAL::Object result = CGAL::intersection(A,B);
  714. F_objects[fa].push_back(result);
  715. F_objects[fb].push_back(result);
  716. }
  717. count_intersection(fa,fb);
  718. return true;
  719. }
  720. template <
  721. typename Kernel,
  722. typename DerivedV,
  723. typename DerivedF,
  724. typename DerivedVV,
  725. typename DerivedFF,
  726. typename DerivedIF,
  727. typename DerivedJ,
  728. typename DerivedIM>
  729. inline bool igl::SelfIntersectMesh<
  730. Kernel,
  731. DerivedV,
  732. DerivedF,
  733. DerivedVV,
  734. DerivedFF,
  735. DerivedIF,
  736. DerivedJ,
  737. DerivedIM>::single_shared_vertex(
  738. const Triangle_3 & A,
  739. const Triangle_3 & B,
  740. const Index fa,
  741. const Index fb,
  742. const Index va,
  743. const Index vb)
  744. {
  745. ////using namespace std;
  746. //CGAL::Object result = CGAL::intersection(A,B);
  747. //if(CGAL::object_cast<Segment_3 >(&result))
  748. //{
  749. // // Append to each triangle's running list
  750. // F_objects[fa].push_back(result);
  751. // F_objects[fb].push_back(result);
  752. // count_intersection(fa,fb);
  753. //}else
  754. //{
  755. // // Then intersection must be at point
  756. // // And point must be at shared vertex
  757. // assert(CGAL::object_cast<Point_3>(&result));
  758. //}
  759. if(single_shared_vertex(A,B,fa,fb,va))
  760. {
  761. return true;
  762. }
  763. return single_shared_vertex(B,A,fb,fa,vb);
  764. }
  765. template <
  766. typename Kernel,
  767. typename DerivedV,
  768. typename DerivedF,
  769. typename DerivedVV,
  770. typename DerivedFF,
  771. typename DerivedIF,
  772. typename DerivedJ,
  773. typename DerivedIM>
  774. inline bool igl::SelfIntersectMesh<
  775. Kernel,
  776. DerivedV,
  777. DerivedF,
  778. DerivedVV,
  779. DerivedFF,
  780. DerivedIF,
  781. DerivedJ,
  782. DerivedIM>::single_shared_vertex(
  783. const Triangle_3 & A,
  784. const Triangle_3 & B,
  785. const Index fa,
  786. const Index fb,
  787. const Index va)
  788. {
  789. // This was not a good idea. It will not handle coplanar triangles well.
  790. using namespace std;
  791. Segment_3 sa(
  792. A.vertex((va+1)%3),
  793. A.vertex((va+2)%3));
  794. if(CGAL::do_intersect(sa,B))
  795. {
  796. CGAL::Object result = CGAL::intersection(sa,B);
  797. if(const Point_3 * p = CGAL::object_cast<Point_3 >(&result))
  798. {
  799. if(!params.detect_only)
  800. {
  801. // Single intersection --> segment from shared point to intersection
  802. CGAL::Object seg = CGAL::make_object(Segment_3(
  803. A.vertex(va),
  804. *p));
  805. F_objects[fa].push_back(seg);
  806. F_objects[fb].push_back(seg);
  807. }
  808. count_intersection(fa,fb);
  809. return true;
  810. }else if(CGAL::object_cast<Segment_3 >(&result))
  811. {
  812. //cerr<<REDRUM("Coplanar at: "<<fa<<" & "<<fb<<" (single shared).")<<endl;
  813. // Must be coplanar
  814. if(params.detect_only)
  815. {
  816. count_intersection(fa,fb);
  817. }else
  818. {
  819. // WRONG:
  820. //// Segment intersection --> triangle from shared point to intersection
  821. //CGAL::Object tri = CGAL::make_object(Triangle_3(
  822. // A.vertex(va),
  823. // s->vertex(0),
  824. // s->vertex(1)));
  825. //F_objects[fa].push_back(tri);
  826. //F_objects[fb].push_back(tri);
  827. //count_intersection(fa,fb);
  828. // Need to do full test. Intersection could be a general poly.
  829. bool test = intersect(A,B,fa,fb);
  830. ((void)test);
  831. assert(test);
  832. }
  833. return true;
  834. }else
  835. {
  836. cerr<<REDRUM("Segment ∩ triangle neither point nor segment?")<<endl;
  837. assert(false);
  838. }
  839. }
  840. return false;
  841. }
  842. template <
  843. typename Kernel,
  844. typename DerivedV,
  845. typename DerivedF,
  846. typename DerivedVV,
  847. typename DerivedFF,
  848. typename DerivedIF,
  849. typename DerivedJ,
  850. typename DerivedIM>
  851. inline bool igl::SelfIntersectMesh<
  852. Kernel,
  853. DerivedV,
  854. DerivedF,
  855. DerivedVV,
  856. DerivedFF,
  857. DerivedIF,
  858. DerivedJ,
  859. DerivedIM>::double_shared_vertex(
  860. const Triangle_3 & A,
  861. const Triangle_3 & B,
  862. const Index fa,
  863. const Index fb)
  864. {
  865. using namespace std;
  866. // Cheaper way to do this than calling do_intersect?
  867. if(
  868. // Can only have an intersection if co-planar
  869. (A.supporting_plane() == B.supporting_plane() ||
  870. A.supporting_plane() == B.supporting_plane().opposite()) &&
  871. CGAL::do_intersect(A,B))
  872. {
  873. // Construct intersection
  874. try
  875. {
  876. CGAL::Object result = CGAL::intersection(A,B);
  877. if(result)
  878. {
  879. if(CGAL::object_cast<Segment_3 >(&result))
  880. {
  881. // not coplanar
  882. return false;
  883. } else if(CGAL::object_cast<Point_3 >(&result))
  884. {
  885. // this "shouldn't" happen but does for inexact
  886. return false;
  887. } else
  888. {
  889. if(!params.detect_only)
  890. {
  891. // Triangle object
  892. F_objects[fa].push_back(result);
  893. F_objects[fb].push_back(result);
  894. }
  895. count_intersection(fa,fb);
  896. //cerr<<REDRUM("Coplanar at: "<<fa<<" & "<<fb<<" (double shared).")<<endl;
  897. return true;
  898. }
  899. }else
  900. {
  901. // CGAL::intersection is disagreeing with do_intersect
  902. return false;
  903. }
  904. }catch(...)
  905. {
  906. // This catches some cgal assertion:
  907. // CGAL error: assertion violation!
  908. // Expression : is_finite(d)
  909. // File : /opt/local/include/CGAL/GMP/Gmpq_type.h
  910. // Line : 132
  911. // Explanation:
  912. // But only if NDEBUG is not defined, otherwise there's an uncaught
  913. // "Floating point exception: 8" SIGFPE
  914. return false;
  915. }
  916. }
  917. // No intersection.
  918. return false;
  919. }
  920. template <
  921. typename Kernel,
  922. typename DerivedV,
  923. typename DerivedF,
  924. typename DerivedVV,
  925. typename DerivedFF,
  926. typename DerivedIF,
  927. typename DerivedJ,
  928. typename DerivedIM>
  929. inline void igl::SelfIntersectMesh<
  930. Kernel,
  931. DerivedV,
  932. DerivedF,
  933. DerivedVV,
  934. DerivedFF,
  935. DerivedIF,
  936. DerivedJ,
  937. DerivedIM>::box_intersect(
  938. const Box& a,
  939. const Box& b)
  940. {
  941. using namespace std;
  942. // Could we write this as a static function of:
  943. //
  944. // F.row(fa)
  945. // F.row(fb)
  946. // A
  947. // B
  948. // index in F and T
  949. Index fa = a.handle()-T.begin();
  950. Index fb = b.handle()-T.begin();
  951. const Triangle_3 & A = *a.handle();
  952. const Triangle_3 & B = *b.handle();
  953. // I'm not going to deal with degenerate triangles, though at some point we
  954. // should
  955. assert(!a.handle()->is_degenerate());
  956. assert(!b.handle()->is_degenerate());
  957. // Number of combinatorially shared vertices
  958. Index comb_shared_vertices = 0;
  959. // Number of geometrically shared vertices (*not* including combinatorially
  960. // shared)
  961. Index geo_shared_vertices = 0;
  962. // Keep track of shared vertex indices (we only handles single shared
  963. // vertices as a special case, so just need last/first/only ones)
  964. Index va=-1,vb=-1;
  965. Index ea,eb;
  966. for(ea=0;ea<3;ea++)
  967. {
  968. for(eb=0;eb<3;eb++)
  969. {
  970. if(F(fa,ea) == F(fb,eb))
  971. {
  972. comb_shared_vertices++;
  973. va = ea;
  974. vb = eb;
  975. }else if(A.vertex(ea) == B.vertex(eb))
  976. {
  977. geo_shared_vertices++;
  978. va = ea;
  979. vb = eb;
  980. }
  981. }
  982. }
  983. const Index total_shared_vertices = comb_shared_vertices + geo_shared_vertices;
  984. if(comb_shared_vertices== 3)
  985. {
  986. // Combinatorially duplicate face, these should be removed by preprocessing
  987. cerr<<REDRUM("Facets "<<fa<<" and "<<fb<<" are combinatorial duplicates")<<endl;
  988. goto done;
  989. }
  990. if(total_shared_vertices== 3)
  991. {
  992. // Geometrically duplicate face, these should be removed by preprocessing
  993. cerr<<REDRUM("Facets "<<fa<<" and "<<fb<<" are geometrical duplicates")<<endl;
  994. goto done;
  995. }
  996. //// SPECIAL CASES ARE BROKEN FOR COPLANAR TRIANGLES
  997. //if(total_shared_vertices > 0)
  998. //{
  999. // bool coplanar =
  1000. // CGAL::coplanar(A.vertex(0),A.vertex(1),A.vertex(2),B.vertex(0)) &&
  1001. // CGAL::coplanar(A.vertex(0),A.vertex(1),A.vertex(2),B.vertex(1)) &&
  1002. // CGAL::coplanar(A.vertex(0),A.vertex(1),A.vertex(2),B.vertex(2));
  1003. // if(coplanar)
  1004. // {
  1005. // cerr<<MAGENTAGIN("Facets "<<fa<<" and "<<fb<<
  1006. // " are coplanar and share vertices")<<endl;
  1007. // goto full;
  1008. // }
  1009. //}
  1010. if(total_shared_vertices == 2)
  1011. {
  1012. // Q: What about coplanar?
  1013. //
  1014. // o o
  1015. // |\ /|
  1016. // | \/ |
  1017. // | /\ |
  1018. // |/ \|
  1019. // o----o
  1020. double_shared_vertex(A,B,fa,fb);
  1021. goto done;
  1022. }
  1023. assert(total_shared_vertices<=1);
  1024. if(total_shared_vertices==1)
  1025. {
  1026. assert(va>=0 && va<3);
  1027. assert(vb>=0 && vb<3);
  1028. //#ifndef NDEBUG
  1029. // CGAL::Object result =
  1030. //#endif
  1031. single_shared_vertex(A,B,fa,fb,va,vb);
  1032. //#ifndef NDEBUG
  1033. // if(!CGAL::object_cast<Segment_3 >(&result))
  1034. // {
  1035. // const Point_3 * p = CGAL::object_cast<Point_3 >(&result);
  1036. // assert(p);
  1037. // for(int ea=0;ea<3;ea++)
  1038. // {
  1039. // for(int eb=0;eb<3;eb++)
  1040. // {
  1041. // if(F(fa,ea) == F(fb,eb))
  1042. // {
  1043. // assert(*p==A.vertex(ea));
  1044. // assert(*p==B.vertex(eb));
  1045. // }
  1046. // }
  1047. // }
  1048. // }
  1049. //#endif
  1050. }else
  1051. {
  1052. //full:
  1053. // No geometrically shared vertices, do general intersect
  1054. intersect(*a.handle(),*b.handle(),fa,fb);
  1055. }
  1056. done:
  1057. return;
  1058. }
  1059. // Compute 2D delaunay triangulation of a given 3d triangle and a list of
  1060. // intersection objects (points,segments,triangles). CGAL uses an affine
  1061. // projection rather than an isometric projection, so we're not guaranteed
  1062. // that the 2D delaunay triangulation here will be a delaunay triangulation
  1063. // in 3D.
  1064. //
  1065. // Inputs:
  1066. // A triangle in 3D
  1067. // A_objects_3 updated list of intersection objects for A
  1068. // Outputs:
  1069. // cdt Contrained delaunay triangulation in projected 2D plane
  1070. template <
  1071. typename Kernel,
  1072. typename DerivedV,
  1073. typename DerivedF,
  1074. typename DerivedVV,
  1075. typename DerivedFF,
  1076. typename DerivedIF,
  1077. typename DerivedJ,
  1078. typename DerivedIM>
  1079. inline void igl::SelfIntersectMesh<
  1080. Kernel,
  1081. DerivedV,
  1082. DerivedF,
  1083. DerivedVV,
  1084. DerivedFF,
  1085. DerivedIF,
  1086. DerivedJ,
  1087. DerivedIM>::projected_delaunay(
  1088. const Triangle_3 & A,
  1089. const ObjectList & A_objects_3,
  1090. CDT_plus_2 & cdt)
  1091. {
  1092. using namespace std;
  1093. // http://www.cgal.org/Manual/3.2/doc_html/cgal_manual/Triangulation_2/Chapter_main.html#Section_2D_Triangulations_Constrained_Plus
  1094. // Plane of triangle A
  1095. Plane_3 P(A.vertex(0),A.vertex(1),A.vertex(2));
  1096. // Insert triangle into vertices
  1097. typename CDT_plus_2::Vertex_handle corners[3];
  1098. for(Index i = 0;i<3;i++)
  1099. {
  1100. corners[i] = cdt.insert(P.to_2d(A.vertex(i)));
  1101. }
  1102. // Insert triangle edges as constraints
  1103. for(Index i = 0;i<3;i++)
  1104. {
  1105. cdt.insert_constraint( corners[(i+1)%3], corners[(i+2)%3]);
  1106. }
  1107. // Insert constraints for intersection objects
  1108. for( const auto & obj : A_objects_3)
  1109. {
  1110. if(const Segment_3 *iseg = CGAL::object_cast<Segment_3 >(&obj))
  1111. {
  1112. // Add segment constraint
  1113. cdt.insert_constraint(P.to_2d(iseg->vertex(0)),P.to_2d(iseg->vertex(1)));
  1114. }else if(const Point_3 *ipoint = CGAL::object_cast<Point_3 >(&obj))
  1115. {
  1116. // Add point
  1117. cdt.insert(P.to_2d(*ipoint));
  1118. } else if(const Triangle_3 *itri = CGAL::object_cast<Triangle_3 >(&obj))
  1119. {
  1120. // Add 3 segment constraints
  1121. cdt.insert_constraint(P.to_2d(itri->vertex(0)),P.to_2d(itri->vertex(1)));
  1122. cdt.insert_constraint(P.to_2d(itri->vertex(1)),P.to_2d(itri->vertex(2)));
  1123. cdt.insert_constraint(P.to_2d(itri->vertex(2)),P.to_2d(itri->vertex(0)));
  1124. } else if(const std::vector<Point_3 > *polyp =
  1125. CGAL::object_cast< std::vector<Point_3 > >(&obj))
  1126. {
  1127. //cerr<<REDRUM("Poly...")<<endl;
  1128. const std::vector<Point_3 > & poly = *polyp;
  1129. const Index m = poly.size();
  1130. assert(m>=2);
  1131. for(Index p = 0;p<m;p++)
  1132. {
  1133. const Index np = (p+1)%m;
  1134. cdt.insert_constraint(P.to_2d(poly[p]),P.to_2d(poly[np]));
  1135. }
  1136. }else
  1137. {
  1138. cerr<<REDRUM("What is this object?!")<<endl;
  1139. assert(false);
  1140. }
  1141. }
  1142. }
  1143. #endif