pack_create.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. // Boost.Geometry Index
  2. //
  3. // R-tree initial packing
  4. //
  5. // Copyright (c) 2011-2023 Adam Wulkiewicz, Lodz, Poland.
  6. // Copyright (c) 2020 Caian Benedicto, Campinas, Brazil.
  7. //
  8. // This file was modified by Oracle on 2019-2023.
  9. // Modifications copyright (c) 2019-2023 Oracle and/or its affiliates.
  10. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  11. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  12. //
  13. // Use, modification and distribution is subject to the Boost Software License,
  14. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_PACK_CREATE_HPP
  17. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_PACK_CREATE_HPP
  18. #include <boost/core/ignore_unused.hpp>
  19. #include <boost/geometry/algorithms/centroid.hpp>
  20. #include <boost/geometry/algorithms/detail/expand_by_epsilon.hpp>
  21. #include <boost/geometry/algorithms/expand.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/algorithms/is_valid.hpp>
  25. #include <boost/geometry/index/detail/algorithms/nth_element.hpp>
  26. #include <boost/geometry/index/detail/rtree/node/node_elements.hpp>
  27. #include <boost/geometry/index/detail/rtree/node/subtree_destroyer.hpp>
  28. #include <boost/geometry/index/parameters.hpp>
  29. #include <boost/geometry/util/constexpr.hpp>
  30. namespace boost { namespace geometry { namespace index { namespace detail { namespace rtree {
  31. namespace pack_utils {
  32. template <std::size_t Dimension>
  33. struct biggest_edge
  34. {
  35. BOOST_STATIC_ASSERT(0 < Dimension);
  36. template <typename Box>
  37. static inline void apply(Box const& box, typename coordinate_type<Box>::type & length, std::size_t & dim_index)
  38. {
  39. biggest_edge<Dimension-1>::apply(box, length, dim_index);
  40. typename coordinate_type<Box>::type curr
  41. = geometry::get<max_corner, Dimension-1>(box) - geometry::get<min_corner, Dimension-1>(box);
  42. if ( length < curr )
  43. {
  44. dim_index = Dimension - 1;
  45. length = curr;
  46. }
  47. }
  48. };
  49. template <>
  50. struct biggest_edge<1>
  51. {
  52. template <typename Box>
  53. static inline void apply(Box const& box, typename coordinate_type<Box>::type & length, std::size_t & dim_index)
  54. {
  55. dim_index = 0;
  56. length = geometry::get<max_corner, 0>(box) - geometry::get<min_corner, 0>(box);
  57. }
  58. };
  59. template <std::size_t I>
  60. struct point_entries_comparer
  61. {
  62. template <typename PointEntry>
  63. bool operator()(PointEntry const& e1, PointEntry const& e2) const
  64. {
  65. return geometry::get<I>(e1.first) < geometry::get<I>(e2.first);
  66. }
  67. };
  68. template <std::size_t I, std::size_t Dimension>
  69. struct nth_element_and_half_boxes
  70. {
  71. template <typename EIt, typename Box>
  72. static inline void apply(EIt first, EIt median, EIt last, Box const& box,
  73. Box & left, Box & right, std::size_t dim_index)
  74. {
  75. if (I == dim_index)
  76. {
  77. index::detail::nth_element(first, median, last, point_entries_comparer<I>());
  78. geometry::convert(box, left);
  79. geometry::convert(box, right);
  80. auto const mi = geometry::get<min_corner, I>(box);
  81. auto const ma = geometry::get<max_corner, I>(box);
  82. auto const center = mi + (ma - mi) / 2;
  83. geometry::set<max_corner, I>(left, center);
  84. geometry::set<min_corner, I>(right, center);
  85. }
  86. else
  87. {
  88. nth_element_and_half_boxes
  89. <
  90. I + 1, Dimension
  91. >::apply(first, median, last, box, left, right, dim_index);
  92. }
  93. }
  94. };
  95. template <std::size_t Dimension>
  96. struct nth_element_and_half_boxes<Dimension, Dimension>
  97. {
  98. template <typename EIt, typename Box>
  99. static inline void apply(EIt , EIt , EIt , Box const& , Box & , Box & , std::size_t ) {}
  100. };
  101. } // namespace pack_utils
  102. // STR leafs number are calculated as rcount/max
  103. // and the number of splitting planes for each dimension as (count/max)^(1/dimension)
  104. // <-> for dimension==2 -> sqrt(count/max)
  105. //
  106. // The main flaw of this algorithm is that the resulting tree will have bad structure for:
  107. // 1. non-uniformly distributed elements
  108. // Statistic check could be performed, e.g. based on variance of lengths of elements edges for each dimension
  109. // 2. elements distributed mainly along one axis
  110. // Calculate bounding box of all elements and then number of dividing planes for a dimension
  111. // from the length of BB edge for this dimension (more or less assuming that elements are uniformly-distributed squares)
  112. //
  113. // Another thing is that the last node may have less elements than Max or even Min.
  114. // The number of splitting planes must be chosen more carefully than count/max
  115. //
  116. // This algorithm is something between STR and TGS
  117. // it is more similar to the top-down recursive kd-tree creation algorithm
  118. // using object median split and split axis of greatest BB edge
  119. // BB is only used as a hint (assuming objects are distributed uniformly)
  120. //
  121. // Implemented algorithm guarantees that the number of elements in nodes will be between Min and Max
  122. // and that nodes are packed as tightly as possible
  123. // e.g. for 177 values Max = 5 and Min = 2 it will construct the following tree:
  124. // ROOT 177
  125. // L1 125 52
  126. // L2 25 25 25 25 25 25 17 10
  127. // L3 5x5 5x5 5x5 5x5 5x5 5x5 3x5+2 2x5
  128. template <typename MembersHolder>
  129. class pack
  130. {
  131. typedef typename MembersHolder::node node;
  132. typedef typename MembersHolder::internal_node internal_node;
  133. typedef typename MembersHolder::leaf leaf;
  134. typedef typename MembersHolder::node_pointer node_pointer;
  135. typedef typename MembersHolder::size_type size_type;
  136. typedef typename MembersHolder::parameters_type parameters_type;
  137. typedef typename MembersHolder::translator_type translator_type;
  138. typedef typename MembersHolder::allocators_type allocators_type;
  139. typedef typename MembersHolder::box_type box_type;
  140. typedef typename geometry::point_type<box_type>::type point_type;
  141. typedef typename geometry::coordinate_type<point_type>::type coordinate_type;
  142. typedef typename detail::default_content_result<box_type>::type content_type;
  143. typedef typename detail::strategy_type<parameters_type>::type strategy_type;
  144. static const std::size_t dimension = geometry::dimension<point_type>::value;
  145. typedef typename rtree::container_from_elements_type<
  146. typename rtree::elements_type<leaf>::type,
  147. size_type
  148. >::type values_counts_container;
  149. typedef typename rtree::elements_type<internal_node>::type internal_elements;
  150. typedef typename internal_elements::value_type internal_element;
  151. typedef rtree::subtree_destroyer<MembersHolder> subtree_destroyer;
  152. public:
  153. // Arbitrary iterators
  154. template <typename InIt> inline static
  155. node_pointer apply(InIt first, InIt last,
  156. size_type & values_count,
  157. size_type & leafs_level,
  158. parameters_type const& parameters,
  159. translator_type const& translator,
  160. allocators_type & allocators)
  161. {
  162. return apply(first, last, values_count, leafs_level, parameters, translator,
  163. allocators, boost::container::new_allocator<void>());
  164. }
  165. template <typename InIt, typename TmpAlloc> inline static
  166. node_pointer apply(InIt first, InIt last,
  167. size_type & values_count,
  168. size_type & leafs_level,
  169. parameters_type const& parameters,
  170. translator_type const& translator,
  171. allocators_type & allocators,
  172. TmpAlloc const& temp_allocator)
  173. {
  174. typedef typename std::iterator_traits<InIt>::difference_type diff_type;
  175. diff_type diff = std::distance(first, last);
  176. if ( diff <= 0 )
  177. return node_pointer(0);
  178. typedef std::pair<point_type, InIt> entry_type;
  179. typedef typename boost::container::allocator_traits<TmpAlloc>::
  180. template rebind_alloc<entry_type> temp_entry_allocator_type;
  181. temp_entry_allocator_type temp_entry_allocator(temp_allocator);
  182. boost::container::vector<entry_type, temp_entry_allocator_type> entries(temp_entry_allocator);
  183. values_count = static_cast<size_type>(diff);
  184. entries.reserve(values_count);
  185. auto const& strategy = index::detail::get_strategy(parameters);
  186. expandable_box<box_type, strategy_type> hint_box(strategy);
  187. for ( ; first != last ; ++first )
  188. {
  189. // NOTE: support for iterators not returning true references adapted
  190. // to Geometry concept and default translator returning true reference
  191. // An alternative would be to dereference the iterator and translate
  192. // in one expression each time the indexable was needed.
  193. typename std::iterator_traits<InIt>::reference in_ref = *first;
  194. typename translator_type::result_type indexable = translator(in_ref);
  195. // NOTE: added for consistency with insert()
  196. // CONSIDER: alternative - ignore invalid indexable or throw an exception
  197. BOOST_GEOMETRY_INDEX_ASSERT(detail::is_valid(indexable), "Indexable is invalid");
  198. hint_box.expand(indexable);
  199. point_type pt;
  200. geometry::centroid(indexable, pt, strategy);
  201. entries.push_back(std::make_pair(pt, first));
  202. }
  203. subtree_elements_counts subtree_counts = calculate_subtree_elements_counts(values_count, parameters, leafs_level);
  204. internal_element el = per_level(entries.begin(), entries.end(), hint_box.get(), values_count, subtree_counts,
  205. parameters, translator, allocators);
  206. return el.second;
  207. }
  208. private:
  209. template <typename BoxType, typename Strategy>
  210. class expandable_box
  211. {
  212. public:
  213. explicit expandable_box(Strategy const& strategy)
  214. : m_strategy(strategy), m_initialized(false)
  215. {}
  216. template <typename Indexable>
  217. explicit expandable_box(Indexable const& indexable, Strategy const& strategy)
  218. : m_strategy(strategy), m_initialized(true)
  219. {
  220. detail::bounds(indexable, m_box, m_strategy);
  221. }
  222. template <typename Indexable>
  223. void expand(Indexable const& indexable)
  224. {
  225. if ( !m_initialized )
  226. {
  227. // it's guaranteed that the Box will be initialized
  228. // only for Points, Boxes and Segments but that's ok
  229. // since only those Geometries can be stored
  230. detail::bounds(indexable, m_box, m_strategy);
  231. m_initialized = true;
  232. }
  233. else
  234. {
  235. detail::expand(m_box, indexable, m_strategy);
  236. }
  237. }
  238. void expand_by_epsilon()
  239. {
  240. geometry::detail::expand_by_epsilon(m_box);
  241. }
  242. BoxType const& get() const
  243. {
  244. BOOST_GEOMETRY_INDEX_ASSERT(m_initialized, "uninitialized envelope accessed");
  245. return m_box;
  246. }
  247. private:
  248. BoxType m_box;
  249. Strategy m_strategy;
  250. bool m_initialized;
  251. };
  252. struct subtree_elements_counts
  253. {
  254. subtree_elements_counts(size_type ma, size_type mi) : maxc(ma), minc(mi) {}
  255. size_type maxc;
  256. size_type minc;
  257. };
  258. template <typename EIt> inline static
  259. internal_element per_level(EIt first, EIt last,
  260. box_type const& hint_box,
  261. size_type values_count,
  262. subtree_elements_counts const& subtree_counts,
  263. parameters_type const& parameters,
  264. translator_type const& translator,
  265. allocators_type & allocators)
  266. {
  267. BOOST_GEOMETRY_INDEX_ASSERT(0 < std::distance(first, last) && static_cast<size_type>(std::distance(first, last)) == values_count,
  268. "unexpected parameters");
  269. if ( subtree_counts.maxc <= 1 )
  270. {
  271. // ROOT or LEAF
  272. BOOST_GEOMETRY_INDEX_ASSERT(values_count <= parameters.get_max_elements(),
  273. "too big number of elements");
  274. // if !root check m_parameters.get_min_elements() <= count
  275. // create new leaf node
  276. node_pointer n = rtree::create_node<allocators_type, leaf>::apply(allocators); // MAY THROW (A)
  277. subtree_destroyer auto_remover(n, allocators);
  278. leaf & l = rtree::get<leaf>(*n);
  279. // reserve space for values
  280. rtree::elements(l).reserve(values_count); // MAY THROW (A)
  281. // calculate values box and copy values
  282. // initialize the box explicitly to avoid GCC-4.4 uninitialized variable warnings with O2
  283. expandable_box<box_type, strategy_type> elements_box(translator(*(first->second)),
  284. detail::get_strategy(parameters));
  285. rtree::elements(l).push_back(*(first->second)); // MAY THROW (A?,C)
  286. for ( ++first ; first != last ; ++first )
  287. {
  288. // NOTE: push_back() must be called at the end in order to support move_iterator.
  289. // The iterator is dereferenced 2x (no temporary reference) to support
  290. // non-true reference types and move_iterator without std::forward<>.
  291. elements_box.expand(translator(*(first->second)));
  292. rtree::elements(l).push_back(*(first->second)); // MAY THROW (A?,C)
  293. }
  294. #ifdef BOOST_GEOMETRY_INDEX_EXPERIMENTAL_ENLARGE_BY_EPSILON
  295. // Enlarge bounds of a leaf node.
  296. // It's because Points and Segments are compared WRT machine epsilon
  297. // This ensures that leafs bounds correspond to the stored elements
  298. // NOTE: this is done only if the Indexable is a different kind of Geometry
  299. // than the bounds (only Box for now). Spatial predicates are checked
  300. // the same way for Geometry of the same kind.
  301. if BOOST_GEOMETRY_CONSTEXPR (! index::detail::is_bounding_geometry
  302. <
  303. typename indexable_type<translator_type>::type
  304. >::value)
  305. {
  306. elements_box.expand_by_epsilon();
  307. }
  308. #endif
  309. auto_remover.release();
  310. return internal_element(elements_box.get(), n);
  311. }
  312. // calculate next max and min subtree counts
  313. subtree_elements_counts next_subtree_counts = subtree_counts;
  314. next_subtree_counts.maxc /= parameters.get_max_elements();
  315. next_subtree_counts.minc /= parameters.get_max_elements();
  316. // create new internal node
  317. node_pointer n = rtree::create_node<allocators_type, internal_node>::apply(allocators); // MAY THROW (A)
  318. subtree_destroyer auto_remover(n, allocators);
  319. internal_node & in = rtree::get<internal_node>(*n);
  320. // reserve space for values
  321. size_type nodes_count = calculate_nodes_count(values_count, subtree_counts);
  322. rtree::elements(in).reserve(nodes_count); // MAY THROW (A)
  323. // calculate values box and copy values
  324. expandable_box<box_type, strategy_type> elements_box(detail::get_strategy(parameters));
  325. per_level_packets(first, last, hint_box, values_count, subtree_counts, next_subtree_counts,
  326. rtree::elements(in), elements_box,
  327. parameters, translator, allocators);
  328. auto_remover.release();
  329. return internal_element(elements_box.get(), n);
  330. }
  331. template <typename EIt, typename ExpandableBox> inline static
  332. void per_level_packets(EIt first, EIt last,
  333. box_type const& hint_box,
  334. size_type values_count,
  335. subtree_elements_counts const& subtree_counts,
  336. subtree_elements_counts const& next_subtree_counts,
  337. internal_elements & elements,
  338. ExpandableBox & elements_box,
  339. parameters_type const& parameters,
  340. translator_type const& translator,
  341. allocators_type & allocators)
  342. {
  343. BOOST_GEOMETRY_INDEX_ASSERT(0 < std::distance(first, last) && static_cast<size_type>(std::distance(first, last)) == values_count,
  344. "unexpected parameters");
  345. BOOST_GEOMETRY_INDEX_ASSERT(subtree_counts.minc <= values_count,
  346. "too small number of elements");
  347. // only one packet
  348. if ( values_count <= subtree_counts.maxc )
  349. {
  350. // the end, move to the next level
  351. internal_element el = per_level(first, last, hint_box, values_count, next_subtree_counts,
  352. parameters, translator, allocators);
  353. // in case if push_back() do throw here
  354. // and even if this is not probable (previously reserved memory, nonthrowing pairs copy)
  355. // this case is also tested by exceptions test.
  356. subtree_destroyer auto_remover(el.second, allocators);
  357. // this container should have memory allocated, reserve() called outside
  358. elements.push_back(el); // MAY THROW (A?,C) - however in normal conditions shouldn't
  359. auto_remover.release();
  360. elements_box.expand(el.first);
  361. return;
  362. }
  363. size_type median_count = calculate_median_count(values_count, subtree_counts);
  364. 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);
  368. box_type left, right;
  369. pack_utils::nth_element_and_half_boxes<0, dimension>
  370. ::apply(first, median, last, hint_box, left, right, greatest_dim_index);
  371. per_level_packets(first, median, left,
  372. median_count, subtree_counts, next_subtree_counts,
  373. elements, elements_box,
  374. parameters, translator, allocators);
  375. per_level_packets(median, last, right,
  376. values_count - median_count, subtree_counts, next_subtree_counts,
  377. elements, elements_box,
  378. parameters, translator, allocators);
  379. }
  380. inline static
  381. subtree_elements_counts calculate_subtree_elements_counts(size_type elements_count, parameters_type const& parameters, size_type & leafs_level)
  382. {
  383. boost::ignore_unused(parameters);
  384. subtree_elements_counts res(1, 1);
  385. leafs_level = 0;
  386. size_type smax = parameters.get_max_elements();
  387. for ( ; smax < elements_count ; smax *= parameters.get_max_elements(), ++leafs_level )
  388. res.maxc = smax;
  389. res.minc = parameters.get_min_elements() * (res.maxc / parameters.get_max_elements());
  390. return res;
  391. }
  392. inline static
  393. size_type calculate_nodes_count(size_type count,
  394. subtree_elements_counts const& subtree_counts)
  395. {
  396. size_type n = count / subtree_counts.maxc;
  397. size_type r = count % subtree_counts.maxc;
  398. if ( 0 < r && r < subtree_counts.minc )
  399. {
  400. size_type count_minus_min = count - subtree_counts.minc;
  401. n = count_minus_min / subtree_counts.maxc;
  402. r = count_minus_min % subtree_counts.maxc;
  403. ++n;
  404. }
  405. if ( 0 < r )
  406. ++n;
  407. return n;
  408. }
  409. inline static
  410. size_type calculate_median_count(size_type count,
  411. subtree_elements_counts const& subtree_counts)
  412. {
  413. // e.g. for max = 5, min = 2, count = 52, subtree_max = 25, subtree_min = 10
  414. size_type n = count / subtree_counts.maxc; // e.g. 52 / 25 = 2
  415. size_type r = count % subtree_counts.maxc; // e.g. 52 % 25 = 2
  416. size_type median_count = (n / 2) * subtree_counts.maxc; // e.g. 2 / 2 * 25 = 25
  417. if ( 0 != r ) // e.g. 0 != 2
  418. {
  419. if ( subtree_counts.minc <= r ) // e.g. 10 <= 2 == false
  420. {
  421. //BOOST_GEOMETRY_INDEX_ASSERT(0 < n, "unexpected value");
  422. median_count = ((n+1)/2) * subtree_counts.maxc; // if calculated ((2+1)/2) * 25 which would be ok, but not in all cases
  423. }
  424. else // r < subtree_counts.second // e.g. 2 < 10 == true
  425. {
  426. size_type count_minus_min = count - subtree_counts.minc; // e.g. 52 - 10 = 42
  427. n = count_minus_min / subtree_counts.maxc; // e.g. 42 / 25 = 1
  428. r = count_minus_min % subtree_counts.maxc; // e.g. 42 % 25 = 17
  429. if ( r == 0 ) // e.g. false
  430. {
  431. // n can't be equal to 0 because then there wouldn't be any element in the other node
  432. //BOOST_GEOMETRY_INDEX_ASSERT(0 < n, "unexpected value");
  433. median_count = ((n+1)/2) * subtree_counts.maxc; // if calculated ((1+1)/2) * 25 which would be ok, but not in all cases
  434. }
  435. else
  436. {
  437. if ( n == 0 ) // e.g. false
  438. median_count = r; // if calculated -> 17 which is wrong!
  439. else
  440. median_count = ((n+2)/2) * subtree_counts.maxc; // e.g. ((1+2)/2) * 25 = 25
  441. }
  442. }
  443. }
  444. return median_count;
  445. }
  446. };
  447. }}}}} // namespace boost::geometry::index::detail::rtree
  448. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_PACK_CREATE_HPP