1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
5 // This file was modified by Oracle on 2014.
6 // Modifications copyright (c) 2014, Oracle and/or its affiliates.
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 // Use, modification and distribution is subject to the Boost Software License,
11 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt)
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP
17 #include <boost/geometry/core/cs.hpp>
18 #include <boost/geometry/core/access.hpp>
19 #include <boost/geometry/core/radian_access.hpp>
20 #include <boost/geometry/core/tags.hpp>
22 #include <boost/geometry/util/math.hpp>
24 #include <boost/geometry/algorithms/not_implemented.hpp>
25 #include <boost/geometry/algorithms/detail/vincenty_inverse.hpp>
27 namespace boost { namespace geometry
30 // An azimuth is an angle between a vector/segment from origin to a point of
31 // interest and a reference vector. Typically north-based azimuth is used.
32 // North direction is used as a reference, angle is measured clockwise
33 // (North - 0deg, East - 90deg). For consistency in 2d cartesian CS
34 // the reference vector is Y axis, angle is measured clockwise.
35 // http://en.wikipedia.org/wiki/Azimuth
37 #ifndef DOXYGEN_NO_DISPATCH
38 namespace detail_dispatch
41 template <typename ReturnType, typename Tag>
43 : not_implemented<Tag>
46 template <typename ReturnType>
47 struct azimuth<ReturnType, geographic_tag>
49 template <typename P1, typename P2, typename Spheroid>
50 static inline ReturnType apply(P1 const& p1, P2 const& p2, Spheroid const& spheroid)
52 return geometry::detail::vincenty_inverse<ReturnType, false, true>().apply
53 ( get_as_radian<0>(p1), get_as_radian<1>(p1),
54 get_as_radian<0>(p2), get_as_radian<1>(p2),
58 template <typename P1, typename P2>
59 static inline ReturnType apply(P1 const& p1, P2 const& p2)
61 return apply(p1, p2, srs::spheroid<ReturnType>());
65 template <typename ReturnType>
66 struct azimuth<ReturnType, spherical_equatorial_tag>
68 template <typename P1, typename P2, typename Sphere>
69 static inline ReturnType apply(P1 const& p1, P2 const& p2, Sphere const& /*unused*/)
71 // http://williams.best.vwh.net/avform.htm#Crs
72 ReturnType dlon = get_as_radian<0>(p2) - get_as_radian<0>(p1);
73 ReturnType cos_p2lat = cos(get_as_radian<1>(p2));
75 // An optimization which should kick in often for Boxes
76 //if ( math::equals(dlon, ReturnType(0)) )
77 //if ( get<0>(p1) == get<0>(p2) )
79 // return - sin(get_as_radian<1>(p1)) * cos_p2lat);
82 // "An alternative formula, not requiring the pre-computation of d"
83 // In the formula below dlon is used as "d"
84 return atan2(sin(dlon) * cos_p2lat,
85 cos(get_as_radian<1>(p1)) * sin(get_as_radian<1>(p2))
86 - sin(get_as_radian<1>(p1)) * cos_p2lat * cos(dlon));
89 template <typename P1, typename P2>
90 static inline ReturnType apply(P1 const& p1, P2 const& p2)
92 return apply(p1, p2, 0); // dummy model
96 template <typename ReturnType>
97 struct azimuth<ReturnType, spherical_polar_tag>
98 : azimuth<ReturnType, spherical_equatorial_tag>
101 template <typename ReturnType>
102 struct azimuth<ReturnType, cartesian_tag>
104 template <typename P1, typename P2, typename Plane>
105 static inline ReturnType apply(P1 const& p1, P2 const& p2, Plane const& /*unused*/)
107 ReturnType x = get<0>(p2) - get<0>(p1);
108 ReturnType y = get<1>(p2) - get<1>(p1);
110 // NOTE: azimuth 0 is at Y axis, increasing right
111 // as in spherical/geographic where 0 is at North axis
115 template <typename P1, typename P2>
116 static inline ReturnType apply(P1 const& p1, P2 const& p2)
118 return apply(p1, p2, 0); // dummy model
123 #endif // DOXYGEN_NO_DISPATCH
125 #ifndef DOXYGEN_NO_DETAIL
129 /// Calculate azimuth between two points.
130 /// The result is in radians.
131 template <typename ReturnType, typename Point1, typename Point2>
132 inline ReturnType azimuth(Point1 const& p1, Point2 const& p2)
134 return detail_dispatch::azimuth
137 typename geometry::cs_tag<Point1>::type
141 /// Calculate azimuth between two points.
142 /// The result is in radians.
143 template <typename ReturnType, typename Point1, typename Point2, typename Model>
144 inline ReturnType azimuth(Point1 const& p1, Point2 const& p2, Model const& model)
146 return detail_dispatch::azimuth
149 typename geometry::cs_tag<Point1>::type
150 >::apply(p1, p2, model);
153 } // namespace detail
154 #endif // DOXYGEN_NO_DETAIL
156 }} // namespace boost::geometry
158 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP