sort_angles.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "sort_angles.h"
  2. #include <algorithm>
  3. template <typename DerivedM, typename DerivedR>
  4. IGL_INLINE void igl::sort_angles(
  5. const Eigen::PlainObjectBase<DerivedM>& M,
  6. Eigen::PlainObjectBase<DerivedR>& R) {
  7. const size_t num_rows = M.rows();
  8. const size_t num_cols = M.cols();
  9. assert(num_cols >= 2);
  10. R.resize(num_rows);
  11. R.setLinSpaced(num_rows, 0, num_rows-1);
  12. // |
  13. // (pi/2, pi) | (0, pi/2)
  14. // |
  15. // -------------+--------------
  16. // |
  17. // (-pi, -pi/2) | (-pi/2, 0)
  18. // |
  19. auto comp = [&](size_t i, size_t j) {
  20. auto yi = M(i, 0);
  21. auto xi = M(i, 1);
  22. auto yj = M(j, 0);
  23. auto xj = M(j, 1);
  24. if (xi == xj && yi == yj) {
  25. for (size_t idx=2; idx<num_cols; idx++) {
  26. auto i_val = M(i, idx);
  27. auto j_val = M(j, idx);
  28. if (i_val != j_val) {
  29. return i_val < j_val;
  30. }
  31. }
  32. // If the entire rows are equal, use the row index.
  33. return i < j;
  34. }
  35. if (xi >= 0 && yi >= 0) {
  36. if (xj >=0 && yj >= 0) {
  37. if (xi != xj) {
  38. return xi > xj;
  39. } else {
  40. return yi < yj;
  41. }
  42. } else if (xj < 0 && yj >= 0) {
  43. return true;
  44. } else if (xj < 0 && yj < 0) {
  45. return false;
  46. } else {
  47. return false;
  48. }
  49. } else if (xi < 0 && yi >= 0) {
  50. if (xj >= 0 && yj >= 0) {
  51. return false;
  52. } else if (xj < 0 && yj >= 0) {
  53. if (xi != xj) {
  54. return xi > xj;
  55. } else {
  56. return yi > yj;
  57. }
  58. } else if (xj < 0 && yj < 0) {
  59. return false;
  60. } else {
  61. return false;
  62. }
  63. } else if (xi < 0 && yi < 0) {
  64. if (xj >= 0 && yj >= 0) {
  65. return true;
  66. } else if (xj < 0 && yj >= 0) {
  67. return true;
  68. } else if (xj < 0 && yj < 0) {
  69. if (xi != xj) {
  70. return xi < xj;
  71. } else {
  72. return yi > yj;
  73. }
  74. } else {
  75. return true;
  76. }
  77. } else {
  78. if (xj >= 0 && yj >= 0) {
  79. return true;
  80. } else if (xj < 0 && yj >= 0) {
  81. return true;
  82. } else if (xj < 0 && yj < 0) {
  83. return false;
  84. } else {
  85. if (xi != xj) {
  86. return xi < xj;
  87. } else {
  88. return yi < yj;
  89. }
  90. }
  91. }
  92. };
  93. std::sort(R.data(), R.data() + num_rows, comp);
  94. }
  95. #ifdef IGL_STATIC_LIBRARY
  96. template void igl::sort_angles<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&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&);
  97. #endif