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.
6 // Modifications copyright (c) 2014 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>
40 #include <boost/geometry/util/range.hpp>
43 namespace boost { namespace geometry
47 #ifndef DOXYGEN_NO_DETAIL
48 namespace detail { namespace copy_segments
52 template <bool Reverse>
53 struct copy_segments_ring
58 typename SegmentIdentifier,
59 typename RobustPolicy,
62 static inline void apply(Ring const& ring,
63 SegmentIdentifier const& seg_id,
64 signed_size_type to_index,
65 RobustPolicy const& robust_policy,
66 RangeOut& current_output)
68 typedef typename closeable_view
74 typedef typename reversible_view
77 Reverse ? iterate_reverse : iterate_forward
80 typedef typename boost::range_iterator<rview_type const>::type iterator;
81 typedef geometry::ever_circling_iterator<iterator> ec_iterator;
84 cview_type cview(ring);
85 rview_type view(cview);
87 // The problem: sometimes we want to from "3" to "2"
88 // -> end = "3" -> end == begin
89 // This is not convenient with iterators.
91 // So we use the ever-circling iterator and determine when to step out
93 signed_size_type const from_index = seg_id.segment_index + 1;
96 BOOST_GEOMETRY_ASSERT(from_index < static_cast<signed_size_type>(boost::size(view)));
98 ec_iterator it(boost::begin(view), boost::end(view),
99 boost::begin(view) + from_index);
101 // [2..4] -> 4 - 2 + 1 = 3 -> {2,3,4} -> OK
102 // [4..2],size=6 -> 6 - 4 + 2 + 1 = 5 -> {4,5,0,1,2} -> OK
103 // [1..1], travel the whole ring round
104 signed_size_type const count = from_index <= to_index
105 ? to_index - from_index + 1
106 : static_cast<signed_size_type>(boost::size(view))
107 - from_index + to_index + 1;
109 for (signed_size_type i = 0; i < count; ++i, ++it)
111 detail::overlay::append_no_dups_or_spikes(current_output, *it, robust_policy);
116 template <bool Reverse, bool RemoveSpikes = true>
117 class copy_segments_linestring
121 template <typename RangeOut, typename Point, typename RobustPolicy>
122 static inline void append_to_output(RangeOut& current_output,
124 RobustPolicy const& robust_policy,
125 boost::true_type const&)
127 detail::overlay::append_no_dups_or_spikes(current_output, point,
132 template <typename RangeOut, typename Point, typename RobustPolicy>
133 static inline void append_to_output(RangeOut& current_output,
136 boost::false_type const&)
138 detail::overlay::append_no_duplicates(current_output, point);
145 typename SegmentIdentifier,
146 typename RobustPolicy,
149 static inline void apply(LineString const& ls,
150 SegmentIdentifier const& seg_id,
151 signed_size_type to_index,
152 RobustPolicy const& robust_policy,
153 RangeOut& current_output)
155 signed_size_type const from_index = seg_id.segment_index + 1;
158 if ( from_index > to_index
160 || to_index >= static_cast<signed_size_type>(boost::size(ls)) )
165 signed_size_type const count = to_index - from_index + 1;
167 typename boost::range_iterator<LineString const>::type
168 it = boost::begin(ls) + from_index;
170 for (signed_size_type i = 0; i < count; ++i, ++it)
172 append_to_output(current_output, *it, robust_policy,
173 boost::integral_constant<bool, RemoveSpikes>());
178 template <bool Reverse>
179 struct copy_segments_polygon
184 typename SegmentIdentifier,
185 typename RobustPolicy,
188 static inline void apply(Polygon const& polygon,
189 SegmentIdentifier const& seg_id,
190 signed_size_type to_index,
191 RobustPolicy const& robust_policy,
192 RangeOut& current_output)
194 // Call ring-version with the right ring
195 copy_segments_ring<Reverse>::apply
197 seg_id.ring_index < 0
198 ? geometry::exterior_ring(polygon)
199 : range::at(geometry::interior_rings(polygon), seg_id.ring_index),
208 template <bool Reverse>
209 struct copy_segments_box
214 typename SegmentIdentifier,
215 typename RobustPolicy,
218 static inline void apply(Box const& box,
219 SegmentIdentifier const& seg_id,
220 signed_size_type to_index,
221 RobustPolicy const& robust_policy,
222 RangeOut& current_output)
224 signed_size_type index = seg_id.segment_index + 1;
225 BOOST_GEOMETRY_ASSERT(index < 5);
227 signed_size_type const count = index <= to_index
228 ? to_index - index + 1
229 : 5 - index + to_index + 1;
231 // Create array of points, the fifth one closes it
232 boost::array<typename point_type<Box>::type, 5> bp;
233 assign_box_corners_oriented<Reverse>(box, bp);
236 // (possibly cyclic) copy to output
237 // (see comments in ring-version)
238 for (signed_size_type i = 0; i < count; i++, index++)
240 detail::overlay::append_no_dups_or_spikes(current_output,
241 bp[index % 5], robust_policy);
248 template<typename Policy>
249 struct copy_segments_multi
253 typename MultiGeometry,
254 typename SegmentIdentifier,
255 typename RobustPolicy,
258 static inline void apply(MultiGeometry const& multi_geometry,
259 SegmentIdentifier const& seg_id,
260 signed_size_type to_index,
261 RobustPolicy const& robust_policy,
262 RangeOut& current_output)
265 BOOST_GEOMETRY_ASSERT
267 seg_id.multi_index >= 0
268 && static_cast<std::size_t>(seg_id.multi_index) < boost::size(multi_geometry)
271 // Call the single-version
272 Policy::apply(range::at(multi_geometry, seg_id.multi_index),
280 }} // namespace detail::copy_segments
281 #endif // DOXYGEN_NO_DETAIL
284 #ifndef DOXYGEN_NO_DISPATCH
293 struct copy_segments : not_implemented<Tag>
297 template <bool Reverse>
298 struct copy_segments<ring_tag, Reverse>
299 : detail::copy_segments::copy_segments_ring<Reverse>
303 template <bool Reverse>
304 struct copy_segments<linestring_tag, Reverse>
305 : detail::copy_segments::copy_segments_linestring<Reverse>
308 template <bool Reverse>
309 struct copy_segments<polygon_tag, Reverse>
310 : detail::copy_segments::copy_segments_polygon<Reverse>
314 template <bool Reverse>
315 struct copy_segments<box_tag, Reverse>
316 : detail::copy_segments::copy_segments_box<Reverse>
320 template<bool Reverse>
321 struct copy_segments<multi_polygon_tag, Reverse>
322 : detail::copy_segments::copy_segments_multi
324 detail::copy_segments::copy_segments_polygon<Reverse>
329 } // namespace dispatch
330 #endif // DOXYGEN_NO_DISPATCH
334 \brief Copy segments from a geometry, starting with the specified segment (seg_id)
335 until the specified index (to_index)
342 typename SegmentIdentifier,
343 typename RobustPolicy,
346 inline void copy_segments(Geometry const& geometry,
347 SegmentIdentifier const& seg_id,
348 signed_size_type to_index,
349 RobustPolicy const& robust_policy,
352 concepts::check<Geometry const>();
354 dispatch::copy_segments
356 typename tag<Geometry>::type,
358 >::apply(geometry, seg_id, to_index, robust_policy, range_out);
362 }} // namespace boost::geometry
365 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_OVERLAY_COPY_SEGMENTS_HPP