compare_circular.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_COMPARE_SPHERICAL_HPP
  7. #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_COMPARE_SPHERICAL_HPP
  8. #include <boost/math/constants/constants.hpp>
  9. #include <boost/geometry/core/cs.hpp>
  10. #include <boost/geometry/core/tags.hpp>
  11. #include <boost/geometry/strategies/compare.hpp>
  12. #include <boost/geometry/util/math.hpp>
  13. namespace boost { namespace geometry
  14. {
  15. namespace strategy { namespace compare
  16. {
  17. #ifndef DOXYGEN_NO_DETAIL
  18. namespace detail
  19. {
  20. template <typename Units>
  21. struct shift
  22. {
  23. };
  24. template <>
  25. struct shift<degree>
  26. {
  27. static inline double full() { return 360.0; }
  28. static inline double half() { return 180.0; }
  29. };
  30. template <>
  31. struct shift<radian>
  32. {
  33. static inline double full() { return 2.0 * boost::math::constants::pi<double>(); }
  34. static inline double half() { return boost::math::constants::pi<double>(); }
  35. };
  36. } // namespace detail
  37. #endif
  38. /*!
  39. \brief Compare (in one direction) strategy for spherical coordinates
  40. \ingroup strategies
  41. \tparam Point point-type
  42. \tparam Dimension dimension
  43. */
  44. template <typename CoordinateType, typename Units, typename Compare>
  45. struct circular_comparator
  46. {
  47. static inline CoordinateType put_in_range(CoordinateType const& c,
  48. double min_border, double max_border)
  49. {
  50. CoordinateType value = c;
  51. while (value < min_border)
  52. {
  53. value += detail::shift<Units>::full();
  54. }
  55. while (value > max_border)
  56. {
  57. value -= detail::shift<Units>::full();
  58. }
  59. return value;
  60. }
  61. inline bool operator()(CoordinateType const& c1, CoordinateType const& c2) const
  62. {
  63. Compare compare;
  64. // Check situation that one of them is e.g. std::numeric_limits.
  65. static const double full = detail::shift<Units>::full();
  66. double mx = 10.0 * full;
  67. if (c1 < -mx || c1 > mx || c2 < -mx || c2 > mx)
  68. {
  69. // do normal comparison, using circular is not useful
  70. return compare(c1, c2);
  71. }
  72. static const double half = full / 2.0;
  73. CoordinateType v1 = put_in_range(c1, -half, half);
  74. CoordinateType v2 = put_in_range(c2, -half, half);
  75. // Two coordinates on a circle are
  76. // at max <= half a circle away from each other.
  77. // So if it is more, shift origin.
  78. CoordinateType diff = geometry::math::abs(v1 - v2);
  79. if (diff > half)
  80. {
  81. v1 = put_in_range(v1, 0, full);
  82. v2 = put_in_range(v2, 0, full);
  83. }
  84. return compare(v1, v2);
  85. }
  86. };
  87. }} // namespace strategy::compare
  88. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  89. // Specialize for the longitude (dim 0)
  90. template
  91. <
  92. typename Point,
  93. template<typename> class CoordinateSystem,
  94. typename Units
  95. >
  96. struct strategy_compare<spherical_polar_tag, 1, Point, CoordinateSystem<Units>, 0>
  97. {
  98. typedef typename coordinate_type<Point>::type coordinate_type;
  99. typedef strategy::compare::circular_comparator
  100. <
  101. coordinate_type,
  102. Units,
  103. std::less<coordinate_type>
  104. > type;
  105. };
  106. template
  107. <
  108. typename Point,
  109. template<typename> class CoordinateSystem,
  110. typename Units
  111. >
  112. struct strategy_compare<spherical_polar_tag, -1, Point, CoordinateSystem<Units>, 0>
  113. {
  114. typedef typename coordinate_type<Point>::type coordinate_type;
  115. typedef strategy::compare::circular_comparator
  116. <
  117. coordinate_type,
  118. Units,
  119. std::greater<coordinate_type>
  120. > type;
  121. };
  122. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  123. }} // namespace boost::geometry
  124. #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_COMPARE_SPHERICAL_HPP