point_in_poly_winding.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2013 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2013-2021.
  5. // Modifications copyright (c) 2013-2021 Oracle and/or its affiliates.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  7. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  8. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  9. // Use, modification and distribution is subject to the Boost Software License,
  10. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. #ifndef BOOST_GEOMETRY_STRATEGY_CARTESIAN_POINT_IN_POLY_WINDING_HPP
  13. #define BOOST_GEOMETRY_STRATEGY_CARTESIAN_POINT_IN_POLY_WINDING_HPP
  14. #include <boost/geometry/core/tags.hpp>
  15. #include <boost/geometry/util/math.hpp>
  16. #include <boost/geometry/util/select_calculation_type.hpp>
  17. #include <boost/geometry/strategy/cartesian/expand_point.hpp>
  18. #include <boost/geometry/strategies/side.hpp>
  19. #include <boost/geometry/strategies/cartesian/point_in_box.hpp>
  20. #include <boost/geometry/strategies/cartesian/disjoint_box_box.hpp>
  21. #include <boost/geometry/strategies/covered_by.hpp>
  22. #include <boost/geometry/strategies/within.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. namespace strategy { namespace within
  26. {
  27. #ifndef DOXYGEN_NO_DETAIL
  28. namespace detail
  29. {
  30. /*!
  31. \brief Within detection using winding rule in cartesian coordinate system.
  32. \ingroup strategies
  33. \tparam SideStrategy A strategy defining creation along sides
  34. \tparam CalculationType \tparam_calculation
  35. */
  36. template <typename SideStrategy, typename CalculationType>
  37. class cartesian_winding_base
  38. {
  39. template <typename Point, typename PointOfSegment>
  40. struct calculation_type
  41. : select_calculation_type
  42. <
  43. Point,
  44. PointOfSegment,
  45. CalculationType
  46. >
  47. {};
  48. /*! subclass to keep state */
  49. class counter
  50. {
  51. int m_count;
  52. bool m_touches;
  53. inline int code() const
  54. {
  55. return m_touches ? 0 : m_count == 0 ? -1 : 1;
  56. }
  57. public :
  58. friend class cartesian_winding_base;
  59. inline counter()
  60. : m_count(0)
  61. , m_touches(false)
  62. {}
  63. };
  64. public:
  65. typedef cartesian_tag cs_tag;
  66. // Typedefs and static methods to fulfill the concept
  67. typedef counter state_type;
  68. template <typename Point, typename PointOfSegment>
  69. static inline bool apply(Point const& point,
  70. PointOfSegment const& s1, PointOfSegment const& s2,
  71. counter& state)
  72. {
  73. bool eq1 = false;
  74. bool eq2 = false;
  75. int count = check_segment(point, s1, s2, state, eq1, eq2);
  76. if (count != 0)
  77. {
  78. int side = 0;
  79. if (count == 1 || count == -1)
  80. {
  81. side = side_equal(point, eq1 ? s1 : s2, count);
  82. }
  83. else // count == 2 || count == -2
  84. {
  85. // 1 left, -1 right
  86. side = SideStrategy::apply(s1, s2, point);
  87. }
  88. if (side == 0)
  89. {
  90. // Point is lying on segment
  91. state.m_touches = true;
  92. state.m_count = 0;
  93. return false;
  94. }
  95. // Side is NEG for right, POS for left.
  96. // The count is -2 for left, 2 for right (or -1/1)
  97. // Side positive thus means RIGHT and LEFTSIDE or LEFT and RIGHTSIDE
  98. // See accompagnying figure (TODO)
  99. if (side * count > 0)
  100. {
  101. state.m_count += count;
  102. }
  103. }
  104. return ! state.m_touches;
  105. }
  106. static inline int result(counter const& state)
  107. {
  108. return state.code();
  109. }
  110. private:
  111. template <typename Point, typename PointOfSegment>
  112. static inline int check_segment(Point const& point,
  113. PointOfSegment const& seg1,
  114. PointOfSegment const& seg2,
  115. counter& state,
  116. bool& eq1, bool& eq2)
  117. {
  118. if (check_touch(point, seg1, seg2, state, eq1, eq2))
  119. {
  120. return 0;
  121. }
  122. return calculate_count(point, seg1, seg2, eq1, eq2);
  123. }
  124. template <typename Point, typename PointOfSegment>
  125. static inline bool check_touch(Point const& point,
  126. PointOfSegment const& seg1,
  127. PointOfSegment const& seg2,
  128. counter& state,
  129. bool& eq1, bool& eq2)
  130. {
  131. typedef typename calculation_type<Point, PointOfSegment>::type calc_t;
  132. calc_t const px = get<0>(point);
  133. calc_t const s1x = get<0>(seg1);
  134. calc_t const s2x = get<0>(seg2);
  135. eq1 = math::equals(s1x, px);
  136. eq2 = math::equals(s2x, px);
  137. // Both equal p -> segment vertical
  138. // The only thing which has to be done is check if point is ON segment
  139. if (eq1 && eq2)
  140. {
  141. calc_t const py = get<1>(point);
  142. calc_t const s1y = get<1>(seg1);
  143. calc_t const s2y = get<1>(seg2);
  144. if ((s1y <= py && s2y >= py) || (s2y <= py && s1y >= py))
  145. {
  146. state.m_touches = true;
  147. }
  148. return true;
  149. }
  150. return false;
  151. }
  152. template <typename Point, typename PointOfSegment>
  153. static inline int calculate_count(Point const& point,
  154. PointOfSegment const& seg1,
  155. PointOfSegment const& seg2,
  156. bool eq1, bool eq2)
  157. {
  158. typedef typename calculation_type<Point, PointOfSegment>::type calc_t;
  159. calc_t const p = get<0>(point);
  160. calc_t const s1 = get<0>(seg1);
  161. calc_t const s2 = get<0>(seg2);
  162. return eq1 ? (s2 > p ? 1 : -1) // Point on level s1, E/W depending on s2
  163. : eq2 ? (s1 > p ? -1 : 1) // idem
  164. : s1 < p && s2 > p ? 2 // Point between s1 -> s2 --> E
  165. : s2 < p && s1 > p ? -2 // Point between s2 -> s1 --> W
  166. : 0;
  167. }
  168. template <typename Point, typename PointOfSegment>
  169. static inline int side_equal(Point const& point,
  170. PointOfSegment const& se,
  171. int count)
  172. {
  173. // NOTE: for D=0 the signs would be reversed
  174. return math::equals(get<1>(point), get<1>(se)) ?
  175. 0 :
  176. get<1>(point) < get<1>(se) ?
  177. // assuming count is equal to 1 or -1
  178. -count : // ( count > 0 ? -1 : 1) :
  179. count; // ( count > 0 ? 1 : -1) ;
  180. }
  181. };
  182. } // namespace detail
  183. #endif // DOXYGEN_NO_DETAIL
  184. /*!
  185. \brief Within detection using winding rule in cartesian coordinate system.
  186. \ingroup strategies
  187. \tparam Point_ \tparam_point
  188. \tparam PointOfSegment_ \tparam_segment_point
  189. \tparam CalculationType \tparam_calculation
  190. \qbk{
  191. [heading See also]
  192. [link geometry.reference.algorithms.within.within_3_with_strategy within (with strategy)]
  193. }
  194. */
  195. template
  196. <
  197. typename Point_ = void, // for backward compatibility
  198. typename PointOfSegment_ = Point_, // for backward compatibility
  199. typename CalculationType = void
  200. >
  201. class cartesian_winding
  202. : public detail::cartesian_winding_base
  203. <
  204. typename side::services::default_strategy<cartesian_tag, CalculationType>::type,
  205. CalculationType
  206. >
  207. {};
  208. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  209. namespace services
  210. {
  211. template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
  212. struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  213. {
  214. using type = cartesian_winding<>;
  215. };
  216. template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
  217. struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, linear_tag, cartesian_tag, cartesian_tag>
  218. {
  219. using type = cartesian_winding<>;
  220. };
  221. } // namespace services
  222. #endif
  223. }} // namespace strategy::within
  224. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  225. namespace strategy { namespace covered_by { namespace services
  226. {
  227. template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
  228. struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, polygonal_tag, cartesian_tag, cartesian_tag>
  229. {
  230. using type = within::cartesian_winding<>;
  231. };
  232. template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
  233. struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, linear_tag, cartesian_tag, cartesian_tag>
  234. {
  235. using type = within::cartesian_winding<>;
  236. };
  237. }}} // namespace strategy::covered_by::services
  238. #endif
  239. }} // namespace boost::geometry
  240. #endif // BOOST_GEOMETRY_STRATEGY_CARTESIAN_POINT_IN_POLY_WINDING_HPP