append_no_dups_or_spikes.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2014, 2017, 2018.
  4. // Modifications copyright (c) 2014-2018 Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPEND_NO_DUPS_OR_SPIKES_HPP
  10. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPEND_NO_DUPS_OR_SPIKES_HPP
  11. #include <boost/range.hpp>
  12. #include <boost/static_assert.hpp>
  13. #include <boost/geometry/algorithms/append.hpp>
  14. #include <boost/geometry/algorithms/detail/point_is_spike_or_equal.hpp>
  15. #include <boost/geometry/algorithms/detail/equals/point_point.hpp>
  16. #include <boost/geometry/core/closure.hpp>
  17. #include <boost/geometry/util/condition.hpp>
  18. #include <boost/geometry/util/range.hpp>
  19. namespace boost { namespace geometry
  20. {
  21. #ifndef DOXYGEN_NO_DETAIL
  22. namespace detail { namespace overlay
  23. {
  24. // TODO: move this / rename this
  25. template <typename Point1, typename Point2, typename EqualsStrategy, typename RobustPolicy>
  26. inline bool points_equal_or_close(Point1 const& point1,
  27. Point2 const& point2,
  28. EqualsStrategy const& strategy,
  29. RobustPolicy const& robust_policy)
  30. {
  31. if (detail::equals::equals_point_point(point1, point2, strategy))
  32. {
  33. return true;
  34. }
  35. if (BOOST_GEOMETRY_CONDITION(! RobustPolicy::enabled))
  36. {
  37. return false;
  38. }
  39. // Try using specified robust policy
  40. typedef typename geometry::robust_point_type
  41. <
  42. Point1,
  43. RobustPolicy
  44. >::type robust_point_type;
  45. robust_point_type point1_rob, point2_rob;
  46. geometry::recalculate(point1_rob, point1, robust_policy);
  47. geometry::recalculate(point2_rob, point2, robust_policy);
  48. // Only if this is the case the same strategy can be used.
  49. BOOST_STATIC_ASSERT((boost::is_same
  50. <
  51. typename geometry::cs_tag<Point1>::type,
  52. typename geometry::cs_tag<robust_point_type>::type
  53. >::value));
  54. return detail::equals::equals_point_point(point1_rob, point2_rob, strategy);
  55. }
  56. template <typename Range, typename Point, typename SideStrategy, typename RobustPolicy>
  57. inline void append_no_dups_or_spikes(Range& range, Point const& point,
  58. SideStrategy const& strategy,
  59. RobustPolicy const& robust_policy)
  60. {
  61. #ifdef BOOST_GEOMETRY_DEBUG_INTERSECTION
  62. std::cout << " add: ("
  63. << geometry::get<0>(point) << ", " << geometry::get<1>(point) << ")"
  64. << std::endl;
  65. #endif
  66. // The code below this condition checks all spikes/dups
  67. // for geometries >= 3 points.
  68. // So we have to check the first potential duplicate differently
  69. if ( boost::size(range) == 1
  70. && points_equal_or_close(*(boost::begin(range)), point,
  71. strategy.get_equals_point_point_strategy(),
  72. robust_policy) )
  73. {
  74. return;
  75. }
  76. traits::push_back<Range>::apply(range, point);
  77. // If a point is equal, or forming a spike, remove the pen-ultimate point
  78. // because this one caused the spike.
  79. // If so, the now-new-pen-ultimate point can again cause a spike
  80. // (possibly at a corner). So keep doing this.
  81. // Besides spikes it will also avoid adding duplicates.
  82. while(boost::size(range) >= 3
  83. && point_is_spike_or_equal(point,
  84. *(boost::end(range) - 3),
  85. *(boost::end(range) - 2),
  86. strategy,
  87. robust_policy))
  88. {
  89. // Use the Concept/traits, so resize and append again
  90. traits::resize<Range>::apply(range, boost::size(range) - 2);
  91. traits::push_back<Range>::apply(range, point);
  92. }
  93. }
  94. template <typename Range, typename Point, typename SideStrategy, typename RobustPolicy>
  95. inline void append_no_collinear(Range& range, Point const& point,
  96. SideStrategy const& strategy,
  97. RobustPolicy const& robust_policy)
  98. {
  99. // Stricter version, not allowing any point in a linear row
  100. // (spike, continuation or same point)
  101. // The code below this condition checks all spikes/dups
  102. // for geometries >= 3 points.
  103. // So we have to check the first potential duplicate differently
  104. if ( boost::size(range) == 1
  105. && points_equal_or_close(*(boost::begin(range)), point,
  106. strategy.get_equals_point_point_strategy(),
  107. robust_policy) )
  108. {
  109. return;
  110. }
  111. traits::push_back<Range>::apply(range, point);
  112. // If a point is equal, or forming a spike, remove the pen-ultimate point
  113. // because this one caused the spike.
  114. // If so, the now-new-pen-ultimate point can again cause a spike
  115. // (possibly at a corner). So keep doing this.
  116. // Besides spikes it will also avoid adding duplicates.
  117. while(boost::size(range) >= 3
  118. && point_is_collinear(point,
  119. *(boost::end(range) - 3),
  120. *(boost::end(range) - 2),
  121. strategy,
  122. robust_policy))
  123. {
  124. // Use the Concept/traits, so resize and append again
  125. traits::resize<Range>::apply(range, boost::size(range) - 2);
  126. traits::push_back<Range>::apply(range, point);
  127. }
  128. }
  129. template <typename Range, typename SideStrategy, typename RobustPolicy>
  130. inline void clean_closing_dups_and_spikes(Range& range,
  131. SideStrategy const& strategy,
  132. RobustPolicy const& robust_policy)
  133. {
  134. std::size_t const minsize
  135. = core_detail::closure::minimum_ring_size
  136. <
  137. geometry::closure<Range>::value
  138. >::value;
  139. if (boost::size(range) <= minsize)
  140. {
  141. return;
  142. }
  143. typedef typename boost::range_iterator<Range>::type iterator_type;
  144. static bool const closed = geometry::closure<Range>::value == geometry::closed;
  145. // TODO: the following algorithm could be rewritten to first look for spikes
  146. // and then erase some number of points from the beginning of the Range
  147. bool found = false;
  148. do
  149. {
  150. found = false;
  151. iterator_type first = boost::begin(range);
  152. iterator_type second = first + 1;
  153. iterator_type ultimate = boost::end(range) - 1;
  154. if (BOOST_GEOMETRY_CONDITION(closed))
  155. {
  156. ultimate--;
  157. }
  158. // Check if closing point is a spike (this is so if the second point is
  159. // considered as collinear w.r.t. the last segment)
  160. if (point_is_collinear(*second, *ultimate, *first, strategy, robust_policy))
  161. {
  162. range::erase(range, first);
  163. if (BOOST_GEOMETRY_CONDITION(closed))
  164. {
  165. // Remove closing last point
  166. range::resize(range, boost::size(range) - 1);
  167. // Add new closing point
  168. range::push_back(range, range::front(range));
  169. }
  170. found = true;
  171. }
  172. } while(found && boost::size(range) > minsize);
  173. }
  174. }} // namespace detail::overlay
  175. #endif // DOXYGEN_NO_DETAIL
  176. }} // namespace boost::geometry
  177. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_APPEND_NO_DUPS_OR_SPIKES_HPP