distance.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  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_ALGORITHMS_DISTANCE_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_DISTANCE_HPP
  12. #include <boost/concept_check.hpp>
  13. #include <boost/mpl/if.hpp>
  14. #include <boost/range.hpp>
  15. #include <boost/typeof/typeof.hpp>
  16. #include <boost/geometry/core/cs.hpp>
  17. #include <boost/geometry/core/closure.hpp>
  18. #include <boost/geometry/core/reverse_dispatch.hpp>
  19. #include <boost/geometry/core/tag_cast.hpp>
  20. #include <boost/geometry/algorithms/not_implemented.hpp>
  21. #include <boost/geometry/algorithms/detail/throw_on_empty_input.hpp>
  22. #include <boost/geometry/geometries/segment.hpp>
  23. #include <boost/geometry/geometries/concepts/check.hpp>
  24. #include <boost/geometry/strategies/distance.hpp>
  25. #include <boost/geometry/strategies/default_distance_result.hpp>
  26. #include <boost/geometry/algorithms/assign.hpp>
  27. #include <boost/geometry/algorithms/within.hpp>
  28. #include <boost/geometry/views/closeable_view.hpp>
  29. #include <boost/geometry/util/math.hpp>
  30. namespace boost { namespace geometry
  31. {
  32. #ifndef DOXYGEN_NO_DETAIL
  33. namespace detail { namespace distance
  34. {
  35. // To avoid spurious namespaces here:
  36. using strategy::distance::services::return_type;
  37. template <typename P1, typename P2, typename Strategy>
  38. struct point_to_point
  39. {
  40. static inline typename return_type<Strategy, P1, P2>::type
  41. apply(P1 const& p1, P2 const& p2, Strategy const& strategy)
  42. {
  43. boost::ignore_unused_variable_warning(strategy);
  44. return strategy.apply(p1, p2);
  45. }
  46. };
  47. template<typename Point, typename Segment, typename Strategy>
  48. struct point_to_segment
  49. {
  50. static inline typename return_type<Strategy, Point, typename point_type<Segment>::type>::type
  51. apply(Point const& point, Segment const& segment, Strategy const& )
  52. {
  53. typename strategy::distance::services::default_strategy
  54. <
  55. segment_tag,
  56. Point,
  57. typename point_type<Segment>::type,
  58. typename cs_tag<Point>::type,
  59. typename cs_tag<typename point_type<Segment>::type>::type,
  60. Strategy
  61. >::type segment_strategy;
  62. typename point_type<Segment>::type p[2];
  63. geometry::detail::assign_point_from_index<0>(segment, p[0]);
  64. geometry::detail::assign_point_from_index<1>(segment, p[1]);
  65. return segment_strategy.apply(point, p[0], p[1]);
  66. }
  67. };
  68. template
  69. <
  70. typename Point,
  71. typename Range,
  72. closure_selector Closure,
  73. typename PPStrategy,
  74. typename PSStrategy
  75. >
  76. struct point_to_range
  77. {
  78. typedef typename return_type<PSStrategy, Point, typename point_type<Range>::type>::type return_type;
  79. static inline return_type apply(Point const& point, Range const& range,
  80. PPStrategy const& pp_strategy, PSStrategy const& ps_strategy)
  81. {
  82. return_type const zero = return_type(0);
  83. if (boost::size(range) == 0)
  84. {
  85. return zero;
  86. }
  87. typedef typename closeable_view<Range const, Closure>::type view_type;
  88. view_type view(range);
  89. // line of one point: return point distance
  90. typedef typename boost::range_iterator<view_type const>::type iterator_type;
  91. iterator_type it = boost::begin(view);
  92. iterator_type prev = it++;
  93. if (it == boost::end(view))
  94. {
  95. return pp_strategy.apply(point, *boost::begin(view));
  96. }
  97. // Create comparable (more efficient) strategy
  98. typedef typename strategy::distance::services::comparable_type<PSStrategy>::type eps_strategy_type;
  99. eps_strategy_type eps_strategy = strategy::distance::services::get_comparable<PSStrategy>::apply(ps_strategy);
  100. // start with first segment distance
  101. return_type d = eps_strategy.apply(point, *prev, *it);
  102. return_type rd = ps_strategy.apply(point, *prev, *it);
  103. // check if other segments are closer
  104. for (++prev, ++it; it != boost::end(view); ++prev, ++it)
  105. {
  106. return_type const ds = eps_strategy.apply(point, *prev, *it);
  107. if (geometry::math::equals(ds, zero))
  108. {
  109. return ds;
  110. }
  111. else if (ds < d)
  112. {
  113. d = ds;
  114. rd = ps_strategy.apply(point, *prev, *it);
  115. }
  116. }
  117. return rd;
  118. }
  119. };
  120. template
  121. <
  122. typename Point,
  123. typename Ring,
  124. closure_selector Closure,
  125. typename PPStrategy,
  126. typename PSStrategy
  127. >
  128. struct point_to_ring
  129. {
  130. typedef std::pair
  131. <
  132. typename return_type<PPStrategy, Point, typename point_type<Ring>::type>::type, bool
  133. > distance_containment;
  134. static inline distance_containment apply(Point const& point,
  135. Ring const& ring,
  136. PPStrategy const& pp_strategy, PSStrategy const& ps_strategy)
  137. {
  138. return distance_containment
  139. (
  140. point_to_range
  141. <
  142. Point,
  143. Ring,
  144. Closure,
  145. PPStrategy,
  146. PSStrategy
  147. >::apply(point, ring, pp_strategy, ps_strategy),
  148. geometry::within(point, ring)
  149. );
  150. }
  151. };
  152. template
  153. <
  154. typename Point,
  155. typename Polygon,
  156. closure_selector Closure,
  157. typename PPStrategy,
  158. typename PSStrategy
  159. >
  160. struct point_to_polygon
  161. {
  162. typedef typename return_type<PPStrategy, Point, typename point_type<Polygon>::type>::type return_type;
  163. typedef std::pair<return_type, bool> distance_containment;
  164. static inline distance_containment apply(Point const& point,
  165. Polygon const& polygon,
  166. PPStrategy const& pp_strategy, PSStrategy const& ps_strategy)
  167. {
  168. // Check distance to all rings
  169. typedef point_to_ring
  170. <
  171. Point,
  172. typename ring_type<Polygon>::type,
  173. Closure,
  174. PPStrategy,
  175. PSStrategy
  176. > per_ring;
  177. distance_containment dc = per_ring::apply(point,
  178. exterior_ring(polygon), pp_strategy, ps_strategy);
  179. typename interior_return_type<Polygon const>::type rings
  180. = interior_rings(polygon);
  181. for (BOOST_AUTO_TPL(it, boost::begin(rings)); it != boost::end(rings); ++it)
  182. {
  183. distance_containment dcr = per_ring::apply(point,
  184. *it, pp_strategy, ps_strategy);
  185. if (dcr.first < dc.first)
  186. {
  187. dc.first = dcr.first;
  188. }
  189. // If it was inside, and also inside inner ring,
  190. // turn off the inside-flag, it is outside the polygon
  191. if (dc.second && dcr.second)
  192. {
  193. dc.second = false;
  194. }
  195. }
  196. return dc;
  197. }
  198. };
  199. // Helper metafunction for default strategy retrieval
  200. template <typename Geometry1, typename Geometry2>
  201. struct default_strategy
  202. : strategy::distance::services::default_strategy
  203. <
  204. point_tag,
  205. typename point_type<Geometry1>::type,
  206. typename point_type<Geometry2>::type
  207. >
  208. {};
  209. }} // namespace detail::distance
  210. #endif // DOXYGEN_NO_DETAIL
  211. #ifndef DOXYGEN_NO_DISPATCH
  212. namespace dispatch
  213. {
  214. using strategy::distance::services::return_type;
  215. template
  216. <
  217. typename Geometry1, typename Geometry2,
  218. typename Strategy = typename detail::distance::default_strategy<Geometry1, Geometry2>::type,
  219. typename Tag1 = typename tag_cast<typename tag<Geometry1>::type, multi_tag>::type,
  220. typename Tag2 = typename tag_cast<typename tag<Geometry2>::type, multi_tag>::type,
  221. typename StrategyTag = typename strategy::distance::services::tag<Strategy>::type,
  222. bool Reverse = reverse_dispatch<Geometry1, Geometry2>::type::value
  223. >
  224. struct distance: not_implemented<Tag1, Tag2>
  225. {};
  226. // If reversal is needed, perform it
  227. template
  228. <
  229. typename Geometry1, typename Geometry2, typename Strategy,
  230. typename Tag1, typename Tag2, typename StrategyTag
  231. >
  232. struct distance
  233. <
  234. Geometry1, Geometry2, Strategy,
  235. Tag1, Tag2, StrategyTag,
  236. true
  237. >
  238. : distance<Geometry2, Geometry1, Strategy, Tag2, Tag1, StrategyTag, false>
  239. {
  240. typedef typename strategy::distance::services::return_type
  241. <
  242. Strategy,
  243. typename point_type<Geometry2>::type,
  244. typename point_type<Geometry1>::type
  245. >::type return_type;
  246. static inline return_type apply(
  247. Geometry1 const& g1,
  248. Geometry2 const& g2,
  249. Strategy const& strategy)
  250. {
  251. return distance
  252. <
  253. Geometry2, Geometry1, Strategy,
  254. Tag2, Tag1, StrategyTag,
  255. false
  256. >::apply(g2, g1, strategy);
  257. }
  258. };
  259. // Point-point
  260. template <typename P1, typename P2, typename Strategy>
  261. struct distance
  262. <
  263. P1, P2, Strategy,
  264. point_tag, point_tag, strategy_tag_distance_point_point,
  265. false
  266. >
  267. : detail::distance::point_to_point<P1, P2, Strategy>
  268. {};
  269. // Point-line version 1, where point-point strategy is specified
  270. template <typename Point, typename Linestring, typename Strategy>
  271. struct distance
  272. <
  273. Point, Linestring, Strategy,
  274. point_tag, linestring_tag, strategy_tag_distance_point_point,
  275. false
  276. >
  277. {
  278. static inline typename return_type<Strategy, Point, typename point_type<Linestring>::type>::type
  279. apply(Point const& point,
  280. Linestring const& linestring,
  281. Strategy const& strategy)
  282. {
  283. typedef typename strategy::distance::services::default_strategy
  284. <
  285. segment_tag,
  286. Point,
  287. typename point_type<Linestring>::type,
  288. typename cs_tag<Point>::type,
  289. typename cs_tag<typename point_type<Linestring>::type>::type,
  290. Strategy
  291. >::type ps_strategy_type;
  292. return detail::distance::point_to_range
  293. <
  294. Point, Linestring, closed, Strategy, ps_strategy_type
  295. >::apply(point, linestring, strategy, ps_strategy_type());
  296. }
  297. };
  298. // Point-line version 2, where point-segment strategy is specified
  299. template <typename Point, typename Linestring, typename Strategy>
  300. struct distance
  301. <
  302. Point, Linestring, Strategy,
  303. point_tag, linestring_tag, strategy_tag_distance_point_segment,
  304. false
  305. >
  306. {
  307. static inline typename return_type<Strategy, Point, typename point_type<Linestring>::type>::type
  308. apply(Point const& point,
  309. Linestring const& linestring,
  310. Strategy const& strategy)
  311. {
  312. typedef typename Strategy::point_strategy_type pp_strategy_type;
  313. return detail::distance::point_to_range
  314. <
  315. Point, Linestring, closed, pp_strategy_type, Strategy
  316. >::apply(point, linestring, pp_strategy_type(), strategy);
  317. }
  318. };
  319. // Point-ring , where point-segment strategy is specified
  320. template <typename Point, typename Ring, typename Strategy>
  321. struct distance
  322. <
  323. Point, Ring, Strategy,
  324. point_tag, ring_tag, strategy_tag_distance_point_point,
  325. false
  326. >
  327. {
  328. typedef typename return_type<Strategy, Point, typename point_type<Ring>::type>::type return_type;
  329. static inline return_type apply(Point const& point,
  330. Ring const& ring,
  331. Strategy const& strategy)
  332. {
  333. typedef typename strategy::distance::services::default_strategy
  334. <
  335. segment_tag,
  336. Point,
  337. typename point_type<Ring>::type
  338. >::type ps_strategy_type;
  339. std::pair<return_type, bool>
  340. dc = detail::distance::point_to_ring
  341. <
  342. Point, Ring,
  343. geometry::closure<Ring>::value,
  344. Strategy, ps_strategy_type
  345. >::apply(point, ring, strategy, ps_strategy_type());
  346. return dc.second ? return_type(0) : dc.first;
  347. }
  348. };
  349. // Point-polygon , where point-segment strategy is specified
  350. template <typename Point, typename Polygon, typename Strategy>
  351. struct distance
  352. <
  353. Point, Polygon, Strategy,
  354. point_tag, polygon_tag, strategy_tag_distance_point_point,
  355. false
  356. >
  357. {
  358. typedef typename return_type<Strategy, Point, typename point_type<Polygon>::type>::type return_type;
  359. static inline return_type apply(Point const& point,
  360. Polygon const& polygon,
  361. Strategy const& strategy)
  362. {
  363. typedef typename strategy::distance::services::default_strategy
  364. <
  365. segment_tag,
  366. Point,
  367. typename point_type<Polygon>::type
  368. >::type ps_strategy_type;
  369. std::pair<return_type, bool>
  370. dc = detail::distance::point_to_polygon
  371. <
  372. Point, Polygon,
  373. geometry::closure<Polygon>::value,
  374. Strategy, ps_strategy_type
  375. >::apply(point, polygon, strategy, ps_strategy_type());
  376. return dc.second ? return_type(0) : dc.first;
  377. }
  378. };
  379. // Point-segment version 1, with point-point strategy
  380. template <typename Point, typename Segment, typename Strategy>
  381. struct distance
  382. <
  383. Point, Segment, Strategy,
  384. point_tag, segment_tag, strategy_tag_distance_point_point,
  385. false
  386. > : detail::distance::point_to_segment<Point, Segment, Strategy>
  387. {};
  388. // Point-segment version 2, with point-segment strategy
  389. template <typename Point, typename Segment, typename Strategy>
  390. struct distance
  391. <
  392. Point, Segment, Strategy,
  393. point_tag, segment_tag, strategy_tag_distance_point_segment,
  394. false
  395. >
  396. {
  397. static inline typename return_type<Strategy, Point, typename point_type<Segment>::type>::type
  398. apply(Point const& point,
  399. Segment const& segment,
  400. Strategy const& strategy)
  401. {
  402. typename point_type<Segment>::type p[2];
  403. geometry::detail::assign_point_from_index<0>(segment, p[0]);
  404. geometry::detail::assign_point_from_index<1>(segment, p[1]);
  405. return strategy.apply(point, p[0], p[1]);
  406. }
  407. };
  408. } // namespace dispatch
  409. #endif // DOXYGEN_NO_DISPATCH
  410. /*!
  411. \brief \brief_calc2{distance} \brief_strategy
  412. \ingroup distance
  413. \details
  414. \details \details_calc{area}. \brief_strategy. \details_strategy_reasons
  415. \tparam Geometry1 \tparam_geometry
  416. \tparam Geometry2 \tparam_geometry
  417. \tparam Strategy \tparam_strategy{Distance}
  418. \param geometry1 \param_geometry
  419. \param geometry2 \param_geometry
  420. \param strategy \param_strategy{distance}
  421. \return \return_calc{distance}
  422. \note The strategy can be a point-point strategy. In case of distance point-line/point-polygon
  423. it may also be a point-segment strategy.
  424. \qbk{distinguish,with strategy}
  425. \qbk{
  426. [heading Available Strategies]
  427. \* [link geometry.reference.strategies.strategy_distance_pythagoras Pythagoras (cartesian)]
  428. \* [link geometry.reference.strategies.strategy_distance_haversine Haversine (spherical)]
  429. \* [link geometry.reference.strategies.strategy_distance_cross_track Cross track (spherical\, point-to-segment)]
  430. \* [link geometry.reference.strategies.strategy_distance_projected_point Projected point (cartesian\, point-to-segment)]
  431. \* more (currently extensions): Vincenty\, Andoyer (geographic)
  432. }
  433. */
  434. /*
  435. Note, in case of a Compilation Error:
  436. if you get:
  437. - "Failed to specialize function template ..."
  438. - "error: no matching function for call to ..."
  439. for distance, it is probably so that there is no specialization
  440. for return_type<...> for your strategy.
  441. */
  442. template <typename Geometry1, typename Geometry2, typename Strategy>
  443. inline typename strategy::distance::services::return_type
  444. <
  445. Strategy,
  446. typename point_type<Geometry1>::type,
  447. typename point_type<Geometry2>::type
  448. >::type
  449. distance(Geometry1 const& geometry1,
  450. Geometry2 const& geometry2,
  451. Strategy const& strategy)
  452. {
  453. concept::check<Geometry1 const>();
  454. concept::check<Geometry2 const>();
  455. detail::throw_on_empty_input(geometry1);
  456. detail::throw_on_empty_input(geometry2);
  457. return dispatch::distance
  458. <
  459. Geometry1,
  460. Geometry2,
  461. Strategy
  462. >::apply(geometry1, geometry2, strategy);
  463. }
  464. /*!
  465. \brief \brief_calc2{distance}
  466. \ingroup distance
  467. \details The default strategy is used, corresponding to the coordinate system of the geometries
  468. \tparam Geometry1 \tparam_geometry
  469. \tparam Geometry2 \tparam_geometry
  470. \param geometry1 \param_geometry
  471. \param geometry2 \param_geometry
  472. \return \return_calc{distance}
  473. \qbk{[include reference/algorithms/distance.qbk]}
  474. */
  475. template <typename Geometry1, typename Geometry2>
  476. inline typename default_distance_result<Geometry1, Geometry2>::type distance(
  477. Geometry1 const& geometry1, Geometry2 const& geometry2)
  478. {
  479. concept::check<Geometry1 const>();
  480. concept::check<Geometry2 const>();
  481. return distance(geometry1, geometry2,
  482. typename detail::distance::default_strategy<Geometry1, Geometry2>::type());
  483. }
  484. }} // namespace boost::geometry
  485. #endif // BOOST_GEOMETRY_ALGORITHMS_DISTANCE_HPP