find.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "find.h"
  9. #include "verbose.h"
  10. template <
  11. typename T,
  12. typename DerivedI,
  13. typename DerivedJ,
  14. typename DerivedV>
  15. IGL_INLINE void igl::find(
  16. const Eigen::SparseMatrix<T>& X,
  17. Eigen::MatrixBase<DerivedI> & I,
  18. Eigen::MatrixBase<DerivedJ> & J,
  19. Eigen::MatrixBase<DerivedV> & V)
  20. {
  21. // Resize outputs to fit nonzeros
  22. I.derived().resize(X.nonZeros(),1);
  23. J.derived().resize(X.nonZeros(),1);
  24. V.derived().resize(X.nonZeros(),1);
  25. int i = 0;
  26. // Iterate over outside
  27. for(int k=0; k<X.outerSize(); ++k)
  28. {
  29. // Iterate over inside
  30. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  31. {
  32. V(i) = it.value();
  33. I(i) = it.row();
  34. J(i) = it.col();
  35. i++;
  36. }
  37. }
  38. }
  39. template <typename T>
  40. IGL_INLINE void igl::find(
  41. const Eigen::SparseVector<T>& X,
  42. Eigen::Matrix<int,Eigen::Dynamic,1> & I,
  43. Eigen::Matrix<T,Eigen::Dynamic,1> & V)
  44. {
  45. // Resize outputs to fit nonzeros
  46. I.resize(X.nonZeros());
  47. V.resize(X.nonZeros());
  48. int i = 0;
  49. // loop over non-zeros
  50. for(typename Eigen::SparseVector<T>::InnerIterator it(X); it; ++it)
  51. {
  52. I(i) = it.index();
  53. V(i) = it.value();
  54. i++;
  55. }
  56. }
  57. #ifndef IGL_HEADER_ONLY
  58. // Explicit template specialization
  59. // generated by autoexplicit.sh
  60. template void igl::find<double, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  61. template void igl::find<double, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  62. // generated by autoexplicit.sh
  63. template void igl::find<double, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  64. #endif