distance_projected_point.hpp 8.5 KB

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