]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/geometry/include/boost/geometry/algorithms/detail/azimuth.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / geometry / include / boost / geometry / algorithms / detail / azimuth.hpp
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.
6 // Modifications copyright (c) 2014, Oracle and/or its affiliates.
7
8 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
9
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)
13
14 #ifndef BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP
15 #define BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP
16
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>
21
22 #include <boost/geometry/util/math.hpp>
23
24 #include <boost/geometry/algorithms/not_implemented.hpp>
25 #include <boost/geometry/algorithms/detail/vincenty_inverse.hpp>
26
27 namespace boost { namespace geometry
28 {
29
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
36
37 #ifndef DOXYGEN_NO_DISPATCH
38 namespace detail_dispatch
39 {
40
41 template <typename ReturnType, typename Tag>
42 struct azimuth
43 : not_implemented<Tag>
44 {};
45
46 template <typename ReturnType>
47 struct azimuth<ReturnType, geographic_tag>
48 {
49 template <typename P1, typename P2, typename Spheroid>
50 static inline ReturnType apply(P1 const& p1, P2 const& p2, Spheroid const& spheroid)
51 {
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),
55 spheroid ).azimuth;
56 }
57
58 template <typename P1, typename P2>
59 static inline ReturnType apply(P1 const& p1, P2 const& p2)
60 {
61 return apply(p1, p2, srs::spheroid<ReturnType>());
62 }
63 };
64
65 template <typename ReturnType>
66 struct azimuth<ReturnType, spherical_equatorial_tag>
67 {
68 template <typename P1, typename P2, typename Sphere>
69 static inline ReturnType apply(P1 const& p1, P2 const& p2, Sphere const& /*unused*/)
70 {
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));
74
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) )
78 //{
79 // return - sin(get_as_radian<1>(p1)) * cos_p2lat);
80 //}
81
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));
87 }
88
89 template <typename P1, typename P2>
90 static inline ReturnType apply(P1 const& p1, P2 const& p2)
91 {
92 return apply(p1, p2, 0); // dummy model
93 }
94 };
95
96 template <typename ReturnType>
97 struct azimuth<ReturnType, spherical_polar_tag>
98 : azimuth<ReturnType, spherical_equatorial_tag>
99 {};
100
101 template <typename ReturnType>
102 struct azimuth<ReturnType, cartesian_tag>
103 {
104 template <typename P1, typename P2, typename Plane>
105 static inline ReturnType apply(P1 const& p1, P2 const& p2, Plane const& /*unused*/)
106 {
107 ReturnType x = get<0>(p2) - get<0>(p1);
108 ReturnType y = get<1>(p2) - get<1>(p1);
109
110 // NOTE: azimuth 0 is at Y axis, increasing right
111 // as in spherical/geographic where 0 is at North axis
112 return atan2(x, y);
113 }
114
115 template <typename P1, typename P2>
116 static inline ReturnType apply(P1 const& p1, P2 const& p2)
117 {
118 return apply(p1, p2, 0); // dummy model
119 }
120 };
121
122 } // detail_dispatch
123 #endif // DOXYGEN_NO_DISPATCH
124
125 #ifndef DOXYGEN_NO_DETAIL
126 namespace detail
127 {
128
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)
133 {
134 return detail_dispatch::azimuth
135 <
136 ReturnType,
137 typename geometry::cs_tag<Point1>::type
138 >::apply(p1, p2);
139 }
140
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)
145 {
146 return detail_dispatch::azimuth
147 <
148 ReturnType,
149 typename geometry::cs_tag<Point1>::type
150 >::apply(p1, p2, model);
151 }
152
153 } // namespace detail
154 #endif // DOXYGEN_NO_DETAIL
155
156 }} // namespace boost::geometry
157
158 #endif // BOOST_GEOMETRY_ALGORITHMS_DETAIL_AZIMUTH_HPP