adjacency_matrix.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // Loop over faces
  46. for(int i = 0;i<F.rows();i++)
  47. {
  48. // Loop over this face
  49. for(int j = 0;j<F.cols();j++)
  50. {
  51. // Get indices of edge: s --> d
  52. int s = F(i,j);
  53. int d = F(i,(j+1)%F.cols());
  54. dyn_A.coeffRef(s, d) = 1;
  55. dyn_A.coeffRef(d, s) = 1;
  56. }
  57. }
  58. A = Eigen::SparseMatrix<T>(dyn_A);
  59. }
  60. #endif