slice.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.h"
  9. #include "colon.h"
  10. #include <vector>
  11. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  12. #include <iostream>
  13. #include <unsupported/Eigen/SparseExtra>
  14. template <typename T>
  15. IGL_INLINE void igl::slice(
  16. const Eigen::SparseMatrix<T>& X,
  17. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  18. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  19. Eigen::SparseMatrix<T>& Y)
  20. {
  21. int xm = X.rows();
  22. int xn = X.cols();
  23. int ym = R.size();
  24. int yn = C.size();
  25. // special case when R or C is empty
  26. if(ym == 0 || yn == 0)
  27. {
  28. Y.resize(ym,yn);
  29. return;
  30. }
  31. assert(R.minCoeff() >= 0);
  32. assert(R.maxCoeff() < xm);
  33. assert(C.minCoeff() >= 0);
  34. assert(C.maxCoeff() < xn);
  35. // initialize row and col permutation vectors
  36. Eigen::VectorXi rowIndexVec = Eigen::VectorXi::LinSpaced(xm,0,xm);
  37. Eigen::VectorXi rowPermVec = Eigen::VectorXi::LinSpaced(xm,0,xm);
  38. for(int i=0;i<ym;i++)
  39. {
  40. int pos = rowIndexVec.coeffRef(R(i));
  41. if(pos != i)
  42. {
  43. int& val = rowPermVec.coeffRef(i);
  44. std::swap(rowIndexVec.coeffRef(val),rowIndexVec.coeffRef(R(i)));
  45. std::swap(rowPermVec.coeffRef(i),rowPermVec.coeffRef(pos));
  46. }
  47. }
  48. Eigen::PermutationMatrix<Eigen::Dynamic,Eigen::Dynamic,int> rowPerm(rowIndexVec);
  49. Eigen::VectorXi colIndexVec = Eigen::VectorXi::LinSpaced(xn,0,xn);
  50. Eigen::VectorXi colPermVec = Eigen::VectorXi::LinSpaced(xn,0,xn);
  51. for(int i=0;i<yn;i++)
  52. {
  53. int pos = colIndexVec.coeffRef(C(i));
  54. if(pos != i)
  55. {
  56. int& val = colPermVec.coeffRef(i);
  57. std::swap(colIndexVec.coeffRef(val),colIndexVec.coeffRef(C(i)));
  58. std::swap(colPermVec.coeffRef(i),colPermVec.coeffRef(pos));
  59. }
  60. }
  61. Eigen::PermutationMatrix<Eigen::Dynamic,Eigen::Dynamic,int> colPerm(colPermVec);
  62. Eigen::SparseMatrix<T> M = (rowPerm * X);
  63. Y = (M * colPerm).block(0,0,ym,yn);
  64. }
  65. template <typename Mat>
  66. IGL_INLINE void igl::slice(
  67. const Mat& X,
  68. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  69. const int dim,
  70. Mat& Y)
  71. {
  72. Eigen::VectorXi C;
  73. switch(dim)
  74. {
  75. case 1:
  76. // boring base case
  77. if(X.cols() == 0)
  78. {
  79. Y.resize(R.size(),0);
  80. return;
  81. }
  82. igl::colon(0,X.cols()-1,C);
  83. return slice(X,R,C,Y);
  84. case 2:
  85. // boring base case
  86. if(X.rows() == 0)
  87. {
  88. Y.resize(0,R.size());
  89. return;
  90. }
  91. igl::colon(0,X.rows()-1,C);
  92. return slice(X,C,R,Y);
  93. default:
  94. assert(false && "Unsupported dimension");
  95. return;
  96. }
  97. }
  98. template <typename DerivedX>
  99. IGL_INLINE void igl::slice(
  100. const Eigen::PlainObjectBase<DerivedX> & X,
  101. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  102. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  103. Eigen::PlainObjectBase<DerivedX> & Y)
  104. {
  105. #ifndef NDEBUG
  106. int xm = X.rows();
  107. int xn = X.cols();
  108. #endif
  109. int ym = R.size();
  110. int yn = C.size();
  111. // special case when R or C is empty
  112. if(ym == 0 || yn == 0)
  113. {
  114. Y.resize(ym,yn);
  115. return;
  116. }
  117. assert(R.minCoeff() >= 0);
  118. assert(R.maxCoeff() < xm);
  119. assert(C.minCoeff() >= 0);
  120. assert(C.maxCoeff() < xn);
  121. // Resize output
  122. Y.resize(ym,yn);
  123. // loop over output rows, then columns
  124. for(int i = 0;i<ym;i++)
  125. {
  126. for(int j = 0;j<yn;j++)
  127. {
  128. Y(i,j) = X(R(i),C(j));
  129. }
  130. }
  131. }
  132. template <typename DerivedX>
  133. IGL_INLINE void igl::slice(
  134. const Eigen::PlainObjectBase<DerivedX> & X,
  135. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  136. Eigen::PlainObjectBase<DerivedX> & Y)
  137. {
  138. // phony column indices
  139. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  140. C.resize(1);
  141. C(0) = 0;
  142. return igl::slice(X,R,C,Y);
  143. }
  144. template <typename DerivedX>
  145. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  146. const Eigen::PlainObjectBase<DerivedX> & X,
  147. const Eigen::Matrix<int,Eigen::Dynamic,1> & R)
  148. {
  149. Eigen::PlainObjectBase<DerivedX> Y;
  150. igl::slice(X,R,Y);
  151. return Y;
  152. }
  153. template <typename DerivedX>
  154. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  155. const Eigen::PlainObjectBase<DerivedX>& X,
  156. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  157. const int dim)
  158. {
  159. Eigen::PlainObjectBase<DerivedX> Y;
  160. // phony column indices
  161. igl::slice(X,R,dim,Y);
  162. return Y;
  163. }
  164. #ifdef IGL_STATIC_LIBRARY
  165. // Explicit template specialization
  166. // generated by autoexplicit.sh
  167. template void igl::slice<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> >&);
  168. // generated by autoexplicit.sh
  169. template void igl::slice<Eigen::Matrix<float, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> >&);
  170. // generated by autoexplicit.sh
  171. template void igl::slice<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> >&);
  172. // generated by autoexplicit.sh
  173. template void igl::slice<Eigen::Matrix<float, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -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<float, -1, -1, 0, -1, -1> >&);
  174. // generated by autoexplicit.sh
  175. template void igl::slice<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>&);
  176. template void igl::slice<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> >&);
  177. template void igl::slice<Eigen::PlainObjectBase<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&, int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  178. template void igl::slice<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>&);
  179. template void igl::slice<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>&);
  180. template Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > igl::slice<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);
  181. template Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > igl::slice<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);
  182. template void igl::slice<std::complex<double> >(Eigen::SparseMatrix<std::complex<double>, 0, int> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, Eigen::SparseMatrix<std::complex<double>, 0, int>&);
  183. template void igl::slice<Eigen::Matrix<double, -1, 3, 0, -1, 3> >(Eigen::Matrix<double, -1, 3, 0, -1, 3> const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, int, Eigen::Matrix<double, -1, 3, 0, -1, 3>&);
  184. template Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > igl::slice<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&);
  185. template Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > igl::slice<Eigen::Matrix<double, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, int);
  186. template Eigen::PlainObjectBase<Eigen::Matrix<double, 1, -1, 1, 1, -1> > igl::slice<Eigen::Matrix<double, 1, -1, 1, 1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, 1, -1, 1, 1, -1> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, int);
  187. template Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > igl::slice<Eigen::Matrix<int, -1, 3, 0, -1, 3> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> > const&, Eigen::Matrix<int, -1, 1, 0, -1, 1> const&, int);
  188. #endif