From 14057a90343bc302f104b241541329513d02ebb5 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Wed, 26 Apr 2017 14:08:37 +0300 Subject: [PATCH] Review fixes. --- routing/cross_mwm_index_graph.hpp | 4 +- routing/edge_estimator.cpp | 4 +- routing/edge_estimator.hpp | 2 +- routing/index_graph_starter.cpp | 29 +++++------- routing/index_graph_starter.hpp | 18 ++++---- routing/index_router.cpp | 51 +++++++++++++-------- routing/routing_tests/index_graph_tools.cpp | 4 +- routing/routing_tests/index_graph_tools.hpp | 4 +- routing/world_graph.hpp | 12 +++-- 9 files changed, 70 insertions(+), 58 deletions(-) diff --git a/routing/cross_mwm_index_graph.hpp b/routing/cross_mwm_index_graph.hpp index 693509cb66..d3456849f7 100644 --- a/routing/cross_mwm_index_graph.hpp +++ b/routing/cross_mwm_index_graph.hpp @@ -48,9 +48,9 @@ public: { if (isEnter) { - std::vector const & enters = + auto const & enters = GetCrossMwmConnectorWithTransitions(numMwmId).GetEnters(); - for (Segment const & enter : enters) + for (auto const & enter : enters) fn(enter); } else diff --git a/routing/edge_estimator.cpp b/routing/edge_estimator.cpp index d3d4665d87..90b9f781a9 100644 --- a/routing/edge_estimator.cpp +++ b/routing/edge_estimator.cpp @@ -46,7 +46,7 @@ public: // EdgeEstimator overrides: double CalcSegmentWeight(Segment const & segment, RoadGeometry const & road) const override; double CalcHeuristic(m2::PointD const & from, m2::PointD const & to) const override; - double CalcLeapEdgeTime(m2::PointD const & from, m2::PointD const & to) const override; + double CalcLeapWeight(m2::PointD const & from, m2::PointD const & to) const override; double GetUTurnPenalty() const override; bool LeapIsAllowed(NumMwmId mwmId) const override; @@ -101,7 +101,7 @@ double CarEdgeEstimator::CalcHeuristic(m2::PointD const & from, m2::PointD const return TimeBetweenSec(from, to, m_maxSpeedMPS); } -double CarEdgeEstimator::CalcLeapEdgeTime(m2::PointD const & from, m2::PointD const & to) const +double CarEdgeEstimator::CalcLeapWeight(m2::PointD const & from, m2::PointD const & to) const { // Let us assume for the time being that // leap edges will be added with a half of max speed. diff --git a/routing/edge_estimator.hpp b/routing/edge_estimator.hpp index e55ece5440..af8883f694 100644 --- a/routing/edge_estimator.hpp +++ b/routing/edge_estimator.hpp @@ -29,7 +29,7 @@ public: // Note 1. The result of the method should be used if it's necessary to add a leap (fake) edge // (|form|, |to|) in road graph. // Note 2. The result of the method should be less or equal to CalcHeuristic(|form|, |to|). - virtual double CalcLeapEdgeTime(m2::PointD const & from, m2::PointD const & to) const = 0; + virtual double CalcLeapWeight(m2::PointD const & from, m2::PointD const & to) const = 0; virtual double GetUTurnPenalty() const = 0; // The leap is the shortcut edge from mwm border enter to exit. // Router can't use leaps on some mwms: e.g. mwm with loaded traffic data. diff --git a/routing/index_graph_starter.cpp b/routing/index_graph_starter.cpp index e4c60085bc..35b6eb5a3a 100644 --- a/routing/index_graph_starter.cpp +++ b/routing/index_graph_starter.cpp @@ -90,7 +90,7 @@ void IndexGraphStarter::GetFakeToNormalEdge(FakeVertex const & fakeVertex, bool Segment const segment(fakeVertex.GetMwmId(), fakeVertex.GetFeatureId(), fakeVertex.GetSegmentIdx(), forward); m2::PointD const & pointTo = GetPoint(segment, true /* front */); - double const weight = m_graph.GetEstimator().CalcLeapEdgeTime(fakeVertex.GetPoint(), pointTo); + double const weight = m_graph.GetEstimator().CalcLeapWeight(fakeVertex.GetPoint(), pointTo); edges.emplace_back(segment, weight); } @@ -102,11 +102,13 @@ void IndexGraphStarter::GetNormalToFakeEdge(Segment const & segment, FakeVertex if (segment.GetMwmId() == fakeVertex.GetMwmId() && m_graph.GetMode() == WorldGraph::Mode::LeapsOnly) { - // It's assumed here that GetEstimator().CalcLeapEdgeTime(p1, p2) == - // GetEstimator().CalcLeapEdgeTime(p2, p1). + // It's assumed here that GetEstimator().CalcLeapWeight(p1, p2) == + // GetEstimator().CalcLeapWeight(p2, p1). if (m_graph.IsTransition(segment, isOutgoing)) + { edges.emplace_back(fakeSegment, - m_graph.GetEstimator().CalcLeapEdgeTime(pointFrom, fakeVertex.GetPoint())); + m_graph.GetEstimator().CalcLeapWeight(pointFrom, fakeVertex.GetPoint())); + } return; } @@ -114,7 +116,7 @@ void IndexGraphStarter::GetNormalToFakeEdge(Segment const & segment, FakeVertex return; edges.emplace_back(fakeSegment, - m_graph.GetEstimator().CalcLeapEdgeTime(pointFrom, fakeVertex.GetPoint())); + m_graph.GetEstimator().CalcLeapWeight(pointFrom, fakeVertex.GetPoint())); } void IndexGraphStarter::ConnectLeapToTransitions(FakeVertex const & fakeVertex, bool isOutgoing, @@ -129,19 +131,10 @@ void IndexGraphStarter::ConnectLeapToTransitions(FakeVertex const & fakeVertex, // finish mwm should be connected with the finish point. So |isEnter| below should be set to true. m_graph.ForEachTransition( fakeVertex.GetMwmId(), !isOutgoing /* isEnter */, [&](Segment const & transition) { - // It's assumed here that GetEstimator().CalcLeapEdgeTime(p1, p2) == - // GetEstimator().CalcLeapEdgeTime(p2, p1). - edges.emplace_back(transition, m_graph.GetEstimator().CalcLeapEdgeTime( - segmentPoint, GetPoint(transition, isOutgoing))); + // It's assumed here that GetEstimator().CalcLeapWeight(p1, p2) == + // GetEstimator().CalcLeapWeight(p2, p1). + edges.emplace_back(transition, m_graph.GetEstimator().CalcLeapWeight( + segmentPoint, GetPoint(transition, isOutgoing))); }); } - -Segment const & IndexGraphStarter::ConvertSegment(Segment const & segment) -{ - if (segment == kStartFakeSegment) - return m_start.GetSegment(); - if (segment == kFinishFakeSegment) - return m_finish.GetSegment(); - return segment; -} } // namespace routing diff --git a/routing/index_graph_starter.hpp b/routing/index_graph_starter.hpp index 2b76c1c906..68e002e395 100644 --- a/routing/index_graph_starter.hpp +++ b/routing/index_graph_starter.hpp @@ -49,11 +49,20 @@ public: m2::PointD const m_point; }; + static uint32_t constexpr kFakeFeatureId = numeric_limits::max(); + static uint32_t constexpr kFakeSegmentIdx = numeric_limits::max(); + static Segment constexpr kStartFakeSegment = + Segment(kFakeNumMwmId, kFakeFeatureId, kFakeSegmentIdx, false); + static Segment constexpr kFinishFakeSegment = + Segment(kFakeNumMwmId, kFakeFeatureId, kFakeSegmentIdx, true); + IndexGraphStarter(FakeVertex const & start, FakeVertex const & finish, WorldGraph & graph); WorldGraph & GetGraph() { return m_graph; } Segment const & GetStart() const { return kStartFakeSegment; } Segment const & GetFinish() const { return kFinishFakeSegment; } + FakeVertex const & GetStartVertex() const { return m_start; } + FakeVertex const & GetFinishVertex() const { return m_finish; } m2::PointD const & GetPoint(Segment const & segment, bool front); static size_t GetRouteNumPoints(vector const & route); @@ -94,16 +103,7 @@ public: return segment.GetFeatureId() == kFakeFeatureId; } - Segment const & ConvertSegment(Segment const & segment); - private: - static uint32_t constexpr kFakeFeatureId = numeric_limits::max(); - static uint32_t constexpr kFakeSegmentIdx = numeric_limits::max(); - static Segment constexpr kStartFakeSegment = - Segment(kFakeNumMwmId, kFakeFeatureId, kFakeSegmentIdx, false); - static Segment constexpr kFinishFakeSegment = - Segment(kFakeNumMwmId, kFakeFeatureId, kFakeSegmentIdx, true); - void GetFakeToNormalEdges(FakeVertex const & fakeVertex, bool isOutgoing, vector & edges); void GetFakeToNormalEdge(FakeVertex const & fakeVertex, bool forward, diff --git a/routing/index_router.cpp b/routing/index_router.cpp index 0229ac28b4..73dbc7f262 100644 --- a/routing/index_router.cpp +++ b/routing/index_router.cpp @@ -134,16 +134,20 @@ IRouter::ResultCode IndexRouter::DoCalculateRoute(string const & startCountry, TrafficStash::Guard guard(*m_trafficStash); WorldGraph graph( - make_unique(m_numMwmIds, m_numMwmTree, m_vehicleModelFactory, m_countryRectFn, - m_index, m_indexManager), - IndexGraphLoader::Create(m_numMwmIds, m_vehicleModelFactory, m_estimator, m_index), - m_estimator); + make_unique(m_numMwmIds, m_numMwmTree, m_vehicleModelFactory, m_countryRectFn, + m_index, m_indexManager), + IndexGraphLoader::Create(m_numMwmIds, m_vehicleModelFactory, m_estimator, m_index), + m_estimator); + + WorldGraph::Mode mode = WorldGraph::Mode::SingleMwm; + if (forSingleMwm) + mode = WorldGraph::Mode::SingleMwm; + else if (AreMwmsNear(start.GetMwmId(), finish.GetMwmId())) + mode = WorldGraph::Mode::LeapsIfPossible; + else + mode = WorldGraph::Mode::LeapsOnly; + graph.SetMode(mode); - auto const getRoutingMode = [&]() { - return AreMwmsNear(start.GetMwmId(), finish.GetMwmId()) ? WorldGraph::Mode::LeapsIfPossible - : WorldGraph::Mode::LeapsOnly; - }; - graph.SetMode(forSingleMwm ? WorldGraph::Mode::SingleMwm : getRoutingMode()); LOG(LINFO, ("Routing in mode:", graph.GetMode())); IndexGraphStarter starter(start, finish, graph); @@ -248,18 +252,29 @@ IRouter::ResultCode IndexRouter::ProcessLeaps(vector const & input, continue; } - Segment const & convertedCurrent = starter.ConvertSegment(input[i]); ++i; CHECK_LESS(i, input.size(), ()); - Segment const & convertedNext = starter.ConvertSegment(input[i]); - CHECK_EQUAL( - convertedCurrent.GetMwmId(), convertedNext.GetMwmId(), - ("Different mwm ids for leap enter and exit, i:", i, "size of input:", input.size())); + Segment const & next = input[i]; + + CHECK_NOT_EQUAL(current, IndexGraphStarter::kFinishFakeSegment, ()); + CHECK_NOT_EQUAL(next, IndexGraphStarter::kStartFakeSegment, ()); + if (current != IndexGraphStarter::kStartFakeSegment && + next != IndexGraphStarter::kFinishFakeSegment) + { + CHECK_EQUAL( + current.GetMwmId(), next.GetMwmId(), + ("Different mwm ids for leap enter and exit, i:", i, "size of input:", input.size())); + } + + IndexGraphStarter::FakeVertex const start = + (current == IndexGraphStarter::kStartFakeSegment) + ? starter.GetStartVertex() + : IndexGraphStarter::FakeVertex(current, starter.GetPoint(current, true /* front */)); + IndexGraphStarter::FakeVertex const finish = + (next == IndexGraphStarter::kFinishFakeSegment) + ? starter.GetFinishVertex() + : IndexGraphStarter::FakeVertex(next, starter.GetPoint(next, true /* front */)); - IndexGraphStarter::FakeVertex const start(convertedCurrent, - starter.GetPoint(convertedCurrent, true /* front */)); - IndexGraphStarter::FakeVertex const finish(convertedNext, - starter.GetPoint(convertedNext, true /* front */)); IndexGraphStarter leapStarter(start, finish, starter.GetGraph()); // Clear previous loaded graphs. diff --git a/routing/routing_tests/index_graph_tools.cpp b/routing/routing_tests/index_graph_tools.cpp index 8d765035d2..b64221a261 100644 --- a/routing/routing_tests/index_graph_tools.cpp +++ b/routing/routing_tests/index_graph_tools.cpp @@ -68,8 +68,8 @@ double WeightedEdgeEstimator::CalcHeuristic(m2::PointD const & /* from */, return 0.0; } -double WeightedEdgeEstimator::CalcLeapEdgeTime(m2::PointD const & /* from */, - m2::PointD const & /* to */) const +double WeightedEdgeEstimator::CalcLeapWeight(m2::PointD const & /* from */, + m2::PointD const & /* to */) const { return 0.0; } diff --git a/routing/routing_tests/index_graph_tools.hpp b/routing/routing_tests/index_graph_tools.hpp index cc9adb1f5b..476651a0d2 100644 --- a/routing/routing_tests/index_graph_tools.hpp +++ b/routing/routing_tests/index_graph_tools.hpp @@ -101,8 +101,8 @@ public: // EdgeEstimator overrides: double CalcSegmentWeight(Segment const & segment, RoadGeometry const & /* road */) const override; double CalcHeuristic(m2::PointD const & /* from */, m2::PointD const & /* to */) const override; - double CalcLeapEdgeTime(m2::PointD const & /* from */, - m2::PointD const & /* to */) const override; + double CalcLeapWeight(m2::PointD const & /* from */, + m2::PointD const & /* to */) const override; double GetUTurnPenalty() const override; bool LeapIsAllowed(NumMwmId /* mwmId */) const override; diff --git a/routing/world_graph.hpp b/routing/world_graph.hpp index 283092f19b..e0c7c067ba 100644 --- a/routing/world_graph.hpp +++ b/routing/world_graph.hpp @@ -19,10 +19,14 @@ class WorldGraph final public: enum class Mode { - SingleMwm, - LeapsOnly, - LeapsIfPossible, - NoLeaps, + SingleMwm, // Mode for building a route within single mwm. + LeapsOnly, // Mode for building a cross mwm route contains of only leaps. In case of start and + // finish they (start and finish) will be connected with all transition segments of + // their mwm with leap (fake) edges. + LeapsIfPossible, // Mode for build cross mwm and single mwm routes. In case of cross mwm route + // if they are neighboring mwms the route will be made without leaps. + // If not the route is made with leaps for intermediate mwms. + NoLeaps, // Mode for building route and getting outgoing/ingoing edges without leaps anyway. }; WorldGraph(std::unique_ptr crossMwmGraph, std::unique_ptr loader,