intersect_other.cpp 9.6 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. typedef CGAL::Constrained_Delaunay_triangulation_2<Kernel,TDS_2,Itag>
  96. CDT_2;
  97. typedef CGAL::Constrained_triangulation_plus_2<CDT_2> CDT_plus_2;
  98. // Axis-align boxes for all-pairs self-intersection detection
  99. typedef std::vector<Triangle_3> Triangles;
  100. typedef typename Triangles::iterator TrianglesIterator;
  101. typedef typename Triangles::const_iterator TrianglesConstIterator;
  102. typedef
  103. CGAL::Box_intersection_d::Box_with_handle_d<double,3,TrianglesIterator>
  104. Box;
  105. typedef
  106. std::map<Index,std::vector<std::pair<Index,CGAL::Object> > >
  107. OffendingMap;
  108. typedef std::map<std::pair<Index,Index>,std::vector<Index> > EdgeMap;
  109. typedef std::pair<Index,Index> EMK;
  110. Triangles TA,TB;
  111. // Compute and process self intersections
  112. mesh_to_cgal_triangle_list(VA,FA,TA);
  113. mesh_to_cgal_triangle_list(VB,FB,TB);
  114. // http://www.cgal.org/Manual/latest/doc_html/cgal_manual/Box_intersection_d/Chapter_main.html#Section_63.5
  115. // Create the corresponding vector of bounding boxes
  116. std::vector<Box> A_boxes,B_boxes;
  117. const auto box_up = [](Triangles & T, std::vector<Box> & boxes) -> void
  118. {
  119. boxes.reserve(T.size());
  120. for (
  121. TrianglesIterator tit = T.begin();
  122. tit != T.end();
  123. ++tit)
  124. {
  125. boxes.push_back(Box(tit->bbox(), tit));
  126. }
  127. };
  128. box_up(TA,A_boxes);
  129. box_up(TB,B_boxes);
  130. OffendingMap offendingA,offendingB;
  131. //EdgeMap edge2facesA,edge2facesB;
  132. std::list<int> lIF;
  133. const auto cb = [&](const Box &a, const Box &b) -> void
  134. {
  135. using namespace std;
  136. // index in F and T
  137. int fa = a.handle()-TA.begin();
  138. int fb = b.handle()-TB.begin();
  139. const Triangle_3 & A = *a.handle();
  140. const Triangle_3 & B = *b.handle();
  141. if(CGAL::do_intersect(A,B))
  142. {
  143. // There was an intersection
  144. lIF.push_back(fa);
  145. lIF.push_back(fb);
  146. if(params.first_only)
  147. {
  148. throw IGL_FIRST_HIT_EXCEPTION;
  149. }
  150. if(!params.detect_only)
  151. {
  152. CGAL::Object result = CGAL::intersection(A,B);
  153. push_result(FA,fa,fb,result,offendingA);
  154. push_result(FB,fb,fa,result,offendingB);
  155. }
  156. }
  157. };
  158. try{
  159. CGAL::box_intersection_d(
  160. A_boxes.begin(), A_boxes.end(),
  161. B_boxes.begin(), B_boxes.end(),
  162. cb);
  163. }catch(int e)
  164. {
  165. // Rethrow if not FIRST_HIT_EXCEPTION
  166. if(e != IGL_FIRST_HIT_EXCEPTION)
  167. {
  168. throw e;
  169. }
  170. // Otherwise just fall through
  171. }
  172. // Convert lIF to Eigen matrix
  173. assert(lIF.size()%2 == 0);
  174. IF.resize(lIF.size()/2,2);
  175. {
  176. int i=0;
  177. for(
  178. list<int>::const_iterator ifit = lIF.begin();
  179. ifit!=lIF.end();
  180. )
  181. {
  182. IF(i,0) = (*ifit);
  183. ifit++;
  184. IF(i,1) = (*ifit);
  185. ifit++;
  186. i++;
  187. }
  188. }
  189. if(!params.detect_only)
  190. {
  191. // Obsolete, now remesh_intersections expects a single mesh
  192. // remesh_intersections(VA,FA,TA,offendingA,VVA,FFA,JA,IMA);
  193. // remesh_intersections(VB,FB,TB,offendingB,VVB,FFB,JB,IMB);
  194. // Combine mesh and offending maps
  195. DerivedVA VAB(VA.rows()+VB.rows(),3);
  196. VAB<<VA,VB;
  197. DerivedFA FAB(FA.rows()+FB.rows(),3);
  198. FAB<<FA,(FB.array()+VA.rows());
  199. Triangles TAB;
  200. TAB.reserve(TA.size()+TB.size());
  201. TAB.insert(TAB.end(),TA.begin(),TA.end());
  202. TAB.insert(TAB.end(),TB.begin(),TB.end());
  203. OffendingMap offending;
  204. //offending.reserve(offendingA.size() + offendingB.size());
  205. for (const auto itr : offendingA)
  206. {
  207. // Remap offenders in FB to FAB
  208. auto offenders = itr.second;
  209. for(auto & offender : offenders)
  210. {
  211. offender.first += FA.rows();
  212. }
  213. offending[itr.first] = offenders;
  214. }
  215. for (const auto itr : offendingB)
  216. {
  217. // Store offenders for FB according to place in FAB
  218. offending[FA.rows() + itr.first] = itr.second;
  219. }
  220. remesh_intersections(
  221. VAB,FAB,TAB,offending,params.stitch_all,VVAB,FFAB,JAB,IMAB);
  222. }
  223. return IF.rows() > 0;
  224. }
  225. }
  226. }
  227. }
  228. template <
  229. typename DerivedVA,
  230. typename DerivedFA,
  231. typename DerivedVB,
  232. typename DerivedFB,
  233. typename DerivedIF,
  234. typename DerivedVVAB,
  235. typename DerivedFFAB,
  236. typename DerivedJAB,
  237. typename DerivedIMAB>
  238. IGL_INLINE bool igl::copyleft::cgal::intersect_other(
  239. const Eigen::PlainObjectBase<DerivedVA> & VA,
  240. const Eigen::PlainObjectBase<DerivedFA> & FA,
  241. const Eigen::PlainObjectBase<DerivedVB> & VB,
  242. const Eigen::PlainObjectBase<DerivedFB> & FB,
  243. const RemeshSelfIntersectionsParam & params,
  244. Eigen::PlainObjectBase<DerivedIF> & IF,
  245. Eigen::PlainObjectBase<DerivedVVAB> & VVAB,
  246. Eigen::PlainObjectBase<DerivedFFAB> & FFAB,
  247. Eigen::PlainObjectBase<DerivedJAB> & JAB,
  248. Eigen::PlainObjectBase<DerivedIMAB> & IMAB)
  249. {
  250. if(params.detect_only)
  251. {
  252. return intersect_other_helper<CGAL::Epick>
  253. (VA,FA,VB,FB,params,IF,VVAB,FFAB,JAB,IMAB);
  254. }else
  255. {
  256. return intersect_other_helper<CGAL::Epeck>
  257. (VA,FA,VB,FB,params,IF,VVAB,FFAB,JAB,IMAB);
  258. }
  259. }
  260. IGL_INLINE bool igl::copyleft::cgal::intersect_other(
  261. const Eigen::MatrixXd & VA,
  262. const Eigen::MatrixXi & FA,
  263. const Eigen::MatrixXd & VB,
  264. const Eigen::MatrixXi & FB,
  265. const bool first_only,
  266. Eigen::MatrixXi & IF)
  267. {
  268. Eigen::MatrixXd VVAB;
  269. Eigen::MatrixXi FFAB;
  270. Eigen::VectorXi JAB,IMAB;
  271. return intersect_other(
  272. VA,FA,VB,FB,{true,first_only},IF,VVAB,FFAB,JAB,IMAB);
  273. }
  274. #ifdef IGL_STATIC_LIBRARY
  275. // Explicit template specialization
  276. #endif