find_zero.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include "find_zero.h"
  2. #include "for_each.h"
  3. #include "any.h"
  4. template <typename AType, typename DerivedI>
  5. IGL_INLINE void igl::find_zero(
  6. const Eigen::SparseMatrix<AType> & A,
  7. const int dim,
  8. Eigen::PlainObjectBase<DerivedI> & I)
  9. {
  10. assert((dim == 1 || dim == 2) && "dim must be 2 or 1");
  11. // Get size of input
  12. int m = A.rows();
  13. int n = A.cols();
  14. // I starts by containing guess where 0 might be
  15. I = DerivedI::Zero(dim==1?n:m);
  16. Eigen::Array<bool,Eigen::Dynamic,1> found =
  17. Eigen::Array<bool,Eigen::Dynamic,1>::Zero(dim==1?n:m);
  18. const auto func = [&I,&found,&dim](int i, int j, const int v)
  19. {
  20. if(dim == 2)
  21. {
  22. std::swap(i,j);
  23. }
  24. // Coded as if dim == 1, assuming swap for dim == 2
  25. // Have we already found a zero?
  26. if(!found(j))
  27. {
  28. if(I(j) != i || v == 0)
  29. {
  30. // either there was an implicit zero between the last element and this
  31. // one, or this one is zero
  32. found(j) = true;
  33. }else
  34. {
  35. // If not found, then guess that next element will be zero
  36. I(j) = I(j)+1;
  37. }
  38. }
  39. };
  40. for_each(A,func);
  41. }
  42. #ifdef IGL_STATIC_LIBRARY
  43. // Explicit template instantiation
  44. // generated by autoexplicit.sh
  45. template void igl::find_zero<bool, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<bool, 0, int> const&, int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  46. #endif