distance_predicates.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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-2023.
  9. // Modifications copyright (c) 2019-2023, Oracle and/or its affiliates.
  10. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  11. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  12. //
  13. // Use, modification and distribution is subject to the Boost Software License,
  14. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_DISTANCE_PREDICATES_HPP
  17. #define BOOST_GEOMETRY_INDEX_DETAIL_DISTANCE_PREDICATES_HPP
  18. #include <boost/geometry/core/static_assert.hpp>
  19. #include <boost/geometry/algorithms/detail/covered_by/interface.hpp>
  20. #include <boost/geometry/algorithms/detail/disjoint/interface.hpp>
  21. #include <boost/geometry/algorithms/detail/intersects/interface.hpp>
  22. #include <boost/geometry/algorithms/detail/overlaps/interface.hpp>
  23. #include <boost/geometry/algorithms/detail/touches/interface.hpp>
  24. #include <boost/geometry/algorithms/detail/within/interface.hpp>
  25. #include <boost/geometry/index/detail/algorithms/comparable_distance_near.hpp>
  26. #include <boost/geometry/index/detail/algorithms/comparable_distance_far.hpp>
  27. #include <boost/geometry/index/detail/algorithms/comparable_distance_centroid.hpp>
  28. #include <boost/geometry/index/detail/algorithms/path_intersection.hpp>
  29. #include <boost/geometry/index/detail/predicates.hpp>
  30. #include <boost/geometry/index/detail/tags.hpp>
  31. namespace boost { namespace geometry { namespace index { namespace detail {
  32. // ------------------------------------------------------------------ //
  33. // relations
  34. // ------------------------------------------------------------------ //
  35. template <typename T>
  36. struct to_nearest
  37. {
  38. to_nearest(T const& v) : value(v) {}
  39. T value;
  40. };
  41. template <typename T>
  42. struct to_centroid
  43. {
  44. to_centroid(T const& v) : value(v) {}
  45. T value;
  46. };
  47. template <typename T>
  48. struct to_furthest
  49. {
  50. to_furthest(T const& v) : value(v) {}
  51. T value;
  52. };
  53. // tags
  54. struct to_nearest_tag {};
  55. struct to_centroid_tag {};
  56. struct to_furthest_tag {};
  57. // ------------------------------------------------------------------ //
  58. // relation traits and access
  59. // ------------------------------------------------------------------ //
  60. template <typename T>
  61. struct relation
  62. {
  63. typedef T value_type;
  64. typedef to_nearest_tag tag;
  65. static inline T const& value(T const& v) { return v; }
  66. static inline T & value(T & v) { return v; }
  67. };
  68. template <typename T>
  69. struct relation< to_nearest<T> >
  70. {
  71. typedef T value_type;
  72. typedef to_nearest_tag tag;
  73. static inline T const& value(to_nearest<T> const& r) { return r.value; }
  74. static inline T & value(to_nearest<T> & r) { return r.value; }
  75. };
  76. template <typename T>
  77. struct relation< to_centroid<T> >
  78. {
  79. typedef T value_type;
  80. typedef to_centroid_tag tag;
  81. static inline T const& value(to_centroid<T> const& r) { return r.value; }
  82. static inline T & value(to_centroid<T> & r) { return r.value; }
  83. };
  84. template <typename T>
  85. struct relation< to_furthest<T> >
  86. {
  87. typedef T value_type;
  88. typedef to_furthest_tag tag;
  89. static inline T const& value(to_furthest<T> const& r) { return r.value; }
  90. static inline T & value(to_furthest<T> & r) { return r.value; }
  91. };
  92. // ------------------------------------------------------------------ //
  93. template
  94. <
  95. typename G1, typename G2, typename Strategy
  96. >
  97. struct comparable_distance_call
  98. {
  99. typedef typename geometry::comparable_distance_result
  100. <
  101. G1, G2, Strategy
  102. >::type result_type;
  103. static inline result_type apply(G1 const& g1, G2 const& g2, Strategy const& s)
  104. {
  105. return geometry::comparable_distance(g1, g2, s);
  106. }
  107. };
  108. template
  109. <
  110. typename G1, typename G2
  111. >
  112. struct comparable_distance_call<G1, G2, default_strategy>
  113. {
  114. typedef typename geometry::default_comparable_distance_result
  115. <
  116. G1, G2
  117. >::type result_type;
  118. static inline result_type apply(G1 const& g1, G2 const& g2, default_strategy const&)
  119. {
  120. return geometry::comparable_distance(g1, g2);
  121. }
  122. };
  123. // ------------------------------------------------------------------ //
  124. // calculate_distance
  125. // ------------------------------------------------------------------ //
  126. template <typename Predicate, typename Indexable, typename Strategy, typename Tag>
  127. struct calculate_distance
  128. {
  129. BOOST_GEOMETRY_STATIC_ASSERT_FALSE(
  130. "Invalid Predicate or Tag.",
  131. Predicate, Indexable, Strategy, Tag);
  132. };
  133. // this handles nearest() with default Point parameter, to_nearest() and bounds
  134. template <typename PointRelation, typename Indexable, typename Strategy, typename Tag>
  135. struct calculate_distance< predicates::nearest<PointRelation>, Indexable, Strategy, Tag>
  136. {
  137. typedef detail::relation<PointRelation> relation;
  138. typedef comparable_distance_call
  139. <
  140. typename relation::value_type,
  141. Indexable,
  142. Strategy
  143. > call_type;
  144. typedef typename call_type::result_type result_type;
  145. static inline bool apply(predicates::nearest<PointRelation> const& p, Indexable const& i,
  146. Strategy const& s, result_type & result)
  147. {
  148. result = call_type::apply(relation::value(p.point_or_relation), i, s);
  149. return true;
  150. }
  151. };
  152. template <typename Point, typename Indexable, typename Strategy>
  153. struct calculate_distance< predicates::nearest< to_centroid<Point> >, Indexable, Strategy, value_tag>
  154. {
  155. typedef Point point_type;
  156. typedef typename geometry::default_comparable_distance_result
  157. <
  158. point_type, Indexable
  159. >::type result_type;
  160. static inline bool apply(predicates::nearest< to_centroid<Point> > const& p, Indexable const& i,
  161. Strategy const& , result_type & result)
  162. {
  163. result = index::detail::comparable_distance_centroid(p.point_or_relation.value, i);
  164. return true;
  165. }
  166. };
  167. template <typename Point, typename Indexable, typename Strategy>
  168. struct calculate_distance< predicates::nearest< to_furthest<Point> >, Indexable, Strategy, value_tag>
  169. {
  170. typedef Point point_type;
  171. typedef typename geometry::default_comparable_distance_result
  172. <
  173. point_type, Indexable
  174. >::type result_type;
  175. static inline bool apply(predicates::nearest< to_furthest<Point> > const& p, Indexable const& i,
  176. Strategy const& , result_type & result)
  177. {
  178. result = index::detail::comparable_distance_far(p.point_or_relation.value, i);
  179. return true;
  180. }
  181. };
  182. template <typename SegmentOrLinestring, typename Indexable, typename Strategy, typename Tag>
  183. struct calculate_distance< predicates::path<SegmentOrLinestring>, Indexable, Strategy, Tag>
  184. {
  185. typedef typename index::detail::default_path_intersection_distance_type<
  186. Indexable, SegmentOrLinestring
  187. >::type result_type;
  188. static inline bool apply(predicates::path<SegmentOrLinestring> const& p, Indexable const& i,
  189. Strategy const& , result_type & result)
  190. {
  191. return index::detail::path_intersection(i, p.geometry, result);
  192. }
  193. };
  194. }}}} // namespace boost::geometry::index::detail
  195. #endif // BOOST_GEOMETRY_INDEX_RTREE_DISTANCE_PREDICATES_HPP