components.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "components.h"
  2. #include <igl/adjacency_matrix.h>
  3. #include <boost/graph/adjacency_matrix.hpp>
  4. #include <boost/graph/connected_components.hpp>
  5. #include <iostream>
  6. #include <vector>
  7. #include <cassert>
  8. template <typename AScalar, typename DerivedC>
  9. IGL_INLINE void igl::components(
  10. const Eigen::SparseMatrix<AScalar> & A,
  11. Eigen::PlainObjectBase<DerivedC> & C)
  12. {
  13. assert(A.rows() == A.cols());
  14. using namespace Eigen;
  15. boost::adjacency_matrix<boost::undirectedS> bA(A.rows());
  16. for(int j=0; j<A.outerSize();j++)
  17. {
  18. // Iterate over inside
  19. for(typename SparseMatrix<AScalar>::InnerIterator it (A,j); it; ++it)
  20. {
  21. if(0 != it.value())
  22. {
  23. boost::add_edge(it.row(),it.col(),bA);
  24. }
  25. }
  26. }
  27. C.resize(A.rows(),1);
  28. boost::connected_components(bA,C.data());
  29. }
  30. template <typename DerivedF, typename DerivedC>
  31. IGL_INLINE void igl::components(
  32. const Eigen::PlainObjectBase<DerivedF> & F,
  33. Eigen::PlainObjectBase<DerivedC> & C)
  34. {
  35. Eigen::SparseMatrix<typename DerivedC::Scalar> A;
  36. igl::adjacency_matrix(F,A);
  37. return components(A,C);
  38. }
  39. #ifndef IGL_HEADER_ONLY
  40. // Explicit template specialization
  41. 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> >&);
  42. #endif