slice_into.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include "colon.h"
  10. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  11. #include <iostream>
  12. #include <unsupported/Eigen/SparseExtra>
  13. template <typename T>
  14. IGL_INLINE void igl::slice_into(
  15. const Eigen::SparseMatrix<T>& X,
  16. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  17. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  18. Eigen::SparseMatrix<T>& Y)
  19. {
  20. #ifndef NDEBUG
  21. int xm = X.rows();
  22. int xn = X.cols();
  23. assert(R.size() == xm);
  24. assert(C.size() == xn);
  25. int ym = Y.size();
  26. int yn = Y.size();
  27. assert(R.minCoeff() >= 0);
  28. assert(R.maxCoeff() < ym);
  29. assert(C.minCoeff() >= 0);
  30. assert(C.maxCoeff() < yn);
  31. #endif
  32. // create temporary dynamic sparse matrix
  33. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_Y(Y);
  34. // Iterate over outside
  35. for(int k=0; k<X.outerSize(); ++k)
  36. {
  37. // Iterate over inside
  38. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  39. {
  40. dyn_Y.coeffRef(R(it.row()),C(it.col())) = it.value();
  41. }
  42. }
  43. Y = Eigen::SparseMatrix<T>(dyn_Y);
  44. }
  45. template <typename DerivedX>
  46. IGL_INLINE void igl::slice_into(
  47. const Eigen::PlainObjectBase<DerivedX> & X,
  48. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  49. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  50. Eigen::PlainObjectBase<DerivedX> & Y)
  51. {
  52. int xm = X.rows();
  53. int xn = X.cols();
  54. #ifndef NDEBUG
  55. assert(R.size() == xm);
  56. assert(C.size() == xn);
  57. int ym = Y.size();
  58. int yn = Y.size();
  59. assert(R.minCoeff() >= 0);
  60. assert(R.maxCoeff() < ym);
  61. assert(C.minCoeff() >= 0);
  62. assert(C.maxCoeff() < yn);
  63. #endif
  64. // Build reindexing maps for columns and rows, -1 means not in map
  65. Eigen::Matrix<int,Eigen::Dynamic,1> RI;
  66. RI.resize(xm);
  67. for(int i = 0;i<xm;i++)
  68. {
  69. for(int j = 0;j<xn;j++)
  70. {
  71. Y(R(i),C(j)) = X(i,j);
  72. }
  73. }
  74. }
  75. template <typename Mat>
  76. IGL_INLINE void igl::slice_into(
  77. const Mat& X,
  78. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  79. const int dim,
  80. Mat& Y)
  81. {
  82. Eigen::VectorXi C;
  83. switch(dim)
  84. {
  85. case 1:
  86. assert(R.size() == X.rows());
  87. // boring base case
  88. if(X.cols() == 0)
  89. {
  90. return;
  91. }
  92. igl::colon(0,X.cols()-1,C);
  93. return slice_into(X,R,C,Y);
  94. case 2:
  95. assert(R.size() == X.cols());
  96. // boring base case
  97. if(X.rows() == 0)
  98. {
  99. return;
  100. }
  101. igl::colon(0,X.rows()-1,C);
  102. return slice_into(X,C,R,Y);
  103. default:
  104. assert(false && "Unsupported dimension");
  105. return;
  106. }
  107. }
  108. template <typename DerivedX>
  109. IGL_INLINE void igl::slice_into(
  110. const Eigen::PlainObjectBase<DerivedX> & X,
  111. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  112. Eigen::PlainObjectBase<DerivedX> & Y)
  113. {
  114. // phony column indices
  115. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  116. C.resize(1);
  117. C(0) = 0;
  118. return igl::slice_into(X,R,C,Y);
  119. }
  120. #ifdef IGL_STATIC_LIBRARY
  121. // Explicit template specialization
  122. // generated by autoexplicit.sh
  123. 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> >&);
  124. 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> >&);
  125. template void igl::slice_into<Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, int, Eigen::Matrix<double, -1, -1, 0, -1, -1>&);
  126. template void igl::slice_into<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  127. template void igl::slice_into<Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::Matrix<double, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, int, Eigen::Matrix<double, -1, 1, 0, -1, 1>&);
  128. template void igl::slice_into<Eigen::SparseMatrix<double, 0, int> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, int, Eigen::SparseMatrix<double, 0, int>&);
  129. template void igl::slice_into<Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, int, Eigen::Matrix<int, -1, 1, 0, -1, 1>&);
  130. template void igl::slice_into<Eigen::PlainObjectBase<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&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  131. template void igl::slice_into<Eigen::PlainObjectBase<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&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  132. 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> >&);
  133. template void igl::slice_into<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  134. template void igl::slice_into<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, int, Eigen::Matrix<int, -1, -1, 0, -1, -1>&);
  135. template void igl::slice_into<int>(Eigen::SparseMatrix<int, 0, int> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::SparseMatrix<int, 0, int>&);
  136. #endif