1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2015 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
6 // Copyright (c) 2014-2015 Samuel Debionne, Grenoble, France.
8 // This file was modified by Oracle on 2015.
9 // Modifications copyright (c) 2015, Oracle and/or its affiliates.
11 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
13 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
14 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
16 // Distributed under the Boost Software License, Version 1.0.
17 // (See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
20 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXPAND_POINT_HPP
21 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXPAND_POINT_HPP
26 #include <boost/mpl/assert.hpp>
27 #include <boost/type_traits/is_same.hpp>
29 #include <boost/geometry/core/access.hpp>
30 #include <boost/geometry/core/coordinate_dimension.hpp>
31 #include <boost/geometry/core/coordinate_system.hpp>
32 #include <boost/geometry/core/coordinate_type.hpp>
33 #include <boost/geometry/core/tags.hpp>
35 #include <boost/geometry/util/math.hpp>
36 #include <boost/geometry/util/select_coordinate_type.hpp>
38 #include <boost/geometry/strategies/compare.hpp>
39 #include <boost/geometry/policies/compare.hpp>
41 #include <boost/geometry/algorithms/detail/normalize.hpp>
42 #include <boost/geometry/algorithms/detail/envelope/transform_units.hpp>
44 #include <boost/geometry/algorithms/dispatch/expand.hpp>
47 namespace boost { namespace geometry
50 #ifndef DOXYGEN_NO_DETAIL
51 namespace detail { namespace expand
57 typename StrategyLess, typename StrategyGreater,
58 std::size_t Dimension, std::size_t DimensionCount
62 template <typename Box, typename Point>
63 static inline void apply(Box& box, Point const& source)
65 typedef typename strategy::compare::detail::select_strategy
67 StrategyLess, 1, Point, Dimension
70 typedef typename strategy::compare::detail::select_strategy
72 StrategyGreater, -1, Point, Dimension
75 typedef typename select_coordinate_type
78 >::type coordinate_type;
83 coordinate_type const coord = get<Dimension>(source);
85 if (less(coord, get<min_corner, Dimension>(box)))
87 set<min_corner, Dimension>(box, coord);
90 if (greater(coord, get<max_corner, Dimension>(box)))
92 set<max_corner, Dimension>(box, coord);
97 StrategyLess, StrategyGreater, Dimension + 1, DimensionCount
98 >::apply(box, source);
105 typename StrategyLess, typename StrategyGreater, std::size_t DimensionCount
109 StrategyLess, StrategyGreater, DimensionCount, DimensionCount
112 template <typename Box, typename Point>
113 static inline void apply(Box&, Point const&) {}
117 // implementation for the spherical equatorial and geographic coordinate systems
120 typename StrategyLess,
121 typename StrategyGreater,
122 std::size_t DimensionCount
124 struct point_loop_on_spheroid
126 template <typename Box, typename Point>
127 static inline void apply(Box& box, Point const& point)
129 typedef typename point_type<Box>::type box_point_type;
130 typedef typename coordinate_type<Box>::type box_coordinate_type;
132 typedef math::detail::constants_on_spheroid
135 typename coordinate_system<Box>::type::units
138 // normalize input point and input box
139 Point p_normalized = detail::return_normalized<Point>(point);
140 detail::normalize(box, box);
142 // transform input point to be of the same type as the box point
143 box_point_type box_point;
144 detail::envelope::transform_units(p_normalized, box_point);
146 box_coordinate_type p_lon = geometry::get<0>(box_point);
147 box_coordinate_type p_lat = geometry::get<1>(box_point);
149 typename coordinate_type<Box>::type
150 b_lon_min = geometry::get<min_corner, 0>(box),
151 b_lat_min = geometry::get<min_corner, 1>(box),
152 b_lon_max = geometry::get<max_corner, 0>(box),
153 b_lat_max = geometry::get<max_corner, 1>(box);
155 if (math::equals(math::abs(p_lat), constants::max_latitude()))
157 // the point of expansion is the either the north or the
158 // south pole; the only important coordinate here is the
159 // pole's latitude, as the longitude can be anything;
160 // we, thus, take into account the point's latitude only and return
161 geometry::set<min_corner, 1>(box, (std::min)(p_lat, b_lat_min));
162 geometry::set<max_corner, 1>(box, (std::max)(p_lat, b_lat_max));
166 if (math::equals(b_lat_min, b_lat_max)
167 && math::equals(math::abs(b_lat_min), constants::max_latitude()))
169 // the box degenerates to either the north or the south pole;
170 // the only important coordinate here is the pole's latitude,
171 // as the longitude can be anything;
172 // we thus take into account the box's latitude only and return
173 geometry::set<min_corner, 0>(box, p_lon);
174 geometry::set<min_corner, 1>(box, (std::min)(p_lat, b_lat_min));
175 geometry::set<max_corner, 0>(box, p_lon);
176 geometry::set<max_corner, 1>(box, (std::max)(p_lat, b_lat_max));
181 b_lat_min = (std::min)(b_lat_min, p_lat);
182 b_lat_max = (std::max)(b_lat_max, p_lat);
185 if (math::smaller(p_lon, b_lon_min))
187 box_coordinate_type p_lon_shifted = p_lon + constants::period();
189 if (math::larger(p_lon_shifted, b_lon_max))
191 // here we could check using: ! math::larger(.., ..)
192 if (math::smaller(b_lon_min - p_lon, p_lon_shifted - b_lon_max))
198 b_lon_max = p_lon_shifted;
202 else if (math::larger(p_lon, b_lon_max))
204 // in this case, and since p_lon is normalized in the range
205 // (-180, 180], we must have that b_lon_max <= 180
207 && math::larger(p_lon - b_lon_max,
208 constants::period() - p_lon + b_lon_min))
211 b_lon_max += constants::period();
219 geometry::set<min_corner, 0>(box, b_lon_min);
220 geometry::set<min_corner, 1>(box, b_lat_min);
221 geometry::set<max_corner, 0>(box, b_lon_max);
222 geometry::set<max_corner, 1>(box, b_lat_max);
226 StrategyLess, StrategyGreater, 2, DimensionCount
227 >::apply(box, point);
232 }} // namespace detail::expand
233 #endif // DOXYGEN_NO_DETAIL
235 #ifndef DOXYGEN_NO_DISPATCH
240 // Box + point -> new box containing also point
243 typename BoxOut, typename Point,
244 typename StrategyLess, typename StrategyGreater,
245 typename CSTagOut, typename CSTag
250 StrategyLess, StrategyGreater,
253 > : detail::expand::point_loop
255 StrategyLess, StrategyGreater, 0, dimension<Point>::value
258 BOOST_MPL_ASSERT_MSG((boost::is_same<CSTagOut, CSTag>::value),
259 COORDINATE_SYSTEMS_MUST_BE_THE_SAME,
260 (types<CSTagOut, CSTag>()));
265 typename BoxOut, typename Point,
266 typename StrategyLess, typename StrategyGreater
271 StrategyLess, StrategyGreater,
273 spherical_equatorial_tag, spherical_equatorial_tag
274 > : detail::expand::point_loop_on_spheroid
276 StrategyLess, StrategyGreater, dimension<Point>::value
282 typename BoxOut, typename Point,
283 typename StrategyLess, typename StrategyGreater
288 StrategyLess, StrategyGreater,
290 geographic_tag, geographic_tag
291 > : detail::expand::point_loop_on_spheroid
293 StrategyLess, StrategyGreater, dimension<Point>::value
298 } // namespace dispatch
299 #endif // DOXYGEN_NO_DISPATCH
301 }} // namespace boost::geometry
303 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_EXPAND_POINT_HPP