1 // Boost.Geometry Index
3 // R-tree linear split algorithm implementation
5 // Copyright (c) 2008 Federico J. Fernandez.
6 // Copyright (c) 2011-2014 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2019.
9 // Modifications copyright (c) 2019 Oracle and/or its affiliates.
10 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12 // Use, modification and distribution is subject to the Boost Software License,
13 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
14 // http://www.boost.org/LICENSE_1_0.txt)
16 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_LINEAR_REDISTRIBUTE_ELEMENTS_HPP
17 #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_LINEAR_REDISTRIBUTE_ELEMENTS_HPP
19 #include <boost/core/ignore_unused.hpp>
20 #include <boost/type_traits/is_unsigned.hpp>
22 #include <boost/geometry/index/detail/algorithms/bounds.hpp>
23 #include <boost/geometry/index/detail/algorithms/content.hpp>
24 #include <boost/geometry/index/detail/bounded_view.hpp>
26 #include <boost/geometry/index/detail/rtree/node/node.hpp>
27 #include <boost/geometry/index/detail/rtree/visitors/insert.hpp>
28 #include <boost/geometry/index/detail/rtree/visitors/is_leaf.hpp>
30 namespace boost { namespace geometry { namespace index {
32 namespace detail { namespace rtree {
36 template <typename R, typename T>
37 inline R difference_dispatch(T const& from, T const& to, ::boost::mpl::bool_<false> const& /*is_unsigned*/)
42 template <typename R, typename T>
43 inline R difference_dispatch(T const& from, T const& to, ::boost::mpl::bool_<true> const& /*is_unsigned*/)
45 return from <= to ? R(to - from) : -R(from - to);
48 template <typename R, typename T>
49 inline R difference(T const& from, T const& to)
51 BOOST_MPL_ASSERT_MSG(!boost::is_unsigned<R>::value, RESULT_CANT_BE_UNSIGNED, (R));
53 typedef ::boost::mpl::bool_<
54 boost::is_unsigned<T>::value
57 return difference_dispatch<R>(from, to, is_unsigned());
61 // In general, all aerial Indexables in the tree with box-like nodes will be analyzed as boxes
62 // because they must fit into larger box. Therefore the algorithm could be the same for Bounds type.
63 // E.g. if Bounds type is sphere, Indexables probably should be analyzed as spheres.
64 // 1. View could be provided to 'see' all Indexables as Bounds type.
65 // Not ok in the case of big types like Ring, however it's possible that Rings won't be supported,
66 // only simple types. Even then if we consider storing Box inside the Sphere we must calculate
67 // the bounding sphere 2x for each box because there are 2 loops. For each calculation this means
68 // 4-2d or 8-3d expansions or -, / and sqrt().
69 // 2. Additional container could be used and reused if the Indexable type is other than the Bounds type.
72 // Still probably the best way would be providing specialized algorithms for each Indexable-Bounds pair!
73 // Probably on pick_seeds algorithm level - For Bounds=Sphere seeds would be choosen differently
76 // there are loops inside find_greatest_normalized_separation::apply()
77 // iteration is done for each DimensionIndex.
78 // Separations and seeds for all DimensionIndex(es) could be calculated at once, stored, then the greatest would be choosen.
80 // The following struct/method was adapted for the preliminary version of the R-tree. Then it was called:
81 // void find_normalized_separations(std::vector<Box> const& boxes, T& separation, unsigned int& first, unsigned int& second) const
83 template <typename Elements, typename Parameters, typename Translator, typename Tag, size_t DimensionIndex>
84 struct find_greatest_normalized_separation
86 typedef typename Elements::value_type element_type;
87 typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
88 typedef typename coordinate_type<indexable_type>::type coordinate_type;
90 typedef typename boost::mpl::if_c<
91 boost::is_integral<coordinate_type>::value,
94 >::type separation_type;
96 typedef typename geometry::point_type<indexable_type>::type point_type;
97 typedef geometry::model::box<point_type> bounds_type;
98 typedef index::detail::bounded_view
100 indexable_type, bounds_type,
101 typename index::detail::strategy_type<Parameters>::type
104 static inline void apply(Elements const& elements,
105 Parameters const& parameters,
106 Translator const& translator,
107 separation_type & separation,
111 const size_t elements_count = parameters.get_max_elements() + 1;
112 BOOST_GEOMETRY_INDEX_ASSERT(elements.size() == elements_count, "unexpected number of elements");
113 BOOST_GEOMETRY_INDEX_ASSERT(2 <= elements_count, "unexpected number of elements");
115 typename index::detail::strategy_type<Parameters>::type const&
116 strategy = index::detail::get_strategy(parameters);
118 // find the lowest low, highest high
119 bounded_view_type bounded_indexable_0(rtree::element_indexable(elements[0], translator),
121 coordinate_type lowest_low = geometry::get<min_corner, DimensionIndex>(bounded_indexable_0);
122 coordinate_type highest_high = geometry::get<max_corner, DimensionIndex>(bounded_indexable_0);
124 // and the lowest high
125 coordinate_type lowest_high = highest_high;
126 size_t lowest_high_index = 0;
127 for ( size_t i = 1 ; i < elements_count ; ++i )
129 bounded_view_type bounded_indexable(rtree::element_indexable(elements[i], translator),
131 coordinate_type min_coord = geometry::get<min_corner, DimensionIndex>(bounded_indexable);
132 coordinate_type max_coord = geometry::get<max_corner, DimensionIndex>(bounded_indexable);
134 if ( max_coord < lowest_high )
136 lowest_high = max_coord;
137 lowest_high_index = i;
140 if ( min_coord < lowest_low )
141 lowest_low = min_coord;
143 if ( highest_high < max_coord )
144 highest_high = max_coord;
147 // find the highest low
148 size_t highest_low_index = lowest_high_index == 0 ? 1 : 0;
149 bounded_view_type bounded_indexable_hl(rtree::element_indexable(elements[highest_low_index], translator),
151 coordinate_type highest_low = geometry::get<min_corner, DimensionIndex>(bounded_indexable_hl);
152 for ( size_t i = highest_low_index ; i < elements_count ; ++i )
154 bounded_view_type bounded_indexable(rtree::element_indexable(elements[i], translator),
156 coordinate_type min_coord = geometry::get<min_corner, DimensionIndex>(bounded_indexable);
157 if ( highest_low < min_coord &&
158 i != lowest_high_index )
160 highest_low = min_coord;
161 highest_low_index = i;
165 coordinate_type const width = highest_high - lowest_low;
167 // highest_low - lowest_high
168 separation = difference<separation_type>(lowest_high, highest_low);
169 // BOOST_GEOMETRY_INDEX_ASSERT(0 <= width);
170 if ( std::numeric_limits<coordinate_type>::epsilon() < width )
173 seed1 = highest_low_index;
174 seed2 = lowest_high_index;
176 ::boost::ignore_unused(parameters);
180 // Version for points doesn't calculate normalized separation since it would always be equal to 1
181 // It returns two seeds most distant to each other, separation is equal to distance
182 template <typename Elements, typename Parameters, typename Translator, size_t DimensionIndex>
183 struct find_greatest_normalized_separation<Elements, Parameters, Translator, point_tag, DimensionIndex>
185 typedef typename Elements::value_type element_type;
186 typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
187 typedef typename coordinate_type<indexable_type>::type coordinate_type;
189 typedef coordinate_type separation_type;
191 static inline void apply(Elements const& elements,
192 Parameters const& parameters,
193 Translator const& translator,
194 separation_type & separation,
198 const size_t elements_count = parameters.get_max_elements() + 1;
199 BOOST_GEOMETRY_INDEX_ASSERT(elements.size() == elements_count, "unexpected number of elements");
200 BOOST_GEOMETRY_INDEX_ASSERT(2 <= elements_count, "unexpected number of elements");
202 // find the lowest low, highest high
203 coordinate_type lowest = geometry::get<DimensionIndex>(rtree::element_indexable(elements[0], translator));
204 coordinate_type highest = geometry::get<DimensionIndex>(rtree::element_indexable(elements[0], translator));
205 size_t lowest_index = 0;
206 size_t highest_index = 0;
207 for ( size_t i = 1 ; i < elements_count ; ++i )
209 coordinate_type coord = geometry::get<DimensionIndex>(rtree::element_indexable(elements[i], translator));
211 if ( coord < lowest )
217 if ( highest < coord )
224 separation = highest - lowest;
225 seed1 = lowest_index;
226 seed2 = highest_index;
228 if ( lowest_index == highest_index )
229 seed2 = (lowest_index + 1) % elements_count; // % is just in case since if this is true lowest_index is 0
231 ::boost::ignore_unused(parameters);
235 template <typename Elements, typename Parameters, typename Translator, size_t Dimension>
236 struct pick_seeds_impl
238 BOOST_STATIC_ASSERT(0 < Dimension);
240 typedef typename Elements::value_type element_type;
241 typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
243 typedef find_greatest_normalized_separation<
244 Elements, Parameters, Translator,
245 typename tag<indexable_type>::type, Dimension - 1
248 typedef typename find_norm_sep::separation_type separation_type;
250 static inline void apply(Elements const& elements,
251 Parameters const& parameters,
252 Translator const& tr,
253 separation_type & separation,
257 pick_seeds_impl<Elements, Parameters, Translator, Dimension - 1>::apply(elements, parameters, tr, separation, seed1, seed2);
259 separation_type current_separation;
261 find_norm_sep::apply(elements, parameters, tr, current_separation, s1, s2);
263 // in the old implementation different operator was used: <= (y axis prefered)
264 if ( separation < current_separation )
266 separation = current_separation;
273 template <typename Elements, typename Parameters, typename Translator>
274 struct pick_seeds_impl<Elements, Parameters, Translator, 1>
276 typedef typename Elements::value_type element_type;
277 typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
278 typedef typename coordinate_type<indexable_type>::type coordinate_type;
280 typedef find_greatest_normalized_separation<
281 Elements, Parameters, Translator,
282 typename tag<indexable_type>::type, 0
285 typedef typename find_norm_sep::separation_type separation_type;
287 static inline void apply(Elements const& elements,
288 Parameters const& parameters,
289 Translator const& tr,
290 separation_type & separation,
294 find_norm_sep::apply(elements, parameters, tr, separation, seed1, seed2);
298 // from void linear_pick_seeds(node_pointer const& n, unsigned int &seed1, unsigned int &seed2) const
300 template <typename Elements, typename Parameters, typename Translator>
301 inline void pick_seeds(Elements const& elements,
302 Parameters const& parameters,
303 Translator const& tr,
307 typedef typename Elements::value_type element_type;
308 typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
310 typedef pick_seeds_impl
312 Elements, Parameters, Translator,
313 geometry::dimension<indexable_type>::value
315 typedef typename impl::separation_type separation_type;
317 separation_type separation = 0;
318 impl::apply(elements, parameters, tr, separation, seed1, seed2);
321 } // namespace linear
323 // from void split_node(node_pointer const& n, node_pointer& n1, node_pointer& n2) const
325 template <typename MembersHolder>
326 struct redistribute_elements<MembersHolder, linear_tag>
328 typedef typename MembersHolder::box_type box_type;
329 typedef typename MembersHolder::parameters_type parameters_type;
330 typedef typename MembersHolder::translator_type translator_type;
331 typedef typename MembersHolder::allocators_type allocators_type;
333 typedef typename MembersHolder::node node;
334 typedef typename MembersHolder::internal_node internal_node;
335 typedef typename MembersHolder::leaf leaf;
337 template <typename Node>
338 static inline void apply(Node & n,
342 parameters_type const& parameters,
343 translator_type const& translator,
344 allocators_type & allocators)
346 typedef typename rtree::elements_type<Node>::type elements_type;
347 typedef typename elements_type::value_type element_type;
348 typedef typename rtree::element_indexable_type<element_type, translator_type>::type indexable_type;
349 typedef typename index::detail::default_content_result<box_type>::type content_type;
351 typename index::detail::strategy_type<parameters_type>::type const&
352 strategy = index::detail::get_strategy(parameters);
354 elements_type & elements1 = rtree::elements(n);
355 elements_type & elements2 = rtree::elements(second_node);
356 const size_t elements1_count = parameters.get_max_elements() + 1;
358 BOOST_GEOMETRY_INDEX_ASSERT(elements1.size() == elements1_count, "unexpected number of elements");
360 // copy original elements - use in-memory storage (std::allocator)
361 // TODO: move if noexcept
362 typedef typename rtree::container_from_elements_type<elements_type, element_type>::type
364 container_type elements_copy(elements1.begin(), elements1.end()); // MAY THROW, STRONG (alloc, copy)
366 // calculate initial seeds
369 linear::pick_seeds(elements_copy, parameters, translator, seed1, seed2);
371 // prepare nodes' elements containers
373 BOOST_GEOMETRY_INDEX_ASSERT(elements2.empty(), "unexpected container state");
378 elements1.push_back(elements_copy[seed1]); // MAY THROW, STRONG (copy)
379 elements2.push_back(elements_copy[seed2]); // MAY THROW, STRONG (alloc, copy)
382 detail::bounds(rtree::element_indexable(elements_copy[seed1], translator),
384 detail::bounds(rtree::element_indexable(elements_copy[seed2], translator),
388 content_type content1 = index::detail::content(box1);
389 content_type content2 = index::detail::content(box2);
391 BOOST_GEOMETRY_INDEX_ASSERT(2 <= elements1_count, "unexpected elements number");
392 size_t remaining = elements1_count - 2;
394 // redistribute the rest of the elements
395 for ( size_t i = 0 ; i < elements1_count ; ++i )
397 if (i != seed1 && i != seed2)
399 element_type const& elem = elements_copy[i];
400 indexable_type const& indexable = rtree::element_indexable(elem, translator);
402 // if there is small number of elements left and the number of elements in node is lesser than min_elems
403 // just insert them to this node
404 if ( elements1.size() + remaining <= parameters.get_min_elements() )
406 elements1.push_back(elem); // MAY THROW, STRONG (copy)
407 index::detail::expand(box1, indexable, strategy);
408 content1 = index::detail::content(box1);
410 else if ( elements2.size() + remaining <= parameters.get_min_elements() )
412 elements2.push_back(elem); // MAY THROW, STRONG (alloc, copy)
413 index::detail::expand(box2, indexable, strategy);
414 content2 = index::detail::content(box2);
416 // choose better node and insert element
419 // calculate enlarged boxes and areas
420 box_type enlarged_box1(box1);
421 box_type enlarged_box2(box2);
422 index::detail::expand(enlarged_box1, indexable, strategy);
423 index::detail::expand(enlarged_box2, indexable, strategy);
424 content_type enlarged_content1 = index::detail::content(enlarged_box1);
425 content_type enlarged_content2 = index::detail::content(enlarged_box2);
427 content_type content_increase1 = enlarged_content1 - content1;
428 content_type content_increase2 = enlarged_content2 - content2;
430 // choose group which box content have to be enlarged least or has smaller content or has fewer elements
431 if ( content_increase1 < content_increase2 ||
432 ( content_increase1 == content_increase2 &&
433 ( content1 < content2 ||
434 ( content1 == content2 && elements1.size() <= elements2.size() ) ) ) )
436 elements1.push_back(elem); // MAY THROW, STRONG (copy)
437 box1 = enlarged_box1;
438 content1 = enlarged_content1;
442 elements2.push_back(elem); // MAY THROW, STRONG (alloc, copy)
443 box2 = enlarged_box2;
444 content2 = enlarged_content2;
448 BOOST_GEOMETRY_INDEX_ASSERT(0 < remaining, "unexpected value");
458 rtree::destroy_elements<MembersHolder>::apply(elements_copy, allocators);
459 //elements_copy.clear();
461 BOOST_RETHROW // RETHROW, BASIC
467 }} // namespace detail::rtree
469 }}} // namespace boost::geometry::index
471 #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_LINEAR_REDISTRIBUTE_ELEMENTS_HPP