edges.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #ifndef IGL_EDGES_H
  2. #define IGL_EDGES_H
  3. #include <Eigen/Dense>
  4. namespace igl
  5. {
  6. // Constructs a list of unique edges represented in a given mesh (V,F)
  7. // Templates:
  8. // T should be a eigen sparse matrix primitive type like int or double
  9. // Inputs:
  10. // F #F by 3 list of mesh faces (must be triangles)
  11. // or
  12. // T #T x 4 matrix of indices of tet corners
  13. // Outputs:
  14. // E #E by 2 list of edges in no particular order
  15. //
  16. // See also: adjacency_matrix
  17. inline void edges(
  18. const Eigen::MatrixXi& F,
  19. Eigen::MatrixXi& E);
  20. }
  21. // Implementation
  22. #include <map>
  23. #include "adjacency_matrix.h"
  24. inline void igl::edges(
  25. const Eigen::MatrixXi& F,
  26. Eigen::MatrixXi& E)
  27. {
  28. // build adjacency matrix
  29. Eigen::SparseMatrix<int> A;
  30. igl::adjacency_matrix(F,A);
  31. // Number of non zeros should be twice number of edges
  32. assert(A.nonZeros()%2 == 0);
  33. // Resize to fit edges
  34. E.resize(A.nonZeros()/2,2);
  35. int i = 0;
  36. // Iterate over outside
  37. for(int k=0; k<A.outerSize(); ++k)
  38. {
  39. // Iterate over inside
  40. for(Eigen::SparseMatrix<int>::InnerIterator it (A,k); it; ++it)
  41. {
  42. // only add edge in one direction
  43. if(it.row()<it.col())
  44. {
  45. E(i,0) = it.row();
  46. E(i,1) = it.col();
  47. i++;
  48. }
  49. }
  50. }
  51. }
  52. #endif