for_each.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2014 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2014-2023.
  7. // Modifications copyright (c) 2014-2023, Oracle and/or its affiliates.
  8. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  9. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  10. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  11. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  12. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  13. // Use, modification and distribution is subject to the Boost Software License,
  14. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. #ifndef BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP
  17. #define BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP
  18. #include <boost/range/begin.hpp>
  19. #include <boost/range/end.hpp>
  20. #include <boost/range/reference.hpp>
  21. #include <boost/range/value_type.hpp>
  22. #include <boost/geometry/algorithms/not_implemented.hpp>
  23. #include <boost/geometry/core/closure.hpp>
  24. #include <boost/geometry/core/exterior_ring.hpp>
  25. #include <boost/geometry/core/interior_rings.hpp>
  26. #include <boost/geometry/core/point_type.hpp>
  27. #include <boost/geometry/core/tag_cast.hpp>
  28. #include <boost/geometry/core/tags.hpp>
  29. #include <boost/geometry/geometries/concepts/check.hpp>
  30. #include <boost/geometry/geometries/segment.hpp>
  31. #include <boost/geometry/views/detail/indexed_point_view.hpp>
  32. namespace boost { namespace geometry
  33. {
  34. #ifndef DOXYGEN_NO_DETAIL
  35. namespace detail { namespace for_each
  36. {
  37. struct fe_point_point
  38. {
  39. template <typename Point, typename Functor>
  40. static inline bool apply(Point& point, Functor&& f)
  41. {
  42. return f(point);
  43. }
  44. };
  45. struct fe_segment_point
  46. {
  47. template <typename Point, typename Functor>
  48. static inline bool apply(Point& , Functor&& )
  49. {
  50. // TODO: if non-const, we should extract the points from the segment
  51. // and call the functor on those two points
  52. //model::referring_segment<Point> s(point, point);
  53. //return f(s);
  54. return true;
  55. }
  56. };
  57. struct fe_point_segment
  58. {
  59. template <typename Segment, typename Functor>
  60. static inline bool apply(Segment& s, Functor&& f)
  61. {
  62. // Or should we guarantee that the type of points is
  63. // point_type<Segment>::type ?
  64. geometry::detail::indexed_point_view<Segment, 0> p0(s);
  65. geometry::detail::indexed_point_view<Segment, 1> p1(s);
  66. return f(p0) && f(p1);
  67. }
  68. };
  69. struct fe_segment_segment
  70. {
  71. template <typename Segment, typename Functor>
  72. static inline bool apply(Segment& s, Functor&& f)
  73. {
  74. // Or should we guarantee that the type of segment is
  75. // referring_segment<...> ?
  76. return f(s);
  77. }
  78. };
  79. template <typename Range>
  80. struct fe_range_value
  81. {
  82. typedef util::transcribe_const_t
  83. <
  84. Range,
  85. typename boost::range_value<Range>::type
  86. > type;
  87. };
  88. template <typename Range>
  89. struct fe_point_type
  90. {
  91. typedef util::transcribe_const_t
  92. <
  93. Range,
  94. typename point_type<Range>::type
  95. > type;
  96. };
  97. template <typename Range>
  98. struct fe_point_type_is_referencable
  99. {
  100. static const bool value =
  101. std::is_const<Range>::value
  102. || std::is_same
  103. <
  104. typename boost::range_reference<Range>::type,
  105. typename fe_point_type<Range>::type&
  106. >::value;
  107. };
  108. template
  109. <
  110. typename Range,
  111. bool UseReferences = fe_point_type_is_referencable<Range>::value
  112. >
  113. struct fe_point_call_f
  114. {
  115. template <typename Iterator, typename Functor>
  116. static inline bool apply(Iterator it, Functor&& f)
  117. {
  118. // Implementation for real references (both const and mutable)
  119. // and const proxy references.
  120. typedef typename fe_point_type<Range>::type point_type;
  121. point_type& p = *it;
  122. return f(p);
  123. }
  124. };
  125. template <typename Range>
  126. struct fe_point_call_f<Range, false>
  127. {
  128. template <typename Iterator, typename Functor>
  129. static inline bool apply(Iterator it, Functor&& f)
  130. {
  131. // Implementation for proxy mutable references.
  132. // Temporary point has to be created and assigned afterwards.
  133. typedef typename fe_point_type<Range>::type point_type;
  134. point_type p = *it;
  135. bool result = f(p);
  136. *it = p;
  137. return result;
  138. }
  139. };
  140. struct fe_point_range
  141. {
  142. template <typename Range, typename Functor>
  143. static inline bool apply(Range& range, Functor&& f)
  144. {
  145. auto const end = boost::end(range);
  146. for (auto it = boost::begin(range); it != end; ++it)
  147. {
  148. if (! fe_point_call_f<Range>::apply(it, f))
  149. {
  150. return false;
  151. }
  152. }
  153. return true;
  154. }
  155. };
  156. template
  157. <
  158. typename Range,
  159. bool UseReferences = fe_point_type_is_referencable<Range>::value
  160. >
  161. struct fe_segment_call_f
  162. {
  163. template <typename Iterator, typename Functor>
  164. static inline bool apply(Iterator it0, Iterator it1, Functor&& f)
  165. {
  166. // Implementation for real references (both const and mutable)
  167. // and const proxy references.
  168. // If const proxy references are returned by iterators
  169. // then const real references here prevents temporary
  170. // objects from being destroyed.
  171. typedef typename fe_point_type<Range>::type point_type;
  172. point_type& p0 = *it0;
  173. point_type& p1 = *it1;
  174. model::referring_segment<point_type> s(p0, p1);
  175. return f(s);
  176. }
  177. };
  178. template <typename Range>
  179. struct fe_segment_call_f<Range, false>
  180. {
  181. template <typename Iterator, typename Functor>
  182. static inline bool apply(Iterator it0, Iterator it1, Functor&& f)
  183. {
  184. // Mutable proxy references returned by iterators.
  185. // Temporary points have to be created and assigned afterwards.
  186. typedef typename fe_point_type<Range>::type point_type;
  187. point_type p0 = *it0;
  188. point_type p1 = *it1;
  189. model::referring_segment<point_type> s(p0, p1);
  190. bool result = f(s);
  191. *it0 = p0;
  192. *it1 = p1;
  193. return result;
  194. }
  195. };
  196. template <closure_selector Closure>
  197. struct fe_segment_range_with_closure
  198. {
  199. template <typename Range, typename Functor>
  200. static inline bool apply(Range& range, Functor&& f)
  201. {
  202. auto it = boost::begin(range);
  203. auto const end = boost::end(range);
  204. if (it == end)
  205. {
  206. return true;
  207. }
  208. auto previous = it++;
  209. if (it == end)
  210. {
  211. return fe_segment_call_f<Range>::apply(previous, previous, f);
  212. }
  213. while (it != end)
  214. {
  215. if (! fe_segment_call_f<Range>::apply(previous, it, f))
  216. {
  217. return false;
  218. }
  219. previous = it++;
  220. }
  221. return true;
  222. }
  223. };
  224. template <>
  225. struct fe_segment_range_with_closure<open>
  226. {
  227. template <typename Range, typename Functor>
  228. static inline bool apply(Range& range, Functor&& f)
  229. {
  230. if (! fe_segment_range_with_closure<closed>::apply(range, f))
  231. {
  232. return false;
  233. }
  234. auto const begin = boost::begin(range);
  235. auto end = boost::end(range);
  236. if (begin == end)
  237. {
  238. return true;
  239. }
  240. --end;
  241. if (begin == end)
  242. {
  243. // single point ranges already handled in closed case above
  244. return true;
  245. }
  246. return fe_segment_call_f<Range>::apply(end, begin, f);
  247. }
  248. };
  249. struct fe_segment_range
  250. {
  251. template <typename Range, typename Functor>
  252. static inline bool apply(Range& range, Functor&& f)
  253. {
  254. return fe_segment_range_with_closure
  255. <
  256. closure<Range>::value
  257. >::apply(range, f);
  258. }
  259. };
  260. template <typename RangePolicy>
  261. struct for_each_polygon
  262. {
  263. template <typename Polygon, typename Functor>
  264. static inline bool apply(Polygon& poly, Functor&& f)
  265. {
  266. if (! RangePolicy::apply(exterior_ring(poly), f))
  267. {
  268. return false;
  269. }
  270. auto&& rings = interior_rings(poly);
  271. auto const end = boost::end(rings);
  272. for (auto it = boost::begin(rings); it != end; ++it)
  273. {
  274. // NOTE: Currently lvalue iterator required
  275. if (! RangePolicy::apply(*it, f))
  276. {
  277. return false;
  278. }
  279. }
  280. return true;
  281. }
  282. };
  283. // Implementation of multi, for both point and segment,
  284. // just calling the single version.
  285. template <typename SinglePolicy>
  286. struct for_each_multi
  287. {
  288. template <typename MultiGeometry, typename Functor>
  289. static inline bool apply(MultiGeometry& multi, Functor&& f)
  290. {
  291. auto const end = boost::end(multi);
  292. for (auto it = boost::begin(multi); it != end; ++it)
  293. {
  294. // NOTE: Currently lvalue iterator required
  295. if (! SinglePolicy::apply(*it, f))
  296. {
  297. return false;
  298. }
  299. }
  300. return true;
  301. }
  302. };
  303. }} // namespace detail::for_each
  304. #endif // DOXYGEN_NO_DETAIL
  305. #ifndef DOXYGEN_NO_DISPATCH
  306. namespace dispatch
  307. {
  308. template
  309. <
  310. typename Geometry,
  311. typename Tag = typename tag_cast<typename tag<Geometry>::type, multi_tag>::type
  312. >
  313. struct for_each_point: not_implemented<Tag>
  314. {};
  315. template <typename Point>
  316. struct for_each_point<Point, point_tag>
  317. : detail::for_each::fe_point_point
  318. {};
  319. template <typename Segment>
  320. struct for_each_point<Segment, segment_tag>
  321. : detail::for_each::fe_point_segment
  322. {};
  323. template <typename Linestring>
  324. struct for_each_point<Linestring, linestring_tag>
  325. : detail::for_each::fe_point_range
  326. {};
  327. template <typename Ring>
  328. struct for_each_point<Ring, ring_tag>
  329. : detail::for_each::fe_point_range
  330. {};
  331. template <typename Polygon>
  332. struct for_each_point<Polygon, polygon_tag>
  333. : detail::for_each::for_each_polygon
  334. <
  335. detail::for_each::fe_point_range
  336. >
  337. {};
  338. template <typename MultiGeometry>
  339. struct for_each_point<MultiGeometry, multi_tag>
  340. : detail::for_each::for_each_multi
  341. <
  342. // Specify the dispatch of the single-version as policy
  343. for_each_point
  344. <
  345. typename detail::for_each::fe_range_value
  346. <
  347. MultiGeometry
  348. >::type
  349. >
  350. >
  351. {};
  352. template
  353. <
  354. typename Geometry,
  355. typename Tag = typename tag<Geometry>::type
  356. >
  357. struct for_each_segment: not_implemented<Tag>
  358. {};
  359. template <typename Point>
  360. struct for_each_segment<Point, point_tag>
  361. : detail::for_each::fe_segment_point // empty
  362. {};
  363. template <typename Segment>
  364. struct for_each_segment<Segment, segment_tag>
  365. : detail::for_each::fe_segment_segment
  366. {};
  367. template <typename Linestring>
  368. struct for_each_segment<Linestring, linestring_tag>
  369. : detail::for_each::fe_segment_range
  370. {};
  371. template <typename Ring>
  372. struct for_each_segment<Ring, ring_tag>
  373. : detail::for_each::fe_segment_range
  374. {};
  375. template <typename Polygon>
  376. struct for_each_segment<Polygon, polygon_tag>
  377. : detail::for_each::for_each_polygon
  378. <
  379. detail::for_each::fe_segment_range
  380. >
  381. {};
  382. template <typename MultiPoint>
  383. struct for_each_segment<MultiPoint, multi_point_tag>
  384. : detail::for_each::fe_segment_point // empty
  385. {};
  386. template <typename MultiLinestring>
  387. struct for_each_segment<MultiLinestring, multi_linestring_tag>
  388. : detail::for_each::for_each_multi
  389. <
  390. detail::for_each::fe_segment_range
  391. >
  392. {};
  393. template <typename MultiPolygon>
  394. struct for_each_segment<MultiPolygon, multi_polygon_tag>
  395. : detail::for_each::for_each_multi
  396. <
  397. detail::for_each::for_each_polygon
  398. <
  399. detail::for_each::fe_segment_range
  400. >
  401. >
  402. {};
  403. } // namespace dispatch
  404. #endif // DOXYGEN_NO_DISPATCH
  405. template<typename Geometry, typename UnaryPredicate>
  406. inline bool all_points_of(Geometry& geometry, UnaryPredicate p)
  407. {
  408. concepts::check<Geometry>();
  409. return dispatch::for_each_point<Geometry>::apply(geometry, p);
  410. }
  411. template<typename Geometry, typename UnaryPredicate>
  412. inline bool all_segments_of(Geometry const& geometry, UnaryPredicate p)
  413. {
  414. concepts::check<Geometry const>();
  415. return dispatch::for_each_segment<Geometry const>::apply(geometry, p);
  416. }
  417. template<typename Geometry, typename UnaryPredicate>
  418. inline bool any_point_of(Geometry& geometry, UnaryPredicate p)
  419. {
  420. concepts::check<Geometry>();
  421. return ! dispatch::for_each_point<Geometry>::apply(geometry, [&](auto&& pt)
  422. {
  423. return ! p(pt);
  424. });
  425. }
  426. template<typename Geometry, typename UnaryPredicate>
  427. inline bool any_segment_of(Geometry const& geometry, UnaryPredicate p)
  428. {
  429. concepts::check<Geometry const>();
  430. return ! dispatch::for_each_segment<Geometry const>::apply(geometry, [&](auto&& s)
  431. {
  432. return ! p(s);
  433. });
  434. }
  435. template<typename Geometry, typename UnaryPredicate>
  436. inline bool none_point_of(Geometry& geometry, UnaryPredicate p)
  437. {
  438. concepts::check<Geometry>();
  439. return dispatch::for_each_point<Geometry>::apply(geometry, [&](auto&& pt)
  440. {
  441. return ! p(pt);
  442. });
  443. }
  444. template<typename Geometry, typename UnaryPredicate>
  445. inline bool none_segment_of(Geometry const& geometry, UnaryPredicate p)
  446. {
  447. concepts::check<Geometry const>();
  448. return dispatch::for_each_segment<Geometry const>::apply(geometry, [&](auto&& s)
  449. {
  450. return ! p(s);
  451. });
  452. }
  453. /*!
  454. \brief \brf_for_each{point}
  455. \details \det_for_each{point}
  456. \ingroup for_each
  457. \param geometry \param_geometry
  458. \param f \par_for_each_f{point}
  459. \tparam Geometry \tparam_geometry
  460. \tparam Functor \tparam_functor
  461. \qbk{[include reference/algorithms/for_each_point.qbk]}
  462. \qbk{[heading Example]}
  463. \qbk{[for_each_point] [for_each_point_output]}
  464. \qbk{[for_each_point_const] [for_each_point_const_output]}
  465. */
  466. template<typename Geometry, typename Functor>
  467. inline Functor for_each_point(Geometry& geometry, Functor f)
  468. {
  469. concepts::check<Geometry>();
  470. dispatch::for_each_point<Geometry>::apply(geometry, [&](auto&& pt)
  471. {
  472. f(pt);
  473. // TODO: Implement separate function?
  474. return true;
  475. });
  476. return f;
  477. }
  478. /*!
  479. \brief \brf_for_each{segment}
  480. \details \det_for_each{segment}
  481. \ingroup for_each
  482. \param geometry \param_geometry
  483. \param f \par_for_each_f{segment}
  484. \tparam Geometry \tparam_geometry
  485. \tparam Functor \tparam_functor
  486. \qbk{[include reference/algorithms/for_each_segment.qbk]}
  487. \qbk{[heading Example]}
  488. \qbk{[for_each_segment_const] [for_each_segment_const_output]}
  489. */
  490. template<typename Geometry, typename Functor>
  491. inline Functor for_each_segment(Geometry& geometry, Functor f)
  492. {
  493. concepts::check<Geometry>();
  494. dispatch::for_each_segment<Geometry>::apply(geometry, [&](auto&& s)
  495. {
  496. f(s);
  497. // TODO: Implement separate function?
  498. return true;
  499. });
  500. return f;
  501. }
  502. }} // namespace boost::geometry
  503. #endif // BOOST_GEOMETRY_ALGORITHMS_FOR_EACH_HPP