slice.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. #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_mask(
  195. const Eigen::PlainObjectBase<DerivedX> & X,
  196. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  197. const Eigen::Array<bool,Eigen::Dynamic,1> & C,
  198. Eigen::PlainObjectBase<DerivedX> & Y)
  199. {
  200. int xm = X.rows();
  201. int xn = X.cols();
  202. int ym = R.count();
  203. int yn = C.count();
  204. assert(R.size() == X.rows() && "R.size() should match X.rows()");
  205. assert(C.size() == X.cols() && "C.size() should match X.cols()");
  206. Y.resize(ym,yn);
  207. {
  208. int yi = 0;
  209. for(int i = 0;i<xm;i++)
  210. {
  211. if(R(i))
  212. {
  213. int yj = 0;
  214. for(int j = 0;j<xn;j++)
  215. {
  216. if(C(j))
  217. {
  218. Y(yi,yj) = X(i,j);
  219. yj++;
  220. }
  221. }
  222. yi++;
  223. }
  224. }
  225. }
  226. }
  227. template <typename DerivedX>
  228. IGL_INLINE void igl::slice_mask(
  229. const Eigen::PlainObjectBase<DerivedX> & X,
  230. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  231. const int dim,
  232. Eigen::PlainObjectBase<DerivedX> & Y)
  233. {
  234. switch(dim)
  235. {
  236. case 1:
  237. {
  238. const int ym = R.count();
  239. Y.resize(ym,X.cols());
  240. assert(X.rows() == R.size() && "X.rows() should match R.size()");
  241. {
  242. int yi = 0;
  243. for(int i = 0;i<X.rows();i++)
  244. {
  245. if(R(i))
  246. {
  247. Y.row(yi++) = X.row(i);
  248. }
  249. }
  250. }
  251. return;
  252. }
  253. case 2:
  254. {
  255. const auto & C = R;
  256. const int yn = C.count();
  257. Y.resize(X.rows(),yn);
  258. assert(X.cols() == R.size() && "X.cols() should match R.size()");
  259. {
  260. int yj = 0;
  261. for(int j = 0;j<X.cols();j++)
  262. {
  263. if(C(j))
  264. {
  265. Y.col(yj++) = X.col(j);
  266. }
  267. }
  268. }
  269. return;
  270. }
  271. default:
  272. assert(false && "Unsupported dimension");
  273. return;
  274. }
  275. }
  276. template <typename DerivedX>
  277. IGL_INLINE void igl::slice(
  278. const Eigen::PlainObjectBase<DerivedX> & X,
  279. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  280. Eigen::PlainObjectBase<DerivedX> & Y)
  281. {
  282. // phony column indices
  283. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  284. C.resize(1);
  285. C(0) = 0;
  286. return igl::slice(X,R,C,Y);
  287. }
  288. template <typename DerivedX>
  289. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  290. const Eigen::PlainObjectBase<DerivedX> & X,
  291. const Eigen::Matrix<int,Eigen::Dynamic,1> & R)
  292. {
  293. Eigen::PlainObjectBase<DerivedX> Y;
  294. igl::slice(X,R,Y);
  295. return Y;
  296. }
  297. template <typename DerivedX>
  298. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  299. const Eigen::PlainObjectBase<DerivedX>& X,
  300. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  301. const int dim)
  302. {
  303. Eigen::PlainObjectBase<DerivedX> Y;
  304. igl::slice(X,R,dim,Y);
  305. return Y;
  306. }
  307. template <typename DerivedX>
  308. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  309. const Eigen::PlainObjectBase<DerivedX> & X,
  310. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  311. const Eigen::Array<bool,Eigen::Dynamic,1> & C)
  312. {
  313. Eigen::PlainObjectBase<DerivedX> Y;
  314. igl::slice_mask(X,R,C,Y);
  315. return Y;
  316. }
  317. template <typename DerivedX>
  318. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  319. const Eigen::PlainObjectBase<DerivedX>& X,
  320. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  321. const int dim)
  322. {
  323. Eigen::PlainObjectBase<DerivedX> Y;
  324. igl::slice_mask(X,R,dim,Y);
  325. return Y;
  326. }
  327. #ifdef IGL_STATIC_LIBRARY
  328. // Explicit template specialization
  329. // generated by autoexplicit.sh
  330. 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> >&);
  331. // generated by autoexplicit.sh
  332. 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> >&);
  333. // generated by autoexplicit.sh
  334. 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> >&);
  335. // generated by autoexplicit.sh
  336. 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> >&);
  337. // generated by autoexplicit.sh
  338. 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>&);
  339. 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> >&);
  340. 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> >&);
  341. 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>&);
  342. 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>&);
  343. 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);
  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 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>&);
  346. 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>&);
  347. 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&);
  348. 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);
  349. 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);
  350. 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);
  351. #endif