polyvector_field_cut_mesh_with_singularities.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <igl/polyvector_field_cut_mesh_with_singularities.h>
  2. #include <igl/dijkstra.h>
  3. #include <igl/vertex_triangle_adjacency.h>
  4. #include <igl/adjacency_list.h>
  5. #include <igl/triangle_triangle_adjacency.h>
  6. #include <igl/is_border_vertex.h>
  7. #include <igl/cut_mesh_from_singularities.h>
  8. #include <set>
  9. template <typename DerivedV, typename DerivedF, typename VFType, typename VVType, typename DerivedTT, typename DerivedC, typename DerivedS>
  10. IGL_INLINE void igl::polyvector_field_cut_mesh_with_singularities(
  11. const Eigen::PlainObjectBase<DerivedV> &V,
  12. const Eigen::PlainObjectBase<DerivedF> &F,
  13. const std::vector<std::vector<VFType> >& VF,
  14. const std::vector<std::vector<VVType> >& VV,
  15. const Eigen::PlainObjectBase<DerivedTT>& TT,
  16. const Eigen::PlainObjectBase<DerivedTT>& TTi,
  17. const Eigen::PlainObjectBase<DerivedS> &singularities,
  18. Eigen::PlainObjectBase<DerivedC> &cuts)
  19. {
  20. //first, get a spanning tree for the mesh (no missmatch needed)
  21. igl::cut_mesh_from_singularities(V, F, Eigen::MatrixXd::Zero(F.rows(), 3).eval(), cuts);
  22. std::set<int> vertices_in_cut;
  23. for (int i =0; i< cuts.rows(); ++i)
  24. for (int j =0;j< cuts.cols(); ++j)
  25. if (cuts(i,j))
  26. vertices_in_cut.insert(F(i,j));
  27. //then, add all singularities one by one by using Dijkstra's algorithm
  28. for (int i = 0; i<singularities.rows(); ++i)
  29. {
  30. std::vector<int> path;
  31. Eigen::VectorXd min_distance;
  32. Eigen::VectorXi previous;
  33. int vertex_found = igl::dijkstra_compute_paths(singularities[i], vertices_in_cut, VV, min_distance, previous);
  34. if(vertex_found ==-1)
  35. // this means that there are no cuts
  36. path.push_back(singularities[i]);
  37. else
  38. igl::dijkstra_get_shortest_path_to(vertex_found, previous, path);
  39. vertices_in_cut.insert(path.begin(), path.end());
  40. //insert to cut
  41. for (int ii = 0; ii<path.size()-1; ++ii)
  42. {
  43. const int &v0 = path[ii];
  44. const int &v1 = path[ii+1];
  45. std::vector<int> vf0 = VF[v0];
  46. std::sort(vf0.begin(), vf0.end());
  47. std::vector<int> vf1 = VF[v1];
  48. std::sort(vf1.begin(), vf1.end());
  49. std::vector<int> common_face_v(std::max(vf0.size(),vf1.size()));
  50. std::vector<int>::iterator it;
  51. it=std::set_intersection (vf0.begin(), vf0.end(), vf1.begin(), vf1.end(), common_face_v.begin());
  52. common_face_v.resize(it-common_face_v.begin());
  53. assert(common_face_v.size() == 2);
  54. const int &fi = common_face_v[0];
  55. int j=-1;
  56. for (unsigned z=0; z<3; ++z)
  57. if (((F(fi,z) == v0) && (F(fi,(z+1)%3) == v1)) ||((F(fi,z) == v1) && (F(fi,(z+1)%3) == v0)))
  58. j=z;
  59. assert(j!=-1);
  60. cuts(fi,j) = 1;
  61. cuts(TT(fi,j), TTi(fi,j)) = 1;
  62. }
  63. }
  64. }
  65. //Wrapper of the above with only vertices and faces as mesh input
  66. template <typename DerivedV, typename DerivedF, typename DerivedC, typename DerivedS>
  67. IGL_INLINE void igl::polyvector_field_cut_mesh_with_singularities(
  68. const Eigen::PlainObjectBase<DerivedV> &V,
  69. const Eigen::PlainObjectBase<DerivedF> &F,
  70. const Eigen::PlainObjectBase<DerivedS> &singularities,
  71. Eigen::PlainObjectBase<DerivedC> &cuts)
  72. {
  73. std::vector<std::vector<int> > VF, VFi;
  74. igl::vertex_triangle_adjacency(V,F,VF,VFi);
  75. std::vector<std::vector<int> > VV;
  76. igl::adjacency_list(F, VV);
  77. Eigen::MatrixXi TT, TTi;
  78. igl::triangle_triangle_adjacency(F,TT,TTi);
  79. igl::polyvector_field_cut_mesh_with_singularities(V, F, VF, VV, TT, TTi, singularities, cuts);
  80. }
  81. #ifdef IGL_STATIC_LIBRARY
  82. // Explicit template specialization
  83. template void igl::polyvector_field_cut_mesh_with_singularities<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, int, int, 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&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  84. template void igl::polyvector_field_cut_mesh_with_singularities<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<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<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  85. #endif