From 8ac393623c214c8424912ef60374ed14a5ee07e4 Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Sun, 12 May 2024 12:05:08 +0200 Subject: [PATCH] Optimise R-tree queries in the case if there is no need to limit maximum number of results --- include/util/coordinate_calculation.hpp | 7 +++++++ include/util/rectangle.hpp | 16 ++++------------ include/util/static_rtree.hpp | 18 +++--------------- 3 files changed, 14 insertions(+), 27 deletions(-) diff --git a/include/util/coordinate_calculation.hpp b/include/util/coordinate_calculation.hpp index 3ab8db076..6a716ffaf 100644 --- a/include/util/coordinate_calculation.hpp +++ b/include/util/coordinate_calculation.hpp @@ -36,6 +36,13 @@ inline double radToDeg(const double radian) } } // namespace detail +const constexpr static double METERS_PER_DEGREE_LAT = 110567.0; + +inline double metersPerLngDegree(const FixedLatitude lat) +{ + return std::cos(detail::degToRad(static_cast(toFloating(lat)))) * METERS_PER_DEGREE_LAT; +} + //! Takes the squared euclidean distance of the input coordinates. Does not return meters! std::uint64_t squaredEuclideanDistance(const Coordinate lhs, const Coordinate rhs); diff --git a/include/util/rectangle.hpp b/include/util/rectangle.hpp index ff4c2c6bc..2d4a45fc8 100644 --- a/include/util/rectangle.hpp +++ b/include/util/rectangle.hpp @@ -4,8 +4,8 @@ #include "coordinate.hpp" #include "util/coordinate.hpp" #include "util/coordinate_calculation.hpp" - #include +#include #include #include @@ -170,19 +170,11 @@ struct RectangleInt2D max_lat != FixedLatitude{std::numeric_limits::min()}; } - static double MetersPerLngDegree(const FixedLatitude lat) - { - constexpr static double kMetersPerDegreeLat = 110567.0f; - - constexpr float kRadPerDeg = (3.14 /* TODO */ / 180.0f); - - return std::cos(kRadPerDeg * static_cast(toFloating(lat))) * kMetersPerDegreeLat; - } static RectangleInt2D ExpandMeters(const Coordinate &coordinate, const double meters) { - constexpr static double kMetersPerDegreeLat = 110567.0f; - const double lat_offset = meters / kMetersPerDegreeLat; - const double lon_offset = meters / MetersPerLngDegree(coordinate.lat); + const double lat_offset = meters / coordinate_calculation::METERS_PER_DEGREE_LAT; + const double lon_offset = + meters / coordinate_calculation::metersPerLngDegree(coordinate.lat); return RectangleInt2D{coordinate.lon - toFixed(FloatLongitude{lon_offset}), coordinate.lon + toFixed(FloatLongitude{lon_offset}), diff --git a/include/util/static_rtree.hpp b/include/util/static_rtree.hpp index 0b562afe1..fb5c0a353 100644 --- a/include/util/static_rtree.hpp +++ b/include/util/static_rtree.hpp @@ -565,21 +565,9 @@ class StaticRTree { return num_results >= max_results; }); } - inline double GetSegmentDistance(const Coordinate input_coordinate, - const CandidateSegment &segment) const - { - BOOST_ASSERT(segment.data.forward_segment_id.id != SPECIAL_SEGMENTID || - !segment.data.forward_segment_id.enabled); - BOOST_ASSERT(segment.data.reverse_segment_id.id != SPECIAL_SEGMENTID || - !segment.data.reverse_segment_id.enabled); - - Coordinate wsg84_coordinate = - util::web_mercator::toWGS84(segment.fixed_projected_coordinate); - - return util::coordinate_calculation::greatCircleDistance(input_coordinate, - wsg84_coordinate); - } - + // NB 1: results are not guaranteed to be sorted by distance + // NB 2: maxDistanceMeters is not a hard limit, it's just a way to reduce the number of edges + // returned template std::vector SearchInRange(const Coordinate input_coordinate, double maxDistanceMeters,