components.cpp 1.5 KB

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