distance_predicates.hpp 7.0 KB

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