1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
7 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
8 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_BUFFER_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_BUFFER_HPP
19 #include <boost/numeric/conversion/cast.hpp>
21 #include <boost/range.hpp>
23 #include <boost/variant/apply_visitor.hpp>
24 #include <boost/variant/static_visitor.hpp>
25 #include <boost/variant/variant_fwd.hpp>
27 #include <boost/geometry/algorithms/clear.hpp>
28 #include <boost/geometry/algorithms/envelope.hpp>
29 #include <boost/geometry/algorithms/is_empty.hpp>
30 #include <boost/geometry/algorithms/not_implemented.hpp>
31 #include <boost/geometry/arithmetic/arithmetic.hpp>
32 #include <boost/geometry/geometries/concepts/check.hpp>
33 #include <boost/geometry/geometries/box.hpp>
34 #include <boost/geometry/util/math.hpp>
36 #include <boost/geometry/algorithms/detail/buffer/buffer_inserter.hpp>
38 namespace boost { namespace geometry
42 #ifndef DOXYGEN_NO_DETAIL
43 namespace detail { namespace buffer
46 template <typename BoxIn, typename BoxOut, typename T, std::size_t C, std::size_t D, std::size_t N>
49 typedef typename coordinate_type<BoxOut>::type coordinate_type;
51 static inline void apply(BoxIn const& box_in, T const& distance, BoxOut& box_out)
53 coordinate_type d = distance;
54 set<C, D>(box_out, get<C, D>(box_in) + d);
55 box_loop<BoxIn, BoxOut, T, C, D + 1, N>::apply(box_in, distance, box_out);
59 template <typename BoxIn, typename BoxOut, typename T, std::size_t C, std::size_t N>
60 struct box_loop<BoxIn, BoxOut, T, C, N, N>
62 static inline void apply(BoxIn const&, T const&, BoxOut&) {}
65 // Extends a box with the same amount in all directions
66 template<typename BoxIn, typename BoxOut, typename T>
67 inline void buffer_box(BoxIn const& box_in, T const& distance, BoxOut& box_out)
69 assert_dimension_equal<BoxIn, BoxOut>();
71 static const std::size_t N = dimension<BoxIn>::value;
73 box_loop<BoxIn, BoxOut, T, min_corner, 0, N>::apply(box_in, -distance, box_out);
74 box_loop<BoxIn, BoxOut, T, max_corner, 0, N>::apply(box_in, distance, box_out);
79 }} // namespace detail::buffer
80 #endif // DOXYGEN_NO_DETAIL
82 #ifndef DOXYGEN_NO_DISPATCH
90 typename TagIn = typename tag<Input>::type,
91 typename TagOut = typename tag<Output>::type
93 struct buffer: not_implemented<TagIn, TagOut>
97 template <typename BoxIn, typename BoxOut>
98 struct buffer<BoxIn, BoxOut, box_tag, box_tag>
100 template <typename Distance>
101 static inline void apply(BoxIn const& box_in, Distance const& distance,
102 Distance const& , BoxOut& box_out)
104 detail::buffer::buffer_box(box_in, distance, box_out);
108 } // namespace dispatch
109 #endif // DOXYGEN_NO_DISPATCH
112 namespace resolve_variant {
114 template <typename Geometry>
117 template <typename Distance, typename GeometryOut>
118 static inline void apply(Geometry const& geometry,
119 Distance const& distance,
120 Distance const& chord_length,
123 dispatch::buffer<Geometry, GeometryOut>::apply(geometry, distance, chord_length, out);
127 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
128 struct buffer<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
130 template <typename Distance, typename GeometryOut>
131 struct visitor: boost::static_visitor<void>
133 Distance const& m_distance;
134 Distance const& m_chord_length;
137 visitor(Distance const& distance,
138 Distance const& chord_length,
140 : m_distance(distance),
141 m_chord_length(chord_length),
145 template <typename Geometry>
146 void operator()(Geometry const& geometry) const
148 buffer<Geometry>::apply(geometry, m_distance, m_chord_length, m_out);
152 template <typename Distance, typename GeometryOut>
153 static inline void apply(
154 boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
155 Distance const& distance,
156 Distance const& chord_length,
160 boost::apply_visitor(visitor<Distance, GeometryOut>(distance, chord_length, out), geometry);
164 } // namespace resolve_variant
168 \brief \brief_calc{buffer}
170 \details \details_calc{buffer, \det_buffer}.
171 \tparam Input \tparam_geometry
172 \tparam Output \tparam_geometry
173 \tparam Distance \tparam_numeric
174 \param geometry_in \param_geometry
175 \param geometry_out \param_geometry
176 \param distance The distance to be used for the buffer
177 \param chord_length (optional) The length of the chord's in the generated arcs around points or bends
179 \qbk{[include reference/algorithms/buffer.qbk]}
181 template <typename Input, typename Output, typename Distance>
182 inline void buffer(Input const& geometry_in, Output& geometry_out,
183 Distance const& distance, Distance const& chord_length = -1)
185 concepts::check<Input const>();
186 concepts::check<Output>();
188 resolve_variant::buffer<Input>::apply(geometry_in, distance, chord_length, geometry_out);
192 \brief \brief_calc{buffer}
194 \details \details_calc{return_buffer, \det_buffer}. \details_return{buffer}.
195 \tparam Input \tparam_geometry
196 \tparam Output \tparam_geometry
197 \tparam Distance \tparam_numeric
198 \param geometry \param_geometry
199 \param distance The distance to be used for the buffer
200 \param chord_length (optional) The length of the chord's in the generated arcs
201 around points or bends (RESERVED, NOT YET USED)
202 \return \return_calc{buffer}
204 template <typename Output, typename Input, typename Distance>
205 Output return_buffer(Input const& geometry, Distance const& distance, Distance const& chord_length = -1)
207 concepts::check<Input const>();
208 concepts::check<Output>();
212 resolve_variant::buffer<Input>::apply(geometry, distance, chord_length, geometry_out);
218 \brief \brief_calc{buffer}
220 \details \details_calc{buffer, \det_buffer}.
221 \tparam GeometryIn \tparam_geometry
222 \tparam MultiPolygon \tparam_geometry{MultiPolygon}
223 \tparam DistanceStrategy A strategy defining distance (or radius)
224 \tparam SideStrategy A strategy defining creation along sides
225 \tparam JoinStrategy A strategy defining creation around convex corners
226 \tparam EndStrategy A strategy defining creation at linestring ends
227 \tparam PointStrategy A strategy defining creation around points
228 \param geometry_in \param_geometry
229 \param geometry_out output multi polygon (or std:: collection of polygons),
230 will contain a buffered version of the input geometry
231 \param distance_strategy The distance strategy to be used
232 \param side_strategy The side strategy to be used
233 \param join_strategy The join strategy to be used
234 \param end_strategy The end strategy to be used
235 \param point_strategy The point strategy to be used
237 \qbk{distinguish,with strategies}
238 \qbk{[include reference/algorithms/buffer_with_strategies.qbk]}
243 typename MultiPolygon,
244 typename DistanceStrategy,
245 typename SideStrategy,
246 typename JoinStrategy,
247 typename EndStrategy,
248 typename PointStrategy
250 inline void buffer(GeometryIn const& geometry_in,
251 MultiPolygon& geometry_out,
252 DistanceStrategy const& distance_strategy,
253 SideStrategy const& side_strategy,
254 JoinStrategy const& join_strategy,
255 EndStrategy const& end_strategy,
256 PointStrategy const& point_strategy)
258 typedef typename boost::range_value<MultiPolygon>::type polygon_type;
259 concepts::check<GeometryIn const>();
260 concepts::check<polygon_type>();
262 typedef typename point_type<GeometryIn>::type point_type;
263 typedef typename rescale_policy_type<point_type>::type rescale_policy_type;
265 geometry_out.clear();
267 if (geometry::is_empty(geometry_in))
269 // Then output geometry is kept empty as well
273 model::box<point_type> box;
274 geometry::envelope(geometry_in, box);
275 geometry::buffer(box, box, distance_strategy.max_distance(join_strategy, end_strategy));
277 rescale_policy_type rescale_policy
278 = boost::geometry::get_rescale_policy<rescale_policy_type>(box);
280 detail::buffer::buffer_inserter<polygon_type>(geometry_in, range::back_inserter(geometry_out),
290 }} // namespace boost::geometry
292 #endif // BOOST_GEOMETRY_ALGORITHMS_BUFFER_HPP