distance_projected_point.hpp 8.8 KB

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