1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2013 Adam Wulkiewicz, Lodz, Poland.
6 // This file was modified by Oracle on 2013-2021.
7 // Modifications copyright (c) 2013-2021 Oracle and/or its affiliates.
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
11 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
13 // Use, modification and distribution is subject to the Boost Software License,
14 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
17 #ifndef BOOST_GEOMETRY_STRATEGY_CARTESIAN_POINT_IN_POLY_WINDING_HPP
18 #define BOOST_GEOMETRY_STRATEGY_CARTESIAN_POINT_IN_POLY_WINDING_HPP
21 #include <boost/geometry/core/tags.hpp>
23 #include <boost/geometry/util/math.hpp>
24 #include <boost/geometry/util/select_calculation_type.hpp>
26 #include <boost/geometry/strategy/cartesian/expand_point.hpp>
27 #include <boost/geometry/strategies/side.hpp>
29 #include <boost/geometry/strategies/cartesian/point_in_box.hpp>
30 #include <boost/geometry/strategies/cartesian/disjoint_box_box.hpp>
31 #include <boost/geometry/strategies/covered_by.hpp>
32 #include <boost/geometry/strategies/within.hpp>
35 namespace boost { namespace geometry
38 namespace strategy { namespace within
43 \brief Within detection using winding rule in cartesian coordinate system.
45 \tparam Point_ \tparam_point
46 \tparam PointOfSegment_ \tparam_segment_point
47 \tparam CalculationType \tparam_calculation
48 \author Barend Gehrels
52 [link geometry.reference.algorithms.within.within_3_with_strategy within (with strategy)]
57 typename Point_ = void, // for backward compatibility
58 typename PointOfSegment_ = Point_, // for backward compatibility
59 typename CalculationType = void
61 class cartesian_winding
63 template <typename Point, typename PointOfSegment>
64 struct calculation_type
65 : select_calculation_type
73 /*! subclass to keep state */
79 inline int code() const
81 return m_touches ? 0 : m_count == 0 ? -1 : 1;
85 friend class cartesian_winding;
95 typedef cartesian_tag cs_tag;
97 // Typedefs and static methods to fulfill the concept
98 typedef counter state_type;
100 template <typename Point, typename PointOfSegment>
101 static inline bool apply(Point const& point,
102 PointOfSegment const& s1, PointOfSegment const& s2,
108 int count = check_segment(point, s1, s2, state, eq1, eq2);
112 if (count == 1 || count == -1)
114 side = side_equal(point, eq1 ? s1 : s2, count);
116 else // count == 2 || count == -2
119 using side_strategy_type
120 = typename side::services::default_strategy
121 <cartesian_tag, CalculationType>::type;
122 side = side_strategy_type::apply(s1, s2, point);
127 // Point is lying on segment
128 state.m_touches = true;
133 // Side is NEG for right, POS for left.
134 // The count is -2 for left, 2 for right (or -1/1)
135 // Side positive thus means RIGHT and LEFTSIDE or LEFT and RIGHTSIDE
136 // See accompagnying figure (TODO)
137 if (side * count > 0)
139 state.m_count += count;
142 return ! state.m_touches;
145 static inline int result(counter const& state)
151 template <typename Point, typename PointOfSegment>
152 static inline int check_segment(Point const& point,
153 PointOfSegment const& seg1,
154 PointOfSegment const& seg2,
156 bool& eq1, bool& eq2)
158 if (check_touch(point, seg1, seg2, state, eq1, eq2))
163 return calculate_count(point, seg1, seg2, eq1, eq2);
166 template <typename Point, typename PointOfSegment>
167 static inline bool check_touch(Point const& point,
168 PointOfSegment const& seg1,
169 PointOfSegment const& seg2,
171 bool& eq1, bool& eq2)
173 typedef typename calculation_type<Point, PointOfSegment>::type calc_t;
175 calc_t const px = get<0>(point);
176 calc_t const s1x = get<0>(seg1);
177 calc_t const s2x = get<0>(seg2);
179 eq1 = math::equals(s1x, px);
180 eq2 = math::equals(s2x, px);
182 // Both equal p -> segment vertical
183 // The only thing which has to be done is check if point is ON segment
186 calc_t const py = get<1>(point);
187 calc_t const s1y = get<1>(seg1);
188 calc_t const s2y = get<1>(seg2);
189 if ((s1y <= py && s2y >= py) || (s2y <= py && s1y >= py))
191 state.m_touches = true;
198 template <typename Point, typename PointOfSegment>
199 static inline int calculate_count(Point const& point,
200 PointOfSegment const& seg1,
201 PointOfSegment const& seg2,
204 typedef typename calculation_type<Point, PointOfSegment>::type calc_t;
206 calc_t const p = get<0>(point);
207 calc_t const s1 = get<0>(seg1);
208 calc_t const s2 = get<0>(seg2);
210 return eq1 ? (s2 > p ? 1 : -1) // Point on level s1, E/W depending on s2
211 : eq2 ? (s1 > p ? -1 : 1) // idem
212 : s1 < p && s2 > p ? 2 // Point between s1 -> s2 --> E
213 : s2 < p && s1 > p ? -2 // Point between s2 -> s1 --> W
217 template <typename Point, typename PointOfSegment>
218 static inline int side_equal(Point const& point,
219 PointOfSegment const& se,
222 // NOTE: for D=0 the signs would be reversed
223 return math::equals(get<1>(point), get<1>(se)) ?
225 get<1>(point) < get<1>(se) ?
226 // assuming count is equal to 1 or -1
227 -count : // ( count > 0 ? -1 : 1) :
228 count; // ( count > 0 ? 1 : -1) ;
233 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
238 template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
239 struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, polygonal_tag, cartesian_tag, cartesian_tag>
241 typedef cartesian_winding<> type;
244 template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
245 struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, linear_tag, cartesian_tag, cartesian_tag>
247 typedef cartesian_winding<> type;
250 } // namespace services
255 }} // namespace strategy::within
258 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
259 namespace strategy { namespace covered_by { namespace services
262 template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
263 struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, polygonal_tag, cartesian_tag, cartesian_tag>
265 typedef within::cartesian_winding<> type;
268 template <typename PointLike, typename Geometry, typename AnyTag1, typename AnyTag2>
269 struct default_strategy<PointLike, Geometry, AnyTag1, AnyTag2, pointlike_tag, linear_tag, cartesian_tag, cartesian_tag>
271 typedef within::cartesian_winding<> type;
274 }}} // namespace strategy::covered_by::services
278 }} // namespace boost::geometry
281 #endif // BOOST_GEOMETRY_STRATEGY_CARTESIAN_POINT_IN_POLY_WINDING_HPP