repmat.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[email protected]>
  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::MatrixBase<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, int majorType>
  30. IGL_INLINE void igl::repmat(
  31. const Eigen::SparseMatrix<T, majorType> & A,
  32. const int r,
  33. const int c,
  34. Eigen::SparseMatrix<T, majorType> & B)
  35. {
  36. assert(r>0);
  37. assert(c>0);
  38. B.resize(r*A.rows(), c*A.cols());
  39. std::vector<Eigen::Triplet<T>> b;
  40. b.reserve(r*c*A.nonZeros());
  41. for(int i = 0; i < r; i++)
  42. {
  43. for(int j = 0; j < c; j++)
  44. {
  45. // loop outer level
  46. for (int k = 0; k < A.outerSize(); ++k)
  47. {
  48. // loop inner level
  49. for (typename Eigen::SparseMatrix<T, majorType>::InnerIterator
  50. it(A,k); it; ++it)
  51. {
  52. Eigen::Triplet<T> triplet(i * A.rows() + it.row(), j * A.cols()
  53. + it.col(), it.value());
  54. b.push_back(triplet);
  55. }
  56. }
  57. }
  58. }
  59. B.setFromTriplets(b.begin(), b.end());
  60. }
  61. #ifdef IGL_STATIC_LIBRARY
  62. // Explicit template instantiation
  63. // generated by autoexplicit.sh
  64. template void igl::repmat<double, 0>(Eigen::SparseMatrix<double, 0, int> const&, int, int, Eigen::SparseMatrix<double, 0, int>&);
  65. // generated by autoexplicit.sh
  66. template void igl::repmat<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  67. template void igl::repmat<Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  68. template void igl::repmat<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  69. template void igl::repmat<double, 1>(Eigen::SparseMatrix<double, 1, int> const&, int, int, Eigen::SparseMatrix<double, 1, int>&);
  70. template void igl::repmat<int, 1>(Eigen::SparseMatrix<int, 1, int> const&, int, int, Eigen::SparseMatrix<int, 1, int>&);
  71. template void igl::repmat<float, 1>(Eigen::SparseMatrix<float, 1, int> const&, int, int, Eigen::SparseMatrix<float, 1, int>&);
  72. template void igl::repmat<int, 0>(Eigen::SparseMatrix<int, 0, int> const&, int, int, Eigen::SparseMatrix<int, 0, int>&);
  73. template void igl::repmat<float, 0>(Eigen::SparseMatrix<float, 0, int> const&, int, int, Eigen::SparseMatrix<float, 0, int>&);
  74. #endif