slice_into.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "slice_into.h"
  9. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  10. #include <iostream>
  11. #include <unsupported/Eigen/SparseExtra>
  12. template <typename T>
  13. IGL_INLINE void igl::slice_into(
  14. const Eigen::SparseMatrix<T>& X,
  15. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  16. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  17. Eigen::SparseMatrix<T>& Y)
  18. {
  19. int xm = X.rows();
  20. int xn = X.cols();
  21. assert(R.size() == xm);
  22. assert(C.size() == xn);
  23. #ifndef NDEBUG
  24. int ym = Y.size();
  25. int yn = Y.size();
  26. assert(R.minCoeff() >= 0);
  27. assert(R.maxCoeff() < ym);
  28. assert(C.minCoeff() >= 0);
  29. assert(C.maxCoeff() < yn);
  30. #endif
  31. // create temporary dynamic sparse matrix
  32. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_Y(Y);
  33. // Iterate over outside
  34. for(int k=0; k<X.outerSize(); ++k)
  35. {
  36. // Iterate over inside
  37. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  38. {
  39. dyn_Y.coeffRef(R(it.row()),C(it.col())) = it.value();
  40. }
  41. }
  42. Y = Eigen::SparseMatrix<T>(dyn_Y);
  43. }
  44. template <typename DerivedX>
  45. IGL_INLINE void igl::slice_into(
  46. const Eigen::PlainObjectBase<DerivedX> & X,
  47. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  48. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  49. Eigen::PlainObjectBase<DerivedX> & Y)
  50. {
  51. int xm = X.rows();
  52. int xn = X.cols();
  53. assert(R.size() == xm);
  54. assert(C.size() == xn);
  55. #ifndef NDEBUG
  56. int ym = Y.size();
  57. int yn = Y.size();
  58. assert(R.minCoeff() >= 0);
  59. assert(R.maxCoeff() < ym);
  60. assert(C.minCoeff() >= 0);
  61. assert(C.maxCoeff() < yn);
  62. #endif
  63. // Build reindexing maps for columns and rows, -1 means not in map
  64. Eigen::Matrix<int,Eigen::Dynamic,1> RI;
  65. RI.resize(xm);
  66. for(int i = 0;i<xm;i++)
  67. {
  68. for(int j = 0;j<xn;j++)
  69. {
  70. Y(R(i),C(j)) = X(i,j);
  71. }
  72. }
  73. }
  74. template <typename DerivedX>
  75. IGL_INLINE void igl::slice_into(
  76. const Eigen::PlainObjectBase<DerivedX> & X,
  77. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  78. Eigen::PlainObjectBase<DerivedX> & Y)
  79. {
  80. // phony column indices
  81. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  82. C.resize(1);
  83. C(0) = 0;
  84. return igl::slice_into(X,R,C,Y);
  85. }
  86. #ifndef IGL_HEADER_ONLY
  87. // Explicit template specialization
  88. // generated by autoexplicit.sh
  89. template void igl::slice_into<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  90. template void igl::slice_into<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  91. #endif