adjacency_list.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 list 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 vector<vector<T> > containing at row i the adjacent vertices of vertex i
  15. //
  16. // Example:
  17. // // Mesh in (V,F)
  18. // vector<vector<double> > A;
  19. // adjacency_list(F,A);
  20. //
  21. // See also: edges, cotmatrix, diag
  22. template <typename T>
  23. inline void adjacency_list(
  24. const Eigen::MatrixXi & F,
  25. std::vector<std::vector<T> >& A
  26. );
  27. }
  28. // Implementation
  29. #include "verbose.h"
  30. template <typename T>
  31. inline void igl::adjacency_list(
  32. const Eigen::MatrixXi & F,
  33. std::vector<std::vector<T> >& A)
  34. {
  35. A.clear();
  36. A.resize(F.maxCoeff()+1);
  37. // Loop over faces
  38. for(int i = 0;i<F.rows();i++)
  39. {
  40. // Loop over this face
  41. for(int j = 0;j<F.cols();j++)
  42. {
  43. // Get indices of edge: s --> d
  44. int s = F(i,j);
  45. int d = F(i,(j+1)%F.cols());
  46. A[s].push_back(d);
  47. A[d].push_back(s);
  48. }
  49. }
  50. // Remove duplicates
  51. for(int i=0; i<A.size();++i)
  52. {
  53. std::sort(A[i].begin(), A[i].end());
  54. A[i].erase(std::unique(A[i].begin(), A[i].end()), A[i].end());
  55. }
  56. }
  57. #endif