accumarray.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2018 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 "accumarray.h"
  9. #include <cassert>
  10. template <
  11. typename DerivedS,
  12. typename DerivedV,
  13. typename DerivedA
  14. >
  15. void igl::accumarray(
  16. const Eigen::MatrixBase<DerivedS> & S,
  17. const Eigen::MatrixBase<DerivedV> & V,
  18. Eigen::PlainObjectBase<DerivedA> & A)
  19. {
  20. assert(V.size() == S.size() && "S and V should be same size");
  21. if(S.size() == 0) { A.resize(0,1); return; }
  22. A.setZero(S.maxCoeff()+1,1);
  23. for(int s = 0;s<S.size();s++)
  24. {
  25. A(S(s)) += V(s);
  26. }
  27. }
  28. template <
  29. typename DerivedS,
  30. typename DerivedA
  31. >
  32. void igl::accumarray(
  33. const Eigen::MatrixBase<DerivedS> & S,
  34. const typename DerivedA::Scalar V,
  35. Eigen::PlainObjectBase<DerivedA> & A)
  36. {
  37. if(S.size() == 0) { A.resize(0,1); return; }
  38. A.setZero(S.maxCoeff()+1,1);
  39. for(int s = 0;s<S.size();s++)
  40. {
  41. A(S(s)) += V;
  42. }
  43. }
  44. #ifdef IGL_STATIC_LIBRARY
  45. // Explicit template instantiation
  46. // generated by autoexplicit.sh
  47. template void igl::accumarray<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  48. template void igl::accumarray<Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  49. #endif