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