From 63df578e6eace6068e8d3524bcea26ea4abb2fa3 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Thu, 19 Apr 2018 13:29:41 +0300 Subject: Gathering all info about loading Geometry and IndexGraph in IndexGraphLoader. --- routing/index_graph.cpp | 16 +++++------ routing/index_graph.hpp | 9 ++---- routing/index_graph_loader.cpp | 44 ++++++++++++++++++----------- routing/routing_tests/index_graph_test.cpp | 2 +- routing/routing_tests/index_graph_tools.cpp | 8 ++++-- 5 files changed, 44 insertions(+), 35 deletions(-) (limited to 'routing') diff --git a/routing/index_graph.cpp b/routing/index_graph.cpp index 6089d4bf94..c8820bc01e 100644 --- a/routing/index_graph.cpp +++ b/routing/index_graph.cpp @@ -57,9 +57,10 @@ bool IsRestricted(RestrictionVec const & restrictions, Segment const & u, Segmen namespace routing { -IndexGraph::IndexGraph(unique_ptr loader, shared_ptr estimator) - : m_geometry(move(loader)), m_estimator(move(estimator)) +IndexGraph::IndexGraph(shared_ptr geometry, shared_ptr estimator) + : m_geometry(geometry), m_estimator(move(estimator)) { + ASSERT(m_geometry, ()); ASSERT(m_estimator, ()); } @@ -83,7 +84,6 @@ void IndexGraph::GetEdgeList(Segment const & segment, bool isOutgoing, vector const & joints) @@ -122,13 +122,13 @@ RouteWeight IndexGraph::HeuristicCostEstimate(Segment const & from, Segment cons RouteWeight IndexGraph::CalcSegmentWeight(Segment const & segment) { return RouteWeight( - m_estimator->CalcSegmentWeight(segment, m_geometry.GetRoad(segment.GetFeatureId()))); + m_estimator->CalcSegmentWeight(segment, m_geometry->GetRoad(segment.GetFeatureId()))); } void IndexGraph::GetNeighboringEdges(Segment const & from, RoadPoint const & rp, bool isOutgoing, vector & edges) { - RoadGeometry const & road = m_geometry.GetRoad(rp.GetFeatureId()); + RoadGeometry const & road = m_geometry->GetRoad(rp.GetFeatureId()); if (!road.IsValid()) return; @@ -156,7 +156,7 @@ void IndexGraph::GetNeighboringEdge(Segment const & from, Segment const & to, bo // Blocking U-turns on internal feature points. RoadPoint const rp = from.GetRoadPoint(isOutgoing); if (IsUTurn(from, to) && m_roadIndex.GetJointId(rp) == Joint::kInvalidId - && !m_geometry.GetRoad(from.GetFeatureId()).IsEndPointId(rp.GetPointId())) + && !m_geometry->GetRoad(from.GetFeatureId()).IsEndPointId(rp.GetPointId())) { return; } @@ -177,8 +177,8 @@ void IndexGraph::GetNeighboringEdge(Segment const & from, Segment const & to, bo RouteWeight IndexGraph::GetPenalties(Segment const & u, Segment const & v) { - bool const fromPassThroughAllowed = m_geometry.GetRoad(u.GetFeatureId()).IsPassThroughAllowed(); - bool const toPassThroughAllowed = m_geometry.GetRoad(v.GetFeatureId()).IsPassThroughAllowed(); + bool const fromPassThroughAllowed = m_geometry->GetRoad(u.GetFeatureId()).IsPassThroughAllowed(); + bool const toPassThroughAllowed = m_geometry->GetRoad(v.GetFeatureId()).IsPassThroughAllowed(); // Route crosses border of pass-through/non-pass-through area if |u| and |v| have different // pass through restrictions. int32_t const passThroughPenalty = fromPassThroughAllowed == toPassThroughAllowed ? 0 : 1; diff --git a/routing/index_graph.hpp b/routing/index_graph.hpp index 4155c5caa6..77eb46363b 100644 --- a/routing/index_graph.hpp +++ b/routing/index_graph.hpp @@ -29,14 +29,14 @@ public: using Weight = RouteWeight; IndexGraph() = default; - explicit IndexGraph(unique_ptr loader, shared_ptr estimator); + IndexGraph(shared_ptr geometry, shared_ptr estimator); // Put outgoing (or ingoing) egdes for segment to the 'edges' vector. void GetEdgeList(Segment const & segment, bool isOutgoing, vector & edges); Joint::Id GetJointId(RoadPoint const & rp) const { return m_roadIndex.GetJointId(rp); } - Geometry & GetGeometry() { return m_geometry; } + Geometry & GetGeometry() { return *m_geometry; } bool IsRoad(uint32_t featureId) const { return m_roadIndex.IsRoad(featureId); } RoadJointIds const & GetRoad(uint32_t featureId) const { return m_roadIndex.GetRoad(featureId); } @@ -65,8 +65,6 @@ public: m_roadIndex.PushFromSerializer(jointId, rp); } - bool IsBuilt() const { return m_isBuilt; } - template void ForEachRoad(F && f) const { @@ -91,12 +89,11 @@ private: return GetGeometry().GetRoad(segment.GetFeatureId()).GetPoint(segment.GetPointId(front)); } - Geometry m_geometry; + shared_ptr m_geometry; shared_ptr m_estimator; RoadIndex m_roadIndex; JointIndex m_jointIndex; RestrictionVec m_restrictions; RoadAccess m_roadAccess; - bool m_isBuilt = false; }; } // namespace routing diff --git a/routing/index_graph_loader.cpp b/routing/index_graph_loader.cpp index 1563c7270f..c568ea3343 100644 --- a/routing/index_graph_loader.cpp +++ b/routing/index_graph_loader.cpp @@ -28,10 +28,14 @@ public: void Clear() override; private: - /// \brief Constructs IndexGraph without deserializing data and building IndexGraph. - IndexGraph & Init(NumMwmId mwmId); - /// \brief Deserializes data and builds IndexGraph. - IndexGraph & Deserialize(NumMwmId numMwmId, IndexGraph & graph); + struct GeometryIndexGraph + { + shared_ptr m_geometry; + shared_ptr m_indexGraph; + }; + + GeometryIndexGraph & CreateGeometry(NumMwmId numMwmId); + GeometryIndexGraph & CreateIndexGraph(NumMwmId numMwmId, GeometryIndexGraph & graph); VehicleType m_vehicleType; bool m_loadAltitudes; @@ -39,7 +43,7 @@ private: shared_ptr m_numMwmIds; shared_ptr m_vehicleModelFactory; shared_ptr m_estimator; - unordered_map> m_graphs; + unordered_map m_graphs; }; IndexGraphLoaderImpl::IndexGraphLoaderImpl( @@ -62,21 +66,24 @@ Geometry & IndexGraphLoaderImpl::GetGeometry(NumMwmId numMwmId) { auto it = m_graphs.find(numMwmId); if (it != m_graphs.end()) - return it->second->GetGeometry(); + return *it->second.m_geometry; - return Init(numMwmId).GetGeometry(); + return *CreateGeometry(numMwmId).m_geometry; } IndexGraph & IndexGraphLoaderImpl::GetIndexGraph(NumMwmId numMwmId) { auto it = m_graphs.find(numMwmId); if (it != m_graphs.end()) - return it->second->IsBuilt() ? *it->second : Deserialize(numMwmId, *it->second); + { + return it->second.m_indexGraph ? *it->second.m_indexGraph + : *CreateIndexGraph(numMwmId, it->second).m_indexGraph; + } - return Deserialize(numMwmId, Init(numMwmId)); + return *CreateIndexGraph(numMwmId, CreateGeometry(numMwmId)).m_indexGraph; } -IndexGraph & IndexGraphLoaderImpl::Init(NumMwmId numMwmId) +IndexGraphLoaderImpl::GeometryIndexGraph & IndexGraphLoaderImpl::CreateGeometry(NumMwmId numMwmId) { platform::CountryFile const & file = m_numMwmIds->GetFile(numMwmId); MwmSet::MwmHandle handle = m_index.GetMwmHandleByCountryFile(file); @@ -86,24 +93,27 @@ IndexGraph & IndexGraphLoaderImpl::Init(NumMwmId numMwmId) shared_ptr vehicleModel = m_vehicleModelFactory->GetVehicleModelForCountry(file.GetName()); - return *( - m_graphs[numMwmId] = make_unique( - GeometryLoader::Create(m_index, handle, vehicleModel, m_loadAltitudes), m_estimator)); + auto & graph = m_graphs[numMwmId]; + graph.m_geometry = + make_shared(GeometryLoader::Create(m_index, handle, vehicleModel, m_loadAltitudes)); + return graph; } -IndexGraph & IndexGraphLoaderImpl::Deserialize(NumMwmId numMwmId, IndexGraph & graph) +IndexGraphLoaderImpl::GeometryIndexGraph & IndexGraphLoaderImpl::CreateIndexGraph( + NumMwmId numMwmId, GeometryIndexGraph & graph) { - CHECK(!graph.IsBuilt(), ()); + CHECK(graph.m_geometry, ()); platform::CountryFile const & file = m_numMwmIds->GetFile(numMwmId); MwmSet::MwmHandle handle = m_index.GetMwmHandleByCountryFile(file); if (!handle.IsAlive()) MYTHROW(RoutingException, ("Can't get mwm handle for", file)); + graph.m_indexGraph = make_shared(graph.m_geometry, m_estimator); my::Timer timer; MwmValue const & mwmValue = *handle.GetValue(); - DeserializeIndexGraph(mwmValue, m_vehicleType, graph); + DeserializeIndexGraph(mwmValue, m_vehicleType, *graph.m_indexGraph); LOG(LINFO, (ROUTING_FILE_TAG, "section for", file.GetName(), "loaded in", timer.ElapsedSeconds(), - "seconds")); + "seconds")); return graph; } diff --git a/routing/routing_tests/index_graph_test.cpp b/routing/routing_tests/index_graph_test.cpp index 584cd35035..2e28334904 100644 --- a/routing/routing_tests/index_graph_test.cpp +++ b/routing/routing_tests/index_graph_test.cpp @@ -131,7 +131,7 @@ UNIT_TEST(EdgesTest) RoadGeometry::Points({{3.0, -1.0}, {3.0, 0.0}, {3.0, 1.0}})); traffic::TrafficCache const trafficCache; - IndexGraph graph(move(loader), CreateEstimatorForCar(trafficCache)); + IndexGraph graph(make_shared(move(loader)), CreateEstimatorForCar(trafficCache)); vector joints; joints.emplace_back(MakeJoint({{0, 1}, {3, 1}})); // J0 diff --git a/routing/routing_tests/index_graph_tools.cpp b/routing/routing_tests/index_graph_tools.cpp index f88dc70c54..975dddeac5 100644 --- a/routing/routing_tests/index_graph_tools.cpp +++ b/routing/routing_tests/index_graph_tools.cpp @@ -2,6 +2,8 @@ #include "testing/testing.hpp" +#include "routing/geometry.hpp" + #include "routing/base/routing_result.hpp" #include "routing_common/car_model.hpp" @@ -311,7 +313,7 @@ unique_ptr BuildWorldGraph(unique_ptr estimator, vector const & joints) { - auto graph = make_unique(move(geometryLoader), estimator); + auto graph = make_unique(make_shared(move(geometryLoader)), estimator); graph->Import(joints); auto indexLoader = make_unique(); indexLoader->AddGraph(kTestNumMwmId, move(graph)); @@ -323,7 +325,7 @@ unique_ptr BuildWorldGraph(unique_ptr estimator, vector const & joints) { - auto graph = make_unique(move(geometryLoader), estimator); + auto graph = make_unique(make_shared(move(geometryLoader)), estimator); graph->Import(joints); auto indexLoader = make_unique(); indexLoader->AddGraph(kTestNumMwmId, move(graph)); @@ -336,7 +338,7 @@ unique_ptr BuildWorldGraph(unique_ptr geo vector const & joints, transit::GraphData const & transitData) { - auto indexGraph = make_unique(move(geometryLoader), estimator); + auto indexGraph = make_unique(make_shared(move(geometryLoader)), estimator); indexGraph->Import(joints); auto transitGraph = make_unique(kTestNumMwmId, estimator); -- cgit v1.2.3