edges.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #include <iostream>
  11. template <typename DerivedF, typename DerivedE>
  12. IGL_INLINE void igl::edges(
  13. const Eigen::MatrixBase<DerivedF> & F,
  14. Eigen::PlainObjectBase<DerivedE> & E)
  15. {
  16. // build adjacency matrix
  17. typedef typename DerivedF::Scalar Index;
  18. Eigen::SparseMatrix<Index> A;
  19. igl::adjacency_matrix(F,A);
  20. // Number of non zeros should be twice number of edges
  21. assert(A.nonZeros()%2 == 0);
  22. // Resize to fit edges
  23. E.resize(A.nonZeros()/2,2);
  24. int i = 0;
  25. // Iterate over outside
  26. for(int k=0; k<A.outerSize(); ++k)
  27. {
  28. // Iterate over inside
  29. for(typename Eigen::SparseMatrix<Index>::InnerIterator it (A,k); it; ++it)
  30. {
  31. // only add edge in one direction
  32. if(it.row()<it.col())
  33. {
  34. E(i,0) = it.row();
  35. E(i,1) = it.col();
  36. i++;
  37. }
  38. }
  39. }
  40. }
  41. #ifdef IGL_STATIC_LIBRARY
  42. // Explicit template instantiation
  43. template void igl::edges<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 2, 0, -1, 2> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&);
  44. template void igl::edges<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  45. template void igl::edges<Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 2, 0, -1, 2> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&);
  46. #endif