append.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. // This file was modified by Oracle on 2014-2020.
  6. // Modifications copyright (c) 2014-2020, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  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_ALGORITHMS_APPEND_HPP
  15. #define BOOST_GEOMETRY_ALGORITHMS_APPEND_HPP
  16. #include <boost/range/begin.hpp>
  17. #include <boost/range/end.hpp>
  18. #include <boost/range/value_type.hpp>
  19. #include <boost/variant/apply_visitor.hpp>
  20. #include <boost/variant/static_visitor.hpp>
  21. #include <boost/variant/variant_fwd.hpp>
  22. #include <boost/geometry/algorithms/num_interior_rings.hpp>
  23. #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
  24. #include <boost/geometry/core/access.hpp>
  25. #include <boost/geometry/core/mutable_range.hpp>
  26. #include <boost/geometry/core/point_type.hpp>
  27. #include <boost/geometry/core/tags.hpp>
  28. #include <boost/geometry/geometries/concepts/check.hpp>
  29. #include <boost/geometry/geometries/variant.hpp>
  30. #include <boost/geometry/util/range.hpp>
  31. namespace boost { namespace geometry
  32. {
  33. #ifndef DOXYGEN_NO_DETAIL
  34. namespace detail { namespace append
  35. {
  36. template <typename Geometry, typename Point>
  37. struct append_no_action
  38. {
  39. static inline void apply(Geometry& , Point const& ,
  40. int = 0, int = 0)
  41. {
  42. }
  43. };
  44. template <typename Geometry, typename Point>
  45. struct append_point
  46. {
  47. static inline void apply(Geometry& geometry, Point const& point,
  48. int = 0, int = 0)
  49. {
  50. typename geometry::point_type<Geometry>::type copy;
  51. geometry::detail::conversion::convert_point_to_point(point, copy);
  52. traits::push_back<Geometry>::apply(geometry, copy);
  53. }
  54. };
  55. template <typename Geometry, typename Range>
  56. struct append_range
  57. {
  58. typedef typename boost::range_value<Range>::type point_type;
  59. static inline void apply(Geometry& geometry, Range const& range,
  60. int = 0, int = 0)
  61. {
  62. for (typename boost::range_iterator<Range const>::type
  63. it = boost::begin(range);
  64. it != boost::end(range);
  65. ++it)
  66. {
  67. append_point<Geometry, point_type>::apply(geometry, *it);
  68. }
  69. }
  70. };
  71. template <typename Polygon, typename Point>
  72. struct point_to_polygon
  73. {
  74. typedef typename ring_type<Polygon>::type ring_type;
  75. typedef typename ring_return_type<Polygon>::type exterior_ring_type;
  76. typedef typename interior_return_type<Polygon>::type interior_ring_range_type;
  77. static inline void apply(Polygon& polygon, Point const& point,
  78. int ring_index, int = 0)
  79. {
  80. if (ring_index == -1)
  81. {
  82. exterior_ring_type ext_ring = exterior_ring(polygon);
  83. append_point<ring_type, Point>::apply(
  84. ext_ring, point);
  85. }
  86. else if (ring_index < int(num_interior_rings(polygon)))
  87. {
  88. interior_ring_range_type int_rings = interior_rings(polygon);
  89. append_point<ring_type, Point>::apply(
  90. range::at(int_rings, ring_index), point);
  91. }
  92. }
  93. };
  94. template <typename Polygon, typename Range>
  95. struct range_to_polygon
  96. {
  97. typedef typename ring_type<Polygon>::type ring_type;
  98. typedef typename ring_return_type<Polygon>::type exterior_ring_type;
  99. typedef typename interior_return_type<Polygon>::type interior_ring_range_type;
  100. static inline void apply(Polygon& polygon, Range const& range,
  101. int ring_index, int = 0)
  102. {
  103. if (ring_index == -1)
  104. {
  105. exterior_ring_type ext_ring = exterior_ring(polygon);
  106. append_range<ring_type, Range>::apply(
  107. ext_ring, range);
  108. }
  109. else if (ring_index < int(num_interior_rings(polygon)))
  110. {
  111. interior_ring_range_type int_rings = interior_rings(polygon);
  112. append_range<ring_type, Range>::apply(
  113. range::at(int_rings, ring_index), range);
  114. }
  115. }
  116. };
  117. }} // namespace detail::append
  118. #endif // DOXYGEN_NO_DETAIL
  119. #ifndef DOXYGEN_NO_DISPATCH
  120. namespace dispatch
  121. {
  122. namespace splitted_dispatch
  123. {
  124. template <typename Tag, typename Geometry, typename Point>
  125. struct append_point
  126. : detail::append::append_no_action<Geometry, Point>
  127. {};
  128. template <typename Geometry, typename Point>
  129. struct append_point<linestring_tag, Geometry, Point>
  130. : detail::append::append_point<Geometry, Point>
  131. {};
  132. template <typename Geometry, typename Point>
  133. struct append_point<ring_tag, Geometry, Point>
  134. : detail::append::append_point<Geometry, Point>
  135. {};
  136. template <typename Polygon, typename Point>
  137. struct append_point<polygon_tag, Polygon, Point>
  138. : detail::append::point_to_polygon<Polygon, Point>
  139. {};
  140. template <typename Tag, typename Geometry, typename Range>
  141. struct append_range
  142. : detail::append::append_no_action<Geometry, Range>
  143. {};
  144. template <typename Geometry, typename Range>
  145. struct append_range<linestring_tag, Geometry, Range>
  146. : detail::append::append_range<Geometry, Range>
  147. {};
  148. template <typename Geometry, typename Range>
  149. struct append_range<ring_tag, Geometry, Range>
  150. : detail::append::append_range<Geometry, Range>
  151. {};
  152. template <typename Polygon, typename Range>
  153. struct append_range<polygon_tag, Polygon, Range>
  154. : detail::append::range_to_polygon<Polygon, Range>
  155. {};
  156. } // namespace splitted_dispatch
  157. // Default: append a range (or linestring or ring or whatever) to any geometry
  158. template
  159. <
  160. typename Geometry, typename RangeOrPoint,
  161. typename TagRangeOrPoint = typename tag<RangeOrPoint>::type
  162. >
  163. struct append
  164. : splitted_dispatch::append_range<typename tag<Geometry>::type, Geometry, RangeOrPoint>
  165. {};
  166. // Specialization for point to append a point to any geometry
  167. template <typename Geometry, typename RangeOrPoint>
  168. struct append<Geometry, RangeOrPoint, point_tag>
  169. : splitted_dispatch::append_point<typename tag<Geometry>::type, Geometry, RangeOrPoint>
  170. {};
  171. } // namespace dispatch
  172. #endif // DOXYGEN_NO_DISPATCH
  173. #ifndef DOXYGEN_NO_DETAIL
  174. namespace detail { namespace append
  175. {
  176. template <typename MultiGeometry, typename RangeOrPoint>
  177. struct append_to_multigeometry
  178. {
  179. static inline void apply(MultiGeometry& multigeometry,
  180. RangeOrPoint const& range_or_point,
  181. int ring_index, int multi_index)
  182. {
  183. dispatch::append
  184. <
  185. typename boost::range_value<MultiGeometry>::type,
  186. RangeOrPoint
  187. >::apply(range::at(multigeometry, multi_index), range_or_point, ring_index);
  188. }
  189. };
  190. }} // namespace detail::append
  191. #endif // DOXYGEN_NO_DETAIL
  192. #ifndef DOXYGEN_NO_DISPATCH
  193. namespace dispatch
  194. {
  195. namespace splitted_dispatch
  196. {
  197. template <typename Geometry, typename Point>
  198. struct append_point<multi_point_tag, Geometry, Point>
  199. : detail::append::append_point<Geometry, Point>
  200. {};
  201. template <typename Geometry, typename Range>
  202. struct append_range<multi_point_tag, Geometry, Range>
  203. : detail::append::append_range<Geometry, Range>
  204. {};
  205. template <typename MultiGeometry, typename RangeOrPoint>
  206. struct append_point<multi_linestring_tag, MultiGeometry, RangeOrPoint>
  207. : detail::append::append_to_multigeometry<MultiGeometry, RangeOrPoint>
  208. {};
  209. template <typename MultiGeometry, typename RangeOrPoint>
  210. struct append_range<multi_linestring_tag, MultiGeometry, RangeOrPoint>
  211. : detail::append::append_to_multigeometry<MultiGeometry, RangeOrPoint>
  212. {};
  213. template <typename MultiGeometry, typename RangeOrPoint>
  214. struct append_point<multi_polygon_tag, MultiGeometry, RangeOrPoint>
  215. : detail::append::append_to_multigeometry<MultiGeometry, RangeOrPoint>
  216. {};
  217. template <typename MultiGeometry, typename RangeOrPoint>
  218. struct append_range<multi_polygon_tag, MultiGeometry, RangeOrPoint>
  219. : detail::append::append_to_multigeometry<MultiGeometry, RangeOrPoint>
  220. {};
  221. } // namespace splitted_dispatch
  222. } // namespace dispatch
  223. #endif // DOXYGEN_NO_DISPATCH
  224. namespace resolve_variant {
  225. template <typename Geometry>
  226. struct append
  227. {
  228. template <typename RangeOrPoint>
  229. static inline void apply(Geometry& geometry,
  230. RangeOrPoint const& range_or_point,
  231. int ring_index,
  232. int multi_index)
  233. {
  234. concepts::check<Geometry>();
  235. dispatch::append<Geometry, RangeOrPoint>::apply(geometry,
  236. range_or_point,
  237. ring_index,
  238. multi_index);
  239. }
  240. };
  241. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  242. struct append<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  243. {
  244. template <typename RangeOrPoint>
  245. struct visitor: boost::static_visitor<void>
  246. {
  247. RangeOrPoint const& m_range_or_point;
  248. int m_ring_index;
  249. int m_multi_index;
  250. visitor(RangeOrPoint const& range_or_point,
  251. int ring_index,
  252. int multi_index):
  253. m_range_or_point(range_or_point),
  254. m_ring_index(ring_index),
  255. m_multi_index(multi_index)
  256. {}
  257. template <typename Geometry>
  258. void operator()(Geometry& geometry) const
  259. {
  260. append<Geometry>::apply(geometry,
  261. m_range_or_point,
  262. m_ring_index,
  263. m_multi_index);
  264. }
  265. };
  266. template <typename RangeOrPoint>
  267. static inline void apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& variant_geometry,
  268. RangeOrPoint const& range_or_point,
  269. int ring_index,
  270. int multi_index)
  271. {
  272. boost::apply_visitor(
  273. visitor<RangeOrPoint>(
  274. range_or_point,
  275. ring_index,
  276. multi_index
  277. ),
  278. variant_geometry
  279. );
  280. }
  281. };
  282. } // namespace resolve_variant;
  283. /*!
  284. \brief Appends one or more points to a linestring, ring, polygon, multi-geometry
  285. \ingroup append
  286. \tparam Geometry \tparam_geometry
  287. \tparam RangeOrPoint Either a range or a point, fullfilling Boost.Range concept or Boost.Geometry Point Concept
  288. \param geometry \param_geometry
  289. \param range_or_point The point or range to add
  290. \param ring_index The index of the ring in case of a polygon:
  291. exterior ring (-1, the default) or interior ring index
  292. \param multi_index The index of the geometry to which the points are appended
  293. \qbk{[include reference/algorithms/append.qbk]}
  294. }
  295. */
  296. template <typename Geometry, typename RangeOrPoint>
  297. inline void append(Geometry& geometry, RangeOrPoint const& range_or_point,
  298. int ring_index = -1, int multi_index = 0)
  299. {
  300. resolve_variant::append<Geometry>
  301. ::apply(geometry, range_or_point, ring_index, multi_index);
  302. }
  303. }} // namespace boost::geometry
  304. #endif // BOOST_GEOMETRY_ALGORITHMS_APPEND_HPP