sortrows.cpp 3.7 KB

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