adjacency_matrix.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  11. #include <iostream>
  12. #include <unsupported/Eigen/SparseExtra>
  13. template <typename T>
  14. IGL_INLINE void igl::adjacency_matrix(
  15. const Eigen::MatrixXi & F,
  16. Eigen::SparseMatrix<T>& A)
  17. {
  18. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor>
  19. dyn_A(F.maxCoeff()+1, F.maxCoeff()+1);
  20. switch(F.cols())
  21. {
  22. case 3:
  23. dyn_A.reserve(6*(F.maxCoeff()+1));
  24. break;
  25. case 4:
  26. dyn_A.reserve(26*(F.maxCoeff()+1));
  27. break;
  28. }
  29. // Loop over faces
  30. for(int i = 0;i<F.rows();i++)
  31. {
  32. // Loop over this face
  33. for(int j = 0;j<F.cols();j++)
  34. {
  35. // Get indices of edge: s --> d
  36. int s = F(i,j);
  37. int d = F(i,(j+1)%F.cols());
  38. dyn_A.coeffRef(s, d) = 1;
  39. dyn_A.coeffRef(d, s) = 1;
  40. }
  41. }
  42. A = Eigen::SparseMatrix<T>(dyn_A);
  43. }
  44. #ifndef IGL_HEADER_ONLY
  45. // Explicit template specialization
  46. template void igl::adjacency_matrix<int>(Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::SparseMatrix<int, 0, int>&);
  47. #endif