slice.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. switch(dim)
  174. {
  175. case 1:
  176. {
  177. const int ym = R.count();
  178. Y.resize(ym,X.cols());
  179. assert(X.rows() == R.size() && "X.rows() should match R.size()");
  180. {
  181. int yi = 0;
  182. for(int i = 0;i<X.rows();i++)
  183. {
  184. if(R(i))
  185. {
  186. Y.row(yi++) = X.row(i);
  187. }
  188. }
  189. }
  190. return;
  191. }
  192. case 2:
  193. {
  194. const auto & C = R;
  195. const int yn = C.count();
  196. Y.resize(X.rows(),yn);
  197. assert(X.cols() == R.size() && "X.cols() should match R.size()");
  198. {
  199. int yj = 0;
  200. for(int j = 0;j<X.cols();j++)
  201. {
  202. if(C(j))
  203. {
  204. Y.col(yj++) = X.col(j);
  205. }
  206. }
  207. }
  208. return;
  209. }
  210. default:
  211. assert(false && "Unsupported dimension");
  212. return;
  213. }
  214. }
  215. template <typename DerivedX>
  216. IGL_INLINE void igl::slice(
  217. const Eigen::PlainObjectBase<DerivedX> & X,
  218. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  219. Eigen::PlainObjectBase<DerivedX> & Y)
  220. {
  221. // phony column indices
  222. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  223. C.resize(1);
  224. C(0) = 0;
  225. return igl::slice(X,R,C,Y);
  226. }
  227. template <typename DerivedX>
  228. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  229. const Eigen::PlainObjectBase<DerivedX> & X,
  230. const Eigen::Matrix<int,Eigen::Dynamic,1> & R)
  231. {
  232. Eigen::PlainObjectBase<DerivedX> Y;
  233. igl::slice(X,R,Y);
  234. return Y;
  235. }
  236. template <typename DerivedX>
  237. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  238. const Eigen::PlainObjectBase<DerivedX>& X,
  239. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  240. const int dim)
  241. {
  242. Eigen::PlainObjectBase<DerivedX> Y;
  243. igl::slice(X,R,dim,Y);
  244. return Y;
  245. }
  246. template <typename DerivedX>
  247. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  248. const Eigen::PlainObjectBase<DerivedX> & X,
  249. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  250. const Eigen::Array<bool,Eigen::Dynamic,1> & C)
  251. {
  252. Eigen::PlainObjectBase<DerivedX> Y;
  253. igl::slice_mask(X,R,C,Y);
  254. return Y;
  255. }
  256. template <typename DerivedX>
  257. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice_mask(
  258. const Eigen::PlainObjectBase<DerivedX>& X,
  259. const Eigen::Array<bool,Eigen::Dynamic,1> & R,
  260. const int dim)
  261. {
  262. Eigen::PlainObjectBase<DerivedX> Y;
  263. igl::slice_mask(X,R,dim,Y);
  264. return Y;
  265. }
  266. #ifdef IGL_STATIC_LIBRARY
  267. // Explicit template specialization
  268. // generated by autoexplicit.sh
  269. 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> >&);
  270. // generated by autoexplicit.sh
  271. 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> >&);
  272. // generated by autoexplicit.sh
  273. 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> >&);
  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::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<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>&);
  278. 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> >&);
  279. 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> >&);
  280. 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>&);
  281. 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>&);
  282. 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);
  283. 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);
  284. 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>&);
  285. 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>&);
  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&);
  287. 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);
  288. 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);
  289. 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);
  290. #endif