1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2008-2015 Bruno Lalande, Paris, France.
4 // Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2009-2015 Mateusz Loskot, London, UK.
7 // This file was modified by Oracle on 2014-2017.
8 // Modifications copyright (c) 2014-2017, Oracle and/or its affiliates.
10 // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
11 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
12 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
14 // Use, modification and distribution is subject to the Boost Software License,
15 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
16 // http://www.boost.org/LICENSE_1_0.txt)
18 #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_POINT_BOX_HPP
19 #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_POINT_BOX_HPP
21 #include <boost/config.hpp>
22 #include <boost/concept_check.hpp>
23 #include <boost/mpl/if.hpp>
24 #include <boost/type_traits/is_void.hpp>
26 #include <boost/geometry/core/access.hpp>
27 #include <boost/geometry/core/assert.hpp>
28 #include <boost/geometry/core/point_type.hpp>
29 #include <boost/geometry/core/radian_access.hpp>
30 #include <boost/geometry/core/tags.hpp>
32 #include <boost/geometry/strategies/distance.hpp>
33 #include <boost/geometry/strategies/concepts/distance_concept.hpp>
34 #include <boost/geometry/strategies/spherical/distance_cross_track.hpp>
36 #include <boost/geometry/util/math.hpp>
37 #include <boost/geometry/algorithms/detail/assign_box_corners.hpp>
40 namespace boost { namespace geometry
43 namespace strategy { namespace distance
49 template <typename ReturnType>
50 class cross_track_point_box_generic
60 ReturnType static inline apply (Point const& point,
64 // this method assumes that the coordinates of the point and
65 // the box are normalized
67 typedef typename point_type<Box>::type box_point_type;
69 box_point_type bottom_left, bottom_right, top_left, top_right;
70 geometry::detail::assign_box_corners(box,
71 bottom_left, bottom_right,
74 ReturnType const plon = geometry::get_as_radian<0>(point);
75 ReturnType const plat = geometry::get_as_radian<1>(point);
77 ReturnType const lon_min = geometry::get_as_radian<0>(bottom_left);
78 ReturnType const lat_min = geometry::get_as_radian<1>(bottom_left);
79 ReturnType const lon_max = geometry::get_as_radian<0>(top_right);
80 ReturnType const lat_max = geometry::get_as_radian<1>(top_right);
82 ReturnType const pi = math::pi<ReturnType>();
83 ReturnType const two_pi = math::two_pi<ReturnType>();
85 typedef typename point_type<Box>::type box_point_type;
87 // First check if the point is within the band defined by the
88 // minimum and maximum longitude of the box; if yes, determine
89 // if the point is above, below or inside the box and compute
90 // the distance (easy in this case)
92 // Notice that the point may not be inside the longitude range
93 // of the box, but the shifted point may be inside the
94 // longitude range of the box; in this case the point is still
95 // considered as inside the longitude range band of the box
96 if ((plon >= lon_min && plon <= lon_max) || plon + two_pi <= lon_max)
100 return geometry::strategy::distance::services::result_from_distance
102 Strategy, Point, box_point_type
103 >::apply(ps_strategy, ps_strategy
104 .vertical_or_meridian(plat, lat_max));
106 else if (plat < lat_min)
108 return geometry::strategy::distance::services::result_from_distance
110 Strategy, Point, box_point_type
111 >::apply(ps_strategy, ps_strategy
112 .vertical_or_meridian(lat_min, plat));
116 BOOST_GEOMETRY_ASSERT(plat >= lat_min && plat <= lat_max);
117 return ReturnType(0);
121 // Otherwise determine which among the two medirian segments of the
122 // box the point is closest to, and compute the distance of
123 // the point to this closest segment
125 // Below lon_midway is the longitude of the meridian that:
126 // (1) is midway between the meridians of the left and right
127 // meridians of the box, and
128 // (2) does not intersect the box
129 ReturnType const two = 2.0;
130 bool use_left_segment;
133 // the box crosses the antimeridian
135 // midway longitude = lon_min - (lon_min + (lon_max - 2 * pi)) / 2;
136 ReturnType const lon_midway = (lon_min - lon_max) / two + pi;
137 BOOST_GEOMETRY_ASSERT(lon_midway >= -pi && lon_midway <= pi);
139 use_left_segment = plon > lon_midway;
143 // the box does not cross the antimeridian
145 ReturnType const lon_sum = lon_min + lon_max;
146 if (math::equals(lon_sum, ReturnType(0)))
148 // special case: the box is symmetric with respect to
149 // the prime meridian; the midway meridian is the antimeridian
151 use_left_segment = plon < lon_min;
155 // midway long. = lon_min - (2 * pi - (lon_max - lon_min)) / 2;
156 ReturnType lon_midway = (lon_min + lon_max) / two - pi;
158 // normalize the midway longitude
161 lon_midway -= two_pi;
163 else if (lon_midway < -pi)
165 lon_midway += two_pi;
167 BOOST_GEOMETRY_ASSERT(lon_midway >= -pi && lon_midway <= pi);
169 // if lon_sum is positive the midway meridian is left
170 // of the box, or right of the box otherwise
171 use_left_segment = lon_sum > 0
172 ? (plon < lon_min && plon >= lon_midway)
173 : (plon <= lon_max || plon > lon_midway);
177 return use_left_segment
178 ? ps_strategy.apply(point, bottom_left, top_left)
179 : ps_strategy.apply(point, bottom_right, top_right);
183 } //namespace details
186 \brief Strategy functor for distance point to box calculation
188 \details Class which calculates the distance of a point to a box, for
189 points and boxes on a sphere or globe
190 \tparam CalculationType \tparam_calculation
191 \tparam Strategy underlying point-point distance strategy
194 [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
199 typename CalculationType = void,
200 typename Strategy = haversine<double, CalculationType>
202 class cross_track_point_box
205 template <typename Point, typename Box>
207 : services::return_type<Strategy, Point, typename point_type<Box>::type>
210 typedef typename Strategy::radius_type radius_type;
214 // point-segment strategy getters
215 struct distance_ps_strategy
217 typedef cross_track<CalculationType, Strategy> type;
220 typedef typename strategy::distance::services::comparable_type
223 >::type pp_comparable_strategy;
225 typedef typename boost::mpl::if_
229 pp_comparable_strategy,
232 typename strategy::distance::services::comparable_type
234 typename distance_ps_strategy::type
236 typename distance_ps_strategy::type
237 >::type ps_strategy_type;
241 inline cross_track_point_box()
244 explicit inline cross_track_point_box(typename Strategy::radius_type const& r)
248 inline cross_track_point_box(Strategy const& s)
255 // It might be useful in the future
256 // to overload constructor with strategy info.
257 // crosstrack(...) {}
259 template <typename Point, typename Box>
260 inline typename return_type<Point, Box>::type
261 apply(Point const& point, Box const& box) const
263 #if !defined(BOOST_MSVC)
266 (concepts::PointDistanceStrategy
268 Strategy, Point, typename point_type<Box>::type
272 typedef typename return_type<Point, Box>::type return_type;
273 return details::cross_track_point_box_generic
274 <return_type>::apply(point, box,
275 ps_strategy_type(m_strategy));
278 inline typename Strategy::radius_type radius() const
280 return m_strategy.radius();
289 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
293 template <typename CalculationType, typename Strategy>
294 struct tag<cross_track_point_box<CalculationType, Strategy> >
296 typedef strategy_tag_distance_point_box type;
300 template <typename CalculationType, typename Strategy, typename P, typename Box>
301 struct return_type<cross_track_point_box<CalculationType, Strategy>, P, Box>
302 : cross_track_point_box
304 CalculationType, Strategy
305 >::template return_type<P, Box>
309 template <typename CalculationType, typename Strategy>
310 struct comparable_type<cross_track_point_box<CalculationType, Strategy> >
312 typedef cross_track_point_box
314 CalculationType, typename comparable_type<Strategy>::type
319 template <typename CalculationType, typename Strategy>
320 struct get_comparable<cross_track_point_box<CalculationType, Strategy> >
322 typedef cross_track_point_box<CalculationType, Strategy> this_strategy;
323 typedef typename comparable_type<this_strategy>::type comparable_type;
326 static inline comparable_type apply(this_strategy const& strategy)
328 return comparable_type(strategy.radius());
333 template <typename CalculationType, typename Strategy, typename P, typename Box>
334 struct result_from_distance
336 cross_track_point_box<CalculationType, Strategy>, P, Box
340 typedef cross_track_point_box<CalculationType, Strategy> this_strategy;
342 typedef typename this_strategy::template return_type
348 template <typename T>
349 static inline return_type apply(this_strategy const& strategy,
352 Strategy s(strategy.radius());
354 return result_from_distance
356 Strategy, P, typename point_type<Box>::type
357 >::apply(s, distance);
362 // define cross_track_point_box<default_point_segment_strategy> as
363 // default point-box strategy for the spherical equatorial coordinate system
364 template <typename Point, typename Box, typename Strategy>
365 struct default_strategy
367 point_tag, box_tag, Point, Box,
368 spherical_equatorial_tag, spherical_equatorial_tag,
372 typedef cross_track_point_box
375 typename boost::mpl::if_
377 boost::is_void<Strategy>,
378 typename default_strategy
380 point_tag, point_tag,
381 Point, typename point_type<Box>::type,
382 spherical_equatorial_tag, spherical_equatorial_tag
390 template <typename Box, typename Point, typename Strategy>
391 struct default_strategy
393 box_tag, point_tag, Box, Point,
394 spherical_equatorial_tag, spherical_equatorial_tag,
398 typedef typename default_strategy
400 point_tag, box_tag, Point, Box,
401 spherical_equatorial_tag, spherical_equatorial_tag,
407 } // namespace services
408 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
411 }} // namespace strategy::distance
414 }} // namespace boost::geometry
417 #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_POINT_BOX_HPP