distance_predicates.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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-2013 Adam Wulkiewicz, Lodz, Poland.
  7. //
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_DISTANCE_PREDICATES_HPP
  12. #define BOOST_GEOMETRY_INDEX_DETAIL_DISTANCE_PREDICATES_HPP
  13. #include <boost/geometry/index/detail/algorithms/comparable_distance_near.hpp>
  14. #include <boost/geometry/index/detail/algorithms/comparable_distance_far.hpp>
  15. #include <boost/geometry/index/detail/algorithms/comparable_distance_centroid.hpp>
  16. #include <boost/geometry/index/detail/algorithms/path_intersection.hpp>
  17. #include <boost/geometry/index/detail/tags.hpp>
  18. namespace boost { namespace geometry { namespace index { namespace detail {
  19. // ------------------------------------------------------------------ //
  20. // relations
  21. // ------------------------------------------------------------------ //
  22. template <typename T>
  23. struct to_nearest
  24. {
  25. to_nearest(T const& v) : value(v) {}
  26. T value;
  27. };
  28. template <typename T>
  29. struct to_centroid
  30. {
  31. to_centroid(T const& v) : value(v) {}
  32. T value;
  33. };
  34. template <typename T>
  35. struct to_furthest
  36. {
  37. to_furthest(T const& v) : value(v) {}
  38. T value;
  39. };
  40. // tags
  41. struct to_nearest_tag {};
  42. struct to_centroid_tag {};
  43. struct to_furthest_tag {};
  44. // ------------------------------------------------------------------ //
  45. // relation traits and access
  46. // ------------------------------------------------------------------ //
  47. template <typename T>
  48. struct relation
  49. {
  50. typedef T value_type;
  51. typedef to_nearest_tag tag;
  52. static inline T const& value(T const& v) { return v; }
  53. static inline T & value(T & v) { return v; }
  54. };
  55. template <typename T>
  56. struct relation< to_nearest<T> >
  57. {
  58. typedef T value_type;
  59. typedef to_nearest_tag tag;
  60. static inline T const& value(to_nearest<T> const& r) { return r.value; }
  61. static inline T & value(to_nearest<T> & r) { return r.value; }
  62. };
  63. template <typename T>
  64. struct relation< to_centroid<T> >
  65. {
  66. typedef T value_type;
  67. typedef to_centroid_tag tag;
  68. static inline T const& value(to_centroid<T> const& r) { return r.value; }
  69. static inline T & value(to_centroid<T> & r) { return r.value; }
  70. };
  71. template <typename T>
  72. struct relation< to_furthest<T> >
  73. {
  74. typedef T value_type;
  75. typedef to_furthest_tag tag;
  76. static inline T const& value(to_furthest<T> const& r) { return r.value; }
  77. static inline T & value(to_furthest<T> & r) { return r.value; }
  78. };
  79. // ------------------------------------------------------------------ //
  80. // calculate_distance
  81. // ------------------------------------------------------------------ //
  82. template <typename Predicate, typename Indexable, typename Tag>
  83. struct calculate_distance
  84. {
  85. BOOST_MPL_ASSERT_MSG((false), INVALID_PREDICATE_OR_TAG, (calculate_distance));
  86. };
  87. // this handles nearest() with default Point parameter, to_nearest() and bounds
  88. template <typename PointRelation, typename Indexable, typename Tag>
  89. struct calculate_distance< nearest<PointRelation>, Indexable, Tag >
  90. {
  91. typedef detail::relation<PointRelation> relation;
  92. typedef typename relation::value_type point_type;
  93. typedef typename geometry::default_distance_result<point_type, Indexable>::type result_type;
  94. static inline bool apply(nearest<PointRelation> const& p, Indexable const& i, result_type & result)
  95. {
  96. result = index::detail::comparable_distance_near(relation::value(p.point_or_relation), i);
  97. return true;
  98. }
  99. };
  100. template <typename Point, typename Indexable>
  101. struct calculate_distance< nearest< to_centroid<Point> >, Indexable, value_tag>
  102. {
  103. typedef Point point_type;
  104. typedef typename geometry::default_distance_result<point_type, Indexable>::type result_type;
  105. static inline bool apply(nearest< to_centroid<Point> > const& p, Indexable const& i, result_type & result)
  106. {
  107. result = index::detail::comparable_distance_centroid(p.point_or_relation.value, i);
  108. return true;
  109. }
  110. };
  111. template <typename Point, typename Indexable>
  112. struct calculate_distance< nearest< to_furthest<Point> >, Indexable, value_tag>
  113. {
  114. typedef Point point_type;
  115. typedef typename geometry::default_distance_result<point_type, Indexable>::type result_type;
  116. static inline bool apply(nearest< to_furthest<Point> > const& p, Indexable const& i, result_type & result)
  117. {
  118. result = index::detail::comparable_distance_far(p.point_or_relation.value, i);
  119. return true;
  120. }
  121. };
  122. template <typename SegmentOrLinestring, typename Indexable, typename Tag>
  123. struct calculate_distance< path<SegmentOrLinestring>, Indexable, Tag>
  124. {
  125. typedef typename index::detail::default_path_intersection_distance_type<
  126. Indexable, SegmentOrLinestring
  127. >::type result_type;
  128. static inline bool apply(path<SegmentOrLinestring> const& p, Indexable const& i, result_type & result)
  129. {
  130. return index::detail::path_intersection(i, p.geometry, result);
  131. }
  132. };
  133. }}}} // namespace boost::geometry::index::detail
  134. #endif // BOOST_GEOMETRY_INDEX_RTREE_DISTANCE_PREDICATES_HPP