predicates.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. // Boost.Geometry Index
  2. //
  3. // Spatial query predicates definition and checks.
  4. //
  5. // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
  6. //
  7. // This file was modified by Oracle on 2019.
  8. // Modifications copyright (c) 2019 Oracle and/or its affiliates.
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. //
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_PREDICATES_HPP
  15. #define BOOST_GEOMETRY_INDEX_DETAIL_PREDICATES_HPP
  16. //#include <utility>
  17. #include <boost/mpl/assert.hpp>
  18. #include <boost/tuple/tuple.hpp>
  19. #include <boost/geometry/index/detail/tags.hpp>
  20. namespace boost { namespace geometry { namespace index { namespace detail {
  21. namespace predicates {
  22. // ------------------------------------------------------------------ //
  23. // predicates
  24. // ------------------------------------------------------------------ //
  25. template <typename Fun, bool IsFunction>
  26. struct satisfies_impl
  27. {
  28. satisfies_impl() : fun(NULL) {}
  29. satisfies_impl(Fun f) : fun(f) {}
  30. Fun * fun;
  31. };
  32. template <typename Fun>
  33. struct satisfies_impl<Fun, false>
  34. {
  35. satisfies_impl() {}
  36. satisfies_impl(Fun const& f) : fun(f) {}
  37. Fun fun;
  38. };
  39. template <typename Fun, bool Negated>
  40. struct satisfies
  41. : satisfies_impl<Fun, ::boost::is_function<Fun>::value>
  42. {
  43. typedef satisfies_impl<Fun, ::boost::is_function<Fun>::value> base;
  44. satisfies() {}
  45. satisfies(Fun const& f) : base(f) {}
  46. satisfies(base const& b) : base(b) {}
  47. };
  48. // ------------------------------------------------------------------ //
  49. struct contains_tag {};
  50. struct covered_by_tag {};
  51. struct covers_tag {};
  52. struct disjoint_tag {};
  53. struct intersects_tag {};
  54. struct overlaps_tag {};
  55. struct touches_tag {};
  56. struct within_tag {};
  57. template <typename Geometry, typename Tag, bool Negated>
  58. struct spatial_predicate
  59. {
  60. spatial_predicate() {}
  61. spatial_predicate(Geometry const& g) : geometry(g) {}
  62. Geometry geometry;
  63. };
  64. // ------------------------------------------------------------------ //
  65. // CONSIDER: separated nearest<> and path<> may be replaced by
  66. // nearest_predicate<Geometry, Tag>
  67. // where Tag = point_tag | path_tag
  68. // IMPROVEMENT: user-defined nearest predicate allowing to define
  69. // all or only geometrical aspects of the search
  70. template <typename PointOrRelation>
  71. struct nearest
  72. {
  73. nearest()
  74. // : count(0)
  75. {}
  76. nearest(PointOrRelation const& por, unsigned k)
  77. : point_or_relation(por)
  78. , count(k)
  79. {}
  80. PointOrRelation point_or_relation;
  81. unsigned count;
  82. };
  83. template <typename SegmentOrLinestring>
  84. struct path
  85. {
  86. path()
  87. // : count(0)
  88. {}
  89. path(SegmentOrLinestring const& g, unsigned k)
  90. : geometry(g)
  91. , count(k)
  92. {}
  93. SegmentOrLinestring geometry;
  94. unsigned count;
  95. };
  96. } // namespace predicates
  97. // ------------------------------------------------------------------ //
  98. // predicate_check
  99. // ------------------------------------------------------------------ //
  100. template <typename Predicate, typename Tag>
  101. struct predicate_check
  102. {
  103. BOOST_MPL_ASSERT_MSG(
  104. (false),
  105. NOT_IMPLEMENTED_FOR_THIS_PREDICATE_OR_TAG,
  106. (predicate_check));
  107. };
  108. // ------------------------------------------------------------------ //
  109. template <typename Fun>
  110. struct predicate_check<predicates::satisfies<Fun, false>, value_tag>
  111. {
  112. template <typename Value, typename Indexable, typename Strategy>
  113. static inline bool apply(predicates::satisfies<Fun, false> const& p, Value const& v, Indexable const& , Strategy const&)
  114. {
  115. return p.fun(v);
  116. }
  117. };
  118. template <typename Fun>
  119. struct predicate_check<predicates::satisfies<Fun, true>, value_tag>
  120. {
  121. template <typename Value, typename Indexable, typename Strategy>
  122. static inline bool apply(predicates::satisfies<Fun, true> const& p, Value const& v, Indexable const& , Strategy const&)
  123. {
  124. return !p.fun(v);
  125. }
  126. };
  127. // ------------------------------------------------------------------ //
  128. template <typename Tag>
  129. struct spatial_predicate_call
  130. {
  131. BOOST_MPL_ASSERT_MSG(false, NOT_IMPLEMENTED_FOR_THIS_TAG, (Tag));
  132. };
  133. template <>
  134. struct spatial_predicate_call<predicates::contains_tag>
  135. {
  136. template <typename G1, typename G2, typename S>
  137. static inline bool apply(G1 const& g1, G2 const& g2, S const&)
  138. {
  139. return geometry::within(g2, g1);
  140. }
  141. };
  142. template <>
  143. struct spatial_predicate_call<predicates::covered_by_tag>
  144. {
  145. template <typename G1, typename G2, typename S>
  146. static inline bool apply(G1 const& g1, G2 const& g2, S const&)
  147. {
  148. return geometry::covered_by(g1, g2);
  149. }
  150. };
  151. template <>
  152. struct spatial_predicate_call<predicates::covers_tag>
  153. {
  154. template <typename G1, typename G2, typename S>
  155. static inline bool apply(G1 const& g1, G2 const& g2, S const&)
  156. {
  157. return geometry::covered_by(g2, g1);
  158. }
  159. };
  160. template <>
  161. struct spatial_predicate_call<predicates::disjoint_tag>
  162. {
  163. template <typename G1, typename G2, typename S>
  164. static inline bool apply(G1 const& g1, G2 const& g2, S const&)
  165. {
  166. return geometry::disjoint(g1, g2);
  167. }
  168. };
  169. // TEMP: used to implement CS-specific intersects predicate for certain
  170. // combinations of geometries until umbrella strategies are implemented
  171. template
  172. <
  173. typename G1, typename G2,
  174. typename Tag1 = typename tag<G1>::type,
  175. typename Tag2 = typename tag<G2>::type
  176. >
  177. struct spatial_predicate_intersects
  178. {
  179. template <typename S>
  180. static inline bool apply(G1 const& g1, G2 const& g2, S const&)
  181. {
  182. return geometry::intersects(g1, g2);
  183. }
  184. };
  185. // TEMP: used in within and relate
  186. template <typename G1, typename G2>
  187. struct spatial_predicate_intersects<G1, G2, box_tag, point_tag>
  188. {
  189. static inline bool apply(G1 const& g1, G2 const& g2, default_strategy const&)
  190. {
  191. return geometry::intersects(g1, g2);
  192. }
  193. template <typename S>
  194. static inline bool apply(G1 const& g1, G2 const& g2, S const& )
  195. {
  196. return geometry::intersects(g1, g2, typename S::covered_by_point_box_strategy_type());
  197. }
  198. };
  199. template <>
  200. struct spatial_predicate_call<predicates::intersects_tag>
  201. {
  202. template <typename G1, typename G2, typename S>
  203. static inline bool apply(G1 const& g1, G2 const& g2, S const& s)
  204. {
  205. return spatial_predicate_intersects<G1, G2>::apply(g1, g2, s);
  206. }
  207. };
  208. template <>
  209. struct spatial_predicate_call<predicates::overlaps_tag>
  210. {
  211. template <typename G1, typename G2, typename S>
  212. static inline bool apply(G1 const& g1, G2 const& g2, S const&)
  213. {
  214. return geometry::overlaps(g1, g2);
  215. }
  216. };
  217. template <>
  218. struct spatial_predicate_call<predicates::touches_tag>
  219. {
  220. template <typename G1, typename G2, typename S>
  221. static inline bool apply(G1 const& g1, G2 const& g2, S const&)
  222. {
  223. return geometry::touches(g1, g2);
  224. }
  225. };
  226. template <>
  227. struct spatial_predicate_call<predicates::within_tag>
  228. {
  229. template <typename G1, typename G2, typename S>
  230. static inline bool apply(G1 const& g1, G2 const& g2, S const&)
  231. {
  232. return geometry::within(g1, g2);
  233. }
  234. };
  235. // ------------------------------------------------------------------ //
  236. // spatial predicate
  237. template <typename Geometry, typename Tag>
  238. struct predicate_check<predicates::spatial_predicate<Geometry, Tag, false>, value_tag>
  239. {
  240. typedef predicates::spatial_predicate<Geometry, Tag, false> Pred;
  241. template <typename Value, typename Indexable, typename Strategy>
  242. static inline bool apply(Pred const& p, Value const&, Indexable const& i, Strategy const& s)
  243. {
  244. return spatial_predicate_call<Tag>::apply(i, p.geometry, s);
  245. }
  246. };
  247. // negated spatial predicate
  248. template <typename Geometry, typename Tag>
  249. struct predicate_check<predicates::spatial_predicate<Geometry, Tag, true>, value_tag>
  250. {
  251. typedef predicates::spatial_predicate<Geometry, Tag, true> Pred;
  252. template <typename Value, typename Indexable, typename Strategy>
  253. static inline bool apply(Pred const& p, Value const&, Indexable const& i, Strategy const& s)
  254. {
  255. return !spatial_predicate_call<Tag>::apply(i, p.geometry, s);
  256. }
  257. };
  258. // ------------------------------------------------------------------ //
  259. template <typename DistancePredicates>
  260. struct predicate_check<predicates::nearest<DistancePredicates>, value_tag>
  261. {
  262. template <typename Value, typename Box, typename Strategy>
  263. static inline bool apply(predicates::nearest<DistancePredicates> const&, Value const&, Box const&, Strategy const&)
  264. {
  265. return true;
  266. }
  267. };
  268. template <typename Linestring>
  269. struct predicate_check<predicates::path<Linestring>, value_tag>
  270. {
  271. template <typename Value, typename Box, typename Strategy>
  272. static inline bool apply(predicates::path<Linestring> const&, Value const&, Box const&, Strategy const&)
  273. {
  274. return true;
  275. }
  276. };
  277. // ------------------------------------------------------------------ //
  278. // predicates_check for bounds
  279. // ------------------------------------------------------------------ //
  280. template <typename Fun, bool Negated>
  281. struct predicate_check<predicates::satisfies<Fun, Negated>, bounds_tag>
  282. {
  283. template <typename Value, typename Box, typename Strategy>
  284. static bool apply(predicates::satisfies<Fun, Negated> const&, Value const&, Box const&, Strategy const&)
  285. {
  286. return true;
  287. }
  288. };
  289. // ------------------------------------------------------------------ //
  290. // NOT NEGATED
  291. // value_tag bounds_tag
  292. // ---------------------------
  293. // contains(I,G) covers(I,G)
  294. // covered_by(I,G) intersects(I,G)
  295. // covers(I,G) covers(I,G)
  296. // disjoint(I,G) !covered_by(I,G)
  297. // intersects(I,G) intersects(I,G)
  298. // overlaps(I,G) intersects(I,G) - possibly change to the version without border case, e.g. intersects_without_border(0,0x1,1, 1,1x2,2) should give false
  299. // touches(I,G) intersects(I,G)
  300. // within(I,G) intersects(I,G) - possibly change to the version without border case, e.g. intersects_without_border(0,0x1,1, 1,1x2,2) should give false
  301. // spatial predicate - default
  302. template <typename Geometry, typename Tag>
  303. struct predicate_check<predicates::spatial_predicate<Geometry, Tag, false>, bounds_tag>
  304. {
  305. typedef predicates::spatial_predicate<Geometry, Tag, false> Pred;
  306. template <typename Value, typename Indexable, typename Strategy>
  307. static inline bool apply(Pred const& p, Value const&, Indexable const& i, Strategy const& s)
  308. {
  309. return spatial_predicate_call<predicates::intersects_tag>::apply(i, p.geometry, s);
  310. }
  311. };
  312. // spatial predicate - contains
  313. template <typename Geometry>
  314. struct predicate_check<predicates::spatial_predicate<Geometry, predicates::contains_tag, false>, bounds_tag>
  315. {
  316. typedef predicates::spatial_predicate<Geometry, predicates::contains_tag, false> Pred;
  317. template <typename Value, typename Indexable, typename Strategy>
  318. static inline bool apply(Pred const& p, Value const&, Indexable const& i, Strategy const& s)
  319. {
  320. return spatial_predicate_call<predicates::covers_tag>::apply(i, p.geometry, s);
  321. }
  322. };
  323. // spatial predicate - covers
  324. template <typename Geometry>
  325. struct predicate_check<predicates::spatial_predicate<Geometry, predicates::covers_tag, false>, bounds_tag>
  326. {
  327. typedef predicates::spatial_predicate<Geometry, predicates::covers_tag, false> Pred;
  328. template <typename Value, typename Indexable, typename Strategy>
  329. static inline bool apply(Pred const& p, Value const&, Indexable const& i, Strategy const& s)
  330. {
  331. return spatial_predicate_call<predicates::covers_tag>::apply(i, p.geometry, s);
  332. }
  333. };
  334. // spatial predicate - disjoint
  335. template <typename Geometry>
  336. struct predicate_check<predicates::spatial_predicate<Geometry, predicates::disjoint_tag, false>, bounds_tag>
  337. {
  338. typedef predicates::spatial_predicate<Geometry, predicates::disjoint_tag, false> Pred;
  339. template <typename Value, typename Indexable, typename Strategy>
  340. static inline bool apply(Pred const& p, Value const&, Indexable const& i, Strategy const& s)
  341. {
  342. return !spatial_predicate_call<predicates::covered_by_tag>::apply(i, p.geometry, s);
  343. }
  344. };
  345. // NEGATED
  346. // value_tag bounds_tag
  347. // ---------------------------
  348. // !contains(I,G) TRUE
  349. // !covered_by(I,G) !covered_by(I,G)
  350. // !covers(I,G) TRUE
  351. // !disjoint(I,G) !disjoint(I,G)
  352. // !intersects(I,G) !covered_by(I,G)
  353. // !overlaps(I,G) TRUE
  354. // !touches(I,G) !intersects(I,G)
  355. // !within(I,G) !within(I,G)
  356. // negated spatial predicate - default
  357. template <typename Geometry, typename Tag>
  358. struct predicate_check<predicates::spatial_predicate<Geometry, Tag, true>, bounds_tag>
  359. {
  360. typedef predicates::spatial_predicate<Geometry, Tag, true> Pred;
  361. template <typename Value, typename Indexable, typename Strategy>
  362. static inline bool apply(Pred const& p, Value const&, Indexable const& i, Strategy const& s)
  363. {
  364. return !spatial_predicate_call<Tag>::apply(i, p.geometry, s);
  365. }
  366. };
  367. // negated spatial predicate - contains
  368. template <typename Geometry>
  369. struct predicate_check<predicates::spatial_predicate<Geometry, predicates::contains_tag, true>, bounds_tag>
  370. {
  371. typedef predicates::spatial_predicate<Geometry, predicates::contains_tag, true> Pred;
  372. template <typename Value, typename Indexable, typename Strategy>
  373. static inline bool apply(Pred const& , Value const&, Indexable const&, Strategy const&)
  374. {
  375. return true;
  376. }
  377. };
  378. // negated spatial predicate - covers
  379. template <typename Geometry>
  380. struct predicate_check<predicates::spatial_predicate<Geometry, predicates::covers_tag, true>, bounds_tag>
  381. {
  382. typedef predicates::spatial_predicate<Geometry, predicates::covers_tag, true> Pred;
  383. template <typename Value, typename Indexable, typename Strategy>
  384. static inline bool apply(Pred const& , Value const&, Indexable const&, Strategy const&)
  385. {
  386. return true;
  387. }
  388. };
  389. // negated spatial predicate - intersects
  390. template <typename Geometry>
  391. struct predicate_check<predicates::spatial_predicate<Geometry, predicates::intersects_tag, true>, bounds_tag>
  392. {
  393. typedef predicates::spatial_predicate<Geometry, predicates::intersects_tag, true> Pred;
  394. template <typename Value, typename Indexable, typename Strategy>
  395. static inline bool apply(Pred const& p, Value const&, Indexable const& i, Strategy const& s)
  396. {
  397. return !spatial_predicate_call<predicates::covered_by_tag>::apply(i, p.geometry, s);
  398. }
  399. };
  400. // negated spatial predicate - overlaps
  401. template <typename Geometry>
  402. struct predicate_check<predicates::spatial_predicate<Geometry, predicates::overlaps_tag, true>, bounds_tag>
  403. {
  404. typedef predicates::spatial_predicate<Geometry, predicates::overlaps_tag, true> Pred;
  405. template <typename Value, typename Indexable, typename Strategy>
  406. static inline bool apply(Pred const& , Value const&, Indexable const&, Strategy const&)
  407. {
  408. return true;
  409. }
  410. };
  411. // negated spatial predicate - touches
  412. template <typename Geometry>
  413. struct predicate_check<predicates::spatial_predicate<Geometry, predicates::touches_tag, true>, bounds_tag>
  414. {
  415. typedef predicates::spatial_predicate<Geometry, predicates::touches_tag, true> Pred;
  416. template <typename Value, typename Indexable, typename Strategy>
  417. static inline bool apply(Pred const& p, Value const&, Indexable const& i, Strategy const&)
  418. {
  419. return !spatial_predicate_call<predicates::intersects_tag>::apply(i, p.geometry);
  420. }
  421. };
  422. // ------------------------------------------------------------------ //
  423. template <typename DistancePredicates>
  424. struct predicate_check<predicates::nearest<DistancePredicates>, bounds_tag>
  425. {
  426. template <typename Value, typename Box, typename Strategy>
  427. static inline bool apply(predicates::nearest<DistancePredicates> const&, Value const&, Box const&, Strategy const&)
  428. {
  429. return true;
  430. }
  431. };
  432. template <typename Linestring>
  433. struct predicate_check<predicates::path<Linestring>, bounds_tag>
  434. {
  435. template <typename Value, typename Box, typename Strategy>
  436. static inline bool apply(predicates::path<Linestring> const&, Value const&, Box const&, Strategy const&)
  437. {
  438. return true;
  439. }
  440. };
  441. // ------------------------------------------------------------------ //
  442. // predicates_length
  443. // ------------------------------------------------------------------ //
  444. template <typename T>
  445. struct predicates_length
  446. {
  447. static const unsigned value = 1;
  448. };
  449. //template <typename F, typename S>
  450. //struct predicates_length< std::pair<F, S> >
  451. //{
  452. // static const unsigned value = 2;
  453. //};
  454. //template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
  455. //struct predicates_length< boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> >
  456. //{
  457. // static const unsigned value = boost::tuples::length< boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> >::value;
  458. //};
  459. template <typename Head, typename Tail>
  460. struct predicates_length< boost::tuples::cons<Head, Tail> >
  461. {
  462. static const unsigned value = boost::tuples::length< boost::tuples::cons<Head, Tail> >::value;
  463. };
  464. // ------------------------------------------------------------------ //
  465. // predicates_element
  466. // ------------------------------------------------------------------ //
  467. template <unsigned I, typename T>
  468. struct predicates_element
  469. {
  470. BOOST_MPL_ASSERT_MSG((I < 1), INVALID_INDEX, (predicates_element));
  471. typedef T type;
  472. static type const& get(T const& p) { return p; }
  473. };
  474. //template <unsigned I, typename F, typename S>
  475. //struct predicates_element< I, std::pair<F, S> >
  476. //{
  477. // BOOST_MPL_ASSERT_MSG((I < 2), INVALID_INDEX, (predicates_element));
  478. //
  479. // typedef F type;
  480. // static type const& get(std::pair<F, S> const& p) { return p.first; }
  481. //};
  482. //
  483. //template <typename F, typename S>
  484. //struct predicates_element< 1, std::pair<F, S> >
  485. //{
  486. // typedef S type;
  487. // static type const& get(std::pair<F, S> const& p) { return p.second; }
  488. //};
  489. //
  490. //template <unsigned I, typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
  491. //struct predicates_element< I, boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> >
  492. //{
  493. // typedef boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> predicate_type;
  494. //
  495. // typedef typename boost::tuples::element<I, predicate_type>::type type;
  496. // static type const& get(predicate_type const& p) { return boost::get<I>(p); }
  497. //};
  498. template <unsigned I, typename Head, typename Tail>
  499. struct predicates_element< I, boost::tuples::cons<Head, Tail> >
  500. {
  501. typedef boost::tuples::cons<Head, Tail> predicate_type;
  502. typedef typename boost::tuples::element<I, predicate_type>::type type;
  503. static type const& get(predicate_type const& p) { return boost::get<I>(p); }
  504. };
  505. // ------------------------------------------------------------------ //
  506. // predicates_check
  507. // ------------------------------------------------------------------ //
  508. //template <typename PairPredicates, typename Tag, unsigned First, unsigned Last>
  509. //struct predicates_check_pair {};
  510. //
  511. //template <typename PairPredicates, typename Tag, unsigned I>
  512. //struct predicates_check_pair<PairPredicates, Tag, I, I>
  513. //{
  514. // template <typename Value, typename Indexable>
  515. // static inline bool apply(PairPredicates const& , Value const& , Indexable const& )
  516. // {
  517. // return true;
  518. // }
  519. //};
  520. //
  521. //template <typename PairPredicates, typename Tag>
  522. //struct predicates_check_pair<PairPredicates, Tag, 0, 1>
  523. //{
  524. // template <typename Value, typename Indexable>
  525. // static inline bool apply(PairPredicates const& p, Value const& v, Indexable const& i)
  526. // {
  527. // return predicate_check<typename PairPredicates::first_type, Tag>::apply(p.first, v, i);
  528. // }
  529. //};
  530. //
  531. //template <typename PairPredicates, typename Tag>
  532. //struct predicates_check_pair<PairPredicates, Tag, 1, 2>
  533. //{
  534. // template <typename Value, typename Indexable>
  535. // static inline bool apply(PairPredicates const& p, Value const& v, Indexable const& i)
  536. // {
  537. // return predicate_check<typename PairPredicates::second_type, Tag>::apply(p.second, v, i);
  538. // }
  539. //};
  540. //
  541. //template <typename PairPredicates, typename Tag>
  542. //struct predicates_check_pair<PairPredicates, Tag, 0, 2>
  543. //{
  544. // template <typename Value, typename Indexable>
  545. // static inline bool apply(PairPredicates const& p, Value const& v, Indexable const& i)
  546. // {
  547. // return predicate_check<typename PairPredicates::first_type, Tag>::apply(p.first, v, i)
  548. // && predicate_check<typename PairPredicates::second_type, Tag>::apply(p.second, v, i);
  549. // }
  550. //};
  551. template <typename TuplePredicates, typename Tag, unsigned First, unsigned Last>
  552. struct predicates_check_tuple
  553. {
  554. template <typename Value, typename Indexable, typename Strategy>
  555. static inline bool apply(TuplePredicates const& p, Value const& v, Indexable const& i, Strategy const& s)
  556. {
  557. return predicate_check
  558. <
  559. typename boost::tuples::element<First, TuplePredicates>::type,
  560. Tag
  561. >::apply(boost::get<First>(p), v, i, s)
  562. && predicates_check_tuple<TuplePredicates, Tag, First+1, Last>::apply(p, v, i, s);
  563. }
  564. };
  565. template <typename TuplePredicates, typename Tag, unsigned First>
  566. struct predicates_check_tuple<TuplePredicates, Tag, First, First>
  567. {
  568. template <typename Value, typename Indexable, typename Strategy>
  569. static inline bool apply(TuplePredicates const& , Value const& , Indexable const& , Strategy const& )
  570. {
  571. return true;
  572. }
  573. };
  574. template <typename Predicate, typename Tag, unsigned First, unsigned Last>
  575. struct predicates_check_impl
  576. {
  577. static const bool check = First < 1 && Last <= 1 && First <= Last;
  578. BOOST_MPL_ASSERT_MSG((check), INVALID_INDEXES, (predicates_check_impl));
  579. template <typename Value, typename Indexable, typename Strategy>
  580. static inline bool apply(Predicate const& p, Value const& v, Indexable const& i, Strategy const& s)
  581. {
  582. return predicate_check<Predicate, Tag>::apply(p, v, i, s);
  583. }
  584. };
  585. //template <typename Predicate1, typename Predicate2, typename Tag, size_t First, size_t Last>
  586. //struct predicates_check_impl<std::pair<Predicate1, Predicate2>, Tag, First, Last>
  587. //{
  588. // BOOST_MPL_ASSERT_MSG((First < 2 && Last <= 2 && First <= Last), INVALID_INDEXES, (predicates_check_impl));
  589. //
  590. // template <typename Value, typename Indexable>
  591. // static inline bool apply(std::pair<Predicate1, Predicate2> const& p, Value const& v, Indexable const& i)
  592. // {
  593. // return predicate_check<Predicate1, Tag>::apply(p.first, v, i)
  594. // && predicate_check<Predicate2, Tag>::apply(p.second, v, i);
  595. // }
  596. //};
  597. //
  598. //template <
  599. // typename T0, typename T1, typename T2, typename T3, typename T4,
  600. // typename T5, typename T6, typename T7, typename T8, typename T9,
  601. // typename Tag, unsigned First, unsigned Last
  602. //>
  603. //struct predicates_check_impl<
  604. // boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>,
  605. // Tag, First, Last
  606. //>
  607. //{
  608. // typedef boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> predicates_type;
  609. //
  610. // static const unsigned pred_len = boost::tuples::length<predicates_type>::value;
  611. // BOOST_MPL_ASSERT_MSG((First < pred_len && Last <= pred_len && First <= Last), INVALID_INDEXES, (predicates_check_impl));
  612. //
  613. // template <typename Value, typename Indexable>
  614. // static inline bool apply(predicates_type const& p, Value const& v, Indexable const& i)
  615. // {
  616. // return predicates_check_tuple<
  617. // predicates_type,
  618. // Tag, First, Last
  619. // >::apply(p, v, i);
  620. // }
  621. //};
  622. template <typename Head, typename Tail, typename Tag, unsigned First, unsigned Last>
  623. struct predicates_check_impl<
  624. boost::tuples::cons<Head, Tail>,
  625. Tag, First, Last
  626. >
  627. {
  628. typedef boost::tuples::cons<Head, Tail> predicates_type;
  629. static const unsigned pred_len = boost::tuples::length<predicates_type>::value;
  630. static const bool check = First < pred_len && Last <= pred_len && First <= Last;
  631. BOOST_MPL_ASSERT_MSG((check), INVALID_INDEXES, (predicates_check_impl));
  632. template <typename Value, typename Indexable, typename Strategy>
  633. static inline bool apply(predicates_type const& p, Value const& v, Indexable const& i, Strategy const& s)
  634. {
  635. return predicates_check_tuple<
  636. predicates_type,
  637. Tag, First, Last
  638. >::apply(p, v, i, s);
  639. }
  640. };
  641. template <typename Tag, unsigned First, unsigned Last, typename Predicates, typename Value, typename Indexable, typename Strategy>
  642. inline bool predicates_check(Predicates const& p, Value const& v, Indexable const& i, Strategy const& s)
  643. {
  644. return detail::predicates_check_impl<Predicates, Tag, First, Last>
  645. ::apply(p, v, i, s);
  646. }
  647. // ------------------------------------------------------------------ //
  648. // nearest predicate helpers
  649. // ------------------------------------------------------------------ //
  650. // predicates_is_nearest
  651. template <typename P>
  652. struct predicates_is_distance
  653. {
  654. static const unsigned value = 0;
  655. };
  656. template <typename DistancePredicates>
  657. struct predicates_is_distance< predicates::nearest<DistancePredicates> >
  658. {
  659. static const unsigned value = 1;
  660. };
  661. template <typename Linestring>
  662. struct predicates_is_distance< predicates::path<Linestring> >
  663. {
  664. static const unsigned value = 1;
  665. };
  666. // predicates_count_nearest
  667. template <typename T>
  668. struct predicates_count_distance
  669. {
  670. static const unsigned value = predicates_is_distance<T>::value;
  671. };
  672. //template <typename F, typename S>
  673. //struct predicates_count_distance< std::pair<F, S> >
  674. //{
  675. // static const unsigned value = predicates_is_distance<F>::value
  676. // + predicates_is_distance<S>::value;
  677. //};
  678. template <typename Tuple, unsigned N>
  679. struct predicates_count_distance_tuple
  680. {
  681. static const unsigned value =
  682. predicates_is_distance<typename boost::tuples::element<N-1, Tuple>::type>::value
  683. + predicates_count_distance_tuple<Tuple, N-1>::value;
  684. };
  685. template <typename Tuple>
  686. struct predicates_count_distance_tuple<Tuple, 1>
  687. {
  688. static const unsigned value =
  689. predicates_is_distance<typename boost::tuples::element<0, Tuple>::type>::value;
  690. };
  691. //template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
  692. //struct predicates_count_distance< boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> >
  693. //{
  694. // static const unsigned value = predicates_count_distance_tuple<
  695. // boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>,
  696. // boost::tuples::length< boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> >::value
  697. // >::value;
  698. //};
  699. template <typename Head, typename Tail>
  700. struct predicates_count_distance< boost::tuples::cons<Head, Tail> >
  701. {
  702. static const unsigned value = predicates_count_distance_tuple<
  703. boost::tuples::cons<Head, Tail>,
  704. boost::tuples::length< boost::tuples::cons<Head, Tail> >::value
  705. >::value;
  706. };
  707. // predicates_find_nearest
  708. template <typename T>
  709. struct predicates_find_distance
  710. {
  711. static const unsigned value = predicates_is_distance<T>::value ? 0 : 1;
  712. };
  713. //template <typename F, typename S>
  714. //struct predicates_find_distance< std::pair<F, S> >
  715. //{
  716. // static const unsigned value = predicates_is_distance<F>::value ? 0 :
  717. // (predicates_is_distance<S>::value ? 1 : 2);
  718. //};
  719. template <typename Tuple, unsigned N>
  720. struct predicates_find_distance_tuple
  721. {
  722. static const bool is_found = predicates_find_distance_tuple<Tuple, N-1>::is_found
  723. || predicates_is_distance<typename boost::tuples::element<N-1, Tuple>::type>::value;
  724. static const unsigned value = predicates_find_distance_tuple<Tuple, N-1>::is_found ?
  725. predicates_find_distance_tuple<Tuple, N-1>::value :
  726. (predicates_is_distance<typename boost::tuples::element<N-1, Tuple>::type>::value ?
  727. N-1 : boost::tuples::length<Tuple>::value);
  728. };
  729. template <typename Tuple>
  730. struct predicates_find_distance_tuple<Tuple, 1>
  731. {
  732. static const bool is_found = predicates_is_distance<typename boost::tuples::element<0, Tuple>::type>::value;
  733. static const unsigned value = is_found ? 0 : boost::tuples::length<Tuple>::value;
  734. };
  735. //template <typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
  736. //struct predicates_find_distance< boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> >
  737. //{
  738. // static const unsigned value = predicates_find_distance_tuple<
  739. // boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9>,
  740. // boost::tuples::length< boost::tuple<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> >::value
  741. // >::value;
  742. //};
  743. template <typename Head, typename Tail>
  744. struct predicates_find_distance< boost::tuples::cons<Head, Tail> >
  745. {
  746. static const unsigned value = predicates_find_distance_tuple<
  747. boost::tuples::cons<Head, Tail>,
  748. boost::tuples::length< boost::tuples::cons<Head, Tail> >::value
  749. >::value;
  750. };
  751. }}}} // namespace boost::geometry::index::detail
  752. #endif // BOOST_GEOMETRY_INDEX_DETAIL_PREDICATES_HPP