First round of lat,lng -> lng,lat switcheroo
This commit is contained in:
+25
-22
@@ -16,49 +16,52 @@ namespace osrm
|
||||
namespace util
|
||||
{
|
||||
|
||||
FixedPointCoordinate::FixedPointCoordinate()
|
||||
: lat(std::numeric_limits<int>::min()), lon(std::numeric_limits<int>::min())
|
||||
Coordinate::Coordinate()
|
||||
: lon(std::numeric_limits<int>::min()), lat(std::numeric_limits<int>::min())
|
||||
{
|
||||
}
|
||||
|
||||
FixedPointCoordinate::FixedPointCoordinate(int lat, int lon) : lat(lat), lon(lon)
|
||||
Coordinate::Coordinate(const FloatLongitude lon_, const FloatLatitude lat_)
|
||||
: Coordinate(toFixed(lon_), toFixed(lat_))
|
||||
{
|
||||
}
|
||||
|
||||
Coordinate::Coordinate(const FixedLongitude lon_, const FixedLatitude lat_) : lon(lon_), lat(lat_)
|
||||
{
|
||||
#ifndef NDEBUG
|
||||
if (0 != (std::abs(lat) >> 30))
|
||||
if (0 != (std::abs(static_cast<int>(lon)) >> 30))
|
||||
{
|
||||
std::bitset<32> y_coordinate_vector(lat);
|
||||
SimpleLogger().Write(logDEBUG) << "broken lat: " << lat
|
||||
<< ", bits: " << y_coordinate_vector;
|
||||
}
|
||||
if (0 != (std::abs(lon) >> 30))
|
||||
{
|
||||
std::bitset<32> x_coordinate_vector(lon);
|
||||
std::bitset<32> x_coordinate_vector(static_cast<int>(lon));
|
||||
SimpleLogger().Write(logDEBUG) << "broken lon: " << lon
|
||||
<< ", bits: " << x_coordinate_vector;
|
||||
}
|
||||
if (0 != (std::abs(static_cast<int>(lat)) >> 30))
|
||||
{
|
||||
std::bitset<32> y_coordinate_vector(static_cast<int>(lat));
|
||||
SimpleLogger().Write(logDEBUG) << "broken lat: " << lat
|
||||
<< ", bits: " << y_coordinate_vector;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
bool FixedPointCoordinate::IsValid() const
|
||||
bool Coordinate::IsValid() const
|
||||
{
|
||||
return !(lat > 90 * COORDINATE_PRECISION || lat < -90 * COORDINATE_PRECISION ||
|
||||
lon > 180 * COORDINATE_PRECISION || lon < -180 * COORDINATE_PRECISION);
|
||||
return !(lat > FixedLatitude(90 * COORDINATE_PRECISION) ||
|
||||
lat < FixedLatitude(-90 * COORDINATE_PRECISION) ||
|
||||
lon > FixedLongitude(180 * COORDINATE_PRECISION) ||
|
||||
lon < FixedLongitude(-180 * COORDINATE_PRECISION));
|
||||
}
|
||||
|
||||
bool operator==(const FixedPointCoordinate lhs, const FixedPointCoordinate rhs)
|
||||
bool operator==(const Coordinate lhs, const Coordinate rhs)
|
||||
{
|
||||
return lhs.lat == rhs.lat && lhs.lon == rhs.lon;
|
||||
}
|
||||
|
||||
bool operator!=(const FixedPointCoordinate lhs, const FixedPointCoordinate rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
bool operator!=(const Coordinate lhs, const Coordinate rhs) { return !(lhs == rhs); }
|
||||
|
||||
std::ostream &operator<<(std::ostream &out, const FixedPointCoordinate coordinate)
|
||||
std::ostream &operator<<(std::ostream &out, const Coordinate coordinate)
|
||||
{
|
||||
out << "(" << static_cast<double>(coordinate.lat / COORDINATE_PRECISION) << ","
|
||||
<< static_cast<double>(coordinate.lon / COORDINATE_PRECISION) << ")";
|
||||
out << "(lon:" << toFloating(coordinate.lon) << ", lat:" << toFloating(coordinate.lat) << ")";
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,12 +17,16 @@ namespace util
|
||||
namespace coordinate_calculation
|
||||
{
|
||||
|
||||
double haversineDistance(const int lat1, const int lon1, const int lat2, const int lon2)
|
||||
double haversineDistance(const Coordinate coordinate_1, const Coordinate coordinate_2)
|
||||
{
|
||||
BOOST_ASSERT(lat1 != std::numeric_limits<int>::min());
|
||||
auto lon1 = static_cast<int>(coordinate_1.lon);
|
||||
auto lat1 = static_cast<int>(coordinate_1.lat);
|
||||
auto lon2 = static_cast<int>(coordinate_2.lon);
|
||||
auto lat2 = static_cast<int>(coordinate_2.lat);
|
||||
BOOST_ASSERT(lon1 != std::numeric_limits<int>::min());
|
||||
BOOST_ASSERT(lat2 != std::numeric_limits<int>::min());
|
||||
BOOST_ASSERT(lat1 != std::numeric_limits<int>::min());
|
||||
BOOST_ASSERT(lon2 != std::numeric_limits<int>::min());
|
||||
BOOST_ASSERT(lat2 != std::numeric_limits<int>::min());
|
||||
const double lt1 = lat1 / COORDINATE_PRECISION;
|
||||
const double ln1 = lon1 / COORDINATE_PRECISION;
|
||||
const double lt2 = lat2 / COORDINATE_PRECISION;
|
||||
@@ -42,22 +46,12 @@ double haversineDistance(const int lat1, const int lon1, const int lat2, const i
|
||||
return EARTH_RADIUS * charv;
|
||||
}
|
||||
|
||||
double haversineDistance(const FixedPointCoordinate coordinate_1,
|
||||
const FixedPointCoordinate coordinate_2)
|
||||
{
|
||||
return haversineDistance(coordinate_1.lat, coordinate_1.lon, coordinate_2.lat,
|
||||
coordinate_2.lon);
|
||||
}
|
||||
|
||||
double greatCircleDistance(const FixedPointCoordinate coordinate_1,
|
||||
const FixedPointCoordinate coordinate_2)
|
||||
{
|
||||
return greatCircleDistance(coordinate_1.lat, coordinate_1.lon, coordinate_2.lat,
|
||||
coordinate_2.lon);
|
||||
}
|
||||
|
||||
double greatCircleDistance(const int lat1, const int lon1, const int lat2, const int lon2)
|
||||
double greatCircleDistance(const Coordinate coordinate_1, const Coordinate coordinate_2)
|
||||
{
|
||||
auto lon1 = static_cast<int>(coordinate_1.lon);
|
||||
auto lat1 = static_cast<int>(coordinate_1.lat);
|
||||
auto lon2 = static_cast<int>(coordinate_2.lon);
|
||||
auto lat2 = static_cast<int>(coordinate_2.lat);
|
||||
BOOST_ASSERT(lat1 != std::numeric_limits<int>::min());
|
||||
BOOST_ASSERT(lon1 != std::numeric_limits<int>::min());
|
||||
BOOST_ASSERT(lat2 != std::numeric_limits<int>::min());
|
||||
@@ -73,65 +67,65 @@ double greatCircleDistance(const int lat1, const int lon1, const int lat2, const
|
||||
return std::hypot(x_value, y_value) * EARTH_RADIUS;
|
||||
}
|
||||
|
||||
double perpendicularDistance(const FixedPointCoordinate source_coordinate,
|
||||
const FixedPointCoordinate target_coordinate,
|
||||
const FixedPointCoordinate query_location)
|
||||
double perpendicularDistance(const Coordinate source_coordinate,
|
||||
const Coordinate target_coordinate,
|
||||
const Coordinate query_location)
|
||||
{
|
||||
double ratio;
|
||||
FixedPointCoordinate nearest_location;
|
||||
Coordinate nearest_location;
|
||||
|
||||
return perpendicularDistance(source_coordinate, target_coordinate, query_location,
|
||||
nearest_location, ratio);
|
||||
}
|
||||
|
||||
double perpendicularDistance(const FixedPointCoordinate segment_source,
|
||||
const FixedPointCoordinate segment_target,
|
||||
const FixedPointCoordinate query_location,
|
||||
FixedPointCoordinate &nearest_location,
|
||||
double perpendicularDistance(const Coordinate segment_source,
|
||||
const Coordinate segment_target,
|
||||
const Coordinate query_location,
|
||||
Coordinate &nearest_location,
|
||||
double &ratio)
|
||||
{
|
||||
using namespace coordinate_calculation;
|
||||
|
||||
return perpendicularDistanceFromProjectedCoordinate(
|
||||
segment_source, segment_target, query_location,
|
||||
{mercator::latToY(query_location.lat / COORDINATE_PRECISION),
|
||||
query_location.lon / COORDINATE_PRECISION},
|
||||
{static_cast<double>(toFloating(query_location.lon)),
|
||||
mercator::latToY(toFloating(query_location.lat))},
|
||||
nearest_location, ratio);
|
||||
}
|
||||
|
||||
double
|
||||
perpendicularDistanceFromProjectedCoordinate(const FixedPointCoordinate source_coordinate,
|
||||
const FixedPointCoordinate target_coordinate,
|
||||
const FixedPointCoordinate query_location,
|
||||
const std::pair<double, double> projected_coordinate)
|
||||
double perpendicularDistanceFromProjectedCoordinate(
|
||||
const Coordinate source_coordinate,
|
||||
const Coordinate target_coordinate,
|
||||
const Coordinate query_location,
|
||||
const std::pair<double, double> projected_xy_coordinate)
|
||||
{
|
||||
double ratio;
|
||||
FixedPointCoordinate nearest_location;
|
||||
Coordinate nearest_location;
|
||||
|
||||
return perpendicularDistanceFromProjectedCoordinate(source_coordinate, target_coordinate,
|
||||
query_location, projected_coordinate,
|
||||
query_location, projected_xy_coordinate,
|
||||
nearest_location, ratio);
|
||||
}
|
||||
|
||||
double
|
||||
perpendicularDistanceFromProjectedCoordinate(const FixedPointCoordinate segment_source,
|
||||
const FixedPointCoordinate segment_target,
|
||||
const FixedPointCoordinate query_location,
|
||||
const std::pair<double, double> projected_coordinate,
|
||||
FixedPointCoordinate &nearest_location,
|
||||
double &ratio)
|
||||
double perpendicularDistanceFromProjectedCoordinate(
|
||||
const Coordinate segment_source,
|
||||
const Coordinate segment_target,
|
||||
const Coordinate query_location,
|
||||
const std::pair<double, double> projected_xy_coordinate,
|
||||
Coordinate &nearest_location,
|
||||
double &ratio)
|
||||
{
|
||||
using namespace coordinate_calculation;
|
||||
|
||||
BOOST_ASSERT(query_location.IsValid());
|
||||
|
||||
// initialize values
|
||||
const double x = projected_coordinate.first;
|
||||
const double y = projected_coordinate.second;
|
||||
const double a = mercator::latToY(segment_source.lat / COORDINATE_PRECISION);
|
||||
const double b = segment_source.lon / COORDINATE_PRECISION;
|
||||
const double c = mercator::latToY(segment_target.lat / COORDINATE_PRECISION);
|
||||
const double d = segment_target.lon / COORDINATE_PRECISION;
|
||||
const double x = projected_xy_coordinate.first;
|
||||
const double y = projected_xy_coordinate.second;
|
||||
const double a = mercator::latToY(toFloating(segment_source.lat));
|
||||
const double b = static_cast<double>(toFloating(segment_source.lon));
|
||||
const double c = mercator::latToY(toFloating(segment_target.lat));
|
||||
const double d = static_cast<double>(toFloating(segment_target.lon));
|
||||
double p, q /*,mX*/, new_y;
|
||||
if (std::abs(a - c) > std::numeric_limits<double>::epsilon())
|
||||
{
|
||||
@@ -184,8 +178,8 @@ perpendicularDistanceFromProjectedCoordinate(const FixedPointCoordinate segment_
|
||||
else
|
||||
{
|
||||
// point lies in between
|
||||
nearest_location.lat = static_cast<int>(mercator::yToLat(p) * COORDINATE_PRECISION);
|
||||
nearest_location.lon = static_cast<int>(q * COORDINATE_PRECISION);
|
||||
nearest_location.lon = toFixed(FloatLongitude(q));
|
||||
nearest_location.lat = toFixed(FloatLatitude(mercator::yToLat(p)));
|
||||
}
|
||||
BOOST_ASSERT(nearest_location.IsValid());
|
||||
|
||||
@@ -206,14 +200,13 @@ double radToDeg(const double radian)
|
||||
return radian * (180.0 * (1. / pi<double>()));
|
||||
}
|
||||
|
||||
double bearing(const FixedPointCoordinate first_coordinate,
|
||||
const FixedPointCoordinate second_coordinate)
|
||||
double bearing(const Coordinate first_coordinate, const Coordinate second_coordinate)
|
||||
{
|
||||
const double lon_diff =
|
||||
second_coordinate.lon / COORDINATE_PRECISION - first_coordinate.lon / COORDINATE_PRECISION;
|
||||
static_cast<double>(toFloating(second_coordinate.lon - first_coordinate.lon));
|
||||
const double lon_delta = degToRad(lon_diff);
|
||||
const double lat1 = degToRad(first_coordinate.lat / COORDINATE_PRECISION);
|
||||
const double lat2 = degToRad(second_coordinate.lat / COORDINATE_PRECISION);
|
||||
const double lat1 = degToRad(static_cast<double>(toFloating(first_coordinate.lat)));
|
||||
const double lat2 = degToRad(static_cast<double>(toFloating(second_coordinate.lat)));
|
||||
const double y = std::sin(lon_delta) * std::cos(lat2);
|
||||
const double x =
|
||||
std::cos(lat1) * std::sin(lat2) - std::sin(lat1) * std::cos(lat2) * std::cos(lon_delta);
|
||||
@@ -230,19 +223,17 @@ double bearing(const FixedPointCoordinate first_coordinate,
|
||||
return result;
|
||||
}
|
||||
|
||||
double computeAngle(const FixedPointCoordinate first,
|
||||
const FixedPointCoordinate second,
|
||||
const FixedPointCoordinate third)
|
||||
double computeAngle(const Coordinate first, const Coordinate second, const Coordinate third)
|
||||
{
|
||||
using namespace boost::math::constants;
|
||||
using namespace coordinate_calculation;
|
||||
|
||||
const double v1x = (first.lon - second.lon) / COORDINATE_PRECISION;
|
||||
const double v1y = mercator::latToY(first.lat / COORDINATE_PRECISION) -
|
||||
mercator::latToY(second.lat / COORDINATE_PRECISION);
|
||||
const double v2x = (third.lon - second.lon) / COORDINATE_PRECISION;
|
||||
const double v2y = mercator::latToY(third.lat / COORDINATE_PRECISION) -
|
||||
mercator::latToY(second.lat / COORDINATE_PRECISION);
|
||||
const double v1x = static_cast<double>(toFloating(first.lon - second.lon));
|
||||
const double v1y =
|
||||
mercator::latToY(toFloating(first.lat)) - mercator::latToY(toFloating(second.lat));
|
||||
const double v2x = static_cast<double>(toFloating(third.lon - second.lon));
|
||||
const double v2y =
|
||||
mercator::latToY(toFloating(third.lat)) - mercator::latToY(toFloating(second.lat));
|
||||
|
||||
double angle = (atan2_lookup(v2y, v2x) - atan2_lookup(v1y, v1x)) * 180. / pi<double>();
|
||||
|
||||
@@ -256,20 +247,22 @@ double computeAngle(const FixedPointCoordinate first,
|
||||
|
||||
namespace mercator
|
||||
{
|
||||
double yToLat(const double value)
|
||||
FloatLatitude yToLat(const double value)
|
||||
{
|
||||
using namespace boost::math::constants;
|
||||
|
||||
return 180. * (1. / pi<long double>()) *
|
||||
(2. * std::atan(std::exp(value * pi<double>() / 180.)) - half_pi<double>());
|
||||
return FloatLatitude(
|
||||
180. * (1. / pi<long double>()) *
|
||||
(2. * std::atan(std::exp(value * pi<double>() / 180.)) - half_pi<double>()));
|
||||
}
|
||||
|
||||
double latToY(const double latitude)
|
||||
double latToY(const FloatLatitude latitude)
|
||||
{
|
||||
using namespace boost::math::constants;
|
||||
|
||||
return 180. * (1. / pi<double>()) *
|
||||
std::log(std::tan((pi<double>() / 4.) + latitude * (pi<double>() / 180.) / 2.));
|
||||
std::log(std::tan((pi<double>() / 4.) +
|
||||
static_cast<double>(latitude) * (pi<double>() / 180.) / 2.));
|
||||
}
|
||||
} // ns mercato // ns mercatorr
|
||||
} // ns coordinate_calculation
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace util
|
||||
namespace
|
||||
{
|
||||
|
||||
std::uint64_t bitInterleaving(const std::uint32_t latitude, const std::uint32_t longitude)
|
||||
std::uint64_t bitInterleaving(const std::uint32_t longitude, const std::uint32_t latitude)
|
||||
{
|
||||
std::uint64_t result = 0;
|
||||
for (std::int8_t index = 31; index >= 0; --index)
|
||||
@@ -69,11 +69,13 @@ void transposeCoordinate(std::uint32_t *x)
|
||||
}
|
||||
} // anonymous ns
|
||||
|
||||
std::uint64_t hilbertCode(const FixedPointCoordinate coordinate)
|
||||
std::uint64_t hilbertCode(const Coordinate coordinate)
|
||||
{
|
||||
unsigned location[2];
|
||||
location[0] = coordinate.lat + static_cast<int>(90 * COORDINATE_PRECISION);
|
||||
location[1] = coordinate.lon + static_cast<int>(180 * COORDINATE_PRECISION);
|
||||
std::uint32_t location[2];
|
||||
location[0] = static_cast<std::int32_t>(coordinate.lon) +
|
||||
static_cast<std::int32_t>(180 * COORDINATE_PRECISION);
|
||||
location[1] = static_cast<std::int32_t>(coordinate.lat) +
|
||||
static_cast<std::int32_t>(90 * COORDINATE_PRECISION);
|
||||
|
||||
transposeCoordinate(location);
|
||||
return bitInterleaving(location[0], location[1]);
|
||||
|
||||
Reference in New Issue
Block a user