1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2014-2017, Oracle and/or its affiliates.
5 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
6 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
11 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_SIMPLE_LINEAR_HPP
12 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_SIMPLE_LINEAR_HPP
17 #include <boost/range.hpp>
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>
26 #include <boost/geometry/util/range.hpp>
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>
32 #include <boost/geometry/algorithms/equals.hpp>
33 #include <boost/geometry/algorithms/intersects.hpp>
34 #include <boost/geometry/algorithms/not_implemented.hpp>
36 #include <boost/geometry/algorithms/detail/check_iterator_range.hpp>
37 #include <boost/geometry/algorithms/detail/signed_size_type.hpp>
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>
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>
50 #include <boost/geometry/algorithms/dispatch/is_simple.hpp>
52 #include <boost/geometry/strategies/intersection.hpp>
55 namespace boost { namespace geometry
59 #ifndef DOXYGEN_NO_DETAIL
60 namespace detail { namespace is_simple
64 template <typename Turn>
65 inline bool check_segment_indices(Turn const& turn,
66 signed_size_type last_index)
69 (turn.operations[0].seg_id.segment_index == 0
70 && turn.operations[1].seg_id.segment_index == last_index)
72 (turn.operations[0].seg_id.segment_index == 0
73 && turn.operations[1].seg_id.segment_index == last_index);
77 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
78 class is_acceptable_turn
79 : not_implemented<Geometry>
82 template <typename Linestring>
83 class is_acceptable_turn<Linestring, linestring_tag>
86 is_acceptable_turn(Linestring const& linestring)
87 : m_linestring(linestring)
88 , m_is_closed(geometry::equals(range::front(linestring),
89 range::back(linestring)))
92 template <typename Turn>
93 inline bool apply(Turn const& turn) const
95 BOOST_GEOMETRY_ASSERT(boost::size(m_linestring) > 1);
97 && turn.method == overlay::method_none
98 && check_segment_indices(turn, boost::size(m_linestring) - 2)
99 && turn.operations[0].fraction.is_zero();
103 Linestring const& m_linestring;
104 bool const m_is_closed;
107 template <typename MultiLinestring>
108 class is_acceptable_turn<MultiLinestring, multi_linestring_tag>
111 typedef typename boost::range_value<MultiLinestring>::type linestring_type;
112 typedef is_acceptable_turn<linestring_type> base_type;
114 template <typename Point, typename Linestring>
115 static inline bool is_boundary_point_of(Point const& point,
116 Linestring const& linestring)
118 BOOST_GEOMETRY_ASSERT(boost::size(linestring) > 1);
120 ! geometry::equals(range::front(linestring),
121 range::back(linestring))
123 (geometry::equals(point, range::front(linestring))
124 || geometry::equals(point, range::back(linestring)));
127 template <typename Turn, typename Linestring>
128 static inline bool is_closing_point_of(Turn const& turn,
129 Linestring const& linestring)
131 BOOST_GEOMETRY_ASSERT(boost::size(linestring) > 1);
133 turn.method == overlay::method_none
135 check_segment_indices(turn, boost::size(linestring) - 2)
137 geometry::equals(range::front(linestring), range::back(linestring))
139 turn.operations[0].fraction.is_zero();
143 template <typename Linestring1, typename Linestring2>
144 static inline bool have_same_boundary_points(Linestring1 const& ls1,
145 Linestring2 const& ls2)
148 geometry::equals(range::front(ls1), range::front(ls2))
150 geometry::equals(range::back(ls1), range::back(ls2))
152 (geometry::equals(range::front(ls1), range::back(ls2))
154 geometry::equals(range::back(ls1), range::front(ls2)))
159 is_acceptable_turn(MultiLinestring const& multilinestring)
160 : m_multilinestring(multilinestring)
163 template <typename Turn>
164 inline bool apply(Turn const& turn) const
166 linestring_type const& ls1 =
167 range::at(m_multilinestring, turn.operations[0].seg_id.multi_index);
169 linestring_type const& ls2 =
170 range::at(m_multilinestring, turn.operations[1].seg_id.multi_index);
172 if (turn.operations[0].seg_id.multi_index
173 == turn.operations[1].seg_id.multi_index)
175 return is_closing_point_of(turn, ls1);
179 is_boundary_point_of(turn.point, ls1)
180 && is_boundary_point_of(turn.point, ls2)
182 ( boost::size(ls1) != 2
183 || boost::size(ls2) != 2
184 || ! have_same_boundary_points(ls1, ls2) );
188 MultiLinestring const& m_multilinestring;
192 template <typename Linear, typename Strategy>
193 inline bool has_self_intersections(Linear const& linear, Strategy const& strategy)
195 typedef typename point_type<Linear>::type point_type;
197 // compute self turns
198 typedef detail::overlay::turn_info
201 geometry::segment_ratio
203 typename geometry::coordinate_type<point_type>::type
207 std::deque<turn_info> turns;
209 typedef detail::overlay::get_turn_info
211 detail::disjoint::assign_disjoint_policy
214 is_acceptable_turn<Linear> predicate(linear);
215 detail::overlay::predicate_based_interrupt_policy
217 is_acceptable_turn<Linear>
218 > interrupt_policy(predicate);
220 // TODO: skip_adjacent should be set to false
221 detail::self_get_turn_points::get_turns
226 detail::no_rescale_policy(),
228 interrupt_policy, 0, true);
230 detail::is_valid::debug_print_turns(turns.begin(), turns.end());
231 debug_print_boundary_points(linear);
233 return interrupt_policy.has_intersections;
237 template <typename Linestring, bool CheckSelfIntersections = true>
238 struct is_simple_linestring
240 template <typename Strategy>
241 static inline bool apply(Linestring const& linestring,
242 Strategy const& strategy)
244 simplicity_failure_policy policy;
245 return ! boost::empty(linestring)
246 && ! detail::is_valid::has_duplicates
249 >::apply(linestring, policy)
250 && ! detail::is_valid::has_spikes
253 >::apply(linestring, policy, strategy.get_side_strategy());
257 template <typename Linestring>
258 struct is_simple_linestring<Linestring, true>
260 template <typename Strategy>
261 static inline bool apply(Linestring const& linestring,
262 Strategy const& strategy)
264 return is_simple_linestring
267 >::apply(linestring, strategy)
268 && ! has_self_intersections(linestring, strategy);
273 template <typename MultiLinestring>
274 struct is_simple_multilinestring
277 template <typename Strategy>
278 struct per_linestring
280 per_linestring(Strategy const& strategy)
281 : m_strategy(strategy)
284 template <typename Linestring>
285 inline bool apply(Linestring const& linestring) const
287 return detail::is_simple::is_simple_linestring
290 false // do not compute self-intersections
291 >::apply(linestring, m_strategy);
294 Strategy const& m_strategy;
298 template <typename Strategy>
299 static inline bool apply(MultiLinestring const& multilinestring,
300 Strategy const& strategy)
302 typedef per_linestring<Strategy> per_ls;
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
309 per_ls, // do not compute self-intersections
310 true // allow empty multilinestring
311 >::apply(boost::begin(multilinestring),
312 boost::end(multilinestring),
319 return ! has_self_intersections(multilinestring, strategy);
325 }} // namespace detail::is_simple
326 #endif // DOXYGEN_NO_DETAIL
330 #ifndef DOXYGEN_NO_DISPATCH
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
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>
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.
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>
357 } // namespace dispatch
358 #endif // DOXYGEN_NO_DISPATCH
361 }} // namespace boost::geometry
364 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_SIMPLE_LINEAR_HPP