redistribute_elements.hpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. // Boost.Geometry Index
  2. //
  3. // R-tree linear split algorithm implementation
  4. //
  5. // Copyright (c) 2008 Federico J. Fernandez.
  6. // Copyright (c) 2011-2022 Adam Wulkiewicz, Lodz, Poland.
  7. //
  8. // This file was modified by Oracle on 2019-2020.
  9. // Modifications copyright (c) 2019-2020 Oracle and/or its affiliates.
  10. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  11. //
  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)
  15. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_LINEAR_REDISTRIBUTE_ELEMENTS_HPP
  16. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_LINEAR_REDISTRIBUTE_ELEMENTS_HPP
  17. #include <type_traits>
  18. #include <boost/core/ignore_unused.hpp>
  19. #include <boost/geometry/core/static_assert.hpp>
  20. #include <boost/geometry/index/detail/algorithms/bounds.hpp>
  21. #include <boost/geometry/index/detail/algorithms/content.hpp>
  22. #include <boost/geometry/index/detail/bounded_view.hpp>
  23. #include <boost/geometry/index/detail/rtree/node/node.hpp>
  24. #include <boost/geometry/index/detail/rtree/visitors/insert.hpp>
  25. #include <boost/geometry/index/detail/rtree/visitors/is_leaf.hpp>
  26. namespace boost { namespace geometry { namespace index {
  27. namespace detail { namespace rtree {
  28. namespace linear {
  29. template <typename R, typename T>
  30. inline R difference_dispatch(T const& from, T const& to, std::false_type /*is_unsigned*/)
  31. {
  32. return to - from;
  33. }
  34. template <typename R, typename T>
  35. inline R difference_dispatch(T const& from, T const& to, std::true_type /*is_unsigned*/)
  36. {
  37. return from <= to ? R(to - from) : -R(from - to);
  38. }
  39. template <typename R, typename T>
  40. inline R difference(T const& from, T const& to)
  41. {
  42. BOOST_GEOMETRY_STATIC_ASSERT((! std::is_unsigned<R>::value),
  43. "Result can not be an unsigned type.",
  44. R);
  45. return difference_dispatch<R>(from, to, std::is_unsigned<T>());
  46. }
  47. // TODO: awulkiew
  48. // In general, all aerial Indexables in the tree with box-like nodes will be analyzed as boxes
  49. // because they must fit into larger box. Therefore the algorithm could be the same for Bounds type.
  50. // E.g. if Bounds type is sphere, Indexables probably should be analyzed as spheres.
  51. // 1. View could be provided to 'see' all Indexables as Bounds type.
  52. // Not ok in the case of big types like Ring, however it's possible that Rings won't be supported,
  53. // only simple types. Even then if we consider storing Box inside the Sphere we must calculate
  54. // the bounding sphere 2x for each box because there are 2 loops. For each calculation this means
  55. // 4-2d or 8-3d expansions or -, / and sqrt().
  56. // 2. Additional container could be used and reused if the Indexable type is other than the Bounds type.
  57. // IMPORTANT!
  58. // Still probably the best way would be providing specialized algorithms for each Indexable-Bounds pair!
  59. // Probably on pick_seeds algorithm level - For Bounds=Sphere seeds would be choosen differently
  60. // TODO: awulkiew
  61. // there are loops inside find_greatest_normalized_separation::apply()
  62. // iteration is done for each DimensionIndex.
  63. // Separations and seeds for all DimensionIndex(es) could be calculated at once, stored, then the greatest would be choosen.
  64. // The following struct/method was adapted for the preliminary version of the R-tree. Then it was called:
  65. // void find_normalized_separations(std::vector<Box> const& boxes, T& separation, unsigned int& first, unsigned int& second) const
  66. template <typename Elements, typename Parameters, typename Translator, typename Tag, size_t DimensionIndex>
  67. struct find_greatest_normalized_separation
  68. {
  69. typedef typename Elements::value_type element_type;
  70. typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
  71. typedef typename coordinate_type<indexable_type>::type coordinate_type;
  72. typedef std::conditional_t
  73. <
  74. std::is_integral<coordinate_type>::value,
  75. double,
  76. coordinate_type
  77. > separation_type;
  78. typedef typename geometry::point_type<indexable_type>::type point_type;
  79. typedef geometry::model::box<point_type> bounds_type;
  80. typedef index::detail::bounded_view
  81. <
  82. indexable_type, bounds_type,
  83. typename index::detail::strategy_type<Parameters>::type
  84. > bounded_view_type;
  85. static inline void apply(Elements const& elements,
  86. Parameters const& parameters,
  87. Translator const& translator,
  88. separation_type & separation,
  89. size_t & seed1,
  90. size_t & seed2)
  91. {
  92. const size_t elements_count = parameters.get_max_elements() + 1;
  93. BOOST_GEOMETRY_INDEX_ASSERT(elements.size() == elements_count, "unexpected number of elements");
  94. BOOST_GEOMETRY_INDEX_ASSERT(2 <= elements_count, "unexpected number of elements");
  95. auto const& strategy = index::detail::get_strategy(parameters);
  96. // find the lowest low, highest high
  97. indexable_type const& indexable_0 = rtree::element_indexable(elements[0], translator);
  98. bounded_view_type const bounded_indexable_0(indexable_0, strategy);
  99. coordinate_type lowest_low = geometry::get<min_corner, DimensionIndex>(bounded_indexable_0);
  100. coordinate_type highest_high = geometry::get<max_corner, DimensionIndex>(bounded_indexable_0);
  101. // and the lowest high
  102. coordinate_type lowest_high = highest_high;
  103. size_t lowest_high_index = 0;
  104. for (size_t i = 1 ; i < elements_count ; ++i)
  105. {
  106. indexable_type const& indexable_i = rtree::element_indexable(elements[i], translator);
  107. bounded_view_type const bounded_indexable(indexable_i, strategy);
  108. coordinate_type min_coord = geometry::get<min_corner, DimensionIndex>(bounded_indexable);
  109. coordinate_type max_coord = geometry::get<max_corner, DimensionIndex>(bounded_indexable);
  110. if (max_coord < lowest_high)
  111. {
  112. lowest_high = max_coord;
  113. lowest_high_index = i;
  114. }
  115. if (min_coord < lowest_low)
  116. {
  117. lowest_low = min_coord;
  118. }
  119. if (highest_high < max_coord)
  120. {
  121. highest_high = max_coord;
  122. }
  123. }
  124. // find the highest low
  125. size_t highest_low_index = lowest_high_index == 0 ? 1 : 0;
  126. indexable_type const& indexable_hl = rtree::element_indexable(elements[highest_low_index], translator);
  127. bounded_view_type const bounded_indexable_hl(indexable_hl, strategy);
  128. coordinate_type highest_low = geometry::get<min_corner, DimensionIndex>(bounded_indexable_hl);
  129. for (size_t i = highest_low_index ; i < elements_count ; ++i)
  130. {
  131. indexable_type const& indexable = rtree::element_indexable(elements[i], translator);
  132. bounded_view_type const bounded_indexable(indexable, strategy);
  133. coordinate_type min_coord = geometry::get<min_corner, DimensionIndex>(bounded_indexable);
  134. if (highest_low < min_coord && i != lowest_high_index)
  135. {
  136. highest_low = min_coord;
  137. highest_low_index = i;
  138. }
  139. }
  140. coordinate_type const width = highest_high - lowest_low;
  141. // highest_low - lowest_high
  142. separation = difference<separation_type>(lowest_high, highest_low);
  143. // BOOST_GEOMETRY_INDEX_ASSERT(0 <= width);
  144. if (std::numeric_limits<coordinate_type>::epsilon() < width)
  145. {
  146. separation /= width;
  147. }
  148. seed1 = highest_low_index;
  149. seed2 = lowest_high_index;
  150. ::boost::ignore_unused(parameters);
  151. }
  152. };
  153. // Version for points doesn't calculate normalized separation since it would always be equal to 1
  154. // It returns two seeds most distant to each other, separation is equal to distance
  155. template <typename Elements, typename Parameters, typename Translator, size_t DimensionIndex>
  156. struct find_greatest_normalized_separation<Elements, Parameters, Translator, point_tag, DimensionIndex>
  157. {
  158. typedef typename Elements::value_type element_type;
  159. typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
  160. typedef typename coordinate_type<indexable_type>::type coordinate_type;
  161. typedef coordinate_type separation_type;
  162. static inline void apply(Elements const& elements,
  163. Parameters const& parameters,
  164. Translator const& translator,
  165. separation_type & separation,
  166. size_t & seed1,
  167. size_t & seed2)
  168. {
  169. const size_t elements_count = parameters.get_max_elements() + 1;
  170. BOOST_GEOMETRY_INDEX_ASSERT(elements.size() == elements_count, "unexpected number of elements");
  171. BOOST_GEOMETRY_INDEX_ASSERT(2 <= elements_count, "unexpected number of elements");
  172. // find the lowest low, highest high
  173. coordinate_type lowest = geometry::get<DimensionIndex>(rtree::element_indexable(elements[0], translator));
  174. coordinate_type highest = geometry::get<DimensionIndex>(rtree::element_indexable(elements[0], translator));
  175. size_t lowest_index = 0;
  176. size_t highest_index = 0;
  177. for ( size_t i = 1 ; i < elements_count ; ++i )
  178. {
  179. coordinate_type coord = geometry::get<DimensionIndex>(rtree::element_indexable(elements[i], translator));
  180. if ( coord < lowest )
  181. {
  182. lowest = coord;
  183. lowest_index = i;
  184. }
  185. if ( highest < coord )
  186. {
  187. highest = coord;
  188. highest_index = i;
  189. }
  190. }
  191. separation = highest - lowest;
  192. seed1 = lowest_index;
  193. seed2 = highest_index;
  194. if ( lowest_index == highest_index )
  195. seed2 = (lowest_index + 1) % elements_count; // % is just in case since if this is true lowest_index is 0
  196. ::boost::ignore_unused(parameters);
  197. }
  198. };
  199. template <typename Elements, typename Parameters, typename Translator, size_t Dimension>
  200. struct pick_seeds_impl
  201. {
  202. BOOST_STATIC_ASSERT(0 < Dimension);
  203. typedef typename Elements::value_type element_type;
  204. typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
  205. typedef find_greatest_normalized_separation<
  206. Elements, Parameters, Translator,
  207. typename tag<indexable_type>::type, Dimension - 1
  208. > find_norm_sep;
  209. typedef typename find_norm_sep::separation_type separation_type;
  210. static inline void apply(Elements const& elements,
  211. Parameters const& parameters,
  212. Translator const& tr,
  213. separation_type & separation,
  214. size_t & seed1,
  215. size_t & seed2)
  216. {
  217. pick_seeds_impl<Elements, Parameters, Translator, Dimension - 1>::apply(elements, parameters, tr, separation, seed1, seed2);
  218. separation_type current_separation;
  219. size_t s1, s2;
  220. find_norm_sep::apply(elements, parameters, tr, current_separation, s1, s2);
  221. // in the old implementation different operator was used: <= (y axis prefered)
  222. if ( separation < current_separation )
  223. {
  224. separation = current_separation;
  225. seed1 = s1;
  226. seed2 = s2;
  227. }
  228. }
  229. };
  230. template <typename Elements, typename Parameters, typename Translator>
  231. struct pick_seeds_impl<Elements, Parameters, Translator, 1>
  232. {
  233. typedef typename Elements::value_type element_type;
  234. typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
  235. typedef typename coordinate_type<indexable_type>::type coordinate_type;
  236. typedef find_greatest_normalized_separation<
  237. Elements, Parameters, Translator,
  238. typename tag<indexable_type>::type, 0
  239. > find_norm_sep;
  240. typedef typename find_norm_sep::separation_type separation_type;
  241. static inline void apply(Elements const& elements,
  242. Parameters const& parameters,
  243. Translator const& tr,
  244. separation_type & separation,
  245. size_t & seed1,
  246. size_t & seed2)
  247. {
  248. find_norm_sep::apply(elements, parameters, tr, separation, seed1, seed2);
  249. }
  250. };
  251. // from void linear_pick_seeds(node_pointer const& n, unsigned int &seed1, unsigned int &seed2) const
  252. template <typename Elements, typename Parameters, typename Translator>
  253. inline void pick_seeds(Elements const& elements,
  254. Parameters const& parameters,
  255. Translator const& tr,
  256. size_t & seed1,
  257. size_t & seed2)
  258. {
  259. typedef typename Elements::value_type element_type;
  260. typedef typename rtree::element_indexable_type<element_type, Translator>::type indexable_type;
  261. typedef pick_seeds_impl
  262. <
  263. Elements, Parameters, Translator,
  264. geometry::dimension<indexable_type>::value
  265. > impl;
  266. typedef typename impl::separation_type separation_type;
  267. separation_type separation = 0;
  268. impl::apply(elements, parameters, tr, separation, seed1, seed2);
  269. }
  270. } // namespace linear
  271. // from void split_node(node_pointer const& n, node_pointer& n1, node_pointer& n2) const
  272. template <typename MembersHolder>
  273. struct redistribute_elements<MembersHolder, linear_tag>
  274. {
  275. typedef typename MembersHolder::box_type box_type;
  276. typedef typename MembersHolder::parameters_type parameters_type;
  277. typedef typename MembersHolder::translator_type translator_type;
  278. typedef typename MembersHolder::allocators_type allocators_type;
  279. typedef typename MembersHolder::node node;
  280. typedef typename MembersHolder::internal_node internal_node;
  281. typedef typename MembersHolder::leaf leaf;
  282. template <typename Node>
  283. static inline void apply(Node & n,
  284. Node & second_node,
  285. box_type & box1,
  286. box_type & box2,
  287. parameters_type const& parameters,
  288. translator_type const& translator,
  289. allocators_type & allocators)
  290. {
  291. typedef typename rtree::elements_type<Node>::type elements_type;
  292. typedef typename elements_type::value_type element_type;
  293. typedef typename rtree::element_indexable_type<element_type, translator_type>::type indexable_type;
  294. typedef typename index::detail::default_content_result<box_type>::type content_type;
  295. typename index::detail::strategy_type<parameters_type>::type const&
  296. strategy = index::detail::get_strategy(parameters);
  297. elements_type & elements1 = rtree::elements(n);
  298. elements_type & elements2 = rtree::elements(second_node);
  299. const size_t elements1_count = parameters.get_max_elements() + 1;
  300. BOOST_GEOMETRY_INDEX_ASSERT(elements1.size() == elements1_count, "unexpected number of elements");
  301. // copy original elements - use in-memory storage (std::allocator)
  302. // TODO: move if noexcept
  303. typedef typename rtree::container_from_elements_type<elements_type, element_type>::type
  304. container_type;
  305. container_type elements_copy(elements1.begin(), elements1.end()); // MAY THROW, STRONG (alloc, copy)
  306. // calculate initial seeds
  307. size_t seed1 = 0;
  308. size_t seed2 = 0;
  309. linear::pick_seeds(elements_copy, parameters, translator, seed1, seed2);
  310. // prepare nodes' elements containers
  311. elements1.clear();
  312. BOOST_GEOMETRY_INDEX_ASSERT(elements2.empty(), "unexpected container state");
  313. BOOST_TRY
  314. {
  315. // add seeds
  316. elements1.push_back(elements_copy[seed1]); // MAY THROW, STRONG (copy)
  317. elements2.push_back(elements_copy[seed2]); // MAY THROW, STRONG (alloc, copy)
  318. // calculate boxes
  319. detail::bounds(rtree::element_indexable(elements_copy[seed1], translator),
  320. box1, strategy);
  321. detail::bounds(rtree::element_indexable(elements_copy[seed2], translator),
  322. box2, strategy);
  323. // initialize areas
  324. content_type content1 = index::detail::content(box1);
  325. content_type content2 = index::detail::content(box2);
  326. BOOST_GEOMETRY_INDEX_ASSERT(2 <= elements1_count, "unexpected elements number");
  327. size_t remaining = elements1_count - 2;
  328. // redistribute the rest of the elements
  329. for ( size_t i = 0 ; i < elements1_count ; ++i )
  330. {
  331. if (i != seed1 && i != seed2)
  332. {
  333. element_type const& elem = elements_copy[i];
  334. indexable_type const& indexable = rtree::element_indexable(elem, translator);
  335. // if there is small number of elements left and the number of elements in node is lesser than min_elems
  336. // just insert them to this node
  337. if ( elements1.size() + remaining <= parameters.get_min_elements() )
  338. {
  339. elements1.push_back(elem); // MAY THROW, STRONG (copy)
  340. index::detail::expand(box1, indexable, strategy);
  341. content1 = index::detail::content(box1);
  342. }
  343. else if ( elements2.size() + remaining <= parameters.get_min_elements() )
  344. {
  345. elements2.push_back(elem); // MAY THROW, STRONG (alloc, copy)
  346. index::detail::expand(box2, indexable, strategy);
  347. content2 = index::detail::content(box2);
  348. }
  349. // choose better node and insert element
  350. else
  351. {
  352. // calculate enlarged boxes and areas
  353. box_type enlarged_box1(box1);
  354. box_type enlarged_box2(box2);
  355. index::detail::expand(enlarged_box1, indexable, strategy);
  356. index::detail::expand(enlarged_box2, indexable, strategy);
  357. content_type enlarged_content1 = index::detail::content(enlarged_box1);
  358. content_type enlarged_content2 = index::detail::content(enlarged_box2);
  359. content_type content_increase1 = enlarged_content1 - content1;
  360. content_type content_increase2 = enlarged_content2 - content2;
  361. // choose group which box content have to be enlarged least or has smaller content or has fewer elements
  362. if ( content_increase1 < content_increase2 ||
  363. ( content_increase1 == content_increase2 &&
  364. ( content1 < content2 ||
  365. ( content1 == content2 && elements1.size() <= elements2.size() ) ) ) )
  366. {
  367. elements1.push_back(elem); // MAY THROW, STRONG (copy)
  368. box1 = enlarged_box1;
  369. content1 = enlarged_content1;
  370. }
  371. else
  372. {
  373. elements2.push_back(elem); // MAY THROW, STRONG (alloc, copy)
  374. box2 = enlarged_box2;
  375. content2 = enlarged_content2;
  376. }
  377. }
  378. BOOST_GEOMETRY_INDEX_ASSERT(0 < remaining, "unexpected value");
  379. --remaining;
  380. }
  381. }
  382. }
  383. BOOST_CATCH(...)
  384. {
  385. elements1.clear();
  386. elements2.clear();
  387. rtree::destroy_elements<MembersHolder>::apply(elements_copy, allocators);
  388. //elements_copy.clear();
  389. BOOST_RETHROW // RETHROW, BASIC
  390. }
  391. BOOST_CATCH_END
  392. }
  393. };
  394. }} // namespace detail::rtree
  395. }}} // namespace boost::geometry::index
  396. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_LINEAR_REDISTRIBUTE_ELEMENTS_HPP