Vanetza
Loading...
Searching...
No Matches
haversine.cpp
1#include <vanetza/geodesy/haversine.hpp>
2#include <boost/units/cmath.hpp>
3#include <cmath>
4#include <limits>
5
6namespace vanetza
7{
8namespace geodesy
9{
10namespace haversine
11{
12
13// arithmetic mean radius of WGS84 ellipsoid
14static const units::Length earth_radius = 6371008.8 * units::si::meter;
15
16units::Length distance(const GeodeticPosition& a, const GeodeticPosition& b)
17{
18 using boost::units::sin;
19 using boost::units::cos;
20
21 const units::Angle delta_phi { b.latitude - a.latitude };
22 const units::Angle delta_lambda { b.longitude - a.longitude };
23
24 const auto sin_dphi = sin(delta_phi / 2.0);
25 const auto sin_dlambda = sin(delta_lambda / 2.0);
26 const auto h = sin_dphi * sin_dphi + cos(a.latitude) * cos(b.latitude) * sin_dlambda * sin_dlambda;
27 const auto c = 2.0 * std::atan2(std::sqrt(h), std::sqrt(1.0 - h));
28
29 return earth_radius * c;
30}
31
32CartesianPosition local_cartesian(
33 const GeodeticPosition& origin,
34 const GeodeticPosition& position)
35{
36 using boost::units::cos;
37
38 const double dlat = units::Angle {position.latitude - origin.latitude }.value();
39 const double dlon = units::Angle {position.longitude - origin.longitude }.value();
40
41 const units::Length x = dlon * cos(origin.latitude) * earth_radius;
42 const units::Length y = dlat * earth_radius;
43
44 return CartesianPosition(x, y);
45}
46
47} // namespace haversine
48} // namespace geodesy
49} // namespace vanetza