slice.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include "slice.h"
  2. #include "colon.h"
  3. #include <vector>
  4. // Bug in unsupported/Eigen/SparseExtra needs iostream first
  5. #include <iostream>
  6. #include <unsupported/Eigen/SparseExtra>
  7. template <typename T>
  8. IGL_INLINE void igl::slice(
  9. const Eigen::SparseMatrix<T>& X,
  10. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  11. const Eigen::Matrix<int,Eigen::Dynamic,1> & C,
  12. Eigen::SparseMatrix<T>& Y)
  13. {
  14. int xm = X.rows();
  15. int xn = X.cols();
  16. int ym = R.size();
  17. int yn = C.size();
  18. // special case when R or C is empty
  19. if(ym == 0 || yn == 0)
  20. {
  21. Y.resize(ym,yn);
  22. return;
  23. }
  24. assert(R.minCoeff() >= 0);
  25. assert(R.maxCoeff() < xm);
  26. assert(C.minCoeff() >= 0);
  27. assert(C.maxCoeff() < xn);
  28. // Build reindexing maps for columns and rows, -1 means not in map
  29. std::vector<std::vector<int> > RI;
  30. RI.resize(xm);
  31. for(int i = 0;i<ym;i++)
  32. {
  33. RI[R(i)].push_back(i);
  34. }
  35. std::vector<std::vector<int> > CI;
  36. CI.resize(xn);
  37. // initialize to -1
  38. for(int i = 0;i<yn;i++)
  39. {
  40. CI[C(i)].push_back(i);
  41. }
  42. // Resize output
  43. Eigen::DynamicSparseMatrix<T, Eigen::RowMajor> dyn_Y(ym,yn);
  44. // Take a guess at the number of nonzeros (this assumes uniform distribution
  45. // not banded or heavily diagonal)
  46. dyn_Y.reserve((X.nonZeros()/(X.rows()*X.cols())) * (ym*yn));
  47. // Iterate over outside
  48. for(int k=0; k<X.outerSize(); ++k)
  49. {
  50. // Iterate over inside
  51. for(typename Eigen::SparseMatrix<T>::InnerIterator it (X,k); it; ++it)
  52. {
  53. std::vector<int>::iterator rit, cit;
  54. for(rit = RI[it.row()].begin();rit != RI[it.row()].end(); rit++)
  55. {
  56. for(cit = CI[it.col()].begin();cit != CI[it.col()].end(); cit++)
  57. {
  58. dyn_Y.coeffRef(*rit,*cit) = it.value();
  59. }
  60. }
  61. }
  62. }
  63. Y = Eigen::SparseMatrix<T>(dyn_Y);
  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(
  134. const Eigen::PlainObjectBase<DerivedX> & X,
  135. const Eigen::Matrix<int,Eigen::Dynamic,1> & R,
  136. Eigen::PlainObjectBase<DerivedX> & Y)
  137. {
  138. // phony column indices
  139. Eigen::Matrix<int,Eigen::Dynamic,1> C;
  140. C.resize(1);
  141. C(0) = 0;
  142. return igl::slice(X,R,C,Y);
  143. }
  144. template <typename DerivedX>
  145. IGL_INLINE Eigen::PlainObjectBase<DerivedX> igl::slice(
  146. const Eigen::PlainObjectBase<DerivedX> & X,
  147. const Eigen::Matrix<int,Eigen::Dynamic,1> & R)
  148. {
  149. Eigen::PlainObjectBase<DerivedX> Y;
  150. // phony column indices
  151. igl::slice(X,R,Y);
  152. return Y;
  153. }
  154. #ifndef IGL_HEADER_ONLY
  155. // Explicit template specialization
  156. // generated by autoexplicit.sh
  157. 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> >&);
  158. // generated by autoexplicit.sh
  159. 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> >&);
  160. // generated by autoexplicit.sh
  161. 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> >&);
  162. // generated by autoexplicit.sh
  163. 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> >&);
  164. // generated by autoexplicit.sh
  165. 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>&);
  166. 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> >&);
  167. 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> >&);
  168. 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>&);
  169. 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>&);
  170. #endif