Skip to content

Commit

Permalink
moved passed assemble step. breaks roundabout counting
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Kobitzsch committed Mar 21, 2016
1 parent 1751fed commit ebf9555
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 104 deletions.
33 changes: 30 additions & 3 deletions include/engine/api/route_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class RouteAPI : public BaseAPI
}

util::json::Object MakeRoute(const std::vector<PhantomNodes> &segment_end_coordinates,
std::vector<std::vector<PathData>> unpacked_path_segments,
const std::vector<std::vector<PathData>> &unpacked_path_segments,
const std::vector<bool> &source_traversed_in_reverse,
const std::vector<bool> &target_traversed_in_reverse) const
{
Expand All @@ -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];
Expand All @@ -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<util::json::Value> json_overview;
if (parameters.overview != RouteParameters::OverviewType::False)
Expand Down
2 changes: 1 addition & 1 deletion include/engine/guidance/assemble_geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 3 additions & 2 deletions include/engine/guidance/post_processing.hpp
Original file line number Diff line number Diff line change
@@ -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 <vector>

Expand All @@ -12,7 +12,8 @@ namespace engine
namespace guidance
{

std::vector<std::vector<PathData>> postProcess(std::vector<std::vector<PathData>> path_data);
//passed as none-reference to modify in-place and move out again
std::vector<RouteStep> postProcess(std::vector<RouteStep> steps);

} // namespace guidance
} // namespace engine
Expand Down
3 changes: 2 additions & 1 deletion src/engine/guidance/assemble_steps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
172 changes: 75 additions & 97 deletions src/engine/guidance/post_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -67,115 +67,109 @@ PathData mergeInto(PathData destination, const PathData &source)

} // namespace detail

void print(const std::vector<std::vector<PathData>> &leg_data)
void print(const std::vector<RouteStep> &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<int>(data.turn_instruction.type);
const auto modifier = static_cast<int>(data.turn_instruction.direction_modifier);
const auto type = static_cast<int>(step.maneuver.instruction.type);
const auto modifier = static_cast<int>(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<std::vector<PathData>> postProcess(std::vector<std::vector<PathData>> leg_data)
std::vector<RouteStep> postProcess(std::vector<RouteStep> 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)
{
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;
}
if (on_roundabout)
{
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;
Expand All @@ -188,26 +182,10 @@ std::vector<std::vector<PathData>> postProcess(std::vector<std::vector<PathData>
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
Expand Down

0 comments on commit ebf9555

Please sign in to comment.