1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2014 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2016, 2018.
6 // Modifications copyright (c) 2016-2018 Oracle and/or its affiliates.
7 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
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)
13 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_BUFFER_TURN_IN_ORIGINAL_VISITOR
14 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_BUFFER_TURN_IN_ORIGINAL_VISITOR
17 #include <boost/core/ignore_unused.hpp>
18 #include <boost/geometry/core/coordinate_type.hpp>
20 #include <boost/geometry/algorithms/detail/buffer/buffer_policies.hpp>
21 #include <boost/geometry/algorithms/expand.hpp>
22 #include <boost/geometry/strategies/agnostic/point_in_poly_winding.hpp>
23 #include <boost/geometry/strategies/buffer.hpp>
26 namespace boost { namespace geometry
30 #ifndef DOXYGEN_NO_DETAIL
31 namespace detail { namespace buffer
34 struct original_get_box
36 template <typename Box, typename Original>
37 static inline void apply(Box& total, Original const& original)
39 assert_coordinate_type_equal(total, original.m_box);
40 typedef typename strategy::expand::services::default_strategy
42 box_tag, typename cs_tag<Box>::type
43 >::type expand_strategy_type;
45 geometry::expand(total, original.m_box, expand_strategy_type());
49 template <typename DisjointBoxBoxStrategy>
50 struct original_overlaps_box
52 template <typename Box, typename Original>
53 static inline bool apply(Box const& box, Original const& original)
55 assert_coordinate_type_equal(box, original.m_box);
56 return ! detail::disjoint::disjoint_box_box(box, original.m_box,
57 DisjointBoxBoxStrategy());
61 struct include_turn_policy
63 template <typename Turn>
64 static inline bool apply(Turn const& turn)
66 return turn.is_turn_traversable;
70 template <typename DisjointPointBoxStrategy>
71 struct turn_in_original_overlaps_box
73 template <typename Box, typename Turn>
74 static inline bool apply(Box const& box, Turn const& turn)
76 if (! turn.is_turn_traversable || turn.within_original)
78 // Skip all points already processed
82 return ! geometry::detail::disjoint::disjoint_point_box(
83 turn.point, box, DisjointPointBoxStrategy());
87 //! Check if specified is in range of specified iterators
88 //! Return value of strategy (true if we can bail out)
96 inline bool point_in_range(Strategy& strategy, State& state,
97 Point const& point, Iterator begin, Iterator end)
99 boost::ignore_unused(strategy);
102 for (Iterator previous = it++; it != end; ++previous, ++it)
104 if (! strategy.apply(point, *previous, *it, state))
106 // We're probably on the boundary
118 typename CoordinateType,
121 inline bool point_in_section(Strategy& strategy, State& state,
122 Point const& point, CoordinateType const& point_x,
123 Iterator begin, Iterator end,
128 // Not a monotonic section, or no change in X-direction
129 return point_in_range(strategy, state, point, begin, end);
132 // We're in a monotonic section in x-direction
135 for (Iterator previous = it++; it != end; ++previous, ++it)
137 // Depending on sections.direction we can quit for this section
138 CoordinateType const previous_x = geometry::get<0>(*previous);
140 if (direction == 1 && point_x < previous_x)
142 // Section goes upwards, x increases, point is is below section
145 else if (direction == -1 && point_x > previous_x)
147 // Section goes downwards, x decreases, point is above section
151 if (! strategy.apply(point, *previous, *it, state))
153 // We're probably on the boundary
161 template <typename Point, typename Original, typename PointInGeometryStrategy>
162 inline int point_in_original(Point const& point, Original const& original,
163 PointInGeometryStrategy const& strategy)
165 typename PointInGeometryStrategy::state_type state;
167 if (boost::size(original.m_sections) == 0
168 || boost::size(original.m_ring) - boost::size(original.m_sections) < 16)
170 // There are no sections, or it does not profit to walk over sections
171 // instead of over points. Boundary of 16 is arbitrary but can influence
173 point_in_range(strategy, state, point,
174 original.m_ring.begin(), original.m_ring.end());
175 return strategy.result(state);
178 typedef typename Original::sections_type sections_type;
179 typedef typename boost::range_iterator<sections_type const>::type iterator_type;
180 typedef typename boost::range_value<sections_type const>::type section_type;
181 typedef typename geometry::coordinate_type<Point>::type coordinate_type;
183 coordinate_type const point_x = geometry::get<0>(point);
185 // Walk through all monotonic sections of this original
186 for (iterator_type it = boost::begin(original.m_sections);
187 it != boost::end(original.m_sections);
190 section_type const& section = *it;
192 if (! section.duplicate
193 && section.begin_index < section.end_index
194 && point_x >= geometry::get<min_corner, 0>(section.bounding_box)
195 && point_x <= geometry::get<max_corner, 0>(section.bounding_box))
197 // x-coordinate of point overlaps with section
198 if (! point_in_section(strategy, state, point, point_x,
199 boost::begin(original.m_ring) + section.begin_index,
200 boost::begin(original.m_ring) + section.end_index + 1,
201 section.directions[0]))
203 // We're probably on the boundary
209 return strategy.result(state);
213 template <typename Turns, typename PointInGeometryStrategy>
214 class turn_in_original_visitor
217 turn_in_original_visitor(Turns& turns, PointInGeometryStrategy const& strategy)
218 : m_mutable_turns(turns)
219 , m_point_in_geometry_strategy(strategy)
222 template <typename Turn, typename Original>
223 inline bool apply(Turn const& turn, Original const& original)
225 if (boost::empty(original.m_ring))
231 if (! turn.is_turn_traversable || turn.within_original)
233 // Skip all points already processed
237 if (geometry::disjoint(turn.point, original.m_box))
243 int const code = point_in_original(turn.point, original, m_point_in_geometry_strategy);
250 Turn& mutable_turn = m_mutable_turns[turn.turn_index];
254 // On border of original: always discard
255 mutable_turn.is_turn_traversable = false;
258 // Point is inside an original ring
259 if (original.m_is_interior)
261 mutable_turn.count_in_original--;
263 else if (original.m_has_interiors)
265 mutable_turn.count_in_original++;
269 // It is an exterior ring and there are no interior rings.
270 // Then we are completely ready with this turn
271 mutable_turn.within_original = true;
272 mutable_turn.count_in_original = 1;
279 Turns& m_mutable_turns;
280 PointInGeometryStrategy const& m_point_in_geometry_strategy;
284 }} // namespace detail::buffer
285 #endif // DOXYGEN_NO_DETAIL
288 }} // namespace boost::geometry
290 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_BUFFER_TURN_IN_ORIGINAL_VISITOR