Be kind to the optimizer, pass coordinates by value (just two ints)

This commit is contained in:
Daniel J. Hofmann
2016-01-21 13:07:24 +01:00
committed by Patrick Niklaus
parent 46fc6f8da4
commit d391df52ba
17 changed files with 165 additions and 162 deletions
+3 -3
View File
@@ -20,8 +20,8 @@ namespace
struct CoordinatePairCalculator
{
CoordinatePairCalculator() = delete;
CoordinatePairCalculator(const util::FixedPointCoordinate &coordinate_a,
const util::FixedPointCoordinate &coordinate_b)
CoordinatePairCalculator(const util::FixedPointCoordinate coordinate_a,
const util::FixedPointCoordinate coordinate_b)
{
// initialize distance calculator with two fixed coordinates a, b
first_lat = (coordinate_a.lat / COORDINATE_PRECISION) * util::RAD;
@@ -30,7 +30,7 @@ struct CoordinatePairCalculator
second_lon = (coordinate_b.lon / COORDINATE_PRECISION) * util::RAD;
}
int operator()(util::FixedPointCoordinate &other) const
int operator()(const util::FixedPointCoordinate other) const
{
// set third coordinate c
const float float_lat1 = (other.lat / COORDINATE_PRECISION) * util::RAD;
+23 -25
View File
@@ -6,8 +6,6 @@
#include <boost/assert.hpp>
#include "osrm/coordinate.hpp"
#include <cmath>
#include <limits>
@@ -44,15 +42,15 @@ 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)
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)
double greatCircleDistance(const FixedPointCoordinate coordinate_1,
const FixedPointCoordinate coordinate_2)
{
return greatCircleDistance(coordinate_1.lat, coordinate_1.lon, coordinate_2.lat,
coordinate_2.lon);
@@ -75,9 +73,9 @@ 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 FixedPointCoordinate source_coordinate,
const FixedPointCoordinate target_coordinate,
const FixedPointCoordinate query_location)
{
double ratio;
FixedPointCoordinate nearest_location;
@@ -86,9 +84,9 @@ double perpendicularDistance(const FixedPointCoordinate &source_coordinate,
nearest_location, ratio);
}
double perpendicularDistance(const FixedPointCoordinate &segment_source,
const FixedPointCoordinate &segment_target,
const FixedPointCoordinate &query_location,
double perpendicularDistance(const FixedPointCoordinate segment_source,
const FixedPointCoordinate segment_target,
const FixedPointCoordinate query_location,
FixedPointCoordinate &nearest_location,
double &ratio)
{
@@ -100,10 +98,10 @@ double perpendicularDistance(const FixedPointCoordinate &segment_source,
}
double
perpendicularDistanceFromProjectedCoordinate(const FixedPointCoordinate &source_coordinate,
const FixedPointCoordinate &target_coordinate,
const FixedPointCoordinate &query_location,
const std::pair<double, double> &projected_coordinate)
perpendicularDistanceFromProjectedCoordinate(const FixedPointCoordinate source_coordinate,
const FixedPointCoordinate target_coordinate,
const FixedPointCoordinate query_location,
const std::pair<double, double> projected_coordinate)
{
double ratio;
FixedPointCoordinate nearest_location;
@@ -114,10 +112,10 @@ perpendicularDistanceFromProjectedCoordinate(const FixedPointCoordinate &source_
}
double
perpendicularDistanceFromProjectedCoordinate(const FixedPointCoordinate &segment_source,
const FixedPointCoordinate &segment_target,
const FixedPointCoordinate &query_location,
const std::pair<double, double> &projected_coordinate,
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)
{
@@ -196,8 +194,8 @@ double degToRad(const double degree) { return degree * (static_cast<double>(M_PI
double radToDeg(const double radian) { return radian * (180.0 * static_cast<double>(M_1_PI)); }
double bearing(const FixedPointCoordinate &first_coordinate,
const FixedPointCoordinate &second_coordinate)
double bearing(const FixedPointCoordinate first_coordinate,
const FixedPointCoordinate second_coordinate)
{
const double lon_diff =
second_coordinate.lon / COORDINATE_PRECISION - first_coordinate.lon / COORDINATE_PRECISION;
@@ -220,9 +218,9 @@ double bearing(const FixedPointCoordinate &first_coordinate,
return result;
}
double computeAngle(const FixedPointCoordinate &first,
const FixedPointCoordinate &second,
const FixedPointCoordinate &third)
double computeAngle(const FixedPointCoordinate first,
const FixedPointCoordinate second,
const FixedPointCoordinate third)
{
const double v1x = (first.lon - second.lon) / COORDINATE_PRECISION;
const double v1y = mercator::latToY(first.lat / COORDINATE_PRECISION) -
+15 -16
View File
@@ -1,13 +1,11 @@
#include "util/hilbert_value.hpp"
#include "osrm/coordinate.hpp"
namespace osrm
{
namespace util
{
uint64_t HilbertCode::operator()(const FixedPointCoordinate &current_coordinate) const
std::uint64_t HilbertCode::operator()(const FixedPointCoordinate current_coordinate) const
{
unsigned location[2];
location[0] = current_coordinate.lat + static_cast<int>(90 * COORDINATE_PRECISION);
@@ -17,10 +15,11 @@ uint64_t HilbertCode::operator()(const FixedPointCoordinate &current_coordinate)
return BitInterleaving(location[0], location[1]);
}
uint64_t HilbertCode::BitInterleaving(const uint32_t latitude, const uint32_t longitude) const
std::uint64_t HilbertCode::BitInterleaving(const std::uint32_t latitude,
const std::uint32_t longitude) const
{
uint64_t result = 0;
for (int8_t index = 31; index >= 0; --index)
std::uint64_t result = 0;
for (std::int8_t index = 31; index >= 0; --index)
{
result |= (latitude >> index) & 1;
result <<= 1;
@@ -33,9 +32,9 @@ uint64_t HilbertCode::BitInterleaving(const uint32_t latitude, const uint32_t lo
return result;
}
void HilbertCode::TransposeCoordinate(uint32_t *X) const
void HilbertCode::TransposeCoordinate(std::uint32_t *x) const
{
uint32_t M = 1u << (32 - 1), P, Q, t;
std::uint32_t M = 1u << (32 - 1), P, Q, t;
int i;
// Inverse undo
for (Q = M; Q > 1; Q >>= 1)
@@ -44,28 +43,28 @@ void HilbertCode::TransposeCoordinate(uint32_t *X) const
for (i = 0; i < 2; ++i)
{
const bool condition = (X[i] & Q);
const bool condition = (x[i] & Q);
if (condition)
{
X[0] ^= P; // invert
x[0] ^= P; // invert
}
else
{
t = (X[0] ^ X[i]) & P;
X[0] ^= t;
X[i] ^= t;
t = (x[0] ^ x[i]) & P;
x[0] ^= t;
x[i] ^= t;
}
} // exchange
}
// Gray encode
for (i = 1; i < 2; ++i)
{
X[i] ^= X[i - 1];
x[i] ^= x[i - 1];
}
t = 0;
for (Q = M; Q > 1; Q >>= 1)
{
const bool condition = (X[2 - 1] & Q);
const bool condition = (x[2 - 1] & Q);
if (condition)
{
t ^= Q - 1;
@@ -73,7 +72,7 @@ void HilbertCode::TransposeCoordinate(uint32_t *X) const
} // check if this for loop is wrong
for (i = 0; i < 2; ++i)
{
X[i] ^= t;
x[i] ^= t;
}
}
}