slice_cached.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 <igl/slice.h>
  13. template <typename TX, typename TY>
  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::SparseMatrix<TY>& Y,
  19. Eigen::VectorXi& data)
  20. {
  21. // Create a sparse matrix whose entries are the ids
  22. Eigen::SparseMatrix<unsigned> TS = X.template cast<unsigned>();
  23. TS.makeCompressed();
  24. for (unsigned i=0;i<TS.nonZeros();++i)
  25. *(TS.valuePtr() + i) = i;
  26. Eigen::SparseMatrix<unsigned> TS_sliced;
  27. igl::slice(TS,R,C,TS_sliced);
  28. Y = TS_sliced.cast<TY>();
  29. data.resize(TS_sliced.nonZeros());
  30. for (unsigned i=0;i<data.size();++i)
  31. {
  32. data[i] = *(TS_sliced.valuePtr() + i);
  33. *(Y.valuePtr() + i) = *(X.valuePtr() + data[i]);
  34. }
  35. }
  36. template <typename TX, typename TY>
  37. IGL_INLINE void igl::slice_cached(
  38. const Eigen::SparseMatrix<TX>& X,
  39. Eigen::SparseMatrix<TY>& Y,
  40. const Eigen::VectorXi& data)
  41. {
  42. for (unsigned i=0; i<data.size(); ++i)
  43. *(Y.valuePtr() + i) = *(X.valuePtr() + data[i]);
  44. }
  45. #ifdef IGL_STATIC_LIBRARY
  46. template void igl::slice_cached_precompute<double, double>(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::SparseMatrix<double, 0, int>&, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
  47. template void igl::slice_cached<double, double>(Eigen::SparseMatrix<double, 0, int> const&, Eigen::SparseMatrix<double, 0, int>&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&);
  48. #endif