Chris@16
|
1 // Boost.Geometry Index
|
Chris@16
|
2 //
|
Chris@16
|
3 // R-tree initial packing
|
Chris@16
|
4 //
|
Chris@16
|
5 // Copyright (c) 2011-2013 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_RTREE_PACK_CREATE_HPP
|
Chris@16
|
12 #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_PACK_CREATE_HPP
|
Chris@16
|
13
|
Chris@16
|
14 namespace boost { namespace geometry { namespace index { namespace detail { namespace rtree {
|
Chris@16
|
15
|
Chris@16
|
16 namespace pack_utils {
|
Chris@16
|
17
|
Chris@16
|
18 template <std::size_t Dimension>
|
Chris@16
|
19 struct biggest_edge
|
Chris@16
|
20 {
|
Chris@16
|
21 BOOST_STATIC_ASSERT(0 < Dimension);
|
Chris@16
|
22 template <typename Box>
|
Chris@16
|
23 static inline void apply(Box const& box, typename coordinate_type<Box>::type & length, std::size_t & dim_index)
|
Chris@16
|
24 {
|
Chris@16
|
25 biggest_edge<Dimension-1>::apply(box, length, dim_index);
|
Chris@16
|
26 typename coordinate_type<Box>::type curr
|
Chris@16
|
27 = geometry::get<max_corner, Dimension-1>(box) - geometry::get<min_corner, Dimension-1>(box);
|
Chris@16
|
28 if ( length < curr )
|
Chris@16
|
29 {
|
Chris@16
|
30 dim_index = Dimension - 1;
|
Chris@16
|
31 length = curr;
|
Chris@16
|
32 }
|
Chris@16
|
33 }
|
Chris@16
|
34 };
|
Chris@16
|
35
|
Chris@16
|
36 template <>
|
Chris@16
|
37 struct biggest_edge<1>
|
Chris@16
|
38 {
|
Chris@16
|
39 template <typename Box>
|
Chris@16
|
40 static inline void apply(Box const& box, typename coordinate_type<Box>::type & length, std::size_t & dim_index)
|
Chris@16
|
41 {
|
Chris@16
|
42 dim_index = 0;
|
Chris@16
|
43 length = geometry::get<max_corner, 0>(box) - geometry::get<min_corner, 0>(box);
|
Chris@16
|
44 }
|
Chris@16
|
45 };
|
Chris@16
|
46
|
Chris@16
|
47 template <std::size_t I>
|
Chris@16
|
48 struct point_entries_comparer
|
Chris@16
|
49 {
|
Chris@16
|
50 template <typename PointEntry>
|
Chris@16
|
51 bool operator()(PointEntry const& e1, PointEntry const& e2) const
|
Chris@16
|
52 {
|
Chris@16
|
53 return geometry::get<I>(e1.first) < geometry::get<I>(e2.first);
|
Chris@16
|
54 }
|
Chris@16
|
55 };
|
Chris@16
|
56
|
Chris@16
|
57 template <std::size_t I, std::size_t Dimension>
|
Chris@16
|
58 struct partial_sort_and_half_boxes
|
Chris@16
|
59 {
|
Chris@16
|
60 template <typename EIt, typename Box>
|
Chris@16
|
61 static inline void apply(EIt first, EIt median, EIt last, Box const& box, Box & left, Box & right, std::size_t dim_index)
|
Chris@16
|
62 {
|
Chris@16
|
63 if ( I == dim_index )
|
Chris@16
|
64 {
|
Chris@16
|
65 std::partial_sort(first, median, last, point_entries_comparer<I>());
|
Chris@16
|
66
|
Chris@16
|
67 geometry::convert(box, left);
|
Chris@16
|
68 geometry::convert(box, right);
|
Chris@16
|
69 typename coordinate_type<Box>::type edge_len
|
Chris@16
|
70 = geometry::get<max_corner, I>(box) - geometry::get<min_corner, I>(box);
|
Chris@16
|
71 typename coordinate_type<Box>::type median
|
Chris@16
|
72 = geometry::get<min_corner, I>(box) + edge_len / 2;
|
Chris@16
|
73 geometry::set<max_corner, I>(left, median);
|
Chris@16
|
74 geometry::set<min_corner, I>(right, median);
|
Chris@16
|
75 }
|
Chris@16
|
76 else
|
Chris@16
|
77 partial_sort_and_half_boxes<I+1, Dimension>::apply(first, median, last, box, left, right, dim_index);
|
Chris@16
|
78 }
|
Chris@16
|
79 };
|
Chris@16
|
80
|
Chris@16
|
81 template <std::size_t Dimension>
|
Chris@16
|
82 struct partial_sort_and_half_boxes<Dimension, Dimension>
|
Chris@16
|
83 {
|
Chris@16
|
84 template <typename EIt, typename Box>
|
Chris@16
|
85 static inline void apply(EIt , EIt , EIt , Box const& , Box & , Box & , std::size_t ) {}
|
Chris@16
|
86 };
|
Chris@16
|
87
|
Chris@16
|
88 } // namespace pack_utils
|
Chris@16
|
89
|
Chris@16
|
90 // STR leafs number are calculated as rcount/max
|
Chris@16
|
91 // and the number of splitting planes for each dimension as (count/max)^(1/dimension)
|
Chris@16
|
92 // <-> for dimension==2 -> sqrt(count/max)
|
Chris@16
|
93 //
|
Chris@16
|
94 // The main flaw of this algorithm is that the resulting tree will have bad structure for:
|
Chris@16
|
95 // 1. non-uniformly distributed elements
|
Chris@16
|
96 // Statistic check could be performed, e.g. based on variance of lengths of elements edges for each dimension
|
Chris@16
|
97 // 2. elements distributed mainly along one axis
|
Chris@16
|
98 // Calculate bounding box of all elements and then number of dividing planes for a dimension
|
Chris@16
|
99 // from the length of BB edge for this dimension (more or less assuming that elements are uniformly-distributed squares)
|
Chris@16
|
100 //
|
Chris@16
|
101 // Another thing is that the last node may have less elements than Max or even Min.
|
Chris@16
|
102 // The number of splitting planes must be chosen more carefully than count/max
|
Chris@16
|
103 //
|
Chris@16
|
104 // This algorithm is something between STR and TGS
|
Chris@16
|
105 // it is more similar to the top-down recursive kd-tree creation algorithm
|
Chris@16
|
106 // using object median split and split axis of greatest BB edge
|
Chris@16
|
107 // BB is only used as a hint (assuming objects are distributed uniformly)
|
Chris@16
|
108 //
|
Chris@16
|
109 // Implemented algorithm guarantees that the number of elements in nodes will be between Min and Max
|
Chris@16
|
110 // and that nodes are packed as tightly as possible
|
Chris@16
|
111 // e.g. for 177 values Max = 5 and Min = 2 it will construct the following tree:
|
Chris@16
|
112 // ROOT 177
|
Chris@16
|
113 // L1 125 52
|
Chris@16
|
114 // L2 25 25 25 25 25 25 17 10
|
Chris@16
|
115 // L3 5x5 5x5 5x5 5x5 5x5 5x5 3x5+2 2x5
|
Chris@16
|
116
|
Chris@16
|
117 template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
|
Chris@16
|
118 class pack
|
Chris@16
|
119 {
|
Chris@16
|
120 typedef typename rtree::node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type node;
|
Chris@16
|
121 typedef typename rtree::internal_node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
|
Chris@16
|
122 typedef typename rtree::leaf<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
|
Chris@16
|
123
|
Chris@16
|
124 typedef typename Allocators::node_pointer node_pointer;
|
Chris@16
|
125 typedef rtree::node_auto_ptr<Value, Options, Translator, Box, Allocators> node_auto_ptr;
|
Chris@16
|
126 typedef typename Allocators::size_type size_type;
|
Chris@16
|
127
|
Chris@16
|
128 typedef typename traits::point_type<Box>::type point_type;
|
Chris@16
|
129 typedef typename traits::coordinate_type<point_type>::type coordinate_type;
|
Chris@16
|
130 typedef typename detail::default_content_result<Box>::type content_type;
|
Chris@16
|
131 typedef typename Options::parameters_type parameters_type;
|
Chris@16
|
132 static const std::size_t dimension = traits::dimension<point_type>::value;
|
Chris@16
|
133
|
Chris@16
|
134 typedef typename rtree::container_from_elements_type<
|
Chris@16
|
135 typename rtree::elements_type<leaf>::type,
|
Chris@16
|
136 std::size_t
|
Chris@16
|
137 >::type values_counts_container;
|
Chris@16
|
138
|
Chris@16
|
139 typedef typename rtree::elements_type<internal_node>::type internal_elements;
|
Chris@16
|
140 typedef typename internal_elements::value_type internal_element;
|
Chris@16
|
141
|
Chris@16
|
142 public:
|
Chris@16
|
143 // Arbitrary iterators
|
Chris@16
|
144 template <typename InIt> inline static
|
Chris@16
|
145 node_pointer apply(InIt first, InIt last, size_type & values_count, size_type & leafs_level,
|
Chris@16
|
146 parameters_type const& parameters, Translator const& translator, Allocators & allocators)
|
Chris@16
|
147 {
|
Chris@16
|
148 typedef typename std::iterator_traits<InIt>::difference_type diff_type;
|
Chris@16
|
149
|
Chris@16
|
150 diff_type diff = std::distance(first, last);
|
Chris@16
|
151 if ( diff <= 0 )
|
Chris@16
|
152 return node_pointer(0);
|
Chris@16
|
153
|
Chris@16
|
154 typedef std::pair<point_type, InIt> entry_type;
|
Chris@16
|
155 std::vector<entry_type> entries;
|
Chris@16
|
156
|
Chris@16
|
157 values_count = static_cast<size_type>(diff);
|
Chris@16
|
158 entries.reserve(values_count);
|
Chris@16
|
159
|
Chris@16
|
160 Box hint_box;
|
Chris@16
|
161 geometry::assign_inverse(hint_box);
|
Chris@16
|
162 for ( ; first != last ; ++first )
|
Chris@16
|
163 {
|
Chris@16
|
164 geometry::expand(hint_box, translator(*first));
|
Chris@16
|
165
|
Chris@16
|
166 point_type pt;
|
Chris@16
|
167 geometry::centroid(translator(*first), pt);
|
Chris@16
|
168 entries.push_back(std::make_pair(pt, first));
|
Chris@16
|
169 }
|
Chris@16
|
170
|
Chris@16
|
171 subtree_elements_counts subtree_counts = calculate_subtree_elements_counts(values_count, parameters, leafs_level);
|
Chris@16
|
172 internal_element el = per_level(entries.begin(), entries.end(), hint_box, values_count, subtree_counts,
|
Chris@16
|
173 parameters, translator, allocators);
|
Chris@16
|
174
|
Chris@16
|
175 return el.second;
|
Chris@16
|
176 }
|
Chris@16
|
177
|
Chris@16
|
178 private:
|
Chris@16
|
179 struct subtree_elements_counts
|
Chris@16
|
180 {
|
Chris@16
|
181 subtree_elements_counts(std::size_t ma, std::size_t mi) : maxc(ma), minc(mi) {}
|
Chris@16
|
182 std::size_t maxc;
|
Chris@16
|
183 std::size_t minc;
|
Chris@16
|
184 };
|
Chris@16
|
185
|
Chris@16
|
186 template <typename EIt> inline static
|
Chris@16
|
187 internal_element per_level(EIt first, EIt last, Box const& hint_box, std::size_t values_count, subtree_elements_counts const& subtree_counts,
|
Chris@16
|
188 parameters_type const& parameters, Translator const& translator, Allocators & allocators)
|
Chris@16
|
189 {
|
Chris@16
|
190 BOOST_ASSERT(0 < std::distance(first, last) && static_cast<std::size_t>(std::distance(first, last)) == values_count);
|
Chris@16
|
191
|
Chris@16
|
192 if ( subtree_counts.maxc <= 1 )
|
Chris@16
|
193 {
|
Chris@16
|
194 // ROOT or LEAF
|
Chris@16
|
195 BOOST_ASSERT(values_count <= parameters.get_max_elements());
|
Chris@16
|
196 // if !root check m_parameters.get_min_elements() <= count
|
Chris@16
|
197
|
Chris@16
|
198 // create new leaf node
|
Chris@16
|
199 node_pointer n = rtree::create_node<Allocators, leaf>::apply(allocators); // MAY THROW (A)
|
Chris@16
|
200 node_auto_ptr auto_remover(n, allocators);
|
Chris@16
|
201 leaf & l = rtree::get<leaf>(*n);
|
Chris@16
|
202
|
Chris@16
|
203 // reserve space for values
|
Chris@16
|
204 rtree::elements(l).reserve(values_count); // MAY THROW (A)
|
Chris@16
|
205 // calculate values box and copy values
|
Chris@16
|
206 Box elements_box;
|
Chris@16
|
207 geometry::assign_inverse(elements_box);
|
Chris@16
|
208 for ( ; first != last ; ++first )
|
Chris@16
|
209 {
|
Chris@16
|
210 rtree::elements(l).push_back(*(first->second)); // MAY THROW (A?,C)
|
Chris@16
|
211 geometry::expand(elements_box, translator(*(first->second)));
|
Chris@16
|
212 }
|
Chris@16
|
213
|
Chris@16
|
214 auto_remover.release();
|
Chris@16
|
215 return internal_element(elements_box, n);
|
Chris@16
|
216 }
|
Chris@16
|
217
|
Chris@16
|
218 // calculate next max and min subtree counts
|
Chris@16
|
219 subtree_elements_counts next_subtree_counts = subtree_counts;
|
Chris@16
|
220 next_subtree_counts.maxc /= parameters.get_max_elements();
|
Chris@16
|
221 next_subtree_counts.minc /= parameters.get_max_elements();
|
Chris@16
|
222
|
Chris@16
|
223 // create new internal node
|
Chris@16
|
224 node_pointer n = rtree::create_node<Allocators, internal_node>::apply(allocators); // MAY THROW (A)
|
Chris@16
|
225 node_auto_ptr auto_remover(n, allocators);
|
Chris@16
|
226 internal_node & in = rtree::get<internal_node>(*n);
|
Chris@16
|
227
|
Chris@16
|
228 // reserve space for values
|
Chris@16
|
229 std::size_t nodes_count = calculate_nodes_count(values_count, subtree_counts);
|
Chris@16
|
230 rtree::elements(in).reserve(nodes_count); // MAY THROW (A)
|
Chris@16
|
231 // calculate values box and copy values
|
Chris@16
|
232 Box elements_box;
|
Chris@16
|
233 geometry::assign_inverse(elements_box);
|
Chris@16
|
234
|
Chris@16
|
235 per_level_packets(first, last, hint_box, values_count, subtree_counts, next_subtree_counts,
|
Chris@16
|
236 rtree::elements(in), elements_box,
|
Chris@16
|
237 parameters, translator, allocators);
|
Chris@16
|
238
|
Chris@16
|
239 auto_remover.release();
|
Chris@16
|
240 return internal_element(elements_box, n);
|
Chris@16
|
241 }
|
Chris@16
|
242
|
Chris@16
|
243 template <typename EIt> inline static
|
Chris@16
|
244 void per_level_packets(EIt first, EIt last, Box const& hint_box,
|
Chris@16
|
245 std::size_t values_count,
|
Chris@16
|
246 subtree_elements_counts const& subtree_counts,
|
Chris@16
|
247 subtree_elements_counts const& next_subtree_counts,
|
Chris@16
|
248 internal_elements & elements, Box & elements_box,
|
Chris@16
|
249 parameters_type const& parameters, Translator const& translator, Allocators & allocators)
|
Chris@16
|
250 {
|
Chris@16
|
251 BOOST_ASSERT(0 < std::distance(first, last) && static_cast<std::size_t>(std::distance(first, last)) == values_count);
|
Chris@16
|
252
|
Chris@16
|
253 BOOST_ASSERT_MSG( subtree_counts.minc <= values_count, "too small number of elements");
|
Chris@16
|
254
|
Chris@16
|
255 // only one packet
|
Chris@16
|
256 if ( values_count <= subtree_counts.maxc )
|
Chris@16
|
257 {
|
Chris@16
|
258 // the end, move to the next level
|
Chris@16
|
259 internal_element el = per_level(first, last, hint_box, values_count, next_subtree_counts,
|
Chris@16
|
260 parameters, translator, allocators);
|
Chris@16
|
261
|
Chris@16
|
262 // in case if push_back() do throw here
|
Chris@16
|
263 // and even if this is not probable (previously reserved memory, nonthrowing pairs copy)
|
Chris@16
|
264 // this case is also tested by exceptions test.
|
Chris@16
|
265 node_auto_ptr auto_remover(el.second, allocators);
|
Chris@16
|
266 // this container should have memory allocated, reserve() called outside
|
Chris@16
|
267 elements.push_back(el); // MAY THROW (A?,C) - however in normal conditions shouldn't
|
Chris@16
|
268 auto_remover.release();
|
Chris@16
|
269
|
Chris@16
|
270 geometry::expand(elements_box, el.first);
|
Chris@16
|
271 return;
|
Chris@16
|
272 }
|
Chris@16
|
273
|
Chris@16
|
274 std::size_t median_count = calculate_median_count(values_count, subtree_counts);
|
Chris@16
|
275 EIt median = first + median_count;
|
Chris@16
|
276
|
Chris@16
|
277 coordinate_type greatest_length;
|
Chris@16
|
278 std::size_t greatest_dim_index = 0;
|
Chris@16
|
279 pack_utils::biggest_edge<dimension>::apply(hint_box, greatest_length, greatest_dim_index);
|
Chris@16
|
280 Box left, right;
|
Chris@16
|
281 pack_utils::partial_sort_and_half_boxes<0, dimension>
|
Chris@16
|
282 ::apply(first, median, last, hint_box, left, right, greatest_dim_index);
|
Chris@16
|
283
|
Chris@16
|
284 per_level_packets(first, median, left,
|
Chris@16
|
285 median_count, subtree_counts, next_subtree_counts,
|
Chris@16
|
286 elements, elements_box,
|
Chris@16
|
287 parameters, translator, allocators);
|
Chris@16
|
288 per_level_packets(median, last, right,
|
Chris@16
|
289 values_count - median_count, subtree_counts, next_subtree_counts,
|
Chris@16
|
290 elements, elements_box,
|
Chris@16
|
291 parameters, translator, allocators);
|
Chris@16
|
292 }
|
Chris@16
|
293
|
Chris@16
|
294 inline static
|
Chris@16
|
295 subtree_elements_counts calculate_subtree_elements_counts(std::size_t elements_count, parameters_type const& parameters, size_type & leafs_level)
|
Chris@16
|
296 {
|
Chris@16
|
297 (void)parameters;
|
Chris@16
|
298
|
Chris@16
|
299 subtree_elements_counts res(1, 1);
|
Chris@16
|
300 leafs_level = 0;
|
Chris@16
|
301
|
Chris@16
|
302 std::size_t smax = parameters.get_max_elements();
|
Chris@16
|
303 for ( ; smax < elements_count ; smax *= parameters.get_max_elements(), ++leafs_level )
|
Chris@16
|
304 res.maxc = smax;
|
Chris@16
|
305
|
Chris@16
|
306 res.minc = parameters.get_min_elements() * (res.maxc / parameters.get_max_elements());
|
Chris@16
|
307
|
Chris@16
|
308 return res;
|
Chris@16
|
309 }
|
Chris@16
|
310
|
Chris@16
|
311 inline static
|
Chris@16
|
312 std::size_t calculate_nodes_count(std::size_t count,
|
Chris@16
|
313 subtree_elements_counts const& subtree_counts)
|
Chris@16
|
314 {
|
Chris@16
|
315 std::size_t n = count / subtree_counts.maxc;
|
Chris@16
|
316 std::size_t r = count % subtree_counts.maxc;
|
Chris@16
|
317
|
Chris@16
|
318 if ( 0 < r && r < subtree_counts.minc )
|
Chris@16
|
319 {
|
Chris@16
|
320 std::size_t count_minus_min = count - subtree_counts.minc;
|
Chris@16
|
321 n = count_minus_min / subtree_counts.maxc;
|
Chris@16
|
322 r = count_minus_min % subtree_counts.maxc;
|
Chris@16
|
323 ++n;
|
Chris@16
|
324 }
|
Chris@16
|
325
|
Chris@16
|
326 if ( 0 < r )
|
Chris@16
|
327 ++n;
|
Chris@16
|
328
|
Chris@16
|
329 return n;
|
Chris@16
|
330 }
|
Chris@16
|
331
|
Chris@16
|
332 inline static
|
Chris@16
|
333 std::size_t calculate_median_count(std::size_t count,
|
Chris@16
|
334 subtree_elements_counts const& subtree_counts)
|
Chris@16
|
335 {
|
Chris@16
|
336 // e.g. for max = 5, min = 2, count = 52, subtree_max = 25, subtree_min = 10
|
Chris@16
|
337
|
Chris@16
|
338 std::size_t n = count / subtree_counts.maxc; // e.g. 52 / 25 = 2
|
Chris@16
|
339 std::size_t r = count % subtree_counts.maxc; // e.g. 52 % 25 = 2
|
Chris@16
|
340 std::size_t median_count = (n / 2) * subtree_counts.maxc; // e.g. 2 / 2 * 25 = 25
|
Chris@16
|
341
|
Chris@16
|
342 if ( 0 != r ) // e.g. 0 != 2
|
Chris@16
|
343 {
|
Chris@16
|
344 if ( subtree_counts.minc <= r ) // e.g. 10 <= 2 == false
|
Chris@16
|
345 {
|
Chris@16
|
346 //BOOST_ASSERT_MSG(0 < n, "unexpected value");
|
Chris@16
|
347 median_count = ((n+1)/2) * subtree_counts.maxc; // if calculated ((2+1)/2) * 25 which would be ok, but not in all cases
|
Chris@16
|
348 }
|
Chris@16
|
349 else // r < subtree_counts.second // e.g. 2 < 10 == true
|
Chris@16
|
350 {
|
Chris@16
|
351 std::size_t count_minus_min = count - subtree_counts.minc; // e.g. 52 - 10 = 42
|
Chris@16
|
352 n = count_minus_min / subtree_counts.maxc; // e.g. 42 / 25 = 1
|
Chris@16
|
353 r = count_minus_min % subtree_counts.maxc; // e.g. 42 % 25 = 17
|
Chris@16
|
354 if ( r == 0 ) // e.g. false
|
Chris@16
|
355 {
|
Chris@16
|
356 // n can't be equal to 0 because then there wouldn't be any element in the other node
|
Chris@16
|
357 //BOOST_ASSERT_MSG(0 < n, "unexpected value");
|
Chris@16
|
358 median_count = ((n+1)/2) * subtree_counts.maxc; // if calculated ((1+1)/2) * 25 which would be ok, but not in all cases
|
Chris@16
|
359 }
|
Chris@16
|
360 else
|
Chris@16
|
361 {
|
Chris@16
|
362 if ( n == 0 ) // e.g. false
|
Chris@16
|
363 median_count = r; // if calculated -> 17 which is wrong!
|
Chris@16
|
364 else
|
Chris@16
|
365 median_count = ((n+2)/2) * subtree_counts.maxc; // e.g. ((1+2)/2) * 25 = 25
|
Chris@16
|
366 }
|
Chris@16
|
367 }
|
Chris@16
|
368 }
|
Chris@16
|
369
|
Chris@16
|
370 return median_count;
|
Chris@16
|
371 }
|
Chris@16
|
372 };
|
Chris@16
|
373
|
Chris@16
|
374 }}}}} // namespace boost::geometry::index::detail::rtree
|
Chris@16
|
375
|
Chris@16
|
376 #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_PACK_CREATE_HPP
|