count.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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 "count.h"
  9. #include "redux.h"
  10. template <typename XType, typename SType>
  11. IGL_INLINE void igl::count(
  12. const Eigen::SparseMatrix<XType>& X,
  13. const int dim,
  14. Eigen::SparseVector<SType>& S)
  15. {
  16. // dim must be 2 or 1
  17. assert(dim == 1 || dim == 2);
  18. // Get size of input
  19. int m = X.rows();
  20. int n = X.cols();
  21. // resize output
  22. if(dim==1)
  23. {
  24. S = Eigen::SparseVector<SType>(n);
  25. }else
  26. {
  27. S = Eigen::SparseVector<SType>(m);
  28. }
  29. // Iterate over outside
  30. for(int k=0; k<X.outerSize(); ++k)
  31. {
  32. // Iterate over inside
  33. for(typename Eigen::SparseMatrix<XType>::InnerIterator it (X,k); it; ++it)
  34. {
  35. if(dim == 1)
  36. {
  37. S.coeffRef(it.col()) += (it.value() == 0? 0: 1);
  38. }else
  39. {
  40. S.coeffRef(it.row()) += (it.value() == 0? 0: 1);
  41. }
  42. }
  43. }
  44. }
  45. template <typename AType, typename DerivedB>
  46. IGL_INLINE void igl::count(
  47. const Eigen::SparseMatrix<AType>& A,
  48. const int dim,
  49. Eigen::PlainObjectBase<DerivedB>& B)
  50. {
  51. typedef typename DerivedB::Scalar Scalar;
  52. igl::redux(A,dim,[](Scalar a, Scalar b){ return a+(b==0?0:1);},B);
  53. }
  54. #ifdef IGL_STATIC_LIBRARY
  55. // Explicit template instantiation
  56. // generated by autoexplicit.sh
  57. template void igl::count<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> >&);
  58. // generated by autoexplicit.sh
  59. template void igl::count<bool, Eigen::Array<int, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<bool, 0, int> const&, int, Eigen::PlainObjectBase<Eigen::Array<int, -1, 1, 0, -1, 1> >&);
  60. #endif