get_distance_measure.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2019 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_ALGORITHMS_DETAIL_OVERLAY_GET_DISTANCE_MEASURE_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_DISTANCE_MEASURE_HPP
  8. #include <boost/geometry/core/access.hpp>
  9. #include <boost/geometry/core/coordinate_system.hpp>
  10. #include <boost/geometry/core/coordinate_type.hpp>
  11. #include <boost/geometry/arithmetic/infinite_line_functions.hpp>
  12. #include <boost/geometry/algorithms/detail/make/make.hpp>
  13. #include <boost/geometry/util/select_coordinate_type.hpp>
  14. #include <cmath>
  15. namespace boost { namespace geometry
  16. {
  17. #ifndef DOXYGEN_NO_DETAIL
  18. namespace detail
  19. {
  20. template <typename T>
  21. struct distance_measure
  22. {
  23. T measure;
  24. distance_measure()
  25. : measure(T())
  26. {}
  27. bool is_small() const { return false; }
  28. bool is_zero() const { return false; }
  29. bool is_positive() const { return false; }
  30. bool is_negative() const { return false; }
  31. };
  32. template <typename T>
  33. struct distance_measure_floating
  34. {
  35. T measure;
  36. distance_measure_floating()
  37. : measure(T())
  38. {}
  39. // Returns true if the distance measure is small.
  40. // This is an arbitrary boundary, to enable some behaviour
  41. // (for example include or exclude turns), which are checked later
  42. // with other conditions.
  43. bool is_small() const { return std::abs(measure) < 1.0e-3; }
  44. // Returns true if the distance measure is absolutely zero
  45. bool is_zero() const { return measure == 0.0; }
  46. // Returns true if the distance measure is positive. Distance measure
  47. // algorithm returns positive value if it is located on the left side.
  48. bool is_positive() const { return measure > 0.0; }
  49. // Returns true if the distance measure is negative. Distance measure
  50. // algorithm returns negative value if it is located on the right side.
  51. bool is_negative() const { return measure < 0.0; }
  52. };
  53. template <>
  54. struct distance_measure<long double>
  55. : public distance_measure_floating<long double> {};
  56. template <>
  57. struct distance_measure<double>
  58. : public distance_measure_floating<double> {};
  59. template <>
  60. struct distance_measure<float>
  61. : public distance_measure_floating<float> {};
  62. } // detail
  63. namespace detail_dispatch
  64. {
  65. // TODO: this is effectively a strategy, but for internal usage.
  66. // It might be moved to the strategies folder.
  67. template <typename CalculationType, typename CsTag>
  68. struct get_distance_measure
  69. : not_implemented<CsTag>
  70. {};
  71. template <typename CalculationType>
  72. struct get_distance_measure<CalculationType, cartesian_tag>
  73. {
  74. typedef detail::distance_measure<CalculationType> result_type;
  75. template <typename SegmentPoint, typename Point>
  76. static result_type apply(SegmentPoint const& p1, SegmentPoint const& p2,
  77. Point const& p)
  78. {
  79. // Get the distance measure / side value
  80. // It is not a real distance and purpose is
  81. // to detect small differences in collinearity
  82. typedef model::infinite_line<CalculationType> line_type;
  83. line_type const line = detail::make::make_infinite_line<CalculationType>(p1, p2);
  84. result_type result;
  85. result.measure = arithmetic::side_value(line, p);
  86. return result;
  87. }
  88. };
  89. template <typename CalculationType>
  90. struct get_distance_measure<CalculationType, spherical_tag>
  91. {
  92. typedef detail::distance_measure<CalculationType> result_type;
  93. template <typename SegmentPoint, typename Point>
  94. static result_type apply(SegmentPoint const& , SegmentPoint const& ,
  95. Point const& )
  96. {
  97. // TODO, optional
  98. result_type result;
  99. return result;
  100. }
  101. };
  102. template <typename CalculationType>
  103. struct get_distance_measure<CalculationType, geographic_tag>
  104. : get_distance_measure<CalculationType, spherical_tag> {};
  105. } // namespace detail_dispatch
  106. namespace detail
  107. {
  108. // Returns a (often very tiny) value to indicate its side, and distance,
  109. // 0 (absolutely 0, not even an epsilon) means collinear. Like side,
  110. // a negative means that p is to the right of p1-p2. And a positive value
  111. // means that p is to the left of p1-p2.
  112. template <typename cs_tag, typename SegmentPoint, typename Point>
  113. static distance_measure<typename select_coordinate_type<SegmentPoint, Point>::type>
  114. get_distance_measure(SegmentPoint const& p1, SegmentPoint const& p2, Point const& p)
  115. {
  116. return detail_dispatch::get_distance_measure
  117. <
  118. typename select_coordinate_type<SegmentPoint, Point>::type,
  119. cs_tag
  120. >::apply(p1, p2, p);
  121. }
  122. } // namespace detail
  123. #endif // DOXYGEN_NO_DETAIL
  124. }} // namespace boost::geometry
  125. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_DISTANCE_MEASURE_HPP