1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2014, 2017.
6 // Modifications copyright (c) 2014-2017 Oracle and/or its affiliates.
8 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
9 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
15 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_COPY_SEGMENTS_HPP
16 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_COPY_SEGMENTS_HPP
21 #include <boost/array.hpp>
22 #include <boost/mpl/assert.hpp>
23 #include <boost/range.hpp>
24 #include <boost/type_traits/integral_constant.hpp>
26 #include <boost/geometry/core/assert.hpp>
27 #include <boost/geometry/core/exterior_ring.hpp>
28 #include <boost/geometry/core/interior_rings.hpp>
29 #include <boost/geometry/core/ring_type.hpp>
30 #include <boost/geometry/core/tags.hpp>
31 #include <boost/geometry/algorithms/not_implemented.hpp>
32 #include <boost/geometry/geometries/concepts/check.hpp>
33 #include <boost/geometry/iterators/ever_circling_iterator.hpp>
34 #include <boost/geometry/views/closeable_view.hpp>
35 #include <boost/geometry/views/reversible_view.hpp>
37 #include <boost/geometry/algorithms/detail/overlay/append_no_duplicates.hpp>
38 #include <boost/geometry/algorithms/detail/overlay/append_no_dups_or_spikes.hpp>
39 #include <boost/geometry/algorithms/detail/signed_size_type.hpp>
41 #include <boost/geometry/util/range.hpp>
44 namespace boost { namespace geometry
48 #ifndef DOXYGEN_NO_DETAIL
49 namespace detail { namespace copy_segments
53 template <bool Reverse>
54 struct copy_segments_ring
59 typename SegmentIdentifier,
60 typename SideStrategy,
61 typename RobustPolicy,
64 static inline void apply(Ring const& ring,
65 SegmentIdentifier const& seg_id,
66 signed_size_type to_index,
67 SideStrategy const& strategy,
68 RobustPolicy const& robust_policy,
69 RangeOut& current_output)
71 typedef typename closeable_view
77 typedef typename reversible_view
80 Reverse ? iterate_reverse : iterate_forward
83 typedef typename boost::range_iterator<rview_type const>::type iterator;
84 typedef geometry::ever_circling_iterator<iterator> ec_iterator;
87 cview_type cview(ring);
88 rview_type view(cview);
90 // The problem: sometimes we want to from "3" to "2"
91 // -> end = "3" -> end == begin
92 // This is not convenient with iterators.
94 // So we use the ever-circling iterator and determine when to step out
96 signed_size_type const from_index = seg_id.segment_index + 1;
99 BOOST_GEOMETRY_ASSERT(from_index < static_cast<signed_size_type>(boost::size(view)));
101 ec_iterator it(boost::begin(view), boost::end(view),
102 boost::begin(view) + from_index);
104 // [2..4] -> 4 - 2 + 1 = 3 -> {2,3,4} -> OK
105 // [4..2],size=6 -> 6 - 4 + 2 + 1 = 5 -> {4,5,0,1,2} -> OK
106 // [1..1], travel the whole ring round
107 signed_size_type const count = from_index <= to_index
108 ? to_index - from_index + 1
109 : static_cast<signed_size_type>(boost::size(view))
110 - from_index + to_index + 1;
112 for (signed_size_type i = 0; i < count; ++i, ++it)
114 detail::overlay::append_no_dups_or_spikes(current_output, *it, strategy, robust_policy);
119 template <bool Reverse, bool RemoveSpikes = true>
120 class copy_segments_linestring
124 template <typename RangeOut, typename Point, typename SideStrategy, typename RobustPolicy>
125 static inline void append_to_output(RangeOut& current_output,
127 SideStrategy const& strategy,
128 RobustPolicy const& robust_policy,
129 boost::true_type const&)
131 detail::overlay::append_no_dups_or_spikes(current_output, point,
137 template <typename RangeOut, typename Point, typename SideStrategy, typename RobustPolicy>
138 static inline void append_to_output(RangeOut& current_output,
142 boost::false_type const&)
144 detail::overlay::append_no_duplicates(current_output, point);
151 typename SegmentIdentifier,
152 typename SideStrategy,
153 typename RobustPolicy,
156 static inline void apply(LineString const& ls,
157 SegmentIdentifier const& seg_id,
158 signed_size_type to_index,
159 SideStrategy const& strategy,
160 RobustPolicy const& robust_policy,
161 RangeOut& current_output)
163 signed_size_type const from_index = seg_id.segment_index + 1;
166 if ( from_index > to_index
168 || to_index >= static_cast<signed_size_type>(boost::size(ls)) )
173 signed_size_type const count = to_index - from_index + 1;
175 typename boost::range_iterator<LineString const>::type
176 it = boost::begin(ls) + from_index;
178 for (signed_size_type i = 0; i < count; ++i, ++it)
180 append_to_output(current_output, *it, strategy, robust_policy,
181 boost::integral_constant<bool, RemoveSpikes>());
186 template <bool Reverse>
187 struct copy_segments_polygon
192 typename SegmentIdentifier,
193 typename SideStrategy,
194 typename RobustPolicy,
197 static inline void apply(Polygon const& polygon,
198 SegmentIdentifier const& seg_id,
199 signed_size_type to_index,
200 SideStrategy const& strategy,
201 RobustPolicy const& robust_policy,
202 RangeOut& current_output)
204 // Call ring-version with the right ring
205 copy_segments_ring<Reverse>::apply
207 seg_id.ring_index < 0
208 ? geometry::exterior_ring(polygon)
209 : range::at(geometry::interior_rings(polygon), seg_id.ring_index),
219 template <bool Reverse>
220 struct copy_segments_box
225 typename SegmentIdentifier,
226 typename SideStrategy,
227 typename RobustPolicy,
230 static inline void apply(Box const& box,
231 SegmentIdentifier const& seg_id,
232 signed_size_type to_index,
233 SideStrategy const& strategy,
234 RobustPolicy const& robust_policy,
235 RangeOut& current_output)
237 signed_size_type index = seg_id.segment_index + 1;
238 BOOST_GEOMETRY_ASSERT(index < 5);
240 signed_size_type const count = index <= to_index
241 ? to_index - index + 1
242 : 5 - index + to_index + 1;
244 // Create array of points, the fifth one closes it
245 boost::array<typename point_type<Box>::type, 5> bp;
246 assign_box_corners_oriented<Reverse>(box, bp);
249 // (possibly cyclic) copy to output
250 // (see comments in ring-version)
251 for (signed_size_type i = 0; i < count; i++, index++)
253 detail::overlay::append_no_dups_or_spikes(current_output,
254 bp[index % 5], strategy, robust_policy);
261 template<typename Policy>
262 struct copy_segments_multi
266 typename MultiGeometry,
267 typename SegmentIdentifier,
268 typename SideStrategy,
269 typename RobustPolicy,
272 static inline void apply(MultiGeometry const& multi_geometry,
273 SegmentIdentifier const& seg_id,
274 signed_size_type to_index,
275 SideStrategy const& strategy,
276 RobustPolicy const& robust_policy,
277 RangeOut& current_output)
280 BOOST_GEOMETRY_ASSERT
282 seg_id.multi_index >= 0
283 && static_cast<std::size_t>(seg_id.multi_index) < boost::size(multi_geometry)
286 // Call the single-version
287 Policy::apply(range::at(multi_geometry, seg_id.multi_index),
296 }} // namespace detail::copy_segments
297 #endif // DOXYGEN_NO_DETAIL
300 #ifndef DOXYGEN_NO_DISPATCH
309 struct copy_segments : not_implemented<Tag>
313 template <bool Reverse>
314 struct copy_segments<ring_tag, Reverse>
315 : detail::copy_segments::copy_segments_ring<Reverse>
319 template <bool Reverse>
320 struct copy_segments<linestring_tag, Reverse>
321 : detail::copy_segments::copy_segments_linestring<Reverse>
324 template <bool Reverse>
325 struct copy_segments<polygon_tag, Reverse>
326 : detail::copy_segments::copy_segments_polygon<Reverse>
330 template <bool Reverse>
331 struct copy_segments<box_tag, Reverse>
332 : detail::copy_segments::copy_segments_box<Reverse>
336 template<bool Reverse>
337 struct copy_segments<multi_polygon_tag, Reverse>
338 : detail::copy_segments::copy_segments_multi
340 detail::copy_segments::copy_segments_polygon<Reverse>
345 } // namespace dispatch
346 #endif // DOXYGEN_NO_DISPATCH
350 \brief Copy segments from a geometry, starting with the specified segment (seg_id)
351 until the specified index (to_index)
358 typename SegmentIdentifier,
359 typename SideStrategy,
360 typename RobustPolicy,
363 inline void copy_segments(Geometry const& geometry,
364 SegmentIdentifier const& seg_id,
365 signed_size_type to_index,
366 SideStrategy const& strategy,
367 RobustPolicy const& robust_policy,
370 concepts::check<Geometry const>();
372 dispatch::copy_segments
374 typename tag<Geometry>::type,
376 >::apply(geometry, seg_id, to_index, strategy, robust_policy, range_out);
380 }} // namespace boost::geometry
383 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_COPY_SEGMENTS_HPP