get_distance_measure.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2019-2021 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2022.
  4. // Modifications copyright (c) 2022 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_DISTANCE_MEASURE_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_DISTANCE_MEASURE_HPP
  11. #include <boost/geometry/core/access.hpp>
  12. #include <boost/geometry/core/coordinate_system.hpp>
  13. #include <boost/geometry/core/coordinate_type.hpp>
  14. #include <boost/geometry/arithmetic/infinite_line_functions.hpp>
  15. #include <boost/geometry/algorithms/detail/make/make.hpp>
  16. #include <boost/geometry/algorithms/not_implemented.hpp>
  17. #include <boost/geometry/util/select_coordinate_type.hpp>
  18. #include <cmath>
  19. namespace boost { namespace geometry
  20. {
  21. #ifndef DOXYGEN_NO_DETAIL
  22. namespace detail
  23. {
  24. template <typename T>
  25. struct distance_measure
  26. {
  27. T measure;
  28. distance_measure()
  29. : measure(T())
  30. {}
  31. // Returns true if the distance measure is absolutely zero
  32. bool is_zero() const
  33. {
  34. return ! is_positive() && ! is_negative();
  35. }
  36. // Returns true if the distance measure is positive. Distance measure
  37. // algorithm returns positive value if it is located on the left side.
  38. bool is_positive() const { return measure > T(0); }
  39. // Returns true if the distance measure is negative. Distance measure
  40. // algorithm returns negative value if it is located on the right side.
  41. bool is_negative() const { return measure < T(0); }
  42. };
  43. } // detail
  44. namespace detail_dispatch
  45. {
  46. // TODO: this is effectively a strategy, but for internal usage.
  47. // It might be moved to the strategies folder.
  48. template <typename CalculationType, typename CsTag>
  49. struct get_distance_measure
  50. : not_implemented<CsTag>
  51. {};
  52. template <typename CalculationType>
  53. struct get_distance_measure<CalculationType, spherical_tag>
  54. {
  55. // By default the distance measure is zero, no side difference
  56. using result_type = detail::distance_measure<CalculationType>;
  57. template <typename SegmentPoint, typename Point>
  58. static result_type apply(SegmentPoint const& , SegmentPoint const& ,
  59. Point const& )
  60. {
  61. result_type const result;
  62. return result;
  63. }
  64. };
  65. template <typename CalculationType>
  66. struct get_distance_measure<CalculationType, geographic_tag>
  67. : get_distance_measure<CalculationType, spherical_tag>
  68. {};
  69. template <typename CalculationType>
  70. struct get_distance_measure<CalculationType, cartesian_tag>
  71. {
  72. using result_type = detail::distance_measure<CalculationType>;
  73. template <typename SegmentPoint, typename Point>
  74. static result_type apply(SegmentPoint const& p1, SegmentPoint const& p2,
  75. Point const& p)
  76. {
  77. // Get the distance measure / side value
  78. // It is not a real distance and purpose is
  79. // to detect small differences in collinearity
  80. auto const line = detail::make::make_infinite_line<CalculationType>(p1, p2);
  81. result_type result;
  82. result.measure = arithmetic::side_value(line, p);
  83. return result;
  84. }
  85. };
  86. } // namespace detail_dispatch
  87. namespace detail
  88. {
  89. // Returns a (often very tiny) value to indicate its side, and distance,
  90. // 0 (absolutely 0, not even an epsilon) means collinear. Like side,
  91. // a negative means that p is to the right of p1-p2. And a positive value
  92. // means that p is to the left of p1-p2.
  93. template <typename SegmentPoint, typename Point, typename Strategies>
  94. inline auto get_distance_measure(SegmentPoint const& p1, SegmentPoint const& p2, Point const& p,
  95. Strategies const&)
  96. {
  97. using calc_t = typename select_coordinate_type<SegmentPoint, Point>::type;
  98. // Verify equality, without using a tolerance
  99. // (so don't use equals or equals_point_point)
  100. // because it is about very tiny differences.
  101. auto identical = [](auto const& point1, auto const& point2)
  102. {
  103. return geometry::get<0>(point1) == geometry::get<0>(point2)
  104. && geometry::get<1>(point1) == geometry::get<1>(point2);
  105. };
  106. if (identical(p1, p) || identical(p2, p))
  107. {
  108. detail::distance_measure<calc_t> const result;
  109. return result;
  110. }
  111. return detail_dispatch::get_distance_measure
  112. <
  113. calc_t,
  114. typename Strategies::cs_tag
  115. >::apply(p1, p2, p);
  116. }
  117. } // namespace detail
  118. #endif // DOXYGEN_NO_DETAIL
  119. }} // namespace boost::geometry
  120. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_DISTANCE_MEASURE_HPP