2016-01-02 11:13:44 -05:00
|
|
|
#include "engine/polyline_compressor.hpp"
|
2014-11-28 04:07:06 -05:00
|
|
|
|
2016-01-28 10:28:44 -05:00
|
|
|
#include <boost/assert.hpp>
|
2016-01-08 07:00:24 -05:00
|
|
|
#include <cstddef>
|
2016-01-26 05:10:40 -05:00
|
|
|
#include <cstdlib>
|
2016-01-22 13:36:10 -05:00
|
|
|
#include <cmath>
|
2016-01-28 10:28:44 -05:00
|
|
|
#include <algorithm>
|
2014-11-28 04:07:06 -05:00
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace engine
|
|
|
|
{
|
2016-01-08 07:00:24 -05:00
|
|
|
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<char>(next_value);
|
|
|
|
number_to_encode >>= 5;
|
|
|
|
}
|
|
|
|
|
|
|
|
number_to_encode += 63;
|
|
|
|
output += static_cast<char>(number_to_encode);
|
|
|
|
return output;
|
|
|
|
}
|
2016-01-05 10:51:13 -05:00
|
|
|
|
2016-01-08 07:00:24 -05:00
|
|
|
std::string encode(std::vector<int> &numbers)
|
2014-11-28 04:07:06 -05:00
|
|
|
{
|
|
|
|
std::string output;
|
2016-01-22 13:36:10 -05:00
|
|
|
for (auto &number : numbers)
|
2014-11-28 04:07:06 -05:00
|
|
|
{
|
2016-01-22 13:36:10 -05:00
|
|
|
bool isNegative = number < 0;
|
|
|
|
|
|
|
|
if (isNegative)
|
|
|
|
{
|
|
|
|
const unsigned binary = std::llabs(number);
|
|
|
|
const unsigned twos = (~binary) + 1u;
|
|
|
|
number = twos;
|
|
|
|
}
|
|
|
|
|
|
|
|
number <<= 1u;
|
|
|
|
|
|
|
|
if (isNegative)
|
2014-11-28 04:07:06 -05:00
|
|
|
{
|
2016-01-22 13:36:10 -05:00
|
|
|
number = ~number;
|
2014-11-28 04:07:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (const int number : numbers)
|
|
|
|
{
|
2016-01-08 07:00:24 -05:00
|
|
|
output += encode(number);
|
2014-11-28 04:07:06 -05:00
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
2016-01-08 07:00:24 -05:00
|
|
|
} // anonymous ns
|
2014-11-28 04:07:06 -05:00
|
|
|
|
2016-01-28 10:28:44 -05:00
|
|
|
std::string encodePolyline(CoordVectorForwardIter begin, CoordVectorForwardIter end)
|
2014-11-28 04:07:06 -05:00
|
|
|
{
|
2016-01-28 10:28:44 -05:00
|
|
|
auto size = std::distance(begin, end);
|
|
|
|
if (size == 0)
|
2014-11-28 04:07:06 -05:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<int> delta_numbers;
|
2016-01-28 10:28:44 -05:00
|
|
|
BOOST_ASSERT(size > 0);
|
|
|
|
delta_numbers.reserve((size - 1) * 2);
|
2016-03-03 18:36:36 -05:00
|
|
|
int current_lat = 0;
|
|
|
|
int current_lon = 0;
|
|
|
|
std::for_each(begin, end, [&delta_numbers, ¤t_lat, ¤t_lon](const util::Coordinate loc)
|
2016-02-23 15:23:13 -05:00
|
|
|
{
|
2016-03-03 18:36:36 -05:00
|
|
|
const int lat_diff = std::round(static_cast<int>(loc.lat) * detail::COORDINATE_TO_POLYLINE) - current_lat;
|
|
|
|
const int lon_diff = std::round(static_cast<int>(loc.lon) * detail::COORDINATE_TO_POLYLINE) - current_lon;
|
2016-02-23 15:23:13 -05:00
|
|
|
delta_numbers.emplace_back(lat_diff);
|
|
|
|
delta_numbers.emplace_back(lon_diff);
|
2016-03-03 18:36:36 -05:00
|
|
|
current_lat += lat_diff;
|
|
|
|
current_lon += lon_diff;
|
2016-02-23 15:23:13 -05:00
|
|
|
});
|
2016-01-08 07:00:24 -05:00
|
|
|
return encode(delta_numbers);
|
2014-11-28 04:07:06 -05:00
|
|
|
}
|
2016-02-24 04:29:23 -05:00
|
|
|
|
2016-02-23 15:23:13 -05:00
|
|
|
std::vector<util::Coordinate> decodePolyline(const std::string &geometry_string)
|
2015-05-31 07:57:27 -04:00
|
|
|
{
|
2016-02-23 15:23:13 -05:00
|
|
|
std::vector<util::Coordinate> new_coordinates;
|
2015-05-31 07:57:27 -04:00
|
|
|
int index = 0, len = geometry_string.size();
|
|
|
|
int lat = 0, lng = 0;
|
2016-01-05 06:04:04 -05:00
|
|
|
|
|
|
|
while (index < len)
|
2015-05-31 07:57:27 -04:00
|
|
|
{
|
|
|
|
int b, shift = 0, result = 0;
|
2016-01-05 06:04:04 -05:00
|
|
|
do
|
2015-05-31 07:57:27 -04:00
|
|
|
{
|
|
|
|
b = geometry_string.at(index++) - 63;
|
|
|
|
result |= (b & 0x1f) << shift;
|
|
|
|
shift += 5;
|
|
|
|
} while (b >= 0x20);
|
|
|
|
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
|
|
|
|
lat += dlat;
|
|
|
|
|
|
|
|
shift = 0;
|
|
|
|
result = 0;
|
2016-01-05 06:04:04 -05:00
|
|
|
do
|
2015-05-31 07:57:27 -04:00
|
|
|
{
|
|
|
|
b = geometry_string.at(index++) - 63;
|
|
|
|
result |= (b & 0x1f) << shift;
|
|
|
|
shift += 5;
|
|
|
|
} while (b >= 0x20);
|
|
|
|
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
|
|
|
|
lng += dlng;
|
|
|
|
|
2016-02-23 15:23:13 -05:00
|
|
|
util::Coordinate p;
|
|
|
|
p.lat = util::FixedLatitude(lat * detail::POLYLINE_TO_COORDINATE);
|
|
|
|
p.lon = util::FixedLongitude(lng * detail::POLYLINE_TO_COORDINATE);
|
2015-05-31 07:57:27 -04:00
|
|
|
new_coordinates.push_back(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_coordinates;
|
|
|
|
}
|
2016-01-05 10:51:13 -05:00
|
|
|
}
|
|
|
|
}
|