sortrows.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "sortrows.h"
  2. #include "SortableRow.h"
  3. #include "sort.h"
  4. #include <vector>
  5. template <typename DerivedX, typename DerivedIX>
  6. IGL_INLINE void igl::sortrows(
  7. const Eigen::PlainObjectBase<DerivedX>& X,
  8. const bool ascending,
  9. Eigen::PlainObjectBase<DerivedX>& Y,
  10. Eigen::PlainObjectBase<DerivedIX>& IX)
  11. {
  12. using namespace std;
  13. using namespace Eigen;
  14. typedef Eigen::Matrix<typename DerivedX::Scalar, Eigen::Dynamic, 1> RowVector;
  15. vector<SortableRow<RowVector> > rows;
  16. rows.resize(X.rows());
  17. // Loop over rows
  18. for(int i = 0;i<X.rows();i++)
  19. {
  20. RowVector ri = X.row(i);
  21. rows[i] = SortableRow<RowVector>(ri);
  22. }
  23. vector<SortableRow<RowVector> > sorted;
  24. std::vector<size_t> index_map;
  25. // Perform sort on rows
  26. igl::sort(rows,ascending,sorted,index_map);
  27. // Resize output
  28. Y.resize(X.rows(),X.cols());
  29. IX.resize(X.rows(),1);
  30. // Convert to eigen
  31. for(int i = 0;i<X.rows();i++)
  32. {
  33. Y.row(i) = sorted[i].data;
  34. IX(i) = index_map[i];
  35. }
  36. }
  37. #ifndef IGL_HEADER_ONLY
  38. 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> >&);
  39. #endif