columnize.cpp 2.3 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 "columnize.h"
  9. #include <cassert>
  10. template <typename DerivedA, typename DerivedB>
  11. IGL_INLINE void igl::columnize(
  12. const Eigen::PlainObjectBase<DerivedA> & A,
  13. const int k,
  14. const int dim,
  15. Eigen::PlainObjectBase<DerivedB> & B)
  16. {
  17. // Eigen matrices must be 2d so dim must be only 1 or 2
  18. assert(dim == 1 || dim == 2);
  19. // block height, width, and number of blocks
  20. int m,n;
  21. if(dim == 1)
  22. {
  23. m = A.rows()/k;
  24. assert(m*(int)k == (int)A.rows());
  25. n = A.cols();
  26. }else// dim == 2
  27. {
  28. m = A.rows();
  29. n = A.cols()/k;
  30. assert(n*(int)k == (int)A.cols());
  31. }
  32. // resize output
  33. B.resize(A.rows()*A.cols(),1);
  34. for(int b = 0;b<(int)k;b++)
  35. {
  36. for(int i = 0;i<m;i++)
  37. {
  38. for(int j = 0;j<n;j++)
  39. {
  40. if(dim == 1)
  41. {
  42. B(j*m*k+i*k+b) = A(i+b*m,j);
  43. }else
  44. {
  45. B(j*m*k+i*k+b) = A(i,b*n+j);
  46. }
  47. }
  48. }
  49. }
  50. }
  51. #ifdef IGL_STATIC_LIBRARY
  52. // Explicit template specialization
  53. template void igl::columnize<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::columnize<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::columnize<Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&);
  56. template void igl::columnize<Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<float, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> > const&, int, int, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 1, 0, -1, 1> >&);
  57. #endif