slice.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. #include <unsupported/Eigen/SparseExtra>
  12. template <typename TX, typename TY>
  13. IGL_INLINE void igl::slice(
  14. const Eigen::SparseMatrix<TX>& X,
  15. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  16. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  17. Eigen::SparseMatrix<TY>& Y)
  18. {
  19. #if 1
  20. int xm = X.rows();
  21. int xn = X.cols();
  22. int ym = R.size();
  23. int yn = C.size();
  24. // special case when R or C is empty
  25. if(ym == 0 || yn == 0)
  26. {
  27. Y.resize(ym,yn);
  28. return;
  29. }
  30. assert(R.minCoeff() >= 0);
  31. assert(R.maxCoeff() < xm);
  32. assert(C.minCoeff() >= 0);
  33. assert(C.maxCoeff() < xn);
  34. // Build reindexing maps for columns and rows, -1 means not in map
  35. std::vector<std::vector<int> > RI;
  36. RI.resize(xm);
  37. for(int i = 0;i<ym;i++)
  38. {
  39. RI[R(i)].push_back(i);
  40. }
  41. std::vector<std::vector<int> > CI;
  42. CI.resize(xn);
  43. // initialize to -1
  44. for(int i = 0;i<yn;i++)
  45. {
  46. CI[C(i)].push_back(i);
  47. }
  48. // Resize output
  49. Eigen::DynamicSparseMatrix<TY, Eigen::RowMajor> dyn_Y(ym,yn);
  50. // Take a guess at the number of nonzeros (this assumes uniform distribution
  51. // not banded or heavily diagonal)
  52. dyn_Y.reserve((X.nonZeros()/(X.rows()*X.cols())) * (ym*yn));
  53. // Iterate over outside
  54. for(int k=0; k<X.outerSize(); ++k)
  55. {
  56. // Iterate over inside
  57. for(typename Eigen::SparseMatrix<TX>::InnerIterator it (X,k); it; ++it)
  58. {
  59. std::vector<int>::iterator rit, cit;
  60. for(rit = RI[it.row()].begin();rit != RI[it.row()].end(); rit++)
  61. {
  62. for(cit = CI[it.col()].begin();cit != CI[it.col()].end(); cit++)
  63. {
  64. dyn_Y.coeffRef(*rit,*cit) = it.value();
  65. }
  66. }
  67. }
  68. }
  69. Y = Eigen::SparseMatrix<TY>(dyn_Y);
  70. #else
  71. // Alec: This is _not_ valid for arbitrary R,C since they don't necessary
  72. // representation a strict permutation of the rows and columns: rows or
  73. // columns could be removed or replicated. The removal of rows seems to be
  74. // handled here (although it's not clear if there is a performance gain when
  75. // the #removals >> #remains). If this is sufficiently faster than the
  76. // correct code above, one could test whether all entries in R and C are
  77. // unique and apply the permutation version if appropriate.
  78. //
  79. int xm = X.rows();
  80. int xn = X.cols();
  81. int ym = R.size();
  82. int yn = C.size();
  83. // special case when R or C is empty
  84. if(ym == 0 || yn == 0)
  85. {
  86. Y.resize(ym,yn);
  87. return;
  88. }
  89. assert(R.minCoeff() >= 0);
  90. assert(R.maxCoeff() < xm);
  91. assert(C.minCoeff() >= 0);
  92. assert(C.maxCoeff() < xn);
  93. // initialize row and col permutation vectors
  94. Eigen::VectorXi rowIndexVec = Eigen::VectorXi::LinSpaced(xm,0,xm-1);
  95. Eigen::VectorXi rowPermVec = Eigen::VectorXi::LinSpaced(xm,0,xm-1);
  96. for(int i=0;i<ym;i++)
  97. {
  98. int pos = rowIndexVec.coeffRef(R(i));
  99. if(pos != i)
  100. {
  101. int& val = rowPermVec.coeffRef(i);
  102. std::swap(rowIndexVec.coeffRef(val),rowIndexVec.coeffRef(R(i)));
  103. std::swap(rowPermVec.coeffRef(i),rowPermVec.coeffRef(pos));
  104. }
  105. }
  106. Eigen::PermutationMatrix<Eigen::Dynamic,Eigen::Dynamic,int> rowPerm(rowIndexVec);
  107. Eigen::VectorXi colIndexVec = Eigen::VectorXi::LinSpaced(xn,0,xn-1);
  108. Eigen::VectorXi colPermVec = Eigen::VectorXi::LinSpaced(xn,0,xn-1);
  109. for(int i=0;i<yn;i++)
  110. {
  111. int pos = colIndexVec.coeffRef(C(i));
  112. if(pos != i)
  113. {
  114. int& val = colPermVec.coeffRef(i);
  115. std::swap(colIndexVec.coeffRef(val),colIndexVec.coeffRef(C(i)));
  116. std::swap(colPermVec.coeffRef(i),colPermVec.coeffRef(pos));
  117. }
  118. }
  119. Eigen::PermutationMatrix<Eigen::Dynamic,Eigen::Dynamic,int> colPerm(colPermVec);
  120. Eigen::SparseMatrix<T> M = (rowPerm * X);
  121. Y = (M * colPerm).block(0,0,ym,yn);
  122. #endif
  123. }
  124. template <typename MatX, typename DerivedR, typename MatY>
  125. IGL_INLINE void igl::slice(
  126. const MatX& X,
  127. const Eigen::PlainObjectBase<DerivedR> & R,
  128. const int dim,
  129. MatY& Y)
  130. {
  131. Eigen::Matrix<typename DerivedR::Scalar,Eigen::Dynamic,1> C;
  132. switch(dim)
  133. {
  134. case 1:
  135. // boring base case
  136. if(X.cols() == 0)
  137. {
  138. Y.resize(R.size(),0);
  139. return;
  140. }
  141. igl::colon(0,X.cols()-1,C);
  142. return slice(X,R,C,Y);
  143. case 2:
  144. // boring base case
  145. if(X.rows() == 0)
  146. {
  147. Y.resize(0,R.size());
  148. return;
  149. }
  150. igl::colon(0,X.rows()-1,C);
  151. return slice(X,C,R,Y);
  152. default:
  153. assert(false && "Unsupported dimension");
  154. return;
  155. }
  156. }
  157. template <
  158. typename DerivedX,
  159. typename DerivedR,
  160. typename DerivedC,
  161. typename DerivedY>
  162. IGL_INLINE void igl::slice(
  163. const Eigen::PlainObjectBase<DerivedX> & X,
  164. const Eigen::PlainObjectBase<DerivedR> & R,
  165. const Eigen::PlainObjectBase<DerivedC> & C,
  166. Eigen::PlainObjectBase<DerivedY> & Y)
  167. {
  168. #ifndef NDEBUG
  169. int xm = X.rows();
  170. int xn = X.cols();
  171. #endif
  172. int ym = R.size();
  173. int yn = C.size();
  174. // special case when R or C is empty
  175. if(ym == 0 || yn == 0)
  176. {
  177. Y.resize(ym,yn);
  178. return;
  179. }
  180. assert(R.minCoeff() >= 0);
  181. assert(R.maxCoeff() < xm);
  182. assert(C.minCoeff() >= 0);
  183. assert(C.maxCoeff() < xn);
  184. // Resize output
  185. Y.resize(ym,yn);
  186. // loop over output rows, then columns
  187. for(int i = 0;i<ym;i++)
  188. {
  189. for(int j = 0;j<yn;j++)
  190. {
  191. Y(i,j) = X(R(i),C(j));
  192. }
  193. }
  194. }
  195. template <typename DerivedX, typename DerivedY>
  196. IGL_INLINE void igl::slice(
  197. const Eigen::PlainObjectBase<DerivedX> & X,
  198. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  199. Eigen::PlainObjectBase<DerivedY> & Y)
  200. {
  201. // phony column indices
  202. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  203. C.resize(1);
  204. C(0) = 0;
  205. return igl::slice(X,R,C,Y);
  206. }
  207. template <typename DerivedX>
  208. IGL_INLINE DerivedX igl::slice(
  209. const Eigen::PlainObjectBase<DerivedX> & X,
  210. const Eigen::Matrix<int,Eigen::Dynamic,1> & R)
  211. {
  212. DerivedX Y;
  213. igl::slice(X,R,Y);
  214. return Y;
  215. }
  216. template <typename DerivedX>
  217. IGL_INLINE DerivedX igl::slice(
  218. const Eigen::PlainObjectBase<DerivedX>& X,
  219. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  220. const int dim)
  221. {
  222. DerivedX Y;
  223. igl::slice(X,R,dim,Y);
  224. return Y;
  225. }
  226. #ifdef IGL_STATIC_LIBRARY
  227. // Explicit template specialization
  228. // generated by autoexplicit.sh
  229. template 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);
  230. template 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);
  231. template 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&);
  232. template 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);
  233. template 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);
  234. template 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);
  235. template Eigen::Matrix<int, -1, -1, 0, -1, -1 > 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&, int);
  236. template void igl::slice<std::complex<double>, 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>&);
  237. template void igl::slice<Eigen::Matrix<int, -1, 1, 0, -1, 1>, 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> >&);
  238. template void igl::slice<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>&);
  239. template void igl::slice<Eigen::SparseMatrix<double, 0, int>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::SparseMatrix<double, 0, int> >(Eigen::SparseMatrix<double, 0, int> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int, Eigen::SparseMatrix<double, 0, int>&);
  240. template void igl::slice<Eigen::Matrix<double, -1, 1, 0, -1, 1>, 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> >&);
  241. template void igl::slice<Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  242. template void igl::slice<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int, Eigen::Matrix<double, -1, -1, 0, -1, -1>&);
  243. template void igl::slice<Eigen::Matrix<float, -1, 1, 0, -1, 1>, 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> >&);
  244. template void igl::slice<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  245. template void igl::slice<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  246. template void igl::slice<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::Matrix<int, -1, -1, 0, -1, -1> const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> > const&, int, Eigen::Matrix<int, -1, -1, 0, -1, -1>&);
  247. template void igl::slice<Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >, Eigen::Matrix<long, -1, 1, 0, -1, 1>, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<long, -1, 1, 0, -1, 1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  248. #endif