SelfIntersectMesh.h 27 KB

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