From 4ea013048a4c8e96e6866c4d24de1cd60ee60fdd Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Thu, 21 Apr 2016 17:35:57 +0300 Subject: [PATCH] [bicycle routing] Moving common methods for pedestrian and bicycle routing to derections_engine unit. --- routing/directions_engine.cpp | 75 +++++++++++++++++++++++++++++++ routing/directions_engine.hpp | 7 +++ routing/pedestrian_directions.cpp | 69 ---------------------------- routing/pedestrian_directions.hpp | 7 --- routing/routing.pro | 3 +- 5 files changed, 84 insertions(+), 77 deletions(-) create mode 100644 routing/directions_engine.cpp diff --git a/routing/directions_engine.cpp b/routing/directions_engine.cpp new file mode 100644 index 0000000000..c79d997457 --- /dev/null +++ b/routing/directions_engine.cpp @@ -0,0 +1,75 @@ +#include "routing/directions_engine.hpp" + +#include "base/assert.hpp" + +namespace +{ +double constexpr KMPH2MPS = 1000.0 / (60 * 60); +} // namespace + +namespace routing +{ +void IDirectionsEngine::CalculateTimes(IRoadGraph const & graph, vector const & path, + Route::TTimes & times) const +{ + double const speedMPS = graph.GetMaxSpeedKMPH() * KMPH2MPS; + + times.reserve(path.size()); + + double trackTimeSec = 0.0; + times.emplace_back(0, trackTimeSec); + + m2::PointD prev = path[0].GetPoint(); + for (size_t i = 1; i < path.size(); ++i) + { + m2::PointD const & curr = path[i].GetPoint(); + + double const lengthM = MercatorBounds::DistanceOnEarth(prev, curr); + trackTimeSec += lengthM / speedMPS; + + times.emplace_back(i, trackTimeSec); + + prev = curr; + } +} + +bool IDirectionsEngine::ReconstructPath(IRoadGraph const & graph, vector const & path, + vector & routeEdges, + my::Cancellable const & cancellable) const +{ + routeEdges.reserve(path.size() - 1); + + Junction curr = path[0]; + vector currEdges; + for (size_t i = 1; i < path.size(); ++i) + { + if (cancellable.IsCancelled()) + return false; + + Junction const & next = path[i]; + + currEdges.clear(); + graph.GetOutgoingEdges(curr, currEdges); + + bool found = false; + for (Edge const & e : currEdges) + { + if (e.GetEndJunction() == next) + { + routeEdges.emplace_back(e); + found = true; + break; + } + } + + if (!found) + return false; + + curr = next; + } + + ASSERT_EQUAL(routeEdges.size()+1, path.size(), ()); + + return true; +} +} // namespace routing diff --git a/routing/directions_engine.hpp b/routing/directions_engine.hpp index 966f9707eb..274f6b2b68 100644 --- a/routing/directions_engine.hpp +++ b/routing/directions_engine.hpp @@ -17,6 +17,13 @@ public: Route::TTimes & times, Route::TTurns & turnsDir, my::Cancellable const & cancellable) = 0; +protected: + bool ReconstructPath(IRoadGraph const & graph, vector const & path, + vector & routeEdges, + my::Cancellable const & cancellable) const; + + void CalculateTimes(IRoadGraph const & graph, vector const & path, + Route::TTimes & times) const; }; } // namespace routing diff --git a/routing/pedestrian_directions.cpp b/routing/pedestrian_directions.cpp index 53e5d4009d..f9a973ef22 100644 --- a/routing/pedestrian_directions.cpp +++ b/routing/pedestrian_directions.cpp @@ -1,18 +1,14 @@ #include "routing/pedestrian_directions.hpp" -#include "routing/turns_generator.hpp" #include "indexer/classificator.hpp" #include "indexer/feature.hpp" #include "indexer/ftypes_matcher.hpp" -#include "indexer/index.hpp" #include "base/assert.hpp" #include "base/logging.hpp" namespace { -double constexpr KMPH2MPS = 1000.0 / (60 * 60); - bool HasType(uint32_t type, feature::TypesHolder const & types) { for (uint32_t t : types) @@ -56,70 +52,6 @@ void PedestrianDirectionsEngine::Generate(IRoadGraph const & graph, vector const & path, - vector & routeEdges, - my::Cancellable const & cancellable) const -{ - routeEdges.reserve(path.size() - 1); - - Junction curr = path[0]; - vector currEdges; - for (size_t i = 1; i < path.size(); ++i) - { - if (cancellable.IsCancelled()) - return false; - - Junction const & next = path[i]; - - currEdges.clear(); - graph.GetOutgoingEdges(curr, currEdges); - - bool found = false; - for (Edge const & e : currEdges) - { - if (e.GetEndJunction() == next) - { - routeEdges.emplace_back(e); - found = true; - break; - } - } - - if (!found) - return false; - - curr = next; - } - - ASSERT_EQUAL(routeEdges.size()+1, path.size(), ()); - - return true; -} - -void PedestrianDirectionsEngine::CalculateTimes(IRoadGraph const & graph, vector const & path, - Route::TTimes & times) const -{ - double const speedMPS = graph.GetMaxSpeedKMPH() * KMPH2MPS; - - times.reserve(path.size()); - - double trackTimeSec = 0.0; - times.emplace_back(0, trackTimeSec); - - m2::PointD prev = path[0].GetPoint(); - for (size_t i = 1; i < path.size(); ++i) - { - m2::PointD const & curr = path[i].GetPoint(); - - double const lengthM = MercatorBounds::DistanceOnEarth(prev, curr); - trackTimeSec += lengthM / speedMPS; - - times.emplace_back(i, trackTimeSec); - - prev = curr; - } -} - void PedestrianDirectionsEngine::CalculateTurns(IRoadGraph const & graph, vector const & routeEdges, Route::TTurns & turnsDir, my::Cancellable const & cancellable) const @@ -156,5 +88,4 @@ void PedestrianDirectionsEngine::CalculateTurns(IRoadGraph const & graph, vector // (index of last junction is the same as number of edges) turnsDir.emplace_back(routeEdges.size(), turns::PedestrianDirection::ReachedYourDestination); } - } // namespace routing diff --git a/routing/pedestrian_directions.hpp b/routing/pedestrian_directions.hpp index a82dd8001b..f58842c675 100644 --- a/routing/pedestrian_directions.hpp +++ b/routing/pedestrian_directions.hpp @@ -17,13 +17,6 @@ public: my::Cancellable const & cancellable) override; private: - bool ReconstructPath(IRoadGraph const & graph, vector const & path, - vector & routeEdges, - my::Cancellable const & cancellable) const; - - void CalculateTimes(IRoadGraph const & graph, vector const & path, - Route::TTimes & times) const; - void CalculateTurns(IRoadGraph const & graph, vector const & routeEdges, Route::TTurns & turnsDir, my::Cancellable const & cancellable) const; diff --git a/routing/routing.pro b/routing/routing.pro index b5b9df45fe..ecb6fe3902 100644 --- a/routing/routing.pro +++ b/routing/routing.pro @@ -20,6 +20,7 @@ SOURCES += \ cross_mwm_road_graph.cpp \ cross_mwm_router.cpp \ cross_routing_context.cpp \ + directions_engine.cpp \ features_road_graph.cpp \ nearest_edge_finder.cpp \ online_absent_fetcher.cpp \ @@ -51,7 +52,7 @@ HEADERS += \ async_router.hpp \ base/astar_algorithm.hpp \ base/followed_polyline.hpp \ - bicycle_model.cpp \ + bicycle_model.hpp \ car_model.hpp \ cross_mwm_road_graph.hpp \ cross_mwm_router.hpp \