1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2013 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2013 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2013 Mateusz Loskot, London, UK.
6 // Copyright (c) 2013-2014 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2017.
9 // Modifications copyright (c) 2017 Oracle and/or its affiliates.
11 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
13 // Use, modification and distribution is subject to the Boost Software License,
14 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
15 // http://www.boost.org/LICENSE_1_0.txt)
17 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXTREME_POINTS_HPP
18 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXTREME_POINTS_HPP
23 #include <boost/range.hpp>
25 #include <boost/geometry/algorithms/detail/interior_iterator.hpp>
27 #include <boost/geometry/core/cs.hpp>
28 #include <boost/geometry/core/point_type.hpp>
29 #include <boost/geometry/core/ring_type.hpp>
30 #include <boost/geometry/core/tags.hpp>
32 #include <boost/geometry/geometries/concepts/check.hpp>
33 #include <boost/geometry/iterators/ever_circling_iterator.hpp>
35 #include <boost/geometry/algorithms/detail/assign_box_corners.hpp>
37 #include <boost/geometry/strategies/side.hpp>
39 #include <boost/geometry/util/math.hpp>
42 namespace boost { namespace geometry
46 #ifndef DOXYGEN_NO_DETAIL
47 namespace detail { namespace extreme_points
50 template <std::size_t Dimension>
53 template <typename Point>
54 inline bool operator()(Point const& lhs, Point const& rhs)
56 return geometry::get<Dimension>(lhs) < geometry::get<Dimension>(rhs);
61 template <std::size_t Dimension, typename PointType, typename CoordinateType>
62 inline void move_along_vector(PointType& point, PointType const& extreme, CoordinateType const& base_value)
64 // Moves a point along the vector (point, extreme) in the direction of the extreme point
65 // This adapts the possibly uneven legs of the triangle (or trapezium-like shape)
68 // /base \ => / \ point .
71 // For so-called intruders, it can be used to adapt both legs to the level of "base"
72 // For the base, it can be used to adapt both legs to the level of the max-value of the intruders
73 // If there are 2 or more extreme values, use the one close to 'point' to have a correct vector
75 CoordinateType const value = geometry::get<Dimension>(point);
76 //if (geometry::math::equals(value, base_value))
77 if (value >= base_value)
82 PointType vector = point;
83 subtract_point(vector, extreme);
85 CoordinateType const diff = geometry::get<Dimension>(vector);
87 // diff should never be zero
88 // because of the way our triangle/trapezium is build.
89 // We just return if it would be the case.
90 if (geometry::math::equals(diff, 0))
95 CoordinateType const base_diff = base_value - geometry::get<Dimension>(extreme);
97 multiply_value(vector, base_diff);
98 divide_value(vector, diff);
102 add_point(point, vector);
106 template <std::size_t Dimension, typename Range, typename CoordinateType>
107 inline void move_along_vector(Range& range, CoordinateType const& base_value)
109 if (range.size() >= 3)
111 move_along_vector<Dimension>(range.front(), *(range.begin() + 1), base_value);
112 move_along_vector<Dimension>(range.back(), *(range.rbegin() + 1), base_value);
117 template<typename Ring, std::size_t Dimension>
118 struct extreme_points_on_ring
121 typedef typename geometry::coordinate_type<Ring>::type coordinate_type;
122 typedef typename boost::range_iterator<Ring const>::type range_iterator;
123 typedef typename geometry::point_type<Ring>::type point_type;
125 template <typename CirclingIterator, typename Points>
126 static inline bool extend(CirclingIterator& it,
128 coordinate_type max_coordinate_value,
129 Points& points, int direction)
131 std::size_t safe_index = 0;
136 points.push_back(*it);
138 if (safe_index++ >= n)
140 // E.g.: ring is completely horizontal or vertical (= invalid, but we don't want to have an infinite loop)
143 } while (geometry::math::equals(geometry::get<Dimension>(*it), max_coordinate_value));
148 // Overload without adding to poinst
149 template <typename CirclingIterator>
150 static inline bool extend(CirclingIterator& it,
152 coordinate_type max_coordinate_value,
155 std::size_t safe_index = 0;
160 if (safe_index++ >= n)
162 // E.g.: ring is completely horizontal or vertical (= invalid, but we don't want to have an infinite loop)
165 } while (geometry::math::equals(geometry::get<Dimension>(*it), max_coordinate_value));
170 template <typename CirclingIterator>
171 static inline bool extent_both_sides(Ring const& ring,
173 CirclingIterator& left,
174 CirclingIterator& right)
176 std::size_t const n = boost::size(ring);
177 coordinate_type const max_coordinate_value = geometry::get<Dimension>(extreme);
179 if (! extend(left, n, max_coordinate_value, -1))
183 if (! extend(right, n, max_coordinate_value, +1))
191 template <typename Collection, typename CirclingIterator>
192 static inline bool collect(Ring const& ring,
195 CirclingIterator& left,
196 CirclingIterator& right)
198 std::size_t const n = boost::size(ring);
199 coordinate_type const max_coordinate_value = geometry::get<Dimension>(extreme);
201 // Collects first left, which is reversed (if more than one point) then adds the top itself, then right
202 if (! extend(left, n, max_coordinate_value, points, -1))
206 std::reverse(points.begin(), points.end());
207 points.push_back(extreme);
208 if (! extend(right, n, max_coordinate_value, points, +1))
216 template <typename Extremes, typename Intruders, typename CirclingIterator, typename SideStrategy>
217 static inline void get_intruders(Ring const& ring, CirclingIterator left, CirclingIterator right,
218 Extremes const& extremes,
219 Intruders& intruders,
220 SideStrategy const& strategy)
222 if (boost::size(extremes) < 3)
226 coordinate_type const min_value = geometry::get<Dimension>(*std::min_element(boost::begin(extremes), boost::end(extremes), compare<Dimension>()));
228 // Also select left/right (if Dimension=1)
229 coordinate_type const other_min = geometry::get<1 - Dimension>(*std::min_element(boost::begin(extremes), boost::end(extremes), compare<1 - Dimension>()));
230 coordinate_type const other_max = geometry::get<1 - Dimension>(*std::max_element(boost::begin(extremes), boost::end(extremes), compare<1 - Dimension>()));
232 std::size_t defensive_check_index = 0; // in case we skip over left/right check, collect modifies right too
233 std::size_t const n = boost::size(ring);
234 while (left != right && defensive_check_index < n)
236 coordinate_type const coordinate = geometry::get<Dimension>(*right);
237 coordinate_type const other_coordinate = geometry::get<1 - Dimension>(*right);
238 if (coordinate > min_value && other_coordinate > other_min && other_coordinate < other_max)
240 int const factor = geometry::point_order<Ring>::value == geometry::clockwise ? 1 : -1;
241 int const first_side = strategy.apply(*right, extremes.front(), *(extremes.begin() + 1)) * factor;
242 int const last_side = strategy.apply(*right, *(extremes.rbegin() + 1), extremes.back()) * factor;
244 // If not lying left from any of the extemes side
245 if (first_side != 1 && last_side != 1)
247 //std::cout << "first " << first_side << " last " << last_side << std::endl;
249 // we start at this intrusion until it is handled, and don't affect our initial left iterator
250 CirclingIterator left_intrusion_it = right;
251 typename boost::range_value<Intruders>::type intruder;
252 collect(ring, *right, intruder, left_intrusion_it, right);
254 // Also moves these to base-level, makes sorting possible which can be done in case of self-tangencies
255 // (we might postpone this action, it is often not necessary. However it is not time-consuming)
256 move_along_vector<Dimension>(intruder, min_value);
257 intruders.push_back(intruder);
262 defensive_check_index++;
266 template <typename Extremes, typename Intruders, typename SideStrategy>
267 static inline void get_intruders(Ring const& ring,
268 Extremes const& extremes,
269 Intruders& intruders,
270 SideStrategy const& strategy)
272 std::size_t const n = boost::size(ring);
275 geometry::ever_circling_range_iterator<Ring const> left(ring);
276 geometry::ever_circling_range_iterator<Ring const> right(ring);
279 get_intruders(ring, left, right, extremes, intruders, strategy);
283 template <typename Iterator, typename SideStrategy>
284 static inline bool right_turn(Ring const& ring, Iterator it, SideStrategy const& strategy)
286 typename std::iterator_traits<Iterator>::difference_type const index
287 = std::distance(boost::begin(ring), it);
288 geometry::ever_circling_range_iterator<Ring const> left(ring);
289 geometry::ever_circling_range_iterator<Ring const> right(ring);
293 if (! extent_both_sides(ring, *it, left, right))
298 int const factor = geometry::point_order<Ring>::value == geometry::clockwise ? 1 : -1;
299 int const first_side = strategy.apply(*(right - 1), *right, *left) * factor;
300 int const last_side = strategy.apply(*left, *(left + 1), *right) * factor;
302 //std::cout << "Candidate at " << geometry::wkt(*it) << " first=" << first_side << " last=" << last_side << std::endl;
304 // Turn should not be left (actually, it should be right because extent removes horizontal/collinear cases)
305 return first_side != 1 && last_side != 1;
309 // Gets the extreme segments (top point plus neighbouring points), plus intruders, if any, on the same ring
310 template <typename Extremes, typename Intruders, typename SideStrategy>
311 static inline bool apply(Ring const& ring,
313 Intruders& intruders,
314 SideStrategy const& strategy)
316 std::size_t const n = boost::size(ring);
322 // Get all maxima, usually one. In case of self-tangencies, or self-crossings,
323 // the max might be is not valid. A valid max should make a right turn
324 range_iterator max_it = boost::begin(ring);
325 compare<Dimension> smaller;
326 for (range_iterator it = max_it + 1; it != boost::end(ring); ++it)
328 if (smaller(*max_it, *it) && right_turn(ring, it, strategy))
334 if (max_it == boost::end(ring))
339 typename std::iterator_traits<range_iterator>::difference_type const
340 index = std::distance(boost::begin(ring), max_it);
341 //std::cout << "Extreme point lies at " << index << " having " << geometry::wkt(*max_it) << std::endl;
343 geometry::ever_circling_range_iterator<Ring const> left(ring);
344 geometry::ever_circling_range_iterator<Ring const> right(ring);
348 // Collect all points (often 3) in a temporary vector
349 std::vector<point_type> points;
351 if (! collect(ring, *max_it, points, left, right))
356 //std::cout << "Built vector of " << points.size() << std::endl;
358 coordinate_type const front_value = geometry::get<Dimension>(points.front());
359 coordinate_type const back_value = geometry::get<Dimension>(points.back());
360 coordinate_type const base_value = (std::max)(front_value, back_value);
361 if (front_value < back_value)
363 move_along_vector<Dimension>(points.front(), *(points.begin() + 1), base_value);
367 move_along_vector<Dimension>(points.back(), *(points.rbegin() + 1), base_value);
370 std::copy(points.begin(), points.end(), std::back_inserter(extremes));
372 get_intruders(ring, left, right, extremes, intruders, strategy);
382 }} // namespace detail::extreme_points
383 #endif // DOXYGEN_NO_DETAIL
386 #ifndef DOXYGEN_NO_DISPATCH
394 std::size_t Dimension,
395 typename GeometryTag = typename tag<Geometry>::type
397 struct extreme_points
401 template<typename Ring, std::size_t Dimension>
402 struct extreme_points<Ring, Dimension, ring_tag>
403 : detail::extreme_points::extreme_points_on_ring<Ring, Dimension>
407 template<typename Polygon, std::size_t Dimension>
408 struct extreme_points<Polygon, Dimension, polygon_tag>
410 template <typename Extremes, typename Intruders, typename SideStrategy>
411 static inline bool apply(Polygon const& polygon, Extremes& extremes, Intruders& intruders,
412 SideStrategy const& strategy)
414 typedef typename geometry::ring_type<Polygon>::type ring_type;
415 typedef detail::extreme_points::extreme_points_on_ring
418 > ring_implementation;
420 if (! ring_implementation::apply(geometry::exterior_ring(polygon),
421 extremes, intruders, strategy))
426 // For a polygon, its interior rings can contain intruders
427 typename interior_return_type<Polygon const>::type
428 rings = interior_rings(polygon);
429 for (typename detail::interior_iterator<Polygon const>::type
430 it = boost::begin(rings); it != boost::end(rings); ++it)
432 ring_implementation::get_intruders(*it, extremes, intruders, strategy);
439 template<typename Box>
440 struct extreme_points<Box, 1, box_tag>
442 template <typename Extremes, typename Intruders, typename SideStrategy>
443 static inline bool apply(Box const& box, Extremes& extremes, Intruders& ,
444 SideStrategy const& )
447 geometry::detail::assign_box_corners_oriented<false>(box, extremes);
448 // ll,ul,ur,lr, contains too exactly the right info
453 template<typename Box>
454 struct extreme_points<Box, 0, box_tag>
456 template <typename Extremes, typename Intruders, typename SideStrategy>
457 static inline bool apply(Box const& box, Extremes& extremes, Intruders& ,
458 SideStrategy const& )
461 geometry::detail::assign_box_corners_oriented<false>(box, extremes);
462 // ll,ul,ur,lr, rotate one to start with UL and end with LL
463 std::rotate(extremes.begin(), extremes.begin() + 1, extremes.end());
468 template<typename MultiPolygon, std::size_t Dimension>
469 struct extreme_points<MultiPolygon, Dimension, multi_polygon_tag>
471 template <typename Extremes, typename Intruders, typename SideStrategy>
472 static inline bool apply(MultiPolygon const& multi, Extremes& extremes,
473 Intruders& intruders, SideStrategy const& strategy)
475 // Get one for the very first polygon, that is (for the moment) enough.
476 // It is not guaranteed the "extreme" then, but for the current purpose
477 // (point_on_surface) it can just be this point.
478 if (boost::size(multi) >= 1)
480 return extreme_points
482 typename boost::range_value<MultiPolygon const>::type,
485 >::apply(*boost::begin(multi), extremes, intruders, strategy);
492 } // namespace dispatch
493 #endif // DOXYGEN_NO_DISPATCH
497 \brief Returns extreme points (for Edge=1 in dimension 1, so the top,
498 for Edge=0 in dimension 0, the right side)
499 \note We could specify a strategy (less/greater) to get bottom/left side too. However, until now we don't need that.
507 typename SideStrategy
509 inline bool extreme_points(Geometry const& geometry,
511 Intruders& intruders,
512 SideStrategy const& strategy)
514 concepts::check<Geometry const>();
516 // Extremes is not required to follow a geometry concept (but it should support an output iterator),
517 // but its elements should fulfil the point-concept
518 concepts::check<typename boost::range_value<Extremes>::type>();
520 // Intruders should contain collections which value type is point-concept
521 // Extremes might be anything (supporting an output iterator), but its elements should fulfil the point-concept
524 typename boost::range_value
526 typename boost::range_value<Intruders>::type
531 return dispatch::extreme_points
535 >::apply(geometry, extremes, intruders, strategy);
540 }} // namespace boost::geometry
543 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXTREME_POINTS_HPP