diff --git a/include/engine/api/route_api.hpp b/include/engine/api/route_api.hpp index 9210a36f94b..869d4302a36 100644 --- a/include/engine/api/route_api.hpp +++ b/include/engine/api/route_api.hpp @@ -71,7 +71,7 @@ class RouteAPI : public BaseAPI } util::json::Object MakeRoute(const std::vector &segment_end_coordinates, - std::vector> unpacked_path_segments, + const std::vector> &unpacked_path_segments, const std::vector &source_traversed_in_reverse, const std::vector &target_traversed_in_reverse) const { @@ -81,7 +81,6 @@ class RouteAPI : public BaseAPI legs.reserve(number_of_legs); leg_geometries.reserve(number_of_legs); - unpacked_path_segments = guidance::postProcess(std::move(unpacked_path_segments)); for (auto idx : util::irange(0UL, number_of_legs)) { const auto &phantoms = segment_end_coordinates[idx]; @@ -97,14 +96,42 @@ class RouteAPI : public BaseAPI if (parameters.steps) { - leg.steps = guidance::assembleSteps( + auto steps = guidance::assembleSteps( BaseAPI::facade, path_data, leg_geometry, phantoms.source_phantom, phantoms.target_phantom, reversed_source, reversed_target); + + /* Perform step-based post-processing. + * + * Using post-processing on basis of route-steps for a single leg at a time + * comes at the cost that we cannot count the correct exit for roundabouts. + * We can only emit the exit nr/intersections up to/starting at a part of the leg. + * If a roundabout is not terminated in a leg, we will end up with a enter-roundabout + * and exit-roundabout-nr where the exit nr is out of sync with the previous enter. + * + * | S | + * * * + * ----* * ---- + * T + * ----* * ---- + * V * * + * | | + * | | + * + * Coming from S via V to T, we end up with the legs S->V and V->T. V-T will say to take + * the second exit, even though counting from S it would be the third. + * For S, we only emit `roundabout` without an exit number, showing that we enter a roundabout + * to find a via point. + * The same exit will be emitted, though, if we should start routing at S, making + * the overall response consistent. + */ + + leg.steps = guidance::postProcess(std::move(steps)); } leg_geometries.push_back(std::move(leg_geometry)); legs.push_back(std::move(leg)); } + auto route = guidance::assembleRoute(legs); boost::optional json_overview; if (parameters.overview != RouteParameters::OverviewType::False) diff --git a/include/engine/guidance/assemble_geometry.hpp b/include/engine/guidance/assemble_geometry.hpp index 769885e12bb..13a74fe12ca 100644 --- a/include/engine/guidance/assemble_geometry.hpp +++ b/include/engine/guidance/assemble_geometry.hpp @@ -51,7 +51,7 @@ LegGeometry assembleGeometry(const DataFacadeT &facade, current_distance += util::coordinate_calculation::haversineDistance(prev_coordinate, coordinate); - if (!isSilent(path_point.turn_instruction)) + if (path_point.turn_instruction != extractor::guidance::TurnInstruction::NO_TURN()) { geometry.segment_distances.push_back(current_distance); geometry.segment_offsets.push_back(geometry.locations.size()); diff --git a/include/engine/guidance/post_processing.hpp b/include/engine/guidance/post_processing.hpp index 9714f1db39d..dc17332d5ea 100644 --- a/include/engine/guidance/post_processing.hpp +++ b/include/engine/guidance/post_processing.hpp @@ -1,7 +1,7 @@ #ifndef ENGINE_GUIDANCE_POST_PROCESSING_HPP #define ENGINE_GUIDANCE_POST_PROCESSING_HPP -#include "engine/internal_route_result.hpp" +#include "engine/guidance/route_step.hpp" #include @@ -12,7 +12,8 @@ namespace engine namespace guidance { -std::vector> postProcess(std::vector> path_data); +//passed as none-reference to modify in-place and move out again +std::vector postProcess(std::vector steps); } // namespace guidance } // namespace engine diff --git a/src/engine/guidance/assemble_steps.cpp b/src/engine/guidance/assemble_steps.cpp index 41c65de816e..e207efdfcf7 100644 --- a/src/engine/guidance/assemble_steps.cpp +++ b/src/engine/guidance/assemble_steps.cpp @@ -19,8 +19,9 @@ StepManeuver stepManeuverFromGeometry(extractor::guidance::TurnInstruction instr const unsigned exit) { auto turn_index = leg_geometry.BackIndex(segment_index); + std::cout << "Turn Index: " << turn_index << " of: " << leg_geometry.locations.size() << std::endl; BOOST_ASSERT(turn_index > 0); - BOOST_ASSERT(turn_index < leg_geometry.locations.size()); + BOOST_ASSERT(turn_index+1 < leg_geometry.locations.size()); // TODO chose a bigger look-a-head to smooth complex geometry const auto pre_turn_coordinate = leg_geometry.locations[turn_index - 1]; diff --git a/src/engine/guidance/post_processing.cpp b/src/engine/guidance/post_processing.cpp index 402666f9f5a..e88d39b107a 100644 --- a/src/engine/guidance/post_processing.cpp +++ b/src/engine/guidance/post_processing.cpp @@ -20,45 +20,45 @@ namespace guidance namespace detail { -bool canMergeTrivially(const PathData &destination, const PathData &source) +bool canMergeTrivially(const RouteStep &destination, const RouteStep &source) { - return destination.exit == 0 && destination.name_id == source.name_id && - destination.travel_mode == source.travel_mode && isSilent(source.turn_instruction); + return destination.maneuver.exit == 0 && destination.name_id == source.name_id && + isSilent(source.maneuver.instruction); } -PathData forwardInto(PathData destination, const PathData &source) +RouteStep forwardInto(RouteStep destination, const RouteStep &source) { // Merge a turn into a silent turn // Overwrites turn instruction and increases exit NR - destination.exit = source.exit; + destination.maneuver.exit = source.maneuver.exit; return destination; } -PathData accumulateInto(PathData destination, const PathData &source) +RouteStep accumulateInto(RouteStep destination, const RouteStep &source) { // Merge a turn into a silent turn // Overwrites turn instruction and increases exit NR BOOST_ASSERT(canMergeTrivially(destination, source)); - destination.exit = source.exit + 1; + destination.maneuver.exit = source.maneuver.exit + 1; return destination; } -PathData mergeInto(PathData destination, const PathData &source) +RouteStep mergeInto(RouteStep destination, const RouteStep &source) { - if (source.turn_instruction == TurnInstruction::NO_TURN()) + if (source.maneuver.instruction == TurnInstruction::NO_TURN()) { BOOST_ASSERT(canMergeTrivially(destination, source)); return detail::forwardInto(destination, source); } - if (source.turn_instruction.type == TurnType::Suppressed) + if (source.maneuver.instruction.type == TurnType::Suppressed) { return detail::forwardInto(destination, source); } - if (source.turn_instruction.type == TurnType::StayOnRoundabout) + if (source.maneuver.instruction.type == TurnType::StayOnRoundabout) { return detail::forwardInto(destination, source); } - if (entersRoundabout(source.turn_instruction)) + if (entersRoundabout(source.maneuver.instruction)) { return detail::forwardInto(destination, source); } @@ -67,96 +67,90 @@ PathData mergeInto(PathData destination, const PathData &source) } // namespace detail -void print(const std::vector> &leg_data) +void print(const std::vector &steps) { std::cout << "Path\n"; - int legnr = 0; - for (const auto &leg : leg_data) + int segment = 0; + for (const auto &step : steps) { - std::cout << "\tLeg: " << ++legnr << "\n"; - int segment = 0; - for (const auto &data : leg) - { - const auto type = static_cast(data.turn_instruction.type); - const auto modifier = static_cast(data.turn_instruction.direction_modifier); + const auto type = static_cast(step.maneuver.instruction.type); + const auto modifier = static_cast(step.maneuver.instruction.direction_modifier); - std::cout << "\t\t[" << ++segment << "]: " << type << " " << modifier - << " exit: " << data.exit << "\n"; - } + std::cout << "\t[" << ++segment << "]: " << type << " " << modifier << " name[" + << step.name_id << "]: " << step.name << " Duration: " << step.duration + << " Distance: " << step.distance << " Geometry" << step.geometry_begin << " " + << step.geometry_end << " exit: " << step.maneuver.exit << "\n"; } - std::cout << std::endl; } -std::vector> postProcess(std::vector> leg_data) +std::vector postProcess(std::vector steps) { - if (leg_data.empty()) - return leg_data; + // the steps should always include the first/last step in form of a location + BOOST_ASSERT(steps.size() >= 2); + if (steps.size() == 2) + return steps; -#define PRINT_DEBUG 0 +#define PRINT_DEBUG 1 unsigned carry_exit = 0; #if PRINT_DEBUG std::cout << "[POSTPROCESSING ITERATION]" << std::endl; std::cout << "Input\n"; - print(leg_data); + print(steps); #endif // Count Street Exits forward bool on_roundabout = false; - for (auto &path_data : leg_data) - { - if (not path_data.empty()) - path_data[0].exit = carry_exit; - for (std::size_t data_index = 0; data_index + 1 < path_data.size(); ++data_index) + for (std::size_t data_index = 0; data_index + 1 < steps.size(); ++data_index) + { + if (entersRoundabout(steps[data_index].maneuver.instruction)) { - if (entersRoundabout(path_data[data_index].turn_instruction)) - { - path_data[data_index].exit += 1; - on_roundabout = true; - } + steps[data_index].maneuver.exit += 1; + on_roundabout = true; + } - if (isSilent(path_data[data_index].turn_instruction) && - path_data[data_index].turn_instruction != TurnInstruction::NO_TURN()) - { - path_data[data_index].exit += 1; - } - if (leavesRoundabout(path_data[data_index].turn_instruction)) - { - if (!on_roundabout) - { - BOOST_ASSERT(leg_data[0][0].turn_instruction.type == - TurnInstruction::NO_TURN()); - if (path_data[data_index].turn_instruction.type == TurnType::ExitRoundabout) - leg_data[0][0].turn_instruction.type = TurnType::EnterRoundabout; - if (path_data[data_index].turn_instruction.type == TurnType::ExitRotary) - leg_data[0][0].turn_instruction.type = TurnType::EnterRotary; - path_data[data_index].exit += 1; - } - on_roundabout = false; - } - if (path_data[data_index].turn_instruction.type == TurnType::EnterRoundaboutAtExit) - { - path_data[data_index].exit += 1; - path_data[data_index].turn_instruction.type = TurnType::EnterRoundabout; - } - else if (path_data[data_index].turn_instruction.type == TurnType::EnterRotaryAtExit) + if (isSilent(steps[data_index].maneuver.instruction) && + steps[data_index].maneuver.instruction != TurnInstruction::NO_TURN()) + { + steps[data_index].maneuver.exit += 1; + } + if (leavesRoundabout(steps[data_index].maneuver.instruction)) + { + if (!on_roundabout) { - path_data[data_index].exit += 1; - path_data[data_index].turn_instruction.type = TurnType::EnterRotary; + //the initial instruction needs to be an enter roundabout, if its the first one + BOOST_ASSERT(steps[0].maneuver.instruction.type == TurnInstruction::NO_TURN()); + if (steps[data_index].maneuver.instruction.type == TurnType::ExitRoundabout) + steps[0].maneuver.instruction.type = TurnType::EnterRoundabout; + if (steps[data_index].maneuver.instruction.type == TurnType::ExitRotary) + steps[0].maneuver.instruction.type = TurnType::EnterRotary; + steps[data_index].maneuver.exit += 1; } + on_roundabout = false; + } + if (steps[data_index].maneuver.instruction.type == TurnType::EnterRoundaboutAtExit) + { + steps[data_index].maneuver.exit += 1; + steps[data_index].maneuver.instruction.type = TurnType::EnterRoundabout; + } + else if (steps[data_index].maneuver.instruction.type == TurnType::EnterRotaryAtExit) + { + steps[data_index].maneuver.exit += 1; + steps[data_index].maneuver.instruction.type = TurnType::EnterRotary; + } - if (isSilent(path_data[data_index].turn_instruction) || - entersRoundabout(path_data[data_index].turn_instruction)) - { - path_data[data_index + 1] = - detail::mergeInto(path_data[data_index + 1], path_data[data_index]); - } - carry_exit = path_data[data_index].exit; + if (isSilent(steps[data_index].maneuver.instruction) || + entersRoundabout(steps[data_index].maneuver.instruction)) + { + steps[data_index + 1] = + detail::mergeInto(steps[data_index + 1], steps[data_index]); } + carry_exit = steps[data_index].maneuver.exit; } #if PRINT_DEBUG std::cout << "Merged\n"; - print(leg_data); + print(steps); #endif +#if 0 on_roundabout = false; // Move Roundabout exit numbers to front for (auto rev_itr = leg_data.rbegin(); rev_itr != leg_data.rend(); ++rev_itr) @@ -164,9 +158,9 @@ std::vector> postProcess(std::vector auto &path_data = *rev_itr; for (std::size_t data_index = path_data.size(); data_index > 1; --data_index) { - if (entersRoundabout(path_data[data_index - 1].turn_instruction)) + if (entersRoundabout(path_data[data_index - 1].maneuver.instruction)) { - if (!on_roundabout && !leavesRoundabout(path_data[data_index - 1].turn_instruction)) + if (!on_roundabout && !leavesRoundabout(path_data[data_index - 1].maneuver.instruction)) path_data[data_index - 1].exit = 0; on_roundabout = false; } @@ -174,8 +168,8 @@ std::vector> postProcess(std::vector { path_data[data_index - 2].exit = path_data[data_index - 1].exit; } - if (leavesRoundabout(path_data[data_index - 1].turn_instruction) && - !entersRoundabout(path_data[data_index - 1].turn_instruction)) + if (leavesRoundabout(path_data[data_index - 1].maneuver.instruction) && + !entersRoundabout(path_data[data_index - 1].maneuver.instruction)) { path_data[data_index - 2].exit = path_data[data_index - 1].exit; on_roundabout = true; @@ -188,26 +182,10 @@ std::vector> postProcess(std::vector prev_leg->back().exit = path_data[0].exit; } } - -#if PRINT_DEBUG - std::cout << "Move To Front\n"; - print(leg_data); #endif - // silence silent turns for good - for (auto &path_data : leg_data) - { - for (auto &data : path_data) - { - if (isSilent(data.turn_instruction) || (leavesRoundabout(data.turn_instruction) && - !entersRoundabout(data.turn_instruction))) - { - data.turn_instruction = TurnInstruction::NO_TURN(); - data.exit = 0; - } - } - } - return leg_data; + //TODO remove silent turns + return steps; } } // namespace guidance