fast_find_intersections.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_intersections.h"
  9. #include "AABB.h"
  10. #include "tri_tri_intersect.h"
  11. template <
  12. typename DerivedV1,
  13. typename DerivedF1,
  14. typename DerivedV2,
  15. typename DerivedF2,
  16. typename DerivedI,
  17. typename DerivedE>
  18. IGL_INLINE void igl::fast_find_intersections(
  19. const Eigen::MatrixBase<DerivedV1>& V1,
  20. const Eigen::MatrixBase<DerivedF1>& F1,
  21. const Eigen::MatrixBase<DerivedV2>& V2,
  22. const Eigen::MatrixBase<DerivedF2>& F2,
  23. Eigen::PlainObjectBase<DerivedI>& intersect_pairs,
  24. Eigen::PlainObjectBase<DerivedE>& edges )
  25. {
  26. using AABBTree=igl::AABB<DerivedV1,3>;
  27. AABBTree tree;
  28. tree.init(V1,F1);
  29. fast_find_intersections(tree, V1, F1, V2, F2, intersect_pairs, edges);
  30. }
  31. template <
  32. typename DerivedV1,
  33. typename DerivedF1,
  34. typename DerivedV2,
  35. typename DerivedF2,
  36. typename DerivedI,
  37. typename DerivedE>
  38. IGL_INLINE void igl::fast_find_intersections(
  39. const AABB<DerivedV1,3> & tree,
  40. const Eigen::MatrixBase<DerivedV1>& V1,
  41. const Eigen::MatrixBase<DerivedF1>& F1,
  42. const Eigen::MatrixBase<DerivedV2>& V2,
  43. const Eigen::MatrixBase<DerivedF2>& F2,
  44. Eigen::PlainObjectBase<DerivedI>& intersect_pairs,
  45. Eigen::PlainObjectBase<DerivedE>& edges )
  46. {
  47. using AABBTree=igl::AABB<DerivedV1,3>;
  48. using Scalar=typename DerivedV1::Scalar;
  49. using BBOX=Eigen::AlignedBox<Scalar,3>;
  50. using VERTEX=Eigen::Matrix<typename DerivedE::Scalar,1,3,Eigen::RowMajor>;
  51. std::vector<VERTEX> _edges;
  52. std::vector<int> _intersect_pairs;
  53. for(int i=0; i<F2.rows(); ++i)
  54. {
  55. BBOX tri_box;
  56. for(int j=0;j<3;++j)
  57. tri_box.extend( V2.row( F2(i,j) ).transpose() );
  58. // find leaf nodes containing intersecting tri_box
  59. // need to specify exact type instead of auto to allow recursion
  60. std::function<void(const AABBTree &,int)> check_intersect =
  61. [&](const AABBTree &t,int d) -> void
  62. {
  63. if(t.m_primitive != -1) //check for the actual intersection //t.is_leaf()
  64. {
  65. bool coplanar=false;
  66. VERTEX edge1,edge2;
  67. if(igl::tri_tri_intersection_test_3d(
  68. V2.row(F2(i,0)), V2.row(F2(i,1)), V2.row(F2(i,2)),
  69. V1.row(F1(t.m_primitive,0)),V1.row(F1(t.m_primitive,1)),V1.row(F1(t.m_primitive,2)),
  70. coplanar,
  71. edge1,edge2))
  72. {
  73. if(!coplanar)
  74. {
  75. _intersect_pairs.push_back(t.m_primitive);
  76. _intersect_pairs.push_back(i);
  77. _edges.push_back(edge1);
  78. _edges.push_back(edge2);
  79. }
  80. }
  81. } else {
  82. if(t.m_box.intersects( tri_box ))
  83. { // need to check all branches
  84. check_intersect(*t.m_left, d+1);
  85. check_intersect(*t.m_right,d+1);
  86. }
  87. }
  88. };
  89. // run actual search
  90. check_intersect(tree, 0);
  91. }
  92. edges.resize(_edges.size(), 3);
  93. for(int i=0; i!=_edges.size(); ++i)
  94. {
  95. edges.row(i) = _edges[i];
  96. }
  97. intersect_pairs.resize(_intersect_pairs.size()/2,2);
  98. for(int i=0; i!=_intersect_pairs.size(); ++i)
  99. {
  100. intersect_pairs(i/2, i%2) = _intersect_pairs[i];
  101. }
  102. }
  103. #ifdef IGL_STATIC_LIBRARY
  104. // Explicit template instantiation
  105. template void igl::fast_find_intersections<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<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::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> >&);
  106. template void igl::fast_find_intersections<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<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::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> >&);
  107. #endif