ring.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  3. // Copyright (c) 2014-2019, Oracle and/or its affiliates.
  4. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Licensed under the Boost Software License version 1.0.
  7. // http://www.boost.org/users/license.html
  8. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_RING_HPP
  9. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_RING_HPP
  10. #include <deque>
  11. #include <boost/core/ignore_unused.hpp>
  12. #include <boost/geometry/core/closure.hpp>
  13. #include <boost/geometry/core/cs.hpp>
  14. #include <boost/geometry/core/point_order.hpp>
  15. #include <boost/geometry/core/tags.hpp>
  16. #include <boost/geometry/util/order_as_direction.hpp>
  17. #include <boost/geometry/util/range.hpp>
  18. #include <boost/geometry/views/closeable_view.hpp>
  19. #include <boost/geometry/algorithms/area.hpp>
  20. #include <boost/geometry/algorithms/intersects.hpp>
  21. #include <boost/geometry/algorithms/validity_failure_type.hpp>
  22. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  23. #include <boost/geometry/algorithms/detail/num_distinct_consecutive_points.hpp>
  24. #include <boost/geometry/algorithms/detail/is_valid/has_duplicates.hpp>
  25. #include <boost/geometry/algorithms/detail/is_valid/has_invalid_coordinate.hpp>
  26. #include <boost/geometry/algorithms/detail/is_valid/has_spikes.hpp>
  27. #include <boost/geometry/algorithms/detail/is_valid/has_valid_self_turns.hpp>
  28. #include <boost/geometry/algorithms/dispatch/is_valid.hpp>
  29. #include <boost/geometry/strategies/area.hpp>
  30. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  31. #include <boost/geometry/io/dsv/write.hpp>
  32. #endif
  33. namespace boost { namespace geometry
  34. {
  35. #ifndef DOXYGEN_NO_DETAIL
  36. namespace detail { namespace is_valid
  37. {
  38. // struct to check whether a ring is topologically closed
  39. template <typename Ring, closure_selector Closure /* open */>
  40. struct is_topologically_closed
  41. {
  42. template <typename VisitPolicy, typename EqPPStrategy>
  43. static inline bool apply(Ring const&, VisitPolicy& visitor, EqPPStrategy const&)
  44. {
  45. boost::ignore_unused(visitor);
  46. return visitor.template apply<no_failure>();
  47. }
  48. };
  49. template <typename Ring>
  50. struct is_topologically_closed<Ring, closed>
  51. {
  52. template <typename VisitPolicy, typename EqPPStrategy>
  53. static inline bool apply(Ring const& ring, VisitPolicy& visitor, EqPPStrategy const&)
  54. {
  55. boost::ignore_unused(visitor);
  56. if (geometry::detail::equals::equals_point_point(range::front(ring),
  57. range::back(ring),
  58. EqPPStrategy()))
  59. {
  60. return visitor.template apply<no_failure>();
  61. }
  62. else
  63. {
  64. return visitor.template apply<failure_not_closed>();
  65. }
  66. }
  67. };
  68. template <typename ResultType, bool IsInteriorRing /* false */>
  69. struct ring_area_predicate
  70. {
  71. typedef std::greater<ResultType> type;
  72. };
  73. template <typename ResultType>
  74. struct ring_area_predicate<ResultType, true>
  75. {
  76. typedef std::less<ResultType> type;
  77. };
  78. template <typename Ring, bool IsInteriorRing>
  79. struct is_properly_oriented
  80. {
  81. template <typename VisitPolicy, typename Strategy>
  82. static inline bool apply(Ring const& ring, VisitPolicy& visitor,
  83. Strategy const& strategy)
  84. {
  85. boost::ignore_unused(visitor);
  86. typedef detail::area::ring_area
  87. <
  88. order_as_direction<geometry::point_order<Ring>::value>::value,
  89. geometry::closure<Ring>::value
  90. > ring_area_type;
  91. typedef typename Strategy::template area_strategy
  92. <
  93. Ring
  94. >::type::template result_type<Ring>::type area_result_type;
  95. typename ring_area_predicate
  96. <
  97. area_result_type, IsInteriorRing
  98. >::type predicate;
  99. // Check area
  100. area_result_type const zero = 0;
  101. area_result_type const area
  102. = ring_area_type::apply(ring,
  103. strategy.template get_area_strategy<Ring>());
  104. if (predicate(area, zero))
  105. {
  106. return visitor.template apply<no_failure>();
  107. }
  108. else
  109. {
  110. return visitor.template apply<failure_wrong_orientation>();
  111. }
  112. }
  113. };
  114. template
  115. <
  116. typename Ring,
  117. bool CheckSelfIntersections = true,
  118. bool IsInteriorRing = false
  119. >
  120. struct is_valid_ring
  121. {
  122. template <typename VisitPolicy, typename Strategy>
  123. static inline bool apply(Ring const& ring, VisitPolicy& visitor,
  124. Strategy const& strategy)
  125. {
  126. typedef typename Strategy::cs_tag cs_tag;
  127. // return invalid if any of the following condition holds:
  128. // (a) the ring's point coordinates are not invalid (e.g., NaN)
  129. // (b) the ring's size is below the minimal one
  130. // (c) the ring consists of at most two distinct points
  131. // (d) the ring is not topologically closed
  132. // (e) the ring has spikes
  133. // (f) the ring has duplicate points (if AllowDuplicates is false)
  134. // (g) the boundary of the ring has self-intersections
  135. // (h) the order of the points is inconsistent with the defined order
  136. //
  137. // Note: no need to check if the area is zero. If this is the
  138. // case, then the ring must have at least two spikes, which is
  139. // checked by condition (d).
  140. if (has_invalid_coordinate<Ring>::apply(ring, visitor))
  141. {
  142. return false;
  143. }
  144. closure_selector const closure = geometry::closure<Ring>::value;
  145. typedef typename closeable_view<Ring const, closure>::type view_type;
  146. if (boost::size(ring)
  147. < core_detail::closure::minimum_ring_size<closure>::value)
  148. {
  149. return visitor.template apply<failure_few_points>();
  150. }
  151. view_type const view(ring);
  152. if (detail::num_distinct_consecutive_points
  153. <
  154. view_type, 4u, true,
  155. not_equal_to
  156. <
  157. typename point_type<Ring>::type,
  158. typename Strategy::equals_point_point_strategy_type
  159. >
  160. >::apply(view)
  161. < 4u)
  162. {
  163. return
  164. visitor.template apply<failure_wrong_topological_dimension>();
  165. }
  166. return
  167. is_topologically_closed<Ring, closure>::apply(ring, visitor, strategy.get_equals_point_point_strategy())
  168. && ! has_duplicates<Ring, closure, cs_tag>::apply(ring, visitor)
  169. && ! has_spikes<Ring, closure>::apply(ring, visitor, strategy.get_side_strategy())
  170. && (! CheckSelfIntersections
  171. || has_valid_self_turns<Ring, typename Strategy::cs_tag>::apply(ring, visitor, strategy))
  172. && is_properly_oriented<Ring, IsInteriorRing>::apply(ring, visitor, strategy);
  173. }
  174. };
  175. }} // namespace detail::is_valid
  176. #endif // DOXYGEN_NO_DETAIL
  177. #ifndef DOXYGEN_NO_DISPATCH
  178. namespace dispatch
  179. {
  180. // A Ring is a Polygon with exterior boundary only.
  181. // The Ring's boundary must be a LinearRing (see OGC 06-103-r4,
  182. // 6.1.7.1, for the definition of LinearRing)
  183. //
  184. // Reference (for polygon validity): OGC 06-103r4 (6.1.11.1)
  185. template <typename Ring, bool AllowEmptyMultiGeometries>
  186. struct is_valid
  187. <
  188. Ring, ring_tag, AllowEmptyMultiGeometries
  189. > : detail::is_valid::is_valid_ring<Ring>
  190. {};
  191. } // namespace dispatch
  192. #endif // DOXYGEN_NO_DISPATCH
  193. }} // namespace boost::geometry
  194. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_RING_HPP