euler_characteristic.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 Michael Rabinovich <[email protected]@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 "euler_characteristic.h"
  9. #include "unique_edge_map.h"
  10. #include <cassert>
  11. template <typename DerivedF>
  12. IGL_INLINE int igl::euler_characteristic(
  13. const Eigen::MatrixBase<DerivedF> & F)
  14. {
  15. const int nf = F.rows();
  16. const int nv = F.maxCoeff()+1;
  17. Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,2> E;
  18. Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,1> EMAP;
  19. Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,2> uE;
  20. unique_edge_map(F,E,uE,EMAP);
  21. // The input is assumed manifold so boundary edges are those with exactly one
  22. // incident face
  23. Eigen::VectorXi count = Eigen::VectorXi::Zero(uE.rows(),1);
  24. for(int e = 0;e<EMAP.size();e++)
  25. {
  26. count(EMAP(e))++;
  27. }
  28. std::vector<Eigen::Triplet<int> > IJV;
  29. for(int u = 0;u<uE.rows();u++)
  30. {
  31. if(count(u) == 1)
  32. {
  33. IJV.emplace_back(uE(u,0),uE(u,1),1);
  34. IJV.emplace_back(uE(u,1),uE(u,0),1);
  35. }
  36. assert(count(u) == 2);
  37. }
  38. Eigen::SparseMatrix<int> A(nv,nv);
  39. A.setFromTriplets(IJV.begin(),IJV.end());
  40. Eigen::VectorXi _C,_K;
  41. // Number of boundary connected components
  42. const int nb = connected_components(A,_C,_K);
  43. std::cout<<"nb: "<<nb<<std::endl;
  44. const int ne = E.rows();
  45. return nv - ne + nf + nb;
  46. }
  47. #ifdef IGL_STATIC_LIBRARY
  48. // Explicit template instantiation
  49. template int igl::euler_characteristic<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&);
  50. #endif