transpose_blocks.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 "transpose_blocks.h"
  9. #include <cassert>
  10. template <typename T>
  11. IGL_INLINE void igl::transpose_blocks(
  12. const Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & A,
  13. const size_t k,
  14. const size_t dim,
  15. Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic> & B)
  16. {
  17. // Eigen matrices must be 2d so dim must be only 1 or 2
  18. assert(dim == 1 || dim == 2);
  19. // Output is not allowed to be input
  20. assert(&A != &B);
  21. // block height, width, and number of blocks
  22. int m,n;
  23. if(dim == 1)
  24. {
  25. m = A.rows()/k;
  26. n = A.cols();
  27. }else// dim == 2
  28. {
  29. m = A.rows();
  30. n = A.cols()/k;
  31. }
  32. // resize output
  33. if(dim == 1)
  34. {
  35. B.resize(n*k,m);
  36. }else//dim ==2
  37. {
  38. B.resize(n,m*k);
  39. }
  40. // loop over blocks
  41. for(int b = 0;b<(int)k;b++)
  42. {
  43. if(dim == 1)
  44. {
  45. B.block(b*n,0,n,m) = A.block(b*m,0,m,n).transpose();
  46. }else//dim ==2
  47. {
  48. B.block(0,b*m,n,m) = A.block(0,b*n,m,n).transpose();
  49. }
  50. }
  51. }
  52. #ifdef IGL_STATIC_LIBRARY
  53. // Explicit template specialization
  54. // generated by autoexplicit.sh
  55. template void igl::transpose_blocks<double>(Eigen::Matrix<double, -1, -1, 0, -1, -1> const&, size_t, size_t, Eigen::Matrix<double, -1, -1, 0, -1, -1>&);
  56. #endif