slice_cached.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 Daniele Panozzo <daniele.panozzo@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 "slice_cached.h"
  9. #include <iostream>
  10. #include <vector>
  11. #include <utility>
  12. #include "slice.h"
  13. template <typename TX, typename TY, typename DerivedI>
  14. IGL_INLINE void igl::slice_cached_precompute(
  15. const Eigen::SparseMatrix<TX>& X,
  16. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  17. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  18. Eigen::MatrixBase<DerivedI>& data,
  19. Eigen::SparseMatrix<TY>& Y
  20. )
  21. {
  22. // Create a sparse matrix whose entries are the ids
  23. Eigen::SparseMatrix<unsigned> TS = X.template cast<unsigned>();
  24. TS.makeCompressed();
  25. for (unsigned i=0;i<TS.nonZeros();++i)
  26. *(TS.valuePtr() + i) = i;
  27. Eigen::SparseMatrix<unsigned> TS_sliced;
  28. igl::slice(TS,R,C,TS_sliced);
  29. Y = TS_sliced.cast<TY>();
  30. data.resize(TS_sliced.nonZeros());
  31. for (unsigned i=0;i<data.size();++i)
  32. {
  33. data[i] = *(TS_sliced.valuePtr() + i);
  34. *(Y.valuePtr() + i) = *(X.valuePtr() + data[i]);
  35. }
  36. }
  37. template <typename TX, typename TY, typename DerivedI>
  38. IGL_INLINE void igl::slice_cached(
  39. const Eigen::SparseMatrix<TX>& X,
  40. const Eigen::MatrixBase<DerivedI>& data,
  41. Eigen::SparseMatrix<TY>& Y
  42. )
  43. {
  44. for (unsigned i=0; i<data.size(); ++i)
  45. *(Y.valuePtr() + i) = *(X.valuePtr() + data[i]);
  46. }
  47. #ifdef IGL_STATIC_LIBRARY
  48. template void igl::slice_cached<double, double, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::SparseMatrix<double, 0, int>&);
  49. template void igl::slice_cached_precompute<double, double, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::SparseMatrix<double, 0, int>&);
  50. #endif