]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/test/algorithms/envelope_expand/test_envelope_expand_on_spheroid.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / geometry / test / algorithms / envelope_expand / test_envelope_expand_on_spheroid.hpp
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3
4 // Copyright (c) 2015-2021, Oracle and/or its affiliates.
5 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
6 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
7
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
10
11 #ifndef BOOST_GEOMETRY_TEST_ENVELOPE_EXPAND_ON_SPHEROID_HPP
12 #define BOOST_GEOMETRY_TEST_ENVELOPE_EXPAND_ON_SPHEROID_HPP
13
14
15 #include <algorithm>
16 #include <cmath>
17 #include <cstddef>
18 #include <iostream>
19
20 #include <boost/geometry/core/access.hpp>
21 #include <boost/geometry/core/coordinate_dimension.hpp>
22 #include <boost/geometry/core/cs.hpp>
23
24 #include <boost/geometry/util/condition.hpp>
25 #include <boost/geometry/util/math.hpp>
26
27 #include <boost/geometry/views/detail/indexed_point_view.hpp>
28
29 #include <boost/geometry/algorithms/assign.hpp>
30
31
32 struct rng
33 {
34 typedef double type;
35
36 rng(double l, double h)
37 : lo(l), hi(h)
38 {
39 BOOST_GEOMETRY_ASSERT(lo <= hi);
40 }
41
42 friend rng operator*(rng const& l, double v) { return rng(l.lo * v, l.hi * v); }
43
44 friend bool operator<=(rng const& l, rng const& r) { return l.lo <= r.hi; }
45 friend bool operator<=(double l, rng const& r) { return l <= r.hi; }
46 friend bool operator<=(rng const& l, double r) { return l.lo <= r; }
47
48 friend bool operator<(rng const& l, rng const& r) { return !operator<=(r, l); }
49 friend bool operator<(double l, rng const& r) { return !operator<=(r, l); }
50 friend bool operator<(rng const& l, double r) { return !operator<=(r, l); }
51
52 friend bool operator==(double l, rng const& r) { return r.lo <= l && l <= r.hi; }
53
54 friend std::ostream & operator<<(std::ostream & os, rng const& v)
55 {
56 return (os << "[" << v.lo << ", " << v.hi << "]");
57 }
58
59 double lo, hi;
60 };
61
62
63 template <typename Units>
64 char const* units2string()
65 {
66 if (BOOST_GEOMETRY_CONDITION((std::is_same<Units, bg::degree>::value)))
67 {
68 return "degrees";
69 }
70 return "radians";
71 }
72
73 template <typename CoordinateSystem>
74 struct other_system_info
75 {};
76
77 template <>
78 struct other_system_info<bg::cs::spherical_equatorial<bg::radian> >
79 {
80 typedef bg::degree units;
81 typedef bg::cs::spherical_equatorial<units> type;
82
83 static inline double convert(double value)
84 {
85 return value * bg::math::r2d<double>();
86 }
87
88 static inline rng convert(rng const& value)
89 {
90 return value * bg::math::r2d<double>();
91 }
92 };
93
94 template <>
95 struct other_system_info<bg::cs::spherical_equatorial<bg::degree> >
96 {
97 typedef bg::radian units;
98 typedef bg::cs::spherical_equatorial<units> type;
99
100 static inline double convert(double value)
101 {
102 return value * bg::math::d2r<double>();
103 }
104
105 static inline rng convert(rng const& value)
106 {
107 return value * bg::math::d2r<double>();
108 }
109 };
110
111 template <>
112 struct other_system_info<bg::cs::spherical<bg::radian> >
113 {
114 typedef bg::degree units;
115 typedef bg::cs::spherical<units> type;
116
117 static inline double convert(double value)
118 {
119 return value * bg::math::r2d<double>();
120 }
121
122 static inline rng convert(rng const& value)
123 {
124 return value * bg::math::r2d<double>();
125 }
126 };
127
128 template <>
129 struct other_system_info<bg::cs::spherical<bg::degree> >
130 {
131 typedef bg::radian units;
132 typedef bg::cs::spherical<units> type;
133
134 static inline double convert(double value)
135 {
136 return value * bg::math::d2r<double>();
137 }
138
139 static inline rng convert(rng const& value)
140 {
141 return value * bg::math::d2r<double>();
142 }
143 };
144
145 template <>
146 struct other_system_info<bg::cs::geographic<bg::radian> >
147 {
148 typedef bg::degree units;
149 typedef bg::cs::geographic<units> type;
150
151 static inline double convert(double value)
152 {
153 return value * bg::math::r2d<double>();
154 }
155
156 static inline rng convert(rng const& value)
157 {
158 return value * bg::math::r2d<double>();
159 }
160 };
161
162 template <>
163 struct other_system_info<bg::cs::geographic<bg::degree> >
164 {
165 typedef bg::radian units;
166 typedef bg::cs::geographic<units> type;
167
168 static inline double convert(double value)
169 {
170 return value * bg::math::d2r<double>();
171 }
172
173 static inline rng convert(rng const& value)
174 {
175 return value * bg::math::d2r<double>();
176 }
177 };
178
179
180 class equals_with_tolerance
181 {
182 private:
183 double m_tolerence;
184
185 template <typename T>
186 static inline T const& get_max(T const& a, T const& b, T const& c)
187 {
188 return (std::max)((std::max)(a, b), c);
189 }
190
191 template <typename T>
192 static inline bool check_close(T const& a, T const& b, double tol)
193 {
194 return (a == b)
195 || (std::abs(a - b) <= tol * get_max(std::abs(a), std::abs(b), 1.0));
196 }
197
198 public:
199 equals_with_tolerance(double tolerence) : m_tolerence(tolerence) {}
200
201 inline bool operator()(double value1, double value2) const
202 {
203 return check_close(value1, value2, m_tolerence);
204 }
205
206 inline bool operator()(double l, rng const& r) const
207 {
208 return (r.lo < l && l < r.hi)
209 || check_close(l, r.lo, m_tolerence)
210 || check_close(l, r.hi, m_tolerence);
211 }
212 };
213
214
215 bool equals_with_eps(double l, double r)
216 {
217 return bg::math::equals(l, r);
218 }
219
220 bool equals_with_eps(double l, rng r)
221 {
222 return (r.lo < l && l < r.hi)
223 || bg::math::equals(l, r.lo)
224 || bg::math::equals(l, r.hi);
225 }
226
227
228 template
229 <
230 typename Box,
231 std::size_t DimensionCount = bg::dimension<Box>::value
232 >
233 struct box_check_equals
234 {
235 template <typename T1, typename T2, typename T3, typename T4>
236 static inline bool apply(Box const& box,
237 T1 const& lon_min, T2 const& lat_min, double,
238 T3 const& lon_max, T4 const& lat_max, double,
239 double tol)
240 {
241 equals_with_tolerance equals(tol);
242
243 #ifndef BOOST_GEOMETRY_TEST_FAILURES
244 // check latitude with tolerance when necessary
245 return equals_with_eps(bg::get<0, 0>(box), lon_min)
246 && (bg::get<0, 1>(box) < 0
247 ? equals(bg::get<0, 1>(box), lat_min)
248 : equals_with_eps(bg::get<0, 1>(box), lat_min))
249 && equals_with_eps(bg::get<1, 0>(box), lon_max)
250 && (bg::get<1, 1>(box) > 0
251 ? equals(bg::get<1, 1>(box), lat_max)
252 : equals_with_eps(bg::get<1, 1>(box), lat_max));
253 #else
254 // check latitude with tolerance when necessary
255 return bg::get<0, 0>(box) == lon_min
256 && (bg::get<0, 1>(box) < 0
257 ? equals(bg::get<0, 1>(box), lat_min)
258 : bg::get<0, 1>(box) == lat_min)
259 && bg::get<1, 0>(box) == lon_max
260 && (bg::get<1, 1>(box) > 0
261 ? equals(bg::get<1, 1>(box), lat_max)
262 : bg::get<1, 1>(box) == lat_max);
263 #endif
264 }
265 };
266
267 template <typename Box>
268 struct box_check_equals<Box, 3>
269 {
270 template <typename T1, typename T2, typename T3, typename T4>
271 static inline bool apply(Box const& box,
272 T1 const& lon_min, T2 const& lat_min, double height_min,
273 T3 const& lon_max, T4 const& lat_max, double height_max,
274 double tol)
275 {
276 #ifndef BOOST_GEOMETRY_TEST_FAILURES
277 equals_with_tolerance equals(tol);
278
279 return box_check_equals<Box, 2>::apply(box,
280 lon_min, lat_min, height_min,
281 lon_max, lat_max, height_max,
282 tol)
283 && equals(bg::get<0, 2>(box), height_min)
284 && equals(bg::get<1, 2>(box), height_max);
285 #else
286 return box_equals<Box, 2>::apply(box,
287 lon_min, lat_min, height_min,
288 lon_max, lat_max, height_max,
289 tol)
290 && bg::get<0, 2>(box) == height_min
291 && bg::get<1, 2>(box) == height_max;
292 #endif
293 }
294 };
295
296
297 template
298 <
299 typename Box1,
300 typename Box2 = Box1,
301 std::size_t DimensionCount = bg::dimension<Box1>::value
302 >
303 struct box_equals
304 {
305 static inline bool apply(Box1 const& box1, Box2 const& box2, double tol)
306 {
307 return box_check_equals<Box1>::apply(box1,
308 bg::get<0, 0>(box2), bg::get<0, 1>(box2), 0.0,
309 bg::get<1, 0>(box2), bg::get<1, 1>(box2), 0.0,
310 tol);
311 }
312 };
313
314 template<typename Box1, typename Box2>
315 struct box_equals<Box1, Box2, 3>
316 {
317 static inline bool apply(Box1 const& box1, Box2 const& box2, double tol)
318 {
319 return box_check_equals<Box1>::apply(box1,
320 bg::get<0, 0>(box2), bg::get<0, 1>(box2), bg::get<0, 2>(box2),
321 bg::get<1, 0>(box2), bg::get<1, 1>(box2), bg::get<1, 2>(box2),
322 tol);
323 }
324 };
325
326
327 template <typename Box, std::size_t Dimension = bg::dimension<Box>::value>
328 struct initialize_box
329 {
330 static inline void apply(Box& box,
331 double lon_min, double lat_min, double,
332 double lon_max, double lat_max, double)
333 {
334 bg::detail::indexed_point_view<Box, bg::min_corner> p_min(box);
335 bg::detail::indexed_point_view<Box, bg::max_corner> p_max(box);
336
337 bg::assign_values(p_min, lon_min, lat_min);
338 bg::assign_values(p_max, lon_max, lat_max);
339 }
340 };
341
342 template <typename Box>
343 struct initialize_box<Box, 3>
344 {
345 static inline void apply(Box& box,
346 double lon_min, double lat_min, double height_min,
347 double lon_max, double lat_max, double height_max)
348 {
349 bg::detail::indexed_point_view<Box, bg::min_corner> p_min(box);
350 bg::detail::indexed_point_view<Box, bg::max_corner> p_max(box);
351
352 bg::assign_values(p_min, lon_min, lat_min, height_min);
353 bg::assign_values(p_max, lon_max, lat_max, height_max);
354 }
355 };
356
357 #endif // BOOST_GEOMETRY_TEST_ENVELOPE_EXPAND_ON_SPHEROID_HPP