components.cpp 1.8 KB

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