distance_predicates.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // Boost.Geometry Index
  2. //
  3. // Spatial index distance predicates, calculators and checkers
  4. // used in nearest query - specialized for envelopes
  5. //
  6. // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
  7. //
  8. // This file was modified by Oracle on 2019.
  9. // Modifications copyright (c) 2019 Oracle and/or its affiliates.
  10. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  11. //
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_DISTANCE_PREDICATES_HPP
  16. #define BOOST_GEOMETRY_INDEX_DETAIL_DISTANCE_PREDICATES_HPP
  17. #include <boost/geometry/index/detail/algorithms/comparable_distance_near.hpp>
  18. #include <boost/geometry/index/detail/algorithms/comparable_distance_far.hpp>
  19. #include <boost/geometry/index/detail/algorithms/comparable_distance_centroid.hpp>
  20. #include <boost/geometry/index/detail/algorithms/path_intersection.hpp>
  21. #include <boost/geometry/index/detail/tags.hpp>
  22. namespace boost { namespace geometry { namespace index { namespace detail {
  23. // ------------------------------------------------------------------ //
  24. // relations
  25. // ------------------------------------------------------------------ //
  26. template <typename T>
  27. struct to_nearest
  28. {
  29. to_nearest(T const& v) : value(v) {}
  30. T value;
  31. };
  32. template <typename T>
  33. struct to_centroid
  34. {
  35. to_centroid(T const& v) : value(v) {}
  36. T value;
  37. };
  38. template <typename T>
  39. struct to_furthest
  40. {
  41. to_furthest(T const& v) : value(v) {}
  42. T value;
  43. };
  44. // tags
  45. struct to_nearest_tag {};
  46. struct to_centroid_tag {};
  47. struct to_furthest_tag {};
  48. // ------------------------------------------------------------------ //
  49. // relation traits and access
  50. // ------------------------------------------------------------------ //
  51. template <typename T>
  52. struct relation
  53. {
  54. typedef T value_type;
  55. typedef to_nearest_tag tag;
  56. static inline T const& value(T const& v) { return v; }
  57. static inline T & value(T & v) { return v; }
  58. };
  59. template <typename T>
  60. struct relation< to_nearest<T> >
  61. {
  62. typedef T value_type;
  63. typedef to_nearest_tag tag;
  64. static inline T const& value(to_nearest<T> const& r) { return r.value; }
  65. static inline T & value(to_nearest<T> & r) { return r.value; }
  66. };
  67. template <typename T>
  68. struct relation< to_centroid<T> >
  69. {
  70. typedef T value_type;
  71. typedef to_centroid_tag tag;
  72. static inline T const& value(to_centroid<T> const& r) { return r.value; }
  73. static inline T & value(to_centroid<T> & r) { return r.value; }
  74. };
  75. template <typename T>
  76. struct relation< to_furthest<T> >
  77. {
  78. typedef T value_type;
  79. typedef to_furthest_tag tag;
  80. static inline T const& value(to_furthest<T> const& r) { return r.value; }
  81. static inline T & value(to_furthest<T> & r) { return r.value; }
  82. };
  83. // ------------------------------------------------------------------ //
  84. template
  85. <
  86. typename G1, typename G2, typename Strategy,
  87. typename Tag1 = typename geometry::tag<G1>::type,
  88. typename Tag2 = typename geometry::tag<G2>::type
  89. >
  90. struct comparable_distance_call_base
  91. {
  92. typedef typename geometry::default_comparable_distance_result
  93. <
  94. G1, G2
  95. >::type result_type;
  96. static inline result_type apply(G1 const& g1, G2 const& g2, Strategy const&)
  97. {
  98. return geometry::comparable_distance(g1, g2);
  99. }
  100. };
  101. template
  102. <
  103. typename G1, typename G2, typename Strategy
  104. >
  105. struct comparable_distance_call_base<G1, G2, Strategy, point_tag, point_tag>
  106. {
  107. typedef typename geometry::comparable_distance_result
  108. <
  109. G1, G2,
  110. typename Strategy::comparable_distance_point_point_strategy_type
  111. >::type result_type;
  112. static inline result_type apply(G1 const& g1, G2 const& g2, Strategy const& s)
  113. {
  114. return geometry::comparable_distance(g1, g2,
  115. s.get_comparable_distance_point_point_strategy());
  116. }
  117. };
  118. template
  119. <
  120. typename G1, typename G2, typename Strategy
  121. >
  122. struct comparable_distance_call_base<G1, G2, Strategy, point_tag, box_tag>
  123. {
  124. typedef typename geometry::comparable_distance_result
  125. <
  126. G1, G2,
  127. typename Strategy::comparable_distance_point_box_strategy_type
  128. >::type result_type;
  129. static inline result_type apply(G1 const& g1, G2 const& g2, Strategy const& s)
  130. {
  131. return geometry::comparable_distance(g1, g2,
  132. s.get_comparable_distance_point_box_strategy());
  133. }
  134. };
  135. template
  136. <
  137. typename G1, typename G2, typename Strategy
  138. >
  139. struct comparable_distance_call_base<G1, G2, Strategy, segment_tag, point_tag>
  140. {
  141. typedef typename geometry::comparable_distance_result
  142. <
  143. G1, G2,
  144. typename Strategy::comparable_distance_point_segment_strategy_type
  145. >::type result_type;
  146. static inline result_type apply(G1 const& g1, G2 const& g2, Strategy const& s)
  147. {
  148. return geometry::comparable_distance(g1, g2,
  149. s.get_comparable_distance_point_segment_strategy());
  150. }
  151. };
  152. template
  153. <
  154. typename G1, typename G2, typename Strategy
  155. >
  156. struct comparable_distance_call_base<G1, G2, Strategy, segment_tag, box_tag>
  157. {
  158. typedef typename geometry::comparable_distance_result
  159. <
  160. G1, G2,
  161. typename Strategy::comparable_distance_segment_box_strategy_type
  162. >::type result_type;
  163. static inline result_type apply(G1 const& g1, G2 const& g2, Strategy const& s)
  164. {
  165. return geometry::comparable_distance(g1, g2,
  166. s.get_comparable_distance_segment_box_strategy());
  167. }
  168. };
  169. template
  170. <
  171. typename G1, typename G2, typename Strategy
  172. >
  173. struct comparable_distance_call_base<G1, G2, Strategy, segment_tag, segment_tag>
  174. {
  175. typedef typename geometry::comparable_distance_result
  176. <
  177. G1, G2,
  178. typename Strategy::comparable_distance_point_segment_strategy_type
  179. >::type result_type;
  180. static inline result_type apply(G1 const& g1, G2 const& g2, Strategy const& s)
  181. {
  182. return geometry::comparable_distance(g1, g2,
  183. s.get_comparable_distance_point_segment_strategy());
  184. }
  185. };
  186. template
  187. <
  188. typename G1, typename G2, typename Strategy
  189. >
  190. struct comparable_distance_call
  191. : comparable_distance_call_base<G1, G2, Strategy>
  192. {};
  193. template
  194. <
  195. typename G1, typename G2
  196. >
  197. struct comparable_distance_call<G1, G2, default_strategy>
  198. : comparable_distance_call_base<G1, G2, default_strategy, void, void>
  199. {};
  200. // ------------------------------------------------------------------ //
  201. // calculate_distance
  202. // ------------------------------------------------------------------ //
  203. template <typename Predicate, typename Indexable, typename Strategy, typename Tag>
  204. struct calculate_distance
  205. {
  206. BOOST_MPL_ASSERT_MSG((false), INVALID_PREDICATE_OR_TAG, (calculate_distance));
  207. };
  208. // this handles nearest() with default Point parameter, to_nearest() and bounds
  209. template <typename PointRelation, typename Indexable, typename Strategy, typename Tag>
  210. struct calculate_distance< predicates::nearest<PointRelation>, Indexable, Strategy, Tag>
  211. {
  212. typedef detail::relation<PointRelation> relation;
  213. typedef comparable_distance_call
  214. <
  215. typename relation::value_type,
  216. Indexable,
  217. Strategy
  218. > call_type;
  219. typedef typename call_type::result_type result_type;
  220. static inline bool apply(predicates::nearest<PointRelation> const& p, Indexable const& i,
  221. Strategy const& s, result_type & result)
  222. {
  223. result = call_type::apply(relation::value(p.point_or_relation), i, s);
  224. return true;
  225. }
  226. };
  227. template <typename Point, typename Indexable, typename Strategy>
  228. struct calculate_distance< predicates::nearest< to_centroid<Point> >, Indexable, Strategy, value_tag>
  229. {
  230. typedef Point point_type;
  231. typedef typename geometry::default_comparable_distance_result
  232. <
  233. point_type, Indexable
  234. >::type result_type;
  235. static inline bool apply(predicates::nearest< to_centroid<Point> > const& p, Indexable const& i,
  236. Strategy const& , result_type & result)
  237. {
  238. result = index::detail::comparable_distance_centroid(p.point_or_relation.value, i);
  239. return true;
  240. }
  241. };
  242. template <typename Point, typename Indexable, typename Strategy>
  243. struct calculate_distance< predicates::nearest< to_furthest<Point> >, Indexable, Strategy, value_tag>
  244. {
  245. typedef Point point_type;
  246. typedef typename geometry::default_comparable_distance_result
  247. <
  248. point_type, Indexable
  249. >::type result_type;
  250. static inline bool apply(predicates::nearest< to_furthest<Point> > const& p, Indexable const& i,
  251. Strategy const& , result_type & result)
  252. {
  253. result = index::detail::comparable_distance_far(p.point_or_relation.value, i);
  254. return true;
  255. }
  256. };
  257. template <typename SegmentOrLinestring, typename Indexable, typename Strategy, typename Tag>
  258. struct calculate_distance< predicates::path<SegmentOrLinestring>, Indexable, Strategy, Tag>
  259. {
  260. typedef typename index::detail::default_path_intersection_distance_type<
  261. Indexable, SegmentOrLinestring
  262. >::type result_type;
  263. static inline bool apply(predicates::path<SegmentOrLinestring> const& p, Indexable const& i,
  264. Strategy const& , result_type & result)
  265. {
  266. return index::detail::path_intersection(i, p.geometry, result);
  267. }
  268. };
  269. }}}} // namespace boost::geometry::index::detail
  270. #endif // BOOST_GEOMETRY_INDEX_RTREE_DISTANCE_PREDICATES_HPP