append.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  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_ALGORITHMS_APPEND_HPP
  11. #define BOOST_GEOMETRY_ALGORITHMS_APPEND_HPP
  12. #include <boost/geometry/algorithms/num_interior_rings.hpp>
  13. #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
  14. #include <boost/geometry/core/access.hpp>
  15. #include <boost/geometry/core/mutable_range.hpp>
  16. #include <boost/geometry/core/point_type.hpp>
  17. #include <boost/geometry/core/tags.hpp>
  18. #include <boost/geometry/geometries/concepts/check.hpp>
  19. #include <boost/geometry/geometries/variant.hpp>
  20. #include <boost/range.hpp>
  21. #include <boost/variant/static_visitor.hpp>
  22. #include <boost/variant/apply_visitor.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. #ifndef DOXYGEN_NO_DETAIL
  26. namespace detail { namespace append
  27. {
  28. template <typename Geometry, typename Point>
  29. struct append_no_action
  30. {
  31. static inline void apply(Geometry& , Point const& ,
  32. int = 0, int = 0)
  33. {
  34. }
  35. };
  36. template <typename Geometry, typename Point>
  37. struct append_point
  38. {
  39. static inline void apply(Geometry& geometry, Point const& point,
  40. int = 0, int = 0)
  41. {
  42. typename geometry::point_type<Geometry>::type copy;
  43. geometry::detail::conversion::convert_point_to_point(point, copy);
  44. traits::push_back<Geometry>::apply(geometry, copy);
  45. }
  46. };
  47. template <typename Geometry, typename Range>
  48. struct append_range
  49. {
  50. typedef typename boost::range_value<Range>::type point_type;
  51. static inline void apply(Geometry& geometry, Range const& range,
  52. int = 0, int = 0)
  53. {
  54. for (typename boost::range_iterator<Range const>::type
  55. it = boost::begin(range);
  56. it != boost::end(range);
  57. ++it)
  58. {
  59. append_point<Geometry, point_type>::apply(geometry, *it);
  60. }
  61. }
  62. };
  63. template <typename Polygon, typename Point>
  64. struct point_to_polygon
  65. {
  66. typedef typename ring_type<Polygon>::type ring_type;
  67. static inline void apply(Polygon& polygon, Point const& point,
  68. int ring_index, int = 0)
  69. {
  70. if (ring_index == -1)
  71. {
  72. append_point<ring_type, Point>::apply(
  73. exterior_ring(polygon), point);
  74. }
  75. else if (ring_index < int(num_interior_rings(polygon)))
  76. {
  77. append_point<ring_type, Point>::apply(
  78. interior_rings(polygon)[ring_index], point);
  79. }
  80. }
  81. };
  82. template <typename Polygon, typename Range>
  83. struct range_to_polygon
  84. {
  85. typedef typename ring_type<Polygon>::type ring_type;
  86. static inline void apply(Polygon& polygon, Range const& range,
  87. int ring_index, int )
  88. {
  89. if (ring_index == -1)
  90. {
  91. append_range<ring_type, Range>::apply(
  92. exterior_ring(polygon), range);
  93. }
  94. else if (ring_index < int(num_interior_rings(polygon)))
  95. {
  96. append_range<ring_type, Range>::apply(
  97. interior_rings(polygon)[ring_index], range);
  98. }
  99. }
  100. };
  101. }} // namespace detail::append
  102. #endif // DOXYGEN_NO_DETAIL
  103. #ifndef DOXYGEN_NO_DISPATCH
  104. namespace dispatch
  105. {
  106. namespace splitted_dispatch
  107. {
  108. template <typename Tag, typename Geometry, typename Point>
  109. struct append_point
  110. : detail::append::append_no_action<Geometry, Point>
  111. {};
  112. template <typename Geometry, typename Point>
  113. struct append_point<linestring_tag, Geometry, Point>
  114. : detail::append::append_point<Geometry, Point>
  115. {};
  116. template <typename Geometry, typename Point>
  117. struct append_point<ring_tag, Geometry, Point>
  118. : detail::append::append_point<Geometry, Point>
  119. {};
  120. template <typename Polygon, typename Point>
  121. struct append_point<polygon_tag, Polygon, Point>
  122. : detail::append::point_to_polygon<Polygon, Point>
  123. {};
  124. template <typename Tag, typename Geometry, typename Range>
  125. struct append_range
  126. : detail::append::append_no_action<Geometry, Range>
  127. {};
  128. template <typename Geometry, typename Range>
  129. struct append_range<linestring_tag, Geometry, Range>
  130. : detail::append::append_range<Geometry, Range>
  131. {};
  132. template <typename Geometry, typename Range>
  133. struct append_range<ring_tag, Geometry, Range>
  134. : detail::append::append_range<Geometry, Range>
  135. {};
  136. template <typename Polygon, typename Range>
  137. struct append_range<polygon_tag, Polygon, Range>
  138. : detail::append::range_to_polygon<Polygon, Range>
  139. {};
  140. } // namespace splitted_dispatch
  141. // Default: append a range (or linestring or ring or whatever) to any geometry
  142. template
  143. <
  144. typename Geometry, typename RangeOrPoint,
  145. typename TagRangeOrPoint = typename tag<RangeOrPoint>::type
  146. >
  147. struct append
  148. : splitted_dispatch::append_range<typename tag<Geometry>::type, Geometry, RangeOrPoint>
  149. {};
  150. // Specialization for point to append a point to any geometry
  151. template <typename Geometry, typename RangeOrPoint>
  152. struct append<Geometry, RangeOrPoint, point_tag>
  153. : splitted_dispatch::append_point<typename tag<Geometry>::type, Geometry, RangeOrPoint>
  154. {};
  155. template <typename Geometry>
  156. struct devarianted_append
  157. {
  158. template <typename RangeOrPoint>
  159. static inline void apply(Geometry& geometry,
  160. RangeOrPoint const& range_or_point,
  161. int ring_index,
  162. int multi_index)
  163. {
  164. concept::check<Geometry>();
  165. append<Geometry, RangeOrPoint>::apply(geometry,
  166. range_or_point,
  167. ring_index,
  168. multi_index);
  169. }
  170. };
  171. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  172. struct devarianted_append<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  173. {
  174. template <typename RangeOrPoint>
  175. struct visitor: boost::static_visitor<void>
  176. {
  177. RangeOrPoint const& m_range_or_point;
  178. int m_ring_index;
  179. int m_multi_index;
  180. visitor(RangeOrPoint const& range_or_point,
  181. int ring_index,
  182. int multi_index):
  183. m_range_or_point(range_or_point),
  184. m_ring_index(ring_index),
  185. m_multi_index(multi_index)
  186. {}
  187. template <typename Geometry>
  188. void operator()(Geometry& geometry) const
  189. {
  190. concept::check<Geometry>();
  191. append<Geometry, RangeOrPoint>::apply(geometry,
  192. m_range_or_point,
  193. m_ring_index,
  194. m_multi_index);
  195. }
  196. };
  197. template <typename RangeOrPoint>
  198. static inline void apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& variant_geometry,
  199. RangeOrPoint const& range_or_point,
  200. int ring_index,
  201. int multi_index)
  202. {
  203. apply_visitor(
  204. visitor<RangeOrPoint>(
  205. range_or_point,
  206. ring_index,
  207. multi_index
  208. ),
  209. variant_geometry
  210. );
  211. }
  212. };
  213. } // namespace dispatch
  214. #endif // DOXYGEN_NO_DISPATCH
  215. /*!
  216. \brief Appends one or more points to a linestring, ring, polygon, multi-geometry
  217. \ingroup append
  218. \tparam Geometry \tparam_geometry
  219. \tparam RangeOrPoint Either a range or a point, fullfilling Boost.Range concept or Boost.Geometry Point Concept
  220. \param geometry \param_geometry
  221. \param range_or_point The point or range to add
  222. \param ring_index The index of the ring in case of a polygon:
  223. exterior ring (-1, the default) or interior ring index
  224. \param multi_index Reserved for multi polygons or multi linestrings
  225. \qbk{[include reference/algorithms/append.qbk]}
  226. }
  227. */
  228. template <typename Geometry, typename RangeOrPoint>
  229. inline void append(Geometry& geometry, RangeOrPoint const& range_or_point,
  230. int ring_index = -1, int multi_index = 0)
  231. {
  232. dispatch::devarianted_append<Geometry>
  233. ::apply(geometry, range_or_point, ring_index, multi_index);
  234. }
  235. }} // namespace boost::geometry
  236. #endif // BOOST_GEOMETRY_ALGORITHMS_APPEND_HPP