intersect_other.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include "intersect_other.h"
  9. #include "CGAL_includes.hpp"
  10. #include "mesh_to_cgal_triangle_list.h"
  11. #include "remesh_intersections.h"
  12. #include "../../slice_mask.h"
  13. #include "../../remove_unreferenced.h"
  14. #ifndef IGL_FIRST_HIT_EXCEPTION
  15. #define IGL_FIRST_HIT_EXCEPTION 10
  16. #endif
  17. // Un-exposed helper functions
  18. namespace igl
  19. {
  20. namespace copyleft
  21. {
  22. namespace cgal
  23. {
  24. template <typename DerivedF>
  25. static IGL_INLINE void push_result(
  26. const Eigen::PlainObjectBase<DerivedF> & F,
  27. const int f,
  28. const int f_other,
  29. const CGAL::Object & result,
  30. std::map<
  31. typename DerivedF::Index,
  32. std::vector<std::pair<typename DerivedF::Index, CGAL::Object> > > &
  33. offending)
  34. //std::map<
  35. // std::pair<typename DerivedF::Index,typename DerivedF::Index>,
  36. // std::vector<typename DerivedF::Index> > & edge2faces)
  37. {
  38. typedef typename DerivedF::Index Index;
  39. typedef std::pair<Index,Index> EMK;
  40. if(offending.count(f) == 0)
  41. {
  42. // first time marking, initialize with new id and empty list
  43. offending[f] = {};
  44. for(Index e = 0; e<3;e++)
  45. {
  46. // append face to edge's list
  47. Index i = F(f,(e+1)%3) < F(f,(e+2)%3) ? F(f,(e+1)%3) : F(f,(e+2)%3);
  48. Index j = F(f,(e+1)%3) < F(f,(e+2)%3) ? F(f,(e+2)%3) : F(f,(e+1)%3);
  49. //edge2faces[EMK(i,j)].push_back(f);
  50. }
  51. }
  52. offending[f].push_back({f_other,result});
  53. }
  54. template <
  55. typename Kernel,
  56. typename DerivedVA,
  57. typename DerivedFA,
  58. typename DerivedVB,
  59. typename DerivedFB,
  60. typename DerivedIF,
  61. typename DerivedVVAB,
  62. typename DerivedFFAB,
  63. typename DerivedJAB,
  64. typename DerivedIMAB>
  65. static IGL_INLINE bool intersect_other_helper(
  66. const Eigen::PlainObjectBase<DerivedVA> & VA,
  67. const Eigen::PlainObjectBase<DerivedFA> & FA,
  68. const Eigen::PlainObjectBase<DerivedVB> & VB,
  69. const Eigen::PlainObjectBase<DerivedFB> & FB,
  70. const RemeshSelfIntersectionsParam & params,
  71. Eigen::PlainObjectBase<DerivedIF> & IF,
  72. Eigen::PlainObjectBase<DerivedVVAB> & VVAB,
  73. Eigen::PlainObjectBase<DerivedFFAB> & FFAB,
  74. Eigen::PlainObjectBase<DerivedJAB> & JAB,
  75. Eigen::PlainObjectBase<DerivedIMAB> & IMAB)
  76. {
  77. using namespace std;
  78. using namespace Eigen;
  79. typedef typename DerivedFA::Index Index;
  80. // 3D Primitives
  81. typedef CGAL::Point_3<Kernel> Point_3;
  82. typedef CGAL::Segment_3<Kernel> Segment_3;
  83. typedef CGAL::Triangle_3<Kernel> Triangle_3;
  84. typedef CGAL::Plane_3<Kernel> Plane_3;
  85. typedef CGAL::Tetrahedron_3<Kernel> Tetrahedron_3;
  86. // 2D Primitives
  87. typedef CGAL::Point_2<Kernel> Point_2;
  88. typedef CGAL::Segment_2<Kernel> Segment_2;
  89. typedef CGAL::Triangle_2<Kernel> Triangle_2;
  90. // 2D Constrained Delaunay Triangulation types
  91. typedef CGAL::Triangulation_vertex_base_2<Kernel> TVB_2;
  92. typedef CGAL::Constrained_triangulation_face_base_2<Kernel> CTAB_2;
  93. typedef CGAL::Triangulation_data_structure_2<TVB_2,CTAB_2> TDS_2;
  94. typedef CGAL::Exact_intersections_tag Itag;
  95. // Axis-align boxes for all-pairs self-intersection detection
  96. typedef std::vector<Triangle_3> Triangles;
  97. typedef typename Triangles::iterator TrianglesIterator;
  98. typedef typename Triangles::const_iterator TrianglesConstIterator;
  99. typedef
  100. CGAL::Box_intersection_d::Box_with_handle_d<double,3,TrianglesIterator>
  101. Box;
  102. typedef
  103. std::map<Index,std::vector<std::pair<Index,CGAL::Object> > >
  104. OffendingMap;
  105. typedef std::map<std::pair<Index,Index>,std::vector<Index> > EdgeMap;
  106. typedef std::pair<Index,Index> EMK;
  107. Triangles TA,TB;
  108. // Compute and process self intersections
  109. mesh_to_cgal_triangle_list(VA,FA,TA);
  110. mesh_to_cgal_triangle_list(VB,FB,TB);
  111. // http://www.cgal.org/Manual/latest/doc_html/cgal_manual/Box_intersection_d/Chapter_main.html#Section_63.5
  112. // Create the corresponding vector of bounding boxes
  113. std::vector<Box> A_boxes,B_boxes;
  114. const auto box_up = [](Triangles & T, std::vector<Box> & boxes) -> void
  115. {
  116. boxes.reserve(T.size());
  117. for (
  118. TrianglesIterator tit = T.begin();
  119. tit != T.end();
  120. ++tit)
  121. {
  122. boxes.push_back(Box(tit->bbox(), tit));
  123. }
  124. };
  125. box_up(TA,A_boxes);
  126. box_up(TB,B_boxes);
  127. OffendingMap offendingA,offendingB;
  128. //EdgeMap edge2facesA,edge2facesB;
  129. std::list<int> lIF;
  130. const auto cb = [&](const Box &a, const Box &b) -> void
  131. {
  132. using namespace std;
  133. // index in F and T
  134. int fa = a.handle()-TA.begin();
  135. int fb = b.handle()-TB.begin();
  136. const Triangle_3 & A = *a.handle();
  137. const Triangle_3 & B = *b.handle();
  138. if(CGAL::do_intersect(A,B))
  139. {
  140. // There was an intersection
  141. lIF.push_back(fa);
  142. lIF.push_back(fb);
  143. if(params.first_only)
  144. {
  145. throw IGL_FIRST_HIT_EXCEPTION;
  146. }
  147. if(!params.detect_only)
  148. {
  149. CGAL::Object result = CGAL::intersection(A,B);
  150. push_result(FA,fa,fb,result,offendingA);
  151. push_result(FB,fb,fa,result,offendingB);
  152. }
  153. }
  154. };
  155. try{
  156. CGAL::box_intersection_d(
  157. A_boxes.begin(), A_boxes.end(),
  158. B_boxes.begin(), B_boxes.end(),
  159. cb);
  160. }catch(int e)
  161. {
  162. // Rethrow if not FIRST_HIT_EXCEPTION
  163. if(e != IGL_FIRST_HIT_EXCEPTION)
  164. {
  165. throw e;
  166. }
  167. // Otherwise just fall through
  168. }
  169. // Convert lIF to Eigen matrix
  170. assert(lIF.size()%2 == 0);
  171. IF.resize(lIF.size()/2,2);
  172. {
  173. int i=0;
  174. for(
  175. list<int>::const_iterator ifit = lIF.begin();
  176. ifit!=lIF.end();
  177. )
  178. {
  179. IF(i,0) = (*ifit);
  180. ifit++;
  181. IF(i,1) = (*ifit);
  182. ifit++;
  183. i++;
  184. }
  185. }
  186. if(!params.detect_only)
  187. {
  188. // Obsolete, now remesh_intersections expects a single mesh
  189. // remesh_intersections(VA,FA,TA,offendingA,VVA,FFA,JA,IMA);
  190. // remesh_intersections(VB,FB,TB,offendingB,VVB,FFB,JB,IMB);
  191. // Combine mesh and offending maps
  192. DerivedVA VAB(VA.rows()+VB.rows(),3);
  193. VAB<<VA,VB;
  194. DerivedFA FAB(FA.rows()+FB.rows(),3);
  195. FAB<<FA,(FB.array()+VA.rows());
  196. Triangles TAB;
  197. TAB.reserve(TA.size()+TB.size());
  198. TAB.insert(TAB.end(),TA.begin(),TA.end());
  199. TAB.insert(TAB.end(),TB.begin(),TB.end());
  200. OffendingMap offending;
  201. //offending.reserve(offendingA.size() + offendingB.size());
  202. for (const auto itr : offendingA)
  203. {
  204. // Remap offenders in FB to FAB
  205. auto offenders = itr.second;
  206. for(auto & offender : offenders)
  207. {
  208. offender.first += FA.rows();
  209. }
  210. offending[itr.first] = offenders;
  211. }
  212. for (const auto itr : offendingB)
  213. {
  214. // Store offenders for FB according to place in FAB
  215. offending[FA.rows() + itr.first] = itr.second;
  216. }
  217. remesh_intersections(
  218. VAB,FAB,TAB,offending,params.stitch_all,VVAB,FFAB,JAB,IMAB);
  219. }
  220. return IF.rows() > 0;
  221. }
  222. }
  223. }
  224. }
  225. template <
  226. typename DerivedVA,
  227. typename DerivedFA,
  228. typename DerivedVB,
  229. typename DerivedFB,
  230. typename DerivedIF,
  231. typename DerivedVVAB,
  232. typename DerivedFFAB,
  233. typename DerivedJAB,
  234. typename DerivedIMAB>
  235. IGL_INLINE bool igl::copyleft::cgal::intersect_other(
  236. const Eigen::PlainObjectBase<DerivedVA> & VA,
  237. const Eigen::PlainObjectBase<DerivedFA> & FA,
  238. const Eigen::PlainObjectBase<DerivedVB> & VB,
  239. const Eigen::PlainObjectBase<DerivedFB> & FB,
  240. const RemeshSelfIntersectionsParam & params,
  241. Eigen::PlainObjectBase<DerivedIF> & IF,
  242. Eigen::PlainObjectBase<DerivedVVAB> & VVAB,
  243. Eigen::PlainObjectBase<DerivedFFAB> & FFAB,
  244. Eigen::PlainObjectBase<DerivedJAB> & JAB,
  245. Eigen::PlainObjectBase<DerivedIMAB> & IMAB)
  246. {
  247. if(params.detect_only)
  248. {
  249. return intersect_other_helper<CGAL::Epick>
  250. (VA,FA,VB,FB,params,IF,VVAB,FFAB,JAB,IMAB);
  251. }else
  252. {
  253. return intersect_other_helper<CGAL::Epeck>
  254. (VA,FA,VB,FB,params,IF,VVAB,FFAB,JAB,IMAB);
  255. }
  256. }
  257. IGL_INLINE bool igl::copyleft::cgal::intersect_other(
  258. const Eigen::MatrixXd & VA,
  259. const Eigen::MatrixXi & FA,
  260. const Eigen::MatrixXd & VB,
  261. const Eigen::MatrixXi & FB,
  262. const bool first_only,
  263. Eigen::MatrixXi & IF)
  264. {
  265. Eigen::MatrixXd VVAB;
  266. Eigen::MatrixXi FFAB;
  267. Eigen::VectorXi JAB,IMAB;
  268. return intersect_other(
  269. VA,FA,VB,FB,{true,first_only},IF,VVAB,FFAB,JAB,IMAB);
  270. }
  271. #ifdef IGL_STATIC_LIBRARY
  272. // Explicit template instantiation
  273. // generated by autoexplicit.sh
  274. template bool igl::copyleft::cgal::intersect_other<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, igl::copyleft::cgal::RemeshSelfIntersectionsParam const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  275. template bool igl::copyleft::cgal::intersect_other<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, -1, 1, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, igl::copyleft::cgal::RemeshSelfIntersectionsParam const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<CGAL::Lazy_exact_nt<CGAL::Gmpq>, -1, -1, 1, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  276. #endif