1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2015-2017, Oracle and/or its affiliates.
5 // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
6 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
7 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
9 // Distributed under the Boost Software License, Version 1.0.
10 // (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_ENVELOPE_RANGE_OF_BOXES_HPP
14 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_ENVELOPE_RANGE_OF_BOXES_HPP
21 #include <boost/range.hpp>
23 #include <boost/geometry/core/access.hpp>
24 #include <boost/geometry/core/assert.hpp>
25 #include <boost/geometry/core/coordinate_system.hpp>
26 #include <boost/geometry/core/coordinate_type.hpp>
28 #include <boost/geometry/util/math.hpp>
29 #include <boost/geometry/util/range.hpp>
31 #include <boost/geometry/algorithms/detail/convert_point_to_point.hpp>
32 #include <boost/geometry/algorithms/detail/max_interval_gap.hpp>
33 #include <boost/geometry/algorithms/detail/expand/indexed.hpp>
36 namespace boost { namespace geometry
39 #ifndef DOXYGEN_NO_DETAIL
40 namespace detail { namespace envelope
45 class longitude_interval
47 typedef T const& reference_type;
51 typedef T difference_type;
53 longitude_interval(T const& left, T const& right)
59 template <std::size_t Index>
60 reference_type get() const
65 difference_type length() const
67 return get<1>() - get<0>();
75 template <typename Units>
76 struct envelope_range_of_longitudes
78 template <std::size_t Index>
81 template <typename Interval>
82 inline bool operator()(Interval const& i1, Interval const& i2) const
84 return math::smaller(i1.template get<Index>(),
85 i2.template get<Index>());
89 template <typename RangeOfLongitudeIntervals, typename Longitude>
90 static inline void apply(RangeOfLongitudeIntervals const& range,
91 Longitude& lon_min, Longitude& lon_max)
93 typedef typename math::detail::constants_on_spheroid
98 Longitude const zero = 0;
99 Longitude const period = constants::period();
101 lon_min = lon_max = zero;
103 // the range of longitude intervals can be empty if all input boxes
104 // degenerate to the north or south pole (or combination of the two)
105 // in this case the initialization values for lon_min and
106 // lon_max are valid choices
107 if (! boost::empty(range))
109 lon_min = std::min_element(boost::begin(range),
111 longitude_less<0>())->template get<0>();
112 lon_max = std::max_element(boost::begin(range),
114 longitude_less<1>())->template get<1>();
116 if (math::larger(lon_max - lon_min, constants::half_period()))
118 Longitude max_gap_left, max_gap_right;
119 Longitude max_gap = geometry::maximum_gap(range,
123 BOOST_GEOMETRY_ASSERT(! math::larger(lon_min, lon_max));
124 BOOST_GEOMETRY_ASSERT
125 (! math::larger(lon_max, constants::max_longitude()));
126 BOOST_GEOMETRY_ASSERT
127 (! math::smaller(lon_min, constants::min_longitude()));
129 BOOST_GEOMETRY_ASSERT
130 (! math::larger(max_gap_left, max_gap_right));
131 BOOST_GEOMETRY_ASSERT
132 (! math::larger(max_gap_right, constants::max_longitude()));
133 BOOST_GEOMETRY_ASSERT
134 (! math::smaller(max_gap_left, constants::min_longitude()));
136 if (math::larger(max_gap, zero))
138 Longitude wrapped_gap = period + lon_min - lon_max;
139 if (math::larger(max_gap, wrapped_gap))
141 lon_min = max_gap_right;
142 lon_max = max_gap_left + period;
151 template <std::size_t Dimension, std::size_t DimensionCount>
152 struct envelope_range_of_boxes_by_expansion
154 template <typename RangeOfBoxes, typename Box, typename Strategy>
155 static inline void apply(RangeOfBoxes const& range_of_boxes,
157 Strategy const& strategy)
159 typedef typename boost::range_value<RangeOfBoxes>::type box_type;
161 typedef typename boost::range_iterator
164 >::type iterator_type;
166 // first initialize MBR
167 detail::indexed_point_view<Box, min_corner> mbr_min(mbr);
168 detail::indexed_point_view<Box, max_corner> mbr_max(mbr);
170 detail::indexed_point_view<box_type const, min_corner>
171 first_box_min(range::front(range_of_boxes));
173 detail::indexed_point_view<box_type const, max_corner>
174 first_box_max(range::front(range_of_boxes));
176 detail::conversion::point_to_point
178 detail::indexed_point_view<box_type const, min_corner>,
179 detail::indexed_point_view<Box, min_corner>,
182 >::apply(first_box_min, mbr_min);
184 detail::conversion::point_to_point
186 detail::indexed_point_view<box_type const, max_corner>,
187 detail::indexed_point_view<Box, max_corner>,
190 >::apply(first_box_max, mbr_max);
192 // now expand using the remaining boxes
193 iterator_type it = boost::begin(range_of_boxes);
194 for (++it; it != boost::end(range_of_boxes); ++it)
196 detail::expand::indexed_loop
201 >::apply(mbr, *it, strategy);
203 detail::expand::indexed_loop
208 >::apply(mbr, *it, strategy);
215 struct envelope_range_of_boxes
217 template <std::size_t Index>
220 template <typename Box>
221 inline bool operator()(Box const& box1, Box const& box2) const
223 return math::smaller(geometry::get<Index, 1>(box1),
224 geometry::get<Index, 1>(box2));
228 template <typename RangeOfBoxes, typename Box, typename Strategy>
229 static inline void apply(RangeOfBoxes const& range_of_boxes,
231 Strategy const& strategy)
233 // boxes in the range are assumed to be normalized already
235 typedef typename boost::range_value<RangeOfBoxes>::type box_type;
236 typedef typename coordinate_type<box_type>::type coordinate_type;
237 typedef typename coordinate_system<box_type>::type::units units_type;
238 typedef typename boost::range_iterator
241 >::type iterator_type;
243 static const bool is_equatorial = ! boost::is_same
245 typename cs_tag<box_type>::type,
249 typedef math::detail::constants_on_spheroid
251 coordinate_type, units_type, is_equatorial
254 typedef longitude_interval<coordinate_type> interval_type;
255 typedef std::vector<interval_type> interval_range_type;
257 BOOST_GEOMETRY_ASSERT(! boost::empty(range_of_boxes));
259 iterator_type it_min = std::min_element(boost::begin(range_of_boxes),
260 boost::end(range_of_boxes),
261 latitude_less<min_corner>());
262 iterator_type it_max = std::max_element(boost::begin(range_of_boxes),
263 boost::end(range_of_boxes),
264 latitude_less<max_corner>());
266 coordinate_type const min_longitude = constants::min_longitude();
267 coordinate_type const max_longitude = constants::max_longitude();
268 coordinate_type const period = constants::period();
270 interval_range_type intervals;
271 for (iterator_type it = boost::begin(range_of_boxes);
272 it != boost::end(range_of_boxes);
275 coordinate_type lat_min = geometry::get<min_corner, 1>(*it);
276 coordinate_type lat_max = geometry::get<max_corner, 1>(*it);
277 if (math::equals(lat_min, constants::max_latitude())
278 || math::equals(lat_max, constants::min_latitude()))
280 // if the box degenerates to the south or north pole
285 coordinate_type lon_left = geometry::get<min_corner, 0>(*it);
286 coordinate_type lon_right = geometry::get<max_corner, 0>(*it);
288 if (math::larger(lon_right, max_longitude))
290 intervals.push_back(interval_type(lon_left, max_longitude));
292 (interval_type(min_longitude, lon_right - period));
296 intervals.push_back(interval_type(lon_left, lon_right));
300 coordinate_type lon_min = 0;
301 coordinate_type lon_max = 0;
302 envelope_range_of_longitudes
305 >::apply(intervals, lon_min, lon_max);
307 // do not convert units; conversion will be performed at a
310 // assign now the min/max longitude/latitude values
311 detail::indexed_point_view<Box, min_corner> mbr_min(mbr);
312 detail::indexed_point_view<Box, max_corner> mbr_max(mbr);
314 geometry::set<0>(mbr_min, lon_min);
315 geometry::set<1>(mbr_min, geometry::get<min_corner, 1>(*it_min));
316 geometry::set<0>(mbr_max, lon_max);
317 geometry::set<1>(mbr_max, geometry::get<max_corner, 1>(*it_max));
319 // what remains to be done is to compute the envelope range
320 // for the remaining dimensions (if any)
321 envelope_range_of_boxes_by_expansion
323 2, dimension<Box>::value
324 >::apply(range_of_boxes, mbr, strategy);
329 }} // namespace detail::envelope
330 #endif // DOXYGEN_NO_DETAIL
332 }} // namespace boost::geometry
334 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_ENVELOPE_RANGE_OF_BOXES_HPP