find.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include <iostream>
  11. template <
  12. typename T,
  13. typename DerivedI,
  14. typename DerivedJ,
  15. typename DerivedV>
  16. IGL_INLINE void igl::find(
  17. const Eigen::SparseMatrix<T>& X,
  18. Eigen::MatrixBase<DerivedI> & I,
  19. Eigen::MatrixBase<DerivedJ> & J,
  20. Eigen::MatrixBase<DerivedV> & V)
  21. {
  22. // Resize outputs to fit nonzeros
  23. I.derived().resize(X.nonZeros(),1);
  24. J.derived().resize(X.nonZeros(),1);
  25. V.derived().resize(X.nonZeros(),1);
  26. int i = 0;
  27. // Iterate over outside
  28. for(int k=0; k<X.outerSize(); ++k)
  29. {
  30. // Iterate over inside
  31. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  32. {
  33. V(i) = it.value();
  34. I(i) = it.row();
  35. J(i) = it.col();
  36. i++;
  37. }
  38. }
  39. }
  40. template <
  41. typename DerivedX,
  42. typename DerivedI,
  43. typename DerivedJ,
  44. typename DerivedV>
  45. IGL_INLINE void igl::find(
  46. const Eigen::PlainObjectBase<DerivedX>& X,
  47. Eigen::PlainObjectBase<DerivedI> & I,
  48. Eigen::PlainObjectBase<DerivedJ> & J,
  49. Eigen::PlainObjectBase<DerivedV> & V)
  50. {
  51. const int nnz = X.count();
  52. I.resize(nnz,1);
  53. J.resize(nnz,1);
  54. V.resize(nnz,1);
  55. {
  56. int k = 0;
  57. for(int j = 0;j<X.cols();j++)
  58. {
  59. for(int i = 0;i<X.rows();i++)
  60. {
  61. if(X(i,j))
  62. {
  63. I(k) = i;
  64. J(k) = j;
  65. V(k) = X(i,j);
  66. k++;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. template <
  73. typename DerivedX,
  74. typename DerivedI>
  75. IGL_INLINE void igl::find(
  76. const Eigen::PlainObjectBase<DerivedX>& X,
  77. Eigen::PlainObjectBase<DerivedI> & I)
  78. {
  79. const int nnz = X.count();
  80. I.resize(nnz,1);
  81. {
  82. int k = 0;
  83. for(int j = 0;j<X.cols();j++)
  84. {
  85. for(int i = 0;i<X.rows();i++)
  86. {
  87. if(X(i,j))
  88. {
  89. I(k) = i+X.rows()*j;
  90. k++;
  91. }
  92. }
  93. }
  94. }
  95. }
  96. template <typename T>
  97. IGL_INLINE void igl::find(
  98. const Eigen::SparseVector<T>& X,
  99. Eigen::Matrix<int,Eigen::Dynamic,1> & I,
  100. Eigen::Matrix<T,Eigen::Dynamic,1> & V)
  101. {
  102. // Resize outputs to fit nonzeros
  103. I.resize(X.nonZeros());
  104. V.resize(X.nonZeros());
  105. int i = 0;
  106. // loop over non-zeros
  107. for(typename Eigen::SparseVector<T>::InnerIterator it(X); it; ++it)
  108. {
  109. I(i) = it.index();
  110. V(i) = it.value();
  111. i++;
  112. }
  113. }
  114. #ifdef IGL_STATIC_LIBRARY
  115. // Explicit template specialization
  116. // generated by autoexplicit.sh
  117. 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> >&);
  118. 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> >&);
  119. // generated by autoexplicit.sh
  120. 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> >&);
  121. #endif