1 // Boost.Geometry (aka GGL, Generic Geometry Library)
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
8 // Licensed under the Boost Software License version 1.0.
9 // http://www.boost.org/users/license.html
11 #ifndef BOOST_GEOMETRY_TEST_ENVELOPE_EXPAND_ON_SPHEROID_HPP
12 #define BOOST_GEOMETRY_TEST_ENVELOPE_EXPAND_ON_SPHEROID_HPP
20 #include <boost/geometry/core/access.hpp>
21 #include <boost/geometry/core/coordinate_dimension.hpp>
22 #include <boost/geometry/core/cs.hpp>
24 #include <boost/geometry/util/condition.hpp>
25 #include <boost/geometry/util/math.hpp>
27 #include <boost/geometry/views/detail/indexed_point_view.hpp>
29 #include <boost/geometry/algorithms/assign.hpp>
36 rng(double l, double h)
39 BOOST_GEOMETRY_ASSERT(lo <= hi);
42 friend rng operator*(rng const& l, double v) { return rng(l.lo * v, l.hi * v); }
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; }
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); }
52 friend bool operator==(double l, rng const& r) { return r.lo <= l && l <= r.hi; }
54 friend std::ostream & operator<<(std::ostream & os, rng const& v)
56 return (os << "[" << v.lo << ", " << v.hi << "]");
63 template <typename Units>
64 char const* units2string()
66 if (BOOST_GEOMETRY_CONDITION((std::is_same<Units, bg::degree>::value)))
73 template <typename CoordinateSystem>
74 struct other_system_info
78 struct other_system_info<bg::cs::spherical_equatorial<bg::radian> >
80 typedef bg::degree units;
81 typedef bg::cs::spherical_equatorial<units> type;
83 static inline double convert(double value)
85 return value * bg::math::r2d<double>();
88 static inline rng convert(rng const& value)
90 return value * bg::math::r2d<double>();
95 struct other_system_info<bg::cs::spherical_equatorial<bg::degree> >
97 typedef bg::radian units;
98 typedef bg::cs::spherical_equatorial<units> type;
100 static inline double convert(double value)
102 return value * bg::math::d2r<double>();
105 static inline rng convert(rng const& value)
107 return value * bg::math::d2r<double>();
112 struct other_system_info<bg::cs::spherical<bg::radian> >
114 typedef bg::degree units;
115 typedef bg::cs::spherical<units> type;
117 static inline double convert(double value)
119 return value * bg::math::r2d<double>();
122 static inline rng convert(rng const& value)
124 return value * bg::math::r2d<double>();
129 struct other_system_info<bg::cs::spherical<bg::degree> >
131 typedef bg::radian units;
132 typedef bg::cs::spherical<units> type;
134 static inline double convert(double value)
136 return value * bg::math::d2r<double>();
139 static inline rng convert(rng const& value)
141 return value * bg::math::d2r<double>();
146 struct other_system_info<bg::cs::geographic<bg::radian> >
148 typedef bg::degree units;
149 typedef bg::cs::geographic<units> type;
151 static inline double convert(double value)
153 return value * bg::math::r2d<double>();
156 static inline rng convert(rng const& value)
158 return value * bg::math::r2d<double>();
163 struct other_system_info<bg::cs::geographic<bg::degree> >
165 typedef bg::radian units;
166 typedef bg::cs::geographic<units> type;
168 static inline double convert(double value)
170 return value * bg::math::d2r<double>();
173 static inline rng convert(rng const& value)
175 return value * bg::math::d2r<double>();
180 class equals_with_tolerance
185 template <typename T>
186 static inline T const& get_max(T const& a, T const& b, T const& c)
188 return (std::max)((std::max)(a, b), c);
191 template <typename T>
192 static inline bool check_close(T const& a, T const& b, double tol)
195 || (std::abs(a - b) <= tol * get_max(std::abs(a), std::abs(b), 1.0));
199 equals_with_tolerance(double tolerence) : m_tolerence(tolerence) {}
201 inline bool operator()(double value1, double value2) const
203 return check_close(value1, value2, m_tolerence);
206 inline bool operator()(double l, rng const& r) const
208 return (r.lo < l && l < r.hi)
209 || check_close(l, r.lo, m_tolerence)
210 || check_close(l, r.hi, m_tolerence);
215 bool equals_with_eps(double l, double r)
217 return bg::math::equals(l, r);
220 bool equals_with_eps(double l, rng r)
222 return (r.lo < l && l < r.hi)
223 || bg::math::equals(l, r.lo)
224 || bg::math::equals(l, r.hi);
231 std::size_t DimensionCount = bg::dimension<Box>::value
233 struct box_check_equals
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,
241 equals_with_tolerance equals(tol);
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));
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);
267 template <typename Box>
268 struct box_check_equals<Box, 3>
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,
276 #ifndef BOOST_GEOMETRY_TEST_FAILURES
277 equals_with_tolerance equals(tol);
279 return box_check_equals<Box, 2>::apply(box,
280 lon_min, lat_min, height_min,
281 lon_max, lat_max, height_max,
283 && equals(bg::get<0, 2>(box), height_min)
284 && equals(bg::get<1, 2>(box), height_max);
286 return box_equals<Box, 2>::apply(box,
287 lon_min, lat_min, height_min,
288 lon_max, lat_max, height_max,
290 && bg::get<0, 2>(box) == height_min
291 && bg::get<1, 2>(box) == height_max;
300 typename Box2 = Box1,
301 std::size_t DimensionCount = bg::dimension<Box1>::value
305 static inline bool apply(Box1 const& box1, Box2 const& box2, double tol)
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,
314 template<typename Box1, typename Box2>
315 struct box_equals<Box1, Box2, 3>
317 static inline bool apply(Box1 const& box1, Box2 const& box2, double tol)
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),
327 template <typename Box, std::size_t Dimension = bg::dimension<Box>::value>
328 struct initialize_box
330 static inline void apply(Box& box,
331 double lon_min, double lat_min, double,
332 double lon_max, double lat_max, double)
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);
337 bg::assign_values(p_min, lon_min, lat_min);
338 bg::assign_values(p_max, lon_max, lat_max);
342 template <typename Box>
343 struct initialize_box<Box, 3>
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)
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);
352 bg::assign_values(p_min, lon_min, lat_min, height_min);
353 bg::assign_values(p_max, lon_max, lat_max, height_max);
357 #endif // BOOST_GEOMETRY_TEST_ENVELOPE_EXPAND_ON_SPHEROID_HPP