1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2019 Barend Gehrels, Amsterdam, the Netherlands.
5 // Use, modification and distribution is subject to the Boost Software License,
6 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // 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
12 #include <boost/geometry/core/access.hpp>
13 #include <boost/geometry/core/coordinate_system.hpp>
14 #include <boost/geometry/core/coordinate_type.hpp>
15 #include <boost/geometry/arithmetic/infinite_line_functions.hpp>
16 #include <boost/geometry/algorithms/detail/make/make.hpp>
17 #include <boost/geometry/util/select_coordinate_type.hpp>
21 namespace boost { namespace geometry
24 #ifndef DOXYGEN_NO_DETAIL
29 struct distance_measure
37 bool is_small() const { return false; }
38 bool is_zero() const { return false; }
39 bool is_positive() const { return false; }
40 bool is_negative() const { return false; }
44 struct distance_measure_floating
48 distance_measure_floating()
52 // Returns true if the distance measure is small.
53 // This is an arbitrary boundary, to enable some behaviour
54 // (for example include or exclude turns), which are checked later
55 // with other conditions.
56 bool is_small() const { return std::abs(measure) < 1.0e-3; }
58 // Returns true if the distance measure is absolutely zero
59 bool is_zero() const { return measure == 0.0; }
61 // Returns true if the distance measure is positive. Distance measure
62 // algorithm returns positive value if it is located on the left side.
63 bool is_positive() const { return measure > 0.0; }
65 // Returns true if the distance measure is negative. Distance measure
66 // algorithm returns negative value if it is located on the right side.
67 bool is_negative() const { return measure < 0.0; }
71 struct distance_measure<long double>
72 : public distance_measure_floating<long double> {};
75 struct distance_measure<double>
76 : public distance_measure_floating<double> {};
79 struct distance_measure<float>
80 : public distance_measure_floating<float> {};
85 namespace detail_dispatch
88 // TODO: this is effectively a strategy, but for internal usage.
89 // It might be moved to the strategies folder.
91 template <typename CalculationType, typename CsTag>
92 struct get_distance_measure
93 : not_implemented<CsTag>
96 template <typename CalculationType>
97 struct get_distance_measure<CalculationType, cartesian_tag>
99 typedef detail::distance_measure<CalculationType> result_type;
101 template <typename SegmentPoint, typename Point>
102 static result_type apply(SegmentPoint const& p1, SegmentPoint const& p2,
105 // Get the distance measure / side value
106 // It is not a real distance and purpose is
107 // to detect small differences in collinearity
109 typedef model::infinite_line<CalculationType> line_type;
110 line_type const line = detail::make::make_infinite_line<CalculationType>(p1, p2);
112 result.measure = arithmetic::side_value(line, p);
117 template <typename CalculationType>
118 struct get_distance_measure<CalculationType, spherical_tag>
120 typedef detail::distance_measure<CalculationType> result_type;
122 template <typename SegmentPoint, typename Point>
123 static result_type apply(SegmentPoint const& , SegmentPoint const& ,
132 template <typename CalculationType>
133 struct get_distance_measure<CalculationType, geographic_tag>
134 : get_distance_measure<CalculationType, spherical_tag> {};
137 } // namespace detail_dispatch
142 // Returns a (often very tiny) value to indicate its side, and distance,
143 // 0 (absolutely 0, not even an epsilon) means collinear. Like side,
144 // a negative means that p is to the right of p1-p2. And a positive value
145 // means that p is to the left of p1-p2.
147 template <typename cs_tag, typename SegmentPoint, typename Point>
148 static distance_measure<typename select_coordinate_type<SegmentPoint, Point>::type>
149 get_distance_measure(SegmentPoint const& p1, SegmentPoint const& p2, Point const& p)
151 return detail_dispatch::get_distance_measure
153 typename select_coordinate_type<SegmentPoint, Point>::type,
159 } // namespace detail
160 #endif // DOXYGEN_NO_DETAIL
162 }} // namespace boost::geometry
164 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_DISTANCE_MEASURE_HPP