point_in_poly_oriented_winding.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2011-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  4. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  5. // Use, modification and distribution is subject to the Boost Software License,
  6. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_GEOMETRY_STRATEGY_AGNOSTIC_POINT_IN_POLY_ORIENTED_WINDING_HPP
  9. #define BOOST_GEOMETRY_STRATEGY_AGNOSTIC_POINT_IN_POLY_ORIENTED_WINDING_HPP
  10. #include <boost/geometry/core/point_order.hpp>
  11. #include <boost/geometry/util/math.hpp>
  12. #include <boost/geometry/util/select_calculation_type.hpp>
  13. #include <boost/geometry/strategies/side.hpp>
  14. #include <boost/geometry/strategies/within.hpp>
  15. namespace boost { namespace geometry
  16. {
  17. namespace strategy { namespace within
  18. {
  19. /*!
  20. \brief Within detection using winding rule, but checking if enclosing ring is
  21. counter clockwise and, if so, reverses the result
  22. \ingroup strategies
  23. \tparam Point \tparam_point
  24. \tparam Reverse True if parameter should be reversed
  25. \tparam PointOfSegment \tparam_segment_point
  26. \tparam CalculationType \tparam_calculation
  27. \author Barend Gehrels
  28. \note Only dependant on "side", -> agnostic, suitable for spherical/latlong
  29. \qbk{
  30. [heading See also]
  31. [link geometry.reference.algorithms.within.within_3_with_strategy within (with strategy)]
  32. }
  33. */
  34. template
  35. <
  36. bool Reverse,
  37. typename Point,
  38. typename PointOfSegment = Point,
  39. typename CalculationType = void
  40. >
  41. class oriented_winding
  42. {
  43. typedef typename select_calculation_type
  44. <
  45. Point,
  46. PointOfSegment,
  47. CalculationType
  48. >::type calculation_type;
  49. typedef typename strategy::side::services::default_strategy
  50. <
  51. typename cs_tag<Point>::type
  52. >::type strategy_side_type;
  53. /*! subclass to keep state */
  54. class counter
  55. {
  56. int m_count;
  57. bool m_touches;
  58. calculation_type m_sum_area;
  59. inline int code() const
  60. {
  61. return m_touches ? 0 : m_count == 0 ? -1 : 1;
  62. }
  63. inline int clockwise_oriented_code() const
  64. {
  65. return (m_sum_area > 0) ? code() : -code();
  66. }
  67. inline int oriented_code() const
  68. {
  69. return Reverse
  70. ? -clockwise_oriented_code()
  71. : clockwise_oriented_code();
  72. }
  73. public :
  74. friend class oriented_winding;
  75. inline counter()
  76. : m_count(0)
  77. , m_touches(false)
  78. , m_sum_area(0)
  79. {}
  80. inline void add_to_area(calculation_type triangle)
  81. {
  82. m_sum_area += triangle;
  83. }
  84. };
  85. template <size_t D>
  86. static inline int check_touch(Point const& point,
  87. PointOfSegment const& seg1, PointOfSegment const& seg2,
  88. counter& state)
  89. {
  90. calculation_type const p = get<D>(point);
  91. calculation_type const s1 = get<D>(seg1);
  92. calculation_type const s2 = get<D>(seg2);
  93. if ((s1 <= p && s2 >= p) || (s2 <= p && s1 >= p))
  94. {
  95. state.m_touches = true;
  96. }
  97. return 0;
  98. }
  99. template <size_t D>
  100. static inline int check_segment(Point const& point,
  101. PointOfSegment const& seg1, PointOfSegment const& seg2,
  102. counter& state)
  103. {
  104. calculation_type const p = get<D>(point);
  105. calculation_type const s1 = get<D>(seg1);
  106. calculation_type const s2 = get<D>(seg2);
  107. // Check if one of segment endpoints is at same level of point
  108. bool eq1 = math::equals(s1, p);
  109. bool eq2 = math::equals(s2, p);
  110. if (eq1 && eq2)
  111. {
  112. // Both equal p -> segment is horizontal (or vertical for D=0)
  113. // The only thing which has to be done is check if point is ON segment
  114. return check_touch<1 - D>(point, seg1, seg2, state);
  115. }
  116. return
  117. eq1 ? (s2 > p ? 1 : -1) // Point on level s1, UP/DOWN depending on s2
  118. : eq2 ? (s1 > p ? -1 : 1) // idem
  119. : s1 < p && s2 > p ? 2 // Point between s1 -> s2 --> UP
  120. : s2 < p && s1 > p ? -2 // Point between s2 -> s1 --> DOWN
  121. : 0;
  122. }
  123. public :
  124. // Typedefs and static methods to fulfill the concept
  125. typedef Point point_type;
  126. typedef PointOfSegment segment_point_type;
  127. typedef counter state_type;
  128. static inline bool apply(Point const& point,
  129. PointOfSegment const& s1, PointOfSegment const& s2,
  130. counter& state)
  131. {
  132. state.add_to_area(get<0>(s2) * get<1>(s1) - get<0>(s1) * get<1>(s2));
  133. int count = check_segment<1>(point, s1, s2, state);
  134. if (count != 0)
  135. {
  136. int side = strategy_side_type::apply(s1, s2, point);
  137. if (side == 0)
  138. {
  139. // Point is lying on segment
  140. state.m_touches = true;
  141. state.m_count = 0;
  142. return false;
  143. }
  144. // Side is NEG for right, POS for left.
  145. // The count is -2 for down, 2 for up (or -1/1)
  146. // Side positive thus means UP and LEFTSIDE or DOWN and RIGHTSIDE
  147. // See accompagnying figure (TODO)
  148. if (side * count > 0)
  149. {
  150. state.m_count += count;
  151. }
  152. }
  153. return ! state.m_touches;
  154. }
  155. static inline int result(counter const& state)
  156. {
  157. return state.oriented_code();
  158. }
  159. };
  160. }} // namespace strategy::within
  161. }} // namespace boost::geometry
  162. #endif // BOOST_GEOMETRY_STRATEGY_AGNOSTIC_POINT_IN_POLY_ORIENTED_WINDING_HPP