facet_components.cpp 2.4 KB

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