sum.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 "sum.h"
  9. template <typename T>
  10. IGL_INLINE void igl::sum(
  11. const Eigen::SparseMatrix<T>& X,
  12. const int dim,
  13. Eigen::SparseVector<T>& S)
  14. {
  15. // dim must be 2 or 1
  16. assert(dim == 1 || dim == 2);
  17. // Get size of input
  18. int m = X.rows();
  19. int n = X.cols();
  20. // resize output
  21. if(dim==1)
  22. {
  23. S = Eigen::SparseVector<T>(n);
  24. }else
  25. {
  26. S = Eigen::SparseVector<T>(m);
  27. }
  28. // Iterate over outside
  29. for(int k=0; k<X.outerSize(); ++k)
  30. {
  31. // Iterate over inside
  32. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  33. {
  34. if(dim == 1)
  35. {
  36. S.coeffRef(it.col()) += it.value();
  37. }else
  38. {
  39. S.coeffRef(it.row()) += it.value();
  40. }
  41. }
  42. }
  43. }
  44. #ifdef IGL_STATIC_LIBRARY
  45. // Explicit template specialization
  46. template void igl::sum<double>(Eigen::SparseMatrix<double, 0, int> const&, int, Eigen::SparseVector<double, 0, int>&);
  47. #endif