test_rtree.hpp 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  1. // Boost.Geometry Index
  2. // Unit Test
  3. // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2019.
  5. // Modifications copyright (c) 2019, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  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)
  10. #ifndef BOOST_GEOMETRY_INDEX_TEST_RTREE_HPP
  11. #define BOOST_GEOMETRY_INDEX_TEST_RTREE_HPP
  12. #include <boost/foreach.hpp>
  13. #include <vector>
  14. #include <algorithm>
  15. #include <geometry_index_test_common.hpp>
  16. #include <boost/geometry/index/rtree.hpp>
  17. #include <boost/geometry/geometries/point.hpp>
  18. #include <boost/geometry/geometries/box.hpp>
  19. #include <boost/geometry/geometries/segment.hpp>
  20. #include <boost/geometry/index/detail/rtree/utilities/are_levels_ok.hpp>
  21. #include <boost/geometry/index/detail/rtree/utilities/are_boxes_ok.hpp>
  22. //#include <boost/geometry/geometries/ring.hpp>
  23. //#include <boost/geometry/geometries/polygon.hpp>
  24. namespace generate {
  25. // Set point's coordinates
  26. template <typename Point>
  27. struct outside_point
  28. {};
  29. template <typename T, typename C>
  30. struct outside_point< bg::model::point<T, 2, C> >
  31. {
  32. typedef bg::model::point<T, 2, C> P;
  33. static P apply()
  34. {
  35. return P(13, 26);
  36. }
  37. };
  38. template <typename T, typename C>
  39. struct outside_point< bg::model::point<T, 3, C> >
  40. {
  41. typedef bg::model::point<T, 3, C> P;
  42. static P apply()
  43. {
  44. return P(13, 26, 13);
  45. }
  46. };
  47. // Default value generation
  48. template <typename Value>
  49. struct value_default
  50. {
  51. static Value apply(){ return Value(); }
  52. };
  53. // Values, input and rtree generation
  54. template <typename Value>
  55. struct value
  56. {};
  57. template <typename T, typename C>
  58. struct value< bg::model::point<T, 2, C> >
  59. {
  60. typedef bg::model::point<T, 2, C> P;
  61. static P apply(int x, int y)
  62. {
  63. return P(x, y);
  64. }
  65. };
  66. template <typename T, typename C>
  67. struct value< bg::model::box< bg::model::point<T, 2, C> > >
  68. {
  69. typedef bg::model::point<T, 2, C> P;
  70. typedef bg::model::box<P> B;
  71. static B apply(int x, int y)
  72. {
  73. return B(P(x, y), P(x + 2, y + 3));
  74. }
  75. };
  76. template <typename T, typename C>
  77. struct value< bg::model::segment< bg::model::point<T, 2, C> > >
  78. {
  79. typedef bg::model::point<T, 2, C> P;
  80. typedef bg::model::segment<P> S;
  81. static S apply(int x, int y)
  82. {
  83. return S(P(x, y), P(x + 2, y + 3));
  84. }
  85. };
  86. template <typename T, typename C>
  87. struct value< std::pair<bg::model::point<T, 2, C>, int> >
  88. {
  89. typedef bg::model::point<T, 2, C> P;
  90. typedef std::pair<P, int> R;
  91. static R apply(int x, int y)
  92. {
  93. return std::make_pair(P(x, y), x + y * 100);
  94. }
  95. };
  96. template <typename T, typename C>
  97. struct value< std::pair<bg::model::box< bg::model::point<T, 2, C> >, int> >
  98. {
  99. typedef bg::model::point<T, 2, C> P;
  100. typedef bg::model::box<P> B;
  101. typedef std::pair<B, int> R;
  102. static R apply(int x, int y)
  103. {
  104. return std::make_pair(B(P(x, y), P(x + 2, y + 3)), x + y * 100);
  105. }
  106. };
  107. template <typename T, typename C>
  108. struct value< std::pair<bg::model::segment< bg::model::point<T, 2, C> >, int> >
  109. {
  110. typedef bg::model::point<T, 2, C> P;
  111. typedef bg::model::segment<P> S;
  112. typedef std::pair<S, int> R;
  113. static R apply(int x, int y)
  114. {
  115. return std::make_pair(S(P(x, y), P(x + 2, y + 3)), x + y * 100);
  116. }
  117. };
  118. template <typename T, typename C>
  119. struct value< boost::tuple<bg::model::point<T, 2, C>, int, int> >
  120. {
  121. typedef bg::model::point<T, 2, C> P;
  122. typedef boost::tuple<P, int, int> R;
  123. static R apply(int x, int y)
  124. {
  125. return boost::make_tuple(P(x, y), x + y * 100, 0);
  126. }
  127. };
  128. template <typename T, typename C>
  129. struct value< boost::tuple<bg::model::box< bg::model::point<T, 2, C> >, int, int> >
  130. {
  131. typedef bg::model::point<T, 2, C> P;
  132. typedef bg::model::box<P> B;
  133. typedef boost::tuple<B, int, int> R;
  134. static R apply(int x, int y)
  135. {
  136. return boost::make_tuple(B(P(x, y), P(x + 2, y + 3)), x + y * 100, 0);
  137. }
  138. };
  139. template <typename T, typename C>
  140. struct value< boost::tuple<bg::model::segment< bg::model::point<T, 2, C> >, int, int> >
  141. {
  142. typedef bg::model::point<T, 2, C> P;
  143. typedef bg::model::segment<P> S;
  144. typedef boost::tuple<S, int, int> R;
  145. static R apply(int x, int y)
  146. {
  147. return boost::make_tuple(S(P(x, y), P(x + 2, y + 3)), x + y * 100, 0);
  148. }
  149. };
  150. template <typename T, typename C>
  151. struct value< bg::model::point<T, 3, C> >
  152. {
  153. typedef bg::model::point<T, 3, C> P;
  154. static P apply(int x, int y, int z)
  155. {
  156. return P(x, y, z);
  157. }
  158. };
  159. template <typename T, typename C>
  160. struct value< bg::model::box< bg::model::point<T, 3, C> > >
  161. {
  162. typedef bg::model::point<T, 3, C> P;
  163. typedef bg::model::box<P> B;
  164. static B apply(int x, int y, int z)
  165. {
  166. return B(P(x, y, z), P(x + 2, y + 3, z + 4));
  167. }
  168. };
  169. template <typename T, typename C>
  170. struct value< std::pair<bg::model::point<T, 3, C>, int> >
  171. {
  172. typedef bg::model::point<T, 3, C> P;
  173. typedef std::pair<P, int> R;
  174. static R apply(int x, int y, int z)
  175. {
  176. return std::make_pair(P(x, y, z), x + y * 100 + z * 10000);
  177. }
  178. };
  179. template <typename T, typename C>
  180. struct value< std::pair<bg::model::box< bg::model::point<T, 3, C> >, int> >
  181. {
  182. typedef bg::model::point<T, 3, C> P;
  183. typedef bg::model::box<P> B;
  184. typedef std::pair<B, int> R;
  185. static R apply(int x, int y, int z)
  186. {
  187. return std::make_pair(B(P(x, y, z), P(x + 2, y + 3, z + 4)), x + y * 100 + z * 10000);
  188. }
  189. };
  190. template <typename T, typename C>
  191. struct value< boost::tuple<bg::model::point<T, 3, C>, int, int> >
  192. {
  193. typedef bg::model::point<T, 3, C> P;
  194. typedef boost::tuple<P, int, int> R;
  195. static R apply(int x, int y, int z)
  196. {
  197. return boost::make_tuple(P(x, y, z), x + y * 100 + z * 10000, 0);
  198. }
  199. };
  200. template <typename T, typename C>
  201. struct value< boost::tuple<bg::model::box< bg::model::point<T, 3, C> >, int, int> >
  202. {
  203. typedef bg::model::point<T, 3, C> P;
  204. typedef bg::model::box<P> B;
  205. typedef boost::tuple<B, int, int> R;
  206. static R apply(int x, int y, int z)
  207. {
  208. return boost::make_tuple(B(P(x, y, z), P(x + 2, y + 3, z + 4)), x + y * 100 + z * 10000, 0);
  209. }
  210. };
  211. #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  212. template <typename T, typename C>
  213. struct value< std::tuple<bg::model::point<T, 2, C>, int, int> >
  214. {
  215. typedef bg::model::point<T, 2, C> P;
  216. typedef std::tuple<P, int, int> R;
  217. static R apply(int x, int y)
  218. {
  219. return std::make_tuple(P(x, y), x + y * 100, 0);
  220. }
  221. };
  222. template <typename T, typename C>
  223. struct value< std::tuple<bg::model::box< bg::model::point<T, 2, C> >, int, int> >
  224. {
  225. typedef bg::model::point<T, 2, C> P;
  226. typedef bg::model::box<P> B;
  227. typedef std::tuple<B, int, int> R;
  228. static R apply(int x, int y)
  229. {
  230. return std::make_tuple(B(P(x, y), P(x + 2, y + 3)), x + y * 100, 0);
  231. }
  232. };
  233. template <typename T, typename C>
  234. struct value< std::tuple<bg::model::segment< bg::model::point<T, 2, C> >, int, int> >
  235. {
  236. typedef bg::model::point<T, 2, C> P;
  237. typedef bg::model::segment<P> S;
  238. typedef std::tuple<S, int, int> R;
  239. static R apply(int x, int y)
  240. {
  241. return std::make_tuple(S(P(x, y), P(x + 2, y + 3)), x + y * 100, 0);
  242. }
  243. };
  244. template <typename T, typename C>
  245. struct value< std::tuple<bg::model::point<T, 3, C>, int, int> >
  246. {
  247. typedef bg::model::point<T, 3, C> P;
  248. typedef std::tuple<P, int, int> R;
  249. static R apply(int x, int y, int z)
  250. {
  251. return std::make_tuple(P(x, y, z), x + y * 100 + z * 10000, 0);
  252. }
  253. };
  254. template <typename T, typename C>
  255. struct value< std::tuple<bg::model::box< bg::model::point<T, 3, C> >, int, int> >
  256. {
  257. typedef bg::model::point<T, 3, C> P;
  258. typedef bg::model::box<P> B;
  259. typedef std::tuple<B, int, int> R;
  260. static R apply(int x, int y, int z)
  261. {
  262. return std::make_tuple(B(P(x, y, z), P(x + 2, y + 3, z + 4)), x + y * 100 + z * 10000, 0);
  263. }
  264. };
  265. #endif // #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  266. } // namespace generate
  267. // shared_ptr value
  268. template <typename Indexable>
  269. struct test_object
  270. {
  271. test_object(Indexable const& indexable_) : indexable(indexable_) {}
  272. Indexable indexable;
  273. };
  274. namespace boost { namespace geometry { namespace index {
  275. template <typename Indexable>
  276. struct indexable< boost::shared_ptr< test_object<Indexable> > >
  277. {
  278. typedef boost::shared_ptr< test_object<Indexable> > value_type;
  279. typedef Indexable const& result_type;
  280. result_type operator()(value_type const& value) const
  281. {
  282. return value->indexable;
  283. }
  284. };
  285. }}}
  286. namespace generate {
  287. template <typename T, typename C>
  288. struct value< boost::shared_ptr<test_object<bg::model::point<T, 2, C> > > >
  289. {
  290. typedef bg::model::point<T, 2, C> P;
  291. typedef test_object<P> O;
  292. typedef boost::shared_ptr<O> R;
  293. static R apply(int x, int y)
  294. {
  295. return R(new O(P(x, y)));
  296. }
  297. };
  298. template <typename T, typename C>
  299. struct value< boost::shared_ptr<test_object<bg::model::point<T, 3, C> > > >
  300. {
  301. typedef bg::model::point<T, 3, C> P;
  302. typedef test_object<P> O;
  303. typedef boost::shared_ptr<O> R;
  304. static R apply(int x, int y, int z)
  305. {
  306. return R(new O(P(x, y, z)));
  307. }
  308. };
  309. template <typename T, typename C>
  310. struct value< boost::shared_ptr<test_object<bg::model::box<bg::model::point<T, 2, C> > > > >
  311. {
  312. typedef bg::model::point<T, 2, C> P;
  313. typedef bg::model::box<P> B;
  314. typedef test_object<B> O;
  315. typedef boost::shared_ptr<O> R;
  316. static R apply(int x, int y)
  317. {
  318. return R(new O(B(P(x, y), P(x + 2, y + 3))));
  319. }
  320. };
  321. template <typename T, typename C>
  322. struct value< boost::shared_ptr<test_object<bg::model::box<bg::model::point<T, 3, C> > > > >
  323. {
  324. typedef bg::model::point<T, 3, C> P;
  325. typedef bg::model::box<P> B;
  326. typedef test_object<B> O;
  327. typedef boost::shared_ptr<O> R;
  328. static R apply(int x, int y, int z)
  329. {
  330. return R(new O(B(P(x, y, z), P(x + 2, y + 3, z + 4))));
  331. }
  332. };
  333. template <typename T, typename C>
  334. struct value< boost::shared_ptr<test_object<bg::model::segment<bg::model::point<T, 2, C> > > > >
  335. {
  336. typedef bg::model::point<T, 2, C> P;
  337. typedef bg::model::segment<P> S;
  338. typedef test_object<S> O;
  339. typedef boost::shared_ptr<O> R;
  340. static R apply(int x, int y)
  341. {
  342. return R(new O(S(P(x, y), P(x + 2, y + 3))));
  343. }
  344. };
  345. } //namespace generate
  346. // counting value
  347. template <typename Indexable>
  348. struct counting_value
  349. {
  350. counting_value() { counter()++; }
  351. counting_value(Indexable const& i) : indexable(i) { counter()++; }
  352. counting_value(counting_value const& c) : indexable(c.indexable) { counter()++; }
  353. ~counting_value() { counter()--; }
  354. static size_t & counter() { static size_t c = 0; return c; }
  355. Indexable indexable;
  356. };
  357. namespace boost { namespace geometry { namespace index {
  358. template <typename Indexable>
  359. struct indexable< counting_value<Indexable> >
  360. {
  361. typedef counting_value<Indexable> value_type;
  362. typedef Indexable const& result_type;
  363. result_type operator()(value_type const& value) const
  364. {
  365. return value.indexable;
  366. }
  367. };
  368. template <typename Indexable>
  369. struct equal_to< counting_value<Indexable> >
  370. {
  371. typedef counting_value<Indexable> value_type;
  372. typedef bool result_type;
  373. bool operator()(value_type const& v1, value_type const& v2) const
  374. {
  375. return boost::geometry::equals(v1.indexable, v2.indexable);
  376. }
  377. };
  378. }}}
  379. namespace generate {
  380. template <typename T, typename C>
  381. struct value< counting_value<bg::model::point<T, 2, C> > >
  382. {
  383. typedef bg::model::point<T, 2, C> P;
  384. typedef counting_value<P> R;
  385. static R apply(int x, int y) { return R(P(x, y)); }
  386. };
  387. template <typename T, typename C>
  388. struct value< counting_value<bg::model::point<T, 3, C> > >
  389. {
  390. typedef bg::model::point<T, 3, C> P;
  391. typedef counting_value<P> R;
  392. static R apply(int x, int y, int z) { return R(P(x, y, z)); }
  393. };
  394. template <typename T, typename C>
  395. struct value< counting_value<bg::model::box<bg::model::point<T, 2, C> > > >
  396. {
  397. typedef bg::model::point<T, 2, C> P;
  398. typedef bg::model::box<P> B;
  399. typedef counting_value<B> R;
  400. static R apply(int x, int y) { return R(B(P(x, y), P(x+2, y+3))); }
  401. };
  402. template <typename T, typename C>
  403. struct value< counting_value<bg::model::box<bg::model::point<T, 3, C> > > >
  404. {
  405. typedef bg::model::point<T, 3, C> P;
  406. typedef bg::model::box<P> B;
  407. typedef counting_value<B> R;
  408. static R apply(int x, int y, int z) { return R(B(P(x, y, z), P(x+2, y+3, z+4))); }
  409. };
  410. template <typename T, typename C>
  411. struct value< counting_value<bg::model::segment<bg::model::point<T, 2, C> > > >
  412. {
  413. typedef bg::model::point<T, 2, C> P;
  414. typedef bg::model::segment<P> S;
  415. typedef counting_value<S> R;
  416. static R apply(int x, int y) { return R(S(P(x, y), P(x+2, y+3))); }
  417. };
  418. } // namespace generate
  419. // value without default constructor
  420. template <typename Indexable>
  421. struct value_no_dctor
  422. {
  423. value_no_dctor(Indexable const& i) : indexable(i) {}
  424. Indexable indexable;
  425. };
  426. namespace boost { namespace geometry { namespace index {
  427. template <typename Indexable>
  428. struct indexable< value_no_dctor<Indexable> >
  429. {
  430. typedef value_no_dctor<Indexable> value_type;
  431. typedef Indexable const& result_type;
  432. result_type operator()(value_type const& value) const
  433. {
  434. return value.indexable;
  435. }
  436. };
  437. template <typename Indexable>
  438. struct equal_to< value_no_dctor<Indexable> >
  439. {
  440. typedef value_no_dctor<Indexable> value_type;
  441. typedef bool result_type;
  442. bool operator()(value_type const& v1, value_type const& v2) const
  443. {
  444. return boost::geometry::equals(v1.indexable, v2.indexable);
  445. }
  446. };
  447. }}}
  448. namespace generate {
  449. template <typename Indexable>
  450. struct value_default< value_no_dctor<Indexable> >
  451. {
  452. static value_no_dctor<Indexable> apply() { return value_no_dctor<Indexable>(Indexable()); }
  453. };
  454. template <typename T, typename C>
  455. struct value< value_no_dctor<bg::model::point<T, 2, C> > >
  456. {
  457. typedef bg::model::point<T, 2, C> P;
  458. typedef value_no_dctor<P> R;
  459. static R apply(int x, int y) { return R(P(x, y)); }
  460. };
  461. template <typename T, typename C>
  462. struct value< value_no_dctor<bg::model::point<T, 3, C> > >
  463. {
  464. typedef bg::model::point<T, 3, C> P;
  465. typedef value_no_dctor<P> R;
  466. static R apply(int x, int y, int z) { return R(P(x, y, z)); }
  467. };
  468. template <typename T, typename C>
  469. struct value< value_no_dctor<bg::model::box<bg::model::point<T, 2, C> > > >
  470. {
  471. typedef bg::model::point<T, 2, C> P;
  472. typedef bg::model::box<P> B;
  473. typedef value_no_dctor<B> R;
  474. static R apply(int x, int y) { return R(B(P(x, y), P(x+2, y+3))); }
  475. };
  476. template <typename T, typename C>
  477. struct value< value_no_dctor<bg::model::box<bg::model::point<T, 3, C> > > >
  478. {
  479. typedef bg::model::point<T, 3, C> P;
  480. typedef bg::model::box<P> B;
  481. typedef value_no_dctor<B> R;
  482. static R apply(int x, int y, int z) { return R(B(P(x, y, z), P(x+2, y+3, z+4))); }
  483. };
  484. template <typename T, typename C>
  485. struct value< value_no_dctor<bg::model::segment<bg::model::point<T, 2, C> > > >
  486. {
  487. typedef bg::model::point<T, 2, C> P;
  488. typedef bg::model::segment<P> S;
  489. typedef value_no_dctor<S> R;
  490. static R apply(int x, int y) { return R(S(P(x, y), P(x+2, y+3))); }
  491. };
  492. // generate input
  493. template <size_t Dimension>
  494. struct input
  495. {};
  496. template <>
  497. struct input<2>
  498. {
  499. template <typename Value, typename Box>
  500. static void apply(std::vector<Value> & input, Box & qbox, int size = 1)
  501. {
  502. BOOST_GEOMETRY_INDEX_ASSERT(0 < size, "the value must be greather than 0");
  503. for ( int i = 0 ; i < 12 * size ; i += 3 )
  504. {
  505. for ( int j = 1 ; j < 25 * size ; j += 4 )
  506. {
  507. input.push_back( generate::value<Value>::apply(i, j) );
  508. }
  509. }
  510. typedef typename bg::traits::point_type<Box>::type P;
  511. qbox = Box(P(3, 0), P(10, 9));
  512. }
  513. };
  514. template <>
  515. struct input<3>
  516. {
  517. template <typename Value, typename Box>
  518. static void apply(std::vector<Value> & input, Box & qbox, int size = 1)
  519. {
  520. BOOST_GEOMETRY_INDEX_ASSERT(0 < size, "the value must be greather than 0");
  521. for ( int i = 0 ; i < 12 * size ; i += 3 )
  522. {
  523. for ( int j = 1 ; j < 25 * size ; j += 4 )
  524. {
  525. for ( int k = 2 ; k < 12 * size ; k += 5 )
  526. {
  527. input.push_back( generate::value<Value>::apply(i, j, k) );
  528. }
  529. }
  530. }
  531. typedef typename bg::traits::point_type<Box>::type P;
  532. qbox = Box(P(3, 0, 3), P(10, 9, 11));
  533. }
  534. };
  535. // generate_value_outside
  536. template <typename Value, size_t Dimension>
  537. struct value_outside_impl
  538. {};
  539. template <typename Value>
  540. struct value_outside_impl<Value, 2>
  541. {
  542. static Value apply()
  543. {
  544. //TODO - for size > 1 in generate_input<> this won't be outside
  545. return generate::value<Value>::apply(13, 26);
  546. }
  547. };
  548. template <typename Value>
  549. struct value_outside_impl<Value, 3>
  550. {
  551. static Value apply()
  552. {
  553. //TODO - for size > 1 in generate_input<> this won't be outside
  554. return generate::value<Value>::apply(13, 26, 13);
  555. }
  556. };
  557. template <typename Rtree>
  558. inline typename Rtree::value_type
  559. value_outside()
  560. {
  561. typedef typename Rtree::value_type V;
  562. typedef typename Rtree::indexable_type I;
  563. return value_outside_impl<V, bg::dimension<I>::value>::apply();
  564. }
  565. template<typename Rtree, typename Elements, typename Box>
  566. void rtree(Rtree & tree, Elements & input, Box & qbox)
  567. {
  568. typedef typename Rtree::indexable_type I;
  569. generate::input<
  570. bg::dimension<I>::value
  571. >::apply(input, qbox);
  572. tree.insert(input.begin(), input.end());
  573. }
  574. } // namespace generate
  575. namespace basictest {
  576. // low level test functions
  577. template <typename Rtree, typename Iter, typename Value>
  578. Iter find(Rtree const& rtree, Iter first, Iter last, Value const& value)
  579. {
  580. for ( ; first != last ; ++first )
  581. if ( rtree.value_eq()(value, *first) )
  582. return first;
  583. return first;
  584. }
  585. template <typename Rtree, typename Value>
  586. void compare_outputs(Rtree const& rtree, std::vector<Value> const& output, std::vector<Value> const& expected_output)
  587. {
  588. bool are_sizes_ok = (expected_output.size() == output.size());
  589. BOOST_CHECK( are_sizes_ok );
  590. if ( are_sizes_ok )
  591. {
  592. BOOST_FOREACH(Value const& v, expected_output)
  593. {
  594. BOOST_CHECK(find(rtree, output.begin(), output.end(), v) != output.end() );
  595. }
  596. }
  597. }
  598. template <typename Rtree, typename Range1, typename Range2>
  599. void exactly_the_same_outputs(Rtree const& rtree, Range1 const& output, Range2 const& expected_output)
  600. {
  601. size_t s1 = std::distance(output.begin(), output.end());
  602. size_t s2 = std::distance(expected_output.begin(), expected_output.end());
  603. BOOST_CHECK(s1 == s2);
  604. if ( s1 == s2 )
  605. {
  606. typename Range1::const_iterator it1 = output.begin();
  607. typename Range2::const_iterator it2 = expected_output.begin();
  608. for ( ; it1 != output.end() && it2 != expected_output.end() ; ++it1, ++it2 )
  609. {
  610. if ( !rtree.value_eq()(*it1, *it2) )
  611. {
  612. BOOST_CHECK(false && "rtree.translator().equals(*it1, *it2)");
  613. break;
  614. }
  615. }
  616. }
  617. }
  618. // alternative version of std::copy taking iterators of differnet types
  619. template <typename First, typename Last, typename Out>
  620. void copy_alt(First first, Last last, Out out)
  621. {
  622. for ( ; first != last ; ++first, ++out )
  623. *out = *first;
  624. }
  625. // test query iterators
  626. template <typename QItF, typename QItL>
  627. void check_fwd_iterators(QItF first, QItL last)
  628. {
  629. QItF vinit = QItF();
  630. BOOST_CHECK(vinit == last);
  631. #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
  632. QItL vinit2 = QItL();
  633. BOOST_CHECK(vinit2 == last);
  634. #endif
  635. QItF def;
  636. BOOST_CHECK(def == last);
  637. #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
  638. QItL def2;
  639. BOOST_CHECK(def2 == last);
  640. #endif
  641. QItF it = first;
  642. for ( ; it != last && first != last ; ++it, ++first)
  643. {
  644. BOOST_CHECK(it == first);
  645. bg::index::equal_to<typename std::iterator_traits<QItF>::value_type> eq;
  646. BOOST_CHECK(eq(*it, *first));
  647. }
  648. BOOST_CHECK(it == last);
  649. BOOST_CHECK(first == last);
  650. }
  651. // spatial query
  652. template <typename Rtree, typename Value, typename Predicates>
  653. void spatial_query(Rtree & rtree, Predicates const& pred, std::vector<Value> const& expected_output)
  654. {
  655. BOOST_CHECK( bgi::detail::rtree::utilities::are_levels_ok(rtree) );
  656. if ( !rtree.empty() )
  657. BOOST_CHECK( bgi::detail::rtree::utilities::are_boxes_ok(rtree) );
  658. std::vector<Value> output;
  659. size_t n = rtree.query(pred, std::back_inserter(output));
  660. BOOST_CHECK( expected_output.size() == n );
  661. compare_outputs(rtree, output, expected_output);
  662. std::vector<Value> output2;
  663. size_t n2 = query(rtree, pred, std::back_inserter(output2));
  664. BOOST_CHECK( n == n2 );
  665. exactly_the_same_outputs(rtree, output, output2);
  666. exactly_the_same_outputs(rtree, output, rtree | bgi::adaptors::queried(pred));
  667. std::vector<Value> output3;
  668. std::copy(rtree.qbegin(pred), rtree.qend(), std::back_inserter(output3));
  669. compare_outputs(rtree, output3, expected_output);
  670. std::vector<Value> output4;
  671. std::copy(qbegin(rtree, pred), qend(rtree), std::back_inserter(output4));
  672. exactly_the_same_outputs(rtree, output3, output4);
  673. check_fwd_iterators(rtree.qbegin(pred), rtree.qend());
  674. #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
  675. {
  676. std::vector<Value> output4;
  677. std::copy(rtree.qbegin_(pred), rtree.qend_(pred), std::back_inserter(output4));
  678. compare_outputs(rtree, output4, expected_output);
  679. output4.clear();
  680. copy_alt(rtree.qbegin_(pred), rtree.qend_(), std::back_inserter(output4));
  681. compare_outputs(rtree, output4, expected_output);
  682. check_fwd_iterators(rtree.qbegin_(pred), rtree.qend_(pred));
  683. check_fwd_iterators(rtree.qbegin_(pred), rtree.qend_());
  684. }
  685. #endif
  686. }
  687. // rtree specific queries tests
  688. template <typename Rtree, typename Value, typename Box>
  689. void intersects(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  690. {
  691. std::vector<Value> expected_output;
  692. BOOST_FOREACH(Value const& v, input)
  693. if ( bg::intersects(tree.indexable_get()(v), qbox) )
  694. expected_output.push_back(v);
  695. //spatial_query(tree, qbox, expected_output);
  696. spatial_query(tree, bgi::intersects(qbox), expected_output);
  697. spatial_query(tree, !bgi::disjoint(qbox), expected_output);
  698. /*typedef bg::traits::point_type<Box>::type P;
  699. bg::model::ring<P> qring;
  700. bg::convert(qbox, qring);
  701. spatial_query(tree, bgi::intersects(qring), expected_output);
  702. spatial_query(tree, !bgi::disjoint(qring), expected_output);
  703. bg::model::polygon<P> qpoly;
  704. bg::convert(qbox, qpoly);
  705. spatial_query(tree, bgi::intersects(qpoly), expected_output);
  706. spatial_query(tree, !bgi::disjoint(qpoly), expected_output);*/
  707. }
  708. template <typename Rtree, typename Value, typename Box>
  709. void disjoint(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  710. {
  711. std::vector<Value> expected_output;
  712. BOOST_FOREACH(Value const& v, input)
  713. if ( bg::disjoint(tree.indexable_get()(v), qbox) )
  714. expected_output.push_back(v);
  715. spatial_query(tree, bgi::disjoint(qbox), expected_output);
  716. spatial_query(tree, !bgi::intersects(qbox), expected_output);
  717. /*typedef bg::traits::point_type<Box>::type P;
  718. bg::model::ring<P> qring;
  719. bg::convert(qbox, qring);
  720. spatial_query(tree, bgi::disjoint(qring), expected_output);
  721. bg::model::polygon<P> qpoly;
  722. bg::convert(qbox, qpoly);
  723. spatial_query(tree, bgi::disjoint(qpoly), expected_output);*/
  724. }
  725. template <typename Tag>
  726. struct contains_impl
  727. {
  728. template <typename Rtree, typename Value, typename Box>
  729. static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  730. {
  731. std::vector<Value> expected_output;
  732. BOOST_FOREACH(Value const& v, input)
  733. if ( bg::within(qbox, tree.indexable_get()(v)) )
  734. expected_output.push_back(v);
  735. spatial_query(tree, bgi::contains(qbox), expected_output);
  736. /*typedef bg::traits::point_type<Box>::type P;
  737. bg::model::ring<P> qring;
  738. bg::convert(qbox, qring);
  739. spatial_query(tree, bgi::contains(qring), expected_output);
  740. bg::model::polygon<P> qpoly;
  741. bg::convert(qbox, qpoly);
  742. spatial_query(tree, bgi::contains(qpoly), expected_output);*/
  743. }
  744. };
  745. template <>
  746. struct contains_impl<bg::point_tag>
  747. {
  748. template <typename Rtree, typename Value, typename Box>
  749. static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
  750. {}
  751. };
  752. template <>
  753. struct contains_impl<bg::segment_tag>
  754. {
  755. template <typename Rtree, typename Value, typename Box>
  756. static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
  757. {}
  758. };
  759. template <typename Rtree, typename Value, typename Box>
  760. void contains(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  761. {
  762. contains_impl<
  763. typename bg::tag<
  764. typename Rtree::indexable_type
  765. >::type
  766. >::apply(tree, input, qbox);
  767. }
  768. template <typename Tag>
  769. struct covered_by_impl
  770. {
  771. template <typename Rtree, typename Value, typename Box>
  772. static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  773. {
  774. std::vector<Value> expected_output;
  775. BOOST_FOREACH(Value const& v, input)
  776. {
  777. if ( bgi::detail::covered_by_bounds(
  778. tree.indexable_get()(v),
  779. qbox,
  780. bgi::detail::get_strategy(tree.parameters())) )
  781. {
  782. expected_output.push_back(v);
  783. }
  784. }
  785. spatial_query(tree, bgi::covered_by(qbox), expected_output);
  786. /*typedef bg::traits::point_type<Box>::type P;
  787. bg::model::ring<P> qring;
  788. bg::convert(qbox, qring);
  789. spatial_query(tree, bgi::covered_by(qring), expected_output);
  790. bg::model::polygon<P> qpoly;
  791. bg::convert(qbox, qpoly);
  792. spatial_query(tree, bgi::covered_by(qpoly), expected_output);*/
  793. }
  794. };
  795. template <>
  796. struct covered_by_impl<bg::segment_tag>
  797. {
  798. template <typename Rtree, typename Value, typename Box>
  799. static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
  800. {}
  801. };
  802. template <typename Rtree, typename Value, typename Box>
  803. void covered_by(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  804. {
  805. covered_by_impl<
  806. typename bg::tag<
  807. typename Rtree::indexable_type
  808. >::type
  809. >::apply(tree, input, qbox);
  810. }
  811. template <typename Tag>
  812. struct covers_impl
  813. {
  814. template <typename Rtree, typename Value, typename Box>
  815. static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  816. {
  817. std::vector<Value> expected_output;
  818. BOOST_FOREACH(Value const& v, input)
  819. if ( bg::covered_by(qbox, tree.indexable_get()(v)) )
  820. expected_output.push_back(v);
  821. spatial_query(tree, bgi::covers(qbox), expected_output);
  822. /*typedef bg::traits::point_type<Box>::type P;
  823. bg::model::ring<P> qring;
  824. bg::convert(qbox, qring);
  825. spatial_query(tree, bgi::covers(qring), expected_output);
  826. bg::model::polygon<P> qpoly;
  827. bg::convert(qbox, qpoly);
  828. spatial_query(tree, bgi::covers(qpoly), expected_output);*/
  829. }
  830. };
  831. template <>
  832. struct covers_impl<bg::point_tag>
  833. {
  834. template <typename Rtree, typename Value, typename Box>
  835. static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
  836. {}
  837. };
  838. template <>
  839. struct covers_impl<bg::segment_tag>
  840. {
  841. template <typename Rtree, typename Value, typename Box>
  842. static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
  843. {}
  844. };
  845. template <typename Rtree, typename Value, typename Box>
  846. void covers(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  847. {
  848. covers_impl<
  849. typename bg::tag<
  850. typename Rtree::indexable_type
  851. >::type
  852. >::apply(tree, input, qbox);
  853. }
  854. template <typename Tag>
  855. struct overlaps_impl
  856. {
  857. template <typename Rtree, typename Value, typename Box>
  858. static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  859. {
  860. std::vector<Value> expected_output;
  861. BOOST_FOREACH(Value const& v, input)
  862. if ( bg::overlaps(tree.indexable_get()(v), qbox) )
  863. expected_output.push_back(v);
  864. spatial_query(tree, bgi::overlaps(qbox), expected_output);
  865. /*typedef bg::traits::point_type<Box>::type P;
  866. bg::model::ring<P> qring;
  867. bg::convert(qbox, qring);
  868. spatial_query(tree, bgi::overlaps(qring), expected_output);
  869. bg::model::polygon<P> qpoly;
  870. bg::convert(qbox, qpoly);
  871. spatial_query(tree, bgi::overlaps(qpoly), expected_output);*/
  872. }
  873. };
  874. template <>
  875. struct overlaps_impl<bg::point_tag>
  876. {
  877. template <typename Rtree, typename Value, typename Box>
  878. static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
  879. {}
  880. };
  881. template <>
  882. struct overlaps_impl<bg::segment_tag>
  883. {
  884. template <typename Rtree, typename Value, typename Box>
  885. static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
  886. {}
  887. };
  888. template <typename Rtree, typename Value, typename Box>
  889. void overlaps(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  890. {
  891. overlaps_impl<
  892. typename bg::tag<
  893. typename Rtree::indexable_type
  894. >::type
  895. >::apply(tree, input, qbox);
  896. }
  897. //template <typename Tag, size_t Dimension>
  898. //struct touches_impl
  899. //{
  900. // template <typename Rtree, typename Value, typename Box>
  901. // static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  902. // {}
  903. //};
  904. //
  905. //template <>
  906. //struct touches_impl<bg::box_tag, 2>
  907. //{
  908. // template <typename Rtree, typename Value, typename Box>
  909. // static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  910. // {
  911. // std::vector<Value> expected_output;
  912. //
  913. // BOOST_FOREACH(Value const& v, input)
  914. // if ( bg::touches(tree.translator()(v), qbox) )
  915. // expected_output.push_back(v);
  916. //
  917. // spatial_query(tree, bgi::touches(qbox), expected_output);
  918. // }
  919. //};
  920. //
  921. //template <typename Rtree, typename Value, typename Box>
  922. //void touches(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  923. //{
  924. // touches_impl<
  925. // bgi::traits::tag<typename Rtree::indexable_type>::type,
  926. // bgi::traits::dimension<typename Rtree::indexable_type>::value
  927. // >::apply(tree, input, qbox);
  928. //}
  929. template <typename Tag>
  930. struct within_impl
  931. {
  932. template <typename Rtree, typename Value, typename Box>
  933. static void apply(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  934. {
  935. std::vector<Value> expected_output;
  936. BOOST_FOREACH(Value const& v, input)
  937. if ( bg::within(tree.indexable_get()(v), qbox) )
  938. expected_output.push_back(v);
  939. spatial_query(tree, bgi::within(qbox), expected_output);
  940. /*typedef bg::traits::point_type<Box>::type P;
  941. bg::model::ring<P> qring;
  942. bg::convert(qbox, qring);
  943. spatial_query(tree, bgi::within(qring), expected_output);
  944. bg::model::polygon<P> qpoly;
  945. bg::convert(qbox, qpoly);
  946. spatial_query(tree, bgi::within(qpoly), expected_output);*/
  947. }
  948. };
  949. template <>
  950. struct within_impl<bg::segment_tag>
  951. {
  952. template <typename Rtree, typename Value, typename Box>
  953. static void apply(Rtree const& /*tree*/, std::vector<Value> const& /*input*/, Box const& /*qbox*/)
  954. {}
  955. };
  956. template <typename Rtree, typename Value, typename Box>
  957. void within(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  958. {
  959. within_impl<
  960. typename bg::tag<
  961. typename Rtree::indexable_type
  962. >::type
  963. >::apply(tree, input, qbox);
  964. }
  965. // rtree nearest queries
  966. template <typename Rtree, typename Point>
  967. struct NearestKLess
  968. {
  969. typedef typename bg::default_distance_result<Point, typename Rtree::indexable_type>::type D;
  970. template <typename Value>
  971. bool operator()(std::pair<D, Value> const& p1, std::pair<D, Value> const& p2) const
  972. {
  973. return p1.first < p2.first;
  974. }
  975. };
  976. template <typename Rtree, typename Point>
  977. struct NearestKTransform
  978. {
  979. typedef typename bg::default_distance_result<Point, typename Rtree::indexable_type>::type D;
  980. template <typename Value>
  981. Value const& operator()(std::pair<D, Value> const& p) const
  982. {
  983. return p.second;
  984. }
  985. };
  986. template <typename Rtree, typename Value, typename Point, typename Distance>
  987. inline void compare_nearest_outputs(Rtree const& rtree, std::vector<Value> const& output, std::vector<Value> const& expected_output, Point const& pt, Distance greatest_distance)
  988. {
  989. // check output
  990. bool are_sizes_ok = (expected_output.size() == output.size());
  991. BOOST_CHECK( are_sizes_ok );
  992. if ( are_sizes_ok )
  993. {
  994. BOOST_FOREACH(Value const& v, output)
  995. {
  996. // TODO - perform explicit check here?
  997. // should all objects which are closest be checked and should exactly the same be found?
  998. if ( find(rtree, expected_output.begin(), expected_output.end(), v) == expected_output.end() )
  999. {
  1000. Distance d = bg::comparable_distance(pt, rtree.indexable_get()(v));
  1001. BOOST_CHECK(d == greatest_distance);
  1002. }
  1003. }
  1004. }
  1005. }
  1006. template <typename Rtree, typename Value, typename Point>
  1007. inline void check_sorted_by_distance(Rtree const& rtree, std::vector<Value> const& output, Point const& pt)
  1008. {
  1009. typedef typename bg::default_distance_result<Point, typename Rtree::indexable_type>::type D;
  1010. D prev_dist = 0;
  1011. BOOST_FOREACH(Value const& v, output)
  1012. {
  1013. D d = bg::comparable_distance(pt, rtree.indexable_get()(v));
  1014. BOOST_CHECK(prev_dist <= d);
  1015. prev_dist = d;
  1016. }
  1017. }
  1018. template <typename Rtree, typename Value, typename Point>
  1019. inline void nearest_query_k(Rtree const& rtree, std::vector<Value> const& input, Point const& pt, unsigned int k)
  1020. {
  1021. // TODO: Nearest object may not be the same as found by the rtree if distances are equal
  1022. // All objects with the same closest distance should be picked
  1023. typedef typename bg::default_distance_result<Point, typename Rtree::indexable_type>::type D;
  1024. std::vector< std::pair<D, Value> > test_output;
  1025. // calculate test output - k closest values pairs
  1026. BOOST_FOREACH(Value const& v, input)
  1027. {
  1028. D d = bg::comparable_distance(pt, rtree.indexable_get()(v));
  1029. if ( test_output.size() < k )
  1030. test_output.push_back(std::make_pair(d, v));
  1031. else
  1032. {
  1033. std::sort(test_output.begin(), test_output.end(), NearestKLess<Rtree, Point>());
  1034. if ( d < test_output.back().first )
  1035. test_output.back() = std::make_pair(d, v);
  1036. }
  1037. }
  1038. // caluclate biggest distance
  1039. std::sort(test_output.begin(), test_output.end(), NearestKLess<Rtree, Point>());
  1040. D greatest_distance = 0;
  1041. if ( !test_output.empty() )
  1042. greatest_distance = test_output.back().first;
  1043. // transform test output to vector of values
  1044. std::vector<Value> expected_output(test_output.size(), generate::value_default<Value>::apply());
  1045. std::transform(test_output.begin(), test_output.end(), expected_output.begin(), NearestKTransform<Rtree, Point>());
  1046. // calculate output using rtree
  1047. std::vector<Value> output;
  1048. rtree.query(bgi::nearest(pt, k), std::back_inserter(output));
  1049. // check output
  1050. compare_nearest_outputs(rtree, output, expected_output, pt, greatest_distance);
  1051. exactly_the_same_outputs(rtree, output, rtree | bgi::adaptors::queried(bgi::nearest(pt, k)));
  1052. std::vector<Value> output2(k, generate::value_default<Value>::apply());
  1053. typename Rtree::size_type found_count = rtree.query(bgi::nearest(pt, k), output2.begin());
  1054. output2.resize(found_count, generate::value_default<Value>::apply());
  1055. exactly_the_same_outputs(rtree, output, output2);
  1056. std::vector<Value> output3;
  1057. std::copy(rtree.qbegin(bgi::nearest(pt, k)), rtree.qend(), std::back_inserter(output3));
  1058. compare_nearest_outputs(rtree, output3, expected_output, pt, greatest_distance);
  1059. check_sorted_by_distance(rtree, output3, pt);
  1060. check_fwd_iterators(rtree.qbegin(bgi::nearest(pt, k)), rtree.qend());
  1061. #ifdef BOOST_GEOMETRY_INDEX_DETAIL_EXPERIMENTAL
  1062. {
  1063. std::vector<Value> output4;
  1064. std::copy(rtree.qbegin_(bgi::nearest(pt, k)), rtree.qend_(bgi::nearest(pt, k)), std::back_inserter(output4));
  1065. exactly_the_same_outputs(rtree, output4, output3);
  1066. output4.clear();
  1067. copy_alt(rtree.qbegin_(bgi::nearest(pt, k)), rtree.qend_(), std::back_inserter(output4));
  1068. exactly_the_same_outputs(rtree, output4, output3);
  1069. check_fwd_iterators(rtree.qbegin_(bgi::nearest(pt, k)), rtree.qend_(bgi::nearest(pt, k)));
  1070. check_fwd_iterators(rtree.qbegin_(bgi::nearest(pt, k)), rtree.qend_());
  1071. }
  1072. #endif
  1073. }
  1074. // rtree nearest not found
  1075. struct AlwaysFalse
  1076. {
  1077. template <typename Value>
  1078. bool operator()(Value const& ) const { return false; }
  1079. };
  1080. template <typename Rtree, typename Point>
  1081. void nearest_query_not_found(Rtree const& rtree, Point const& pt)
  1082. {
  1083. typedef typename Rtree::value_type Value;
  1084. std::vector<Value> output_v;
  1085. size_t n_res = rtree.query(bgi::nearest(pt, 5) && bgi::satisfies(AlwaysFalse()), std::back_inserter(output_v));
  1086. BOOST_CHECK(output_v.size() == n_res);
  1087. BOOST_CHECK(n_res < 5);
  1088. }
  1089. template <typename Value>
  1090. bool satisfies_fun(Value const& ) { return true; }
  1091. struct satisfies_obj
  1092. {
  1093. template <typename Value>
  1094. bool operator()(Value const& ) const { return true; }
  1095. };
  1096. template <typename Rtree, typename Value>
  1097. void satisfies(Rtree const& rtree, std::vector<Value> const& input)
  1098. {
  1099. std::vector<Value> result;
  1100. rtree.query(bgi::satisfies(satisfies_obj()), std::back_inserter(result));
  1101. BOOST_CHECK(result.size() == input.size());
  1102. result.clear();
  1103. rtree.query(!bgi::satisfies(satisfies_obj()), std::back_inserter(result));
  1104. BOOST_CHECK(result.size() == 0);
  1105. result.clear();
  1106. rtree.query(bgi::satisfies(satisfies_fun<Value>), std::back_inserter(result));
  1107. BOOST_CHECK(result.size() == input.size());
  1108. result.clear();
  1109. rtree.query(!bgi::satisfies(satisfies_fun<Value>), std::back_inserter(result));
  1110. BOOST_CHECK(result.size() == 0);
  1111. #ifndef BOOST_NO_CXX11_LAMBDAS
  1112. result.clear();
  1113. rtree.query(bgi::satisfies([](Value const&){ return true; }), std::back_inserter(result));
  1114. BOOST_CHECK(result.size() == input.size());
  1115. result.clear();
  1116. rtree.query(!bgi::satisfies([](Value const&){ return true; }), std::back_inserter(result));
  1117. BOOST_CHECK(result.size() == 0);
  1118. #endif
  1119. }
  1120. // rtree copying and moving
  1121. template <typename Rtree, typename Box>
  1122. void copy_swap_move(Rtree const& tree, Box const& qbox)
  1123. {
  1124. typedef typename Rtree::value_type Value;
  1125. typedef typename Rtree::parameters_type Params;
  1126. size_t s = tree.size();
  1127. Params params = tree.parameters();
  1128. std::vector<Value> expected_output;
  1129. tree.query(bgi::intersects(qbox), std::back_inserter(expected_output));
  1130. // copy constructor
  1131. Rtree t1(tree);
  1132. BOOST_CHECK(tree.empty() == t1.empty());
  1133. BOOST_CHECK(tree.size() == t1.size());
  1134. BOOST_CHECK(t1.parameters().get_max_elements() == params.get_max_elements());
  1135. BOOST_CHECK(t1.parameters().get_min_elements() == params.get_min_elements());
  1136. std::vector<Value> output;
  1137. t1.query(bgi::intersects(qbox), std::back_inserter(output));
  1138. exactly_the_same_outputs(t1, output, expected_output);
  1139. // copying assignment operator
  1140. t1 = tree;
  1141. BOOST_CHECK(tree.empty() == t1.empty());
  1142. BOOST_CHECK(tree.size() == t1.size());
  1143. BOOST_CHECK(t1.parameters().get_max_elements() == params.get_max_elements());
  1144. BOOST_CHECK(t1.parameters().get_min_elements() == params.get_min_elements());
  1145. output.clear();
  1146. t1.query(bgi::intersects(qbox), std::back_inserter(output));
  1147. exactly_the_same_outputs(t1, output, expected_output);
  1148. Rtree t2(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
  1149. t2.swap(t1);
  1150. BOOST_CHECK(tree.empty() == t2.empty());
  1151. BOOST_CHECK(tree.size() == t2.size());
  1152. BOOST_CHECK(true == t1.empty());
  1153. BOOST_CHECK(0 == t1.size());
  1154. // those fails e.g. on darwin 4.2.1 because it can't copy base obejcts properly
  1155. BOOST_CHECK(t1.parameters().get_max_elements() == params.get_max_elements());
  1156. BOOST_CHECK(t1.parameters().get_min_elements() == params.get_min_elements());
  1157. BOOST_CHECK(t2.parameters().get_max_elements() == params.get_max_elements());
  1158. BOOST_CHECK(t2.parameters().get_min_elements() == params.get_min_elements());
  1159. output.clear();
  1160. t1.query(bgi::intersects(qbox), std::back_inserter(output));
  1161. BOOST_CHECK(output.empty());
  1162. output.clear();
  1163. t2.query(bgi::intersects(qbox), std::back_inserter(output));
  1164. exactly_the_same_outputs(t2, output, expected_output);
  1165. t2.swap(t1);
  1166. // those fails e.g. on darwin 4.2.1 because it can't copy base obejcts properly
  1167. BOOST_CHECK(t1.parameters().get_max_elements() == params.get_max_elements());
  1168. BOOST_CHECK(t1.parameters().get_min_elements() == params.get_min_elements());
  1169. BOOST_CHECK(t2.parameters().get_max_elements() == params.get_max_elements());
  1170. BOOST_CHECK(t2.parameters().get_min_elements() == params.get_min_elements());
  1171. // moving constructor
  1172. Rtree t3(boost::move(t1), tree.get_allocator());
  1173. BOOST_CHECK(t3.size() == s);
  1174. BOOST_CHECK(t1.size() == 0);
  1175. BOOST_CHECK(t3.parameters().get_max_elements() == params.get_max_elements());
  1176. BOOST_CHECK(t3.parameters().get_min_elements() == params.get_min_elements());
  1177. output.clear();
  1178. t3.query(bgi::intersects(qbox), std::back_inserter(output));
  1179. exactly_the_same_outputs(t3, output, expected_output);
  1180. // moving assignment operator
  1181. t1 = boost::move(t3);
  1182. BOOST_CHECK(t1.size() == s);
  1183. BOOST_CHECK(t3.size() == 0);
  1184. BOOST_CHECK(t1.parameters().get_max_elements() == params.get_max_elements());
  1185. BOOST_CHECK(t1.parameters().get_min_elements() == params.get_min_elements());
  1186. output.clear();
  1187. t1.query(bgi::intersects(qbox), std::back_inserter(output));
  1188. exactly_the_same_outputs(t1, output, expected_output);
  1189. //TODO - test SWAP
  1190. ::boost::ignore_unused(params);
  1191. }
  1192. template <typename I, typename O>
  1193. inline void my_copy(I first, I last, O out)
  1194. {
  1195. for ( ; first != last ; ++first, ++out )
  1196. *out = *first;
  1197. }
  1198. // rtree creation and insertion
  1199. template <typename Rtree, typename Value, typename Box>
  1200. void create_insert(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  1201. {
  1202. std::vector<Value> expected_output;
  1203. tree.query(bgi::intersects(qbox), std::back_inserter(expected_output));
  1204. {
  1205. Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
  1206. BOOST_FOREACH(Value const& v, input)
  1207. t.insert(v);
  1208. BOOST_CHECK(tree.size() == t.size());
  1209. std::vector<Value> output;
  1210. t.query(bgi::intersects(qbox), std::back_inserter(output));
  1211. exactly_the_same_outputs(t, output, expected_output);
  1212. }
  1213. {
  1214. Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
  1215. //std::copy(input.begin(), input.end(), bgi::inserter(t));
  1216. my_copy(input.begin(), input.end(), bgi::inserter(t)); // to suppress MSVC warnings
  1217. BOOST_CHECK(tree.size() == t.size());
  1218. std::vector<Value> output;
  1219. t.query(bgi::intersects(qbox), std::back_inserter(output));
  1220. exactly_the_same_outputs(t, output, expected_output);
  1221. }
  1222. {
  1223. Rtree t(input.begin(), input.end(), tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
  1224. BOOST_CHECK(tree.size() == t.size());
  1225. std::vector<Value> output;
  1226. t.query(bgi::intersects(qbox), std::back_inserter(output));
  1227. compare_outputs(t, output, expected_output);
  1228. }
  1229. {
  1230. Rtree t(input, tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
  1231. BOOST_CHECK(tree.size() == t.size());
  1232. std::vector<Value> output;
  1233. t.query(bgi::intersects(qbox), std::back_inserter(output));
  1234. compare_outputs(t, output, expected_output);
  1235. }
  1236. {
  1237. Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
  1238. t.insert(input.begin(), input.end());
  1239. BOOST_CHECK(tree.size() == t.size());
  1240. std::vector<Value> output;
  1241. t.query(bgi::intersects(qbox), std::back_inserter(output));
  1242. exactly_the_same_outputs(t, output, expected_output);
  1243. }
  1244. {
  1245. Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
  1246. t.insert(input);
  1247. BOOST_CHECK(tree.size() == t.size());
  1248. std::vector<Value> output;
  1249. t.query(bgi::intersects(qbox), std::back_inserter(output));
  1250. exactly_the_same_outputs(t, output, expected_output);
  1251. }
  1252. {
  1253. Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
  1254. BOOST_FOREACH(Value const& v, input)
  1255. bgi::insert(t, v);
  1256. BOOST_CHECK(tree.size() == t.size());
  1257. std::vector<Value> output;
  1258. bgi::query(t, bgi::intersects(qbox), std::back_inserter(output));
  1259. exactly_the_same_outputs(t, output, expected_output);
  1260. }
  1261. {
  1262. Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
  1263. bgi::insert(t, input.begin(), input.end());
  1264. BOOST_CHECK(tree.size() == t.size());
  1265. std::vector<Value> output;
  1266. bgi::query(t, bgi::intersects(qbox), std::back_inserter(output));
  1267. exactly_the_same_outputs(t, output, expected_output);
  1268. }
  1269. {
  1270. Rtree t(tree.parameters(), tree.indexable_get(), tree.value_eq(), tree.get_allocator());
  1271. bgi::insert(t, input);
  1272. BOOST_CHECK(tree.size() == t.size());
  1273. std::vector<Value> output;
  1274. bgi::query(t, bgi::intersects(qbox), std::back_inserter(output));
  1275. exactly_the_same_outputs(t, output, expected_output);
  1276. }
  1277. }
  1278. // rtree removing
  1279. template <typename Rtree, typename Box>
  1280. void remove(Rtree const& tree, Box const& qbox)
  1281. {
  1282. typedef typename Rtree::value_type Value;
  1283. std::vector<Value> values_to_remove;
  1284. tree.query(bgi::intersects(qbox), std::back_inserter(values_to_remove));
  1285. BOOST_CHECK(0 < values_to_remove.size());
  1286. std::vector<Value> expected_output;
  1287. tree.query(bgi::disjoint(qbox), std::back_inserter(expected_output));
  1288. size_t expected_removed_count = values_to_remove.size();
  1289. size_t expected_size_after_remove = tree.size() - values_to_remove.size();
  1290. // Add value which is not stored in the Rtree
  1291. Value outsider = generate::value_outside<Rtree>();
  1292. values_to_remove.push_back(outsider);
  1293. {
  1294. Rtree t(tree);
  1295. size_t r = 0;
  1296. BOOST_FOREACH(Value const& v, values_to_remove)
  1297. r += t.remove(v);
  1298. BOOST_CHECK( r == expected_removed_count );
  1299. std::vector<Value> output;
  1300. t.query(bgi::disjoint(qbox), std::back_inserter(output));
  1301. BOOST_CHECK( t.size() == expected_size_after_remove );
  1302. BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
  1303. compare_outputs(t, output, expected_output);
  1304. }
  1305. {
  1306. Rtree t(tree);
  1307. size_t r = t.remove(values_to_remove.begin(), values_to_remove.end());
  1308. BOOST_CHECK( r == expected_removed_count );
  1309. std::vector<Value> output;
  1310. t.query(bgi::disjoint(qbox), std::back_inserter(output));
  1311. BOOST_CHECK( t.size() == expected_size_after_remove );
  1312. BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
  1313. compare_outputs(t, output, expected_output);
  1314. }
  1315. {
  1316. Rtree t(tree);
  1317. size_t r = t.remove(values_to_remove);
  1318. BOOST_CHECK( r == expected_removed_count );
  1319. std::vector<Value> output;
  1320. t.query(bgi::disjoint(qbox), std::back_inserter(output));
  1321. BOOST_CHECK( t.size() == expected_size_after_remove );
  1322. BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
  1323. compare_outputs(t, output, expected_output);
  1324. }
  1325. {
  1326. Rtree t(tree);
  1327. size_t r = 0;
  1328. BOOST_FOREACH(Value const& v, values_to_remove)
  1329. r += bgi::remove(t, v);
  1330. BOOST_CHECK( r == expected_removed_count );
  1331. std::vector<Value> output;
  1332. bgi::query(t, bgi::disjoint(qbox), std::back_inserter(output));
  1333. BOOST_CHECK( t.size() == expected_size_after_remove );
  1334. BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
  1335. compare_outputs(t, output, expected_output);
  1336. }
  1337. {
  1338. Rtree t(tree);
  1339. size_t r = bgi::remove(t, values_to_remove.begin(), values_to_remove.end());
  1340. BOOST_CHECK( r == expected_removed_count );
  1341. std::vector<Value> output;
  1342. bgi::query(t, bgi::disjoint(qbox), std::back_inserter(output));
  1343. BOOST_CHECK( t.size() == expected_size_after_remove );
  1344. BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
  1345. compare_outputs(t, output, expected_output);
  1346. }
  1347. {
  1348. Rtree t(tree);
  1349. size_t r = bgi::remove(t, values_to_remove);
  1350. BOOST_CHECK( r == expected_removed_count );
  1351. std::vector<Value> output;
  1352. bgi::query(t, bgi::disjoint(qbox), std::back_inserter(output));
  1353. BOOST_CHECK( t.size() == expected_size_after_remove );
  1354. BOOST_CHECK( output.size() == tree.size() - expected_removed_count );
  1355. compare_outputs(t, output, expected_output);
  1356. }
  1357. }
  1358. template <typename Rtree, typename Value, typename Box>
  1359. void clear(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  1360. {
  1361. std::vector<Value> values_to_remove;
  1362. tree.query(bgi::intersects(qbox), std::back_inserter(values_to_remove));
  1363. BOOST_CHECK(0 < values_to_remove.size());
  1364. //clear
  1365. {
  1366. Rtree t(tree);
  1367. std::vector<Value> expected_output;
  1368. t.query(bgi::intersects(qbox), std::back_inserter(expected_output));
  1369. size_t s = t.size();
  1370. t.clear();
  1371. BOOST_CHECK(t.empty());
  1372. BOOST_CHECK(t.size() == 0);
  1373. t.insert(input);
  1374. BOOST_CHECK(t.size() == s);
  1375. std::vector<Value> output;
  1376. t.query(bgi::intersects(qbox), std::back_inserter(output));
  1377. exactly_the_same_outputs(t, output, expected_output);
  1378. }
  1379. }
  1380. template <typename Rtree, typename Value>
  1381. void range(Rtree & tree, std::vector<Value> const& input)
  1382. {
  1383. check_fwd_iterators(tree.begin(), tree.end());
  1384. size_t count = std::distance(tree.begin(), tree.end());
  1385. BOOST_CHECK(count == tree.size());
  1386. BOOST_CHECK(count == input.size());
  1387. count = std::distance(boost::begin(tree), boost::end(tree));
  1388. BOOST_CHECK(count == tree.size());
  1389. count = boost::size(tree);
  1390. BOOST_CHECK(count == tree.size());
  1391. count = 0;
  1392. BOOST_FOREACH(Value const& v, tree)
  1393. {
  1394. boost::ignore_unused(v);
  1395. ++count;
  1396. }
  1397. BOOST_CHECK(count == tree.size());
  1398. }
  1399. // rtree queries
  1400. template <typename Rtree, typename Value, typename Box>
  1401. void queries(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  1402. {
  1403. basictest::intersects(tree, input, qbox);
  1404. basictest::disjoint(tree, input, qbox);
  1405. basictest::covered_by(tree, input, qbox);
  1406. basictest::overlaps(tree, input, qbox);
  1407. //basictest::touches(tree, input, qbox);
  1408. basictest::within(tree, input, qbox);
  1409. basictest::contains(tree, input, qbox);
  1410. basictest::covers(tree, input, qbox);
  1411. typedef typename bg::point_type<Box>::type P;
  1412. P pt;
  1413. bg::centroid(qbox, pt);
  1414. basictest::nearest_query_k(tree, input, pt, 10);
  1415. basictest::nearest_query_not_found(tree, generate::outside_point<P>::apply());
  1416. basictest::satisfies(tree, input);
  1417. }
  1418. // rtree creation and modification
  1419. template <typename Rtree, typename Value, typename Box>
  1420. void modifiers(Rtree const& tree, std::vector<Value> const& input, Box const& qbox)
  1421. {
  1422. basictest::copy_swap_move(tree, qbox);
  1423. basictest::create_insert(tree, input, qbox);
  1424. basictest::remove(tree, qbox);
  1425. basictest::clear(tree, input, qbox);
  1426. }
  1427. } // namespace basictest
  1428. template <typename Value, typename Parameters, typename Allocator>
  1429. void test_rtree_queries(Parameters const& parameters, Allocator const& allocator)
  1430. {
  1431. typedef bgi::indexable<Value> I;
  1432. typedef bgi::equal_to<Value> E;
  1433. typedef typename Allocator::template rebind<Value>::other A;
  1434. typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
  1435. typedef typename Tree::bounds_type B;
  1436. Tree tree(parameters, I(), E(), allocator);
  1437. std::vector<Value> input;
  1438. B qbox;
  1439. generate::rtree(tree, input, qbox);
  1440. basictest::queries(tree, input, qbox);
  1441. Tree empty_tree(parameters, I(), E(), allocator);
  1442. std::vector<Value> empty_input;
  1443. basictest::queries(empty_tree, empty_input, qbox);
  1444. }
  1445. template <typename Value, typename Parameters, typename Allocator>
  1446. void test_rtree_modifiers(Parameters const& parameters, Allocator const& allocator)
  1447. {
  1448. typedef bgi::indexable<Value> I;
  1449. typedef bgi::equal_to<Value> E;
  1450. typedef typename Allocator::template rebind<Value>::other A;
  1451. typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
  1452. typedef typename Tree::bounds_type B;
  1453. Tree tree(parameters, I(), E(), allocator);
  1454. std::vector<Value> input;
  1455. B qbox;
  1456. generate::rtree(tree, input, qbox);
  1457. basictest::modifiers(tree, input, qbox);
  1458. Tree empty_tree(parameters, I(), E(), allocator);
  1459. std::vector<Value> empty_input;
  1460. basictest::copy_swap_move(empty_tree, qbox);
  1461. }
  1462. // run all tests for a single Algorithm and single rtree
  1463. // defined by Value
  1464. template <typename Value, typename Parameters, typename Allocator>
  1465. void test_rtree_by_value(Parameters const& parameters, Allocator const& allocator)
  1466. {
  1467. test_rtree_queries<Value>(parameters, allocator);
  1468. test_rtree_modifiers<Value>(parameters, allocator);
  1469. }
  1470. // rtree inserting and removing of counting_value
  1471. template <typename Indexable, typename Parameters, typename Allocator>
  1472. void test_count_rtree_values(Parameters const& parameters, Allocator const& allocator)
  1473. {
  1474. typedef counting_value<Indexable> Value;
  1475. typedef bgi::indexable<Value> I;
  1476. typedef bgi::equal_to<Value> E;
  1477. typedef typename Allocator::template rebind<Value>::other A;
  1478. typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
  1479. typedef typename Tree::bounds_type B;
  1480. Tree t(parameters, I(), E(), allocator);
  1481. std::vector<Value> input;
  1482. B qbox;
  1483. generate::rtree(t, input, qbox);
  1484. size_t rest_count = input.size();
  1485. BOOST_CHECK(t.size() + rest_count == Value::counter());
  1486. std::vector<Value> values_to_remove;
  1487. t.query(bgi::intersects(qbox), std::back_inserter(values_to_remove));
  1488. rest_count += values_to_remove.size();
  1489. BOOST_CHECK(t.size() + rest_count == Value::counter());
  1490. size_t values_count = Value::counter();
  1491. BOOST_FOREACH(Value const& v, values_to_remove)
  1492. {
  1493. size_t r = t.remove(v);
  1494. --values_count;
  1495. BOOST_CHECK(1 == r);
  1496. BOOST_CHECK(Value::counter() == values_count);
  1497. BOOST_CHECK(t.size() + rest_count == values_count);
  1498. }
  1499. }
  1500. // rtree count
  1501. template <typename Indexable, typename Parameters, typename Allocator>
  1502. void test_rtree_count(Parameters const& parameters, Allocator const& allocator)
  1503. {
  1504. typedef std::pair<Indexable, int> Value;
  1505. typedef bgi::indexable<Value> I;
  1506. typedef bgi::equal_to<Value> E;
  1507. typedef typename Allocator::template rebind<Value>::other A;
  1508. typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
  1509. typedef typename Tree::bounds_type B;
  1510. Tree t(parameters, I(), E(), allocator);
  1511. std::vector<Value> input;
  1512. B qbox;
  1513. generate::rtree(t, input, qbox);
  1514. BOOST_CHECK(t.count(input[0]) == 1);
  1515. BOOST_CHECK(t.count(input[0].first) == 1);
  1516. t.insert(input[0]);
  1517. BOOST_CHECK(t.count(input[0]) == 2);
  1518. BOOST_CHECK(t.count(input[0].first) == 2);
  1519. t.insert(std::make_pair(input[0].first, -1));
  1520. BOOST_CHECK(t.count(input[0]) == 2);
  1521. BOOST_CHECK(t.count(input[0].first) == 3);
  1522. }
  1523. // test rtree box
  1524. template <typename Value, typename Parameters, typename Allocator>
  1525. void test_rtree_bounds(Parameters const& parameters, Allocator const& allocator)
  1526. {
  1527. typedef bgi::indexable<Value> I;
  1528. typedef bgi::equal_to<Value> E;
  1529. typedef typename Allocator::template rebind<Value>::other A;
  1530. typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
  1531. typedef typename Tree::bounds_type B;
  1532. //typedef typename bg::traits::point_type<B>::type P;
  1533. Tree t(parameters, I(), E(), allocator);
  1534. std::vector<Value> input;
  1535. B qbox;
  1536. B b;
  1537. bg::assign_inverse(b);
  1538. BOOST_CHECK(bg::equals(t.bounds(), b));
  1539. generate::rtree(t, input, qbox);
  1540. b = bgi::detail::rtree::values_box<B>(input.begin(), input.end(), t.indexable_get(),
  1541. bgi::detail::get_strategy(parameters));
  1542. BOOST_CHECK(bg::equals(t.bounds(), b));
  1543. BOOST_CHECK(bg::equals(t.bounds(), bgi::bounds(t)));
  1544. size_t s = input.size();
  1545. while ( s/2 < input.size() && !input.empty() )
  1546. {
  1547. t.remove(input.back());
  1548. input.pop_back();
  1549. }
  1550. b = bgi::detail::rtree::values_box<B>(input.begin(), input.end(), t.indexable_get(),
  1551. bgi::detail::get_strategy(parameters));
  1552. BOOST_CHECK(bg::equals(t.bounds(), b));
  1553. Tree t2(t);
  1554. BOOST_CHECK(bg::equals(t2.bounds(), b));
  1555. t2.clear();
  1556. t2 = t;
  1557. BOOST_CHECK(bg::equals(t2.bounds(), b));
  1558. t2.clear();
  1559. t2 = boost::move(t);
  1560. BOOST_CHECK(bg::equals(t2.bounds(), b));
  1561. t.clear();
  1562. bg::assign_inverse(b);
  1563. BOOST_CHECK(bg::equals(t.bounds(), b));
  1564. }
  1565. // test rtree iterator
  1566. template <typename Indexable, typename Parameters, typename Allocator>
  1567. void test_rtree_range(Parameters const& parameters, Allocator const& allocator)
  1568. {
  1569. typedef std::pair<Indexable, int> Value;
  1570. typedef bgi::indexable<Value> I;
  1571. typedef bgi::equal_to<Value> E;
  1572. typedef typename Allocator::template rebind<Value>::other A;
  1573. typedef bgi::rtree<Value, Parameters, I, E, A> Tree;
  1574. typedef typename Tree::bounds_type B;
  1575. Tree t(parameters, I(), E(), allocator);
  1576. std::vector<Value> input;
  1577. B qbox;
  1578. generate::rtree(t, input, qbox);
  1579. basictest::range(t, input);
  1580. basictest::range((Tree const&)t, input);
  1581. }
  1582. template <typename Indexable, typename Parameters, typename Allocator>
  1583. void test_rtree_additional(Parameters const& parameters, Allocator const& allocator)
  1584. {
  1585. test_count_rtree_values<Indexable>(parameters, allocator);
  1586. test_rtree_count<Indexable>(parameters, allocator);
  1587. test_rtree_bounds<Indexable>(parameters, allocator);
  1588. test_rtree_range<Indexable>(parameters, allocator);
  1589. }
  1590. // run all tests for one Algorithm for some number of rtrees
  1591. // defined by some number of Values constructed from given Point
  1592. template<typename Point, typename Parameters, typename Allocator>
  1593. void test_rtree_for_point(Parameters const& parameters, Allocator const& allocator)
  1594. {
  1595. typedef std::pair<Point, int> PairP;
  1596. typedef boost::tuple<Point, int, int> TupleP;
  1597. typedef boost::shared_ptr< test_object<Point> > SharedPtrP;
  1598. typedef value_no_dctor<Point> VNoDCtor;
  1599. test_rtree_by_value<Point, Parameters>(parameters, allocator);
  1600. test_rtree_by_value<PairP, Parameters>(parameters, allocator);
  1601. test_rtree_by_value<TupleP, Parameters>(parameters, allocator);
  1602. test_rtree_by_value<SharedPtrP, Parameters>(parameters, allocator);
  1603. test_rtree_by_value<VNoDCtor, Parameters>(parameters, allocator);
  1604. test_rtree_additional<Point>(parameters, allocator);
  1605. #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1606. typedef std::tuple<Point, int, int> StdTupleP;
  1607. test_rtree_by_value<StdTupleP, Parameters>(parameters, allocator);
  1608. #endif
  1609. }
  1610. template<typename Point, typename Parameters, typename Allocator>
  1611. void test_rtree_for_box(Parameters const& parameters, Allocator const& allocator)
  1612. {
  1613. typedef bg::model::box<Point> Box;
  1614. typedef std::pair<Box, int> PairB;
  1615. typedef boost::tuple<Box, int, int> TupleB;
  1616. typedef value_no_dctor<Box> VNoDCtor;
  1617. test_rtree_by_value<Box, Parameters>(parameters, allocator);
  1618. test_rtree_by_value<PairB, Parameters>(parameters, allocator);
  1619. test_rtree_by_value<TupleB, Parameters>(parameters, allocator);
  1620. test_rtree_by_value<VNoDCtor, Parameters>(parameters, allocator);
  1621. test_rtree_additional<Box>(parameters, allocator);
  1622. #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1623. typedef std::tuple<Box, int, int> StdTupleB;
  1624. test_rtree_by_value<StdTupleB, Parameters>(parameters, allocator);
  1625. #endif
  1626. }
  1627. template<typename Point, typename Parameters>
  1628. void test_rtree_for_point(Parameters const& parameters)
  1629. {
  1630. test_rtree_for_point<Point>(parameters, std::allocator<int>());
  1631. }
  1632. template<typename Point, typename Parameters>
  1633. void test_rtree_for_box(Parameters const& parameters)
  1634. {
  1635. test_rtree_for_box<Point>(parameters, std::allocator<int>());
  1636. }
  1637. namespace testset {
  1638. template<typename Indexable, typename Parameters, typename Allocator>
  1639. void modifiers(Parameters const& parameters, Allocator const& allocator)
  1640. {
  1641. typedef std::pair<Indexable, int> Pair;
  1642. typedef boost::tuple<Indexable, int, int> Tuple;
  1643. typedef boost::shared_ptr< test_object<Indexable> > SharedPtr;
  1644. typedef value_no_dctor<Indexable> VNoDCtor;
  1645. test_rtree_modifiers<Indexable>(parameters, allocator);
  1646. test_rtree_modifiers<Pair>(parameters, allocator);
  1647. test_rtree_modifiers<Tuple>(parameters, allocator);
  1648. test_rtree_modifiers<SharedPtr>(parameters, allocator);
  1649. test_rtree_modifiers<VNoDCtor>(parameters, allocator);
  1650. #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1651. typedef std::tuple<Indexable, int, int> StdTuple;
  1652. test_rtree_modifiers<StdTuple>(parameters, allocator);
  1653. #endif
  1654. }
  1655. template<typename Indexable, typename Parameters, typename Allocator>
  1656. void queries(Parameters const& parameters, Allocator const& allocator)
  1657. {
  1658. typedef std::pair<Indexable, int> Pair;
  1659. typedef boost::tuple<Indexable, int, int> Tuple;
  1660. typedef boost::shared_ptr< test_object<Indexable> > SharedPtr;
  1661. typedef value_no_dctor<Indexable> VNoDCtor;
  1662. test_rtree_queries<Indexable>(parameters, allocator);
  1663. test_rtree_queries<Pair>(parameters, allocator);
  1664. test_rtree_queries<Tuple>(parameters, allocator);
  1665. test_rtree_queries<SharedPtr>(parameters, allocator);
  1666. test_rtree_queries<VNoDCtor>(parameters, allocator);
  1667. #if !defined(BOOST_NO_CXX11_HDR_TUPLE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  1668. typedef std::tuple<Indexable, int, int> StdTuple;
  1669. test_rtree_queries<StdTuple>(parameters, allocator);
  1670. #endif
  1671. }
  1672. template<typename Indexable, typename Parameters, typename Allocator>
  1673. void additional(Parameters const& parameters, Allocator const& allocator)
  1674. {
  1675. test_rtree_additional<Indexable, Parameters>(parameters, allocator);
  1676. }
  1677. } // namespace testset
  1678. #endif // BOOST_GEOMETRY_INDEX_TEST_RTREE_HPP