append.hpp 11 KB

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