]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/geometry/strategies/cartesian/box_in_box.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / geometry / strategies / cartesian / box_in_box.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2
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.
7
8 // This file was modified by Oracle on 2015-2020.
9 // Modifications copyright (c) 2016-2020, Oracle and/or its affiliates.
10
11 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
12
13 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
14 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
15
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)
19
20 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BOX_IN_BOX_HPP
21 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BOX_IN_BOX_HPP
22
23
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>
29
30
31 namespace boost { namespace geometry { namespace strategy
32 {
33
34
35 namespace within
36 {
37
38
39 #ifndef DOXYGEN_NO_DETAIL
40 namespace detail
41 {
42
43
44 struct box_within_coord
45 {
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)
51 {
52 return bing_min <= bed_min && bed_max <= bing_max // contained in containing
53 && bed_min < bed_max; // interiors overlap
54 }
55 };
56
57
58 struct box_covered_by_coord
59 {
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)
65 {
66 return bed_min >= bing_min && bed_max <= bing_max;
67 }
68 };
69
70
71 struct box_within_longitude_diff
72 {
73 template <typename CalcT>
74 static inline bool apply(CalcT const& diff_ed)
75 {
76 return diff_ed > CalcT(0);
77 }
78 };
79
80 struct box_covered_by_longitude_diff
81 {
82 template <typename CalcT>
83 static inline bool apply(CalcT const&)
84 {
85 return true;
86 }
87 };
88
89 template <typename Geometry,
90 typename CoordCheck,
91 typename InteriorCheck>
92 struct box_longitude_range
93 {
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)
99 {
100 typedef typename select_most_precise
101 <
102 BoxContainedValue,
103 BoxContainingValue
104 >::type calc_t;
105 typedef typename geometry::detail::cs_angular_units<Geometry>::type units_t;
106 typedef math::detail::constants_on_spheroid<calc_t, units_t> constants;
107
108 if (CoordCheck::apply(bed_min, bed_max, bing_min, bing_max))
109 {
110 return true;
111 }
112
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;
116
117 // if containing covers the whole globe it contains all
118 if (diff_ing >= constants::period())
119 {
120 return true;
121 }
122
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))
126 {
127 return false;
128 }
129
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);
132
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*/;
136 }
137 };
138
139
140 template
141 <
142 template <typename, std::size_t, typename> class SubStrategy,
143 typename CSTag,
144 std::size_t Dimension,
145 std::size_t DimensionCount
146 >
147 struct relate_box_box_loop
148 {
149 template <typename Box1, typename Box2>
150 static inline bool apply(Box1 const& b_contained, Box2 const& b_containing)
151 {
152 assert_dimension_equal<Box1, Box2>();
153
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)
159 )
160 )
161 {
162 return false;
163 }
164
165 return within::detail::relate_box_box_loop
166 <
167 SubStrategy, CSTag,
168 Dimension + 1, DimensionCount
169 >::apply(b_contained, b_containing);
170 }
171 };
172
173 template
174 <
175 template <typename, std::size_t, typename> class SubStrategy,
176 typename CSTag,
177 std::size_t DimensionCount
178 >
179 struct relate_box_box_loop<SubStrategy, CSTag, DimensionCount, DimensionCount>
180 {
181 template <typename Box1, typename Box2>
182 static inline bool apply(Box1 const& , Box2 const& )
183 {
184 return true;
185 }
186 };
187
188
189 template <typename Geometry, std::size_t Dimension, typename CSTag>
190 struct box_within_range
191 : within::detail::box_within_coord
192 {};
193
194
195 template <typename Geometry, std::size_t Dimension, typename CSTag>
196 struct box_covered_by_range
197 : within::detail::box_covered_by_coord
198 {};
199
200
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
205 <
206 Geometry,
207 within::detail::box_within_coord,
208 within::detail::box_within_longitude_diff
209 >
210 {};
211
212
213 template <typename Geometry>
214 struct box_covered_by_range<Geometry, 0, spherical_tag>
215 : within::detail::box_longitude_range
216 <
217 Geometry,
218 within::detail::box_covered_by_coord,
219 within::detail::box_covered_by_longitude_diff
220 >
221 {};
222
223
224 } // namespace detail
225 #endif // DOXYGEN_NO_DETAIL
226
227
228 struct cartesian_box_box
229 {
230 template <typename Box1, typename Box2>
231 static inline bool apply(Box1 const& box1, Box2 const& box2)
232 {
233 return within::detail::relate_box_box_loop
234 <
235 within::detail::box_within_range,
236 cartesian_tag,
237 0, dimension<Box1>::type::value
238 >::apply(box1, box2);
239 }
240 };
241
242 struct spherical_box_box
243 {
244 template <typename Box1, typename Box2>
245 static inline bool apply(Box1 const& box1, Box2 const& box2)
246 {
247 return within::detail::relate_box_box_loop
248 <
249 within::detail::box_within_range,
250 spherical_tag,
251 0, dimension<Box1>::type::value
252 >::apply(box1, box2);
253 }
254 };
255
256
257 } // namespace within
258
259
260 namespace covered_by
261 {
262
263
264 struct cartesian_box_box
265 {
266 template <typename Box1, typename Box2>
267 static inline bool apply(Box1 const& box1, Box2 const& box2)
268 {
269 return within::detail::relate_box_box_loop
270 <
271 within::detail::box_covered_by_range,
272 cartesian_tag,
273 0, dimension<Box1>::type::value
274 >::apply(box1, box2);
275 }
276 };
277
278 struct spherical_box_box
279 {
280 template <typename Box1, typename Box2>
281 static inline bool apply(Box1 const& box1, Box2 const& box2)
282 {
283 return within::detail::relate_box_box_loop
284 <
285 within::detail::box_covered_by_range,
286 spherical_tag,
287 0, dimension<Box1>::type::value
288 >::apply(box1, box2);
289 }
290 };
291
292
293 }
294
295
296 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
297
298
299 namespace within { namespace services
300 {
301
302 template <typename BoxContained, typename BoxContaining>
303 struct default_strategy
304 <
305 BoxContained, BoxContaining,
306 box_tag, box_tag,
307 areal_tag, areal_tag,
308 cartesian_tag, cartesian_tag
309 >
310 {
311 typedef cartesian_box_box type;
312 };
313
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
317 <
318 BoxContained, BoxContaining,
319 box_tag, box_tag,
320 areal_tag, areal_tag,
321 spherical_tag, spherical_tag
322 >
323 {
324 typedef spherical_box_box type;
325 };
326
327
328 }} // namespace within::services
329
330 namespace covered_by { namespace services
331 {
332
333 template <typename BoxContained, typename BoxContaining>
334 struct default_strategy
335 <
336 BoxContained, BoxContaining,
337 box_tag, box_tag,
338 areal_tag, areal_tag,
339 cartesian_tag, cartesian_tag
340 >
341 {
342 typedef cartesian_box_box type;
343 };
344
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
348 <
349 BoxContained, BoxContaining,
350 box_tag, box_tag,
351 areal_tag, areal_tag,
352 spherical_tag, spherical_tag
353 >
354 {
355 typedef spherical_box_box type;
356 };
357
358
359 }} // namespace covered_by::services
360
361
362 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
363
364
365 }}} // namespace boost::geometry::strategy
366
367 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_BOX_IN_BOX_HPP