edges.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "edges.h"
  9. #include "adjacency_matrix.h"
  10. template <typename DerivedF, typename DerivedE>
  11. IGL_INLINE void igl::edges(
  12. const Eigen::PlainObjectBase<DerivedF> & F,
  13. Eigen::PlainObjectBase<DerivedE> & E)
  14. {
  15. // build adjacency matrix
  16. typedef typename DerivedF::Scalar Index;
  17. Eigen::SparseMatrix<Index> A;
  18. igl::adjacency_matrix(F,A);
  19. // Number of non zeros should be twice number of edges
  20. assert(A.nonZeros()%2 == 0);
  21. // Resize to fit edges
  22. E.resize(A.nonZeros()/2,2);
  23. int i = 0;
  24. // Iterate over outside
  25. for(int k=0; k<A.outerSize(); ++k)
  26. {
  27. // Iterate over inside
  28. for(typename Eigen::SparseMatrix<Index>::InnerIterator it (A,k); it; ++it)
  29. {
  30. // only add edge in one direction
  31. if(it.row()<it.col())
  32. {
  33. E(i,0) = it.row();
  34. E(i,1) = it.col();
  35. i++;
  36. }
  37. }
  38. }
  39. }