]>
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 | |
39 | \note The implementation is inspired by terralib http://www.terralib.org (LGPL) | |
40 | \note but totally revised afterwards, especially for cases on segments | |
41 | \note Only dependant on "side", -> agnostic, suitable for spherical/latlong | |
42 | ||
43 | \qbk{ | |
44 | [heading See also] | |
45 | [link geometry.reference.algorithms.within.within_3_with_strategy within (with strategy)] | |
46 | } | |
47 | */ | |
48 | template | |
49 | < | |
50 | bool Reverse, | |
51 | typename Point, | |
52 | typename PointOfSegment = Point, | |
53 | typename CalculationType = void | |
54 | > | |
55 | class oriented_winding | |
56 | { | |
57 | typedef typename select_calculation_type | |
58 | < | |
59 | Point, | |
60 | PointOfSegment, | |
61 | CalculationType | |
62 | >::type calculation_type; | |
63 | ||
64 | ||
65 | typedef typename strategy::side::services::default_strategy | |
66 | < | |
67 | typename cs_tag<Point>::type | |
68 | >::type strategy_side_type; | |
69 | ||
70 | ||
71 | /*! subclass to keep state */ | |
72 | class counter | |
73 | { | |
74 | int m_count; | |
75 | bool m_touches; | |
76 | calculation_type m_sum_area; | |
77 | ||
78 | inline int code() const | |
79 | { | |
80 | return m_touches ? 0 : m_count == 0 ? -1 : 1; | |
81 | } | |
82 | inline int clockwise_oriented_code() const | |
83 | { | |
84 | return (m_sum_area > 0) ? code() : -code(); | |
85 | } | |
86 | inline int oriented_code() const | |
87 | { | |
88 | return Reverse | |
89 | ? -clockwise_oriented_code() | |
90 | : clockwise_oriented_code(); | |
91 | } | |
92 | ||
93 | public : | |
94 | friend class oriented_winding; | |
95 | ||
96 | inline counter() | |
97 | : m_count(0) | |
98 | , m_touches(false) | |
99 | , m_sum_area(0) | |
100 | {} | |
101 | ||
102 | inline void add_to_area(calculation_type triangle) | |
103 | { | |
104 | m_sum_area += triangle; | |
105 | } | |
106 | ||
107 | }; | |
108 | ||
109 | ||
110 | template <size_t D> | |
111 | static inline int check_touch(Point const& point, | |
112 | PointOfSegment const& seg1, PointOfSegment const& seg2, | |
113 | counter& state) | |
114 | { | |
115 | calculation_type const p = get<D>(point); | |
116 | calculation_type const s1 = get<D>(seg1); | |
117 | calculation_type const s2 = get<D>(seg2); | |
118 | if ((s1 <= p && s2 >= p) || (s2 <= p && s1 >= p)) | |
119 | { | |
120 | state.m_touches = true; | |
121 | } | |
122 | return 0; | |
123 | } | |
124 | ||
125 | ||
126 | template <size_t D> | |
127 | static inline int check_segment(Point const& point, | |
128 | PointOfSegment const& seg1, PointOfSegment const& seg2, | |
129 | counter& state) | |
130 | { | |
131 | calculation_type const p = get<D>(point); | |
132 | calculation_type const s1 = get<D>(seg1); | |
133 | calculation_type const s2 = get<D>(seg2); | |
134 | ||
135 | ||
136 | // Check if one of segment endpoints is at same level of point | |
137 | bool eq1 = math::equals(s1, p); | |
138 | bool eq2 = math::equals(s2, p); | |
139 | ||
140 | if (eq1 && eq2) | |
141 | { | |
142 | // Both equal p -> segment is horizontal (or vertical for D=0) | |
143 | // The only thing which has to be done is check if point is ON segment | |
144 | return check_touch<1 - D>(point, seg1, seg2, state); | |
145 | } | |
146 | ||
147 | return | |
148 | eq1 ? (s2 > p ? 1 : -1) // Point on level s1, UP/DOWN depending on s2 | |
149 | : eq2 ? (s1 > p ? -1 : 1) // idem | |
150 | : s1 < p && s2 > p ? 2 // Point between s1 -> s2 --> UP | |
151 | : s2 < p && s1 > p ? -2 // Point between s2 -> s1 --> DOWN | |
152 | : 0; | |
153 | } | |
154 | ||
155 | ||
156 | ||
157 | ||
158 | public : | |
159 | ||
160 | // Typedefs and static methods to fulfill the concept | |
161 | typedef Point point_type; | |
162 | typedef PointOfSegment segment_point_type; | |
163 | typedef counter state_type; | |
164 | ||
165 | static inline bool apply(Point const& point, | |
166 | PointOfSegment const& s1, PointOfSegment const& s2, | |
167 | counter& state) | |
168 | { | |
169 | state.add_to_area(get<0>(s2) * get<1>(s1) - get<0>(s1) * get<1>(s2)); | |
170 | ||
171 | int count = check_segment<1>(point, s1, s2, state); | |
172 | if (count != 0) | |
173 | { | |
174 | int side = strategy_side_type::apply(s1, s2, point); | |
175 | if (side == 0) | |
176 | { | |
177 | // Point is lying on segment | |
178 | state.m_touches = true; | |
179 | state.m_count = 0; | |
180 | return false; | |
181 | } | |
182 | ||
183 | // Side is NEG for right, POS for left. | |
184 | // The count is -2 for down, 2 for up (or -1/1) | |
185 | // Side positive thus means UP and LEFTSIDE or DOWN and RIGHTSIDE | |
186 | // See accompagnying figure (TODO) | |
187 | if (side * count > 0) | |
188 | { | |
189 | state.m_count += count; | |
190 | } | |
191 | } | |
192 | return ! state.m_touches; | |
193 | } | |
194 | ||
195 | static inline int result(counter const& state) | |
196 | { | |
197 | return state.oriented_code(); | |
198 | } | |
199 | }; | |
200 | ||
201 | ||
202 | }} // namespace strategy::within | |
203 | ||
204 | ||
205 | }} // namespace boost::geometry | |
206 | ||
207 | ||
208 | #endif // BOOST_GEOMETRY_STRATEGY_AGNOSTIC_POINT_IN_POLY_ORIENTED_WINDING_HPP |