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