sortrows.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "sortrows.h"
  9. #include "SortableRow.h"
  10. #include "sort.h"
  11. #include "colon.h"
  12. #include "IndexComparison.h"
  13. #include <vector>
  14. // Obsolete slower version converst to vector
  15. //template <typename DerivedX, typename DerivedIX>
  16. //IGL_INLINE void igl::sortrows(
  17. // const Eigen::PlainObjectBase<DerivedX>& X,
  18. // const bool ascending,
  19. // Eigen::PlainObjectBase<DerivedX>& Y,
  20. // Eigen::PlainObjectBase<DerivedIX>& IX)
  21. //{
  22. // using namespace std;
  23. // using namespace Eigen;
  24. // typedef Eigen::Matrix<typename DerivedX::Scalar, Eigen::Dynamic, 1> RowVector;
  25. // vector<SortableRow<RowVector> > rows;
  26. // rows.resize(X.rows());
  27. // // Loop over rows
  28. // for(int i = 0;i<X.rows();i++)
  29. // {
  30. // RowVector ri = X.row(i);
  31. // rows[i] = SortableRow<RowVector>(ri);
  32. // }
  33. // vector<SortableRow<RowVector> > sorted;
  34. // std::vector<size_t> index_map;
  35. // // Perform sort on rows
  36. // igl::sort(rows,ascending,sorted,index_map);
  37. // // Resize output
  38. // Y.resize(X.rows(),X.cols());
  39. // IX.resize(X.rows(),1);
  40. // // Convert to eigen
  41. // for(int i = 0;i<X.rows();i++)
  42. // {
  43. // Y.row(i) = sorted[i].data;
  44. // IX(i,0) = index_map[i];
  45. // }
  46. //}
  47. template <typename DerivedX, typename DerivedIX>
  48. IGL_INLINE void igl::sortrows(
  49. const Eigen::PlainObjectBase<DerivedX>& X,
  50. const bool ascending,
  51. Eigen::PlainObjectBase<DerivedX>& Y,
  52. Eigen::PlainObjectBase<DerivedIX>& IX)
  53. {
  54. using namespace std;
  55. using namespace Eigen;
  56. using namespace igl;
  57. // Resize output
  58. Y.resize(X.rows(),X.cols());
  59. IX.resize(X.rows(),1);
  60. for(int i = 0;i<X.rows();i++)
  61. {
  62. IX(i) = i;
  63. }
  64. std::sort(
  65. IX.data(),
  66. IX.data()+IX.size(),
  67. igl::IndexRowLessThan<const Eigen::PlainObjectBase<DerivedX> & >(X));
  68. // if not ascending then reverse
  69. if(!ascending)
  70. {
  71. std::reverse(IX.data(),IX.data()+IX.size());
  72. }
  73. for(int i = 0;i<X.rows();i++)
  74. {
  75. Y.row(i) = X.row(IX(i));
  76. }
  77. }
  78. #ifndef IGL_HEADER_ONLY
  79. template void igl::sortrows<Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  80. template void igl::sortrows<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  81. template void igl::sortrows<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&);
  82. template void igl::sortrows<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  83. template void igl::sortrows<Eigen::Matrix<double, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> > const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  84. template void igl::sortrows<Eigen::Matrix<int, -1, 2, 0, -1, 2>, Eigen::Matrix<int, -1, 1, 0, -1, 1> >(Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> > const&, bool, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 2, 0, -1, 2> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  85. #endif