interface.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // Boost.Geometry
  2. // Copyright (c) 2014-2021, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  4. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  5. // Licensed under the Boost Software License version 1.0.
  6. // http://www.boost.org/users/license.html
  7. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_INTERFACE_HPP
  8. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_INTERFACE_HPP
  9. #include <sstream>
  10. #include <string>
  11. #include <boost/geometry/algorithms/detail/visit.hpp>
  12. #include <boost/geometry/algorithms/dispatch/is_valid.hpp>
  13. #include <boost/geometry/core/cs.hpp>
  14. #include <boost/geometry/core/visit.hpp>
  15. #include <boost/geometry/geometries/adapted/boost_variant.hpp> // For backward compatibility
  16. #include <boost/geometry/geometries/concepts/check.hpp>
  17. #include <boost/geometry/policies/is_valid/default_policy.hpp>
  18. #include <boost/geometry/policies/is_valid/failing_reason_policy.hpp>
  19. #include <boost/geometry/policies/is_valid/failure_type_policy.hpp>
  20. #include <boost/geometry/strategies/default_strategy.hpp>
  21. #include <boost/geometry/strategies/detail.hpp>
  22. #include <boost/geometry/strategies/relate/services.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. namespace resolve_strategy
  26. {
  27. template
  28. <
  29. typename Strategy,
  30. bool IsUmbrella = strategies::detail::is_umbrella_strategy<Strategy>::value
  31. >
  32. struct is_valid
  33. {
  34. template <typename Geometry, typename VisitPolicy>
  35. static inline bool apply(Geometry const& geometry,
  36. VisitPolicy& visitor,
  37. Strategy const& strategy)
  38. {
  39. return dispatch::is_valid<Geometry>::apply(geometry, visitor, strategy);
  40. }
  41. };
  42. template <typename Strategy>
  43. struct is_valid<Strategy, false>
  44. {
  45. template <typename Geometry, typename VisitPolicy>
  46. static inline bool apply(Geometry const& geometry,
  47. VisitPolicy& visitor,
  48. Strategy const& strategy)
  49. {
  50. using strategies::relate::services::strategy_converter;
  51. return dispatch::is_valid
  52. <
  53. Geometry
  54. >::apply(geometry, visitor,
  55. strategy_converter<Strategy>::get(strategy));
  56. }
  57. };
  58. template <>
  59. struct is_valid<default_strategy, false>
  60. {
  61. template <typename Geometry, typename VisitPolicy>
  62. static inline bool apply(Geometry const& geometry,
  63. VisitPolicy& visitor,
  64. default_strategy)
  65. {
  66. // NOTE: Currently the strategy is only used for Areal geometries
  67. typedef typename strategies::relate::services::default_strategy
  68. <
  69. Geometry, Geometry
  70. >::type strategy_type;
  71. return dispatch::is_valid<Geometry>::apply(geometry, visitor,
  72. strategy_type());
  73. }
  74. };
  75. } // namespace resolve_strategy
  76. namespace resolve_dynamic
  77. {
  78. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  79. struct is_valid
  80. {
  81. template <typename VisitPolicy, typename Strategy>
  82. static inline bool apply(Geometry const& geometry,
  83. VisitPolicy& visitor,
  84. Strategy const& strategy)
  85. {
  86. concepts::check<Geometry const>();
  87. return resolve_strategy::is_valid
  88. <
  89. Strategy
  90. >::apply(geometry, visitor, strategy);
  91. }
  92. };
  93. template <typename Geometry>
  94. struct is_valid<Geometry, dynamic_geometry_tag>
  95. {
  96. template <typename VisitPolicy, typename Strategy>
  97. static inline bool apply(Geometry const& geometry,
  98. VisitPolicy& policy_visitor,
  99. Strategy const& strategy)
  100. {
  101. bool result = true;
  102. traits::visit<Geometry>::apply([&](auto const& g)
  103. {
  104. result = is_valid<util::remove_cref_t<decltype(g)>>::apply(g, policy_visitor, strategy);
  105. }, geometry);
  106. return result;
  107. }
  108. };
  109. template <typename Geometry>
  110. struct is_valid<Geometry, geometry_collection_tag>
  111. {
  112. template <typename VisitPolicy, typename Strategy>
  113. static inline bool apply(Geometry const& geometry,
  114. VisitPolicy& policy_visitor,
  115. Strategy const& strategy)
  116. {
  117. bool result = true;
  118. detail::visit_breadth_first([&](auto const& g)
  119. {
  120. result = is_valid<util::remove_cref_t<decltype(g)>>::apply(g, policy_visitor, strategy);
  121. return result;
  122. }, geometry);
  123. return result;
  124. }
  125. };
  126. } // namespace resolve_dynamic
  127. // Undocumented for now
  128. template <typename Geometry, typename VisitPolicy, typename Strategy>
  129. inline bool is_valid(Geometry const& geometry,
  130. VisitPolicy& visitor,
  131. Strategy const& strategy)
  132. {
  133. return resolve_dynamic::is_valid<Geometry>::apply(geometry, visitor, strategy);
  134. }
  135. /*!
  136. \brief \brief_check{is valid (in the OGC sense)}
  137. \ingroup is_valid
  138. \tparam Geometry \tparam_geometry
  139. \tparam Strategy \tparam_strategy{Is_valid}
  140. \param geometry \param_geometry
  141. \param strategy \param_strategy{is_valid}
  142. \return \return_check{is valid (in the OGC sense);
  143. furthermore, the following geometries are considered valid:
  144. multi-geometries with no elements,
  145. linear geometries containing spikes,
  146. areal geometries with duplicate (consecutive) points}
  147. \qbk{distinguish,with strategy}
  148. \qbk{[include reference/algorithms/is_valid.qbk]}
  149. */
  150. template <typename Geometry, typename Strategy>
  151. inline bool is_valid(Geometry const& geometry, Strategy const& strategy)
  152. {
  153. is_valid_default_policy<> visitor;
  154. return resolve_dynamic::is_valid<Geometry>::apply(geometry, visitor, strategy);
  155. }
  156. /*!
  157. \brief \brief_check{is valid (in the OGC sense)}
  158. \ingroup is_valid
  159. \tparam Geometry \tparam_geometry
  160. \param geometry \param_geometry
  161. \return \return_check{is valid (in the OGC sense);
  162. furthermore, the following geometries are considered valid:
  163. multi-geometries with no elements,
  164. linear geometries containing spikes,
  165. areal geometries with duplicate (consecutive) points}
  166. \qbk{[include reference/algorithms/is_valid.qbk]}
  167. */
  168. template <typename Geometry>
  169. inline bool is_valid(Geometry const& geometry)
  170. {
  171. return is_valid(geometry, default_strategy());
  172. }
  173. /*!
  174. \brief \brief_check{is valid (in the OGC sense)}
  175. \ingroup is_valid
  176. \tparam Geometry \tparam_geometry
  177. \tparam Strategy \tparam_strategy{Is_valid}
  178. \param geometry \param_geometry
  179. \param failure An enumeration value indicating that the geometry is
  180. valid or not, and if not valid indicating the reason why
  181. \param strategy \param_strategy{is_valid}
  182. \return \return_check{is valid (in the OGC sense);
  183. furthermore, the following geometries are considered valid:
  184. multi-geometries with no elements,
  185. linear geometries containing spikes,
  186. areal geometries with duplicate (consecutive) points}
  187. \qbk{distinguish,with failure value and strategy}
  188. \qbk{[include reference/algorithms/is_valid_with_failure.qbk]}
  189. */
  190. template <typename Geometry, typename Strategy>
  191. inline bool is_valid(Geometry const& geometry, validity_failure_type& failure, Strategy const& strategy)
  192. {
  193. failure_type_policy<> visitor;
  194. bool result = resolve_dynamic::is_valid<Geometry>::apply(geometry, visitor, strategy);
  195. failure = visitor.failure();
  196. return result;
  197. }
  198. /*!
  199. \brief \brief_check{is valid (in the OGC sense)}
  200. \ingroup is_valid
  201. \tparam Geometry \tparam_geometry
  202. \param geometry \param_geometry
  203. \param failure An enumeration value indicating that the geometry is
  204. valid or not, and if not valid indicating the reason why
  205. \return \return_check{is valid (in the OGC sense);
  206. furthermore, the following geometries are considered valid:
  207. multi-geometries with no elements,
  208. linear geometries containing spikes,
  209. areal geometries with duplicate (consecutive) points}
  210. \qbk{distinguish,with failure value}
  211. \qbk{[include reference/algorithms/is_valid_with_failure.qbk]}
  212. */
  213. template <typename Geometry>
  214. inline bool is_valid(Geometry const& geometry, validity_failure_type& failure)
  215. {
  216. return is_valid(geometry, failure, default_strategy());
  217. }
  218. /*!
  219. \brief \brief_check{is valid (in the OGC sense)}
  220. \ingroup is_valid
  221. \tparam Geometry \tparam_geometry
  222. \tparam Strategy \tparam_strategy{Is_valid}
  223. \param geometry \param_geometry
  224. \param message A string containing a message stating if the geometry
  225. is valid or not, and if not valid a reason why
  226. \param strategy \param_strategy{is_valid}
  227. \return \return_check{is valid (in the OGC sense);
  228. furthermore, the following geometries are considered valid:
  229. multi-geometries with no elements,
  230. linear geometries containing spikes,
  231. areal geometries with duplicate (consecutive) points}
  232. \qbk{distinguish,with message and strategy}
  233. \qbk{[include reference/algorithms/is_valid_with_message.qbk]}
  234. */
  235. template <typename Geometry, typename Strategy>
  236. inline bool is_valid(Geometry const& geometry, std::string& message, Strategy const& strategy)
  237. {
  238. std::ostringstream stream;
  239. failing_reason_policy<> visitor(stream);
  240. bool result = resolve_dynamic::is_valid<Geometry>::apply(geometry, visitor, strategy);
  241. message = stream.str();
  242. return result;
  243. }
  244. /*!
  245. \brief \brief_check{is valid (in the OGC sense)}
  246. \ingroup is_valid
  247. \tparam Geometry \tparam_geometry
  248. \param geometry \param_geometry
  249. \param message A string containing a message stating if the geometry
  250. is valid or not, and if not valid a reason why
  251. \return \return_check{is valid (in the OGC sense);
  252. furthermore, the following geometries are considered valid:
  253. multi-geometries with no elements,
  254. linear geometries containing spikes,
  255. areal geometries with duplicate (consecutive) points}
  256. \qbk{distinguish,with message}
  257. \qbk{[include reference/algorithms/is_valid_with_message.qbk]}
  258. */
  259. template <typename Geometry>
  260. inline bool is_valid(Geometry const& geometry, std::string& message)
  261. {
  262. return is_valid(geometry, message, default_strategy());
  263. }
  264. }} // namespace boost::geometry
  265. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_INTERFACE_HPP