]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/geometry/algorithms/detail/is_simple/linear.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / geometry / algorithms / detail / is_simple / linear.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
3 // Copyright (c) 2014-2017, Oracle and/or its affiliates.
4
5 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
6 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
7
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
10
11 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_SIMPLE_LINEAR_HPP
12 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_SIMPLE_LINEAR_HPP
13
14 #include <algorithm>
15 #include <deque>
16
17 #include <boost/range.hpp>
18
19 #include <boost/geometry/core/assert.hpp>
20 #include <boost/geometry/core/closure.hpp>
21 #include <boost/geometry/core/coordinate_type.hpp>
22 #include <boost/geometry/core/point_type.hpp>
23 #include <boost/geometry/core/tag.hpp>
24 #include <boost/geometry/core/tags.hpp>
25
26 #include <boost/geometry/util/range.hpp>
27
28 #include <boost/geometry/policies/predicate_based_interrupt_policy.hpp>
29 #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
30 #include <boost/geometry/policies/robustness/segment_ratio.hpp>
31
32 #include <boost/geometry/algorithms/equals.hpp>
33 #include <boost/geometry/algorithms/intersects.hpp>
34 #include <boost/geometry/algorithms/not_implemented.hpp>
35
36 #include <boost/geometry/algorithms/detail/check_iterator_range.hpp>
37 #include <boost/geometry/algorithms/detail/signed_size_type.hpp>
38
39 #include <boost/geometry/algorithms/detail/disjoint/linear_linear.hpp>
40 #include <boost/geometry/algorithms/detail/overlay/get_turn_info.hpp>
41 #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
42 #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
43 #include <boost/geometry/algorithms/detail/is_valid/has_duplicates.hpp>
44 #include <boost/geometry/algorithms/detail/is_valid/has_spikes.hpp>
45
46 #include <boost/geometry/algorithms/detail/is_simple/debug_print_boundary_points.hpp>
47 #include <boost/geometry/algorithms/detail/is_simple/failure_policy.hpp>
48 #include <boost/geometry/algorithms/detail/is_valid/debug_print_turns.hpp>
49
50 #include <boost/geometry/algorithms/dispatch/is_simple.hpp>
51
52 #include <boost/geometry/strategies/intersection.hpp>
53
54
55 namespace boost { namespace geometry
56 {
57
58
59 #ifndef DOXYGEN_NO_DETAIL
60 namespace detail { namespace is_simple
61 {
62
63
64 template <typename Turn>
65 inline bool check_segment_indices(Turn const& turn,
66 signed_size_type last_index)
67 {
68 return
69 (turn.operations[0].seg_id.segment_index == 0
70 && turn.operations[1].seg_id.segment_index == last_index)
71 ||
72 (turn.operations[0].seg_id.segment_index == 0
73 && turn.operations[1].seg_id.segment_index == last_index);
74 }
75
76
77 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
78 class is_acceptable_turn
79 : not_implemented<Geometry>
80 {};
81
82 template <typename Linestring>
83 class is_acceptable_turn<Linestring, linestring_tag>
84 {
85 public:
86 is_acceptable_turn(Linestring const& linestring)
87 : m_linestring(linestring)
88 , m_is_closed(geometry::equals(range::front(linestring),
89 range::back(linestring)))
90 {}
91
92 template <typename Turn>
93 inline bool apply(Turn const& turn) const
94 {
95 BOOST_GEOMETRY_ASSERT(boost::size(m_linestring) > 1);
96 return m_is_closed
97 && turn.method == overlay::method_none
98 && check_segment_indices(turn, boost::size(m_linestring) - 2)
99 && turn.operations[0].fraction.is_zero();
100 }
101
102 private:
103 Linestring const& m_linestring;
104 bool const m_is_closed;
105 };
106
107 template <typename MultiLinestring>
108 class is_acceptable_turn<MultiLinestring, multi_linestring_tag>
109 {
110 private:
111 typedef typename boost::range_value<MultiLinestring>::type linestring_type;
112 typedef is_acceptable_turn<linestring_type> base_type;
113
114 template <typename Point, typename Linestring>
115 static inline bool is_boundary_point_of(Point const& point,
116 Linestring const& linestring)
117 {
118 BOOST_GEOMETRY_ASSERT(boost::size(linestring) > 1);
119 return
120 ! geometry::equals(range::front(linestring),
121 range::back(linestring))
122 &&
123 (geometry::equals(point, range::front(linestring))
124 || geometry::equals(point, range::back(linestring)));
125 }
126
127 template <typename Turn, typename Linestring>
128 static inline bool is_closing_point_of(Turn const& turn,
129 Linestring const& linestring)
130 {
131 BOOST_GEOMETRY_ASSERT(boost::size(linestring) > 1);
132 return
133 turn.method == overlay::method_none
134 &&
135 check_segment_indices(turn, boost::size(linestring) - 2)
136 &&
137 geometry::equals(range::front(linestring), range::back(linestring))
138 &&
139 turn.operations[0].fraction.is_zero();
140 ;
141 }
142
143 template <typename Linestring1, typename Linestring2>
144 static inline bool have_same_boundary_points(Linestring1 const& ls1,
145 Linestring2 const& ls2)
146 {
147 return
148 geometry::equals(range::front(ls1), range::front(ls2))
149 ?
150 geometry::equals(range::back(ls1), range::back(ls2))
151 :
152 (geometry::equals(range::front(ls1), range::back(ls2))
153 &&
154 geometry::equals(range::back(ls1), range::front(ls2)))
155 ;
156 }
157
158 public:
159 is_acceptable_turn(MultiLinestring const& multilinestring)
160 : m_multilinestring(multilinestring)
161 {}
162
163 template <typename Turn>
164 inline bool apply(Turn const& turn) const
165 {
166 linestring_type const& ls1 =
167 range::at(m_multilinestring, turn.operations[0].seg_id.multi_index);
168
169 linestring_type const& ls2 =
170 range::at(m_multilinestring, turn.operations[1].seg_id.multi_index);
171
172 if (turn.operations[0].seg_id.multi_index
173 == turn.operations[1].seg_id.multi_index)
174 {
175 return is_closing_point_of(turn, ls1);
176 }
177
178 return
179 is_boundary_point_of(turn.point, ls1)
180 && is_boundary_point_of(turn.point, ls2)
181 &&
182 ( boost::size(ls1) != 2
183 || boost::size(ls2) != 2
184 || ! have_same_boundary_points(ls1, ls2) );
185 }
186
187 private:
188 MultiLinestring const& m_multilinestring;
189 };
190
191
192 template <typename Linear, typename Strategy>
193 inline bool has_self_intersections(Linear const& linear, Strategy const& strategy)
194 {
195 typedef typename point_type<Linear>::type point_type;
196
197 // compute self turns
198 typedef detail::overlay::turn_info
199 <
200 point_type,
201 geometry::segment_ratio
202 <
203 typename geometry::coordinate_type<point_type>::type
204 >
205 > turn_info;
206
207 std::deque<turn_info> turns;
208
209 typedef detail::overlay::get_turn_info
210 <
211 detail::disjoint::assign_disjoint_policy
212 > turn_policy;
213
214 is_acceptable_turn<Linear> predicate(linear);
215 detail::overlay::predicate_based_interrupt_policy
216 <
217 is_acceptable_turn<Linear>
218 > interrupt_policy(predicate);
219
220 // TODO: skip_adjacent should be set to false
221 detail::self_get_turn_points::get_turns
222 <
223 false, turn_policy
224 >::apply(linear,
225 strategy,
226 detail::no_rescale_policy(),
227 turns,
228 interrupt_policy, 0, true);
229
230 detail::is_valid::debug_print_turns(turns.begin(), turns.end());
231 debug_print_boundary_points(linear);
232
233 return interrupt_policy.has_intersections;
234 }
235
236
237 template <typename Linestring, bool CheckSelfIntersections = true>
238 struct is_simple_linestring
239 {
240 template <typename Strategy>
241 static inline bool apply(Linestring const& linestring,
242 Strategy const& strategy)
243 {
244 simplicity_failure_policy policy;
245 return ! boost::empty(linestring)
246 && ! detail::is_valid::has_duplicates
247 <
248 Linestring, closed
249 >::apply(linestring, policy)
250 && ! detail::is_valid::has_spikes
251 <
252 Linestring, closed
253 >::apply(linestring, policy, strategy.get_side_strategy());
254 }
255 };
256
257 template <typename Linestring>
258 struct is_simple_linestring<Linestring, true>
259 {
260 template <typename Strategy>
261 static inline bool apply(Linestring const& linestring,
262 Strategy const& strategy)
263 {
264 return is_simple_linestring
265 <
266 Linestring, false
267 >::apply(linestring, strategy)
268 && ! has_self_intersections(linestring, strategy);
269 }
270 };
271
272
273 template <typename MultiLinestring>
274 struct is_simple_multilinestring
275 {
276 private:
277 template <typename Strategy>
278 struct per_linestring
279 {
280 per_linestring(Strategy const& strategy)
281 : m_strategy(strategy)
282 {}
283
284 template <typename Linestring>
285 inline bool apply(Linestring const& linestring) const
286 {
287 return detail::is_simple::is_simple_linestring
288 <
289 Linestring,
290 false // do not compute self-intersections
291 >::apply(linestring, m_strategy);
292 }
293
294 Strategy const& m_strategy;
295 };
296
297 public:
298 template <typename Strategy>
299 static inline bool apply(MultiLinestring const& multilinestring,
300 Strategy const& strategy)
301 {
302 typedef per_linestring<Strategy> per_ls;
303
304 // check each of the linestrings for simplicity
305 // but do not compute self-intersections yet; these will be
306 // computed for the entire multilinestring
307 if ( ! detail::check_iterator_range
308 <
309 per_ls, // do not compute self-intersections
310 true // allow empty multilinestring
311 >::apply(boost::begin(multilinestring),
312 boost::end(multilinestring),
313 per_ls(strategy))
314 )
315 {
316 return false;
317 }
318
319 return ! has_self_intersections(multilinestring, strategy);
320 }
321 };
322
323
324
325 }} // namespace detail::is_simple
326 #endif // DOXYGEN_NO_DETAIL
327
328
329
330 #ifndef DOXYGEN_NO_DISPATCH
331 namespace dispatch
332 {
333
334 // A linestring is a curve.
335 // A curve is simple if it does not pass through the same point twice,
336 // with the possible exception of its two endpoints
337 //
338 // Reference: OGC 06-103r4 (6.1.6.1)
339 template <typename Linestring>
340 struct is_simple<Linestring, linestring_tag>
341 : detail::is_simple::is_simple_linestring<Linestring>
342 {};
343
344
345 // A MultiLinestring is a MultiCurve
346 // A MultiCurve is simple if all of its elements are simple and the
347 // only intersections between any two elements occur at Points that
348 // are on the boundaries of both elements.
349 //
350 // Reference: OGC 06-103r4 (6.1.8.1; Fig. 9)
351 template <typename MultiLinestring>
352 struct is_simple<MultiLinestring, multi_linestring_tag>
353 : detail::is_simple::is_simple_multilinestring<MultiLinestring>
354 {};
355
356
357 } // namespace dispatch
358 #endif // DOXYGEN_NO_DISPATCH
359
360
361 }} // namespace boost::geometry
362
363
364 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_SIMPLE_LINEAR_HPP