1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2017-2018 Oracle and/or its affiliates.
4 // Contributed and/or modified by Vissarion Fisikopoulos, on behalf of Oracle
5 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_ENVELOPE_SEGMENT_HPP
12 #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_ENVELOPE_SEGMENT_HPP
18 #include <boost/core/ignore_unused.hpp>
19 #include <boost/mpl/if.hpp>
20 #include <boost/numeric/conversion/cast.hpp>
21 #include <boost/type_traits/is_same.hpp>
23 #include <boost/geometry/algorithms/detail/envelope/transform_units.hpp>
25 #include <boost/geometry/core/assert.hpp>
26 #include <boost/geometry/core/coordinate_system.hpp>
27 #include <boost/geometry/core/coordinate_type.hpp>
28 #include <boost/geometry/core/cs.hpp>
29 #include <boost/geometry/core/point_type.hpp>
30 #include <boost/geometry/core/radian_access.hpp>
31 #include <boost/geometry/core/tags.hpp>
33 #include <boost/geometry/formulas/meridian_segment.hpp>
34 #include <boost/geometry/formulas/vertex_latitude.hpp>
36 #include <boost/geometry/geometries/helper_geometry.hpp>
38 #include <boost/geometry/strategies/cartesian/envelope_segment.hpp>
39 #include <boost/geometry/strategies/envelope.hpp>
40 #include <boost/geometry/strategies/normalize.hpp>
41 #include <boost/geometry/strategies/spherical/azimuth.hpp>
42 #include <boost/geometry/strategies/spherical/expand_box.hpp>
44 #include <boost/geometry/util/math.hpp>
46 namespace boost { namespace geometry { namespace strategy { namespace envelope
49 #ifndef DOXYGEN_NO_DETAIL
53 template <typename CalculationType, typename CS_Tag>
54 struct envelope_segment_call_vertex_latitude
56 template <typename T1, typename T2, typename Strategy>
57 static inline CalculationType apply(T1 const& lat1,
61 return geometry::formula::vertex_latitude<CalculationType, CS_Tag>
66 template <typename CalculationType>
67 struct envelope_segment_call_vertex_latitude<CalculationType, geographic_tag>
69 template <typename T1, typename T2, typename Strategy>
70 static inline CalculationType apply(T1 const& lat1,
72 Strategy const& strategy)
74 return geometry::formula::vertex_latitude<CalculationType, geographic_tag>
75 ::apply(lat1, alp1, strategy.model());
79 template <typename Units, typename CS_Tag>
80 struct envelope_segment_convert_polar
83 static inline void pre(T & , T & ) {}
86 static inline void post(T & , T & ) {}
89 template <typename Units>
90 struct envelope_segment_convert_polar<Units, spherical_polar_tag>
93 static inline void pre(T & lat1, T & lat2)
95 lat1 = math::latitude_convert_ep<Units>(lat1);
96 lat2 = math::latitude_convert_ep<Units>(lat2);
100 static inline void post(T & lat1, T & lat2)
102 lat1 = math::latitude_convert_ep<Units>(lat1);
103 lat2 = math::latitude_convert_ep<Units>(lat2);
104 std::swap(lat1, lat2);
108 template <typename CS_Tag>
109 class envelope_segment_impl
113 // degrees or radians
114 template <typename CalculationType>
115 static inline void swap(CalculationType& lon1,
116 CalculationType& lat1,
117 CalculationType& lon2,
118 CalculationType& lat2)
120 std::swap(lon1, lon2);
121 std::swap(lat1, lat2);
125 template <typename CalculationType>
126 static inline bool contains_pi_half(CalculationType const& a1,
127 CalculationType const& a2)
129 // azimuths a1 and a2 are assumed to be in radians
130 BOOST_GEOMETRY_ASSERT(! math::equals(a1, a2));
132 static CalculationType const pi_half = math::half_pi<CalculationType>();
135 ? (a1 < pi_half && pi_half < a2)
136 : (a1 > pi_half && pi_half > a2);
139 // radians or degrees
140 template <typename Units, typename CoordinateType>
141 static inline bool crosses_antimeridian(CoordinateType const& lon1,
142 CoordinateType const& lon2)
144 typedef math::detail::constants_on_spheroid
146 CoordinateType, Units
149 return math::abs(lon1 - lon2) > constants::half_period(); // > pi
152 // degrees or radians
153 template <typename Units, typename CalculationType, typename Strategy>
154 static inline void compute_box_corners(CalculationType& lon1,
155 CalculationType& lat1,
156 CalculationType& lon2,
157 CalculationType& lat2,
160 Strategy const& strategy)
162 // coordinates are assumed to be in radians
163 BOOST_GEOMETRY_ASSERT(lon1 <= lon2);
164 boost::ignore_unused(lon1, lon2);
166 CalculationType lat1_rad = math::as_radian<Units>(lat1);
167 CalculationType lat2_rad = math::as_radian<Units>(lat2);
169 if (math::equals(a1, a2))
171 // the segment must lie on the equator or is very short or is meridian
177 std::swap(lat1, lat2);
178 std::swap(lat1_rad, lat2_rad);
182 if (contains_pi_half(a1, a2))
184 CalculationType p_max = envelope_segment_call_vertex_latitude
185 <CalculationType, CS_Tag>::apply(lat1_rad, a1, strategy);
187 CalculationType const mid_lat = lat1 + lat2;
190 // update using min latitude
191 CalculationType const lat_min_rad = -p_max;
192 CalculationType const lat_min
193 = math::from_radian<Units>(lat_min_rad);
202 // update using max latitude
203 CalculationType const lat_max_rad = p_max;
204 CalculationType const lat_max
205 = math::from_radian<Units>(lat_max_rad);
215 template <typename Units, typename CalculationType>
216 static inline void special_cases(CalculationType& lon1,
217 CalculationType& lat1,
218 CalculationType& lon2,
219 CalculationType& lat2)
221 typedef math::detail::constants_on_spheroid
223 CalculationType, Units
226 bool is_pole1 = math::equals(math::abs(lat1), constants::max_latitude());
227 bool is_pole2 = math::equals(math::abs(lat2), constants::max_latitude());
229 if (is_pole1 && is_pole2)
231 // both points are poles; nothing more to do:
232 // longitudes are already normalized to 0
237 else if (is_pole1 && !is_pole2)
239 // first point is a pole, second point is not:
240 // make the longitude of the first point the same as that
241 // of the second point
244 else if (!is_pole1 && is_pole2)
246 // second point is a pole, first point is not:
247 // make the longitude of the second point the same as that
248 // of the first point
254 // segment lies on a meridian
257 std::swap(lat1, lat2);
262 BOOST_GEOMETRY_ASSERT(!is_pole1 && !is_pole2);
266 swap(lon1, lat1, lon2, lat2);
269 if (crosses_antimeridian<Units>(lon1, lon2))
271 lon1 += constants::period();
272 swap(lon1, lat1, lon2, lat2);
279 typename CalculationType,
282 static inline void create_box(CalculationType lon1,
283 CalculationType lat1,
284 CalculationType lon2,
285 CalculationType lat2,
288 typedef typename coordinate_type<Box>::type box_coordinate_type;
290 typedef typename helper_geometry
292 Box, box_coordinate_type, Units
293 >::type helper_box_type;
295 helper_box_type helper_mbr;
300 >(helper_mbr, boost::numeric_cast<box_coordinate_type>(lon1));
305 >(helper_mbr, boost::numeric_cast<box_coordinate_type>(lat1));
310 >(helper_mbr, boost::numeric_cast<box_coordinate_type>(lon2));
315 >(helper_mbr, boost::numeric_cast<box_coordinate_type>(lat2));
317 geometry::detail::envelope::transform_units(helper_mbr, mbr);
321 template <typename Units, typename CalculationType, typename Strategy>
322 static inline void apply(CalculationType& lon1,
323 CalculationType& lat1,
324 CalculationType& lon2,
325 CalculationType& lat2,
326 Strategy const& strategy)
328 special_cases<Units>(lon1, lat1, lon2, lat2);
330 CalculationType lon1_rad = math::as_radian<Units>(lon1);
331 CalculationType lat1_rad = math::as_radian<Units>(lat1);
332 CalculationType lon2_rad = math::as_radian<Units>(lon2);
333 CalculationType lat2_rad = math::as_radian<Units>(lat2);
334 CalculationType alp1, alp2;
335 strategy.apply(lon1_rad, lat1_rad, lon2_rad, lat2_rad, alp1, alp2);
337 compute_box_corners<Units>(lon1, lat1, lon2, lat2, alp1, alp2, strategy);
344 typename CalculationType,
348 static inline void apply(CalculationType lon1,
349 CalculationType lat1,
350 CalculationType lon2,
351 CalculationType lat2,
353 Strategy const& strategy)
355 typedef envelope_segment_convert_polar<Units, typename cs_tag<Box>::type> convert_polar;
357 convert_polar::pre(lat1, lat2);
359 apply<Units>(lon1, lat1, lon2, lat2, strategy);
361 convert_polar::post(lat1, lat2);
363 create_box<Units>(lon1, lat1, lon2, lat2, mbr);
368 } // namespace detail
369 #endif // DOXYGEN_NO_DETAIL
374 typename CalculationType = void
376 class spherical_segment
379 typedef strategy::expand::spherical_box box_expand_strategy_type;
380 static inline box_expand_strategy_type get_box_expand_strategy()
382 return box_expand_strategy_type();
385 template <typename Point, typename Box>
386 static inline void apply(Point const& point1, Point const& point2,
389 Point p1_normalized, p2_normalized;
390 strategy::normalize::spherical_point::apply(point1, p1_normalized);
391 strategy::normalize::spherical_point::apply(point2, p2_normalized);
393 geometry::strategy::azimuth::spherical<CalculationType> azimuth_spherical;
395 typedef typename geometry::detail::cs_angular_units<Point>::type units_type;
397 // first compute the envelope range for the first two coordinates
398 strategy::envelope::detail::envelope_segment_impl
400 spherical_equatorial_tag
401 >::template apply<units_type>(geometry::get<0>(p1_normalized),
402 geometry::get<1>(p1_normalized),
403 geometry::get<0>(p2_normalized),
404 geometry::get<1>(p2_normalized),
408 // now compute the envelope range for coordinates of
409 // dimension 2 and higher
410 strategy::envelope::detail::envelope_one_segment
412 2, dimension<Point>::value
413 >::apply(point1, point2, box);
417 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
422 template <typename CalculationType>
423 struct default_strategy<segment_tag, spherical_equatorial_tag, CalculationType>
425 typedef strategy::envelope::spherical_segment<CalculationType> type;
429 template <typename CalculationType>
430 struct default_strategy<segment_tag, spherical_polar_tag, CalculationType>
432 typedef strategy::envelope::spherical_segment<CalculationType> type;
437 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
440 }} // namespace strategy::envelope
442 }} //namepsace boost::geometry
444 #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_ENVELOPE_SEGMENT_HPP