Chris@16
|
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
|
Chris@16
|
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
|
Chris@16
|
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
|
Chris@16
|
6
|
Chris@101
|
7 // This file was modified by Oracle on 2014, 2015.
|
Chris@101
|
8 // Modifications copyright (c) 2014-2015 Oracle and/or its affiliates.
|
Chris@101
|
9
|
Chris@101
|
10 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
|
Chris@101
|
11
|
Chris@16
|
12 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
|
Chris@16
|
13 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
|
Chris@16
|
14
|
Chris@16
|
15 // Use, modification and distribution is subject to the Boost Software License,
|
Chris@16
|
16 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
17 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
18
|
Chris@16
|
19 #ifndef BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_HPP
|
Chris@16
|
20 #define BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_HPP
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/array.hpp>
|
Chris@16
|
23
|
Chris@101
|
24 #include <boost/variant/apply_visitor.hpp>
|
Chris@101
|
25 #include <boost/variant/static_visitor.hpp>
|
Chris@101
|
26 #include <boost/variant/variant_fwd.hpp>
|
Chris@101
|
27
|
Chris@16
|
28 #include <boost/geometry/core/cs.hpp>
|
Chris@16
|
29 #include <boost/geometry/core/point_order.hpp>
|
Chris@101
|
30 #include <boost/geometry/core/closure.hpp>
|
Chris@16
|
31 #include <boost/geometry/core/exterior_ring.hpp>
|
Chris@16
|
32
|
Chris@16
|
33 #include <boost/geometry/geometries/concepts/check.hpp>
|
Chris@16
|
34
|
Chris@16
|
35 #include <boost/geometry/strategies/convex_hull.hpp>
|
Chris@16
|
36 #include <boost/geometry/strategies/concepts/convex_hull_concept.hpp>
|
Chris@101
|
37 #include <boost/geometry/strategies/default_strategy.hpp>
|
Chris@101
|
38
|
Chris@101
|
39 #include <boost/geometry/util/condition.hpp>
|
Chris@16
|
40
|
Chris@16
|
41 #include <boost/geometry/views/detail/range_type.hpp>
|
Chris@16
|
42
|
Chris@16
|
43 #include <boost/geometry/algorithms/num_points.hpp>
|
Chris@16
|
44 #include <boost/geometry/algorithms/detail/as_range.hpp>
|
Chris@16
|
45 #include <boost/geometry/algorithms/detail/assign_box_corners.hpp>
|
Chris@16
|
46
|
Chris@16
|
47
|
Chris@16
|
48 namespace boost { namespace geometry
|
Chris@16
|
49 {
|
Chris@16
|
50
|
Chris@16
|
51
|
Chris@16
|
52 #ifndef DOXYGEN_NO_DETAIL
|
Chris@16
|
53 namespace detail { namespace convex_hull
|
Chris@16
|
54 {
|
Chris@16
|
55
|
Chris@101
|
56 template <order_selector Order, closure_selector Closure>
|
Chris@16
|
57 struct hull_insert
|
Chris@16
|
58 {
|
Chris@16
|
59
|
Chris@16
|
60 // Member template function (to avoid inconvenient declaration
|
Chris@16
|
61 // of output-iterator-type, from hull_to_geometry)
|
Chris@16
|
62 template <typename Geometry, typename OutputIterator, typename Strategy>
|
Chris@16
|
63 static inline OutputIterator apply(Geometry const& geometry,
|
Chris@16
|
64 OutputIterator out, Strategy const& strategy)
|
Chris@16
|
65 {
|
Chris@16
|
66 typename Strategy::state_type state;
|
Chris@16
|
67
|
Chris@16
|
68 strategy.apply(geometry, state);
|
Chris@101
|
69 strategy.result(state, out, Order == clockwise, Closure != open);
|
Chris@16
|
70 return out;
|
Chris@16
|
71 }
|
Chris@16
|
72 };
|
Chris@16
|
73
|
Chris@16
|
74 struct hull_to_geometry
|
Chris@16
|
75 {
|
Chris@16
|
76 template <typename Geometry, typename OutputGeometry, typename Strategy>
|
Chris@16
|
77 static inline void apply(Geometry const& geometry, OutputGeometry& out,
|
Chris@16
|
78 Strategy const& strategy)
|
Chris@16
|
79 {
|
Chris@16
|
80 hull_insert
|
Chris@16
|
81 <
|
Chris@101
|
82 geometry::point_order<OutputGeometry>::value,
|
Chris@101
|
83 geometry::closure<OutputGeometry>::value
|
Chris@16
|
84 >::apply(geometry,
|
Chris@16
|
85 std::back_inserter(
|
Chris@16
|
86 // Handle linestring, ring and polygon the same:
|
Chris@16
|
87 detail::as_range
|
Chris@16
|
88 <
|
Chris@16
|
89 typename range_type<OutputGeometry>::type
|
Chris@16
|
90 >(out)), strategy);
|
Chris@16
|
91 }
|
Chris@16
|
92 };
|
Chris@16
|
93
|
Chris@16
|
94 }} // namespace detail::convex_hull
|
Chris@16
|
95 #endif // DOXYGEN_NO_DETAIL
|
Chris@16
|
96
|
Chris@16
|
97
|
Chris@16
|
98 #ifndef DOXYGEN_NO_DISPATCH
|
Chris@16
|
99 namespace dispatch
|
Chris@16
|
100 {
|
Chris@16
|
101
|
Chris@16
|
102
|
Chris@16
|
103 template
|
Chris@16
|
104 <
|
Chris@16
|
105 typename Geometry,
|
Chris@16
|
106 typename Tag = typename tag<Geometry>::type
|
Chris@16
|
107 >
|
Chris@16
|
108 struct convex_hull
|
Chris@16
|
109 : detail::convex_hull::hull_to_geometry
|
Chris@16
|
110 {};
|
Chris@16
|
111
|
Chris@16
|
112 template <typename Box>
|
Chris@16
|
113 struct convex_hull<Box, box_tag>
|
Chris@16
|
114 {
|
Chris@16
|
115 template <typename OutputGeometry, typename Strategy>
|
Chris@16
|
116 static inline void apply(Box const& box, OutputGeometry& out,
|
Chris@16
|
117 Strategy const& )
|
Chris@16
|
118 {
|
Chris@16
|
119 static bool const Close
|
Chris@16
|
120 = geometry::closure<OutputGeometry>::value == closed;
|
Chris@16
|
121 static bool const Reverse
|
Chris@16
|
122 = geometry::point_order<OutputGeometry>::value == counterclockwise;
|
Chris@16
|
123
|
Chris@16
|
124 // A hull for boxes is trivial. Any strategy is (currently) skipped.
|
Chris@16
|
125 boost::array<typename point_type<Box>::type, 4> range;
|
Chris@16
|
126 geometry::detail::assign_box_corners_oriented<Reverse>(box, range);
|
Chris@16
|
127 geometry::append(out, range);
|
Chris@101
|
128 if (BOOST_GEOMETRY_CONDITION(Close))
|
Chris@16
|
129 {
|
Chris@16
|
130 geometry::append(out, *boost::begin(range));
|
Chris@16
|
131 }
|
Chris@16
|
132 }
|
Chris@16
|
133 };
|
Chris@16
|
134
|
Chris@16
|
135
|
Chris@16
|
136
|
Chris@101
|
137 template <order_selector Order, closure_selector Closure>
|
Chris@16
|
138 struct convex_hull_insert
|
Chris@101
|
139 : detail::convex_hull::hull_insert<Order, Closure>
|
Chris@16
|
140 {};
|
Chris@16
|
141
|
Chris@16
|
142
|
Chris@16
|
143 } // namespace dispatch
|
Chris@16
|
144 #endif // DOXYGEN_NO_DISPATCH
|
Chris@16
|
145
|
Chris@16
|
146
|
Chris@101
|
147 namespace resolve_strategy {
|
Chris@101
|
148
|
Chris@101
|
149 struct convex_hull
|
Chris@101
|
150 {
|
Chris@101
|
151 template <typename Geometry, typename OutputGeometry, typename Strategy>
|
Chris@101
|
152 static inline void apply(Geometry const& geometry,
|
Chris@101
|
153 OutputGeometry& out,
|
Chris@101
|
154 Strategy const& strategy)
|
Chris@101
|
155 {
|
Chris@101
|
156 BOOST_CONCEPT_ASSERT( (geometry::concept::ConvexHullStrategy<Strategy>) );
|
Chris@101
|
157 dispatch::convex_hull<Geometry>::apply(geometry, out, strategy);
|
Chris@101
|
158 }
|
Chris@101
|
159
|
Chris@101
|
160 template <typename Geometry, typename OutputGeometry>
|
Chris@101
|
161 static inline void apply(Geometry const& geometry,
|
Chris@101
|
162 OutputGeometry& out,
|
Chris@101
|
163 default_strategy)
|
Chris@101
|
164 {
|
Chris@101
|
165 typedef typename strategy_convex_hull<
|
Chris@101
|
166 Geometry,
|
Chris@101
|
167 typename point_type<Geometry>::type
|
Chris@101
|
168 >::type strategy_type;
|
Chris@101
|
169
|
Chris@101
|
170 apply(geometry, out, strategy_type());
|
Chris@101
|
171 }
|
Chris@101
|
172 };
|
Chris@101
|
173
|
Chris@101
|
174 struct convex_hull_insert
|
Chris@101
|
175 {
|
Chris@101
|
176 template <typename Geometry, typename OutputIterator, typename Strategy>
|
Chris@101
|
177 static inline OutputIterator apply(Geometry const& geometry,
|
Chris@101
|
178 OutputIterator& out,
|
Chris@101
|
179 Strategy const& strategy)
|
Chris@101
|
180 {
|
Chris@101
|
181 BOOST_CONCEPT_ASSERT( (geometry::concept::ConvexHullStrategy<Strategy>) );
|
Chris@101
|
182
|
Chris@101
|
183 return dispatch::convex_hull_insert<
|
Chris@101
|
184 geometry::point_order<Geometry>::value,
|
Chris@101
|
185 geometry::closure<Geometry>::value
|
Chris@101
|
186 >::apply(geometry, out, strategy);
|
Chris@101
|
187 }
|
Chris@101
|
188
|
Chris@101
|
189 template <typename Geometry, typename OutputIterator>
|
Chris@101
|
190 static inline OutputIterator apply(Geometry const& geometry,
|
Chris@101
|
191 OutputIterator& out,
|
Chris@101
|
192 default_strategy)
|
Chris@101
|
193 {
|
Chris@101
|
194 typedef typename strategy_convex_hull<
|
Chris@101
|
195 Geometry,
|
Chris@101
|
196 typename point_type<Geometry>::type
|
Chris@101
|
197 >::type strategy_type;
|
Chris@101
|
198
|
Chris@101
|
199 return apply(geometry, out, strategy_type());
|
Chris@101
|
200 }
|
Chris@101
|
201 };
|
Chris@101
|
202
|
Chris@101
|
203 } // namespace resolve_strategy
|
Chris@101
|
204
|
Chris@101
|
205
|
Chris@101
|
206 namespace resolve_variant {
|
Chris@101
|
207
|
Chris@101
|
208 template <typename Geometry>
|
Chris@101
|
209 struct convex_hull
|
Chris@101
|
210 {
|
Chris@101
|
211 template <typename OutputGeometry, typename Strategy>
|
Chris@101
|
212 static inline void apply(Geometry const& geometry, OutputGeometry& out, Strategy const& strategy)
|
Chris@101
|
213 {
|
Chris@101
|
214 concept::check_concepts_and_equal_dimensions<
|
Chris@101
|
215 const Geometry,
|
Chris@101
|
216 OutputGeometry
|
Chris@101
|
217 >();
|
Chris@101
|
218
|
Chris@101
|
219 resolve_strategy::convex_hull::apply(geometry, out, strategy);
|
Chris@101
|
220 }
|
Chris@101
|
221 };
|
Chris@101
|
222
|
Chris@101
|
223 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
|
Chris@101
|
224 struct convex_hull<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
|
Chris@101
|
225 {
|
Chris@101
|
226 template <typename OutputGeometry, typename Strategy>
|
Chris@101
|
227 struct visitor: boost::static_visitor<void>
|
Chris@101
|
228 {
|
Chris@101
|
229 OutputGeometry& m_out;
|
Chris@101
|
230 Strategy const& m_strategy;
|
Chris@101
|
231
|
Chris@101
|
232 visitor(OutputGeometry& out, Strategy const& strategy)
|
Chris@101
|
233 : m_out(out), m_strategy(strategy)
|
Chris@101
|
234 {}
|
Chris@101
|
235
|
Chris@101
|
236 template <typename Geometry>
|
Chris@101
|
237 void operator()(Geometry const& geometry) const
|
Chris@101
|
238 {
|
Chris@101
|
239 convex_hull<Geometry>::apply(geometry, m_out, m_strategy);
|
Chris@101
|
240 }
|
Chris@101
|
241 };
|
Chris@101
|
242
|
Chris@101
|
243 template <typename OutputGeometry, typename Strategy>
|
Chris@101
|
244 static inline void
|
Chris@101
|
245 apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
|
Chris@101
|
246 OutputGeometry& out,
|
Chris@101
|
247 Strategy const& strategy)
|
Chris@101
|
248 {
|
Chris@101
|
249 boost::apply_visitor(visitor<OutputGeometry, Strategy>(out, strategy), geometry);
|
Chris@101
|
250 }
|
Chris@101
|
251 };
|
Chris@101
|
252
|
Chris@101
|
253 template <typename Geometry>
|
Chris@101
|
254 struct convex_hull_insert
|
Chris@101
|
255 {
|
Chris@101
|
256 template <typename OutputIterator, typename Strategy>
|
Chris@101
|
257 static inline OutputIterator apply(Geometry const& geometry, OutputIterator& out, Strategy const& strategy)
|
Chris@101
|
258 {
|
Chris@101
|
259 // Concept: output point type = point type of input geometry
|
Chris@101
|
260 concept::check<Geometry const>();
|
Chris@101
|
261 concept::check<typename point_type<Geometry>::type>();
|
Chris@101
|
262
|
Chris@101
|
263 return resolve_strategy::convex_hull_insert::apply(geometry, out, strategy);
|
Chris@101
|
264 }
|
Chris@101
|
265 };
|
Chris@101
|
266
|
Chris@101
|
267 template <BOOST_VARIANT_ENUM_PARAMS(typename T)>
|
Chris@101
|
268 struct convex_hull_insert<boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> >
|
Chris@101
|
269 {
|
Chris@101
|
270 template <typename OutputIterator, typename Strategy>
|
Chris@101
|
271 struct visitor: boost::static_visitor<OutputIterator>
|
Chris@101
|
272 {
|
Chris@101
|
273 OutputIterator& m_out;
|
Chris@101
|
274 Strategy const& m_strategy;
|
Chris@101
|
275
|
Chris@101
|
276 visitor(OutputIterator& out, Strategy const& strategy)
|
Chris@101
|
277 : m_out(out), m_strategy(strategy)
|
Chris@101
|
278 {}
|
Chris@101
|
279
|
Chris@101
|
280 template <typename Geometry>
|
Chris@101
|
281 OutputIterator operator()(Geometry const& geometry) const
|
Chris@101
|
282 {
|
Chris@101
|
283 return convex_hull_insert<Geometry>::apply(geometry, m_out, m_strategy);
|
Chris@101
|
284 }
|
Chris@101
|
285 };
|
Chris@101
|
286
|
Chris@101
|
287 template <typename OutputIterator, typename Strategy>
|
Chris@101
|
288 static inline OutputIterator
|
Chris@101
|
289 apply(boost::variant<BOOST_VARIANT_ENUM_PARAMS(T)> const& geometry,
|
Chris@101
|
290 OutputIterator& out,
|
Chris@101
|
291 Strategy const& strategy)
|
Chris@101
|
292 {
|
Chris@101
|
293 return boost::apply_visitor(visitor<OutputIterator, Strategy>(out, strategy), geometry);
|
Chris@101
|
294 }
|
Chris@101
|
295 };
|
Chris@101
|
296
|
Chris@101
|
297 } // namespace resolve_variant
|
Chris@101
|
298
|
Chris@101
|
299
|
Chris@16
|
300 template<typename Geometry, typename OutputGeometry, typename Strategy>
|
Chris@16
|
301 inline void convex_hull(Geometry const& geometry,
|
Chris@16
|
302 OutputGeometry& out, Strategy const& strategy)
|
Chris@16
|
303 {
|
Chris@16
|
304 if (geometry::num_points(geometry) == 0)
|
Chris@16
|
305 {
|
Chris@16
|
306 // Leave output empty
|
Chris@16
|
307 return;
|
Chris@16
|
308 }
|
Chris@16
|
309
|
Chris@101
|
310 resolve_variant::convex_hull<Geometry>::apply(geometry, out, strategy);
|
Chris@16
|
311 }
|
Chris@16
|
312
|
Chris@16
|
313
|
Chris@16
|
314 /*!
|
Chris@16
|
315 \brief \brief_calc{convex hull}
|
Chris@16
|
316 \ingroup convex_hull
|
Chris@16
|
317 \details \details_calc{convex_hull,convex hull}.
|
Chris@16
|
318 \tparam Geometry the input geometry type
|
Chris@16
|
319 \tparam OutputGeometry the output geometry type
|
Chris@16
|
320 \param geometry \param_geometry, input geometry
|
Chris@16
|
321 \param hull \param_geometry \param_set{convex hull}
|
Chris@16
|
322
|
Chris@16
|
323 \qbk{[include reference/algorithms/convex_hull.qbk]}
|
Chris@16
|
324 */
|
Chris@16
|
325 template<typename Geometry, typename OutputGeometry>
|
Chris@16
|
326 inline void convex_hull(Geometry const& geometry,
|
Chris@16
|
327 OutputGeometry& hull)
|
Chris@16
|
328 {
|
Chris@101
|
329 convex_hull(geometry, hull, default_strategy());
|
Chris@16
|
330 }
|
Chris@16
|
331
|
Chris@16
|
332 #ifndef DOXYGEN_NO_DETAIL
|
Chris@16
|
333 namespace detail { namespace convex_hull
|
Chris@16
|
334 {
|
Chris@16
|
335
|
Chris@16
|
336
|
Chris@16
|
337 template<typename Geometry, typename OutputIterator, typename Strategy>
|
Chris@16
|
338 inline OutputIterator convex_hull_insert(Geometry const& geometry,
|
Chris@16
|
339 OutputIterator out, Strategy const& strategy)
|
Chris@16
|
340 {
|
Chris@101
|
341 return resolve_variant::convex_hull_insert<Geometry>
|
Chris@101
|
342 ::apply(geometry, out, strategy);
|
Chris@16
|
343 }
|
Chris@16
|
344
|
Chris@16
|
345
|
Chris@16
|
346 /*!
|
Chris@16
|
347 \brief Calculate the convex hull of a geometry, output-iterator version
|
Chris@16
|
348 \ingroup convex_hull
|
Chris@16
|
349 \tparam Geometry the input geometry type
|
Chris@16
|
350 \tparam OutputIterator: an output-iterator
|
Chris@16
|
351 \param geometry the geometry to calculate convex hull from
|
Chris@16
|
352 \param out an output iterator outputing points of the convex hull
|
Chris@16
|
353 \note This overloaded version outputs to an output iterator.
|
Chris@16
|
354 In this case, nothing is known about its point-type or
|
Chris@16
|
355 about its clockwise order. Therefore, the input point-type
|
Chris@16
|
356 and order are copied
|
Chris@16
|
357
|
Chris@16
|
358 */
|
Chris@16
|
359 template<typename Geometry, typename OutputIterator>
|
Chris@16
|
360 inline OutputIterator convex_hull_insert(Geometry const& geometry,
|
Chris@16
|
361 OutputIterator out)
|
Chris@16
|
362 {
|
Chris@101
|
363 return convex_hull_insert(geometry, out, default_strategy());
|
Chris@16
|
364 }
|
Chris@16
|
365
|
Chris@16
|
366
|
Chris@16
|
367 }} // namespace detail::convex_hull
|
Chris@16
|
368 #endif // DOXYGEN_NO_DETAIL
|
Chris@16
|
369
|
Chris@16
|
370
|
Chris@16
|
371 }} // namespace boost::geometry
|
Chris@16
|
372
|
Chris@16
|
373
|
Chris@16
|
374 #endif // BOOST_GEOMETRY_ALGORITHMS_CONVEX_HULL_HPP
|