• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry
2 
3 // Copyright (c) 2017 Barend Gehrels, Amsterdam, the Netherlands.
4 
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)
8 
9 #ifndef BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP
10 #define BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP
11 
12 #include <cstddef>
13 
14 #include <boost/range.hpp>
15 
16 #include <boost/variant/apply_visitor.hpp>
17 #include <boost/variant/static_visitor.hpp>
18 #include <boost/variant/variant_fwd.hpp>
19 
20 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
21 
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>
27 
28 #include <boost/geometry/geometries/concepts/check.hpp>
29 
30 #include <boost/geometry/algorithms/disjoint.hpp>
31 #include <boost/geometry/algorithms/detail/multi_modify.hpp>
32 
33 namespace boost { namespace geometry
34 {
35 
36 // Silence warning C4127: conditional expression is constant
37 #if defined(_MSC_VER)
38 #pragma warning(push)
39 #pragma warning(disable : 4127)
40 #endif
41 
42 #ifndef DOXYGEN_NO_DETAIL
43 namespace detail { namespace correct_closure
44 {
45 
46 template <typename Geometry>
47 struct nop
48 {
applyboost::geometry::detail::correct_closure::nop49     static inline void apply(Geometry& )
50     {}
51 };
52 
53 
54 // Close a ring, if not closed, or open it
55 template <typename Ring>
56 struct close_or_open_ring
57 {
applyboost::geometry::detail::correct_closure::close_or_open_ring58     static inline void apply(Ring& r)
59     {
60         if (boost::size(r) <= 2)
61         {
62             return;
63         }
64 
65         bool const disjoint = geometry::disjoint(*boost::begin(r),
66                                                  *(boost::end(r) - 1));
67         closure_selector const s = geometry::closure<Ring>::value;
68 
69         if (disjoint && s == closed)
70         {
71             // Close it by adding first point
72             geometry::append(r, *boost::begin(r));
73         }
74         else if (! disjoint && s != closed)
75         {
76             // Open it by removing last point
77             geometry::traits::resize<Ring>::apply(r, boost::size(r) - 1);
78         }
79     }
80 };
81 
82 // Close/open exterior ring and all its interior rings
83 template <typename Polygon>
84 struct close_or_open_polygon
85 {
86     typedef typename ring_type<Polygon>::type ring_type;
87 
applyboost::geometry::detail::correct_closure::close_or_open_polygon88     static inline void apply(Polygon& poly)
89     {
90         close_or_open_ring<ring_type>::apply(exterior_ring(poly));
91 
92         typename interior_return_type<Polygon>::type
93             rings = interior_rings(poly);
94 
95         for (typename detail::interior_iterator<Polygon>::type
96                 it = boost::begin(rings); it != boost::end(rings); ++it)
97         {
98             close_or_open_ring<ring_type>::apply(*it);
99         }
100     }
101 };
102 
103 }} // namespace detail::correct_closure
104 #endif // DOXYGEN_NO_DETAIL
105 
106 
107 #ifndef DOXYGEN_NO_DISPATCH
108 namespace dispatch
109 {
110 
111 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
112 struct correct_closure: not_implemented<Tag>
113 {};
114 
115 template <typename Point>
116 struct correct_closure<Point, point_tag>
117     : detail::correct_closure::nop<Point>
118 {};
119 
120 template <typename LineString>
121 struct correct_closure<LineString, linestring_tag>
122     : detail::correct_closure::nop<LineString>
123 {};
124 
125 template <typename Segment>
126 struct correct_closure<Segment, segment_tag>
127     : detail::correct_closure::nop<Segment>
128 {};
129 
130 
131 template <typename Box>
132 struct correct_closure<Box, box_tag>
133     : detail::correct_closure::nop<Box>
134 {};
135 
136 template <typename Ring>
137 struct correct_closure<Ring, ring_tag>
138     : detail::correct_closure::close_or_open_ring<Ring>
139 {};
140 
141 template <typename Polygon>
142 struct correct_closure<Polygon, polygon_tag>
143     : detail::correct_closure::close_or_open_polygon<Polygon>
144 {};
145 
146 
147 template <typename MultiPoint>
148 struct correct_closure<MultiPoint, multi_point_tag>
149     : detail::correct_closure::nop<MultiPoint>
150 {};
151 
152 
153 template <typename MultiLineString>
154 struct correct_closure<MultiLineString, multi_linestring_tag>
155     : detail::correct_closure::nop<MultiLineString>
156 {};
157 
158 
159 template <typename Geometry>
160 struct correct_closure<Geometry, multi_polygon_tag>
161     : detail::multi_modify
162         <
163             Geometry,
164             detail::correct_closure::close_or_open_polygon
165                 <
166                     typename boost::range_value<Geometry>::type
167                 >
168         >
169 {};
170 
171 
172 } // namespace dispatch
173 #endif // DOXYGEN_NO_DISPATCH
174 
175 
176 namespace resolve_variant
177 {
178 
179 template <typename Geometry>
180 struct correct_closure
181 {
applyboost::geometry::resolve_variant::correct_closure182     static inline void apply(Geometry& geometry)
183     {
184         concepts::check<Geometry const>();
185         dispatch::correct_closure<Geometry>::apply(geometry);
186     }
187 };
188 
189 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
190 struct correct_closure<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
191 {
192     struct visitor: boost::static_visitor<void>
193     {
194         template <typename Geometry>
operator ()boost::geometry::resolve_variant::correct_closure::visitor195         void operator()(Geometry& geometry) const
196         {
197             correct_closure<Geometry>::apply(geometry);
198         }
199     };
200 
201     static inline void
applyboost::geometry::resolve_variant::correct_closure202     apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)>& geometry)
203     {
204         visitor vis;
205         boost::apply_visitor(vis, geometry);
206     }
207 };
208 
209 } // namespace resolve_variant
210 
211 
212 /*!
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
216     is appended.
217 \ingroup correct_closure
218 \tparam Geometry \tparam_geometry
219 \param geometry \param_geometry which will be corrected if necessary
220 */
221 template <typename Geometry>
correct_closure(Geometry & geometry)222 inline void correct_closure(Geometry& geometry)
223 {
224     resolve_variant::correct_closure<Geometry>::apply(geometry);
225 }
226 
227 
228 #if defined(_MSC_VER)
229 #pragma warning(pop)
230 #endif
231 
232 }} // namespace boost::geometry
233 
234 
235 #endif // BOOST_GEOMETRY_ALGORITHMS_CORRECT_CLOSURE_HPP
236