facet_adjacency_matrix.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2020 Alec Jacobson <[email protected]>
  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 "facet_adjacency_matrix.h"
  9. #include "unique_edge_map.h"
  10. template <typename DerivedF, typename Atype>
  11. IGL_INLINE void igl::facet_adjacency_matrix(
  12. const Eigen::MatrixBase<DerivedF> & F, Eigen::SparseMatrix<Atype> & A)
  13. {
  14. typedef typename DerivedF::Scalar Index;
  15. const auto m = F.rows();
  16. Eigen::Matrix<Index,Eigen::Dynamic,1> EMAP,uEE,uEC;
  17. Eigen::Matrix<Index,Eigen::Dynamic,2> E,uE;
  18. igl::unique_edge_map(F,E,uE,EMAP,uEC,uEE);
  19. std::vector<Eigen::Triplet<Index> > AIJV;
  20. AIJV.reserve(2*uE.rows());
  21. const Eigen::Index nu = uE.rows();
  22. for(Eigen::Index ue = 0;ue<nu;ue++)
  23. {
  24. // number of faces incident on this unique edge
  25. const Eigen::Index mue = uEC(ue+1)-uEC(ue);
  26. // base offset in uEE
  27. const Eigen::Index uECue = uEC(ue);
  28. assert(uECue < uEE.rows());
  29. for(Eigen::Index i = 0;i<mue;i++)
  30. {
  31. const auto ii = uEE(uECue+i)%m;
  32. for(Eigen::Index j = 0;j<mue;j++)
  33. {
  34. const auto jj = uEE(uECue+j)%m;
  35. if(ii != jj){ AIJV.emplace_back(ii,jj,1);}
  36. }
  37. }
  38. }
  39. // Facet Adjacency matrix (with (arbitrary) >0
  40. A.resize(m,m);
  41. A.setFromTriplets(AIJV.begin(),AIJV.end());
  42. // Set all non-zerro values to 1
  43. for(Eigen::Index g = 0;g<A.outerSize();g++)
  44. {
  45. for(typename Eigen::SparseMatrix<Atype>::InnerIterator it (A,g); it; ++it)
  46. {
  47. if(it.value()) it.valueRef() = 1;
  48. }
  49. }
  50. }
  51. #ifdef IGL_STATIC_LIBRARY
  52. // Explicit template instantiation
  53. // generated by autoexplicit.sh
  54. template void igl::facet_adjacency_matrix<Eigen::Matrix<int, -1, -1, 0, -1, -1>, bool>(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<bool, 0, int>&);
  55. // generated by autoexplicit.sh
  56. template void igl::facet_adjacency_matrix<Eigen::Matrix<int, -1, -1, 0, -1, -1>, int>(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<int, 0, int>&);
  57. #endif