has_spikes.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2014-2019, 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_HAS_SPIKES_HPP
  8. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_HAS_SPIKES_HPP
  9. #include <algorithm>
  10. #include <boost/core/ignore_unused.hpp>
  11. #include <boost/range.hpp>
  12. #include <boost/type_traits/is_same.hpp>
  13. #include <boost/geometry/core/assert.hpp>
  14. #include <boost/geometry/core/point_type.hpp>
  15. #include <boost/geometry/core/tag.hpp>
  16. #include <boost/geometry/core/tags.hpp>
  17. #include <boost/geometry/policies/is_valid/default_policy.hpp>
  18. #include <boost/geometry/util/range.hpp>
  19. #include <boost/geometry/views/closeable_view.hpp>
  20. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  21. #include <boost/geometry/algorithms/validity_failure_type.hpp>
  22. #include <boost/geometry/algorithms/detail/point_is_spike_or_equal.hpp>
  23. #include <boost/geometry/io/dsv/write.hpp>
  24. namespace boost { namespace geometry
  25. {
  26. #ifndef DOXYGEN_NO_DETAIL
  27. namespace detail { namespace is_valid
  28. {
  29. template <typename Point, typename Strategy>
  30. struct equal_to
  31. {
  32. Point const& m_point;
  33. equal_to(Point const& point)
  34. : m_point(point)
  35. {}
  36. template <typename OtherPoint>
  37. inline bool operator()(OtherPoint const& other) const
  38. {
  39. return geometry::detail::equals::equals_point_point(m_point, other, Strategy());
  40. }
  41. };
  42. template <typename Point, typename Strategy>
  43. struct not_equal_to
  44. {
  45. Point const& m_point;
  46. not_equal_to(Point const& point)
  47. : m_point(point)
  48. {}
  49. template <typename OtherPoint>
  50. inline bool operator()(OtherPoint const& other) const
  51. {
  52. return ! geometry::detail::equals::equals_point_point(other, m_point, Strategy());
  53. }
  54. };
  55. template <typename Range, closure_selector Closure>
  56. struct has_spikes
  57. {
  58. template <typename Iterator, typename SideStrategy>
  59. static inline Iterator find_different_from_first(Iterator first,
  60. Iterator last,
  61. SideStrategy const& )
  62. {
  63. typedef not_equal_to
  64. <
  65. typename point_type<Range>::type,
  66. typename SideStrategy::equals_point_point_strategy_type
  67. > not_equal;
  68. BOOST_GEOMETRY_ASSERT(first != last);
  69. Iterator second = first;
  70. ++second;
  71. return std::find_if(second, last, not_equal(*first));
  72. }
  73. template <typename View, typename VisitPolicy, typename SideStrategy>
  74. static inline bool apply_at_closure(View const& view, VisitPolicy& visitor,
  75. SideStrategy const& strategy,
  76. bool is_linear)
  77. {
  78. boost::ignore_unused(visitor);
  79. typedef typename boost::range_iterator<View const>::type iterator;
  80. iterator cur = boost::begin(view);
  81. typename boost::range_reverse_iterator
  82. <
  83. View const
  84. >::type prev = find_different_from_first(boost::rbegin(view),
  85. boost::rend(view),
  86. strategy);
  87. iterator next = find_different_from_first(cur, boost::end(view),
  88. strategy);
  89. if (detail::is_spike_or_equal(*next, *cur, *prev, strategy))
  90. {
  91. return
  92. ! visitor.template apply<failure_spikes>(is_linear, *cur);
  93. }
  94. else
  95. {
  96. return ! visitor.template apply<no_failure>();
  97. }
  98. }
  99. template <typename VisitPolicy, typename SideStrategy>
  100. static inline bool apply(Range const& range, VisitPolicy& visitor,
  101. SideStrategy const& strategy)
  102. {
  103. boost::ignore_unused(visitor);
  104. typedef typename closeable_view<Range const, Closure>::type view_type;
  105. typedef typename boost::range_iterator<view_type const>::type iterator;
  106. bool const is_linear
  107. = boost::is_same<typename tag<Range>::type, linestring_tag>::value;
  108. view_type const view(range);
  109. iterator prev = boost::begin(view);
  110. iterator cur = find_different_from_first(prev, boost::end(view), strategy);
  111. if (cur == boost::end(view))
  112. {
  113. // the range has only one distinct point, so it
  114. // cannot have a spike
  115. return ! visitor.template apply<no_failure>();
  116. }
  117. iterator next = find_different_from_first(cur, boost::end(view), strategy);
  118. if (next == boost::end(view))
  119. {
  120. // the range has only two distinct points, so it
  121. // cannot have a spike
  122. return ! visitor.template apply<no_failure>();
  123. }
  124. while (next != boost::end(view))
  125. {
  126. // Verify spike. TODO: this is a reverse order from expected
  127. // in is_spike_or_equal, but this order calls the side
  128. // strategy in the way to correctly detect the spikes,
  129. // also in geographic cases going over the pole
  130. if (detail::is_spike_or_equal(*next, *cur, *prev, strategy))
  131. {
  132. return
  133. ! visitor.template apply<failure_spikes>(is_linear, *cur);
  134. }
  135. prev = cur;
  136. cur = next;
  137. next = find_different_from_first(cur, boost::end(view), strategy);
  138. }
  139. if (geometry::detail::equals::
  140. equals_point_point(range::front(view), range::back(view),
  141. strategy.get_equals_point_point_strategy()))
  142. {
  143. return apply_at_closure(view, visitor, strategy, is_linear);
  144. }
  145. return ! visitor.template apply<no_failure>();
  146. }
  147. };
  148. }} // namespace detail::is_valid
  149. #endif // DOXYGEN_NO_DETAIL
  150. }} // namespace boost::geometry
  151. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_IS_VALID_HAS_SPIKES_HPP