Chris@16
|
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
|
Chris@16
|
2
|
Chris@101
|
3 // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
|
Chris@101
|
4 // Copyright (c) 2008-2014 Barend Gehrels, Amsterdam, the Netherlands.
|
Chris@101
|
5 // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
|
Chris@101
|
6
|
Chris@101
|
7 // This file was modified by Oracle on 2014.
|
Chris@101
|
8 // Modifications copyright (c) 2014, Oracle and/or its affiliates.
|
Chris@101
|
9
|
Chris@101
|
10 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
|
Chris@16
|
11
|
Chris@16
|
12 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
|
Chris@16
|
13 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
|
Chris@16
|
14
|
Chris@16
|
15 // Use, modification and distribution is subject to the Boost Software License,
|
Chris@16
|
16 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
17 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
18
|
Chris@16
|
19 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PROJECTED_POINT_HPP
|
Chris@16
|
20 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PROJECTED_POINT_HPP
|
Chris@16
|
21
|
Chris@16
|
22
|
Chris@16
|
23 #include <boost/concept_check.hpp>
|
Chris@16
|
24 #include <boost/mpl/if.hpp>
|
Chris@16
|
25 #include <boost/type_traits.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 #include <boost/geometry/core/access.hpp>
|
Chris@16
|
28 #include <boost/geometry/core/point_type.hpp>
|
Chris@16
|
29
|
Chris@16
|
30 #include <boost/geometry/algorithms/convert.hpp>
|
Chris@16
|
31 #include <boost/geometry/arithmetic/arithmetic.hpp>
|
Chris@16
|
32 #include <boost/geometry/arithmetic/dot_product.hpp>
|
Chris@16
|
33
|
Chris@16
|
34 #include <boost/geometry/strategies/tags.hpp>
|
Chris@16
|
35 #include <boost/geometry/strategies/distance.hpp>
|
Chris@16
|
36 #include <boost/geometry/strategies/default_distance_result.hpp>
|
Chris@16
|
37 #include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
|
Chris@16
|
38
|
Chris@16
|
39 #include <boost/geometry/util/select_coordinate_type.hpp>
|
Chris@16
|
40
|
Chris@16
|
41 // Helper geometry (projected point on line)
|
Chris@16
|
42 #include <boost/geometry/geometries/point.hpp>
|
Chris@16
|
43
|
Chris@16
|
44
|
Chris@16
|
45 namespace boost { namespace geometry
|
Chris@16
|
46 {
|
Chris@16
|
47
|
Chris@16
|
48
|
Chris@16
|
49 namespace strategy { namespace distance
|
Chris@16
|
50 {
|
Chris@16
|
51
|
Chris@16
|
52 /*!
|
Chris@16
|
53 \brief Strategy for distance point to segment
|
Chris@16
|
54 \ingroup strategies
|
Chris@16
|
55 \details Calculates distance using projected-point method, and (optionally) Pythagoras
|
Chris@16
|
56 \author Adapted from: http://geometryalgorithms.com/Archive/algorithm_0102/algorithm_0102.htm
|
Chris@16
|
57 \tparam CalculationType \tparam_calculation
|
Chris@16
|
58 \tparam Strategy underlying point-point distance strategy
|
Chris@16
|
59 \par Concepts for Strategy:
|
Chris@16
|
60 - cartesian_distance operator(Point,Point)
|
Chris@16
|
61 \note If the Strategy is a "comparable::pythagoras", this strategy
|
Chris@16
|
62 automatically is a comparable projected_point strategy (so without sqrt)
|
Chris@16
|
63
|
Chris@16
|
64 \qbk{
|
Chris@16
|
65 [heading See also]
|
Chris@16
|
66 [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
|
Chris@16
|
67 }
|
Chris@16
|
68
|
Chris@16
|
69 */
|
Chris@16
|
70 template
|
Chris@16
|
71 <
|
Chris@16
|
72 typename CalculationType = void,
|
Chris@16
|
73 typename Strategy = pythagoras<CalculationType>
|
Chris@16
|
74 >
|
Chris@16
|
75 class projected_point
|
Chris@16
|
76 {
|
Chris@16
|
77 public :
|
Chris@16
|
78 // The three typedefs below are necessary to calculate distances
|
Chris@16
|
79 // from segments defined in integer coordinates.
|
Chris@16
|
80
|
Chris@16
|
81 // Integer coordinates can still result in FP distances.
|
Chris@16
|
82 // There is a division, which must be represented in FP.
|
Chris@16
|
83 // So promote.
|
Chris@16
|
84 template <typename Point, typename PointOfSegment>
|
Chris@16
|
85 struct calculation_type
|
Chris@16
|
86 : promote_floating_point
|
Chris@16
|
87 <
|
Chris@16
|
88 typename strategy::distance::services::return_type
|
Chris@16
|
89 <
|
Chris@16
|
90 Strategy,
|
Chris@16
|
91 Point,
|
Chris@16
|
92 PointOfSegment
|
Chris@16
|
93 >::type
|
Chris@16
|
94 >
|
Chris@16
|
95 {};
|
Chris@16
|
96
|
Chris@16
|
97 public :
|
Chris@16
|
98
|
Chris@16
|
99 template <typename Point, typename PointOfSegment>
|
Chris@16
|
100 inline typename calculation_type<Point, PointOfSegment>::type
|
Chris@16
|
101 apply(Point const& p, PointOfSegment const& p1, PointOfSegment const& p2) const
|
Chris@16
|
102 {
|
Chris@16
|
103 assert_dimension_equal<Point, PointOfSegment>();
|
Chris@16
|
104
|
Chris@16
|
105 typedef typename calculation_type<Point, PointOfSegment>::type calculation_type;
|
Chris@16
|
106
|
Chris@16
|
107 // A projected point of points in Integer coordinates must be able to be
|
Chris@16
|
108 // represented in FP.
|
Chris@16
|
109 typedef model::point
|
Chris@16
|
110 <
|
Chris@16
|
111 calculation_type,
|
Chris@16
|
112 dimension<PointOfSegment>::value,
|
Chris@16
|
113 typename coordinate_system<PointOfSegment>::type
|
Chris@16
|
114 > fp_point_type;
|
Chris@16
|
115
|
Chris@16
|
116 // For convenience
|
Chris@16
|
117 typedef fp_point_type fp_vector_type;
|
Chris@16
|
118
|
Chris@101
|
119 /*
|
Chris@101
|
120 Algorithm [p: (px,py), p1: (x1,y1), p2: (x2,y2)]
|
Chris@16
|
121 VECTOR v(x2 - x1, y2 - y1)
|
Chris@16
|
122 VECTOR w(px - x1, py - y1)
|
Chris@16
|
123 c1 = w . v
|
Chris@16
|
124 c2 = v . v
|
Chris@16
|
125 b = c1 / c2
|
Chris@16
|
126 RETURN POINT(x1 + b * vx, y1 + b * vy)
|
Chris@16
|
127 */
|
Chris@16
|
128
|
Chris@16
|
129 // v is multiplied below with a (possibly) FP-value, so should be in FP
|
Chris@16
|
130 // For consistency we define w also in FP
|
Chris@101
|
131 fp_vector_type v, w, projected;
|
Chris@16
|
132
|
Chris@16
|
133 geometry::convert(p2, v);
|
Chris@16
|
134 geometry::convert(p, w);
|
Chris@101
|
135 geometry::convert(p1, projected);
|
Chris@101
|
136 subtract_point(v, projected);
|
Chris@101
|
137 subtract_point(w, projected);
|
Chris@16
|
138
|
Chris@16
|
139 Strategy strategy;
|
Chris@16
|
140 boost::ignore_unused_variable_warning(strategy);
|
Chris@16
|
141
|
Chris@16
|
142 calculation_type const zero = calculation_type();
|
Chris@16
|
143 calculation_type const c1 = dot_product(w, v);
|
Chris@16
|
144 if (c1 <= zero)
|
Chris@16
|
145 {
|
Chris@16
|
146 return strategy.apply(p, p1);
|
Chris@16
|
147 }
|
Chris@16
|
148 calculation_type const c2 = dot_product(v, v);
|
Chris@16
|
149 if (c2 <= c1)
|
Chris@16
|
150 {
|
Chris@16
|
151 return strategy.apply(p, p2);
|
Chris@16
|
152 }
|
Chris@16
|
153
|
Chris@16
|
154 // See above, c1 > 0 AND c2 > c1 so: c2 != 0
|
Chris@16
|
155 calculation_type const b = c1 / c2;
|
Chris@16
|
156
|
Chris@16
|
157 multiply_value(v, b);
|
Chris@16
|
158 add_point(projected, v);
|
Chris@16
|
159
|
Chris@16
|
160 return strategy.apply(p, projected);
|
Chris@16
|
161 }
|
Chris@16
|
162 };
|
Chris@16
|
163
|
Chris@16
|
164 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
|
Chris@16
|
165 namespace services
|
Chris@16
|
166 {
|
Chris@16
|
167
|
Chris@16
|
168 template <typename CalculationType, typename Strategy>
|
Chris@16
|
169 struct tag<projected_point<CalculationType, Strategy> >
|
Chris@16
|
170 {
|
Chris@16
|
171 typedef strategy_tag_distance_point_segment type;
|
Chris@16
|
172 };
|
Chris@16
|
173
|
Chris@16
|
174
|
Chris@16
|
175 template <typename CalculationType, typename Strategy, typename P, typename PS>
|
Chris@16
|
176 struct return_type<projected_point<CalculationType, Strategy>, P, PS>
|
Chris@16
|
177 : projected_point<CalculationType, Strategy>::template calculation_type<P, PS>
|
Chris@16
|
178 {};
|
Chris@16
|
179
|
Chris@16
|
180
|
Chris@16
|
181
|
Chris@16
|
182 template <typename CalculationType, typename Strategy>
|
Chris@16
|
183 struct comparable_type<projected_point<CalculationType, Strategy> >
|
Chris@16
|
184 {
|
Chris@16
|
185 // Define a projected_point strategy with its underlying point-point-strategy
|
Chris@16
|
186 // being comparable
|
Chris@16
|
187 typedef projected_point
|
Chris@16
|
188 <
|
Chris@16
|
189 CalculationType,
|
Chris@16
|
190 typename comparable_type<Strategy>::type
|
Chris@16
|
191 > type;
|
Chris@16
|
192 };
|
Chris@16
|
193
|
Chris@16
|
194
|
Chris@16
|
195 template <typename CalculationType, typename Strategy>
|
Chris@16
|
196 struct get_comparable<projected_point<CalculationType, Strategy> >
|
Chris@16
|
197 {
|
Chris@16
|
198 typedef typename comparable_type
|
Chris@16
|
199 <
|
Chris@16
|
200 projected_point<CalculationType, Strategy>
|
Chris@16
|
201 >::type comparable_type;
|
Chris@16
|
202 public :
|
Chris@16
|
203 static inline comparable_type apply(projected_point<CalculationType, Strategy> const& )
|
Chris@16
|
204 {
|
Chris@16
|
205 return comparable_type();
|
Chris@16
|
206 }
|
Chris@16
|
207 };
|
Chris@16
|
208
|
Chris@16
|
209
|
Chris@16
|
210 template <typename CalculationType, typename Strategy, typename P, typename PS>
|
Chris@16
|
211 struct result_from_distance<projected_point<CalculationType, Strategy>, P, PS>
|
Chris@16
|
212 {
|
Chris@16
|
213 private :
|
Chris@16
|
214 typedef typename return_type<projected_point<CalculationType, Strategy>, P, PS>::type return_type;
|
Chris@16
|
215 public :
|
Chris@16
|
216 template <typename T>
|
Chris@16
|
217 static inline return_type apply(projected_point<CalculationType, Strategy> const& , T const& value)
|
Chris@16
|
218 {
|
Chris@16
|
219 Strategy s;
|
Chris@16
|
220 return result_from_distance<Strategy, P, PS>::apply(s, value);
|
Chris@16
|
221 }
|
Chris@16
|
222 };
|
Chris@16
|
223
|
Chris@16
|
224
|
Chris@16
|
225 // Get default-strategy for point-segment distance calculation
|
Chris@16
|
226 // while still have the possibility to specify point-point distance strategy (PPS)
|
Chris@16
|
227 // It is used in algorithms/distance.hpp where users specify PPS for distance
|
Chris@16
|
228 // of point-to-segment or point-to-linestring.
|
Chris@16
|
229 // Convenient for geographic coordinate systems especially.
|
Chris@16
|
230 template <typename Point, typename PointOfSegment, typename Strategy>
|
Chris@101
|
231 struct default_strategy
|
Chris@101
|
232 <
|
Chris@101
|
233 point_tag, segment_tag, Point, PointOfSegment,
|
Chris@101
|
234 cartesian_tag, cartesian_tag, Strategy
|
Chris@101
|
235 >
|
Chris@16
|
236 {
|
Chris@16
|
237 typedef strategy::distance::projected_point
|
Chris@16
|
238 <
|
Chris@16
|
239 void,
|
Chris@16
|
240 typename boost::mpl::if_
|
Chris@16
|
241 <
|
Chris@16
|
242 boost::is_void<Strategy>,
|
Chris@16
|
243 typename default_strategy
|
Chris@16
|
244 <
|
Chris@101
|
245 point_tag, point_tag, Point, PointOfSegment,
|
Chris@16
|
246 cartesian_tag, cartesian_tag
|
Chris@16
|
247 >::type,
|
Chris@16
|
248 Strategy
|
Chris@16
|
249 >::type
|
Chris@16
|
250 > type;
|
Chris@16
|
251 };
|
Chris@16
|
252
|
Chris@101
|
253 template <typename PointOfSegment, typename Point, typename Strategy>
|
Chris@101
|
254 struct default_strategy
|
Chris@101
|
255 <
|
Chris@101
|
256 segment_tag, point_tag, PointOfSegment, Point,
|
Chris@101
|
257 cartesian_tag, cartesian_tag, Strategy
|
Chris@101
|
258 >
|
Chris@101
|
259 {
|
Chris@101
|
260 typedef typename default_strategy
|
Chris@101
|
261 <
|
Chris@101
|
262 point_tag, segment_tag, Point, PointOfSegment,
|
Chris@101
|
263 cartesian_tag, cartesian_tag, Strategy
|
Chris@101
|
264 >::type type;
|
Chris@101
|
265 };
|
Chris@101
|
266
|
Chris@16
|
267
|
Chris@16
|
268 } // namespace services
|
Chris@16
|
269 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
|
Chris@16
|
270
|
Chris@16
|
271
|
Chris@16
|
272 }} // namespace strategy::distance
|
Chris@16
|
273
|
Chris@16
|
274
|
Chris@16
|
275 }} // namespace boost::geometry
|
Chris@16
|
276
|
Chris@16
|
277
|
Chris@16
|
278 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PROJECTED_POINT_HPP
|