SelfIntersectMesh.h 26 KB

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