adjacency_matrix.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "adjacency_matrix.h"
  9. #include "verbose.h"
  10. #include <vector>
  11. template <typename DerivedF, typename T>
  12. IGL_INLINE void igl::adjacency_matrix(
  13. const Eigen::PlainObjectBase<DerivedF> & F,
  14. Eigen::SparseMatrix<T>& A)
  15. {
  16. using namespace std;
  17. using namespace Eigen;
  18. typedef typename DerivedF::Scalar Index;
  19. typedef Triplet<T> IJV;
  20. vector<IJV > ijv;
  21. ijv.reserve(F.size()*2);
  22. // Loop over faces
  23. for(int i = 0;i<F.rows();i++)
  24. {
  25. // Loop over this face
  26. for(int j = 0;j<F.cols();j++)
  27. {
  28. // Get indices of edge: s --> d
  29. Index s = F(i,j);
  30. Index d = F(i,(j+1)%F.cols());
  31. ijv.push_back(IJV(s,d,1));
  32. ijv.push_back(IJV(d,s,1));
  33. }
  34. }
  35. const Index n = F.maxCoeff()+1;
  36. A.resize(n,n);
  37. switch(F.cols())
  38. {
  39. case 3:
  40. A.reserve(6*(F.maxCoeff()+1));
  41. break;
  42. case 4:
  43. A.reserve(26*(F.maxCoeff()+1));
  44. break;
  45. }
  46. A.setFromTriplets(ijv.begin(),ijv.end());
  47. // Force all non-zeros to be one
  48. // Iterate over outside
  49. for(int k=0; k<A.outerSize(); ++k)
  50. {
  51. // Iterate over inside
  52. for(typename Eigen::SparseMatrix<T>::InnerIterator it (A,k); it; ++it)
  53. {
  54. assert(it.value() != 0);
  55. A.coeffRef(it.row(),it.col()) = 1;
  56. }
  57. }
  58. }
  59. #ifdef IGL_STATIC_LIBRARY
  60. // Explicit template specialization
  61. template void igl::adjacency_matrix<Eigen::Matrix<int, -1, -1, 0, -1, -1>, double>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<double, 0, int>&);
  62. template void igl::adjacency_matrix<Eigen::Matrix<int, -1, -1, 0, -1, -1>, int>(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::SparseMatrix<int, 0, int>&);
  63. #endif