repmat.cpp 2.3 KB

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