facet_components.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. typedef typename DerivedF::Index Index;
  12. vector<vector<vector<Index > > > TT;
  13. vector<vector<vector<Index > > > TTi;
  14. triangle_triangle_adjacency(F,TT,TTi);
  15. Eigen::VectorXi counts;
  16. return facet_components(TT,C,counts);
  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. typedef TTIndex Index;
  29. const Index m = TT.size();
  30. C.resize(m,1);
  31. vector<bool> seen(m,false);
  32. Index id = 0;
  33. vector<Index> vcounts;
  34. for(Index g = 0;g<m;g++)
  35. {
  36. if(seen[g])
  37. {
  38. continue;
  39. }
  40. vcounts.push_back(0);
  41. queue<Index> Q;
  42. Q.push(g);
  43. while(!Q.empty())
  44. {
  45. const Index f = Q.front();
  46. Q.pop();
  47. if(seen[f])
  48. {
  49. continue;
  50. }
  51. seen[f] = true;
  52. vcounts[id]++;
  53. C(f,0) = id;
  54. // Face f's neighbor lists opposite opposite each corner
  55. for(const auto & c : TT[f])
  56. {
  57. // Each neighbor
  58. for(const auto & n : c)
  59. {
  60. if(!seen[n])
  61. {
  62. Q.push(n);
  63. }
  64. }
  65. }
  66. }
  67. id++;
  68. }
  69. assert((size_t) id == vcounts.size());
  70. const size_t ncc = vcounts.size();
  71. assert((size_t)C.maxCoeff()+1 == ncc);
  72. counts.resize(ncc,1);
  73. for(size_t i = 0;i<ncc;i++)
  74. {
  75. counts(i) = vcounts[i];
  76. }
  77. }
  78. #ifdef IGL_STATIC_LIBRARY
  79. // Explicit template specialization
  80. 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> >&);
  81. #endif