From e46237d8d5d39f77ebfe17fc53911923b5c5d745 Mon Sep 17 00:00:00 2001 From: "Daniel J. Hofmann" Date: Fri, 8 Jan 2016 13:00:24 +0100 Subject: [PATCH] Make PolylineCompresser's encode and decode free standing functions --- include/engine/polyline_compressor.hpp | 21 ++++------- src/engine/polyline_compressor.cpp | 48 +++++++++++++------------- src/engine/polyline_formatter.cpp | 4 ++- src/engine/route_parameters.cpp | 6 ++-- unit_tests/engine/geometry_string.cpp | 3 +- 5 files changed, 39 insertions(+), 43 deletions(-) diff --git a/include/engine/polyline_compressor.hpp b/include/engine/polyline_compressor.hpp index 9a7ba4d02d7..8f7b695bf92 100644 --- a/include/engine/polyline_compressor.hpp +++ b/include/engine/polyline_compressor.hpp @@ -2,6 +2,7 @@ #define POLYLINECOMPRESSOR_H_ #include "osrm/coordinate.hpp" +#include "engine/segment_information.hpp" #include #include @@ -10,21 +11,13 @@ namespace osrm { namespace engine { +// Encodes geometry into polyline format. +// See: https://developers.google.com/maps/documentation/utilities/polylinealgorithm +std::string polylineEncode(const std::vector &geometry); -struct SegmentInformation; - -class PolylineCompressor -{ - private: - std::string encode_vector(std::vector &numbers) const; - - std::string encode_number(const int number_to_encode) const; - - public: - std::string get_encoded_string(const std::vector &polyline) const; - - std::vector decode_string(const std::string &geometry_string) const; -}; +// Decodes geometry from polyline format +// See: https://developers.google.com/maps/documentation/utilities/polylinealgorithm +std::vector polylineDecode(const std::string &polyline); } } diff --git a/src/engine/polyline_compressor.cpp b/src/engine/polyline_compressor.cpp index 042653084f7..2af6735a3b9 100644 --- a/src/engine/polyline_compressor.cpp +++ b/src/engine/polyline_compressor.cpp @@ -1,14 +1,30 @@ #include "engine/polyline_compressor.hpp" -#include "engine/segment_information.hpp" -#include "osrm/coordinate.hpp" +#include namespace osrm { namespace engine { +namespace /*detail*/ // anonymous to keep TU local +{ + +std::string encode(int number_to_encode) +{ + std::string output; + while (number_to_encode >= 0x20) + { + const int next_value = (0x20 | (number_to_encode & 0x1f)) + 63; + output += static_cast(next_value); + number_to_encode >>= 5; + } + + number_to_encode += 63; + output += static_cast(number_to_encode); + return output; +} -std::string PolylineCompressor::encode_vector(std::vector &numbers) const +std::string encode(std::vector &numbers) { std::string output; const auto end = numbers.size(); @@ -22,28 +38,13 @@ std::string PolylineCompressor::encode_vector(std::vector &numbers) const } for (const int number : numbers) { - output += encode_number(number); + output += encode(number); } return output; } +} // anonymous ns -std::string PolylineCompressor::encode_number(int number_to_encode) const -{ - std::string output; - while (number_to_encode >= 0x20) - { - const int next_value = (0x20 | (number_to_encode & 0x1f)) + 63; - output += static_cast(next_value); - number_to_encode >>= 5; - } - - number_to_encode += 63; - output += static_cast(number_to_encode); - return output; -} - -std::string -PolylineCompressor::get_encoded_string(const std::vector &polyline) const +std::string polylineEncode(const std::vector &polyline) { if (polyline.empty()) { @@ -64,11 +65,10 @@ PolylineCompressor::get_encoded_string(const std::vector &po previous_coordinate = segment.location; } } - return encode_vector(delta_numbers); + return encode(delta_numbers); } -std::vector -PolylineCompressor::decode_string(const std::string &geometry_string) const +std::vector polylineDecode(const std::string &geometry_string) { std::vector new_coordinates; int index = 0, len = geometry_string.size(); diff --git a/src/engine/polyline_formatter.cpp b/src/engine/polyline_formatter.cpp index 0b22ddb2442..617ef7977ae 100644 --- a/src/engine/polyline_formatter.cpp +++ b/src/engine/polyline_formatter.cpp @@ -5,6 +5,8 @@ #include "osrm/coordinate.hpp" +#include + namespace osrm { namespace engine @@ -13,7 +15,7 @@ namespace engine util::json::String PolylineFormatter::printEncodedString(const std::vector &polyline) const { - return util::json::String(PolylineCompressor().get_encoded_string(polyline)); + return util::json::String(polylineEncode(polyline)); } util::json::Array diff --git a/src/engine/route_parameters.cpp b/src/engine/route_parameters.cpp index 6565896f352..bbcf40def70 100644 --- a/src/engine/route_parameters.cpp +++ b/src/engine/route_parameters.cpp @@ -7,6 +7,9 @@ #include "engine/polyline_compressor.hpp" +#include +#include + namespace osrm { namespace engine @@ -153,8 +156,7 @@ void RouteParameters::AddSource(const boost::fusion::vector &rec void RouteParameters::SetCoordinatesFromGeometry(const std::string &geometry_string) { - PolylineCompressor pc; - coordinates = pc.decode_string(geometry_string); + coordinates = polylineDecode(geometry_string); } } } diff --git a/unit_tests/engine/geometry_string.cpp b/unit_tests/engine/geometry_string.cpp index 182cabda887..235df9b6b1a 100644 --- a/unit_tests/engine/geometry_string.cpp +++ b/unit_tests/engine/geometry_string.cpp @@ -18,8 +18,7 @@ BOOST_AUTO_TEST_CASE(decode) { // Polyline string for the 5 coordinates const std::string polyline = "_gjaR_gjaR_pR_ibE_pR_ibE_pR_ibE_pR_ibE"; - PolylineCompressor pc; - std::vector coords = pc.decode_string(polyline); + const auto coords = polylineDecode(polyline); // Test coordinates; these would be the coordinates we give the loc parameter, // e.g. loc=10.00,10.0&loc=10.01,10.1...