extract_manifold_patches.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "extract_manifold_patches.h"
  2. #include <cassert>
  3. #include <limits>
  4. #include <queue>
  5. template<
  6. typename DerivedF,
  7. typename DerivedEMAP,
  8. typename uE2EType,
  9. typename DerivedP>
  10. IGL_INLINE size_t igl::extract_manifold_patches(
  11. const Eigen::PlainObjectBase<DerivedF>& F,
  12. const Eigen::PlainObjectBase<DerivedEMAP>& EMAP,
  13. const std::vector<std::vector<uE2EType> >& uE2E,
  14. Eigen::PlainObjectBase<DerivedP>& P) {
  15. assert(F.cols() == 3);
  16. const size_t num_faces = F.rows();
  17. auto edge_index_to_face_index = [&](size_t ei) { return ei % num_faces; };
  18. auto face_and_corner_index_to_edge_index = [&](size_t fi, size_t ci) {
  19. return ci*num_faces + fi;
  20. };
  21. auto is_manifold_edge = [&](size_t fi, size_t ci) {
  22. const size_t ei = face_and_corner_index_to_edge_index(fi, ci);
  23. return uE2E[EMAP(ei, 0)].size() == 2;
  24. };
  25. auto get_adj_face_index = [&](size_t fi, size_t ci) -> size_t {
  26. const size_t ei = face_and_corner_index_to_edge_index(fi, ci);
  27. const auto& adj_faces = uE2E[EMAP(ei, 0)];
  28. assert(adj_faces.size() == 2);
  29. if (adj_faces[0] == ei) {
  30. return edge_index_to_face_index(adj_faces[1]);
  31. } else {
  32. assert(adj_faces[1] == ei);
  33. return edge_index_to_face_index(adj_faces[0]);
  34. }
  35. };
  36. typedef typename DerivedP::Scalar Scalar;
  37. const Scalar INVALID = std::numeric_limits<Scalar>::max();
  38. P.resize(num_faces,1);
  39. P.setConstant(INVALID);
  40. size_t num_patches = 0;
  41. for (size_t i=0; i<num_faces; i++) {
  42. if (P(i,0) != INVALID) continue;
  43. std::queue<size_t> Q;
  44. Q.push(i);
  45. P(i,0) = num_patches;
  46. while (!Q.empty()) {
  47. const size_t fid = Q.front();
  48. Q.pop();
  49. for (size_t j=0; j<3; j++) {
  50. if (is_manifold_edge(fid, j)) {
  51. const size_t adj_fid = get_adj_face_index(fid, j);
  52. if (P(adj_fid,0) == INVALID) {
  53. Q.push(adj_fid);
  54. P(adj_fid,0) = num_patches;
  55. }
  56. }
  57. }
  58. }
  59. num_patches++;
  60. }
  61. assert((P.array() != INVALID).all());
  62. return num_patches;
  63. }
  64. #ifdef IGL_STATIC_LIBRARY
  65. template unsigned long igl::extract_manifold_patches<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, unsigned long, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, std::vector<std::vector<unsigned long, std::allocator<unsigned long> >, std::allocator<std::vector<unsigned long, std::allocator<unsigned long> > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  66. #endif