ring.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2017 Adam Wulkiewicz, Lodz, Poland.
  3. // Copyright (c) 2014-2021, 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/range/size.hpp>
  12. #include <boost/core/ignore_unused.hpp>
  13. #include <boost/geometry/core/closure.hpp>
  14. #include <boost/geometry/core/cs.hpp>
  15. #include <boost/geometry/core/point_order.hpp>
  16. #include <boost/geometry/core/tags.hpp>
  17. #include <boost/geometry/util/order_as_direction.hpp>
  18. #include <boost/geometry/util/range.hpp>
  19. #include <boost/geometry/views/closeable_view.hpp>
  20. #include <boost/geometry/algorithms/area.hpp>
  21. #include <boost/geometry/algorithms/intersects.hpp>
  22. #include <boost/geometry/algorithms/validity_failure_type.hpp>
  23. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  24. #include <boost/geometry/algorithms/detail/num_distinct_consecutive_points.hpp>
  25. #include <boost/geometry/algorithms/detail/is_valid/has_duplicates.hpp>
  26. #include <boost/geometry/algorithms/detail/is_valid/has_invalid_coordinate.hpp>
  27. #include <boost/geometry/algorithms/detail/is_valid/has_spikes.hpp>
  28. #include <boost/geometry/algorithms/detail/is_valid/has_valid_self_turns.hpp>
  29. #include <boost/geometry/algorithms/dispatch/is_valid.hpp>
  30. // TEMP - with UmbrellaStrategy this will be not needed
  31. #include <boost/geometry/strategy/area.hpp>
  32. #include <boost/geometry/strategies/area/services.hpp>
  33. // TODO: use point_order instead of area
  34. #ifdef BOOST_GEOMETRY_TEST_DEBUG
  35. #include <boost/geometry/io/dsv/write.hpp>
  36. #endif
  37. namespace boost { namespace geometry
  38. {
  39. #ifndef DOXYGEN_NO_DETAIL
  40. namespace detail { namespace is_valid
  41. {
  42. // struct to check whether a ring is topologically closed
  43. template <typename Ring, closure_selector Closure = geometry::closure<Ring>::value>
  44. struct is_topologically_closed
  45. {
  46. template <typename VisitPolicy, typename Strategy>
  47. static inline bool apply(Ring const&, VisitPolicy& visitor, Strategy const&)
  48. {
  49. boost::ignore_unused(visitor);
  50. return visitor.template apply<no_failure>();
  51. }
  52. };
  53. template <typename Ring>
  54. struct is_topologically_closed<Ring, closed>
  55. {
  56. template <typename VisitPolicy, typename Strategy>
  57. static inline bool apply(Ring const& ring, VisitPolicy& visitor, Strategy const& strategy)
  58. {
  59. boost::ignore_unused(visitor);
  60. using geometry::detail::equals::equals_point_point;
  61. if (equals_point_point(range::front(ring), range::back(ring), strategy))
  62. {
  63. return visitor.template apply<no_failure>();
  64. }
  65. else
  66. {
  67. return visitor.template apply<failure_not_closed>();
  68. }
  69. }
  70. };
  71. // TODO: use calculate_point_order here
  72. template <typename Ring, bool IsInteriorRing>
  73. struct is_properly_oriented
  74. {
  75. template <typename VisitPolicy, typename Strategy>
  76. static inline bool apply(Ring const& ring, VisitPolicy& visitor,
  77. Strategy const& strategy)
  78. {
  79. boost::ignore_unused(visitor);
  80. // Check area
  81. auto const area = detail::area::ring_area::apply(ring, strategy);
  82. decltype(area) const zero = 0;
  83. if (IsInteriorRing ? (area < zero) : (area > zero))
  84. {
  85. return visitor.template apply<no_failure>();
  86. }
  87. else
  88. {
  89. return visitor.template apply<failure_wrong_orientation>();
  90. }
  91. }
  92. };
  93. template
  94. <
  95. typename Ring,
  96. bool CheckSelfIntersections = true,
  97. bool IsInteriorRing = false
  98. >
  99. struct is_valid_ring
  100. {
  101. template <typename VisitPolicy, typename Strategy>
  102. static inline bool apply(Ring const& ring, VisitPolicy& visitor,
  103. Strategy const& strategy)
  104. {
  105. // return invalid if any of the following condition holds:
  106. // (a) the ring's point coordinates are not invalid (e.g., NaN)
  107. // (b) the ring's size is below the minimal one
  108. // (c) the ring consists of at most two distinct points
  109. // (d) the ring is not topologically closed
  110. // (e) the ring has spikes
  111. // (f) the ring has duplicate points (if AllowDuplicates is false)
  112. // (g) the boundary of the ring has self-intersections
  113. // (h) the order of the points is inconsistent with the defined order
  114. //
  115. // Note: no need to check if the area is zero. If this is the
  116. // case, then the ring must have at least two spikes, which is
  117. // checked by condition (d).
  118. if (has_invalid_coordinate<Ring>::apply(ring, visitor))
  119. {
  120. return false;
  121. }
  122. if (boost::size(ring) < detail::minimum_ring_size<Ring>::value)
  123. {
  124. return visitor.template apply<failure_few_points>();
  125. }
  126. detail::closed_view<Ring const> const view(ring);
  127. if (detail::num_distinct_consecutive_points
  128. <
  129. decltype(view), 4u, true
  130. >::apply(view, strategy)
  131. < 4u)
  132. {
  133. return
  134. visitor.template apply<failure_wrong_topological_dimension>();
  135. }
  136. return
  137. is_topologically_closed<Ring>::apply(ring, visitor, strategy)
  138. && ! has_duplicates<Ring>::apply(ring, visitor, strategy)
  139. && ! has_spikes<Ring>::apply(ring, visitor, strategy)
  140. && (! CheckSelfIntersections
  141. || has_valid_self_turns<Ring, typename Strategy::cs_tag>::apply(ring, visitor, strategy))
  142. && is_properly_oriented<Ring, IsInteriorRing>::apply(ring, visitor, strategy);
  143. }
  144. };
  145. }} // namespace detail::is_valid
  146. #endif // DOXYGEN_NO_DETAIL
  147. #ifndef DOXYGEN_NO_DISPATCH
  148. namespace dispatch
  149. {
  150. // A Ring is a Polygon with exterior boundary only.
  151. // The Ring's boundary must be a LinearRing (see OGC 06-103-r4,
  152. // 6.1.7.1, for the definition of LinearRing)
  153. //
  154. // Reference (for polygon validity): OGC 06-103r4 (6.1.11.1)
  155. template <typename Ring, bool AllowEmptyMultiGeometries>
  156. struct is_valid
  157. <
  158. Ring, ring_tag, AllowEmptyMultiGeometries
  159. > : detail::is_valid::is_valid_ring<Ring>
  160. {};
  161. } // namespace dispatch
  162. #endif // DOXYGEN_NO_DISPATCH
  163. }} // namespace boost::geometry
  164. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_RING_HPP