From 40d02978858502ed5f08fa7aa69bb9afc72b2515 Mon Sep 17 00:00:00 2001 From: Michael Krasnyk Date: Wed, 3 May 2017 14:03:19 +0200 Subject: [PATCH] Added unpacked_nodes vector to annotatePath interface --- .../routing_algorithms/routing_base.hpp | 36 ++--- .../routing_algorithms/routing_base_ch.hpp | 23 ++-- .../routing_algorithms/routing_base_mld.hpp | 124 +++++++++--------- .../direct_shortest_path.cpp | 64 ++++----- 4 files changed, 126 insertions(+), 121 deletions(-) diff --git a/include/engine/routing_algorithms/routing_base.hpp b/include/engine/routing_algorithms/routing_base.hpp index 515560db6..23cf32bff 100644 --- a/include/engine/routing_algorithms/routing_base.hpp +++ b/include/engine/routing_algorithms/routing_base.hpp @@ -93,38 +93,38 @@ void insertNodesInHeaps(Heap &forward_heap, Heap &reverse_heap, const PhantomNod template void annotatePath(const FacadeT &facade, - const NodeID source_node, - const NodeID target_node, - const std::vector &unpacked_edges, const PhantomNodes &phantom_node_pair, + const std::vector &unpacked_nodes, + const std::vector &unpacked_edges, std::vector &unpacked_path) { - BOOST_ASSERT(source_node != SPECIAL_NODEID && target_node != SPECIAL_NODEID); - BOOST_ASSERT(!unpacked_edges.empty() || source_node == target_node); + BOOST_ASSERT(!unpacked_nodes.empty()); + BOOST_ASSERT(unpacked_nodes.size() == unpacked_edges.size() + 1); const bool start_traversed_in_reverse = - phantom_node_pair.source_phantom.forward_segment_id.id != source_node; + phantom_node_pair.source_phantom.forward_segment_id.id != unpacked_nodes.front(); const bool target_traversed_in_reverse = - phantom_node_pair.target_phantom.forward_segment_id.id != target_node; + phantom_node_pair.target_phantom.forward_segment_id.id != unpacked_nodes.back(); - BOOST_ASSERT(phantom_node_pair.source_phantom.forward_segment_id.id == source_node || - phantom_node_pair.source_phantom.reverse_segment_id.id == source_node); - BOOST_ASSERT(phantom_node_pair.target_phantom.forward_segment_id.id == target_node || - phantom_node_pair.target_phantom.reverse_segment_id.id == target_node); + BOOST_ASSERT(phantom_node_pair.source_phantom.forward_segment_id.id == unpacked_nodes.front() || + phantom_node_pair.source_phantom.reverse_segment_id.id == unpacked_nodes.front()); + BOOST_ASSERT(phantom_node_pair.target_phantom.forward_segment_id.id == unpacked_nodes.back() || + phantom_node_pair.target_phantom.reverse_segment_id.id == unpacked_nodes.back()); - for (auto edge_id : unpacked_edges) + auto node_from = unpacked_nodes.begin(), node_last = std::prev(unpacked_nodes.end()); + for (auto edge = unpacked_edges.begin(); node_from != node_last; ++node_from, ++edge) { - const auto &edge_data = facade.GetEdgeData(edge_id); - const auto turn_id = edge_data.turn_id; // edge-based edge ID - const auto ebg_node_id = facade.GetEdgeBasedNodeID(turn_id); // edge-based source node ID - const auto name_index = facade.GetNameIndex(ebg_node_id); + const auto &edge_data = facade.GetEdgeData(*edge); + const auto turn_id = edge_data.turn_id; // edge-based graph edge index + const auto node_id = *node_from; // edge-based graph node index + const auto name_index = facade.GetNameIndex(node_id); const auto turn_instruction = facade.GetTurnInstructionForEdgeID(turn_id); const extractor::TravelMode travel_mode = (unpacked_path.empty() && start_traversed_in_reverse) ? phantom_node_pair.source_phantom.backward_travel_mode - : facade.GetTravelMode(ebg_node_id); + : facade.GetTravelMode(node_id); - const auto geometry_index = facade.GetGeometryIndex(ebg_node_id); + const auto geometry_index = facade.GetGeometryIndex(node_id); std::vector id_vector; std::vector weight_vector; diff --git a/include/engine/routing_algorithms/routing_base_ch.hpp b/include/engine/routing_algorithms/routing_base_ch.hpp index 21583acda..0ac059b84 100644 --- a/include/engine/routing_algorithms/routing_base_ch.hpp +++ b/include/engine/routing_algorithms/routing_base_ch.hpp @@ -299,22 +299,25 @@ void unpackPath(const FacadeT &facade, const auto nodes_number = std::distance(packed_path_begin, packed_path_end); BOOST_ASSERT(nodes_number > 0); + std::vector unpacked_nodes; std::vector unpacked_edges; + unpacked_nodes.reserve(nodes_number); + unpacked_edges.reserve(nodes_number); - auto source_node = *packed_path_begin, target_node = *packed_path_begin; + unpacked_nodes.push_back(*packed_path_begin); if (nodes_number > 1) { - target_node = *std::prev(packed_path_end); - unpacked_edges.reserve(std::distance(packed_path_begin, packed_path_end)); - unpackPath( - facade, - packed_path_begin, - packed_path_end, - [&facade, &unpacked_edges](std::pair & /* edge */, - const auto &edge_id) { unpacked_edges.push_back(edge_id); }); + unpackPath(facade, + packed_path_begin, + packed_path_end, + [&](std::pair &edge, const auto &edge_id) { + BOOST_ASSERT(edge.first == unpacked_nodes.back()); + unpacked_nodes.push_back(edge.second); + unpacked_edges.push_back(edge_id); + }); } - annotatePath(facade, source_node, target_node, unpacked_edges, phantom_nodes, unpacked_path); + annotatePath(facade, phantom_nodes, unpacked_nodes, unpacked_edges, unpacked_path); } /** diff --git a/include/engine/routing_algorithms/routing_base_mld.hpp b/include/engine/routing_algorithms/routing_base_mld.hpp index 852d8dfd0..b0fe2230c 100644 --- a/include/engine/routing_algorithms/routing_base_mld.hpp +++ b/include/engine/routing_algorithms/routing_base_mld.hpp @@ -180,7 +180,7 @@ void routingStep(const datafacade::ContiguousInternalMemoryDataFacade } template -std::tuple> +std::tuple, std::vector> search(SearchEngineData &engine_working_data, const datafacade::ContiguousInternalMemoryDataFacade &facade, SearchEngineData::QueryHeap &forward_heap, @@ -235,8 +235,7 @@ search(SearchEngineData &engine_working_data, // No path found for both target nodes? if (weight >= weight_upper_bound || SPECIAL_NODEID == middle) { - return std::make_tuple( - INVALID_EDGE_WEIGHT, SPECIAL_NODEID, SPECIAL_NODEID, std::vector()); + return std::make_tuple(INVALID_EDGE_WEIGHT, std::vector(), std::vector()); } // Get packed path as edges {from node ID, to node ID, edge ID} @@ -260,11 +259,14 @@ search(SearchEngineData &engine_working_data, current_node = parent_node; parent_node = reverse_heap.GetData(parent_node).parent; } - const NodeID target_node = current_node; // Unpack path - std::vector unpacked_path; - unpacked_path.reserve(packed_path.size()); + std::vector unpacked_nodes; + std::vector unpacked_edges; + unpacked_nodes.reserve(packed_path.size()); + unpacked_edges.reserve(packed_path.size()); + + unpacked_nodes.push_back(source_node); for (auto const &packed_edge : packed_path) { NodeID source, target; @@ -272,7 +274,8 @@ search(SearchEngineData &engine_working_data, std::tie(source, target, overlay_edge) = packed_edge; if (!overlay_edge) { // a base graph edge - unpacked_path.push_back(facade.FindEdge(source, target)); + unpacked_nodes.push_back(target); + unpacked_edges.push_back(facade.FindEdge(source, target)); } else { // an overlay graph edge @@ -291,26 +294,28 @@ search(SearchEngineData &engine_working_data, // TODO: when structured bindings will be allowed change to // auto [subpath_weight, subpath_source, subpath_target, subpath] = ... EdgeWeight subpath_weight; - NodeID subpath_source, subpath_target; - std::vector subpath; - std::tie(subpath_weight, subpath_source, subpath_target, subpath) = - search(engine_working_data, - facade, - forward_heap, - reverse_heap, - force_loop_forward, - force_loop_reverse, - INVALID_EDGE_WEIGHT, - sublevel, - parent_cell_id); - BOOST_ASSERT(!subpath.empty()); - BOOST_ASSERT(subpath_source == source); - BOOST_ASSERT(subpath_target == target); - unpacked_path.insert(unpacked_path.end(), subpath.begin(), subpath.end()); + std::vector subpath_nodes; + std::vector subpath_edges; + std::tie(subpath_weight, subpath_nodes, subpath_edges) = search(engine_working_data, + facade, + forward_heap, + reverse_heap, + force_loop_forward, + force_loop_reverse, + INVALID_EDGE_WEIGHT, + sublevel, + parent_cell_id); + BOOST_ASSERT(!subpath_edges.empty()); + BOOST_ASSERT(subpath_nodes.size() > 1); + BOOST_ASSERT(subpath_nodes.front() == source); + BOOST_ASSERT(subpath_nodes.back() == target); + unpacked_nodes.insert( + unpacked_nodes.end(), std::next(subpath_nodes.begin()), subpath_nodes.end()); + unpacked_edges.insert(unpacked_edges.end(), subpath_edges.begin(), subpath_edges.end()); } } - return std::make_tuple(weight, source_node, target_node, std::move(unpacked_path)); + return std::make_tuple(weight, std::move(unpacked_nodes), std::move(unpacked_edges)); } // TODO reorder parameters @@ -326,27 +331,18 @@ inline void search(SearchEngineData &engine_working_data, const PhantomNodes &phantom_nodes, const EdgeWeight weight_upper_bound = INVALID_EDGE_WEIGHT) { - NodeID source_node, target_node; - std::vector unpacked_edges; - std::tie(weight, source_node, target_node, unpacked_edges) = mld::search(engine_working_data, - facade, - forward_heap, - reverse_heap, - force_loop_forward, - force_loop_reverse, - weight_upper_bound, - phantom_nodes); - - if (weight != INVALID_EDGE_WEIGHT) - { - packed_leg.push_back(source_node); - std::transform(unpacked_edges.begin(), - unpacked_edges.end(), - std::back_inserter(packed_leg), - [&facade](const auto edge) { return facade.GetTarget(edge); }); - } + // TODO: change search calling interface to use unpacked_edges result + std::tie(weight, packed_leg, std::ignore) = mld::search(engine_working_data, + facade, + forward_heap, + reverse_heap, + force_loop_forward, + force_loop_reverse, + weight_upper_bound, + phantom_nodes); } +// TODO: remove CH-related stub template void unpackPath(const FacadeT &facade, RandomIter packed_path_begin, @@ -357,21 +353,24 @@ void unpackPath(const FacadeT &facade, const auto nodes_number = std::distance(packed_path_begin, packed_path_end); BOOST_ASSERT(nodes_number > 0); + std::vector unpacked_nodes; std::vector unpacked_edges; + unpacked_nodes.reserve(nodes_number); + unpacked_edges.reserve(nodes_number); - auto source_node = *packed_path_begin, target_node = *packed_path_begin; - + unpacked_nodes.push_back(*packed_path_begin); if (nodes_number > 1) { - target_node = *std::prev(packed_path_end); - util::for_each_pair(packed_path_begin, - packed_path_end, - [&facade, &unpacked_edges](const auto from, const auto to) { - unpacked_edges.push_back(facade.FindEdge(from, to)); - }); + util::for_each_pair( + packed_path_begin, + packed_path_end, + [&facade, &unpacked_nodes, &unpacked_edges](const auto from, const auto to) { + unpacked_nodes.push_back(to); + unpacked_edges.push_back(facade.FindEdge(from, to)); + }); } - annotatePath(facade, source_node, target_node, unpacked_edges, phantom_nodes, unpacked_path); + annotatePath(facade, phantom_nodes, unpacked_nodes, unpacked_edges, unpacked_path); } inline double @@ -390,22 +389,23 @@ getNetworkDistance(SearchEngineData &engine_working_data, insertNodesInHeaps(forward_heap, reverse_heap, phantom_nodes); EdgeWeight weight; - NodeID source_node, target_node; + std::vector unpacked_nodes; std::vector unpacked_edges; - std::tie(weight, source_node, target_node, unpacked_edges) = search(engine_working_data, - facade, - forward_heap, - reverse_heap, - DO_NOT_FORCE_LOOPS, - DO_NOT_FORCE_LOOPS, - weight_upper_bound, - phantom_nodes); + std::tie(weight, unpacked_nodes, unpacked_edges) = search(engine_working_data, + facade, + forward_heap, + reverse_heap, + DO_NOT_FORCE_LOOPS, + DO_NOT_FORCE_LOOPS, + weight_upper_bound, + phantom_nodes); if (weight == INVALID_EDGE_WEIGHT) return std::numeric_limits::max(); std::vector unpacked_path; - annotatePath(facade, source_node, target_node, unpacked_edges, phantom_nodes, unpacked_path); + + annotatePath(facade, phantom_nodes, unpacked_nodes, unpacked_edges, unpacked_path); return getPathDistance(facade, unpacked_path, source_phantom, target_phantom); } diff --git a/src/engine/routing_algorithms/direct_shortest_path.cpp b/src/engine/routing_algorithms/direct_shortest_path.cpp index 588ad7d2f..9a2047e77 100644 --- a/src/engine/routing_algorithms/direct_shortest_path.cpp +++ b/src/engine/routing_algorithms/direct_shortest_path.cpp @@ -15,13 +15,12 @@ template InternalRouteResult extractRoute(const datafacade::ContiguousInternalMemoryDataFacade &facade, const EdgeWeight weight, - const NodeID source_node, - const NodeID target_node, - const std::vector &edges, - const PhantomNodes &nodes) + const PhantomNodes &phantom_nodes, + const std::vector &unpacked_nodes, + const std::vector &unpacked_edges) { InternalRouteResult raw_route_data; - raw_route_data.segment_end_coordinates = {nodes}; + raw_route_data.segment_end_coordinates = {phantom_nodes}; // No path found for both target nodes? if (INVALID_EDGE_WEIGHT == weight) { @@ -33,15 +32,14 @@ extractRoute(const datafacade::ContiguousInternalMemoryDataFacade &f raw_route_data.shortest_path_length = weight; raw_route_data.unpacked_path_segments.resize(1); raw_route_data.source_traversed_in_reverse.push_back( - (source_node != nodes.source_phantom.forward_segment_id.id)); + (unpacked_nodes.front() != phantom_nodes.source_phantom.forward_segment_id.id)); raw_route_data.target_traversed_in_reverse.push_back( - (target_node != nodes.target_phantom.forward_segment_id.id)); + (unpacked_nodes.back() != phantom_nodes.target_phantom.forward_segment_id.id)); annotatePath(facade, - source_node, - target_node, - edges, - nodes, + phantom_nodes, + unpacked_nodes, + unpacked_edges, raw_route_data.unpacked_path_segments.front()); return raw_route_data; @@ -81,22 +79,26 @@ InternalRouteResult directShortestPathSearchImpl( DO_NOT_FORCE_LOOPS, phantom_nodes); + std::vector unpacked_nodes; std::vector unpacked_edges; - auto source_node = SPECIAL_NODEID, target_node = SPECIAL_NODEID; + if (!packed_leg.empty()) { - source_node = packed_leg.front(); - target_node = packed_leg.back(); + unpacked_nodes.reserve(packed_leg.size()); unpacked_edges.reserve(packed_leg.size()); - ch::unpackPath( - facade, - packed_leg.begin(), - packed_leg.end(), - [&facade, &unpacked_edges](std::pair & /* edge */, - const auto &edge_id) { unpacked_edges.push_back(edge_id); }); + unpacked_nodes.push_back(packed_leg.front()); + ch::unpackPath(facade, + packed_leg.begin(), + packed_leg.end(), + [&unpacked_nodes, &unpacked_edges](std::pair &edge, + const auto &edge_id) { + BOOST_ASSERT(edge.first == unpacked_nodes.back()); + unpacked_nodes.push_back(edge.second); + unpacked_edges.push_back(edge_id); + }); } - return extractRoute(facade, weight, source_node, target_node, unpacked_edges, phantom_nodes); + return extractRoute(facade, weight, phantom_nodes, unpacked_nodes, unpacked_edges); } } // namespace ch @@ -130,18 +132,18 @@ InternalRouteResult directShortestPathSearch( // TODO: when structured bindings will be allowed change to // auto [weight, source_node, target_node, unpacked_edges] = ... EdgeWeight weight; - NodeID source_node, target_node; + std::vector unpacked_nodes; std::vector unpacked_edges; - std::tie(weight, source_node, target_node, unpacked_edges) = mld::search(engine_working_data, - facade, - forward_heap, - reverse_heap, - DO_NOT_FORCE_LOOPS, - DO_NOT_FORCE_LOOPS, - INVALID_EDGE_WEIGHT, - phantom_nodes); + std::tie(weight, unpacked_nodes, unpacked_edges) = mld::search(engine_working_data, + facade, + forward_heap, + reverse_heap, + DO_NOT_FORCE_LOOPS, + DO_NOT_FORCE_LOOPS, + INVALID_EDGE_WEIGHT, + phantom_nodes); - return extractRoute(facade, weight, source_node, target_node, unpacked_edges, phantom_nodes); + return extractRoute(facade, weight, phantom_nodes, unpacked_nodes, unpacked_edges); } } // namespace routing_algorithms