Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / geometry / algorithms / detail / overlay / get_distance_measure.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
3 // Copyright (c) 2019 Barend Gehrels, Amsterdam, the Netherlands.
4
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)
8
9 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_DISTANCE_MEASURE_HPP
10 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_DISTANCE_MEASURE_HPP
11
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>
18
19 #include <cmath>
20
21 namespace boost { namespace geometry
22 {
23
24 #ifndef DOXYGEN_NO_DETAIL
25 namespace detail
26 {
27
28 template <typename T>
29 struct distance_measure
30 {
31     T measure;
32
33     distance_measure()
34         : measure(T())
35     {}
36
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; }
41 };
42
43 template <typename T>
44 struct distance_measure_floating
45 {
46     T measure;
47
48     distance_measure_floating()
49         : measure(T())
50     {}
51
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; }
57
58     // Returns true if the distance measure is absolutely zero
59     bool is_zero() const { return measure == 0.0; }
60
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; }
64
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; }
68 };
69
70 template <>
71 struct distance_measure<long double>
72     : public distance_measure_floating<long double> {};
73
74 template <>
75 struct distance_measure<double>
76     : public distance_measure_floating<double> {};
77
78 template <>
79 struct distance_measure<float>
80     : public distance_measure_floating<float> {};
81
82 } // detail
83
84
85 namespace detail_dispatch
86 {
87
88 // TODO: this is effectively a strategy, but for internal usage.
89 // It might be moved to the strategies folder.
90
91 template <typename CalculationType, typename CsTag>
92 struct get_distance_measure
93         : not_implemented<CsTag>
94 {};
95
96 template <typename CalculationType>
97 struct get_distance_measure<CalculationType, cartesian_tag>
98 {
99     typedef detail::distance_measure<CalculationType> result_type;
100
101     template <typename SegmentPoint, typename Point>
102     static result_type apply(SegmentPoint const& p1, SegmentPoint const& p2,
103                              Point const& p)
104     {
105         // Get the distance measure / side value
106         // It is not a real distance and purpose is
107         // to detect small differences in collinearity
108
109         typedef model::infinite_line<CalculationType> line_type;
110         line_type const line = detail::make::make_infinite_line<CalculationType>(p1, p2);
111         result_type result;
112         result.measure = arithmetic::side_value(line, p);
113         return result;
114     }
115 };
116
117 template <typename CalculationType>
118 struct get_distance_measure<CalculationType, spherical_tag>
119 {
120     typedef detail::distance_measure<CalculationType> result_type;
121
122     template <typename SegmentPoint, typename Point>
123     static result_type apply(SegmentPoint const& , SegmentPoint const& ,
124                              Point const& )
125     {
126         // TODO, optional
127         result_type result;
128         return result;
129     }
130 };
131
132 template <typename CalculationType>
133 struct get_distance_measure<CalculationType, geographic_tag>
134         : get_distance_measure<CalculationType, spherical_tag> {};
135
136
137 } // namespace detail_dispatch
138
139 namespace detail
140 {
141
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.
146
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)
150 {
151     return detail_dispatch::get_distance_measure
152             <
153                 typename select_coordinate_type<SegmentPoint, Point>::type,
154                 cs_tag
155             >::apply(p1, p2, p);
156
157 }
158
159 } // namespace detail
160 #endif // DOXYGEN_NO_DETAIL
161
162 }} // namespace boost::geometry
163
164 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_GET_DISTANCE_MEASURE_HPP