partition.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_PARTITION_HPP
  7. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_PARTITION_HPP
  8. #include <vector>
  9. #include <boost/range/algorithm/copy.hpp>
  10. #include <boost/geometry/algorithms/assign.hpp>
  11. #include <boost/geometry/core/coordinate_type.hpp>
  12. namespace boost { namespace geometry
  13. {
  14. namespace detail { namespace partition
  15. {
  16. typedef std::vector<std::size_t> index_vector_type;
  17. template <int Dimension, typename Box>
  18. inline void divide_box(Box const& box, Box& lower_box, Box& upper_box)
  19. {
  20. typedef typename coordinate_type<Box>::type ctype;
  21. // Divide input box into two parts, e.g. left/right
  22. ctype two = 2;
  23. ctype mid = (geometry::get<min_corner, Dimension>(box)
  24. + geometry::get<max_corner, Dimension>(box)) / two;
  25. lower_box = box;
  26. upper_box = box;
  27. geometry::set<max_corner, Dimension>(lower_box, mid);
  28. geometry::set<min_corner, Dimension>(upper_box, mid);
  29. }
  30. // Divide collection into three subsets: lower, upper and oversized
  31. // (not-fitting)
  32. // (lower == left or bottom, upper == right or top)
  33. template <typename OverlapsPolicy, typename InputCollection, typename Box>
  34. static inline void divide_into_subsets(Box const& lower_box,
  35. Box const& upper_box,
  36. InputCollection const& collection,
  37. index_vector_type const& input,
  38. index_vector_type& lower,
  39. index_vector_type& upper,
  40. index_vector_type& exceeding)
  41. {
  42. typedef boost::range_iterator
  43. <
  44. index_vector_type const
  45. >::type index_iterator_type;
  46. for(index_iterator_type it = boost::begin(input);
  47. it != boost::end(input);
  48. ++it)
  49. {
  50. bool const lower_overlapping = OverlapsPolicy::apply(lower_box,
  51. collection[*it]);
  52. bool const upper_overlapping = OverlapsPolicy::apply(upper_box,
  53. collection[*it]);
  54. if (lower_overlapping && upper_overlapping)
  55. {
  56. exceeding.push_back(*it);
  57. }
  58. else if (lower_overlapping)
  59. {
  60. lower.push_back(*it);
  61. }
  62. else if (upper_overlapping)
  63. {
  64. upper.push_back(*it);
  65. }
  66. else
  67. {
  68. // Is nowhere! Should not occur!
  69. BOOST_ASSERT(true);
  70. }
  71. }
  72. }
  73. // Match collection with itself
  74. template <typename InputCollection, typename Policy>
  75. static inline void handle_one(InputCollection const& collection,
  76. index_vector_type const& input,
  77. Policy& policy)
  78. {
  79. typedef boost::range_iterator<index_vector_type const>::type
  80. index_iterator_type;
  81. // Quadratic behaviour at lowest level (lowest quad, or all exceeding)
  82. for(index_iterator_type it1 = boost::begin(input);
  83. it1 != boost::end(input);
  84. ++it1)
  85. {
  86. index_iterator_type it2 = it1;
  87. for(++it2; it2 != boost::end(input); ++it2)
  88. {
  89. policy.apply(collection[*it1], collection[*it2]);
  90. }
  91. }
  92. }
  93. // Match collection 1 with collection 2
  94. template <typename InputCollection, typename Policy>
  95. static inline void handle_two(
  96. InputCollection const& collection1, index_vector_type const& input1,
  97. InputCollection const& collection2, index_vector_type const& input2,
  98. Policy& policy)
  99. {
  100. typedef boost::range_iterator
  101. <
  102. index_vector_type const
  103. >::type index_iterator_type;
  104. for(index_iterator_type it1 = boost::begin(input1);
  105. it1 != boost::end(input1);
  106. ++it1)
  107. {
  108. for(index_iterator_type it2 = boost::begin(input2);
  109. it2 != boost::end(input2);
  110. ++it2)
  111. {
  112. policy.apply(collection1[*it1], collection2[*it2]);
  113. }
  114. }
  115. }
  116. template
  117. <
  118. int Dimension,
  119. typename Box,
  120. typename OverlapsPolicy,
  121. typename VisitBoxPolicy
  122. >
  123. class partition_one_collection
  124. {
  125. typedef std::vector<std::size_t> index_vector_type;
  126. typedef typename coordinate_type<Box>::type ctype;
  127. typedef partition_one_collection
  128. <
  129. 1 - Dimension,
  130. Box,
  131. OverlapsPolicy,
  132. VisitBoxPolicy
  133. > sub_divide;
  134. template <typename InputCollection, typename Policy>
  135. static inline void next_level(Box const& box,
  136. InputCollection const& collection,
  137. index_vector_type const& input,
  138. int level, std::size_t min_elements,
  139. Policy& policy, VisitBoxPolicy& box_policy)
  140. {
  141. if (boost::size(input) > 0)
  142. {
  143. if (std::size_t(boost::size(input)) > min_elements && level < 100)
  144. {
  145. sub_divide::apply(box, collection, input, level + 1,
  146. min_elements, policy, box_policy);
  147. }
  148. else
  149. {
  150. handle_one(collection, input, policy);
  151. }
  152. }
  153. }
  154. public :
  155. template <typename InputCollection, typename Policy>
  156. static inline void apply(Box const& box,
  157. InputCollection const& collection,
  158. index_vector_type const& input,
  159. int level,
  160. std::size_t min_elements,
  161. Policy& policy, VisitBoxPolicy& box_policy)
  162. {
  163. box_policy.apply(box, level);
  164. Box lower_box, upper_box;
  165. divide_box<Dimension>(box, lower_box, upper_box);
  166. index_vector_type lower, upper, exceeding;
  167. divide_into_subsets<OverlapsPolicy>(lower_box, upper_box, collection,
  168. input, lower, upper, exceeding);
  169. if (boost::size(exceeding) > 0)
  170. {
  171. // All what is not fitting a partition should be combined
  172. // with each other, and with all which is fitting.
  173. handle_one(collection, exceeding, policy);
  174. handle_two(collection, exceeding, collection, lower, policy);
  175. handle_two(collection, exceeding, collection, upper, policy);
  176. }
  177. // Recursively call operation both parts
  178. next_level(lower_box, collection, lower, level, min_elements,
  179. policy, box_policy);
  180. next_level(upper_box, collection, upper, level, min_elements,
  181. policy, box_policy);
  182. }
  183. };
  184. template
  185. <
  186. int Dimension,
  187. typename Box,
  188. typename OverlapsPolicy,
  189. typename VisitBoxPolicy
  190. >
  191. class partition_two_collections
  192. {
  193. typedef std::vector<std::size_t> index_vector_type;
  194. typedef typename coordinate_type<Box>::type ctype;
  195. typedef partition_two_collections
  196. <
  197. 1 - Dimension,
  198. Box,
  199. OverlapsPolicy,
  200. VisitBoxPolicy
  201. > sub_divide;
  202. template <typename InputCollection, typename Policy>
  203. static inline void next_level(Box const& box,
  204. InputCollection const& collection1,
  205. index_vector_type const& input1,
  206. InputCollection const& collection2,
  207. index_vector_type const& input2,
  208. int level, std::size_t min_elements,
  209. Policy& policy, VisitBoxPolicy& box_policy)
  210. {
  211. if (boost::size(input1) > 0 && boost::size(input2) > 0)
  212. {
  213. if (std::size_t(boost::size(input1)) > min_elements
  214. && std::size_t(boost::size(input2)) > min_elements
  215. && level < 100)
  216. {
  217. sub_divide::apply(box, collection1, input1, collection2,
  218. input2, level + 1, min_elements,
  219. policy, box_policy);
  220. }
  221. else
  222. {
  223. box_policy.apply(box, level + 1);
  224. handle_two(collection1, input1, collection2, input2, policy);
  225. }
  226. }
  227. }
  228. public :
  229. template <typename InputCollection, typename Policy>
  230. static inline void apply(Box const& box,
  231. InputCollection const& collection1, index_vector_type const& input1,
  232. InputCollection const& collection2, index_vector_type const& input2,
  233. int level,
  234. std::size_t min_elements,
  235. Policy& policy, VisitBoxPolicy& box_policy)
  236. {
  237. box_policy.apply(box, level);
  238. Box lower_box, upper_box;
  239. divide_box<Dimension>(box, lower_box, upper_box);
  240. index_vector_type lower1, upper1, exceeding1;
  241. index_vector_type lower2, upper2, exceeding2;
  242. divide_into_subsets<OverlapsPolicy>(lower_box, upper_box, collection1,
  243. input1, lower1, upper1, exceeding1);
  244. divide_into_subsets<OverlapsPolicy>(lower_box, upper_box, collection2,
  245. input2, lower2, upper2, exceeding2);
  246. if (boost::size(exceeding1) > 0)
  247. {
  248. // All exceeding from 1 with 2:
  249. handle_two(collection1, exceeding1, collection2, exceeding2,
  250. policy);
  251. // All exceeding from 1 with lower and upper of 2:
  252. handle_two(collection1, exceeding1, collection2, lower2, policy);
  253. handle_two(collection1, exceeding1, collection2, upper2, policy);
  254. }
  255. if (boost::size(exceeding2) > 0)
  256. {
  257. // All exceeding from 2 with lower and upper of 1:
  258. handle_two(collection1, lower1, collection2, exceeding2, policy);
  259. handle_two(collection1, upper1, collection2, exceeding2, policy);
  260. }
  261. next_level(lower_box, collection1, lower1, collection2, lower2, level,
  262. min_elements, policy, box_policy);
  263. next_level(upper_box, collection1, upper1, collection2, upper2, level,
  264. min_elements, policy, box_policy);
  265. }
  266. };
  267. }} // namespace detail::partition
  268. struct visit_no_policy
  269. {
  270. template <typename Box>
  271. static inline void apply(Box const&, int )
  272. {}
  273. };
  274. template
  275. <
  276. typename Box,
  277. typename ExpandPolicy,
  278. typename OverlapsPolicy,
  279. typename VisitBoxPolicy = visit_no_policy
  280. >
  281. class partition
  282. {
  283. typedef std::vector<std::size_t> index_vector_type;
  284. template <typename InputCollection>
  285. static inline void expand_to_collection(InputCollection const& collection,
  286. Box& total, index_vector_type& index_vector)
  287. {
  288. std::size_t index = 0;
  289. for(typename boost::range_iterator<InputCollection const>::type it
  290. = boost::begin(collection);
  291. it != boost::end(collection);
  292. ++it, ++index)
  293. {
  294. ExpandPolicy::apply(total, *it);
  295. index_vector.push_back(index);
  296. }
  297. }
  298. public :
  299. template <typename InputCollection, typename VisitPolicy>
  300. static inline void apply(InputCollection const& collection,
  301. VisitPolicy& visitor,
  302. std::size_t min_elements = 16,
  303. VisitBoxPolicy box_visitor = visit_no_policy()
  304. )
  305. {
  306. if (std::size_t(boost::size(collection)) > min_elements)
  307. {
  308. index_vector_type index_vector;
  309. Box total;
  310. assign_inverse(total);
  311. expand_to_collection(collection, total, index_vector);
  312. detail::partition::partition_one_collection
  313. <
  314. 0, Box,
  315. OverlapsPolicy,
  316. VisitBoxPolicy
  317. >::apply(total, collection, index_vector, 0, min_elements,
  318. visitor, box_visitor);
  319. }
  320. else
  321. {
  322. typedef typename boost::range_iterator
  323. <
  324. InputCollection const
  325. >::type iterator_type;
  326. for(iterator_type it1 = boost::begin(collection);
  327. it1 != boost::end(collection);
  328. ++it1)
  329. {
  330. iterator_type it2 = it1;
  331. for(++it2; it2 != boost::end(collection); ++it2)
  332. {
  333. visitor.apply(*it1, *it2);
  334. }
  335. }
  336. }
  337. }
  338. template <typename InputCollection, typename VisitPolicy>
  339. static inline void apply(InputCollection const& collection1,
  340. InputCollection const& collection2,
  341. VisitPolicy& visitor,
  342. std::size_t min_elements = 16,
  343. VisitBoxPolicy box_visitor = visit_no_policy()
  344. )
  345. {
  346. if (std::size_t(boost::size(collection1)) > min_elements
  347. && std::size_t(boost::size(collection2)) > min_elements)
  348. {
  349. index_vector_type index_vector1, index_vector2;
  350. Box total;
  351. assign_inverse(total);
  352. expand_to_collection(collection1, total, index_vector1);
  353. expand_to_collection(collection2, total, index_vector2);
  354. detail::partition::partition_two_collections
  355. <
  356. 0, Box, OverlapsPolicy, VisitBoxPolicy
  357. >::apply(total,
  358. collection1, index_vector1,
  359. collection2, index_vector2,
  360. 0, min_elements, visitor, box_visitor);
  361. }
  362. else
  363. {
  364. typedef typename boost::range_iterator
  365. <
  366. InputCollection const
  367. >::type iterator_type;
  368. for(iterator_type it1 = boost::begin(collection1);
  369. it1 != boost::end(collection1);
  370. ++it1)
  371. {
  372. for(iterator_type it2 = boost::begin(collection2);
  373. it2 != boost::end(collection2);
  374. ++it2)
  375. {
  376. visitor.apply(*it1, *it2);
  377. }
  378. }
  379. }
  380. }
  381. };
  382. }} // namespace boost::geometry
  383. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_PARTITION_HPP