From 5ba26d3d6d0cf4ec9f7b3adc9269eb58fd6549ce Mon Sep 17 00:00:00 2001 From: Huyen Chau Nguyen Date: Wed, 25 Apr 2018 10:59:07 -0400 Subject: [PATCH] refactor cheap ruler cache --- src/util/coordinate_calculation.cpp | 39 +++++++++++++---------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/util/coordinate_calculation.cpp b/src/util/coordinate_calculation.cpp index e1d9aa1935..cf0b29dd1c 100644 --- a/src/util/coordinate_calculation.cpp +++ b/src/util/coordinate_calculation.cpp @@ -22,26 +22,23 @@ namespace coordinate_calculation namespace { -mapbox::cheap_ruler::CheapRuler cheap_ruler_cache[] = { - mapbox::cheap_ruler::CheapRuler(-90, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(-80, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(-70, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(-60, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(-50, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(-40, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(-30, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(-20, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(-10, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(0, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(10, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(20, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(30, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(40, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(50, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(60, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(70, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(80, mapbox::cheap_ruler::CheapRuler::Meters), - mapbox::cheap_ruler::CheapRuler(90, mapbox::cheap_ruler::CheapRuler::Meters)}; +class CheapRulerContainer { +public: + CheapRulerContainer(const int number_of_rulers) + : cheap_ruler_cache(number_of_rulers, mapbox::cheap_ruler::CheapRuler(0)), step(180. / number_of_rulers){ + for (int n = 0; n < number_of_rulers; n++) { + cheap_ruler_cache[n] = mapbox::cheap_ruler::CheapRuler(-90 + step * n, mapbox::cheap_ruler::CheapRuler::Meters); + } + }; + mapbox::cheap_ruler::CheapRuler& getRuler(const double lat) { + return cheap_ruler_cache[std::min((int)std::round((lat + 90)/step), (int)cheap_ruler_cache.size() - 1)]; + }; + +private: + std::vector cheap_ruler_cache; + const double step; +}; +CheapRulerContainer cheap_ruler_container(3600); } // Does not project the coordinates! @@ -68,7 +65,7 @@ double fccApproximateDistance(const Coordinate coordinate_1, const Coordinate co const auto lat1 = static_cast(util::toFloating(coordinate_1.lat)); const auto lon2 = static_cast(util::toFloating(coordinate_2.lon)); const auto lat2 = static_cast(util::toFloating(coordinate_2.lat)); - return cheap_ruler_cache[std::lround(lat1 / 10) + 9].distance({lon1, lat1}, {lon2, lat2}); + return cheap_ruler_container.getRuler(lat1).distance({lon1, lat1}, {lon2, lat2}); } double haversineDistance(const Coordinate coordinate_1, const Coordinate coordinate_2)