fast_find_mesh_selfintersect.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2022 Vladimir S. FONOV <[email protected]>
  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 "fast_find_mesh_selfintersect.h"
  9. #include "AABB.h"
  10. #include "Guigue2003_tri_tri_intersect.h"
  11. template <
  12. typename DerivedV,
  13. typename DerivedF,
  14. typename DerivedI>
  15. IGL_INLINE bool igl::fast_find_mesh_selfintersect(
  16. const Eigen::MatrixBase<DerivedV>& V,
  17. const Eigen::MatrixBase<DerivedF>& F,
  18. Eigen::PlainObjectBase<DerivedI>& intersect)
  19. {
  20. using Scalar=typename DerivedV::Scalar;
  21. using BBOX=Eigen::AlignedBox<Scalar,3>;
  22. using AABBTree=igl::AABB<DerivedV,3>;
  23. AABBTree tree;
  24. tree.init(V,F);
  25. bool _intersects=false;
  26. intersect.resize(F.rows(),1);
  27. intersect.setConstant(0);
  28. for(int i=0; i<F.rows(); ++i)
  29. {
  30. if( intersect(i) ) continue;
  31. BBOX tri_box;
  32. for(int j=0;j<3;++j)
  33. tri_box.extend( V.row( F(i,j) ).transpose() );
  34. auto adjacent_faces = [](auto A,auto B)->bool {
  35. for(int i=0;i<3;++i)
  36. for(int j=0;j<3;j++)
  37. {
  38. if(A(i)==B(j))
  39. return true;
  40. }
  41. return false;
  42. };
  43. // find leaf nodes containing intersecting tri_box
  44. // need to declare full type to enable recursion
  45. std::function<bool(const AABBTree &,int)> check_intersect =
  46. [&](const AABBTree &t,int d) -> bool
  47. {
  48. if(t.m_primitive != -1) //check for the actual intersection (is_leaf)
  49. {
  50. if(t.m_primitive==i) //itself
  51. return false;
  52. if(adjacent_faces(F.row(i), F.row(t.m_primitive)) )
  53. return false;
  54. bool coplanar=false;
  55. Eigen::Matrix<Scalar,1,3,Eigen::RowMajor> edge1,edge2;
  56. if(igl::tri_tri_intersection_test_3d(
  57. V.row(F(i,0)),V.row(F(i,1)),V.row(F(i,2)),
  58. V.row(F(t.m_primitive,0)),V.row(F(t.m_primitive,1)),V.row(F(t.m_primitive,2)),
  59. coplanar,
  60. edge1,edge2))
  61. {
  62. if(!coplanar)
  63. {
  64. intersect(i)=1;
  65. intersect(t.m_primitive)=1;
  66. return true;
  67. }
  68. }
  69. } else {
  70. if(t.m_box.intersects(tri_box)) {
  71. // need to check both subtrees
  72. bool r1=check_intersect(*t.m_left ,d+1);
  73. bool r2=check_intersect(*t.m_right,d+1);
  74. return r1||r2;
  75. }
  76. }
  77. return false;
  78. };
  79. bool r=check_intersect(tree,0);
  80. _intersects = _intersects || r;
  81. }
  82. return _intersects;
  83. }
  84. template <
  85. typename DerivedV,
  86. typename DerivedF,
  87. typename DerivedI,
  88. typename DerivedE>
  89. IGL_INLINE bool igl::fast_find_mesh_selfintersect(
  90. const Eigen::MatrixBase<DerivedV>& V,
  91. const Eigen::MatrixBase<DerivedF>& F,
  92. Eigen::PlainObjectBase<DerivedI>& intersect,
  93. Eigen::PlainObjectBase<DerivedE>& edges )
  94. {
  95. using Scalar=typename DerivedV::Scalar;
  96. using BBOX=Eigen::AlignedBox<Scalar,3>;
  97. using AABBTree=igl::AABB<DerivedV,3>;
  98. using EDGE=Eigen::Matrix<typename DerivedE::Scalar,1,3,Eigen::RowMajor>;
  99. std::vector<EDGE> _edges;
  100. AABBTree tree;
  101. tree.init(V,F);
  102. bool _intersects=false;
  103. intersect.resize(F.rows(),1);
  104. intersect.setConstant(0);
  105. for(int i=0; i<F.rows(); ++i)
  106. {
  107. int _inter=-1;
  108. if( intersect(i) ) continue;
  109. BBOX tri_box;
  110. for(int j=0;j<3;++j)
  111. tri_box.extend( V.row( F(i,j) ).transpose() );
  112. auto adjacent_faces = [](auto A,auto B)->bool {
  113. for(int i=0;i<3;++i)
  114. for(int j=0;j<3;++j)
  115. {
  116. if(A(i) == B(j))
  117. return true;
  118. }
  119. return false;
  120. };
  121. // find leaf nodes containing intersecting tri_box
  122. std::function<bool(const AABBTree &,int)> check_intersect =
  123. [&](const AABBTree &t,int d) -> bool
  124. {
  125. if(t.m_primitive != -1) //check for the actual intersection //t.is_leaf()
  126. {
  127. if(t.m_primitive==i) //itself
  128. return false;
  129. if(adjacent_faces(F.row(i), F.row(t.m_primitive)) )
  130. return false;
  131. bool coplanar=false;
  132. EDGE edge1,edge2;
  133. if(igl::tri_tri_intersection_test_3d(
  134. V.row(F(i,0)), V.row(F(i,1)), V.row(F(i,2)),
  135. V.row(F(t.m_primitive,0)),V.row(F(t.m_primitive,1)),V.row(F(t.m_primitive,2)),
  136. coplanar,
  137. edge1,edge2))
  138. {
  139. if(!coplanar)
  140. {
  141. intersect(i)=1;
  142. intersect(t.m_primitive)=1;
  143. _edges.push_back(edge1);
  144. _edges.push_back(edge2);
  145. return true;
  146. }
  147. }
  148. } else {
  149. if(t.m_box.intersects( tri_box ))
  150. {
  151. bool r1=check_intersect(*t.m_left, d+1);
  152. bool r2=check_intersect(*t.m_right,d+1);
  153. return r1||r2;
  154. }
  155. }
  156. return false;
  157. };
  158. // run actual search
  159. bool r=check_intersect(tree, 0);
  160. _intersects = _intersects || r;
  161. }
  162. edges.resize(_edges.size(),3);
  163. int i=0;
  164. for(auto e=std::cbegin(_edges);e!=std::cend(_edges);++e,++i)
  165. {
  166. edges.row(i) = *e;
  167. }
  168. return _intersects;
  169. }
  170. #ifdef IGL_STATIC_LIBRARY
  171. // Explicit template instantiation
  172. template bool igl::fast_find_mesh_selfintersect<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<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  173. template bool igl::fast_find_mesh_selfintersect<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::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  174. #endif