sort_angles.cpp 3.6 KB

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