Skip to content

Commit

Permalink
refactor, adjusted for comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Kobitzsch committed Mar 22, 2016
1 parent f369fb4 commit be4ad82
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 109 deletions.
4 changes: 2 additions & 2 deletions include/engine/guidance/assemble_steps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ std::vector<RouteStep> assembleSteps(const DataFacadeT &facade,
const bool source_traversed_in_reverse,
const bool target_traversed_in_reverse)
{
const double constexpr ZERO_DURACTION = 0., ZERO_DISTANCE = 0.;
const double constexpr ZERO_DURATION = 0., ZERO_DISTANCE = 0.;
const EdgeWeight source_duration =
source_traversed_in_reverse ? source_node.reverse_weight : source_node.forward_weight;
const auto source_mode = source_traversed_in_reverse ? source_node.backward_travel_mode
Expand Down Expand Up @@ -167,7 +167,7 @@ std::vector<RouteStep> assembleSteps(const DataFacadeT &facade,
WaypointType::Arrive, leg_geometry);
steps.push_back(RouteStep{target_node.name_id,
facade.GetNameForID(target_node.name_id),
ZERO_DURACTION,
ZERO_DURATION,
ZERO_DISTANCE,
target_mode,
final_maneuver,
Expand Down
8 changes: 4 additions & 4 deletions src/engine/guidance/assemble_steps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ StepManeuver stepManeuverFromGeometry(extractor::guidance::TurnInstruction instr
util::coordinate_calculation::bearing(pre_turn_coordinate, turn_coordinate);
}
return StepManeuver{
turn_coordinate,
std::move(turn_coordinate),
pre_turn_bearing,
post_turn_bearing,
instruction,
std::move(instruction),
waypoint_type,
INVALID_EXIT_NR,
{} // no intermediate intersections
Expand All @@ -67,10 +67,10 @@ StepManeuver stepManeuverFromGeometry(extractor::guidance::TurnInstruction instr
util::coordinate_calculation::bearing(turn_coordinate, post_turn_coordinate);

return StepManeuver{
turn_coordinate,
std::move(turn_coordinate),
pre_turn_bearing,
post_turn_bearing,
instruction,
std::move(instruction),
WaypointType::None,
INVALID_EXIT_NR,
{} // no intermediate intersections
Expand Down
233 changes: 130 additions & 103 deletions src/engine/guidance/post_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include <boost/range/algorithm_ext/erase.hpp>

#include <iostream>
#include <vector>
#include <cstddef>
#include <utility>

using TurnInstruction = osrm::extractor::guidance::TurnInstruction;
using TurnType = osrm::extractor::guidance::TurnType;
Expand Down Expand Up @@ -39,6 +40,122 @@ RouteStep forwardInto(RouteStep destination, const RouteStep &source)
return destination;
}

void fixFinalRoundabout(std::vector<RouteStep> &steps)
{
for (std::size_t propagation_index = steps.size() - 1; propagation_index > 0;
--propagation_index)
{
auto &propagation_step = steps[propagation_index];
if (entersRoundabout(propagation_step.maneuver.instruction))
{
propagation_step.maneuver.exit = 0;
propagation_step.geometry_end = steps.back().geometry_begin;
break;
}
else if (propagation_step.maneuver.instruction.type == TurnType::StayOnRoundabout)
{
//TODO this operates on the data that is in the instructions.
//We are missing out on the final segment after the last stay-on-roundabout
//instruction though. it is not contained somewhere until now
steps[propagation_index - 1] =
forwardInto(std::move(steps[propagation_index - 1]), propagation_step);
propagation_step.maneuver.instruction =
TurnInstruction::NO_TURN(); // mark intermediate instructions invalid
}
}
}

bool setUpRoundabout(RouteStep &step)
{
// basic entry into a roundabout
// Special case handling, if an entry is directly tied to an exit
const auto instruction = step.maneuver.instruction;
if (instruction.type == TurnType::EnterRotaryAtExit ||
instruction.type == TurnType::EnterRoundaboutAtExit)
{
step.maneuver.exit = 1;
// prevent futher special case handling of these two.
if (instruction.type == TurnType::EnterRotaryAtExit)
step.maneuver.instruction = TurnType::EnterRotary;
else
step.maneuver.instruction = TurnType::EnterRoundabout;
}

if (leavesRoundabout(instruction))
{
step.maneuver.exit = 1; // count the otherwise missing exit
if (instruction.type == TurnType::EnterRotaryAtExit)
step.maneuver.instruction = TurnType::EnterRotary;
else
step.maneuver.instruction = TurnType::EnterRoundabout;
return false;
}
else
{
return true;
}
}

void closeOffRoundabout(const bool on_roundabout,
std::vector<RouteStep> &steps,
const std::size_t step_index)
{
auto &step = steps[step_index];
step.maneuver.exit += 1;
if (!on_roundabout)
{

// We reached a special case that requires the addition of a special route step in
// the beginning.
// We started in a roundabout, so to announce the exit, we move use the exit
// instruction and
// move it right to the beginning to make sure to immediately announce the exit.
BOOST_ASSERT(leavesRoundabout(steps[1].maneuver.instruction) ||
steps[1].maneuver.instruction.type == TurnType::StayOnRoundabout);
steps[0].geometry_end = 1;
steps[1] = detail::forwardInto(steps[1], steps[0]);
steps[0].duration = 0;
steps[0].distance = 0;
steps[1].maneuver.instruction.type = step.maneuver.instruction.type == TurnType::ExitRotary
? TurnType::EnterRotary
: TurnType::EnterRoundabout;
}

// Normal exit from the roundabout, or exit from a previously fixed roundabout.
// Propagate the index back to the entering
// location and
// prepare the current silent set of instructions for removal.
if (step_index > 1)
{
std::cout << "Propagating backwards from " << step_index << std::endl;
// The very first route-step is head, so we cannot iterate past that one
for (std::size_t propagation_index = step_index - 1; propagation_index > 0;
--propagation_index)
{
auto &propagation_step = steps[propagation_index];
propagation_step = detail::forwardInto(propagation_step, steps[propagation_index + 1]);
if (entersRoundabout(propagation_step.maneuver.instruction))
{
// TODO at this point, we can remember the additional name for a rotary
// This requires some initial thought on the data format, though
propagation_step.maneuver.exit = step.maneuver.exit;
propagation_step.geometry_end = step.geometry_end;
propagation_step.name = step.name;
propagation_step.name_id = step.name_id;
break;
}
else
{
BOOST_ASSERT(propagation_step.maneuver.instruction.type =
TurnType::StayOnRoundabout);
propagation_step.maneuver.instruction =
TurnInstruction::NO_TURN(); // mark intermediate instructions invalid
}
}
// remove exit
step.maneuver.instruction = TurnInstruction::NO_TURN();
}
}
} // namespace detail

void print(const std::vector<RouteStep> &steps)
Expand Down Expand Up @@ -77,8 +194,8 @@ std::vector<RouteStep> postProcess(std::vector<RouteStep> steps)
if (steps.size() == 2)
return steps;

#define PRINT_DEBUG 0
#if PRINT_DEBUG
#define OSRM_POST_PROCESSING_PRINT_DEBUG 0
#if OSRM_POST_PROCESSING_PRINT_DEBUG
std::cout << "[POSTPROCESSING ITERATION]" << std::endl;
std::cout << "Input\n";
print(steps);
Expand All @@ -95,8 +212,7 @@ std::vector<RouteStep> postProcess(std::vector<RouteStep> steps)
into.maneuver.intersections.push_back(
{last_step.duration, last_step.distance, intersection.maneuver.location});

into = detail::forwardInto(std::move(into), intersection);
return into;
return detail::forwardInto(std::move(into), intersection);
};

// count the exits forward. if enter/exit roundabout happen both, no further treatment is
Expand All @@ -111,33 +227,9 @@ std::vector<RouteStep> postProcess(std::vector<RouteStep> steps)
if (entersRoundabout(instruction))
{
last_valid_instruction = step_index;
// basic entry into a roundabout
// Special case handling, if an entry is directly tied to an exit
if (instruction.type == TurnType::EnterRotaryAtExit ||
instruction.type == TurnType::EnterRoundaboutAtExit)
{
step.maneuver.exit = 1;
// prevent futher special case handling of these two.
if (instruction.type == TurnType::EnterRotaryAtExit)
step.maneuver.instruction = TurnType::EnterRotary;
else
step.maneuver.instruction = TurnType::EnterRoundabout;
}

if (leavesRoundabout(instruction))
{
step.maneuver.exit = 1; // count the otherwise missing exit
if (instruction.type == TurnType::EnterRotaryAtExit)
step.maneuver.instruction = TurnType::EnterRotary;
else
step.maneuver.instruction = TurnType::EnterRoundabout;
}
else
{
on_roundabout = true;
if (step_index + 1 < steps.size())
steps[step_index + 1].maneuver.exit = step.maneuver.exit;
}
on_roundabout = detail::setUpRoundabout(step);
if (on_roundabout && step_index + 1 < steps.size())
steps[step_index + 1].maneuver.exit = step.maneuver.exit;
}
else if (instruction.type == TurnType::StayOnRoundabout)
{
Expand All @@ -148,65 +240,14 @@ std::vector<RouteStep> postProcess(std::vector<RouteStep> steps)
}
else if (leavesRoundabout(instruction))
{
// count the exit (0 based vs 1 based counting)
step.maneuver.exit += 1;
if (!on_roundabout)
{

// We reached a special case that requires the addition of a special route step in
// the beginning.
// We started in a roundabout, so to announce the exit, we move use the exit
// instruction and
// move it right to the beginning to make sure to immediately announce the exit.
BOOST_ASSERT(leavesRoundabout(steps[1].maneuver.instruction) ||
steps[1].maneuver.instruction.type == TurnType::StayOnRoundabout);
steps[0].geometry_end = 1;
steps[1] = detail::forwardInto(steps[1], steps[0]);
steps[0].duration = 0;
steps[0].distance = 0;
steps[1].maneuver.instruction.type =
step.maneuver.instruction.type == TurnType::ExitRotary
? TurnType::EnterRotary
: TurnType::EnterRoundabout;

// remember the now enter-instruction as valid
// in case the we are not on a roundabout, the very first instruction
// after the depart will be transformed into a roundabout and become
// the first valid instruction
last_valid_instruction = 1;
}

// Normal exit from the roundabout, or exit from a previously fixed roundabout.
// Propagate the index back to the entering
// location and
// prepare the current silent set of instructions for removal.
if (step_index > 1)
{
// The very first route-step is head, so we cannot iterate past that one
for (std::size_t propagation_index = step_index - 1; propagation_index > 0;
--propagation_index)
{
auto &propagation_step = steps[propagation_index];
propagation_step =
detail::forwardInto(propagation_step, steps[propagation_index + 1]);
if (entersRoundabout(propagation_step.maneuver.instruction))
{
// TODO at this point, we can remember the additional name for a rotary
// This requires some initial thought on the data format, though
propagation_step.maneuver.exit = step.maneuver.exit;
propagation_step.geometry_end = step.geometry_end;
propagation_step.name = step.name;
propagation_step.name_id = step.name_id;
break;
}
else
{
BOOST_ASSERT(propagation_step.maneuver.instruction.type =
TurnType::StayOnRoundabout);
propagation_step.maneuver.instruction =
TurnInstruction::NO_TURN(); // mark intermediate instructions invalid
}
}
// remove exit
step.maneuver.instruction = TurnInstruction::NO_TURN();
}
detail::closeOffRoundabout(on_roundabout, steps, step_index);
on_roundabout = false;
}
else if (instruction.type == TurnType::Suppressed)
Expand All @@ -228,21 +269,7 @@ std::vector<RouteStep> postProcess(std::vector<RouteStep> steps)
// A roundabout without exit translates to enter-roundabout.
if (on_roundabout)
{
for (std::size_t propagation_index = steps.size() - 1; propagation_index > 0;
--propagation_index)
{
auto &propagation_step = steps[propagation_index];
if (entersRoundabout(propagation_step.maneuver.instruction))
{
propagation_step.maneuver.exit = 0;
break;
}
else if (propagation_step.maneuver.instruction == TurnType::StayOnRoundabout)
{
propagation_step.maneuver.instruction =
TurnInstruction::NO_TURN(); // mark intermediate instructions invalid
}
}
detail::fixFinalRoundabout(steps);
}

// finally clean up the post-processed instructions.
Expand All @@ -259,7 +286,7 @@ std::vector<RouteStep> postProcess(std::vector<RouteStep> steps)

boost::remove_erase_if(steps, not_is_valid);

#if PRINT_DEBUG
#if OSRM_POST_PROCESSING_PRINT_DEBUG
std::cout << "Merged\n";
print(steps);
#endif
Expand Down

0 comments on commit be4ad82

Please sign in to comment.