slice.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // Build reindexing maps for columns and rows, -1 means not in map
  36. std::vector<std::vector<int> > RI;
  37. RI.resize(xm);
  38. for(int i = 0;i<ym;i++)
  39. {
  40. RI[R(i)].push_back(i);
  41. }
  42. std::vector<std::vector<int> > CI;
  43. CI.resize(xn);
  44. // initialize to -1
  45. for(int i = 0;i<yn;i++)
  46. {
  47. CI[C(i)].push_back(i);
  48. }
  49. // Resize output
  50. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_Y(ym,yn);
  51. // Take a guess at the number of nonzeros (this assumes uniform distribution
  52. // not banded or heavily diagonal)
  53. dyn_Y.reserve((X.nonZeros()/(X.rows()*X.cols())) * (ym*yn));
  54. // Iterate over outside
  55. for(int k=0; k<X.outerSize(); ++k)
  56. {
  57. // Iterate over inside
  58. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  59. {
  60. std::vector<int>::iterator rit, cit;
  61. for(rit = RI[it.row()].begin();rit != RI[it.row()].end(); rit++)
  62. {
  63. for(cit = CI[it.col()].begin();cit != CI[it.col()].end(); cit++)
  64. {
  65. dyn_Y.coeffRef(*rit,*cit) = it.value();
  66. }
  67. }
  68. }
  69. }
  70. Y = Eigen::SparseMatrix<T>(dyn_Y);
  71. }
  72. template <typename Mat>
  73. IGL_INLINE void igl::slice(
  74. const Mat& X,
  75. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  76. const int dim,
  77. Mat& Y)
  78. {
  79. Eigen::VectorXi C;
  80. switch(dim)
  81. {
  82. case 1:
  83. // boring base case
  84. if(X.cols() == 0)
  85. {
  86. Y.resize(R.size(),0);
  87. return;
  88. }
  89. igl::colon(0,X.cols()-1,C);
  90. return slice(X,R,C,Y);
  91. case 2:
  92. // boring base case
  93. if(X.rows() == 0)
  94. {
  95. Y.resize(0,R.size());
  96. return;
  97. }
  98. igl::colon(0,X.rows()-1,C);
  99. return slice(X,C,R,Y);
  100. default:
  101. assert(false && "Unsupported dimension");
  102. return;
  103. }
  104. }
  105. template <typename DerivedX>
  106. IGL_INLINE void igl::slice(
  107. const Eigen::PlainObjectBase<DerivedX> & X,
  108. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  109. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  110. Eigen::PlainObjectBase<DerivedX> & Y)
  111. {
  112. #ifndef NDEBUG
  113. int xm = X.rows();
  114. int xn = X.cols();
  115. #endif
  116. int ym = R.size();
  117. int yn = C.size();
  118. // special case when R or C is empty
  119. if(ym == 0 || yn == 0)
  120. {
  121. Y.resize(ym,yn);
  122. return;
  123. }
  124. assert(R.minCoeff() >= 0);
  125. assert(R.maxCoeff() < xm);
  126. assert(C.minCoeff() >= 0);
  127. assert(C.maxCoeff() < xn);
  128. // Resize output
  129. Y.resize(ym,yn);
  130. // loop over output rows, then columns
  131. for(int i = 0;i<ym;i++)
  132. {
  133. for(int j = 0;j<yn;j++)
  134. {
  135. Y(i,j) = X(R(i),C(j));
  136. }
  137. }
  138. }
  139. template <typename DerivedX>
  140. IGL_INLINE void igl::slice(
  141. const Eigen::PlainObjectBase<DerivedX> & X,
  142. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  143. Eigen::PlainObjectBase<DerivedX> & Y)
  144. {
  145. // phony column indices
  146. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  147. C.resize(1);
  148. C(0) = 0;
  149. return igl::slice(X,R,C,Y);
  150. }
  151. template <typename DerivedX>
  152. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  153. const Eigen::PlainObjectBase<DerivedX> & X,
  154. const Eigen::Matrix<int,Eigen::Dynamic,1> & R)
  155. {
  156. Eigen::PlainObjectBase<DerivedX> Y;
  157. igl::slice(X,R,Y);
  158. return Y;
  159. }
  160. template <typename DerivedX>
  161. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  162. const Eigen::PlainObjectBase<DerivedX>& X,
  163. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  164. const int dim)
  165. {
  166. Eigen::PlainObjectBase<DerivedX> Y;
  167. // phony column indices
  168. igl::slice(X,R,dim,Y);
  169. return Y;
  170. }
  171. #ifdef IGL_STATIC_LIBRARY
  172. // Explicit template specialization
  173. // generated by autoexplicit.sh
  174. 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> >&);
  175. // generated by autoexplicit.sh
  176. 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> >&);
  177. // generated by autoexplicit.sh
  178. 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> >&);
  179. // generated by autoexplicit.sh
  180. 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> >&);
  181. // generated by autoexplicit.sh
  182. 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>&);
  183. 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> >&);
  184. 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> >&);
  185. 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>&);
  186. 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>&);
  187. 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);
  188. 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);
  189. 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>&);
  190. template void igl::slice<std::__1::complex<double> >(Eigen::SparseMatrix<std::__1::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::__1::complex<double>, 0, int>&);
  191. #endif