// This file is part of libigl, a simple c++ geometry processing library. // // Copyright (C) 2013 Alec Jacobson // // This Source Code Form is subject to the terms of the Mozilla Public License // v. 2.0. If a copy of the MPL was not distributed with this file, You can // obtain one at http://mozilla.org/MPL/2.0/. #include "vertex_components.h" #include "adjacency_matrix.h" #include #include template IGL_INLINE void igl::vertex_components( const Eigen::SparseMatrix & A, Eigen::PlainObjectBase & C, Eigen::PlainObjectBase & counts) { using namespace Eigen; using namespace std; assert(A.rows() == A.cols() && "A should be square."); const size_t n = A.rows(); Array seen = Array::Zero(n,1); C.resize(n,1); typename DerivedC::Scalar id = 0; vector vcounts; // breadth first search for(int k=0; k Q; Q.push(k); vcounts.push_back(0); while(!Q.empty()) { const int f = Q.front(); Q.pop(); if(seen(f)) { continue; } seen(f) = true; C(f,0) = id; vcounts[id]++; // Iterate over inside for(typename SparseMatrix::InnerIterator it (A,f); it; ++it) { const int g = it.index(); if(!seen(g) && it.value()) { Q.push(g); } } } id++; } assert((size_t) id == vcounts.size()); const size_t ncc = vcounts.size(); assert((size_t)C.maxCoeff()+1 == ncc); counts.resize(ncc,1); for(size_t i = 0;i IGL_INLINE void igl::vertex_components( const Eigen::SparseMatrix & A, Eigen::PlainObjectBase & C) { Eigen::VectorXi counts; return vertex_components(A,C,counts); } template IGL_INLINE void igl::vertex_components( const Eigen::MatrixBase & F, Eigen::PlainObjectBase & C) { Eigen::SparseMatrix A; adjacency_matrix(F,A); return vertex_components(A,C); } #ifdef IGL_STATIC_LIBRARY // Explicit template instantiation // generated by autoexplicit.sh template void igl::vertex_components >(Eigen::SparseMatrix const&, Eigen::PlainObjectBase >&); template void igl::vertex_components, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&); template void igl::vertex_components >(Eigen::SparseMatrix const&, Eigen::PlainObjectBase >&); template void igl::vertex_components >(Eigen::SparseMatrix const&, Eigen::PlainObjectBase >&); template void igl::vertex_components, Eigen::Matrix >(Eigen::SparseMatrix const&, Eigen::PlainObjectBase >&, Eigen::PlainObjectBase >&); template void igl::vertex_components, Eigen::Matrix >(Eigen::MatrixBase > const&, Eigen::PlainObjectBase >&); #endif