find.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "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::DenseBase<DerivedI> & I,
  19. Eigen::DenseBase<DerivedJ> & J,
  20. Eigen::DenseBase<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 < typename T>
  41. IGL_INLINE std::vector<Eigen::Triplet<T> > igl::find(
  42. const Eigen::SparseMatrix<T>& X)
  43. {
  44. std::vector<Eigen::Triplet<T>> ijv;
  45. for(int i = 0; i < X.outerSize(); i++)
  46. {
  47. for(typename Eigen::SparseMatrix<T>::InnerIterator it(X,i); it; it++)
  48. {
  49. // Match find above
  50. //if(it.value())
  51. {
  52. ijv.emplace_back(it.row(),it.col(),it.value());
  53. }
  54. }
  55. }
  56. return ijv;
  57. }
  58. template <
  59. typename DerivedX,
  60. typename DerivedI,
  61. typename DerivedJ,
  62. typename DerivedV>
  63. IGL_INLINE void igl::find(
  64. const Eigen::DenseBase<DerivedX>& X,
  65. Eigen::PlainObjectBase<DerivedI> & I,
  66. Eigen::PlainObjectBase<DerivedJ> & J,
  67. Eigen::PlainObjectBase<DerivedV> & V)
  68. {
  69. const int nnz = X.count();
  70. I.resize(nnz,1);
  71. J.resize(nnz,1);
  72. V.resize(nnz,1);
  73. {
  74. int k = 0;
  75. for(int j = 0;j<X.cols();j++)
  76. {
  77. for(int i = 0;i<X.rows();i++)
  78. {
  79. if(X(i,j))
  80. {
  81. I(k) = i;
  82. J(k) = j;
  83. V(k) = X(i,j);
  84. k++;
  85. }
  86. }
  87. }
  88. }
  89. }
  90. template <
  91. typename DerivedX,
  92. typename DerivedI>
  93. IGL_INLINE void igl::find(
  94. const Eigen::DenseBase<DerivedX>& X,
  95. Eigen::PlainObjectBase<DerivedI> & I)
  96. {
  97. const int nnz = X.count();
  98. I.resize(nnz,1);
  99. {
  100. int k = 0;
  101. for(int j = 0;j<X.cols();j++)
  102. {
  103. for(int i = 0;i<X.rows();i++)
  104. {
  105. if(X(i,j))
  106. {
  107. I(k) = i+X.rows()*j;
  108. k++;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. template <typename T>
  115. IGL_INLINE void igl::find(
  116. const Eigen::SparseVector<T>& X,
  117. Eigen::Matrix<int,Eigen::Dynamic,1> & I,
  118. Eigen::Matrix<T,Eigen::Dynamic,1> & V)
  119. {
  120. // Resize outputs to fit nonzeros
  121. I.resize(X.nonZeros());
  122. V.resize(X.nonZeros());
  123. int i = 0;
  124. // loop over non-zeros
  125. for(typename Eigen::SparseVector<T>::InnerIterator it(X); it; ++it)
  126. {
  127. I(i) = it.index();
  128. V(i) = it.value();
  129. i++;
  130. }
  131. }
  132. template <int RowsAtCompileTime, int MaxRowsAtCompileTime>
  133. IGL_INLINE std::vector<int> igl::find(
  134. const Eigen::Array<bool,RowsAtCompileTime,1,0,MaxRowsAtCompileTime,1> & M)
  135. {
  136. std::vector<int> I;
  137. // This reserve seems to be worth it even if it means running over M twice
  138. I.reserve(M.count());
  139. for(int i = 0;i<M.size();i++)
  140. {
  141. if(M(i))
  142. {
  143. I.push_back(i);
  144. }
  145. }
  146. return I;
  147. }
  148. #ifdef IGL_STATIC_LIBRARY
  149. // Explicit template instantiation
  150. template std::vector<int> igl::find<Eigen::Dynamic, Eigen::Dynamic>(Eigen::Array<bool, Eigen::Dynamic, 1, 0, Eigen::Dynamic, 1> const&);
  151. template void igl::find<bool, Eigen::Matrix<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<bool, 0, int> const&, Eigen::DenseBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  152. template void igl::find<int, 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::SparseMatrix<int, 0, int> const&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  153. template void igl::find<bool, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Array<bool, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<bool, 0, int> const&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Array<bool, -1, 1, 0, -1, 1> >&);
  154. template void igl::find<Eigen::Array<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Array<bool, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  155. 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::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  156. 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::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  157. 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::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  158. template void igl::find<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  159. template void igl::find<double, Eigen::Matrix<long, -1, 1, 0, -1, 1>, Eigen::Matrix<long, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::DenseBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> >&, Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  160. template void igl::find<Eigen::Matrix<bool, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::DenseBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  161. #endif