diff --git a/Include/osrm/coordinate.hpp b/Include/osrm/coordinate.hpp index b6903930e..7a74498fc 100644 --- a/Include/osrm/coordinate.hpp +++ b/Include/osrm/coordinate.hpp @@ -45,25 +45,28 @@ struct FixedPointCoordinate FixedPointCoordinate(); FixedPointCoordinate(int lat, int lon); - template - FixedPointCoordinate(const T &coordinate) : lat(coordinate.lat), lon(coordinate.lon) + template + FixedPointCoordinate(const T &coordinate) + : lat(coordinate.lat), lon(coordinate.lon) { - static_assert(std::is_same::value, "coordinate types incompatible"); - static_assert(std::is_same::value, "coordinate types incompatible"); + static_assert(std::is_same::value, + "coordinate types incompatible"); + static_assert(std::is_same::value, + "coordinate types incompatible"); } - void Reset(); - bool isSet() const; + void reset(); + bool is_set() const; bool is_valid() const; bool operator==(const FixedPointCoordinate &other) const; - float GetBearing(const FixedPointCoordinate &other) const; - void Output(std::ostream &out) const; + float bearing(const FixedPointCoordinate &other) const; + void output(std::ostream &out) const; }; inline std::ostream &operator<<(std::ostream &out_stream, FixedPointCoordinate const &coordinate) { - coordinate.Output(out_stream); + coordinate.output(out_stream); return out_stream; } diff --git a/data_structures/coordinate.cpp b/data_structures/coordinate.cpp index de0b696f0..00cacfeef 100644 --- a/data_structures/coordinate.cpp +++ b/data_structures/coordinate.cpp @@ -63,12 +63,12 @@ FixedPointCoordinate::FixedPointCoordinate(int lat, int lon) : lat(lat), lon(lon #endif } -void FixedPointCoordinate::Reset() +void FixedPointCoordinate::reset() { lat = std::numeric_limits::min(); lon = std::numeric_limits::min(); } -bool FixedPointCoordinate::isSet() const +bool FixedPointCoordinate::is_set() const { return (std::numeric_limits::min() != lat) && (std::numeric_limits::min() != lon); } @@ -86,30 +86,12 @@ bool FixedPointCoordinate::operator==(const FixedPointCoordinate &other) const return lat == other.lat && lon == other.lon; } -void FixedPointCoordinate::Output(std::ostream &out) const +void FixedPointCoordinate::output(std::ostream &out) const { out << "(" << lat / COORDINATE_PRECISION << "," << lon / COORDINATE_PRECISION << ")"; } -float FixedPointCoordinate::GetBearing(const FixedPointCoordinate &other) const +float FixedPointCoordinate::bearing(const FixedPointCoordinate &other) const { - const float lon_delta = coordinate_calculation::deg_to_rad(lon / COORDINATE_PRECISION - - other.lon / COORDINATE_PRECISION); - const float lat1 = coordinate_calculation::deg_to_rad(other.lat / COORDINATE_PRECISION); - const float lat2 = coordinate_calculation::deg_to_rad(lat / COORDINATE_PRECISION); - const float y_value = std::sin(lon_delta) * std::cos(lat2); - const float x_value = - std::cos(lat1) * std::sin(lat2) - std::sin(lat1) * std::cos(lat2) * std::cos(lon_delta); - float result = coordinate_calculation::rad_to_deg(std::atan2(y_value, x_value)); - - while (result < 0.f) - { - result += 360.f; - } - - while (result >= 360.f) - { - result -= 360.f; - } - return result; + return coordinate_calculation::bearing(*this, other); } diff --git a/data_structures/coordinate_calculation.cpp b/data_structures/coordinate_calculation.cpp index 27a054ded..1b8dd618e 100644 --- a/data_structures/coordinate_calculation.cpp +++ b/data_structures/coordinate_calculation.cpp @@ -239,3 +239,26 @@ float coordinate_calculation::rad_to_deg(const float radian) { return radian * (180.f * static_cast(M_1_PI)); } + +float coordinate_calculation::bearing(const FixedPointCoordinate &first_coordinate, + const FixedPointCoordinate &second_coordinate) +{ + const float lon_diff = + second_coordinate.lon / COORDINATE_PRECISION - first_coordinate.lon / COORDINATE_PRECISION; + const float lon_delta = deg_to_rad(lon_diff); + const float lat1 = deg_to_rad(first_coordinate.lat / COORDINATE_PRECISION); + const float lat2 = deg_to_rad(second_coordinate.lat / COORDINATE_PRECISION); + const float y = sin(lon_delta) * cos(lat2); + const float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(lon_delta); + float result = rad_to_deg(std::atan2(y, x)); + while (result < 0.f) + { + result += 360.f; + } + + while (result >= 360.f) + { + result -= 360.f; + } + return result; +} diff --git a/data_structures/coordinate_calculation.hpp b/data_structures/coordinate_calculation.hpp index adf076a66..359fdb585 100644 --- a/data_structures/coordinate_calculation.hpp +++ b/data_structures/coordinate_calculation.hpp @@ -75,6 +75,9 @@ struct coordinate_calculation static float deg_to_rad(const float degree); static float rad_to_deg(const float radian); + + static float bearing(const FixedPointCoordinate &first_coordinate, + const FixedPointCoordinate &second_coordinate); }; #endif // COORDINATE_CALCULATION diff --git a/descriptors/description_factory.cpp b/descriptors/description_factory.cpp index 0977d611d..18bd44328 100644 --- a/descriptors/description_factory.cpp +++ b/descriptors/description_factory.cpp @@ -237,7 +237,7 @@ void DescriptionFactory::Run(const unsigned zoom_level) via_indices.push_back(necessary_pieces); } const double angle = - path_description[i + 1].location.GetBearing(path_description[i].location); + path_description[i + 1].location.bearing(path_description[i].location); path_description[i].bearing = static_cast(angle * 10); } }