]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/geometry/strategies/cartesian/point_in_poly_winding.hpp
bump version to 18.2.2-pve1
[ceph.git] / ceph / src / boost / boost / geometry / strategies / cartesian / point_in_poly_winding.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2013 Adam Wulkiewicz, Lodz, Poland.
5
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
9
10 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
11 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
12
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)
16
17 #ifndef BOOST_GEOMETRY_STRATEGY_CARTESIAN_POINT_IN_POLY_WINDING_HPP
18 #define BOOST_GEOMETRY_STRATEGY_CARTESIAN_POINT_IN_POLY_WINDING_HPP
19
20
21 #include <boost/geometry/core/tags.hpp>
22
23 #include <boost/geometry/util/math.hpp>
24 #include <boost/geometry/util/select_calculation_type.hpp>
25
26 #include <boost/geometry/strategy/cartesian/expand_point.hpp>
27 #include <boost/geometry/strategies/side.hpp>
28
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>
33
34
35 namespace boost { namespace geometry
36 {
37
38 namespace strategy { namespace within
39 {
40
41
42 /*!
43 \brief Within detection using winding rule in cartesian coordinate system.
44 \ingroup strategies
45 \tparam Point_ \tparam_point
46 \tparam PointOfSegment_ \tparam_segment_point
47 \tparam CalculationType \tparam_calculation
48 \author Barend Gehrels
49
50 \qbk{
51 [heading See also]
52 [link geometry.reference.algorithms.within.within_3_with_strategy within (with strategy)]
53 }
54 */
55 template
56 <
57 typename Point_ = void, // for backward compatibility
58 typename PointOfSegment_ = Point_, // for backward compatibility
59 typename CalculationType = void
60 >
61 class cartesian_winding
62 {
63 template <typename Point, typename PointOfSegment>
64 struct calculation_type
65 : select_calculation_type
66 <
67 Point,
68 PointOfSegment,
69 CalculationType
70 >
71 {};
72
73 /*! subclass to keep state */
74 class counter
75 {
76 int m_count;
77 bool m_touches;
78
79 inline int code() const
80 {
81 return m_touches ? 0 : m_count == 0 ? -1 : 1;
82 }
83
84 public :
85 friend class cartesian_winding;
86
87 inline counter()
88 : m_count(0)
89 , m_touches(false)
90 {}
91
92 };
93
94 public:
95 typedef cartesian_tag cs_tag;
96
97 // Typedefs and static methods to fulfill the concept
98 typedef counter state_type;
99
100 template <typename Point, typename PointOfSegment>
101 static inline bool apply(Point const& point,
102 PointOfSegment const& s1, PointOfSegment const& s2,
103 counter& state)
104 {
105 bool eq1 = false;
106 bool eq2 = false;
107
108 int count = check_segment(point, s1, s2, state, eq1, eq2);
109 if (count != 0)
110 {
111 int side = 0;
112 if (count == 1 || count == -1)
113 {
114 side = side_equal(point, eq1 ? s1 : s2, count);
115 }
116 else // count == 2 || count == -2
117 {
118 // 1 left, -1 right
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);
123 }
124
125 if (side == 0)
126 {
127 // Point is lying on segment
128 state.m_touches = true;
129 state.m_count = 0;
130 return false;
131 }
132
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)
138 {
139 state.m_count += count;
140 }
141 }
142 return ! state.m_touches;
143 }
144
145 static inline int result(counter const& state)
146 {
147 return state.code();
148 }
149
150 private:
151 template <typename Point, typename PointOfSegment>
152 static inline int check_segment(Point const& point,
153 PointOfSegment const& seg1,
154 PointOfSegment const& seg2,
155 counter& state,
156 bool& eq1, bool& eq2)
157 {
158 if (check_touch(point, seg1, seg2, state, eq1, eq2))
159 {
160 return 0;
161 }
162
163 return calculate_count(point, seg1, seg2, eq1, eq2);
164 }
165
166 template <typename Point, typename PointOfSegment>
167 static inline bool check_touch(Point const& point,
168 PointOfSegment const& seg1,
169 PointOfSegment const& seg2,
170 counter& state,
171 bool& eq1, bool& eq2)
172 {
173 typedef typename calculation_type<Point, PointOfSegment>::type calc_t;
174
175 calc_t const px = get<0>(point);
176 calc_t const s1x = get<0>(seg1);
177 calc_t const s2x = get<0>(seg2);
178
179 eq1 = math::equals(s1x, px);
180 eq2 = math::equals(s2x, px);
181
182 // Both equal p -> segment vertical
183 // The only thing which has to be done is check if point is ON segment
184 if (eq1 && eq2)
185 {
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))
190 {
191 state.m_touches = true;
192 }
193 return true;
194 }
195 return false;
196 }
197
198 template <typename Point, typename PointOfSegment>
199 static inline int calculate_count(Point const& point,
200 PointOfSegment const& seg1,
201 PointOfSegment const& seg2,
202 bool eq1, bool eq2)
203 {
204 typedef typename calculation_type<Point, PointOfSegment>::type calc_t;
205
206 calc_t const p = get<0>(point);
207 calc_t const s1 = get<0>(seg1);
208 calc_t const s2 = get<0>(seg2);
209
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
214 : 0;
215 }
216
217 template <typename Point, typename PointOfSegment>
218 static inline int side_equal(Point const& point,
219 PointOfSegment const& se,
220 int count)
221 {
222 // NOTE: for D=0 the signs would be reversed
223 return math::equals(get<1>(point), get<1>(se)) ?
224 0 :
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) ;
229 }
230 };
231
232
233 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
234
235 namespace services
236 {
237
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>
240 {
241 typedef cartesian_winding<> type;
242 };
243
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>
246 {
247 typedef cartesian_winding<> type;
248 };
249
250 } // namespace services
251
252 #endif
253
254
255 }} // namespace strategy::within
256
257
258 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
259 namespace strategy { namespace covered_by { namespace services
260 {
261
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>
264 {
265 typedef within::cartesian_winding<> type;
266 };
267
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>
270 {
271 typedef within::cartesian_winding<> type;
272 };
273
274 }}} // namespace strategy::covered_by::services
275 #endif
276
277
278 }} // namespace boost::geometry
279
280
281 #endif // BOOST_GEOMETRY_STRATEGY_CARTESIAN_POINT_IN_POLY_WINDING_HPP