repmat.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "repmat.h"
  2. template <typename DerivedA, typename DerivedB>
  3. IGL_INLINE void igl::repmat(
  4. const Eigen::PlainObjectBase<DerivedA> & A,
  5. const int r,
  6. const int c,
  7. Eigen::PlainObjectBase<DerivedB> & B)
  8. {
  9. assert(r>0);
  10. assert(c>0);
  11. // Make room for output
  12. B.resize(r*A.rows(),c*A.cols());
  13. // copy tiled blocks
  14. for(int i = 0;i<r;i++)
  15. {
  16. for(int j = 0;j<c;j++)
  17. {
  18. B.block(i*A.rows(),j*A.cols(),A.rows(),A.cols()) = A;
  19. }
  20. }
  21. }
  22. template <typename T>
  23. IGL_INLINE void igl::repmat(
  24. const Eigen::SparseMatrix<T> & A,
  25. const int r,
  26. const int c,
  27. Eigen::SparseMatrix<T> & B)
  28. {
  29. assert(r>0);
  30. assert(c>0);
  31. B.resize(r*A.rows(),c*A.cols());
  32. B.reserve(r*c*A.nonZeros());
  33. for(int i = 0;i<r;i++)
  34. {
  35. for(int j = 0;j<c;j++)
  36. {
  37. // Loop outer level
  38. for (int k=0; k<A.outerSize(); ++k)
  39. {
  40. // loop inner level
  41. for (typename Eigen::SparseMatrix<T>::InnerIterator it(A,k); it; ++it)
  42. {
  43. B.insert(i*A.rows()+it.row(),j*A.cols() + it.col()) = it.value();
  44. }
  45. }
  46. }
  47. }
  48. B.finalize();
  49. }
  50. #ifndef IGL_HEADER_ONLY
  51. // Explicit template specialization
  52. // generated by autoexplicit.sh
  53. template void igl::repmat<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  54. template void igl::repmat<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  55. template void igl::repmat<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  56. #endif