vertex_components.cpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "vertex_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::vertex_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. // breadth first search
  27. for(int k=0; k<A.outerSize(); ++k)
  28. {
  29. if(seen(k))
  30. {
  31. continue;
  32. }
  33. queue<int> Q;
  34. Q.push(k);
  35. vcounts.push_back(0);
  36. while(!Q.empty())
  37. {
  38. const int f = Q.front();
  39. Q.pop();
  40. if(seen(f))
  41. {
  42. continue;
  43. }
  44. seen(f) = true;
  45. C(f,0) = id;
  46. vcounts[id]++;
  47. // Iterate over inside
  48. for(typename SparseMatrix<AScalar>::InnerIterator it (A,f); it; ++it)
  49. {
  50. const int g = it.index();
  51. if(!seen(g) && it.value())
  52. {
  53. Q.push(g);
  54. }
  55. }
  56. }
  57. id++;
  58. }
  59. assert((size_t) id == vcounts.size());
  60. const size_t ncc = vcounts.size();
  61. assert((size_t)C.maxCoeff()+1 == ncc);
  62. counts.resize(ncc,1);
  63. for(size_t i = 0;i<ncc;i++)
  64. {
  65. counts(i) = vcounts[i];
  66. }
  67. }
  68. template <typename AScalar, typename DerivedC>
  69. IGL_INLINE void igl::vertex_components(
  70. const Eigen::SparseMatrix<AScalar> & A,
  71. Eigen::PlainObjectBase<DerivedC> & C)
  72. {
  73. Eigen::VectorXi counts;
  74. return vertex_components(A,C,counts);
  75. }
  76. template <typename DerivedF, typename DerivedC>
  77. IGL_INLINE void igl::vertex_components(
  78. const Eigen::MatrixBase<DerivedF> & F,
  79. Eigen::PlainObjectBase<DerivedC> & C)
  80. {
  81. Eigen::SparseMatrix<typename DerivedC::Scalar> A;
  82. adjacency_matrix(F,A);
  83. return vertex_components(A,C);
  84. }
  85. #ifdef IGL_STATIC_LIBRARY
  86. // Explicit template instantiation
  87. // generated by autoexplicit.sh
  88. template void igl::vertex_components<bool, Eigen::Array<int, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<bool, 0, int> const&, Eigen::PlainObjectBase<Eigen::Array<int, -1, 1, 0, -1, 1> >&);
  89. 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> >&);
  90. template void igl::vertex_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> >&);
  91. template void igl::vertex_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> >&);
  92. template void igl::vertex_components<double, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(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> >&);
  93. 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> >&);
  94. #endif