slice.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. printf("slice: 1\n");
  80. Y.resize(0,R.cols());
  81. // Y.resize(R.rows(),0);
  82. return;
  83. }
  84. igl::colon(0,X.cols()-1,C);
  85. return slice(X,R,C,Y);
  86. case 2:
  87. // boring base case
  88. if(X.rows() == 0)
  89. {
  90. printf("slice: 2\n");
  91. Y.resize(R.rows(),0);
  92. // Y.resize(0,R.cols());
  93. return;
  94. }
  95. igl::colon(0,X.rows()-1,C);
  96. return slice(X,C,R,Y);
  97. default:
  98. assert(false && "Unsupported dimension");
  99. return;
  100. }
  101. }
  102. template <typename DerivedX>
  103. IGL_INLINE void igl::slice(
  104. const Eigen::PlainObjectBase<DerivedX> & X,
  105. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  106. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  107. Eigen::PlainObjectBase<DerivedX> & Y)
  108. {
  109. #ifndef NDEBUG
  110. int xm = X.rows();
  111. int xn = X.cols();
  112. #endif
  113. int ym = R.size();
  114. int yn = C.size();
  115. // special case when R or C is empty
  116. if(ym == 0 || yn == 0)
  117. {
  118. Y.resize(ym,yn);
  119. return;
  120. }
  121. assert(R.minCoeff() >= 0);
  122. assert(R.maxCoeff() < xm);
  123. assert(C.minCoeff() >= 0);
  124. assert(C.maxCoeff() < xn);
  125. // Resize output
  126. Y.resize(ym,yn);
  127. // loop over output rows, then columns
  128. for(int i = 0;i<ym;i++)
  129. {
  130. for(int j = 0;j<yn;j++)
  131. {
  132. Y(i,j) = X(R(i),C(j));
  133. }
  134. }
  135. }
  136. template <typename DerivedX>
  137. IGL_INLINE void igl::slice_mask(
  138. const Eigen::PlainObjectBase<DerivedX> & X,
  139. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  140. const Eigen::Array<bool,Eigen::Dynamic,1> & C,
  141. Eigen::PlainObjectBase<DerivedX> & Y)
  142. {
  143. int xm = X.rows();
  144. int xn = X.cols();
  145. int ym = R.count();
  146. int yn = C.count();
  147. assert(R.size() == X.rows() && "R.size() should match X.rows()");
  148. assert(C.size() == X.cols() && "C.size() should match X.cols()");
  149. Y.resize(ym,yn);
  150. {
  151. int yi = 0;
  152. for(int i = 0;i<xm;i++)
  153. {
  154. if(R(i))
  155. {
  156. int yj = 0;
  157. for(int j = 0;j<xn;j++)
  158. {
  159. if(C(j))
  160. {
  161. Y(yi,yj) = X(i,j);
  162. yj++;
  163. }
  164. }
  165. yi++;
  166. }
  167. }
  168. }
  169. }
  170. template <typename DerivedX>
  171. IGL_INLINE void igl::slice_mask(
  172. const Eigen::PlainObjectBase<DerivedX> & X,
  173. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  174. const int dim,
  175. Eigen::PlainObjectBase<DerivedX> & Y)
  176. {
  177. switch(dim)
  178. {
  179. case 1:
  180. {
  181. const int ym = R.count();
  182. Y.resize(ym,X.cols());
  183. assert(X.rows() == R.size() && "X.rows() should match R.size()");
  184. {
  185. int yi = 0;
  186. for(int i = 0;i<X.rows();i++)
  187. {
  188. if(R(i))
  189. {
  190. Y.row(yi++) = X.row(i);
  191. }
  192. }
  193. }
  194. return;
  195. }
  196. case 2:
  197. {
  198. const auto & C = R;
  199. const int yn = C.count();
  200. Y.resize(X.rows(),yn);
  201. assert(X.cols() == R.size() && "X.cols() should match R.size()");
  202. {
  203. int yj = 0;
  204. for(int j = 0;j<X.cols();j++)
  205. {
  206. if(C(j))
  207. {
  208. Y.col(yj++) = X.col(j);
  209. }
  210. }
  211. }
  212. return;
  213. }
  214. default:
  215. assert(false && "Unsupported dimension");
  216. return;
  217. }
  218. }
  219. template <typename DerivedX>
  220. IGL_INLINE void igl::slice(
  221. const Eigen::PlainObjectBase<DerivedX> & X,
  222. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  223. Eigen::PlainObjectBase<DerivedX> & Y)
  224. {
  225. // phony column indices
  226. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  227. C.resize(1);
  228. C(0) = 0;
  229. return igl::slice(X,R,C,Y);
  230. }
  231. template <typename DerivedX>
  232. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  233. const Eigen::PlainObjectBase<DerivedX> & X,
  234. const Eigen::Matrix<int,Eigen::Dynamic,1> & R)
  235. {
  236. Eigen::PlainObjectBase<DerivedX> Y;
  237. igl::slice(X,R,Y);
  238. return Y;
  239. }
  240. template <typename DerivedX>
  241. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  242. const Eigen::PlainObjectBase<DerivedX>& X,
  243. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  244. const int dim)
  245. {
  246. Eigen::PlainObjectBase<DerivedX> Y;
  247. igl::slice(X,R,dim,Y);
  248. return Y;
  249. }
  250. template <typename DerivedX>
  251. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  252. const Eigen::PlainObjectBase<DerivedX> & X,
  253. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  254. const Eigen::Array<bool,Eigen::Dynamic,1> & C)
  255. {
  256. Eigen::PlainObjectBase<DerivedX> Y;
  257. igl::slice_mask(X,R,C,Y);
  258. return Y;
  259. }
  260. template <typename DerivedX>
  261. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  262. const Eigen::PlainObjectBase<DerivedX>& X,
  263. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  264. const int dim)
  265. {
  266. Eigen::PlainObjectBase<DerivedX> Y;
  267. igl::slice_mask(X,R,dim,Y);
  268. return Y;
  269. }
  270. #ifdef IGL_STATIC_LIBRARY
  271. // Explicit template specialization
  272. // generated by autoexplicit.sh
  273. 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> >&);
  274. // generated by autoexplicit.sh
  275. 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> >&);
  276. // generated by autoexplicit.sh
  277. 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> >&);
  278. // generated by autoexplicit.sh
  279. 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> >&);
  280. // generated by autoexplicit.sh
  281. 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>&);
  282. 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> >&);
  283. 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> >&);
  284. 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>&);
  285. 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>&);
  286. 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);
  287. 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);
  288. 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>&);
  289. 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>&);
  290. 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&);
  291. 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);
  292. 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);
  293. 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);
  294. 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>&);
  295. #endif