repmat.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #ifndef IGL_REPMAT_H
  2. #define IGL_REPMAT_H
  3. #define EIGEN_YES_I_KNOW_SPARSE_MODULE_IS_NOT_STABLE_YET
  4. #include <Eigen/Dense>
  5. #include <Eigen/Sparse>
  6. namespace igl
  7. {
  8. // Ideally this is a super overloaded function that behaves just like
  9. // matlab's repmat
  10. // Replicate and tile a matrix
  11. //
  12. // Templates:
  13. // T should be a eigen matrix primitive type like int or double
  14. // Inputs:
  15. // A m by n input matrix
  16. // r number of row-direction copies
  17. // c number of col-direction copies
  18. // Outputs:
  19. // B r*m by c*n output matrix
  20. //
  21. template <typename T,const int W, const int H>
  22. inline void repmat(
  23. const Eigen::Matrix<T,W,H> & A,
  24. const int r,
  25. const int c,
  26. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B);
  27. }
  28. // Implementation
  29. template <typename T,const int W, const int H>
  30. inline void igl::repmat(
  31. const Eigen::Matrix<T,W,H> & A,
  32. const int r,
  33. const int c,
  34. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B)
  35. {
  36. assert(r>0);
  37. assert(c>0);
  38. // Make room for output
  39. B.resize(r*A.rows(),c*A.cols());
  40. // copy tiled blocks
  41. for(int i = 0;i<r;i++)
  42. {
  43. for(int j = 0;j<c;j++)
  44. {
  45. B.block(i*A.rows(),j*A.cols(),A.rows(),A.cols()) = A;
  46. }
  47. }
  48. }
  49. #endif