From 4c7ec467036c3a082bb6372d652ff6babf86364d Mon Sep 17 00:00:00 2001 From: Olga Khlopkova Date: Fri, 11 Dec 2020 17:25:57 +0300 Subject: [PATCH] [routing] Fix for weight calculatin in RegionsSparseGraph. --- routing/index_graph_starter.cpp | 11 +++++++---- routing/regions_sparse_graph.cpp | 32 +++++++++++++++++++++++++++++--- routing/regions_sparse_graph.hpp | 3 ++- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/routing/index_graph_starter.cpp b/routing/index_graph_starter.cpp index 0d9661a96b..03a4c06144 100644 --- a/routing/index_graph_starter.cpp +++ b/routing/index_graph_starter.cpp @@ -227,7 +227,8 @@ void IndexGraphStarter::GetEdgesList(astar::VertexData const & v } else if (IsRegionsGraphMode()) { - m_regionsGraph->GetEdgeList(real, isOutgoing, edges, ingoingSegmentWeight); + m_regionsGraph->GetEdgeList(real, isOutgoing, edges, + GetJunction(segment, true /* front */).GetLatLon()); } else { @@ -250,7 +251,8 @@ void IndexGraphStarter::GetEdgesList(astar::VertexData const & v } else if (IsRegionsGraphMode()) { - m_regionsGraph->GetEdgeList(segment, isOutgoing, edges, ingoingSegmentWeight); + m_regionsGraph->GetEdgeList(segment, isOutgoing, edges, + GetJunction(segment, true /* front */).GetLatLon()); } else { @@ -296,11 +298,12 @@ RouteWeight IndexGraphStarter::CalcSegmentWeight(Segment const & segment, // Theoretically it may be differ from |RouteWeight(0)| because some road access block // may be kept in it and it is up to |RouteWeight| to know how to multiply by zero. + if (IsRegionsGraphMode()) + return RouteWeight(partLen); + Weight weight; if (IsGuidesSegment(real)) weight = CalcGuidesSegmentWeight(real, purpose); - else if (IsRegionsGraphMode()) - weight = m_regionsGraph->CalcSegmentWeight(real); else weight = m_graph.CalcSegmentWeight(real, purpose); if (fullLen == 0.0) diff --git a/routing/regions_sparse_graph.cpp b/routing/regions_sparse_graph.cpp index 50d72cc072..e53eb83f63 100644 --- a/routing/regions_sparse_graph.cpp +++ b/routing/regions_sparse_graph.cpp @@ -7,6 +7,7 @@ #include "coding/file_reader.hpp" +#include "geometry/distance_on_sphere.hpp" #include "geometry/mercator.hpp" #include "base/file_name_utils.hpp" @@ -38,6 +39,7 @@ void RegionsSparseGraph::LoadRegionsSparseGraph() ReaderSource reader(mwmValue.m_cont.GetReader(ROUTING_WORLD_FILE_TAG)); CrossBorderGraphSerializer::Deserialize(m_graph, reader, m_numMwmIds); + ASSERT(!m_graph.m_segments.empty(), ()); } std::optional RegionsSparseGraph::GetFakeEnding(m2::PointD const & point) const @@ -58,6 +60,7 @@ std::optional RegionsSparseGraph::GetFakeEnding(m2::PointD const & p bool const oneWay = false; auto const & frontJunction = data.m_end.m_point; auto const & backJunction = data.m_start.m_point; + auto const & projectedJunction = CalcProjectionToSegment(backJunction, frontJunction, point); Segment const segment(data.m_start.m_numMwmId, segmentId /* featureId */, 0 /* segmentIdx */, @@ -70,9 +73,19 @@ std::optional RegionsSparseGraph::GetFakeEnding(m2::PointD const & p return ending; } +double GetWeight(CrossBorderSegment const & toSeg, LatLonWithAltitude const & coordFrom, + NumMwmId fromMwm) +{ + auto const & coordTo = + toSeg.m_start.m_numMwmId == fromMwm ? toSeg.m_start.m_point : toSeg.m_end.m_point; + CHECK(toSeg.m_start.m_numMwmId == fromMwm || toSeg.m_end.m_numMwmId == fromMwm, + (fromMwm, toSeg.m_start.m_numMwmId, toSeg.m_end.m_numMwmId, coordTo)); + return toSeg.m_weight + ms::DistanceOnEarth(coordFrom.GetLatLon(), coordTo.GetLatLon()); +} + void RegionsSparseGraph::GetEdgeList(Segment const & segment, bool isOutgoing, std::vector & edges, - RouteWeight const & prevWeight) const + ms::LatLon const & prevSegFront) const { auto const & data = GetDataById(segment.GetFeatureId()); @@ -83,12 +96,25 @@ void RegionsSparseGraph::GetEdgeList(Segment const & segment, bool isOutgoing, if (it == m_graph.m_mwms.end()) return; + LatLonWithAltitude const & segCoord = + (segment.IsForward() == isOutgoing) ? data.m_end.m_point : data.m_start.m_point; + for (auto const & id : it->second) { auto const outData = GetDataById(id); - auto const weight = isOutgoing ? RouteWeight(outData.m_weight) : prevWeight; + if (id == segment.GetFeatureId()) + continue; + LatLonWithAltitude const segFront = + segment.IsForward() ? outData.m_end.m_point : outData.m_start.m_point; - Segment const edge(outData.m_start.m_numMwmId, id /* featureId */, 0 /* segmentIdx */, + auto const weight = isOutgoing + ? RouteWeight(GetWeight(outData, segCoord, targetMwm)) + : RouteWeight(ms::DistanceOnEarth(segFront.GetLatLon(), prevSegFront)); + + auto const outMwm = targetMwm == outData.m_start.m_numMwmId ? outData.m_end.m_numMwmId + : outData.m_start.m_numMwmId; + + Segment const edge(outMwm, id /* featureId */, 0 /* segmentIdx */, segment.IsForward() /* isForward */); edges.emplace_back(edge, weight); } diff --git a/routing/regions_sparse_graph.hpp b/routing/regions_sparse_graph.hpp index 82f2eb6000..74368ee25f 100644 --- a/routing/regions_sparse_graph.hpp +++ b/routing/regions_sparse_graph.hpp @@ -9,6 +9,7 @@ #include "indexer/data_source.hpp" +#include "geometry/latlon.hpp" #include "geometry/mercator.hpp" #include "geometry/point2d.hpp" @@ -36,7 +37,7 @@ public: std::optional GetFakeEnding(m2::PointD const & point) const; void GetEdgeList(Segment const & segment, bool isOutgoing, std::vector & edges, - RouteWeight const & prevWeight) const; + ms::LatLon const & prevSegFront) const; routing::LatLonWithAltitude const & GetJunction(Segment const & segment, bool front) const;