3 // Copyright (c) 2014-2021, 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_INTERFACE_HPP
12 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_INTERFACE_HPP
17 #include <boost/geometry/algorithms/detail/visit.hpp>
18 #include <boost/geometry/algorithms/dispatch/is_valid.hpp>
19 #include <boost/geometry/core/cs.hpp>
20 #include <boost/geometry/core/visit.hpp>
21 #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility
22 #include <boost/geometry/geometries/concepts/check.hpp>
23 #include <boost/geometry/policies/is_valid/default_policy.hpp>
24 #include <boost/geometry/policies/is_valid/failing_reason_policy.hpp>
25 #include <boost/geometry/policies/is_valid/failure_type_policy.hpp>
26 #include <boost/geometry/strategies/default_strategy.hpp>
27 #include <boost/geometry/strategies/detail.hpp>
28 #include <boost/geometry/strategies/relate/services.hpp>
31 namespace boost { namespace geometry
34 namespace resolve_strategy
40 bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategy>::value
44 template <typename Geometry, typename VisitPolicy>
45 static inline bool apply(Geometry const& geometry,
47 Strategy const& strategy)
49 return dispatch::is_valid<Geometry>::apply(geometry, visitor, strategy);
54 template <typename Strategy>
55 struct is_valid<Strategy, false>
57 template <typename Geometry, typename VisitPolicy>
58 static inline bool apply(Geometry const& geometry,
60 Strategy const& strategy)
62 using strategies::relate::services::strategy_converter;
63 return dispatch::is_valid
66 >::apply(geometry, visitor,
67 strategy_converter<Strategy>::get(strategy));
72 struct is_valid<default_strategy, false>
74 template <typename Geometry, typename VisitPolicy>
75 static inline bool apply(Geometry const& geometry,
79 // NOTE: Currently the strategy is only used for Areal geometries
80 typedef typename strategies::relate::services::default_strategy
83 >::type strategy_type;
85 return dispatch::is_valid<Geometry>::apply(geometry, visitor,
90 } // namespace resolve_strategy
92 namespace resolve_dynamic
95 template <typename Geometry, typename Tag = typename tag<Geometry>::type>
98 template <typename VisitPolicy, typename Strategy>
99 static inline bool apply(Geometry const& geometry,
100 VisitPolicy& visitor,
101 Strategy const& strategy)
103 concepts::check<Geometry const>();
105 return resolve_strategy::is_valid
108 >::apply(geometry, visitor, strategy);
112 template <typename Geometry>
113 struct is_valid<Geometry, dynamic_geometry_tag>
115 template <typename VisitPolicy, typename Strategy>
116 static inline bool apply(Geometry const& geometry,
117 VisitPolicy& policy_visitor,
118 Strategy const& strategy)
121 traits::visit<Geometry>::apply([&](auto const& g)
123 result = is_valid<util::remove_cref_t<decltype(g)>>::apply(g, policy_visitor, strategy);
129 template <typename Geometry>
130 struct is_valid<Geometry, geometry_collection_tag>
132 template <typename VisitPolicy, typename Strategy>
133 static inline bool apply(Geometry const& geometry,
134 VisitPolicy& policy_visitor,
135 Strategy const& strategy)
138 detail::visit_breadth_first([&](auto const& g)
140 result = is_valid<util::remove_cref_t<decltype(g)>>::apply(g, policy_visitor, strategy);
147 } // namespace resolve_dynamic
150 // Undocumented for now
151 template <typename Geometry, typename VisitPolicy, typename Strategy>
152 inline bool is_valid(Geometry const& geometry,
153 VisitPolicy& visitor,
154 Strategy const& strategy)
156 return resolve_dynamic::is_valid<Geometry>::apply(geometry, visitor, strategy);
161 \brief \brief_check{is valid (in the OGC sense)}
163 \tparam Geometry \tparam_geometry
164 \tparam Strategy \tparam_strategy{Is_valid}
165 \param geometry \param_geometry
166 \param strategy \param_strategy{is_valid}
167 \return \return_check{is valid (in the OGC sense);
168 furthermore, the following geometries are considered valid:
169 multi-geometries with no elements,
170 linear geometries containing spikes,
171 areal geometries with duplicate (consecutive) points}
173 \qbk{distinguish,with strategy}
174 \qbk{[include reference/algorithms/is_valid.qbk]}
176 template <typename Geometry, typename Strategy>
177 inline bool is_valid(Geometry const& geometry, Strategy const& strategy)
179 is_valid_default_policy<> visitor;
180 return resolve_dynamic::is_valid<Geometry>::apply(geometry, visitor, strategy);
184 \brief \brief_check{is valid (in the OGC sense)}
186 \tparam Geometry \tparam_geometry
187 \param geometry \param_geometry
188 \return \return_check{is valid (in the OGC sense);
189 furthermore, the following geometries are considered valid:
190 multi-geometries with no elements,
191 linear geometries containing spikes,
192 areal geometries with duplicate (consecutive) points}
194 \qbk{[include reference/algorithms/is_valid.qbk]}
196 template <typename Geometry>
197 inline bool is_valid(Geometry const& geometry)
199 return is_valid(geometry, default_strategy());
204 \brief \brief_check{is valid (in the OGC sense)}
206 \tparam Geometry \tparam_geometry
207 \tparam Strategy \tparam_strategy{Is_valid}
208 \param geometry \param_geometry
209 \param failure An enumeration value indicating that the geometry is
210 valid or not, and if not valid indicating the reason why
211 \param strategy \param_strategy{is_valid}
212 \return \return_check{is valid (in the OGC sense);
213 furthermore, the following geometries are considered valid:
214 multi-geometries with no elements,
215 linear geometries containing spikes,
216 areal geometries with duplicate (consecutive) points}
218 \qbk{distinguish,with failure value and strategy}
219 \qbk{[include reference/algorithms/is_valid_with_failure.qbk]}
221 template <typename Geometry, typename Strategy>
222 inline bool is_valid(Geometry const& geometry, validity_failure_type& failure, Strategy const& strategy)
224 failure_type_policy<> visitor;
225 bool result = resolve_dynamic::is_valid<Geometry>::apply(geometry, visitor, strategy);
226 failure = visitor.failure();
231 \brief \brief_check{is valid (in the OGC sense)}
233 \tparam Geometry \tparam_geometry
234 \param geometry \param_geometry
235 \param failure An enumeration value indicating that the geometry is
236 valid or not, and if not valid indicating the reason why
237 \return \return_check{is valid (in the OGC sense);
238 furthermore, the following geometries are considered valid:
239 multi-geometries with no elements,
240 linear geometries containing spikes,
241 areal geometries with duplicate (consecutive) points}
243 \qbk{distinguish,with failure value}
244 \qbk{[include reference/algorithms/is_valid_with_failure.qbk]}
246 template <typename Geometry>
247 inline bool is_valid(Geometry const& geometry, validity_failure_type& failure)
249 return is_valid(geometry, failure, default_strategy());
254 \brief \brief_check{is valid (in the OGC sense)}
256 \tparam Geometry \tparam_geometry
257 \tparam Strategy \tparam_strategy{Is_valid}
258 \param geometry \param_geometry
259 \param message A string containing a message stating if the geometry
260 is valid or not, and if not valid a reason why
261 \param strategy \param_strategy{is_valid}
262 \return \return_check{is valid (in the OGC sense);
263 furthermore, the following geometries are considered valid:
264 multi-geometries with no elements,
265 linear geometries containing spikes,
266 areal geometries with duplicate (consecutive) points}
268 \qbk{distinguish,with message and strategy}
269 \qbk{[include reference/algorithms/is_valid_with_message.qbk]}
271 template <typename Geometry, typename Strategy>
272 inline bool is_valid(Geometry const& geometry, std::string& message, Strategy const& strategy)
274 std::ostringstream stream;
275 failing_reason_policy<> visitor(stream);
276 bool result = resolve_dynamic::is_valid<Geometry>::apply(geometry, visitor, strategy);
277 message = stream.str();
282 \brief \brief_check{is valid (in the OGC sense)}
284 \tparam Geometry \tparam_geometry
285 \param geometry \param_geometry
286 \param message A string containing a message stating if the geometry
287 is valid or not, and if not valid a reason why
288 \return \return_check{is valid (in the OGC sense);
289 furthermore, the following geometries are considered valid:
290 multi-geometries with no elements,
291 linear geometries containing spikes,
292 areal geometries with duplicate (consecutive) points}
294 \qbk{distinguish,with message}
295 \qbk{[include reference/algorithms/is_valid_with_message.qbk]}
297 template <typename Geometry>
298 inline bool is_valid(Geometry const& geometry, std::string& message)
300 return is_valid(geometry, message, default_strategy());
304 }} // namespace boost::geometry
306 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_INTERFACE_HPP