1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 
5 // This file was modified by Oracle on 2014, 2016, 2017.
6 // Modifications copyright (c) 2014-2017, Oracle and/or its affiliates.
7 
8 // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
9 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
10 
11 // Use, modification and distribution is subject to the Boost Software License,
12 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
14 
15 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP
16 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP
17 
18 #include <boost/geometry/core/cs.hpp>
19 #include <boost/geometry/core/access.hpp>
20 #include <boost/geometry/core/radian_access.hpp>
21 #include <boost/geometry/core/tags.hpp>
22 
23 #include <boost/geometry/util/math.hpp>
24 
25 #include <boost/geometry/algorithms/not_implemented.hpp>
26 
27 #include <boost/geometry/formulas/spherical.hpp>
28 #include <boost/geometry/formulas/vincenty_inverse.hpp>
29 
30 namespace boost { namespace geometry
31 {
32 
33 // An azimuth is an angle between a vector/segment from origin to a point of
34 // interest and a reference vector. Typically north-based azimuth is used.
35 // North direction is used as a reference, angle is measured clockwise
36 // (North - 0deg, East - 90deg). For consistency in 2d cartesian CS
37 // the reference vector is Y axis, angle is measured clockwise.
38 // http://en.wikipedia.org/wiki/Azimuth
39 
40 #ifndef DOXYGEN_NO_DISPATCH
41 namespace detail_dispatch
42 {
43 
44 template <typename ReturnType, typename Tag>
45 struct azimuth
46     : not_implemented<Tag>
47 {};
48 
49 template <typename ReturnType>
50 struct azimuth<ReturnType, geographic_tag>
51 {
52     template <typename P1, typename P2, typename Spheroid>
applyboost::geometry::detail_dispatch::azimuth53     static inline ReturnType apply(P1 const& p1, P2 const& p2, Spheroid const& spheroid)
54     {
55         return geometry::formula::vincenty_inverse<ReturnType, false, true>().apply
56                     ( get_as_radian<0>(p1), get_as_radian<1>(p1),
57                       get_as_radian<0>(p2), get_as_radian<1>(p2),
58                       spheroid ).azimuth;
59     }
60 
61     template <typename P1, typename P2>
applyboost::geometry::detail_dispatch::azimuth62     static inline ReturnType apply(P1 const& p1, P2 const& p2)
63     {
64         return apply(p1, p2, srs::spheroid<ReturnType>());
65     }
66 };
67 
68 template <typename ReturnType>
69 struct azimuth<ReturnType, spherical_equatorial_tag>
70 {
71     template <typename P1, typename P2, typename Sphere>
applyboost::geometry::detail_dispatch::azimuth72     static inline ReturnType apply(P1 const& p1, P2 const& p2, Sphere const& /*unused*/)
73     {
74         return geometry::formula::spherical_azimuth<ReturnType, false>
75                     ( get_as_radian<0>(p1), get_as_radian<1>(p1),
76                       get_as_radian<0>(p2), get_as_radian<1>(p2)).azimuth;
77     }
78 
79     template <typename P1, typename P2>
applyboost::geometry::detail_dispatch::azimuth80     static inline ReturnType apply(P1 const& p1, P2 const& p2)
81     {
82         return apply(p1, p2, 0); // dummy model
83     }
84 };
85 
86 template <typename ReturnType>
87 struct azimuth<ReturnType, spherical_polar_tag>
88     : azimuth<ReturnType, spherical_equatorial_tag>
89 {};
90 
91 template <typename ReturnType>
92 struct azimuth<ReturnType, cartesian_tag>
93 {
94     template <typename P1, typename P2, typename Plane>
applyboost::geometry::detail_dispatch::azimuth95     static inline ReturnType apply(P1 const& p1, P2 const& p2, Plane const& /*unused*/)
96     {
97         ReturnType x = get<0>(p2) - get<0>(p1);
98         ReturnType y = get<1>(p2) - get<1>(p1);
99 
100         // NOTE: azimuth 0 is at Y axis, increasing right
101         // as in spherical/geographic where 0 is at North axis
102         return atan2(x, y);
103     }
104 
105     template <typename P1, typename P2>
applyboost::geometry::detail_dispatch::azimuth106     static inline ReturnType apply(P1 const& p1, P2 const& p2)
107     {
108         return apply(p1, p2, 0); // dummy model
109     }
110 };
111 
112 } // detail_dispatch
113 #endif // DOXYGEN_NO_DISPATCH
114 
115 #ifndef DOXYGEN_NO_DETAIL
116 namespace detail
117 {
118 
119 /// Calculate azimuth between two points.
120 /// The result is in radians.
121 template <typename ReturnType, typename Point1, typename Point2>
azimuth(Point1 const & p1,Point2 const & p2)122 inline ReturnType azimuth(Point1 const& p1, Point2 const& p2)
123 {
124     return detail_dispatch::azimuth
125             <
126                 ReturnType,
127                 typename geometry::cs_tag<Point1>::type
128             >::apply(p1, p2);
129 }
130 
131 /// Calculate azimuth between two points.
132 /// The result is in radians.
133 template <typename ReturnType, typename Point1, typename Point2, typename Model>
azimuth(Point1 const & p1,Point2 const & p2,Model const & model)134 inline ReturnType azimuth(Point1 const& p1, Point2 const& p2, Model const& model)
135 {
136     return detail_dispatch::azimuth
137             <
138                 ReturnType,
139                 typename geometry::cs_tag<Point1>::type
140             >::apply(p1, p2, model);
141 }
142 
143 } // namespace detail
144 #endif // DOXYGEN_NO_DETAIL
145 
146 }} // namespace boost::geometry
147 
148 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP
149