Make PolylineCompresser's encode and decode free standing functions
This commit is contained in:
		
							parent
							
								
									c590596dbe
								
							
						
					
					
						commit
						c65dd16460
					
				| @ -2,6 +2,7 @@ | |||||||
| #define POLYLINECOMPRESSOR_H_ | #define POLYLINECOMPRESSOR_H_ | ||||||
| 
 | 
 | ||||||
| #include "osrm/coordinate.hpp" | #include "osrm/coordinate.hpp" | ||||||
|  | #include "engine/segment_information.hpp" | ||||||
| 
 | 
 | ||||||
| #include <string> | #include <string> | ||||||
| #include <vector> | #include <vector> | ||||||
| @ -10,21 +11,13 @@ namespace osrm | |||||||
| { | { | ||||||
| namespace engine | namespace engine | ||||||
| { | { | ||||||
|  | // Encodes geometry into polyline format.
 | ||||||
|  | // See: https://developers.google.com/maps/documentation/utilities/polylinealgorithm
 | ||||||
|  | std::string polylineEncode(const std::vector<SegmentInformation> &geometry); | ||||||
| 
 | 
 | ||||||
| struct SegmentInformation; | // Decodes geometry from polyline format
 | ||||||
| 
 | // See: https://developers.google.com/maps/documentation/utilities/polylinealgorithm
 | ||||||
| class PolylineCompressor | std::vector<util::FixedPointCoordinate> polylineDecode(const std::string &polyline); | ||||||
| { |  | ||||||
|   private: |  | ||||||
|     std::string encode_vector(std::vector<int> &numbers) const; |  | ||||||
| 
 |  | ||||||
|     std::string encode_number(const int number_to_encode) const; |  | ||||||
| 
 |  | ||||||
|   public: |  | ||||||
|     std::string get_encoded_string(const std::vector<SegmentInformation> &polyline) const; |  | ||||||
| 
 |  | ||||||
|     std::vector<util::FixedPointCoordinate> decode_string(const std::string &geometry_string) const; |  | ||||||
| }; |  | ||||||
| } | } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
| @ -1,33 +1,15 @@ | |||||||
| #include "engine/polyline_compressor.hpp" | #include "engine/polyline_compressor.hpp" | ||||||
| #include "engine/segment_information.hpp" |  | ||||||
| 
 | 
 | ||||||
| #include "osrm/coordinate.hpp" | #include <cstddef> | ||||||
| 
 | 
 | ||||||
| namespace osrm | namespace osrm | ||||||
| { | { | ||||||
| namespace engine | namespace engine | ||||||
| { | { | ||||||
| 
 | namespace /*detail*/ // anonymous to keep TU local
 | ||||||
| std::string PolylineCompressor::encode_vector(std::vector<int> &numbers) const |  | ||||||
| { | { | ||||||
|     std::string output; |  | ||||||
|     const auto end = numbers.size(); |  | ||||||
|     for (std::size_t i = 0; i < end; ++i) |  | ||||||
|     { |  | ||||||
|         numbers[i] <<= 1; |  | ||||||
|         if (numbers[i] < 0) |  | ||||||
|         { |  | ||||||
|             numbers[i] = ~(numbers[i]); |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|     for (const int number : numbers) |  | ||||||
|     { |  | ||||||
|         output += encode_number(number); |  | ||||||
|     } |  | ||||||
|     return output; |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| std::string PolylineCompressor::encode_number(int number_to_encode) const | std::string encode(int number_to_encode) | ||||||
| { | { | ||||||
|     std::string output; |     std::string output; | ||||||
|     while (number_to_encode >= 0x20) |     while (number_to_encode >= 0x20) | ||||||
| @ -42,8 +24,27 @@ std::string PolylineCompressor::encode_number(int number_to_encode) const | |||||||
|     return output; |     return output; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| std::string | std::string encode(std::vector<int> &numbers) | ||||||
| PolylineCompressor::get_encoded_string(const std::vector<SegmentInformation> &polyline) const | { | ||||||
|  |     std::string output; | ||||||
|  |     const auto end = numbers.size(); | ||||||
|  |     for (std::size_t i = 0; i < end; ++i) | ||||||
|  |     { | ||||||
|  |         numbers[i] <<= 1; | ||||||
|  |         if (numbers[i] < 0) | ||||||
|  |         { | ||||||
|  |             numbers[i] = ~(numbers[i]); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |     for (const int number : numbers) | ||||||
|  |     { | ||||||
|  |         output += encode(number); | ||||||
|  |     } | ||||||
|  |     return output; | ||||||
|  | } | ||||||
|  | } // anonymous ns
 | ||||||
|  | 
 | ||||||
|  | std::string polylineEncode(const std::vector<SegmentInformation> &polyline) | ||||||
| { | { | ||||||
|     if (polyline.empty()) |     if (polyline.empty()) | ||||||
|     { |     { | ||||||
| @ -64,11 +65,10 @@ PolylineCompressor::get_encoded_string(const std::vector<SegmentInformation> &po | |||||||
|             previous_coordinate = segment.location; |             previous_coordinate = segment.location; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|     return encode_vector(delta_numbers); |     return encode(delta_numbers); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| std::vector<util::FixedPointCoordinate> | std::vector<util::FixedPointCoordinate> polylineDecode(const std::string &geometry_string) | ||||||
| PolylineCompressor::decode_string(const std::string &geometry_string) const |  | ||||||
| { | { | ||||||
|     std::vector<util::FixedPointCoordinate> new_coordinates; |     std::vector<util::FixedPointCoordinate> new_coordinates; | ||||||
|     int index = 0, len = geometry_string.size(); |     int index = 0, len = geometry_string.size(); | ||||||
|  | |||||||
| @ -5,6 +5,8 @@ | |||||||
| 
 | 
 | ||||||
| #include "osrm/coordinate.hpp" | #include "osrm/coordinate.hpp" | ||||||
| 
 | 
 | ||||||
|  | #include <vector> | ||||||
|  | 
 | ||||||
| namespace osrm | namespace osrm | ||||||
| { | { | ||||||
| namespace engine | namespace engine | ||||||
| @ -13,7 +15,7 @@ namespace engine | |||||||
| util::json::String | util::json::String | ||||||
| PolylineFormatter::printEncodedString(const std::vector<SegmentInformation> &polyline) const | PolylineFormatter::printEncodedString(const std::vector<SegmentInformation> &polyline) const | ||||||
| { | { | ||||||
|     return util::json::String(PolylineCompressor().get_encoded_string(polyline)); |     return util::json::String(polylineEncode(polyline)); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| util::json::Array | util::json::Array | ||||||
|  | |||||||
| @ -7,6 +7,9 @@ | |||||||
| 
 | 
 | ||||||
| #include "engine/polyline_compressor.hpp" | #include "engine/polyline_compressor.hpp" | ||||||
| 
 | 
 | ||||||
|  | #include <string> | ||||||
|  | #include <utility> | ||||||
|  | 
 | ||||||
| namespace osrm | namespace osrm | ||||||
| { | { | ||||||
| namespace engine | namespace engine | ||||||
| @ -153,8 +156,7 @@ void RouteParameters::AddSource(const boost::fusion::vector<double, double> &rec | |||||||
| 
 | 
 | ||||||
| void RouteParameters::SetCoordinatesFromGeometry(const std::string &geometry_string) | void RouteParameters::SetCoordinatesFromGeometry(const std::string &geometry_string) | ||||||
| { | { | ||||||
|     PolylineCompressor pc; |     coordinates = polylineDecode(geometry_string); | ||||||
|     coordinates = pc.decode_string(geometry_string); |  | ||||||
| } | } | ||||||
| } | } | ||||||
| } | } | ||||||
|  | |||||||
| @ -18,8 +18,7 @@ BOOST_AUTO_TEST_CASE(decode) | |||||||
| { | { | ||||||
|     // Polyline string for the 5 coordinates
 |     // Polyline string for the 5 coordinates
 | ||||||
|     const std::string polyline = "_gjaR_gjaR_pR_ibE_pR_ibE_pR_ibE_pR_ibE"; |     const std::string polyline = "_gjaR_gjaR_pR_ibE_pR_ibE_pR_ibE_pR_ibE"; | ||||||
|     PolylineCompressor pc; |     const auto coords = polylineDecode(polyline); | ||||||
|     std::vector<util::FixedPointCoordinate> coords = pc.decode_string(polyline); |  | ||||||
| 
 | 
 | ||||||
|     // Test coordinates; these would be the coordinates we give the loc parameter,
 |     // Test coordinates; these would be the coordinates we give the loc parameter,
 | ||||||
|     // e.g. loc=10.00,10.0&loc=10.01,10.1...
 |     // e.g. loc=10.00,10.0&loc=10.01,10.1...
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user