slice.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. printf("slice: 1\n");
  141. Y.resize(0,R.cols());
  142. // Y.resize(R.rows(),0);
  143. return;
  144. }
  145. igl::colon(0,X.cols()-1,C);
  146. return slice(X,R,C,Y);
  147. case 2:
  148. // boring base case
  149. if(X.rows() == 0)
  150. {
  151. printf("slice: 2\n");
  152. Y.resize(R.rows(),0);
  153. // Y.resize(0,R.cols());
  154. return;
  155. }
  156. igl::colon(0,X.rows()-1,C);
  157. return slice(X,C,R,Y);
  158. default:
  159. assert(false && "Unsupported dimension");
  160. return;
  161. }
  162. }
  163. template <typename DerivedX>
  164. IGL_INLINE void igl::slice(
  165. const Eigen::PlainObjectBase<DerivedX> & X,
  166. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  167. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  168. Eigen::PlainObjectBase<DerivedX> & Y)
  169. {
  170. #ifndef NDEBUG
  171. int xm = X.rows();
  172. int xn = X.cols();
  173. #endif
  174. int ym = R.size();
  175. int yn = C.size();
  176. // special case when R or C is empty
  177. if(ym == 0 || yn == 0)
  178. {
  179. Y.resize(ym,yn);
  180. return;
  181. }
  182. assert(R.minCoeff() >= 0);
  183. assert(R.maxCoeff() < xm);
  184. assert(C.minCoeff() >= 0);
  185. assert(C.maxCoeff() < xn);
  186. // Resize output
  187. Y.resize(ym,yn);
  188. // loop over output rows, then columns
  189. for(int i = 0;i<ym;i++)
  190. {
  191. for(int j = 0;j<yn;j++)
  192. {
  193. Y(i,j) = X(R(i),C(j));
  194. }
  195. }
  196. }
  197. template <typename DerivedX>
  198. IGL_INLINE void igl::slice(
  199. const Eigen::PlainObjectBase<DerivedX> & X,
  200. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  201. Eigen::PlainObjectBase<DerivedX> & Y)
  202. {
  203. // phony column indices
  204. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  205. C.resize(1);
  206. C(0) = 0;
  207. return igl::slice(X,R,C,Y);
  208. }
  209. template <typename DerivedX>
  210. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  211. const Eigen::PlainObjectBase<DerivedX> & X,
  212. const Eigen::Matrix<int,Eigen::Dynamic,1> & R)
  213. {
  214. Eigen::PlainObjectBase<DerivedX> Y;
  215. igl::slice(X,R,Y);
  216. return Y;
  217. }
  218. template <typename DerivedX>
  219. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  220. const Eigen::PlainObjectBase<DerivedX>& X,
  221. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  222. const int dim)
  223. {
  224. Eigen::PlainObjectBase<DerivedX> Y;
  225. igl::slice(X,R,dim,Y);
  226. return Y;
  227. }
  228. #ifdef IGL_STATIC_LIBRARY
  229. // Explicit template specialization
  230. // generated by autoexplicit.sh
  231. 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> >&);
  232. // generated by autoexplicit.sh
  233. 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> >&);
  234. // generated by autoexplicit.sh
  235. 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> >&);
  236. // generated by autoexplicit.sh
  237. 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> >&);
  238. // generated by autoexplicit.sh
  239. 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>&);
  240. 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> >&);
  241. 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> >&);
  242. 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>&);
  243. 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>&);
  244. 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);
  245. 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);
  246. 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>&);
  247. 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>&);
  248. 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&);
  249. 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);
  250. 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);
  251. 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);
  252. 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>&);
  253. #endif