3 // Copyright (c) 2017 Barend Gehrels, Amsterdam, the Netherlands.
5 // Use, modification and distribution is subject to the Boost Software License,
6 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP
10 #define BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP
14 #include <boost/range.hpp>
16 #include <boost/variant/apply_visitor.hpp>
17 #include <boost/variant/static_visitor.hpp>
18 #include <boost/variant/variant_fwd.hpp>
20 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
22 #include <boost/geometry/core/closure.hpp>
23 #include <boost/geometry/core/exterior_ring.hpp>
24 #include <boost/geometry/core/interior_rings.hpp>
25 #include <boost/geometry/core/ring_type.hpp>
26 #include <boost/geometry/core/tags.hpp>
28 #include <boost/geometry/geometries/concepts/check.hpp>
30 #include <boost/geometry/algorithms/disjoint.hpp>
31 #include <boost/geometry/algorithms/detail/multi_modify.hpp>
33 namespace boost { namespace geometry
36 // Silence warning C4127: conditional expression is constant
39 #pragma warning(disable : 4127)
42 #ifndef DOXYGEN_NO_DETAIL
43 namespace detail { namespace correct_closure
46 template <typename Geometry>
49 static inline void apply(Geometry& )
54 // Close a ring, if not closed, or open it
55 template <typename Ring>
56 struct close_or_open_ring
58 static inline void apply(Ring& r)
60 if (boost::size(r) <= 2)
65 bool const disjoint = geometry::disjoint(*boost::begin(r),
66 *(boost::end(r) - 1));
67 closure_selector const s = geometry::closure<Ring>::value;
69 if (disjoint && s == closed)
71 // Close it by adding first point
72 geometry::append(r, *boost::begin(r));
74 else if (! disjoint && s != closed)
76 // Open it by removing last point
77 geometry::traits::resize<Ring>::apply(r, boost::size(r) - 1);
82 // Close/open exterior ring and all its interior rings
83 template <typename Polygon>
84 struct close_or_open_polygon
86 typedef typename ring_type<Polygon>::type ring_type;
88 static inline void apply(Polygon& poly)
90 close_or_open_ring<ring_type>::apply(exterior_ring(poly));
92 typename interior_return_type<Polygon>::type
93 rings = interior_rings(poly);
95 for (typename detail::interior_iterator<Polygon>::type
96 it = boost::begin(rings); it != boost::end(rings); ++it)
98 close_or_open_ring<ring_type>::apply(*it);
103 }} // namespace detail::correct_closure
104 #endif // DOXYGEN_NO_DETAIL
107 #ifndef DOXYGEN_NO_DISPATCH
111 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
112 struct correct_closure: not_implemented<Tag>
115 template <typename Point>
116 struct correct_closure<Point, point_tag>
117 : detail::correct_closure::nop<Point>
120 template <typename LineString>
121 struct correct_closure<LineString, linestring_tag>
122 : detail::correct_closure::nop<LineString>
125 template <typename Segment>
126 struct correct_closure<Segment, segment_tag>
127 : detail::correct_closure::nop<Segment>
131 template <typename Box>
132 struct correct_closure<Box, box_tag>
133 : detail::correct_closure::nop<Box>
136 template <typename Ring>
137 struct correct_closure<Ring, ring_tag>
138 : detail::correct_closure::close_or_open_ring<Ring>
141 template <typename Polygon>
142 struct correct_closure<Polygon, polygon_tag>
143 : detail::correct_closure::close_or_open_polygon<Polygon>
147 template <typename MultiPoint>
148 struct correct_closure<MultiPoint, multi_point_tag>
149 : detail::correct_closure::nop<MultiPoint>
153 template <typename MultiLineString>
154 struct correct_closure<MultiLineString, multi_linestring_tag>
155 : detail::correct_closure::nop<MultiLineString>
159 template <typename Geometry>
160 struct correct_closure<Geometry, multi_polygon_tag>
161 : detail::multi_modify
164 detail::correct_closure::close_or_open_polygon
166 typename boost::range_value<Geometry>::type
172 } // namespace dispatch
173 #endif // DOXYGEN_NO_DISPATCH
176 namespace resolve_variant
179 template <typename Geometry>
180 struct correct_closure
182 static inline void apply(Geometry& geometry)
184 concepts::check<Geometry const>();
185 dispatch::correct_closure<Geometry>::apply(geometry);
189 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
190 struct correct_closure<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
192 struct visitor: boost::static_visitor<void>
194 template <typename Geometry>
195 void operator()(Geometry& geometry) const
197 correct_closure<Geometry>::apply(geometry);
202 apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& geometry)
205 boost::apply_visitor(vis, geometry);
209 } // namespace resolve_variant
213 \brief Closes or opens a geometry, according to its type
214 \details Corrects a geometry w.r.t. closure points to all rings which do not
215 have a closing point and are typed as they should have one, the first point
217 \ingroup correct_closure
218 \tparam Geometry \tparam_geometry
219 \param geometry \param_geometry which will be corrected if necessary
221 template <typename Geometry>
222 inline void correct_closure(Geometry& geometry)
224 resolve_variant::correct_closure<Geometry>::apply(geometry);
228 #if defined(_MSC_VER)
232 }} // namespace boost::geometry
235 #endif // BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP