intersect_other.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "intersect_other.h"
  2. #include "CGAL_includes.hpp"
  3. #include "mesh_to_cgal_triangle_list.h"
  4. #ifndef IGL_FIRST_HIT_EXCEPTION
  5. #define IGL_FIRST_HIT_EXCEPTION 10
  6. #endif
  7. IGL_INLINE void igl::intersect_other(
  8. const Eigen::MatrixXd & V,
  9. const Eigen::MatrixXi & F,
  10. const Eigen::MatrixXd & U,
  11. const Eigen::MatrixXi & G,
  12. const bool first_only,
  13. Eigen::MatrixXi & IF)
  14. {
  15. using namespace std;
  16. using namespace Eigen;
  17. typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
  18. // 3D Primitives
  19. typedef CGAL::Point_3<Kernel> Point_3;
  20. typedef CGAL::Segment_3<Kernel> Segment_3;
  21. typedef CGAL::Triangle_3<Kernel> Triangle_3;
  22. typedef CGAL::Plane_3<Kernel> Plane_3;
  23. typedef CGAL::Tetrahedron_3<Kernel> Tetrahedron_3;
  24. typedef CGAL::Polyhedron_3<Kernel> Polyhedron_3;
  25. typedef CGAL::Nef_polyhedron_3<Kernel> Nef_polyhedron_3;
  26. // 2D Primitives
  27. typedef CGAL::Point_2<Kernel> Point_2;
  28. typedef CGAL::Segment_2<Kernel> Segment_2;
  29. typedef CGAL::Triangle_2<Kernel> Triangle_2;
  30. // 2D Constrained Delaunay Triangulation types
  31. typedef CGAL::Triangulation_vertex_base_2<Kernel> TVB_2;
  32. typedef CGAL::Constrained_triangulation_face_base_2<Kernel> CTFB_2;
  33. typedef CGAL::Triangulation_data_structure_2<TVB_2,CTFB_2> TDS_2;
  34. typedef CGAL::Exact_intersections_tag Itag;
  35. typedef CGAL::Constrained_Delaunay_triangulation_2<Kernel,TDS_2,Itag>
  36. CDT_2;
  37. typedef CGAL::Constrained_triangulation_plus_2<CDT_2> CDT_plus_2;
  38. // Axis-align boxes for all-pairs self-intersection detection
  39. typedef std::vector<Triangle_3> Triangles;
  40. typedef typename Triangles::iterator TrianglesIterator;
  41. typedef typename Triangles::const_iterator TrianglesConstIterator;
  42. typedef
  43. CGAL::Box_intersection_d::Box_with_handle_d<double,3,TrianglesIterator>
  44. Box;
  45. Triangles TF,TG;
  46. // Compute and process self intersections
  47. mesh_to_cgal_triangle_list(V,F,TF);
  48. mesh_to_cgal_triangle_list(U,G,TG);
  49. // http://www.cgal.org/Manual/latest/doc_html/cgal_manual/Box_intersection_d/Chapter_main.html#Section_63.5
  50. // Create the corresponding vector of bounding boxes
  51. std::vector<Box> F_boxes,G_boxes;
  52. const auto box_up = [](Triangles & T, std::vector<Box> & boxes)
  53. {
  54. boxes.reserve(T.size());
  55. for (
  56. TrianglesIterator tit = T.begin();
  57. tit != T.end();
  58. ++tit)
  59. {
  60. boxes.push_back(Box(tit->bbox(), tit));
  61. }
  62. };
  63. box_up(TF,F_boxes);
  64. box_up(TG,G_boxes);
  65. std::list<int> lIF;
  66. const auto cb = [&](const Box &a, const Box &b)
  67. {
  68. using namespace std;
  69. // index in F and T
  70. int fa = a.handle()-TF.begin();
  71. int fb = b.handle()-TG.begin();
  72. const Triangle_3 & A = *a.handle();
  73. const Triangle_3 & B = *b.handle();
  74. if(CGAL::do_intersect(A,B))
  75. {
  76. // There was an intersection
  77. lIF.push_back(fa);
  78. lIF.push_back(fb);
  79. if(first_only)
  80. {
  81. throw IGL_FIRST_HIT_EXCEPTION;
  82. }
  83. }
  84. };
  85. try{
  86. CGAL::box_intersection_d(
  87. F_boxes.begin(), F_boxes.end(),
  88. G_boxes.begin(), G_boxes.end(),
  89. cb);
  90. }catch(int e)
  91. {
  92. // Rethrow if not FIRST_HIT_EXCEPTION
  93. if(e != IGL_FIRST_HIT_EXCEPTION)
  94. {
  95. throw e;
  96. }
  97. // Otherwise just fall through
  98. }
  99. // Convert lIF to Eigen matrix
  100. assert(lIF.size()%2 == 0);
  101. IF.resize(lIF.size()/2,2);
  102. {
  103. int i=0;
  104. for(
  105. list<int>::const_iterator ifit = lIF.begin();
  106. ifit!=lIF.end();
  107. )
  108. {
  109. IF(i,0) = (*ifit);
  110. ifit++;
  111. IF(i,1) = (*ifit);
  112. ifit++;
  113. i++;
  114. }
  115. }
  116. }