edges.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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( const Eigen::MatrixXi& F, Eigen::MatrixXi& E);
  18. }
  19. // Implementation
  20. #include <map>
  21. #include <adjacency_matrix.h>
  22. inline void igl::edges( const Eigen::MatrixXi& F, Eigen::MatrixXi& E)
  23. {
  24. // build adjacency matrix
  25. Eigen::SparseMatrix<int> A;
  26. igl::adjacency_matrix(F,A);
  27. // Number of non zeros should be twice number of edges
  28. assert(A.nonZeros()%2 == 0);
  29. // Resize to fit edges
  30. E.resize(A.nonZeros()/2,2);
  31. int i = 0;
  32. // Iterate over outside
  33. for(int k=0; k<A.outerSize(); ++k)
  34. {
  35. // Iterate over inside
  36. for(Eigen::SparseMatrix<int>::InnerIterator it (A,k); it; ++it)
  37. {
  38. // only add edge in one direction
  39. if(it.row()<it.col())
  40. {
  41. E(i,0) = it.row();
  42. E(i,1) = it.col();
  43. i++;
  44. }
  45. }
  46. }
  47. }
  48. #endif