3 // Copyright (c) 2017 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2020.
6 // Modifications copyright (c) 2020 Oracle and/or its affiliates.
7 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
9 // Use, modification and distribution is subject to the Boost Software License,
10 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
11 // http://www.boost.org/LICENSE_1_0.txt)
13 #ifndef BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP
14 #define BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP
18 #include <boost/range/begin.hpp>
19 #include <boost/range/end.hpp>
20 #include <boost/range/size.hpp>
21 #include <boost/range/value_type.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/detail/interior_iterator.hpp>
29 #include <boost/geometry/core/closure.hpp>
30 #include <boost/geometry/core/exterior_ring.hpp>
31 #include <boost/geometry/core/interior_rings.hpp>
32 #include <boost/geometry/core/ring_type.hpp>
33 #include <boost/geometry/core/tags.hpp>
35 #include <boost/geometry/geometries/concepts/check.hpp>
37 #include <boost/geometry/algorithms/disjoint.hpp>
38 #include <boost/geometry/algorithms/detail/multi_modify.hpp>
40 namespace boost { namespace geometry
43 // Silence warning C4127: conditional expression is constant
46 #pragma warning(disable : 4127)
49 #ifndef DOXYGEN_NO_DETAIL
50 namespace detail { namespace correct_closure
53 template <typename Geometry>
56 static inline void apply(Geometry& )
61 // Close a ring, if not closed, or open it
62 template <typename Ring>
63 struct close_or_open_ring
65 static inline void apply(Ring& r)
67 if (boost::size(r) <= 2)
72 bool const disjoint = geometry::disjoint(*boost::begin(r),
73 *(boost::end(r) - 1));
74 closure_selector const s = geometry::closure<Ring>::value;
76 if (disjoint && s == closed)
78 // Close it by adding first point
79 geometry::append(r, *boost::begin(r));
81 else if (! disjoint && s != closed)
83 // Open it by removing last point
84 geometry::traits::resize<Ring>::apply(r, boost::size(r) - 1);
89 // Close/open exterior ring and all its interior rings
90 template <typename Polygon>
91 struct close_or_open_polygon
93 typedef typename ring_type<Polygon>::type ring_type;
95 static inline void apply(Polygon& poly)
97 close_or_open_ring<ring_type>::apply(exterior_ring(poly));
99 typename interior_return_type<Polygon>::type
100 rings = interior_rings(poly);
102 for (typename detail::interior_iterator<Polygon>::type
103 it = boost::begin(rings); it != boost::end(rings); ++it)
105 close_or_open_ring<ring_type>::apply(*it);
110 }} // namespace detail::correct_closure
111 #endif // DOXYGEN_NO_DETAIL
114 #ifndef DOXYGEN_NO_DISPATCH
118 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
119 struct correct_closure: not_implemented<Tag>
122 template <typename Point>
123 struct correct_closure<Point, point_tag>
124 : detail::correct_closure::nop<Point>
127 template <typename LineString>
128 struct correct_closure<LineString, linestring_tag>
129 : detail::correct_closure::nop<LineString>
132 template <typename Segment>
133 struct correct_closure<Segment, segment_tag>
134 : detail::correct_closure::nop<Segment>
138 template <typename Box>
139 struct correct_closure<Box, box_tag>
140 : detail::correct_closure::nop<Box>
143 template <typename Ring>
144 struct correct_closure<Ring, ring_tag>
145 : detail::correct_closure::close_or_open_ring<Ring>
148 template <typename Polygon>
149 struct correct_closure<Polygon, polygon_tag>
150 : detail::correct_closure::close_or_open_polygon<Polygon>
154 template <typename MultiPoint>
155 struct correct_closure<MultiPoint, multi_point_tag>
156 : detail::correct_closure::nop<MultiPoint>
160 template <typename MultiLineString>
161 struct correct_closure<MultiLineString, multi_linestring_tag>
162 : detail::correct_closure::nop<MultiLineString>
166 template <typename Geometry>
167 struct correct_closure<Geometry, multi_polygon_tag>
168 : detail::multi_modify
171 detail::correct_closure::close_or_open_polygon
173 typename boost::range_value<Geometry>::type
179 } // namespace dispatch
180 #endif // DOXYGEN_NO_DISPATCH
183 namespace resolve_variant
186 template <typename Geometry>
187 struct correct_closure
189 static inline void apply(Geometry& geometry)
191 concepts::check<Geometry const>();
192 dispatch::correct_closure<Geometry>::apply(geometry);
196 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
197 struct correct_closure<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
199 struct visitor: boost::static_visitor<void>
201 template <typename Geometry>
202 void operator()(Geometry& geometry) const
204 correct_closure<Geometry>::apply(geometry);
209 apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& geometry)
212 boost::apply_visitor(vis, geometry);
216 } // namespace resolve_variant
220 \brief Closes or opens a geometry, according to its type
221 \details Corrects a geometry w.r.t. closure points to all rings which do not
222 have a closing point and are typed as they should have one, the first point
224 \ingroup correct_closure
225 \tparam Geometry \tparam_geometry
226 \param geometry \param_geometry which will be corrected if necessary
228 template <typename Geometry>
229 inline void correct_closure(Geometry& geometry)
231 resolve_variant::correct_closure<Geometry>::apply(geometry);
235 #if defined(_MSC_VER)
239 }} // namespace boost::geometry
242 #endif // BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP