adjacency_matrix.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef IGL_ADJACENCY_MATRIX_H
  2. #define IGL_ADJACENCY_MATRIX_H
  3. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  4. #include <Eigen/Dense>
  5. #include <Eigen/Sparse>
  6. namespace igl
  7. {
  8. // Constructs the graph adjacency matrix of a given mesh (V,F)
  9. // Templates:
  10. // T should be a eigen sparse matrix primitive type like int or double
  11. // Inputs:
  12. // F #F by dim list of mesh faces (must be triangles)
  13. // Outputs:
  14. // A max(F) by max(F) cotangent matrix, each row i corresponding to V(i,:)
  15. //
  16. // Example:
  17. // // Mesh in (V,F)
  18. // Eigen::SparseMatrix<double> A;
  19. // adjacency_matrix(F,A);
  20. // // sum each row
  21. // SparseVector<double> Asum;
  22. // sum(A,1,Asum);
  23. // // Convert row sums into diagonal of sparse matrix
  24. // SparseMatrix<double> Adiag;
  25. // diag(Asum,Adiag);
  26. // // Build uniform laplacian
  27. // SparseMatrix<double> U;
  28. // U = A-Adiag;
  29. //
  30. // See also: edges, cotmatrix, diag
  31. template <typename T>
  32. inline void adjacency_matrix(
  33. const Eigen::MatrixXi & F,
  34. Eigen::SparseMatrix<T>& A);
  35. }
  36. // Implementation
  37. #include "verbose.h"
  38. template <typename T>
  39. inline void igl::adjacency_matrix(
  40. const Eigen::MatrixXi & F,
  41. Eigen::SparseMatrix<T>& A)
  42. {
  43. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor>
  44. dyn_A(F.maxCoeff()+1, F.maxCoeff()+1);
  45. dyn_A.reserve(6*(F.maxCoeff()+1));
  46. // Loop over faces
  47. for(int i = 0;i<F.rows();i++)
  48. {
  49. // Loop over this face
  50. for(int j = 0;j<F.cols();j++)
  51. {
  52. // Get indices of edge: s --> d
  53. int s = F(i,j);
  54. int d = F(i,(j+1)%F.cols());
  55. dyn_A.coeffRef(s, d) = 1;
  56. dyn_A.coeffRef(d, s) = 1;
  57. }
  58. }
  59. A = Eigen::SparseMatrix<T>(dyn_A);
  60. }
  61. #endif