facet_components.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "facet_components.h"
  2. #include <igl/triangle_triangle_adjacency.h>
  3. #include <vector>
  4. #include <queue>
  5. template <typename DerivedF, typename DerivedC>
  6. IGL_INLINE void igl::facet_components(
  7. const Eigen::PlainObjectBase<DerivedF> & F,
  8. Eigen::PlainObjectBase<DerivedC> & C)
  9. {
  10. using namespace std;
  11. using namespace igl;
  12. typedef typename DerivedF::Index Index;
  13. vector<vector<vector<Index > > > TT;
  14. vector<vector<vector<Index > > > TTi;
  15. triangle_triangle_adjacency(F,TT,TTi);
  16. return facet_components(TT,C);
  17. }
  18. template <
  19. typename TTIndex,
  20. typename DerivedC,
  21. typename Derivedcounts>
  22. IGL_INLINE void igl::facet_components(
  23. const std::vector<std::vector<std::vector<TTIndex > > > & TT,
  24. Eigen::PlainObjectBase<DerivedC> & C,
  25. Eigen::PlainObjectBase<Derivedcounts> & counts)
  26. {
  27. using namespace std;
  28. using namespace igl;
  29. typedef TTIndex Index;
  30. const Index m = TT.size();
  31. C.resize(m,1);
  32. vector<bool> seen(m,false);
  33. Index id = 0;
  34. vector<Index> vcounts;
  35. for(Index g = 0;g<m;g++)
  36. {
  37. if(seen[g])
  38. {
  39. continue;
  40. }
  41. vcounts.push_back(0);
  42. queue<Index> Q;
  43. Q.push(g);
  44. while(!Q.empty())
  45. {
  46. const Index f = Q.front();
  47. Q.pop();
  48. if(seen[f])
  49. {
  50. continue;
  51. }
  52. seen[f] = true;
  53. vcounts[id]++;
  54. C(f,0) = id;
  55. // Face f's neighbor lists opposite opposite each corner
  56. for(const auto & c : TT[f])
  57. {
  58. // Each neighbor
  59. for(const auto & n : c)
  60. {
  61. if(!seen[n])
  62. {
  63. Q.push(n);
  64. }
  65. }
  66. }
  67. }
  68. id++;
  69. }
  70. assert(id == vcounts.size());
  71. const size_t ncc = vcounts.size();
  72. assert(C.maxCoeff()+1 == ncc);
  73. counts.resize(ncc,1);
  74. for(size_t i = 0;i<ncc;i++)
  75. {
  76. counts(i) = vcounts[i];
  77. }
  78. }
  79. #ifdef IGL_STATIC_LIBRARY
  80. // Explicit template specialization
  81. template void igl::facet_components<long, Eigen::Matrix<long, -1, 1, 0, -1, 1>, Eigen::Matrix<long, -1, 1, 0, -1, 1> >(std::vector<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > >, std::allocator<std::vector<std::vector<long, std::allocator<long> >, std::allocator<std::vector<long, std::allocator<long> > > > > > const&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&);
  82. #endif