Chris@16
|
1 // Boost.Geometry Index
|
Chris@16
|
2 //
|
Chris@16
|
3 // minmaxdist used in R-tree k nearest neighbors query
|
Chris@16
|
4 //
|
Chris@101
|
5 // Copyright (c) 2011-2014 Adam Wulkiewicz, Lodz, Poland.
|
Chris@16
|
6 //
|
Chris@16
|
7 // Use, modification and distribution is subject to the Boost Software License,
|
Chris@16
|
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
9 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_MINMAXDIST_HPP
|
Chris@16
|
12 #define BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_MINMAXDIST_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/geometry/algorithms/distance.hpp>
|
Chris@16
|
15 #include <boost/geometry/algorithms/comparable_distance.hpp>
|
Chris@16
|
16
|
Chris@16
|
17 #include <boost/geometry/index/detail/algorithms/diff_abs.hpp>
|
Chris@16
|
18 #include <boost/geometry/index/detail/algorithms/sum_for_indexable.hpp>
|
Chris@16
|
19 #include <boost/geometry/index/detail/algorithms/smallest_for_indexable.hpp>
|
Chris@16
|
20
|
Chris@16
|
21 namespace boost { namespace geometry { namespace index { namespace detail {
|
Chris@16
|
22
|
Chris@16
|
23 struct minmaxdist_tag {};
|
Chris@16
|
24
|
Chris@16
|
25 template <
|
Chris@16
|
26 typename Point,
|
Chris@16
|
27 typename BoxIndexable,
|
Chris@16
|
28 size_t DimensionIndex>
|
Chris@16
|
29 struct smallest_for_indexable_dimension<Point, BoxIndexable, box_tag, minmaxdist_tag, DimensionIndex>
|
Chris@16
|
30 {
|
Chris@101
|
31 typedef typename geometry::default_comparable_distance_result<Point, BoxIndexable>::type result_type;
|
Chris@16
|
32
|
Chris@16
|
33 inline static result_type apply(Point const& pt, BoxIndexable const& i, result_type const& maxd)
|
Chris@16
|
34 {
|
Chris@16
|
35 typedef typename coordinate_type<Point>::type point_coord_t;
|
Chris@16
|
36 typedef typename coordinate_type<BoxIndexable>::type indexable_coord_t;
|
Chris@16
|
37
|
Chris@16
|
38 point_coord_t pt_c = geometry::get<DimensionIndex>(pt);
|
Chris@16
|
39 indexable_coord_t ind_c_min = geometry::get<geometry::min_corner, DimensionIndex>(i);
|
Chris@16
|
40 indexable_coord_t ind_c_max = geometry::get<geometry::max_corner, DimensionIndex>(i);
|
Chris@16
|
41
|
Chris@16
|
42 indexable_coord_t ind_c_avg = ind_c_min + (ind_c_max - ind_c_min) / 2;
|
Chris@16
|
43 // TODO: awulkiew - is (ind_c_min + ind_c_max) / 2 safe?
|
Chris@16
|
44
|
Chris@16
|
45 // TODO: awulkiew - optimize! don't calculate 2x pt_c <= ind_c_avg
|
Chris@16
|
46 // take particular case pt_c == ind_c_avg into account
|
Chris@16
|
47
|
Chris@16
|
48 result_type closer_comp = 0;
|
Chris@16
|
49 if ( pt_c <= ind_c_avg )
|
Chris@16
|
50 closer_comp = detail::diff_abs(pt_c, ind_c_min); // unsigned values protection
|
Chris@16
|
51 else
|
Chris@16
|
52 closer_comp = ind_c_max - pt_c;
|
Chris@16
|
53
|
Chris@16
|
54 result_type further_comp = 0;
|
Chris@16
|
55 if ( ind_c_avg <= pt_c )
|
Chris@16
|
56 further_comp = pt_c - ind_c_min;
|
Chris@16
|
57 else
|
Chris@16
|
58 further_comp = detail::diff_abs(pt_c, ind_c_max); // unsigned values protection
|
Chris@16
|
59
|
Chris@16
|
60 return (maxd + closer_comp * closer_comp) - further_comp * further_comp;
|
Chris@16
|
61 }
|
Chris@16
|
62 };
|
Chris@16
|
63
|
Chris@16
|
64 template <typename Point, typename Indexable, typename IndexableTag>
|
Chris@16
|
65 struct minmaxdist_impl
|
Chris@16
|
66 {
|
Chris@16
|
67 BOOST_MPL_ASSERT_MSG(
|
Chris@16
|
68 (false),
|
Chris@16
|
69 NOT_IMPLEMENTED_FOR_THIS_INDEXABLE_TAG_TYPE,
|
Chris@16
|
70 (minmaxdist_impl));
|
Chris@16
|
71 };
|
Chris@16
|
72
|
Chris@16
|
73 template <typename Point, typename Indexable>
|
Chris@16
|
74 struct minmaxdist_impl<Point, Indexable, point_tag>
|
Chris@16
|
75 {
|
Chris@101
|
76 typedef typename geometry::default_comparable_distance_result<Point, Indexable>::type result_type;
|
Chris@16
|
77
|
Chris@16
|
78 inline static result_type apply(Point const& pt, Indexable const& i)
|
Chris@16
|
79 {
|
Chris@16
|
80 return geometry::comparable_distance(pt, i);
|
Chris@16
|
81 }
|
Chris@16
|
82 };
|
Chris@16
|
83
|
Chris@16
|
84 template <typename Point, typename Indexable>
|
Chris@16
|
85 struct minmaxdist_impl<Point, Indexable, box_tag>
|
Chris@16
|
86 {
|
Chris@101
|
87 typedef typename geometry::default_comparable_distance_result<Point, Indexable>::type result_type;
|
Chris@16
|
88
|
Chris@16
|
89 inline static result_type apply(Point const& pt, Indexable const& i)
|
Chris@16
|
90 {
|
Chris@16
|
91 result_type maxd = geometry::comparable_distance(pt, i);
|
Chris@16
|
92
|
Chris@16
|
93 return smallest_for_indexable<
|
Chris@16
|
94 Point,
|
Chris@16
|
95 Indexable,
|
Chris@16
|
96 box_tag,
|
Chris@16
|
97 minmaxdist_tag,
|
Chris@16
|
98 dimension<Indexable>::value
|
Chris@16
|
99 >::apply(pt, i, maxd);
|
Chris@16
|
100 }
|
Chris@16
|
101 };
|
Chris@16
|
102
|
Chris@16
|
103 /**
|
Chris@16
|
104 * This is comparable distace.
|
Chris@16
|
105 */
|
Chris@16
|
106 template <typename Point, typename Indexable>
|
Chris@101
|
107 typename geometry::default_comparable_distance_result<Point, Indexable>::type
|
Chris@16
|
108 minmaxdist(Point const& pt, Indexable const& i)
|
Chris@16
|
109 {
|
Chris@16
|
110 return detail::minmaxdist_impl<
|
Chris@16
|
111 Point,
|
Chris@16
|
112 Indexable,
|
Chris@16
|
113 typename tag<Indexable>::type
|
Chris@16
|
114 >::apply(pt, i);
|
Chris@16
|
115 }
|
Chris@16
|
116
|
Chris@16
|
117 }}}} // namespace boost::geometry::index::detail
|
Chris@16
|
118
|
Chris@16
|
119 #endif // BOOST_GEOMETRY_INDEX_DETAIL_ALGORITHMS_MINMAXDIST_HPP
|