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