point_is_spike_or_equal.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
  5. // Copyright (c) 2013-2015 Adam Wulkiewicz, Lodz, Poland.
  6. // This file was modified by Oracle on 2015, 2017, 2019.
  7. // Modifications copyright (c) 2015-2019 Oracle and/or its affiliates.
  8. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  9. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_POINT_IS_EQUAL_OR_SPIKE_HPP
  14. #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_POINT_IS_EQUAL_OR_SPIKE_HPP
  15. #include <boost/geometry/algorithms/detail/direction_code.hpp>
  16. #include <boost/geometry/algorithms/detail/recalculate.hpp>
  17. #include <boost/geometry/core/cs.hpp>
  18. #include <boost/geometry/policies/robustness/robust_point_type.hpp>
  19. #include <boost/geometry/strategies/side.hpp>
  20. #include <boost/geometry/util/condition.hpp>
  21. #include <boost/geometry/util/math.hpp>
  22. namespace boost { namespace geometry
  23. {
  24. #ifndef DOXYGEN_NO_DETAIL
  25. namespace detail
  26. {
  27. // Checks if a point ("last_point") causes a spike w.r.t.
  28. // the specified two other points (segment_a, segment_b)
  29. //
  30. // x-------x------x
  31. // a lp b
  32. //
  33. // Above, lp generates a spike w.r.t. segment(a,b)
  34. // So specify last point first, then (a,b)
  35. // The segment's orientation does matter: if lp is to the right of b
  36. // no spike is reported
  37. template
  38. <
  39. typename Point1, typename Point2, typename Point3,
  40. typename SideStrategy
  41. >
  42. inline bool point_is_spike_or_equal(Point1 const& last_point, // prev | back
  43. Point2 const& segment_a, // next | back - 2
  44. Point3 const& segment_b, // curr | back - 1 | spike's vertex
  45. SideStrategy const& strategy)
  46. {
  47. typedef typename SideStrategy::cs_tag cs_tag;
  48. int const side = strategy.apply(segment_a, segment_b, last_point);
  49. if (side == 0)
  50. {
  51. // Last point is collinear w.r.t previous segment.
  52. return direction_code<cs_tag>(segment_a, segment_b, last_point) < 1;
  53. }
  54. return false;
  55. }
  56. template
  57. <
  58. typename Point1,
  59. typename Point2,
  60. typename Point3,
  61. typename SideStrategy,
  62. typename RobustPolicy
  63. >
  64. inline bool point_is_spike_or_equal(Point1 const& last_point,
  65. Point2 const& segment_a,
  66. Point3 const& segment_b,
  67. SideStrategy const& strategy,
  68. RobustPolicy const& robust_policy)
  69. {
  70. if (point_is_spike_or_equal(last_point, segment_a, segment_b, strategy))
  71. {
  72. return true;
  73. }
  74. if (BOOST_GEOMETRY_CONDITION(! RobustPolicy::enabled))
  75. {
  76. return false;
  77. }
  78. // Try using specified robust policy
  79. typedef typename geometry::robust_point_type
  80. <
  81. Point1,
  82. RobustPolicy
  83. >::type robust_point_type;
  84. robust_point_type last_point_rob, segment_a_rob, segment_b_rob;
  85. geometry::recalculate(last_point_rob, last_point, robust_policy);
  86. geometry::recalculate(segment_a_rob, segment_a, robust_policy);
  87. geometry::recalculate(segment_b_rob, segment_b, robust_policy);
  88. return point_is_spike_or_equal
  89. (
  90. last_point_rob,
  91. segment_a_rob,
  92. segment_b_rob,
  93. strategy
  94. );
  95. }
  96. template
  97. <
  98. typename Point1,
  99. typename Point2,
  100. typename Point3,
  101. typename SideStrategy,
  102. typename RobustPolicy
  103. >
  104. inline bool point_is_collinear(Point1 const& last_point,
  105. Point2 const& segment_a,
  106. Point3 const& segment_b,
  107. SideStrategy const& strategy,
  108. RobustPolicy const& robust_policy)
  109. {
  110. int const side = strategy.apply(segment_a, segment_b, last_point);
  111. if (side == 0)
  112. {
  113. return true;
  114. }
  115. // This part (or whole method, because it is then trivial)
  116. // will be removed after rescaling
  117. if (BOOST_GEOMETRY_CONDITION(! RobustPolicy::enabled))
  118. {
  119. return false;
  120. }
  121. // Redo, using specified robust policy
  122. typedef typename geometry::robust_point_type
  123. <
  124. Point1,
  125. RobustPolicy
  126. >::type robust_point_type;
  127. robust_point_type last_point_rob, segment_a_rob, segment_b_rob;
  128. geometry::recalculate(last_point_rob, last_point, robust_policy);
  129. geometry::recalculate(segment_a_rob, segment_a, robust_policy);
  130. geometry::recalculate(segment_b_rob, segment_b, robust_policy);
  131. int const side_rob = strategy.apply(segment_a_rob, segment_b_rob, last_point_rob);
  132. return side_rob == 0;
  133. }
  134. //! Version with intuitive order (A, B, C). The original order was
  135. //! unclear (C, A, B). It was used in a different way in has_spikes.
  136. //! On longer term the C,A,B version can be deprecated
  137. template
  138. <
  139. typename Point1,
  140. typename Point2,
  141. typename Point3,
  142. typename SideStrategy
  143. >
  144. inline bool is_spike_or_equal(Point1 const& a,
  145. Point2 const& b,
  146. Point3 const& c,
  147. SideStrategy const& strategy)
  148. {
  149. return point_is_spike_or_equal(c, a, b, strategy);
  150. }
  151. } // namespace detail
  152. #endif
  153. }} // namespace boost::geometry
  154. #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_POINT_IS_EQUAL_OR_SPIKE_HPP