slice.cpp 10 KB

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