sort_angles.cpp 3.5 KB

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