edges.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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. igl::edges(A,E);
  21. }
  22. template <typename DerivedI, typename DerivedC, typename DerivedE>
  23. IGL_INLINE void igl::edges(
  24. const Eigen::MatrixBase<DerivedI> & I,
  25. const Eigen::MatrixBase<DerivedC> & C,
  26. Eigen::PlainObjectBase<DerivedE> & E)
  27. {
  28. typedef typename DerivedE::Scalar Index;
  29. Eigen::SparseMatrix<Index> A;
  30. igl::adjacency_matrix(I,C,A);
  31. igl::edges(A,E);
  32. }
  33. template <typename T, typename DerivedE>
  34. IGL_INLINE void igl::edges(
  35. const Eigen::SparseMatrix<T> & A,
  36. Eigen::PlainObjectBase<DerivedE> & E)
  37. {
  38. // Number of non zeros should be twice number of edges
  39. assert(A.nonZeros()%2 == 0);
  40. // Resize to fit edges
  41. E.resize(A.nonZeros()/2,2);
  42. int i = 0;
  43. // Iterate over outside
  44. for(int k=0; k<A.outerSize(); ++k)
  45. {
  46. // Iterate over inside
  47. for(typename Eigen::SparseMatrix<T>::InnerIterator it (A,k); it; ++it)
  48. {
  49. // only add edge in one direction
  50. if(it.row()<it.col())
  51. {
  52. E(i,0) = it.row();
  53. E(i,1) = it.col();
  54. i++;
  55. }
  56. }
  57. }
  58. assert(i == E.rows() && "A should be symmetric");
  59. }
  60. #ifdef IGL_STATIC_LIBRARY
  61. // Explicit template instantiation
  62. // generated by autoexplicit.sh
  63. template void igl::edges<Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 2, 1, -1, 2>>(Eigen::MatrixBase<Eigen::Matrix<int, -1, 3, 1, -1, 3>> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 1, -1, 2>>&);
  64. // generated by autoexplicit.sh
  65. template void igl::edges<Eigen::Matrix<int, -1, 1, 0, -1, 1>, 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::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  66. 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> >&);
  67. 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> >&);
  68. 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> >&);
  69. #endif