vertex_components.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "vertex_components.h"
  9. #include "adjacency_matrix.h"
  10. #include <queue>
  11. #include <vector>
  12. template <typename DerivedA, typename DerivedC, typename Derivedcounts>
  13. IGL_INLINE void igl::vertex_components(
  14. const Eigen::SparseCompressedBase<DerivedA> & A,
  15. Eigen::PlainObjectBase<DerivedC> & C,
  16. Eigen::PlainObjectBase<Derivedcounts> & counts)
  17. {
  18. assert(A.rows() == A.cols() && "A should be square.");
  19. const size_t n = A.rows();
  20. Eigen::Array<bool ,Eigen::Dynamic,1> seen = Eigen::Array<bool ,Eigen::Dynamic,1>::Zero(n,1);
  21. C.resize(n,1);
  22. typename DerivedC::Scalar id = 0;
  23. std::vector<typename Derivedcounts::Scalar> vcounts;
  24. // breadth first search
  25. for(int k=0; k<A.outerSize(); ++k)
  26. {
  27. if(seen(k))
  28. {
  29. continue;
  30. }
  31. std::queue<int> Q;
  32. Q.push(k);
  33. vcounts.push_back(0);
  34. while(!Q.empty())
  35. {
  36. const int f = Q.front();
  37. Q.pop();
  38. if(seen(f))
  39. {
  40. continue;
  41. }
  42. seen(f) = true;
  43. C(f,0) = id;
  44. vcounts[id]++;
  45. // Iterate over inside
  46. for(typename DerivedA::InnerIterator it (A,f); it; ++it)
  47. {
  48. const int g = it.index();
  49. if(!seen(g) && it.value())
  50. {
  51. Q.push(g);
  52. }
  53. }
  54. }
  55. id++;
  56. }
  57. assert((size_t) id == vcounts.size());
  58. const size_t ncc = vcounts.size();
  59. assert((size_t)C.maxCoeff()+1 == ncc);
  60. counts.resize(ncc,1);
  61. for(size_t i = 0;i<ncc;i++)
  62. {
  63. counts(i) = vcounts[i];
  64. }
  65. }
  66. template <typename DerivedA, typename DerivedC>
  67. IGL_INLINE void igl::vertex_components(
  68. const Eigen::SparseCompressedBase<DerivedA> & A,
  69. Eigen::PlainObjectBase<DerivedC> & C)
  70. {
  71. Eigen::VectorXi counts;
  72. return vertex_components(A,C,counts);
  73. }
  74. template <typename DerivedF, typename DerivedC>
  75. IGL_INLINE void igl::vertex_components(
  76. const Eigen::MatrixBase<DerivedF> & F,
  77. Eigen::PlainObjectBase<DerivedC> & C)
  78. {
  79. Eigen::SparseMatrix<typename DerivedC::Scalar> A;
  80. adjacency_matrix(F,A);
  81. return vertex_components(A,C);
  82. }
  83. #ifdef IGL_STATIC_LIBRARY
  84. // Explicit template instantiation
  85. // generated by autoexplicit.sh
  86. template void igl::vertex_components<Eigen::SparseMatrix<bool, 0, int>, Eigen::Array<int, -1, 1, 0, -1, 1> >(Eigen::SparseCompressedBase<Eigen::SparseMatrix<bool, 0, int>> const&, Eigen::PlainObjectBase<Eigen::Array<int, -1, 1, 0, -1, 1> >&);
  87. template void igl::vertex_components<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  88. template void igl::vertex_components<Eigen::SparseMatrix<int, 0, int>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::SparseCompressedBase<Eigen::SparseMatrix<int, 0, int>> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  89. template void igl::vertex_components<Eigen::SparseMatrix<int, 0, int>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::SparseCompressedBase<Eigen::SparseMatrix<int, 0, int>> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  90. template void igl::vertex_components<Eigen::SparseMatrix<double, 0, int>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::SparseCompressedBase<Eigen::SparseMatrix<double, 0, int>> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  91. template void igl::vertex_components<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  92. #endif