1 // Boost.Geometry Index
3 // R-tree initial packing
5 // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_PACK_CREATE_HPP
12 #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_PACK_CREATE_HPP
14 #include <boost/geometry/algorithms/expand.hpp>
15 #include <boost/geometry/index/detail/algorithms/bounds.hpp>
17 #include <boost/geometry/algorithms/detail/expand_by_epsilon.hpp>
19 namespace boost { namespace geometry { namespace index { namespace detail { namespace rtree {
21 namespace pack_utils {
23 template <std::size_t Dimension>
26 BOOST_STATIC_ASSERT(0 < Dimension);
27 template <typename Box>
28 static inline void apply(Box const& box, typename coordinate_type<Box>::type & length, std::size_t & dim_index)
30 biggest_edge<Dimension-1>::apply(box, length, dim_index);
31 typename coordinate_type<Box>::type curr
32 = geometry::get<max_corner, Dimension-1>(box) - geometry::get<min_corner, Dimension-1>(box);
35 dim_index = Dimension - 1;
42 struct biggest_edge<1>
44 template <typename Box>
45 static inline void apply(Box const& box, typename coordinate_type<Box>::type & length, std::size_t & dim_index)
48 length = geometry::get<max_corner, 0>(box) - geometry::get<min_corner, 0>(box);
52 template <std::size_t I>
53 struct point_entries_comparer
55 template <typename PointEntry>
56 bool operator()(PointEntry const& e1, PointEntry const& e2) const
58 return geometry::get<I>(e1.first) < geometry::get<I>(e2.first);
62 template <std::size_t I, std::size_t Dimension>
63 struct nth_element_and_half_boxes
65 template <typename EIt, typename Box>
66 static inline void apply(EIt first, EIt median, EIt last, Box const& box, Box & left, Box & right, std::size_t dim_index)
70 std::nth_element(first, median, last, point_entries_comparer<I>());
72 geometry::convert(box, left);
73 geometry::convert(box, right);
74 typename coordinate_type<Box>::type edge_len
75 = geometry::get<max_corner, I>(box) - geometry::get<min_corner, I>(box);
76 typename coordinate_type<Box>::type median
77 = geometry::get<min_corner, I>(box) + edge_len / 2;
78 geometry::set<max_corner, I>(left, median);
79 geometry::set<min_corner, I>(right, median);
82 nth_element_and_half_boxes<I+1, Dimension>::apply(first, median, last, box, left, right, dim_index);
86 template <std::size_t Dimension>
87 struct nth_element_and_half_boxes<Dimension, Dimension>
89 template <typename EIt, typename Box>
90 static inline void apply(EIt , EIt , EIt , Box const& , Box & , Box & , std::size_t ) {}
93 } // namespace pack_utils
95 // STR leafs number are calculated as rcount/max
96 // and the number of splitting planes for each dimension as (count/max)^(1/dimension)
97 // <-> for dimension==2 -> sqrt(count/max)
99 // The main flaw of this algorithm is that the resulting tree will have bad structure for:
100 // 1. non-uniformly distributed elements
101 // Statistic check could be performed, e.g. based on variance of lengths of elements edges for each dimension
102 // 2. elements distributed mainly along one axis
103 // Calculate bounding box of all elements and then number of dividing planes for a dimension
104 // from the length of BB edge for this dimension (more or less assuming that elements are uniformly-distributed squares)
106 // Another thing is that the last node may have less elements than Max or even Min.
107 // The number of splitting planes must be chosen more carefully than count/max
109 // This algorithm is something between STR and TGS
110 // it is more similar to the top-down recursive kd-tree creation algorithm
111 // using object median split and split axis of greatest BB edge
112 // BB is only used as a hint (assuming objects are distributed uniformly)
114 // Implemented algorithm guarantees that the number of elements in nodes will be between Min and Max
115 // and that nodes are packed as tightly as possible
116 // e.g. for 177 values Max = 5 and Min = 2 it will construct the following tree:
119 // L2 25 25 25 25 25 25 17 10
120 // L3 5x5 5x5 5x5 5x5 5x5 5x5 3x5+2 2x5
122 template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
125 typedef typename rtree::node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type node;
126 typedef typename rtree::internal_node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
127 typedef typename rtree::leaf<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
129 typedef typename Allocators::node_pointer node_pointer;
130 typedef rtree::subtree_destroyer<Value, Options, Translator, Box, Allocators> subtree_destroyer;
131 typedef typename Allocators::size_type size_type;
133 typedef typename geometry::point_type<Box>::type point_type;
134 typedef typename geometry::coordinate_type<point_type>::type coordinate_type;
135 typedef typename detail::default_content_result<Box>::type content_type;
136 typedef typename Options::parameters_type parameters_type;
137 static const std::size_t dimension = geometry::dimension<point_type>::value;
139 typedef typename rtree::container_from_elements_type<
140 typename rtree::elements_type<leaf>::type,
142 >::type values_counts_container;
144 typedef typename rtree::elements_type<internal_node>::type internal_elements;
145 typedef typename internal_elements::value_type internal_element;
148 // Arbitrary iterators
149 template <typename InIt> inline static
150 node_pointer apply(InIt first, InIt last, size_type & values_count, size_type & leafs_level,
151 parameters_type const& parameters, Translator const& translator, Allocators & allocators)
153 typedef typename std::iterator_traits<InIt>::difference_type diff_type;
155 diff_type diff = std::distance(first, last);
157 return node_pointer(0);
159 typedef std::pair<point_type, InIt> entry_type;
160 std::vector<entry_type> entries;
162 values_count = static_cast<size_type>(diff);
163 entries.reserve(values_count);
165 expandable_box<Box> hint_box;
166 for ( ; first != last ; ++first )
168 // NOTE: support for iterators not returning true references adapted
169 // to Geometry concept and default translator returning true reference
170 // An alternative would be to dereference the iterator and translate
171 // in one expression each time the indexable was needed.
172 typename std::iterator_traits<InIt>::reference in_ref = *first;
173 typename Translator::result_type indexable = translator(in_ref);
175 // NOTE: added for consistency with insert()
176 // CONSIDER: alternative - ignore invalid indexable or throw an exception
177 BOOST_GEOMETRY_INDEX_ASSERT(detail::is_valid(indexable), "Indexable is invalid");
179 hint_box.expand(indexable);
182 geometry::centroid(indexable, pt);
183 entries.push_back(std::make_pair(pt, first));
186 subtree_elements_counts subtree_counts = calculate_subtree_elements_counts(values_count, parameters, leafs_level);
187 internal_element el = per_level(entries.begin(), entries.end(), hint_box.get(), values_count, subtree_counts,
188 parameters, translator, allocators);
194 template <typename BoxType>
199 : m_initialized(false)
202 template <typename Indexable>
203 explicit expandable_box(Indexable const& indexable)
204 : m_initialized(true)
206 detail::bounds(indexable, m_box);
209 template <typename Indexable>
210 void expand(Indexable const& indexable)
212 if ( !m_initialized )
214 // it's guaranteed that the Box will be initialized
215 // only for Points, Boxes and Segments but that's ok
216 // since only those Geometries can be stored
217 detail::bounds(indexable, m_box);
218 m_initialized = true;
222 geometry::expand(m_box, indexable);
226 void expand_by_epsilon()
228 geometry::detail::expand_by_epsilon(m_box);
231 BoxType const& get() const
233 BOOST_GEOMETRY_INDEX_ASSERT(m_initialized, "uninitialized envelope accessed");
242 struct subtree_elements_counts
244 subtree_elements_counts(std::size_t ma, std::size_t mi) : maxc(ma), minc(mi) {}
249 template <typename EIt> inline static
250 internal_element per_level(EIt first, EIt last, Box const& hint_box, std::size_t values_count, subtree_elements_counts const& subtree_counts,
251 parameters_type const& parameters, Translator const& translator, Allocators & allocators)
253 BOOST_GEOMETRY_INDEX_ASSERT(0 < std::distance(first, last) && static_cast<std::size_t>(std::distance(first, last)) == values_count,
254 "unexpected parameters");
256 if ( subtree_counts.maxc <= 1 )
259 BOOST_GEOMETRY_INDEX_ASSERT(values_count <= parameters.get_max_elements(),
260 "too big number of elements");
261 // if !root check m_parameters.get_min_elements() <= count
263 // create new leaf node
264 node_pointer n = rtree::create_node<Allocators, leaf>::apply(allocators); // MAY THROW (A)
265 subtree_destroyer auto_remover(n, allocators);
266 leaf & l = rtree::get<leaf>(*n);
268 // reserve space for values
269 rtree::elements(l).reserve(values_count); // MAY THROW (A)
271 // calculate values box and copy values
272 // initialize the box explicitly to avoid GCC-4.4 uninitialized variable warnings with O2
273 expandable_box<Box> elements_box(translator(*(first->second)));
274 rtree::elements(l).push_back(*(first->second)); // MAY THROW (A?,C)
275 for ( ++first ; first != last ; ++first )
277 // NOTE: push_back() must be called at the end in order to support move_iterator.
278 // The iterator is dereferenced 2x (no temporary reference) to support
279 // non-true reference types and move_iterator without boost::forward<>.
280 elements_box.expand(translator(*(first->second)));
281 rtree::elements(l).push_back(*(first->second)); // MAY THROW (A?,C)
284 #ifdef BOOST_GEOMETRY_INDEX_EXPERIMENTAL_ENLARGE_BY_EPSILON
285 // Enlarge bounds of a leaf node.
286 // It's because Points and Segments are compared WRT machine epsilon
287 // This ensures that leafs bounds correspond to the stored elements
288 // NOTE: this is done only if the Indexable is a different kind of Geometry
289 // than the bounds (only Box for now). Spatial predicates are checked
290 // the same way for Geometry of the same kind.
291 if ( BOOST_GEOMETRY_CONDITION((
292 ! index::detail::is_bounding_geometry
294 typename indexable_type<Translator>::type
297 elements_box.expand_by_epsilon();
301 auto_remover.release();
302 return internal_element(elements_box.get(), n);
305 // calculate next max and min subtree counts
306 subtree_elements_counts next_subtree_counts = subtree_counts;
307 next_subtree_counts.maxc /= parameters.get_max_elements();
308 next_subtree_counts.minc /= parameters.get_max_elements();
310 // create new internal node
311 node_pointer n = rtree::create_node<Allocators, internal_node>::apply(allocators); // MAY THROW (A)
312 subtree_destroyer auto_remover(n, allocators);
313 internal_node & in = rtree::get<internal_node>(*n);
315 // reserve space for values
316 std::size_t nodes_count = calculate_nodes_count(values_count, subtree_counts);
317 rtree::elements(in).reserve(nodes_count); // MAY THROW (A)
318 // calculate values box and copy values
319 expandable_box<Box> elements_box;
321 per_level_packets(first, last, hint_box, values_count, subtree_counts, next_subtree_counts,
322 rtree::elements(in), elements_box,
323 parameters, translator, allocators);
325 auto_remover.release();
326 return internal_element(elements_box.get(), n);
329 template <typename EIt, typename ExpandableBox> inline static
330 void per_level_packets(EIt first, EIt last, Box const& hint_box,
331 std::size_t values_count,
332 subtree_elements_counts const& subtree_counts,
333 subtree_elements_counts const& next_subtree_counts,
334 internal_elements & elements, ExpandableBox & elements_box,
335 parameters_type const& parameters, Translator const& translator, Allocators & allocators)
337 BOOST_GEOMETRY_INDEX_ASSERT(0 < std::distance(first, last) && static_cast<std::size_t>(std::distance(first, last)) == values_count,
338 "unexpected parameters");
340 BOOST_GEOMETRY_INDEX_ASSERT(subtree_counts.minc <= values_count,
341 "too small number of elements");
344 if ( values_count <= subtree_counts.maxc )
346 // the end, move to the next level
347 internal_element el = per_level(first, last, hint_box, values_count, next_subtree_counts,
348 parameters, translator, allocators);
350 // in case if push_back() do throw here
351 // and even if this is not probable (previously reserved memory, nonthrowing pairs copy)
352 // this case is also tested by exceptions test.
353 subtree_destroyer auto_remover(el.second, allocators);
354 // this container should have memory allocated, reserve() called outside
355 elements.push_back(el); // MAY THROW (A?,C) - however in normal conditions shouldn't
356 auto_remover.release();
358 elements_box.expand(el.first);
362 std::size_t median_count = calculate_median_count(values_count, subtree_counts);
363 EIt median = first + median_count;
365 coordinate_type greatest_length;
366 std::size_t greatest_dim_index = 0;
367 pack_utils::biggest_edge<dimension>::apply(hint_box, greatest_length, greatest_dim_index);
369 pack_utils::nth_element_and_half_boxes<0, dimension>
370 ::apply(first, median, last, hint_box, left, right, greatest_dim_index);
372 per_level_packets(first, median, left,
373 median_count, subtree_counts, next_subtree_counts,
374 elements, elements_box,
375 parameters, translator, allocators);
376 per_level_packets(median, last, right,
377 values_count - median_count, subtree_counts, next_subtree_counts,
378 elements, elements_box,
379 parameters, translator, allocators);
383 subtree_elements_counts calculate_subtree_elements_counts(std::size_t elements_count, parameters_type const& parameters, size_type & leafs_level)
385 boost::ignore_unused_variable_warning(parameters);
387 subtree_elements_counts res(1, 1);
390 std::size_t smax = parameters.get_max_elements();
391 for ( ; smax < elements_count ; smax *= parameters.get_max_elements(), ++leafs_level )
394 res.minc = parameters.get_min_elements() * (res.maxc / parameters.get_max_elements());
400 std::size_t calculate_nodes_count(std::size_t count,
401 subtree_elements_counts const& subtree_counts)
403 std::size_t n = count / subtree_counts.maxc;
404 std::size_t r = count % subtree_counts.maxc;
406 if ( 0 < r && r < subtree_counts.minc )
408 std::size_t count_minus_min = count - subtree_counts.minc;
409 n = count_minus_min / subtree_counts.maxc;
410 r = count_minus_min % subtree_counts.maxc;
421 std::size_t calculate_median_count(std::size_t count,
422 subtree_elements_counts const& subtree_counts)
424 // e.g. for max = 5, min = 2, count = 52, subtree_max = 25, subtree_min = 10
426 std::size_t n = count / subtree_counts.maxc; // e.g. 52 / 25 = 2
427 std::size_t r = count % subtree_counts.maxc; // e.g. 52 % 25 = 2
428 std::size_t median_count = (n / 2) * subtree_counts.maxc; // e.g. 2 / 2 * 25 = 25
430 if ( 0 != r ) // e.g. 0 != 2
432 if ( subtree_counts.minc <= r ) // e.g. 10 <= 2 == false
434 //BOOST_GEOMETRY_INDEX_ASSERT(0 < n, "unexpected value");
435 median_count = ((n+1)/2) * subtree_counts.maxc; // if calculated ((2+1)/2) * 25 which would be ok, but not in all cases
437 else // r < subtree_counts.second // e.g. 2 < 10 == true
439 std::size_t count_minus_min = count - subtree_counts.minc; // e.g. 52 - 10 = 42
440 n = count_minus_min / subtree_counts.maxc; // e.g. 42 / 25 = 1
441 r = count_minus_min % subtree_counts.maxc; // e.g. 42 % 25 = 17
442 if ( r == 0 ) // e.g. false
444 // n can't be equal to 0 because then there wouldn't be any element in the other node
445 //BOOST_GEOMETRY_INDEX_ASSERT(0 < n, "unexpected value");
446 median_count = ((n+1)/2) * subtree_counts.maxc; // if calculated ((1+1)/2) * 25 which would be ok, but not in all cases
450 if ( n == 0 ) // e.g. false
451 median_count = r; // if calculated -> 17 which is wrong!
453 median_count = ((n+2)/2) * subtree_counts.maxc; // e.g. ((1+2)/2) * 25 = 25
462 }}}}} // namespace boost::geometry::index::detail::rtree
464 #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_PACK_CREATE_HPP