1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
4 // Copyright (c) 2008-2014 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
7 // This file was modified by Oracle on 2014.
8 // Modifications copyright (c) 2014, Oracle and/or its affiliates.
10 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
12 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
13 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
15 // Use, modification and distribution is subject to the Boost Software License,
16 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt)
19 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PROJECTED_POINT_HPP
20 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PROJECTED_POINT_HPP
23 #include <boost/concept_check.hpp>
24 #include <boost/mpl/if.hpp>
25 #include <boost/type_traits/is_void.hpp>
27 #include <boost/geometry/core/access.hpp>
28 #include <boost/geometry/core/point_type.hpp>
30 #include <boost/geometry/algorithms/convert.hpp>
31 #include <boost/geometry/arithmetic/arithmetic.hpp>
32 #include <boost/geometry/arithmetic/dot_product.hpp>
34 #include <boost/geometry/strategies/tags.hpp>
35 #include <boost/geometry/strategies/distance.hpp>
36 #include <boost/geometry/strategies/default_distance_result.hpp>
37 #include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
39 #include <boost/geometry/util/select_coordinate_type.hpp>
41 // Helper geometry (projected point on line)
42 #include <boost/geometry/geometries/point.hpp>
45 namespace boost { namespace geometry
49 namespace strategy { namespace distance
53 \brief Strategy for distance point to segment
55 \details Calculates distance using projected-point method, and (optionally) Pythagoras
56 \author Adapted from: http://geometryalgorithms.com/Archive/algorithm_0102/algorithm_0102.htm
57 \tparam CalculationType \tparam_calculation
58 \tparam Strategy underlying point-point distance strategy
59 \par Concepts for Strategy:
60 - cartesian_distance operator(Point,Point)
61 \note If the Strategy is a "comparable::pythagoras", this strategy
62 automatically is a comparable projected_point strategy (so without sqrt)
66 [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
72 typename CalculationType = void,
73 typename Strategy = pythagoras<CalculationType>
78 // The three typedefs below are necessary to calculate distances
79 // from segments defined in integer coordinates.
81 // Integer coordinates can still result in FP distances.
82 // There is a division, which must be represented in FP.
84 template <typename Point, typename PointOfSegment>
85 struct calculation_type
86 : promote_floating_point
88 typename strategy::distance::services::return_type
99 template <typename Point, typename PointOfSegment>
100 inline typename calculation_type<Point, PointOfSegment>::type
101 apply(Point const& p, PointOfSegment const& p1, PointOfSegment const& p2) const
103 assert_dimension_equal<Point, PointOfSegment>();
105 typedef typename calculation_type<Point, PointOfSegment>::type calculation_type;
107 // A projected point of points in Integer coordinates must be able to be
108 // represented in FP.
112 dimension<PointOfSegment>::value,
113 typename coordinate_system<PointOfSegment>::type
117 typedef fp_point_type fp_vector_type;
120 Algorithm [p: (px,py), p1: (x1,y1), p2: (x2,y2)]
121 VECTOR v(x2 - x1, y2 - y1)
122 VECTOR w(px - x1, py - y1)
126 RETURN POINT(x1 + b * vx, y1 + b * vy)
129 // v is multiplied below with a (possibly) FP-value, so should be in FP
130 // For consistency we define w also in FP
131 fp_vector_type v, w, projected;
133 geometry::convert(p2, v);
134 geometry::convert(p, w);
135 geometry::convert(p1, projected);
136 subtract_point(v, projected);
137 subtract_point(w, projected);
140 boost::ignore_unused_variable_warning(strategy);
142 calculation_type const zero = calculation_type();
143 calculation_type const c1 = dot_product(w, v);
146 return strategy.apply(p, p1);
148 calculation_type const c2 = dot_product(v, v);
151 return strategy.apply(p, p2);
154 // See above, c1 > 0 AND c2 > c1 so: c2 != 0
155 calculation_type const b = c1 / c2;
157 multiply_value(v, b);
158 add_point(projected, v);
160 return strategy.apply(p, projected);
164 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
168 template <typename CalculationType, typename Strategy>
169 struct tag<projected_point<CalculationType, Strategy> >
171 typedef strategy_tag_distance_point_segment type;
175 template <typename CalculationType, typename Strategy, typename P, typename PS>
176 struct return_type<projected_point<CalculationType, Strategy>, P, PS>
177 : projected_point<CalculationType, Strategy>::template calculation_type<P, PS>
182 template <typename CalculationType, typename Strategy>
183 struct comparable_type<projected_point<CalculationType, Strategy> >
185 // Define a projected_point strategy with its underlying point-point-strategy
187 typedef projected_point
190 typename comparable_type<Strategy>::type
195 template <typename CalculationType, typename Strategy>
196 struct get_comparable<projected_point<CalculationType, Strategy> >
198 typedef typename comparable_type
200 projected_point<CalculationType, Strategy>
201 >::type comparable_type;
203 static inline comparable_type apply(projected_point<CalculationType, Strategy> const& )
205 return comparable_type();
210 template <typename CalculationType, typename Strategy, typename P, typename PS>
211 struct result_from_distance<projected_point<CalculationType, Strategy>, P, PS>
214 typedef typename return_type<projected_point<CalculationType, Strategy>, P, PS>::type return_type;
216 template <typename T>
217 static inline return_type apply(projected_point<CalculationType, Strategy> const& , T const& value)
220 return result_from_distance<Strategy, P, PS>::apply(s, value);
225 // Get default-strategy for point-segment distance calculation
226 // while still have the possibility to specify point-point distance strategy (PPS)
227 // It is used in algorithms/distance.hpp where users specify PPS for distance
228 // of point-to-segment or point-to-linestring.
229 // Convenient for geographic coordinate systems especially.
230 template <typename Point, typename PointOfSegment, typename Strategy>
231 struct default_strategy
233 point_tag, segment_tag, Point, PointOfSegment,
234 cartesian_tag, cartesian_tag, Strategy
237 typedef strategy::distance::projected_point
240 typename boost::mpl::if_
242 boost::is_void<Strategy>,
243 typename default_strategy
245 point_tag, point_tag, Point, PointOfSegment,
246 cartesian_tag, cartesian_tag
253 template <typename PointOfSegment, typename Point, typename Strategy>
254 struct default_strategy
256 segment_tag, point_tag, PointOfSegment, Point,
257 cartesian_tag, cartesian_tag, Strategy
260 typedef typename default_strategy
262 point_tag, segment_tag, Point, PointOfSegment,
263 cartesian_tag, cartesian_tag, Strategy
268 } // namespace services
269 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
272 }} // namespace strategy::distance
275 }} // namespace boost::geometry
278 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PROJECTED_POINT_HPP