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) 2013-2015 Adam Wulkiewicz, Lodz, Poland.
8 // This file was modified by Oracle on 2015-2020.
9 // Modifications copyright (c) 2016-2020, Oracle and/or its affiliates.
11 // Contributed and/or modified by Adam Wulkiewicz, 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 // Use, modification and distribution is subject to the Boost Software License,
17 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
20 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BOX_IN_BOX_HPP
21 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BOX_IN_BOX_HPP
24 #include <boost/geometry/core/access.hpp>
25 #include <boost/geometry/core/coordinate_dimension.hpp>
26 #include <boost/geometry/strategies/covered_by.hpp>
27 #include <boost/geometry/strategies/within.hpp>
28 #include <boost/geometry/util/normalize_spheroidal_coordinates.hpp>
31 namespace boost { namespace geometry { namespace strategy
39 #ifndef DOXYGEN_NO_DETAIL
44 struct box_within_coord
46 template <typename BoxContainedValue, typename BoxContainingValue>
47 static inline bool apply(BoxContainedValue const& bed_min,
48 BoxContainedValue const& bed_max,
49 BoxContainingValue const& bing_min,
50 BoxContainingValue const& bing_max)
52 return bing_min <= bed_min && bed_max <= bing_max // contained in containing
53 && bed_min < bed_max; // interiors overlap
58 struct box_covered_by_coord
60 template <typename BoxContainedValue, typename BoxContainingValue>
61 static inline bool apply(BoxContainedValue const& bed_min,
62 BoxContainedValue const& bed_max,
63 BoxContainingValue const& bing_min,
64 BoxContainingValue const& bing_max)
66 return bed_min >= bing_min && bed_max <= bing_max;
71 struct box_within_longitude_diff
73 template <typename CalcT>
74 static inline bool apply(CalcT const& diff_ed)
76 return diff_ed > CalcT(0);
80 struct box_covered_by_longitude_diff
82 template <typename CalcT>
83 static inline bool apply(CalcT const&)
89 template <typename Geometry,
91 typename InteriorCheck>
92 struct box_longitude_range
94 template <typename BoxContainedValue, typename BoxContainingValue>
95 static inline bool apply(BoxContainedValue const& bed_min,
96 BoxContainedValue const& bed_max,
97 BoxContainingValue const& bing_min,
98 BoxContainingValue const& bing_max)
100 typedef typename select_most_precise
105 typedef typename geometry::detail::cs_angular_units<Geometry>::type units_t;
106 typedef math::detail::constants_on_spheroid<calc_t, units_t> constants;
108 if (CoordCheck::apply(bed_min, bed_max, bing_min, bing_max))
113 // min <= max <=> diff >= 0
114 calc_t const diff_ed = bed_max - bed_min;
115 calc_t const diff_ing = bing_max - bing_min;
117 // if containing covers the whole globe it contains all
118 if (diff_ing >= constants::period())
123 // if containing is smaller it cannot contain
124 // and check interior (within vs covered_by)
125 if (diff_ing < diff_ed || ! InteriorCheck::apply(diff_ed))
130 // calculate positive longitude translation with bing_min as origin
131 calc_t const diff_min = math::longitude_distance_unsigned<units_t>(bing_min, bed_min);
133 // max of contained translated into the containing origin must be lesser than max of containing
134 return bing_min + diff_min + diff_ed <= bing_max
135 /*|| bing_max - diff_min - diff_ed >= bing_min*/;
142 template <typename, std::size_t, typename> class SubStrategy,
144 std::size_t Dimension,
145 std::size_t DimensionCount
147 struct relate_box_box_loop
149 template <typename Box1, typename Box2>
150 static inline bool apply(Box1 const& b_contained, Box2 const& b_containing)
152 assert_dimension_equal<Box1, Box2>();
154 if (! SubStrategy<Box1, Dimension, CSTag>::apply(
155 get<min_corner, Dimension>(b_contained),
156 get<max_corner, Dimension>(b_contained),
157 get<min_corner, Dimension>(b_containing),
158 get<max_corner, Dimension>(b_containing)
165 return within::detail::relate_box_box_loop
168 Dimension + 1, DimensionCount
169 >::apply(b_contained, b_containing);
175 template <typename, std::size_t, typename> class SubStrategy,
177 std::size_t DimensionCount
179 struct relate_box_box_loop<SubStrategy, CSTag, DimensionCount, DimensionCount>
181 template <typename Box1, typename Box2>
182 static inline bool apply(Box1 const& , Box2 const& )
189 template <typename Geometry, std::size_t Dimension, typename CSTag>
190 struct box_within_range
191 : within::detail::box_within_coord
195 template <typename Geometry, std::size_t Dimension, typename CSTag>
196 struct box_covered_by_range
197 : within::detail::box_covered_by_coord
201 // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
202 template <typename Geometry>
203 struct box_within_range<Geometry, 0, spherical_tag>
204 : within::detail::box_longitude_range
207 within::detail::box_within_coord,
208 within::detail::box_within_longitude_diff
213 template <typename Geometry>
214 struct box_covered_by_range<Geometry, 0, spherical_tag>
215 : within::detail::box_longitude_range
218 within::detail::box_covered_by_coord,
219 within::detail::box_covered_by_longitude_diff
224 } // namespace detail
225 #endif // DOXYGEN_NO_DETAIL
228 struct cartesian_box_box
230 template <typename Box1, typename Box2>
231 static inline bool apply(Box1 const& box1, Box2 const& box2)
233 return within::detail::relate_box_box_loop
235 within::detail::box_within_range,
237 0, dimension<Box1>::type::value
238 >::apply(box1, box2);
242 struct spherical_box_box
244 template <typename Box1, typename Box2>
245 static inline bool apply(Box1 const& box1, Box2 const& box2)
247 return within::detail::relate_box_box_loop
249 within::detail::box_within_range,
251 0, dimension<Box1>::type::value
252 >::apply(box1, box2);
257 } // namespace within
264 struct cartesian_box_box
266 template <typename Box1, typename Box2>
267 static inline bool apply(Box1 const& box1, Box2 const& box2)
269 return within::detail::relate_box_box_loop
271 within::detail::box_covered_by_range,
273 0, dimension<Box1>::type::value
274 >::apply(box1, box2);
278 struct spherical_box_box
280 template <typename Box1, typename Box2>
281 static inline bool apply(Box1 const& box1, Box2 const& box2)
283 return within::detail::relate_box_box_loop
285 within::detail::box_covered_by_range,
287 0, dimension<Box1>::type::value
288 >::apply(box1, box2);
296 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
299 namespace within { namespace services
302 template <typename BoxContained, typename BoxContaining>
303 struct default_strategy
305 BoxContained, BoxContaining,
307 areal_tag, areal_tag,
308 cartesian_tag, cartesian_tag
311 typedef cartesian_box_box type;
314 // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
315 template <typename BoxContained, typename BoxContaining>
316 struct default_strategy
318 BoxContained, BoxContaining,
320 areal_tag, areal_tag,
321 spherical_tag, spherical_tag
324 typedef spherical_box_box type;
328 }} // namespace within::services
330 namespace covered_by { namespace services
333 template <typename BoxContained, typename BoxContaining>
334 struct default_strategy
336 BoxContained, BoxContaining,
338 areal_tag, areal_tag,
339 cartesian_tag, cartesian_tag
342 typedef cartesian_box_box type;
345 // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
346 template <typename BoxContained, typename BoxContaining>
347 struct default_strategy
349 BoxContained, BoxContaining,
351 areal_tag, areal_tag,
352 spherical_tag, spherical_tag
355 typedef spherical_box_box type;
359 }} // namespace covered_by::services
362 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
365 }}} // namespace boost::geometry::strategy
367 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BOX_IN_BOX_HPP