Vanetza
Loading...
Searching...
No Matches
distance.cpp
1#include <vanetza/common/position_fix.hpp>
2#include <vanetza/asn1/security_profile.hpp>
3#include VANETZA_ASN1_SECURITY_HEADER(Latitude.h)
4#include VANETZA_ASN1_SECURITY_HEADER(Longitude.h)
5#include VANETZA_ASN1_SECURITY_HEADER(TwoDLocation.h)
6#include <vanetza/security/v3/distance.hpp>
7#include <boost/units/cmath.hpp>
8
9namespace vanetza
10{
11namespace security
12{
13namespace v3
14{
15
16units::Length distance(const PositionFix& one, const asn1::TwoDLocation& other)
17{
18 static const auto earth_radius = 6371000.0 * units::si::meter;
19 const auto other_lat = convert_latitude(other.latitude);
20 const auto other_lon = convert_longitude(other.longitude);
21 const units::Angle delta_phi { one.latitude - other_lat };
22 const units::Angle delta_lambda { one.longitude - other_lon };
23 const auto sin_delta_phi = sin(delta_phi / 2.0);
24 const auto sin_delta_lambda = sin(delta_lambda / 2.0);
25 const auto a = sin_delta_phi * sin_delta_phi +
26 cos(one.latitude) * cos(other_lat) * sin_delta_lambda * sin_delta_lambda;
27 const auto c = 2.0 * atan2(sqrt(a), sqrt(1 - a));
28 return earth_radius * c;
29}
30
31units::GeoAngle convert_latitude(const asn1::Latitude& in)
32{
33 static constexpr long latitude_scale = Vanetza_Security_NinetyDegreeInt_max;
34 if (in >= Vanetza_Security_NinetyDegreeInt_min && in <= Vanetza_Security_NinetyDegreeInt_max) {
35 return in * 90.0 / latitude_scale * units::degree;
36 } else {
37 return units::GeoAngle::from_value(std::numeric_limits<double>::quiet_NaN());
38 }
39}
40
41units::GeoAngle convert_longitude(const asn1::Longitude& in)
42{
43 static constexpr long longitude_scale = Vanetza_Security_OneEightyDegreeInt_max;
44 if (in >= Vanetza_Security_OneEightyDegreeInt_min && in <= Vanetza_Security_OneEightyDegreeInt_max) {
45 return in * 180.0 / longitude_scale * units::degree;
46 } else {
47 return units::GeoAngle::from_value(std::numeric_limits<double>::quiet_NaN());
48 }
49}
50
51} // namespace v3
52} // namespace security
53} // namespace vanetza