components.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  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 "components.h"
  9. #include "adjacency_matrix.h"
  10. #include <queue>
  11. #include <vector>
  12. template <typename AScalar, typename DerivedC, typename Derivedcounts>
  13. IGL_INLINE void igl::components(
  14. const Eigen::SparseMatrix<AScalar> & A,
  15. Eigen::PlainObjectBase<DerivedC> & C,
  16. Eigen::PlainObjectBase<Derivedcounts> & counts)
  17. {
  18. using namespace Eigen;
  19. using namespace std;
  20. assert(A.rows() == A.cols() && "A should be square.");
  21. const size_t n = A.rows();
  22. Array<bool,Dynamic,1> seen = Array<bool,Dynamic,1>::Zero(n,1);
  23. C.resize(n,1);
  24. typename DerivedC::Scalar id = 0;
  25. vector<typename Derivedcounts::Scalar> vcounts;
  26. for(int k=0; k<A.outerSize(); ++k)
  27. {
  28. if(seen(k))
  29. {
  30. continue;
  31. }
  32. queue<int> Q;
  33. Q.push(k);
  34. vcounts.push_back(0);
  35. while(!Q.empty())
  36. {
  37. const int f = Q.front();
  38. Q.pop();
  39. if(seen(f))
  40. {
  41. continue;
  42. }
  43. seen(f) = true;
  44. C(f,0) = id;
  45. vcounts[id]++;
  46. // Iterate over inside
  47. for(typename SparseMatrix<AScalar>::InnerIterator it (A,f); it; ++it)
  48. {
  49. const int g = it.index();
  50. if(!seen(g))
  51. {
  52. Q.push(g);
  53. }
  54. }
  55. }
  56. id++;
  57. }
  58. assert((size_t) id == vcounts.size());
  59. const size_t ncc = vcounts.size();
  60. assert((size_t)C.maxCoeff()+1 == ncc);
  61. counts.resize(ncc,1);
  62. for(size_t i = 0;i<ncc;i++)
  63. {
  64. counts(i) = vcounts[i];
  65. }
  66. }
  67. template <typename AScalar, typename DerivedC>
  68. IGL_INLINE void igl::components(
  69. const Eigen::SparseMatrix<AScalar> & A,
  70. Eigen::PlainObjectBase<DerivedC> & C)
  71. {
  72. Eigen::VectorXi counts;
  73. return components(A,C,counts);
  74. }
  75. template <typename DerivedF, typename DerivedC>
  76. IGL_INLINE void igl::components(
  77. const Eigen::PlainObjectBase<DerivedF> & F,
  78. Eigen::PlainObjectBase<DerivedC> & C)
  79. {
  80. Eigen::SparseMatrix<typename DerivedC::Scalar> A;
  81. adjacency_matrix(F,A);
  82. return components(A,C);
  83. }
  84. #ifdef IGL_STATIC_LIBRARY
  85. // Explicit template specialization
  86. template void igl::components<Eigen::Matrix<int, -1, -1, 0, -1, -1>, 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> >&);
  87. template void igl::components<int, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<int, 0, int> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  88. template void igl::components<int, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::SparseMatrix<int, 0, int> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  89. #endif