1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2014-2015, Oracle and/or its affiliates.
5 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
7 // Licensed under the Boost Software License version 1.0.
8 // http://www.boost.org/users/license.html
10 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_SIMPLE_LINEAR_HPP
11 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_SIMPLE_LINEAR_HPP
16 #include <boost/range.hpp>
18 #include <boost/geometry/core/assert.hpp>
19 #include <boost/geometry/core/closure.hpp>
20 #include <boost/geometry/core/coordinate_type.hpp>
21 #include <boost/geometry/core/point_type.hpp>
22 #include <boost/geometry/core/tag.hpp>
23 #include <boost/geometry/core/tags.hpp>
25 #include <boost/geometry/util/range.hpp>
27 #include <boost/geometry/policies/predicate_based_interrupt_policy.hpp>
28 #include <boost/geometry/policies/robustness/no_rescale_policy.hpp>
29 #include <boost/geometry/policies/robustness/segment_ratio.hpp>
31 #include <boost/geometry/algorithms/equals.hpp>
32 #include <boost/geometry/algorithms/intersects.hpp>
33 #include <boost/geometry/algorithms/not_implemented.hpp>
35 #include <boost/geometry/algorithms/detail/check_iterator_range.hpp>
36 #include <boost/geometry/algorithms/detail/signed_size_type.hpp>
38 #include <boost/geometry/algorithms/detail/disjoint/linear_linear.hpp>
39 #include <boost/geometry/algorithms/detail/overlay/get_turn_info.hpp>
40 #include <boost/geometry/algorithms/detail/overlay/turn_info.hpp>
41 #include <boost/geometry/algorithms/detail/overlay/self_turn_points.hpp>
42 #include <boost/geometry/algorithms/detail/is_valid/has_duplicates.hpp>
43 #include <boost/geometry/algorithms/detail/is_valid/has_spikes.hpp>
45 #include <boost/geometry/algorithms/detail/is_simple/debug_print_boundary_points.hpp>
46 #include <boost/geometry/algorithms/detail/is_simple/failure_policy.hpp>
47 #include <boost/geometry/algorithms/detail/is_valid/debug_print_turns.hpp>
49 #include <boost/geometry/algorithms/dispatch/is_simple.hpp>
52 namespace boost { namespace geometry
56 #ifndef DOXYGEN_NO_DETAIL
57 namespace detail { namespace is_simple
61 template <typename Turn>
62 inline bool check_segment_indices(Turn const& turn,
63 signed_size_type last_index)
66 (turn.operations[0].seg_id.segment_index == 0
67 && turn.operations[1].seg_id.segment_index == last_index)
69 (turn.operations[0].seg_id.segment_index == 0
70 && turn.operations[1].seg_id.segment_index == last_index);
74 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
75 class is_acceptable_turn
76 : not_implemented<Geometry>
79 template <typename Linestring>
80 class is_acceptable_turn<Linestring, linestring_tag>
83 is_acceptable_turn(Linestring const& linestring)
84 : m_linestring(linestring)
85 , m_is_closed(geometry::equals(range::front(linestring),
86 range::back(linestring)))
89 template <typename Turn>
90 inline bool apply(Turn const& turn) const
92 BOOST_GEOMETRY_ASSERT(boost::size(m_linestring) > 1);
94 && turn.method == overlay::method_none
95 && check_segment_indices(turn, boost::size(m_linestring) - 2)
96 && turn.operations[0].fraction.is_zero();
100 Linestring const& m_linestring;
101 bool const m_is_closed;
104 template <typename MultiLinestring>
105 class is_acceptable_turn<MultiLinestring, multi_linestring_tag>
108 typedef typename boost::range_value<MultiLinestring>::type linestring_type;
109 typedef is_acceptable_turn<linestring_type> base_type;
111 template <typename Point, typename Linestring>
112 static inline bool is_boundary_point_of(Point const& point,
113 Linestring const& linestring)
115 BOOST_GEOMETRY_ASSERT(boost::size(linestring) > 1);
117 ! geometry::equals(range::front(linestring),
118 range::back(linestring))
120 (geometry::equals(point, range::front(linestring))
121 || geometry::equals(point, range::back(linestring)));
124 template <typename Turn, typename Linestring>
125 static inline bool is_closing_point_of(Turn const& turn,
126 Linestring const& linestring)
128 BOOST_GEOMETRY_ASSERT(boost::size(linestring) > 1);
130 turn.method == overlay::method_none
132 check_segment_indices(turn, boost::size(linestring) - 2)
134 geometry::equals(range::front(linestring), range::back(linestring))
136 turn.operations[0].fraction.is_zero();
140 template <typename Linestring1, typename Linestring2>
141 static inline bool have_same_boundary_points(Linestring1 const& ls1,
142 Linestring2 const& ls2)
145 geometry::equals(range::front(ls1), range::front(ls2))
147 geometry::equals(range::back(ls1), range::back(ls2))
149 (geometry::equals(range::front(ls1), range::back(ls2))
151 geometry::equals(range::back(ls1), range::front(ls2)))
156 is_acceptable_turn(MultiLinestring const& multilinestring)
157 : m_multilinestring(multilinestring)
160 template <typename Turn>
161 inline bool apply(Turn const& turn) const
163 linestring_type const& ls1 =
164 range::at(m_multilinestring, turn.operations[0].seg_id.multi_index);
166 linestring_type const& ls2 =
167 range::at(m_multilinestring, turn.operations[1].seg_id.multi_index);
169 if (turn.operations[0].seg_id.multi_index
170 == turn.operations[1].seg_id.multi_index)
172 return is_closing_point_of(turn, ls1);
176 is_boundary_point_of(turn.point, ls1)
177 && is_boundary_point_of(turn.point, ls2)
179 ( boost::size(ls1) != 2
180 || boost::size(ls2) != 2
181 || ! have_same_boundary_points(ls1, ls2) );
185 MultiLinestring const& m_multilinestring;
189 template <typename Linear>
190 inline bool has_self_intersections(Linear const& linear)
192 typedef typename point_type<Linear>::type point_type;
194 // compute self turns
195 typedef detail::overlay::turn_info
198 geometry::segment_ratio
200 typename geometry::coordinate_type<point_type>::type
204 std::deque<turn_info> turns;
206 typedef detail::overlay::get_turn_info
208 detail::disjoint::assign_disjoint_policy
211 is_acceptable_turn<Linear> predicate(linear);
212 detail::overlay::predicate_based_interrupt_policy
214 is_acceptable_turn<Linear>
215 > interrupt_policy(predicate);
217 detail::self_get_turn_points::get_turns
221 detail::no_rescale_policy(),
225 detail::is_valid::debug_print_turns(turns.begin(), turns.end());
226 debug_print_boundary_points(linear);
228 return interrupt_policy.has_intersections;
232 template <typename Linestring, bool CheckSelfIntersections = true>
233 struct is_simple_linestring
235 static inline bool apply(Linestring const& linestring)
237 simplicity_failure_policy policy;
238 return ! boost::empty(linestring)
239 && ! detail::is_valid::has_duplicates
242 >::apply(linestring, policy)
243 && ! detail::is_valid::has_spikes
246 >::apply(linestring, policy)
247 && ! (CheckSelfIntersections && has_self_intersections(linestring));
252 template <typename MultiLinestring>
253 struct is_simple_multilinestring
255 static inline bool apply(MultiLinestring const& multilinestring)
257 // check each of the linestrings for simplicity
258 // but do not compute self-intersections yet; these will be
259 // computed for the entire multilinestring
260 if ( ! detail::check_iterator_range
264 typename boost::range_value<MultiLinestring>::type,
265 false // do not compute self-intersections
267 true // allow empty multilinestring
268 >::apply(boost::begin(multilinestring),
269 boost::end(multilinestring))
275 return ! has_self_intersections(multilinestring);
281 }} // namespace detail::is_simple
282 #endif // DOXYGEN_NO_DETAIL
286 #ifndef DOXYGEN_NO_DISPATCH
290 // A linestring is a curve.
291 // A curve is simple if it does not pass through the same point twice,
292 // with the possible exception of its two endpoints
294 // Reference: OGC 06-103r4 (6.1.6.1)
295 template <typename Linestring>
296 struct is_simple<Linestring, linestring_tag>
297 : detail::is_simple::is_simple_linestring<Linestring>
301 // A MultiLinestring is a MultiCurve
302 // A MultiCurve is simple if all of its elements are simple and the
303 // only intersections between any two elements occur at Points that
304 // are on the boundaries of both elements.
306 // Reference: OGC 06-103r4 (6.1.8.1; Fig. 9)
307 template <typename MultiLinestring>
308 struct is_simple<MultiLinestring, multi_linestring_tag>
309 : detail::is_simple::is_simple_multilinestring<MultiLinestring>
313 } // namespace dispatch
314 #endif // DOXYGEN_NO_DISPATCH
317 }} // namespace boost::geometry
320 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_SIMPLE_LINEAR_HPP