interface.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Boost.Geometry
  2. // Copyright (c) 2014-2018, 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/variant/apply_visitor.hpp>
  12. #include <boost/variant/static_visitor.hpp>
  13. #include <boost/variant/variant_fwd.hpp>
  14. #include <boost/geometry/algorithms/dispatch/is_valid.hpp>
  15. #include <boost/geometry/core/cs.hpp>
  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/intersection.hpp>
  22. namespace boost { namespace geometry
  23. {
  24. namespace resolve_strategy
  25. {
  26. struct is_valid
  27. {
  28. template <typename Geometry, typename VisitPolicy, typename Strategy>
  29. static inline bool apply(Geometry const& geometry,
  30. VisitPolicy& visitor,
  31. Strategy const& strategy)
  32. {
  33. return dispatch::is_valid<Geometry>::apply(geometry, visitor, strategy);
  34. }
  35. template <typename Geometry, typename VisitPolicy>
  36. static inline bool apply(Geometry const& geometry,
  37. VisitPolicy& visitor,
  38. default_strategy)
  39. {
  40. // NOTE: Currently the strategy is only used for Areal geometries
  41. typedef typename strategy::intersection::services::default_strategy
  42. <
  43. typename cs_tag<Geometry>::type
  44. >::type strategy_type;
  45. return dispatch::is_valid<Geometry>::apply(geometry, visitor, strategy_type());
  46. }
  47. };
  48. } // namespace resolve_strategy
  49. namespace resolve_variant
  50. {
  51. template <typename Geometry>
  52. struct is_valid
  53. {
  54. template <typename VisitPolicy, typename Strategy>
  55. static inline bool apply(Geometry const& geometry,
  56. VisitPolicy& visitor,
  57. Strategy const& strategy)
  58. {
  59. concepts::check<Geometry const>();
  60. return resolve_strategy::is_valid::apply(geometry, visitor, strategy);
  61. }
  62. };
  63. template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
  64. struct is_valid<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
  65. {
  66. template <typename VisitPolicy, typename Strategy>
  67. struct visitor : boost::static_visitor<bool>
  68. {
  69. visitor(VisitPolicy& policy, Strategy const& strategy)
  70. : m_policy(policy)
  71. , m_strategy(strategy)
  72. {}
  73. template <typename Geometry>
  74. bool operator()(Geometry const& geometry) const
  75. {
  76. return is_valid<Geometry>::apply(geometry, m_policy, m_strategy);
  77. }
  78. VisitPolicy& m_policy;
  79. Strategy const& m_strategy;
  80. };
  81. template <typename VisitPolicy, typename Strategy>
  82. static inline bool
  83. apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
  84. VisitPolicy& policy_visitor,
  85. Strategy const& strategy)
  86. {
  87. return boost::apply_visitor(visitor<VisitPolicy, Strategy>(policy_visitor, strategy),
  88. geometry);
  89. }
  90. };
  91. } // namespace resolve_variant
  92. // Undocumented for now
  93. template <typename Geometry, typename VisitPolicy, typename Strategy>
  94. inline bool is_valid(Geometry const& geometry,
  95. VisitPolicy& visitor,
  96. Strategy const& strategy)
  97. {
  98. return resolve_variant::is_valid<Geometry>::apply(geometry, visitor, strategy);
  99. }
  100. /*!
  101. \brief \brief_check{is valid (in the OGC sense)}
  102. \ingroup is_valid
  103. \tparam Geometry \tparam_geometry
  104. \tparam Strategy \tparam_strategy{Is_valid}
  105. \param geometry \param_geometry
  106. \param strategy \param_strategy{is_valid}
  107. \return \return_check{is valid (in the OGC sense);
  108. furthermore, the following geometries are considered valid:
  109. multi-geometries with no elements,
  110. linear geometries containing spikes,
  111. areal geometries with duplicate (consecutive) points}
  112. \qbk{distinguish,with strategy}
  113. \qbk{[include reference/algorithms/is_valid.qbk]}
  114. */
  115. template <typename Geometry, typename Strategy>
  116. inline bool is_valid(Geometry const& geometry, Strategy const& strategy)
  117. {
  118. is_valid_default_policy<> visitor;
  119. return resolve_variant::is_valid<Geometry>::apply(geometry, visitor, strategy);
  120. }
  121. /*!
  122. \brief \brief_check{is valid (in the OGC sense)}
  123. \ingroup is_valid
  124. \tparam Geometry \tparam_geometry
  125. \param geometry \param_geometry
  126. \return \return_check{is valid (in the OGC sense);
  127. furthermore, the following geometries are considered valid:
  128. multi-geometries with no elements,
  129. linear geometries containing spikes,
  130. areal geometries with duplicate (consecutive) points}
  131. \qbk{[include reference/algorithms/is_valid.qbk]}
  132. */
  133. template <typename Geometry>
  134. inline bool is_valid(Geometry const& geometry)
  135. {
  136. return is_valid(geometry, default_strategy());
  137. }
  138. /*!
  139. \brief \brief_check{is valid (in the OGC sense)}
  140. \ingroup is_valid
  141. \tparam Geometry \tparam_geometry
  142. \tparam Strategy \tparam_strategy{Is_valid}
  143. \param geometry \param_geometry
  144. \param failure An enumeration value indicating that the geometry is
  145. valid or not, and if not valid indicating the reason why
  146. \param strategy \param_strategy{is_valid}
  147. \return \return_check{is valid (in the OGC sense);
  148. furthermore, the following geometries are considered valid:
  149. multi-geometries with no elements,
  150. linear geometries containing spikes,
  151. areal geometries with duplicate (consecutive) points}
  152. \qbk{distinguish,with failure value and strategy}
  153. \qbk{[include reference/algorithms/is_valid_with_failure.qbk]}
  154. */
  155. template <typename Geometry, typename Strategy>
  156. inline bool is_valid(Geometry const& geometry, validity_failure_type& failure, Strategy const& strategy)
  157. {
  158. failure_type_policy<> visitor;
  159. bool result = resolve_variant::is_valid<Geometry>::apply(geometry, visitor, strategy);
  160. failure = visitor.failure();
  161. return result;
  162. }
  163. /*!
  164. \brief \brief_check{is valid (in the OGC sense)}
  165. \ingroup is_valid
  166. \tparam Geometry \tparam_geometry
  167. \param geometry \param_geometry
  168. \param failure An enumeration value indicating that the geometry is
  169. valid or not, and if not valid indicating the reason why
  170. \return \return_check{is valid (in the OGC sense);
  171. furthermore, the following geometries are considered valid:
  172. multi-geometries with no elements,
  173. linear geometries containing spikes,
  174. areal geometries with duplicate (consecutive) points}
  175. \qbk{distinguish,with failure value}
  176. \qbk{[include reference/algorithms/is_valid_with_failure.qbk]}
  177. */
  178. template <typename Geometry>
  179. inline bool is_valid(Geometry const& geometry, validity_failure_type& failure)
  180. {
  181. return is_valid(geometry, failure, default_strategy());
  182. }
  183. /*!
  184. \brief \brief_check{is valid (in the OGC sense)}
  185. \ingroup is_valid
  186. \tparam Geometry \tparam_geometry
  187. \tparam Strategy \tparam_strategy{Is_valid}
  188. \param geometry \param_geometry
  189. \param message A string containing a message stating if the geometry
  190. is valid or not, and if not valid a reason why
  191. \param strategy \param_strategy{is_valid}
  192. \return \return_check{is valid (in the OGC sense);
  193. furthermore, the following geometries are considered valid:
  194. multi-geometries with no elements,
  195. linear geometries containing spikes,
  196. areal geometries with duplicate (consecutive) points}
  197. \qbk{distinguish,with message and strategy}
  198. \qbk{[include reference/algorithms/is_valid_with_message.qbk]}
  199. */
  200. template <typename Geometry, typename Strategy>
  201. inline bool is_valid(Geometry const& geometry, std::string& message, Strategy const& strategy)
  202. {
  203. std::ostringstream stream;
  204. failing_reason_policy<> visitor(stream);
  205. bool result = resolve_variant::is_valid<Geometry>::apply(geometry, visitor, strategy);
  206. message = stream.str();
  207. return result;
  208. }
  209. /*!
  210. \brief \brief_check{is valid (in the OGC sense)}
  211. \ingroup is_valid
  212. \tparam Geometry \tparam_geometry
  213. \param geometry \param_geometry
  214. \param message A string containing a message stating if the geometry
  215. is valid or not, and if not valid a reason why
  216. \return \return_check{is valid (in the OGC sense);
  217. furthermore, the following geometries are considered valid:
  218. multi-geometries with no elements,
  219. linear geometries containing spikes,
  220. areal geometries with duplicate (consecutive) points}
  221. \qbk{distinguish,with message}
  222. \qbk{[include reference/algorithms/is_valid_with_message.qbk]}
  223. */
  224. template <typename Geometry>
  225. inline bool is_valid(Geometry const& geometry, std::string& message)
  226. {
  227. return is_valid(geometry, message, default_strategy());
  228. }
  229. }} // namespace boost::geometry
  230. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_INTERFACE_HPP