SelfIntersectMesh.h 26 KB

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