slice.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. int xm = X.rows();
  22. int xn = X.cols();
  23. int ym = R.size();
  24. int yn = C.size();
  25. // special case when R or C is empty
  26. if(ym == 0 || yn == 0)
  27. {
  28. Y.resize(ym,yn);
  29. return;
  30. }
  31. assert(R.minCoeff() >= 0);
  32. assert(R.maxCoeff() < xm);
  33. assert(C.minCoeff() >= 0);
  34. assert(C.maxCoeff() < xn);
  35. // initialize row and col permutation vectors
  36. Eigen::VectorXi rowIndexVec = Eigen::VectorXi::LinSpaced(xm,0,xm);
  37. Eigen::VectorXi rowPermVec = Eigen::VectorXi::LinSpaced(xm,0,xm);
  38. for(int i=0;i<ym;i++)
  39. {
  40. int pos = rowIndexVec.coeffRef(R(i));
  41. if(pos != i)
  42. {
  43. int& val = rowPermVec.coeffRef(i);
  44. std::swap(rowIndexVec.coeffRef(val),rowIndexVec.coeffRef(R(i)));
  45. std::swap(rowPermVec.coeffRef(i),rowPermVec.coeffRef(pos));
  46. }
  47. }
  48. Eigen::PermutationMatrix<Eigen::Dynamic,Eigen::Dynamic,int> rowPerm(rowIndexVec);
  49. Eigen::VectorXi colIndexVec = Eigen::VectorXi::LinSpaced(xn,0,xn);
  50. Eigen::VectorXi colPermVec = Eigen::VectorXi::LinSpaced(xn,0,xn);
  51. for(int i=0;i<yn;i++)
  52. {
  53. int pos = colIndexVec.coeffRef(C(i));
  54. if(pos != i)
  55. {
  56. int& val = colPermVec.coeffRef(i);
  57. std::swap(colIndexVec.coeffRef(val),colIndexVec.coeffRef(C(i)));
  58. std::swap(colPermVec.coeffRef(i),colPermVec.coeffRef(pos));
  59. }
  60. }
  61. Eigen::PermutationMatrix<Eigen::Dynamic,Eigen::Dynamic,int> colPerm(colPermVec);
  62. Eigen::SparseMatrix<T> M = (rowPerm * X);
  63. Y = (M * colPerm).block(0,0,ym,yn);
  64. }
  65. template <typename Mat>
  66. IGL_INLINE void igl::slice(
  67. const Mat& X,
  68. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  69. const int dim,
  70. Mat& Y)
  71. {
  72. Eigen::VectorXi C;
  73. switch(dim)
  74. {
  75. case 1:
  76. // boring base case
  77. if(X.cols() == 0)
  78. {
  79. Y.resize(R.size(),0);
  80. return;
  81. }
  82. igl::colon(0,X.cols()-1,C);
  83. return slice(X,R,C,Y);
  84. case 2:
  85. // boring base case
  86. if(X.rows() == 0)
  87. {
  88. Y.resize(0,R.size());
  89. return;
  90. }
  91. igl::colon(0,X.rows()-1,C);
  92. return slice(X,C,R,Y);
  93. default:
  94. assert(false && "Unsupported dimension");
  95. return;
  96. }
  97. }
  98. template <typename DerivedX>
  99. IGL_INLINE void igl::slice(
  100. const Eigen::PlainObjectBase<DerivedX> & X,
  101. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  102. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  103. Eigen::PlainObjectBase<DerivedX> & Y)
  104. {
  105. #ifndef NDEBUG
  106. int xm = X.rows();
  107. int xn = X.cols();
  108. #endif
  109. int ym = R.size();
  110. int yn = C.size();
  111. // special case when R or C is empty
  112. if(ym == 0 || yn == 0)
  113. {
  114. Y.resize(ym,yn);
  115. return;
  116. }
  117. assert(R.minCoeff() >= 0);
  118. assert(R.maxCoeff() < xm);
  119. assert(C.minCoeff() >= 0);
  120. assert(C.maxCoeff() < xn);
  121. // Resize output
  122. Y.resize(ym,yn);
  123. // loop over output rows, then columns
  124. for(int i = 0;i<ym;i++)
  125. {
  126. for(int j = 0;j<yn;j++)
  127. {
  128. Y(i,j) = X(R(i),C(j));
  129. }
  130. }
  131. }
  132. template <typename DerivedX>
  133. IGL_INLINE void igl::slice_mask(
  134. const Eigen::PlainObjectBase<DerivedX> & X,
  135. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  136. const Eigen::Array<bool,Eigen::Dynamic,1> & C,
  137. Eigen::PlainObjectBase<DerivedX> & Y)
  138. {
  139. int xm = X.rows();
  140. int xn = X.cols();
  141. int ym = R.count();
  142. int yn = C.count();
  143. assert(R.size() == X.rows() && "R.size() should match X.rows()");
  144. assert(C.size() == X.cols() && "C.size() should match X.cols()");
  145. Y.resize(ym,yn);
  146. {
  147. int yi = 0;
  148. for(int i = 0;i<xm;i++)
  149. {
  150. if(R(i))
  151. {
  152. int yj = 0;
  153. for(int j = 0;j<xn;j++)
  154. {
  155. if(C(j))
  156. {
  157. Y(yi,yj) = X(i,j);
  158. yj++;
  159. }
  160. }
  161. yi++;
  162. }
  163. }
  164. }
  165. }
  166. template <typename DerivedX>
  167. IGL_INLINE void igl::slice_mask(
  168. const Eigen::PlainObjectBase<DerivedX> & X,
  169. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  170. const int dim,
  171. Eigen::PlainObjectBase<DerivedX> & Y)
  172. {
  173. int xm = X.rows();
  174. int xn = X.cols();
  175. switch(dim)
  176. {
  177. case 1:
  178. {
  179. const int ym = R.count();
  180. Y.resize(ym,X.cols());
  181. assert(X.rows() == R.size() && "X.rows() should match R.size()");
  182. {
  183. int yi = 0;
  184. for(int i = 0;i<X.rows();i++)
  185. {
  186. if(R(i))
  187. {
  188. Y.row(yi++) = X.row(i);
  189. }
  190. }
  191. }
  192. return;
  193. }
  194. case 2:
  195. {
  196. const auto & C = R;
  197. const int yn = C.count();
  198. Y.resize(X.rows(),yn);
  199. assert(X.cols() == R.size() && "X.cols() should match R.size()");
  200. {
  201. int yj = 0;
  202. for(int j = 0;j<X.cols();j++)
  203. {
  204. if(C(j))
  205. {
  206. Y.col(yj++) = X.col(j);
  207. }
  208. }
  209. }
  210. return;
  211. }
  212. default:
  213. assert(false && "Unsupported dimension");
  214. return;
  215. }
  216. }
  217. template <typename DerivedX>
  218. IGL_INLINE void igl::slice(
  219. const Eigen::PlainObjectBase<DerivedX> & X,
  220. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  221. Eigen::PlainObjectBase<DerivedX> & Y)
  222. {
  223. // phony column indices
  224. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  225. C.resize(1);
  226. C(0) = 0;
  227. return igl::slice(X,R,C,Y);
  228. }
  229. template <typename DerivedX>
  230. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  231. const Eigen::PlainObjectBase<DerivedX> & X,
  232. const Eigen::Matrix<int,Eigen::Dynamic,1> & R)
  233. {
  234. Eigen::PlainObjectBase<DerivedX> Y;
  235. igl::slice(X,R,Y);
  236. return Y;
  237. }
  238. template <typename DerivedX>
  239. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  240. const Eigen::PlainObjectBase<DerivedX>& X,
  241. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  242. const int dim)
  243. {
  244. Eigen::PlainObjectBase<DerivedX> Y;
  245. igl::slice(X,R,dim,Y);
  246. return Y;
  247. }
  248. template <typename DerivedX>
  249. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  250. const Eigen::PlainObjectBase<DerivedX> & X,
  251. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  252. const Eigen::Array<bool,Eigen::Dynamic,1> & C)
  253. {
  254. Eigen::PlainObjectBase<DerivedX> Y;
  255. igl::slice_mask(X,R,C,Y);
  256. return Y;
  257. }
  258. template <typename DerivedX>
  259. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  260. const Eigen::PlainObjectBase<DerivedX>& X,
  261. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  262. const int dim)
  263. {
  264. Eigen::PlainObjectBase<DerivedX> Y;
  265. igl::slice_mask(X,R,dim,Y);
  266. return Y;
  267. }
  268. #ifdef IGL_STATIC_LIBRARY
  269. // Explicit template specialization
  270. // generated by autoexplicit.sh
  271. 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> >&);
  272. // generated by autoexplicit.sh
  273. 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> >&);
  274. // generated by autoexplicit.sh
  275. 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> >&);
  276. // generated by autoexplicit.sh
  277. 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> >&);
  278. // generated by autoexplicit.sh
  279. 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>&);
  280. 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> >&);
  281. 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> >&);
  282. 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>&);
  283. 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>&);
  284. 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);
  285. 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);
  286. 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>&);
  287. 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>&);
  288. 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&);
  289. 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);
  290. 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);
  291. 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);
  292. #endif