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, 2016, 2017, 2019.
9 // Modifications copyright (c) 2016-2019, 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 } // namespace detail
190 #endif // DOXYGEN_NO_DETAIL
193 // for backward compatibility
194 template <typename Geometry, std::size_t Dimension, typename CSTag>
195 struct box_within_range
196 : within::detail::box_within_coord
200 template <typename Geometry, std::size_t Dimension, typename CSTag>
201 struct box_covered_by_range
202 : within::detail::box_covered_by_coord
206 // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
207 template <typename Geometry>
208 struct box_within_range<Geometry, 0, spherical_tag>
209 : within::detail::box_longitude_range
212 within::detail::box_within_coord,
213 within::detail::box_within_longitude_diff
218 template <typename Geometry>
219 struct box_covered_by_range<Geometry, 0, spherical_tag>
220 : within::detail::box_longitude_range
223 within::detail::box_covered_by_coord,
224 within::detail::box_covered_by_longitude_diff
229 // for backward compatibility
234 template <typename, std::size_t, typename> class SubStrategy = box_within_range
238 template <typename Box1, typename Box2>
239 static inline bool apply(Box1 const& box1, Box2 const& box2)
241 typedef typename tag_cast
243 typename geometry::cs_tag<Box1>::type,
247 return within::detail::relate_box_box_loop
250 0, dimension<Box1>::type::value
251 >::apply(box1, box2);
256 struct cartesian_box_box
258 template <typename Box1, typename Box2>
259 static inline bool apply(Box1 const& box1, Box2 const& box2)
261 return within::detail::relate_box_box_loop
265 0, dimension<Box1>::type::value
266 >::apply(box1, box2);
270 struct spherical_box_box
272 template <typename Box1, typename Box2>
273 static inline bool apply(Box1 const& box1, Box2 const& box2)
275 return within::detail::relate_box_box_loop
279 0, dimension<Box1>::type::value
280 >::apply(box1, box2);
285 } // namespace within
292 struct cartesian_box_box
294 template <typename Box1, typename Box2>
295 static inline bool apply(Box1 const& box1, Box2 const& box2)
297 return within::detail::relate_box_box_loop
299 strategy::within::box_covered_by_range,
301 0, dimension<Box1>::type::value
302 >::apply(box1, box2);
306 struct spherical_box_box
308 template <typename Box1, typename Box2>
309 static inline bool apply(Box1 const& box1, Box2 const& box2)
311 return within::detail::relate_box_box_loop
313 strategy::within::box_covered_by_range,
315 0, dimension<Box1>::type::value
316 >::apply(box1, box2);
324 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
327 namespace within { namespace services
330 template <typename BoxContained, typename BoxContaining>
331 struct default_strategy
333 BoxContained, BoxContaining,
335 areal_tag, areal_tag,
336 cartesian_tag, cartesian_tag
339 typedef cartesian_box_box type;
342 // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
343 template <typename BoxContained, typename BoxContaining>
344 struct default_strategy
346 BoxContained, BoxContaining,
348 areal_tag, areal_tag,
349 spherical_tag, spherical_tag
352 typedef spherical_box_box type;
356 }} // namespace within::services
358 namespace covered_by { namespace services
361 template <typename BoxContained, typename BoxContaining>
362 struct default_strategy
364 BoxContained, BoxContaining,
366 areal_tag, areal_tag,
367 cartesian_tag, cartesian_tag
370 typedef cartesian_box_box type;
373 // spherical_equatorial_tag, spherical_polar_tag and geographic_cat are casted to spherical_tag
374 template <typename BoxContained, typename BoxContaining>
375 struct default_strategy
377 BoxContained, BoxContaining,
379 areal_tag, areal_tag,
380 spherical_tag, spherical_tag
383 typedef spherical_box_box type;
387 }} // namespace covered_by::services
390 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
393 }}} // namespace boost::geometry::strategy
395 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BOX_IN_BOX_HPP