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// 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
12#ifndef BOOST_GEOMETRY_INDEX_DETAIL_DISTANCE_PREDICATES_HPP
13#define BOOST_GEOMETRY_INDEX_DETAIL_DISTANCE_PREDICATES_HPP
14
15#include <boost/geometry/index/detail/algorithms/comparable_distance_near.hpp>
16#include <boost/geometry/index/detail/algorithms/comparable_distance_far.hpp>
17#include <boost/geometry/index/detail/algorithms/comparable_distance_centroid.hpp>
18#include <boost/geometry/index/detail/algorithms/path_intersection.hpp>
19
20#include <boost/geometry/index/detail/tags.hpp>
21
22namespace boost { namespace geometry { namespace index { namespace detail {
23
24// ------------------------------------------------------------------ //
25// relations
26// ------------------------------------------------------------------ //
27
28template <typename T>
29struct to_nearest
30{
31 to_nearest(T const& v) : value(v) {}
32 T value;
33};
34
35template <typename T>
36struct to_centroid
37{
38 to_centroid(T const& v) : value(v) {}
39 T value;
40};
41
42template <typename T>
43struct to_furthest
44{
45 to_furthest(T const& v) : value(v) {}
46 T value;
47};
48
49// tags
50
51struct to_nearest_tag {};
52struct to_centroid_tag {};
53struct to_furthest_tag {};
54
55// ------------------------------------------------------------------ //
56// relation traits and access
57// ------------------------------------------------------------------ //
58
59template <typename T>
60struct relation
61{
62 typedef T value_type;
63 typedef to_nearest_tag tag;
64 static inline T const& value(T const& v) { return v; }
65 static inline T & value(T & v) { return v; }
66};
67
68template <typename T>
69struct 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
77template <typename T>
78struct relation< to_centroid<T> >
79{
80 typedef T value_type;
81 typedef to_centroid_tag tag;
82 static inline T const& value(to_centroid<T> const& r) { return r.value; }
83 static inline T & value(to_centroid<T> & r) { return r.value; }
84};
85
86template <typename T>
87struct relation< to_furthest<T> >
88{
89 typedef T value_type;
90 typedef to_furthest_tag tag;
91 static inline T const& value(to_furthest<T> const& r) { return r.value; }
92 static inline T & value(to_furthest<T> & r) { return r.value; }
93};
94
95// ------------------------------------------------------------------ //
96// calculate_distance
97// ------------------------------------------------------------------ //
98
99template <typename Predicate, typename Indexable, typename Tag>
100struct calculate_distance
101{
102 BOOST_MPL_ASSERT_MSG((false), INVALID_PREDICATE_OR_TAG, (calculate_distance));
103};
104
105// this handles nearest() with default Point parameter, to_nearest() and bounds
106template <typename PointRelation, typename Indexable, typename Tag>
107struct calculate_distance< predicates::nearest<PointRelation>, Indexable, Tag >
108{
109 typedef detail::relation<PointRelation> relation;
110 typedef typename relation::value_type point_type;
111 typedef typename geometry::default_comparable_distance_result<point_type, Indexable>::type result_type;
112
113 static inline bool apply(predicates::nearest<PointRelation> const& p, Indexable const& i, result_type & result)
114 {
115 result = geometry::comparable_distance(relation::value(p.point_or_relation), i);
116 return true;
117 }
118};
119
120template <typename Point, typename Indexable>
121struct calculate_distance< predicates::nearest< to_centroid<Point> >, Indexable, value_tag>
122{
123 typedef Point point_type;
124 typedef typename geometry::default_comparable_distance_result<point_type, Indexable>::type result_type;
125
126 static inline bool apply(predicates::nearest< to_centroid<Point> > const& p, Indexable const& i, result_type & result)
127 {
128 result = index::detail::comparable_distance_centroid(p.point_or_relation.value, i);
129 return true;
130 }
131};
132
133template <typename Point, typename Indexable>
134struct calculate_distance< predicates::nearest< to_furthest<Point> >, Indexable, value_tag>
135{
136 typedef Point point_type;
137 typedef typename geometry::default_comparable_distance_result<point_type, Indexable>::type result_type;
138
139 static inline bool apply(predicates::nearest< to_furthest<Point> > const& p, Indexable const& i, result_type & result)
140 {
141 result = index::detail::comparable_distance_far(p.point_or_relation.value, i);
142 return true;
143 }
144};
145
146template <typename SegmentOrLinestring, typename Indexable, typename Tag>
147struct calculate_distance< predicates::path<SegmentOrLinestring>, Indexable, Tag>
148{
149 typedef typename index::detail::default_path_intersection_distance_type<
150 Indexable, SegmentOrLinestring
151 >::type result_type;
152
153 static inline bool apply(predicates::path<SegmentOrLinestring> const& p, Indexable const& i, result_type & result)
154 {
155 return index::detail::path_intersection(i, p.geometry, result);
156 }
157};
158
159}}}} // namespace boost::geometry::index::detail
160
161#endif // BOOST_GEOMETRY_INDEX_RTREE_DISTANCE_PREDICATES_HPP
162

source code of boost/boost/geometry/index/detail/distance_predicates.hpp