distance_projected_point.hpp 8.0 KB

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