slice.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 "matlab_format.h"
  11. #include <vector>
  12. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  13. #include <iostream>
  14. #include <unsupported/Eigen/SparseExtra>
  15. template <typename T>
  16. IGL_INLINE void igl::slice(
  17. const Eigen::SparseMatrix<T>& X,
  18. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  19. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  20. Eigen::SparseMatrix<T>& Y)
  21. {
  22. #if true
  23. int xm = X.rows();
  24. int xn = X.cols();
  25. int ym = R.size();
  26. int yn = C.size();
  27. // special case when R or C is empty
  28. if(ym == 0 || yn == 0)
  29. {
  30. Y.resize(ym,yn);
  31. return;
  32. }
  33. assert(R.minCoeff() >= 0);
  34. assert(R.maxCoeff() < xm);
  35. assert(C.minCoeff() >= 0);
  36. assert(C.maxCoeff() < xn);
  37. // Build reindexing maps for columns and rows, -1 means not in map
  38. std::vector<std::vector<int> > RI;
  39. RI.resize(xm);
  40. for(int i = 0;i<ym;i++)
  41. {
  42. RI[R(i)].push_back(i);
  43. }
  44. std::vector<std::vector<int> > CI;
  45. CI.resize(xn);
  46. // initialize to -1
  47. for(int i = 0;i<yn;i++)
  48. {
  49. CI[C(i)].push_back(i);
  50. }
  51. // Resize output
  52. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_Y(ym,yn);
  53. // Take a guess at the number of nonzeros (this assumes uniform distribution
  54. // not banded or heavily diagonal)
  55. dyn_Y.reserve((X.nonZeros()/(X.rows()*X.cols())) * (ym*yn));
  56. // Iterate over outside
  57. for(int k=0; k<X.outerSize(); ++k)
  58. {
  59. // Iterate over inside
  60. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  61. {
  62. std::vector<int>::iterator rit, cit;
  63. for(rit = RI[it.row()].begin();rit != RI[it.row()].end(); rit++)
  64. {
  65. for(cit = CI[it.col()].begin();cit != CI[it.col()].end(); cit++)
  66. {
  67. dyn_Y.coeffRef(*rit,*cit) = it.value();
  68. }
  69. }
  70. }
  71. }
  72. Y = Eigen::SparseMatrix<T>(dyn_Y);
  73. #else
  74. // Alec: This is _not_ valid for arbitrary R,C since they don't necessary
  75. // representation a strict permutation of the rows and columns: rows or
  76. // columns could be removed or replicated. The removal of rows seems to be
  77. // handled here (although it's not clear if there is a performance gain when
  78. // the #removals >> #remains). If this is sufficiently faster than the
  79. // correct code above, one could test whether all entries in R and C are
  80. // unique and apply the permutation version if appropriate.
  81. //
  82. int xm = X.rows();
  83. int xn = X.cols();
  84. int ym = R.size();
  85. int yn = C.size();
  86. // special case when R or C is empty
  87. if(ym == 0 || yn == 0)
  88. {
  89. Y.resize(ym,yn);
  90. return;
  91. }
  92. assert(R.minCoeff() >= 0);
  93. assert(R.maxCoeff() < xm);
  94. assert(C.minCoeff() >= 0);
  95. assert(C.maxCoeff() < xn);
  96. // initialize row and col permutation vectors
  97. Eigen::VectorXi rowIndexVec = Eigen::VectorXi::LinSpaced(xm,0,xm-1);
  98. Eigen::VectorXi rowPermVec = Eigen::VectorXi::LinSpaced(xm,0,xm-1);
  99. for(int i=0;i<ym;i++)
  100. {
  101. int pos = rowIndexVec.coeffRef(R(i));
  102. if(pos != i)
  103. {
  104. int& val = rowPermVec.coeffRef(i);
  105. std::swap(rowIndexVec.coeffRef(val),rowIndexVec.coeffRef(R(i)));
  106. std::swap(rowPermVec.coeffRef(i),rowPermVec.coeffRef(pos));
  107. }
  108. }
  109. Eigen::PermutationMatrix<Eigen::Dynamic,Eigen::Dynamic,int> rowPerm(rowIndexVec);
  110. Eigen::VectorXi colIndexVec = Eigen::VectorXi::LinSpaced(xn,0,xn-1);
  111. Eigen::VectorXi colPermVec = Eigen::VectorXi::LinSpaced(xn,0,xn-1);
  112. for(int i=0;i<yn;i++)
  113. {
  114. int pos = colIndexVec.coeffRef(C(i));
  115. if(pos != i)
  116. {
  117. int& val = colPermVec.coeffRef(i);
  118. std::swap(colIndexVec.coeffRef(val),colIndexVec.coeffRef(C(i)));
  119. std::swap(colPermVec.coeffRef(i),colPermVec.coeffRef(pos));
  120. }
  121. }
  122. Eigen::PermutationMatrix<Eigen::Dynamic,Eigen::Dynamic,int> colPerm(colPermVec);
  123. Eigen::SparseMatrix<T> M = (rowPerm * X);
  124. Y = (M * colPerm).block(0,0,ym,yn);
  125. #endif
  126. }
  127. template <typename Mat>
  128. IGL_INLINE void igl::slice(
  129. const Mat& X,
  130. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  131. const int dim,
  132. Mat& Y)
  133. {
  134. Eigen::VectorXi C;
  135. switch(dim)
  136. {
  137. case 1:
  138. // boring base case
  139. if(X.cols() == 0)
  140. {
  141. Y.resize(R.size(),0);
  142. return;
  143. }
  144. igl::colon(0,X.cols()-1,C);
  145. return slice(X,R,C,Y);
  146. case 2:
  147. // boring base case
  148. if(X.rows() == 0)
  149. {
  150. Y.resize(0,R.size());
  151. return;
  152. }
  153. igl::colon(0,X.rows()-1,C);
  154. return slice(X,C,R,Y);
  155. default:
  156. assert(false && "Unsupported dimension");
  157. return;
  158. }
  159. }
  160. template <typename DerivedX>
  161. IGL_INLINE void igl::slice(
  162. const Eigen::PlainObjectBase<DerivedX> & X,
  163. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  164. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  165. Eigen::PlainObjectBase<DerivedX> & Y)
  166. {
  167. #ifndef NDEBUG
  168. int xm = X.rows();
  169. int xn = X.cols();
  170. #endif
  171. int ym = R.size();
  172. int yn = C.size();
  173. // special case when R or C is empty
  174. if(ym == 0 || yn == 0)
  175. {
  176. Y.resize(ym,yn);
  177. return;
  178. }
  179. assert(R.minCoeff() >= 0);
  180. assert(R.maxCoeff() < xm);
  181. assert(C.minCoeff() >= 0);
  182. assert(C.maxCoeff() < xn);
  183. // Resize output
  184. Y.resize(ym,yn);
  185. // loop over output rows, then columns
  186. for(int i = 0;i<ym;i++)
  187. {
  188. for(int j = 0;j<yn;j++)
  189. {
  190. Y(i,j) = X(R(i),C(j));
  191. }
  192. }
  193. }
  194. template <typename DerivedX>
  195. IGL_INLINE void igl::slice_mask(
  196. const Eigen::PlainObjectBase<DerivedX> & X,
  197. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  198. const Eigen::Array<bool,Eigen::Dynamic,1> & C,
  199. Eigen::PlainObjectBase<DerivedX> & Y)
  200. {
  201. int xm = X.rows();
  202. int xn = X.cols();
  203. int ym = R.count();
  204. int yn = C.count();
  205. assert(R.size() == X.rows() && "R.size() should match X.rows()");
  206. assert(C.size() == X.cols() && "C.size() should match X.cols()");
  207. Y.resize(ym,yn);
  208. {
  209. int yi = 0;
  210. for(int i = 0;i<xm;i++)
  211. {
  212. if(R(i))
  213. {
  214. int yj = 0;
  215. for(int j = 0;j<xn;j++)
  216. {
  217. if(C(j))
  218. {
  219. Y(yi,yj) = X(i,j);
  220. yj++;
  221. }
  222. }
  223. yi++;
  224. }
  225. }
  226. }
  227. }
  228. template <typename DerivedX>
  229. IGL_INLINE void igl::slice_mask(
  230. const Eigen::PlainObjectBase<DerivedX> & X,
  231. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  232. const int dim,
  233. Eigen::PlainObjectBase<DerivedX> & Y)
  234. {
  235. switch(dim)
  236. {
  237. case 1:
  238. {
  239. const int ym = R.count();
  240. Y.resize(ym,X.cols());
  241. assert(X.rows() == R.size() && "X.rows() should match R.size()");
  242. {
  243. int yi = 0;
  244. for(int i = 0;i<X.rows();i++)
  245. {
  246. if(R(i))
  247. {
  248. Y.row(yi++) = X.row(i);
  249. }
  250. }
  251. }
  252. return;
  253. }
  254. case 2:
  255. {
  256. const auto & C = R;
  257. const int yn = C.count();
  258. Y.resize(X.rows(),yn);
  259. assert(X.cols() == R.size() && "X.cols() should match R.size()");
  260. {
  261. int yj = 0;
  262. for(int j = 0;j<X.cols();j++)
  263. {
  264. if(C(j))
  265. {
  266. Y.col(yj++) = X.col(j);
  267. }
  268. }
  269. }
  270. return;
  271. }
  272. default:
  273. assert(false && "Unsupported dimension");
  274. return;
  275. }
  276. }
  277. template <typename DerivedX>
  278. IGL_INLINE void igl::slice(
  279. const Eigen::PlainObjectBase<DerivedX> & X,
  280. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  281. Eigen::PlainObjectBase<DerivedX> & Y)
  282. {
  283. // phony column indices
  284. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  285. C.resize(1);
  286. C(0) = 0;
  287. return igl::slice(X,R,C,Y);
  288. }
  289. template <typename DerivedX>
  290. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  291. const Eigen::PlainObjectBase<DerivedX> & X,
  292. const Eigen::Matrix<int,Eigen::Dynamic,1> & R)
  293. {
  294. Eigen::PlainObjectBase<DerivedX> Y;
  295. igl::slice(X,R,Y);
  296. return Y;
  297. }
  298. template <typename DerivedX>
  299. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  300. const Eigen::PlainObjectBase<DerivedX>& X,
  301. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  302. const int dim)
  303. {
  304. Eigen::PlainObjectBase<DerivedX> Y;
  305. igl::slice(X,R,dim,Y);
  306. return Y;
  307. }
  308. template <typename DerivedX>
  309. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  310. const Eigen::PlainObjectBase<DerivedX> & X,
  311. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  312. const Eigen::Array<bool,Eigen::Dynamic,1> & C)
  313. {
  314. Eigen::PlainObjectBase<DerivedX> Y;
  315. igl::slice_mask(X,R,C,Y);
  316. return Y;
  317. }
  318. template <typename DerivedX>
  319. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  320. const Eigen::PlainObjectBase<DerivedX>& X,
  321. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  322. const int dim)
  323. {
  324. Eigen::PlainObjectBase<DerivedX> Y;
  325. igl::slice_mask(X,R,dim,Y);
  326. return Y;
  327. }
  328. #ifdef IGL_STATIC_LIBRARY
  329. // Explicit template specialization
  330. // generated by autoexplicit.sh
  331. 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> >&);
  332. // generated by autoexplicit.sh
  333. 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> >&);
  334. // generated by autoexplicit.sh
  335. 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> >&);
  336. // generated by autoexplicit.sh
  337. 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> >&);
  338. // generated by autoexplicit.sh
  339. 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>&);
  340. 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> >&);
  341. 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> >&);
  342. 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>&);
  343. 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>&);
  344. 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);
  345. 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);
  346. 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>&);
  347. 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>&);
  348. 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&);
  349. 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);
  350. 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);
  351. 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);
  352. #endif