1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 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_POLICIES_IS_VALID_FAILING_REASON_POLICY_HPP
12 #define BOOST_GEOMETRY_POLICIES_IS_VALID_FAILING_REASON_POLICY_HPP
16 #include <boost/geometry/io/dsv/write.hpp>
17 #include <boost/geometry/util/condition.hpp>
18 #include <boost/geometry/util/range.hpp>
19 #include <boost/geometry/algorithms/validity_failure_type.hpp>
20 #include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
23 namespace boost { namespace geometry
27 inline char const* validity_failure_type_message(validity_failure_type failure)
32 return "Geometry is valid";
33 case failure_few_points:
34 return "Geometry has too few points";
35 case failure_wrong_topological_dimension:
36 return "Geometry has wrong topological dimension";
37 case failure_not_closed:
38 return "Geometry is defined as closed but is open";
40 return "Geometry has spikes";
41 case failure_self_intersections:
42 return "Geometry has invalid self-intersections";
43 case failure_wrong_orientation:
44 return "Geometry has wrong orientation";
45 case failure_interior_rings_outside:
46 return "Geometry has interior rings defined outside the outer boundary";
47 case failure_nested_interior_rings:
48 return "Geometry has nested interior rings";
49 case failure_disconnected_interior:
50 return "Geometry has disconnected interior";
51 case failure_intersecting_interiors:
52 return "Multi-polygon has intersecting interiors";
53 case failure_duplicate_points:
54 return "Geometry has duplicate (consecutive) points";
55 case failure_wrong_corner_order:
56 return "Box has corners in wrong order";
57 case failure_invalid_coordinate:
58 return "Geometry has point(s) with invalid coordinate(s)";
59 default: // to avoid -Wreturn-type warning
65 template <bool AllowDuplicates = true, bool AllowSpikes = true>
66 class failing_reason_policy
70 validity_failure_type transform_failure_type(validity_failure_type failure)
72 if (BOOST_GEOMETRY_CONDITION(
73 AllowDuplicates && failure == failure_duplicate_points))
81 validity_failure_type transform_failure_type(validity_failure_type failure,
84 if (BOOST_GEOMETRY_CONDITION(
85 is_linear && AllowSpikes && failure == failure_spikes))
89 return transform_failure_type(failure);
92 inline void set_failure_message(validity_failure_type failure)
96 m_oss << validity_failure_type_message(failure);
101 validity_failure_type Failure,
103 typename Data2 = Data1,
104 typename Dummy = void
108 static inline void apply(std::ostringstream&, Data1 const&)
112 static inline void apply(std::ostringstream&,
119 template <typename SpikePoint>
120 struct process_data<failure_spikes, bool, SpikePoint>
122 static inline void apply(std::ostringstream& oss,
124 SpikePoint const& spike_point)
126 if (BOOST_GEOMETRY_CONDITION(is_linear && AllowSpikes))
131 oss << ". A spike point was found with apex at "
132 << geometry::dsv(spike_point);
136 template <typename Turns>
137 struct process_data<failure_self_intersections, Turns>
140 void apply_to_segment_identifier(std::ostringstream& oss,
141 segment_identifier seg_id)
143 oss << "{" << seg_id.source_index
144 << ", " << seg_id.multi_index
145 << ", " << seg_id.ring_index
146 << ", " << seg_id.segment_index
150 static inline void apply(std::ostringstream& oss,
153 typedef typename boost::range_value<Turns>::type turn_type;
154 turn_type const& turn = range::front(turns);
155 oss << ". A self-intersection point was found at "
156 << geometry::dsv(turn.point);
158 oss << "; method: " << method_char(turn.method)
160 << operation_char(turn.operations[0].operation)
162 << operation_char(turn.operations[1].operation)
163 << "; segment IDs {source, multi, ring, segment}: ";
164 apply_to_segment_identifier(oss, turn.operations[0].seg_id);
166 apply_to_segment_identifier(oss, turn.operations[1].seg_id);
170 template <typename Point>
171 struct process_data<failure_duplicate_points, Point>
173 static inline void apply(std::ostringstream& oss,
176 if (BOOST_GEOMETRY_CONDITION(AllowDuplicates))
180 oss << ". Duplicate points were found near point "
181 << geometry::dsv(point);
186 failing_reason_policy(std::ostringstream& oss)
190 template <validity_failure_type Failure>
193 validity_failure_type const failure = transform_failure_type(Failure);
194 set_failure_message(failure);
195 return failure == no_failure;
198 template <validity_failure_type Failure, typename Data>
199 inline bool apply(Data const& data)
201 validity_failure_type const failure = transform_failure_type(Failure);
202 set_failure_message(failure);
203 process_data<Failure, Data>::apply(m_oss, data);
204 return failure == no_failure;
207 template <validity_failure_type Failure, typename Data1, typename Data2>
208 inline bool apply(Data1 const& data1, Data2 const& data2)
210 validity_failure_type const failure
211 = transform_failure_type(Failure, data1);
212 set_failure_message(failure);
213 process_data<Failure, Data1, Data2>::apply(m_oss, data1, data2);
214 return failure == no_failure;
218 std::ostringstream& m_oss;
222 }} // namespace boost::geometry
224 #endif // BOOST_GEOMETRY_POLICIES_IS_VALID_FAILING_REASON_POLICY_HPP