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
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_VALID_LINEAR_HPP
12 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_LINEAR_HPP
16 #include <boost/range.hpp>
18 #include <boost/geometry/core/closure.hpp>
19 #include <boost/geometry/core/point_type.hpp>
20 #include <boost/geometry/core/tags.hpp>
22 #include <boost/geometry/util/condition.hpp>
23 #include <boost/geometry/util/range.hpp>
25 #include <boost/geometry/algorithms/equals.hpp>
26 #include <boost/geometry/algorithms/validity_failure_type.hpp>
27 #include <boost/geometry/algorithms/detail/check_iterator_range.hpp>
28 #include <boost/geometry/algorithms/detail/is_valid/has_invalid_coordinate.hpp>
29 #include <boost/geometry/algorithms/detail/is_valid/has_spikes.hpp>
30 #include <boost/geometry/algorithms/detail/num_distinct_consecutive_points.hpp>
32 #include <boost/geometry/algorithms/dispatch/is_valid.hpp>
35 namespace boost { namespace geometry
38 #ifndef DOXYGEN_NO_DETAIL
39 namespace detail { namespace is_valid
43 template <typename Linestring>
44 struct is_valid_linestring
46 template <typename VisitPolicy>
47 static inline bool apply(Linestring const& linestring,
50 if (has_invalid_coordinate<Linestring>::apply(linestring, visitor))
55 if (boost::size(linestring) < 2)
57 return visitor.template apply<failure_few_points>();
60 std::size_t num_distinct = detail::num_distinct_consecutive_points
65 not_equal_to<typename point_type<Linestring>::type>
68 if (num_distinct < 2u)
71 visitor.template apply<failure_wrong_topological_dimension>();
74 if (num_distinct == 2u)
76 return visitor.template apply<no_failure>();
78 return ! has_spikes<Linestring, closed>::apply(linestring, visitor);
83 }} // namespace detail::is_valid
84 #endif // DOXYGEN_NO_DETAIL
89 #ifndef DOXYGEN_NO_DISPATCH
94 // A linestring is a curve.
95 // A curve is 1-dimensional so it has to have at least two distinct
97 // A curve is simple if it does not pass through the same point twice,
98 // with the possible exception of its two endpoints
100 // There is an option here as to whether spikes are allowed for linestrings;
101 // here we pass this as an additional template parameter: allow_spikes
102 // If allow_spikes is set to true, spikes are allowed, false otherwise.
103 // By default, spikes are disallowed
105 // Reference: OGC 06-103r4 (6.1.6.1)
106 template <typename Linestring, bool AllowEmptyMultiGeometries>
109 Linestring, linestring_tag, AllowEmptyMultiGeometries
110 > : detail::is_valid::is_valid_linestring<Linestring>
114 // A MultiLinestring is a MultiCurve
115 // A MultiCurve is simple if all of its elements are simple and the
116 // only intersections between any two elements occur at Points that
117 // are on the boundaries of both elements.
119 // Reference: OGC 06-103r4 (6.1.8.1; Fig. 9)
120 template <typename MultiLinestring, bool AllowEmptyMultiGeometries>
123 MultiLinestring, multi_linestring_tag, AllowEmptyMultiGeometries
127 template <typename VisitPolicy>
128 struct per_linestring
130 per_linestring(VisitPolicy& policy) : m_policy(policy) {}
132 template <typename Linestring>
133 inline bool apply(Linestring const& linestring) const
135 return detail::is_valid::is_valid_linestring
138 >::apply(linestring, m_policy);
141 VisitPolicy& m_policy;
145 template <typename VisitPolicy>
146 static inline bool apply(MultiLinestring const& multilinestring,
147 VisitPolicy& visitor)
149 if (BOOST_GEOMETRY_CONDITION(
150 AllowEmptyMultiGeometries && boost::empty(multilinestring)))
152 return visitor.template apply<no_failure>();
155 return detail::check_iterator_range
157 per_linestring<VisitPolicy>,
158 false // do not check for empty multilinestring (done above)
159 >::apply(boost::begin(multilinestring),
160 boost::end(multilinestring),
161 per_linestring<VisitPolicy>(visitor));
166 } // namespace dispatch
167 #endif // DOXYGEN_NO_DISPATCH
170 }} // namespace boost::geometry
173 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_LINEAR_HPP