SelfIntersectMesh.h 28 KB

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