refactor of post-processing
- moves collapse into a dedicated set of functions / files - make collapse scenarios distinct (slight performance cost) - reduce verbosity for short name segments (now actually working, was supposed to do so before)
This commit is contained in:
committed by
Patrick Niklaus
parent
8d83c3adbb
commit
6c3390f14d
@@ -0,0 +1,405 @@
|
||||
#include "engine/guidance/collapse_scenario_detection.hpp"
|
||||
#include "extractor/guidance/constants.hpp"
|
||||
#include "util/bearing.hpp"
|
||||
|
||||
#include <numeric>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace engine
|
||||
{
|
||||
namespace guidance
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// check bearings for u-turns.
|
||||
// since bearings are wrapped around at 0 (we only support 0,360), we need to do some minor math to
|
||||
// check if bearings `a` and `b` go in opposite directions. In general we accept some minor
|
||||
// deviations for u-turns.
|
||||
bool bearingsAreReversed(const double bearing_in, const double bearing_out)
|
||||
{
|
||||
// Nearly perfectly reversed angles have a difference close to 180 degrees (straight)
|
||||
const double left_turn_angle = [&]() {
|
||||
if (0 <= bearing_out && bearing_out <= bearing_in)
|
||||
return bearing_in - bearing_out;
|
||||
return bearing_in + 360 - bearing_out;
|
||||
}();
|
||||
return util::angularDeviation(left_turn_angle, 180) <= 35;
|
||||
}
|
||||
|
||||
// to collapse steps, we focus on short segments that don't interact with other roads. To collapse
|
||||
// two instructions into one, we need to look at to instrutions immediately after each other.
|
||||
bool noIntermediaryIntersections(const RouteStep &step)
|
||||
{
|
||||
return std::all_of(step.intersections.begin() + 1,
|
||||
step.intersections.end(),
|
||||
[](const auto &intersection) { return intersection.entry.size() == 2; });
|
||||
}
|
||||
|
||||
// Link roads, as far as we are concerned, are short unnamed segments between to named segments.
|
||||
bool isLinkroad(const RouteStep &pre_link_step,
|
||||
const RouteStep &link_step,
|
||||
const RouteStep &post_link_step)
|
||||
{
|
||||
const constexpr double MAX_LINK_ROAD_LENGTH = 2 * MAX_COLLAPSE_DISTANCE;
|
||||
const auto is_short = link_step.distance <= MAX_LINK_ROAD_LENGTH;
|
||||
const auto unnamed = link_step.name_id == EMPTY_NAMEID;
|
||||
const auto between_named =
|
||||
(pre_link_step.name_id != EMPTY_NAMEID) && (post_link_step.name_id != EMPTY_NAMEID);
|
||||
|
||||
return is_short && unnamed && between_named && noIntermediaryIntersections(link_step);
|
||||
}
|
||||
|
||||
// Just like a link step, but shorter and no name restrictions.
|
||||
bool isShortAndUndisturbed(const RouteStep &step)
|
||||
{
|
||||
const auto is_short = step.distance <= MAX_COLLAPSE_DISTANCE;
|
||||
return is_short && noIntermediaryIntersections(step);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
bool basicCollapsePreconditions(const RouteStepIterator first, const RouteStepIterator second)
|
||||
{
|
||||
const auto has_roundabout_type = hasRoundaboutType(first->maneuver.instruction) ||
|
||||
hasRoundaboutType(second->maneuver.instruction);
|
||||
|
||||
const auto waypoint_type = hasWaypointType(*first) || hasWaypointType(*second);
|
||||
|
||||
return !has_roundabout_type && !waypoint_type && haveSameMode(*first, *second);
|
||||
}
|
||||
|
||||
bool basicCollapsePreconditions(const RouteStepIterator first,
|
||||
const RouteStepIterator second,
|
||||
const RouteStepIterator third)
|
||||
{
|
||||
const auto has_roundabout_type = hasRoundaboutType(first->maneuver.instruction) ||
|
||||
hasRoundaboutType(second->maneuver.instruction) ||
|
||||
hasRoundaboutType(third->maneuver.instruction);
|
||||
|
||||
// require modes to match up
|
||||
return !has_roundabout_type && haveSameMode(*first, *second, *third);
|
||||
}
|
||||
|
||||
bool isStaggeredIntersection(const RouteStepIterator step_prior_to_intersection,
|
||||
const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
BOOST_ASSERT(!hasWaypointType(*step_entering_intersection) &&
|
||||
!(hasWaypointType(*step_leaving_intersection)));
|
||||
// don't touch roundabouts
|
||||
if (entersRoundabout(step_entering_intersection->maneuver.instruction) ||
|
||||
entersRoundabout(step_leaving_intersection->maneuver.instruction))
|
||||
return false;
|
||||
// Base decision on distance since the zig-zag is a visual clue.
|
||||
// If adjusted, make sure to check validity of the is_right/is_left classification below
|
||||
const constexpr auto MAX_STAGGERED_DISTANCE = 3; // debatable, but keep short to be on safe side
|
||||
|
||||
const auto angle = [](const RouteStep &step) {
|
||||
const auto &intersection = step.intersections.front();
|
||||
const auto entry_bearing = intersection.bearings[intersection.in];
|
||||
const auto exit_bearing = intersection.bearings[intersection.out];
|
||||
return util::bearing::angleBetween(entry_bearing, exit_bearing);
|
||||
};
|
||||
|
||||
// Instead of using turn modifiers (e.g. as in isRightTurn) we want to be more strict here.
|
||||
// We do not want to trigger e.g. on sharp uturn'ish turns or going straight "turns".
|
||||
// Therefore we use the turn angle to derive 90 degree'ish right / left turns.
|
||||
// This more closely resembles what we understand as Staggered Intersection.
|
||||
// We have to be careful in cases with larger MAX_STAGGERED_DISTANCE values. If the distance
|
||||
// gets large, sharper angles might be not obvious enough to consider them a staggered
|
||||
// intersection. We might need to consider making the decision here dependent on the actual turn
|
||||
// angle taken. To do so, we could scale the angle-limits by a factor depending on the distance
|
||||
// between the turns.
|
||||
const auto is_right = [](const double angle) { return angle > 45 && angle < 135; };
|
||||
const auto is_left = [](const double angle) { return angle > 225 && angle < 315; };
|
||||
|
||||
const auto left_right =
|
||||
is_left(angle(*step_entering_intersection)) && is_right(angle(*step_leaving_intersection));
|
||||
const auto right_left =
|
||||
is_right(angle(*step_entering_intersection)) && is_left(angle(*step_leaving_intersection));
|
||||
|
||||
// A RouteStep holds distance/duration from the maneuver to the subsequent step.
|
||||
// We are only interested in the distance between the first and the second.
|
||||
const auto is_short = step_entering_intersection->distance < MAX_STAGGERED_DISTANCE;
|
||||
const auto intermediary_mode_change =
|
||||
step_prior_to_intersection->mode == step_leaving_intersection->mode &&
|
||||
step_entering_intersection->mode != step_leaving_intersection->mode;
|
||||
|
||||
const auto mode_change_when_entering =
|
||||
step_prior_to_intersection->mode != step_entering_intersection->mode;
|
||||
|
||||
// previous step maneuver intersections should be length 1 to indicate that
|
||||
// there are no intersections between the two potentially collapsible turns
|
||||
return is_short && (left_right || right_left) && !intermediary_mode_change &&
|
||||
!mode_change_when_entering && noIntermediaryIntersections(*step_entering_intersection);
|
||||
}
|
||||
|
||||
bool isUTurn(const RouteStepIterator step_prior_to_intersection,
|
||||
const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
if (!basicCollapsePreconditions(
|
||||
step_prior_to_intersection, step_entering_intersection, step_leaving_intersection))
|
||||
return false;
|
||||
|
||||
// the most basic condition for a uturn is that we actually turn around
|
||||
const bool takes_u_turn = bearingsAreReversed(
|
||||
util::bearing::reverse(step_entering_intersection->intersections.front()
|
||||
.bearings[step_entering_intersection->intersections.front().in]),
|
||||
step_leaving_intersection->intersections.front()
|
||||
.bearings[step_leaving_intersection->intersections.front().out]);
|
||||
|
||||
if (!takes_u_turn)
|
||||
return false;
|
||||
|
||||
// TODO check for name match after additional step
|
||||
const auto names_match = haveSameName(*step_prior_to_intersection, *step_leaving_intersection);
|
||||
|
||||
// names within a u-turn road have to match from entry step to exit step
|
||||
if (!names_match)
|
||||
return false;
|
||||
|
||||
const auto collapsable = isShortAndUndisturbed(*step_entering_intersection);
|
||||
const auto only_allowed_turn = (numberOfAllowedTurns(*step_leaving_intersection) == 1) &&
|
||||
noIntermediaryIntersections(*step_entering_intersection);
|
||||
|
||||
return collapsable || isLinkroad(*step_prior_to_intersection,
|
||||
*step_entering_intersection,
|
||||
*step_leaving_intersection) ||
|
||||
only_allowed_turn;
|
||||
}
|
||||
|
||||
bool isNameOszillation(const RouteStepIterator step_prior_to_intersection,
|
||||
const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
if (!basicCollapsePreconditions(
|
||||
step_prior_to_intersection, step_entering_intersection, step_leaving_intersection))
|
||||
return false;
|
||||
|
||||
const auto are_name_changes =
|
||||
(hasTurnType(*step_entering_intersection, TurnType::NewName) ||
|
||||
(hasTurnType(*step_entering_intersection, TurnType::Turn) &&
|
||||
hasModifier(*step_entering_intersection, DirectionModifier::Straight))) &&
|
||||
(hasTurnType(*step_leaving_intersection, TurnType::NewName) ||
|
||||
(hasTurnType(*step_leaving_intersection, TurnType::Suppressed) &&
|
||||
step_leaving_intersection->name_id == EMPTY_NAMEID) ||
|
||||
(hasTurnType(*step_leaving_intersection, TurnType::Turn) &&
|
||||
hasModifier(*step_leaving_intersection, DirectionModifier::Straight)));
|
||||
|
||||
if (!are_name_changes)
|
||||
return false;
|
||||
|
||||
const auto names_match =
|
||||
// accept empty names as well as same names
|
||||
step_prior_to_intersection->name_id == step_leaving_intersection->name_id ||
|
||||
haveSameName(*step_prior_to_intersection, *step_leaving_intersection);
|
||||
return names_match;
|
||||
}
|
||||
|
||||
bool maneuverPreceededByNameChange(const RouteStepIterator step_prior_to_intersection,
|
||||
const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
if (!basicCollapsePreconditions(
|
||||
step_prior_to_intersection, step_entering_intersection, step_leaving_intersection))
|
||||
return false;
|
||||
|
||||
const auto short_and_undisturbed = isShortAndUndisturbed(*step_entering_intersection);
|
||||
const auto is_name_change =
|
||||
hasTurnType(*step_entering_intersection, TurnType::NewName) ||
|
||||
((hasTurnType(*step_entering_intersection, TurnType::Turn) ||
|
||||
hasTurnType(*step_entering_intersection, TurnType::Continue)) &&
|
||||
hasModifier(*step_entering_intersection, DirectionModifier::Straight));
|
||||
|
||||
const auto followed_by_maneuver =
|
||||
hasTurnType(*step_leaving_intersection) &&
|
||||
!hasTurnType(*step_leaving_intersection, TurnType::Suppressed);
|
||||
|
||||
return short_and_undisturbed && is_name_change && followed_by_maneuver;
|
||||
}
|
||||
|
||||
bool maneuverPreceededBySuppressedDirection(const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
if (!basicCollapsePreconditions(step_entering_intersection, step_leaving_intersection))
|
||||
return false;
|
||||
|
||||
const auto short_and_undisturbed = isShortAndUndisturbed(*step_entering_intersection);
|
||||
|
||||
const auto is_suppressed_direction =
|
||||
hasTurnType(*step_entering_intersection, TurnType::Suppressed) &&
|
||||
!hasModifier(*step_entering_intersection, DirectionModifier::Straight);
|
||||
|
||||
const auto followed_by_maneuver =
|
||||
hasTurnType(*step_leaving_intersection) &&
|
||||
!hasTurnType(*step_leaving_intersection, TurnType::Suppressed);
|
||||
|
||||
const auto keeps_direction =
|
||||
areSameSide(*step_entering_intersection, *step_leaving_intersection);
|
||||
|
||||
const auto has_choice = numberOfAllowedTurns(*step_entering_intersection) > 1;
|
||||
|
||||
return short_and_undisturbed && has_choice && is_suppressed_direction && followed_by_maneuver &&
|
||||
keeps_direction;
|
||||
}
|
||||
|
||||
bool suppressedStraightBetweenTurns(const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_at_center_of_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
if (!basicCollapsePreconditions(
|
||||
step_entering_intersection, step_at_center_of_intersection, step_leaving_intersection))
|
||||
return false;
|
||||
|
||||
const auto both_short_enough =
|
||||
step_entering_intersection->distance < 0.8 * MAX_COLLAPSE_DISTANCE &&
|
||||
step_at_center_of_intersection->distance < 0.8 * MAX_COLLAPSE_DISTANCE;
|
||||
|
||||
const auto similar_length =
|
||||
(step_entering_intersection->distance < 5 &&
|
||||
step_at_center_of_intersection->distance < 5) ||
|
||||
std::min(step_entering_intersection->distance, step_at_center_of_intersection->distance) /
|
||||
std::max(step_entering_intersection->distance,
|
||||
step_at_center_of_intersection->distance) >
|
||||
0.75;
|
||||
|
||||
const auto correct_types =
|
||||
hasTurnType(*step_at_center_of_intersection, TurnType::Suppressed) &&
|
||||
hasModifier(*step_at_center_of_intersection, DirectionModifier::Straight) &&
|
||||
(hasTurnType(*step_entering_intersection, TurnType::Turn) ||
|
||||
hasTurnType(*step_entering_intersection, TurnType::Continue)) &&
|
||||
(hasTurnType(*step_leaving_intersection, TurnType::Turn) ||
|
||||
hasTurnType(*step_leaving_intersection, TurnType::Continue) ||
|
||||
hasTurnType(*step_leaving_intersection, TurnType::OnRamp));
|
||||
|
||||
return both_short_enough && similar_length && correct_types;
|
||||
}
|
||||
|
||||
bool maneuverSucceededByNameChange(const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
if (!basicCollapsePreconditions(step_entering_intersection, step_leaving_intersection))
|
||||
return false;
|
||||
|
||||
const auto short_and_undisturbed = isShortAndUndisturbed(*step_entering_intersection);
|
||||
const auto followed_by_name_change =
|
||||
hasTurnType(*step_leaving_intersection, TurnType::NewName) ||
|
||||
((hasTurnType(*step_leaving_intersection, TurnType::Turn) ||
|
||||
hasTurnType(*step_leaving_intersection, TurnType::Continue)) &&
|
||||
hasModifier(*step_leaving_intersection, DirectionModifier::Straight));
|
||||
|
||||
const auto is_maneuver = hasTurnType(*step_entering_intersection) &&
|
||||
!hasTurnType(*step_entering_intersection, TurnType::Suppressed);
|
||||
|
||||
// a straight name change can overrule max collapse distance
|
||||
const auto is_strong_name_change =
|
||||
hasTurnType(*step_leaving_intersection, TurnType::NewName) &&
|
||||
hasModifier(*step_leaving_intersection, DirectionModifier::Straight) &&
|
||||
step_entering_intersection->distance <= 1.5 * MAX_COLLAPSE_DISTANCE;
|
||||
|
||||
// also allow a bit more, if the new name is without choice
|
||||
const auto is_choiceless_name_change =
|
||||
hasTurnType(*step_leaving_intersection, TurnType::NewName) &&
|
||||
numberOfAllowedTurns(*step_leaving_intersection) == 1 &&
|
||||
step_entering_intersection->distance <= 1.5 * MAX_COLLAPSE_DISTANCE;
|
||||
|
||||
return (short_and_undisturbed || is_strong_name_change || is_choiceless_name_change) &&
|
||||
followed_by_name_change && is_maneuver;
|
||||
}
|
||||
|
||||
bool maneuverSucceededBySuppressedDirection(const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
if (!basicCollapsePreconditions(step_entering_intersection, step_leaving_intersection))
|
||||
return false;
|
||||
|
||||
const auto short_and_undisturbed = isShortAndUndisturbed(*step_entering_intersection);
|
||||
|
||||
const auto followed_by_suppressed_direction =
|
||||
hasTurnType(*step_leaving_intersection, TurnType::Suppressed) &&
|
||||
!hasModifier(*step_leaving_intersection, DirectionModifier::Straight);
|
||||
|
||||
const auto is_maneuver = hasTurnType(*step_entering_intersection) &&
|
||||
!hasTurnType(*step_entering_intersection, TurnType::Suppressed);
|
||||
|
||||
const auto keeps_direction =
|
||||
areSameSide(*step_entering_intersection, *step_leaving_intersection);
|
||||
|
||||
return short_and_undisturbed && followed_by_suppressed_direction && is_maneuver &&
|
||||
keeps_direction;
|
||||
}
|
||||
|
||||
bool nameChangeImmediatelyAfterSuppressed(const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
if (!basicCollapsePreconditions(step_entering_intersection, step_leaving_intersection))
|
||||
return false;
|
||||
|
||||
const auto very_short = step_entering_intersection->distance < 0.25 * MAX_COLLAPSE_DISTANCE;
|
||||
const auto correct_types = hasTurnType(*step_entering_intersection, TurnType::Suppressed) &&
|
||||
hasTurnType(*step_leaving_intersection, TurnType::NewName);
|
||||
|
||||
return very_short && correct_types;
|
||||
}
|
||||
|
||||
bool closeChoicelessTurnAfterTurn(const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
if (!basicCollapsePreconditions(step_entering_intersection, step_leaving_intersection))
|
||||
return false;
|
||||
|
||||
const auto short_and_undisturbed = isShortAndUndisturbed(*step_entering_intersection);
|
||||
const auto is_turn = !hasModifier(*step_entering_intersection, DirectionModifier::Straight);
|
||||
|
||||
const auto followed_by_choiceless = numberOfAllowedTurns(*step_leaving_intersection) == 1;
|
||||
const auto followed_by_suppressed =
|
||||
hasTurnType(*step_leaving_intersection, TurnType::Suppressed);
|
||||
|
||||
return short_and_undisturbed && is_turn && followed_by_choiceless && !followed_by_suppressed;
|
||||
}
|
||||
|
||||
bool doubleChoiceless(const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
if (!basicCollapsePreconditions(step_entering_intersection, step_leaving_intersection))
|
||||
return false;
|
||||
|
||||
const auto double_choiceless =
|
||||
(numberOfAllowedTurns(*step_leaving_intersection) == 1) &&
|
||||
(step_entering_intersection->intersections.size() == 2) &&
|
||||
(std::count(step_entering_intersection->intersections.back().entry.begin(),
|
||||
step_entering_intersection->intersections.back().entry.end(),
|
||||
true) == 1);
|
||||
|
||||
const auto short_enough = step_entering_intersection->distance < 1.5 * MAX_COLLAPSE_DISTANCE;
|
||||
|
||||
return double_choiceless && short_enough;
|
||||
}
|
||||
|
||||
bool straightTurnFollowedByChoiceless(const RouteStepIterator step_entering_intersection,
|
||||
const RouteStepIterator step_leaving_intersection)
|
||||
{
|
||||
if (!basicCollapsePreconditions(step_entering_intersection, step_leaving_intersection))
|
||||
return false;
|
||||
|
||||
const auto is_short = step_entering_intersection->distance <= 2 * MAX_COLLAPSE_DISTANCE;
|
||||
const auto has_correct_type = hasTurnType(*step_entering_intersection, TurnType::Continue) ||
|
||||
hasTurnType(*step_entering_intersection, TurnType::Turn);
|
||||
|
||||
const auto is_straight = hasModifier(*step_entering_intersection, DirectionModifier::Straight);
|
||||
|
||||
const auto only_choice = numberOfAllowedTurns(*step_leaving_intersection) == 1;
|
||||
|
||||
return is_short && has_correct_type && is_straight && only_choice &&
|
||||
noIntermediaryIntersections(*step_entering_intersection);
|
||||
}
|
||||
|
||||
} /* namespace guidance */
|
||||
} /* namespace engine */
|
||||
} /* namespace osrm */
|
||||
@@ -0,0 +1,460 @@
|
||||
#include "engine/guidance/collapse_turns.hpp"
|
||||
#include "extractor/guidance/constants.hpp"
|
||||
#include "extractor/guidance/turn_instruction.hpp"
|
||||
#include "engine/guidance/collapse_scenario_detection.hpp"
|
||||
#include "engine/guidance/collapsing_utility.hpp"
|
||||
#include "util/bearing.hpp"
|
||||
#include "util/guidance/name_announcements.hpp"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
using osrm::extractor::guidance::TurnInstruction;
|
||||
using osrm::util::angularDeviation;
|
||||
using namespace osrm::extractor::guidance;
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace engine
|
||||
{
|
||||
namespace guidance
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
const constexpr double MAX_COLLAPSE_DISTANCE = 30;
|
||||
|
||||
// find the combined turn angle for two turns. Not in all scenarios we can easily add both angles
|
||||
// (e.g 90 degree left followed by 90 degree right would be no turn at all).
|
||||
double findTotalTurnAngle(const RouteStep &entry_step, const RouteStep &exit_step)
|
||||
{
|
||||
if (entry_step.geometry_begin > exit_step.geometry_begin)
|
||||
return findTotalTurnAngle(exit_step, entry_step);
|
||||
|
||||
const auto exit_intersection = exit_step.intersections.front();
|
||||
const auto exit_step_exit_bearing = exit_intersection.bearings[exit_intersection.out];
|
||||
const auto exit_step_entry_bearing =
|
||||
util::bearing::reverse(exit_intersection.bearings[exit_intersection.in]);
|
||||
|
||||
const auto entry_intersection = entry_step.intersections.front();
|
||||
const auto entry_step_entry_bearing =
|
||||
util::bearing::reverse(entry_intersection.bearings[entry_intersection.in]);
|
||||
const auto entry_step_exit_bearing = entry_intersection.bearings[entry_intersection.out];
|
||||
|
||||
const auto exit_angle =
|
||||
util::bearing::angleBetween(exit_step_entry_bearing, exit_step_exit_bearing);
|
||||
const auto entry_angle =
|
||||
util::bearing::angleBetween(entry_step_entry_bearing, entry_step_exit_bearing);
|
||||
|
||||
const double total_angle =
|
||||
util::bearing::angleBetween(entry_step_entry_bearing, exit_step_exit_bearing);
|
||||
|
||||
// both angles are in the same direction, the total turn gets increased
|
||||
//
|
||||
// a ---- b
|
||||
// \
|
||||
// c
|
||||
// |
|
||||
// d
|
||||
//
|
||||
// Will be considered just like
|
||||
//
|
||||
// a -----b
|
||||
// |
|
||||
// c
|
||||
// |
|
||||
// d
|
||||
const auto use_total_angle = [&]() {
|
||||
// only consider actual turns in combination:
|
||||
if (angularDeviation(total_angle, 180) < 0.5 * NARROW_TURN_ANGLE)
|
||||
return false;
|
||||
|
||||
// entry step is short and the exit and the exit step does not have intersections??
|
||||
if (entry_step.distance < MAX_COLLAPSE_DISTANCE)
|
||||
return true;
|
||||
|
||||
if (entry_step.distance > 2 * MAX_COLLAPSE_DISTANCE)
|
||||
return false;
|
||||
|
||||
// both go roughly in the same direction
|
||||
if ((entry_angle <= 185 && exit_angle <= 185) || (entry_angle >= 175 && exit_angle >= 175))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}();
|
||||
|
||||
// We allow for minor deviations from a straight line
|
||||
if (use_total_angle)
|
||||
{
|
||||
return total_angle;
|
||||
}
|
||||
else
|
||||
{
|
||||
// to prevent ignoring angles like
|
||||
//
|
||||
// a -- b
|
||||
// |
|
||||
// c -- d
|
||||
//
|
||||
// We don't combine both turn angles here but keep the very first turn angle.
|
||||
// We choose the first one, since we consider the first maneuver in a merge range the
|
||||
// important one
|
||||
return entry_angle;
|
||||
}
|
||||
}
|
||||
|
||||
inline void handleSliproad(RouteStepIterator sliproad_step)
|
||||
{
|
||||
// find the next step after the sliproad step itself (this is not necessarily the next step,
|
||||
// since we might have to skip over traffic lights/node penalties)
|
||||
auto next_step = [&sliproad_step]() {
|
||||
auto next_step = findNextTurn(sliproad_step);
|
||||
while (isTrafficLightStep(*next_step))
|
||||
{
|
||||
// in sliproad checks, we should have made sure not to include invalid modes
|
||||
BOOST_ASSERT(haveSameMode(*sliproad_step, *next_step));
|
||||
sliproad_step->ElongateBy(*next_step);
|
||||
next_step->Invalidate();
|
||||
next_step = findNextTurn(next_step);
|
||||
}
|
||||
BOOST_ASSERT(haveSameMode(*sliproad_step, *next_step));
|
||||
return next_step;
|
||||
}();
|
||||
|
||||
// have we reached the end?
|
||||
if (hasWaypointType(*next_step))
|
||||
{
|
||||
setInstructionType(*sliproad_step, TurnType::Turn);
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto previous_step = findPreviousTurn(sliproad_step);
|
||||
const auto connecting_same_name_roads = haveSameName(*previous_step, *next_step);
|
||||
auto sliproad_turn_type = connecting_same_name_roads ? TurnType::Continue : TurnType::Turn;
|
||||
setInstructionType(*sliproad_step, sliproad_turn_type);
|
||||
combineRouteSteps(*sliproad_step,
|
||||
*next_step,
|
||||
AdjustToCombinedTurnAngleStrategy(),
|
||||
TransferSignageStrategy(),
|
||||
TransferLanesStrategy());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// STRATEGIES
|
||||
|
||||
// keep signage/other entries in route step intact
|
||||
void NoModificationStrategy::operator()(RouteStep &, const RouteStep &) const
|
||||
{
|
||||
// actually do nothing.
|
||||
}
|
||||
|
||||
// transfer turn type from a different turn
|
||||
void TransferTurnTypeStrategy::operator()(RouteStep &step_at_turn_location,
|
||||
const RouteStep &transfer_from_step) const
|
||||
{
|
||||
step_at_turn_location.maneuver = transfer_from_step.maneuver;
|
||||
}
|
||||
|
||||
void AdjustToCombinedTurnAngleStrategy::operator()(RouteStep &step_at_turn_location,
|
||||
const RouteStep &transfer_from_step) const
|
||||
{
|
||||
// TODO assert transfer_from_step == step_at_turn_location + 1
|
||||
const auto angle = findTotalTurnAngle(step_at_turn_location, transfer_from_step);
|
||||
step_at_turn_location.maneuver.instruction.direction_modifier = getTurnDirection(angle);
|
||||
}
|
||||
|
||||
AdjustToCombinedTurnStrategy::AdjustToCombinedTurnStrategy(
|
||||
const RouteStep &step_prior_to_intersection)
|
||||
: step_prior_to_intersection(step_prior_to_intersection)
|
||||
{
|
||||
}
|
||||
|
||||
void AdjustToCombinedTurnStrategy::operator()(RouteStep &step_at_turn_location,
|
||||
const RouteStep &transfer_from_step) const
|
||||
{
|
||||
const auto angle = findTotalTurnAngle(step_at_turn_location, transfer_from_step);
|
||||
const auto new_modifier = getTurnDirection(angle);
|
||||
|
||||
// a turn that is a new name or straight (turn/continue)
|
||||
const auto is_non_turn = [](const RouteStep &step) {
|
||||
return hasTurnType(step, TurnType::NewName) ||
|
||||
(hasTurnType(step, TurnType::Turn) &&
|
||||
hasModifier(step, DirectionModifier::Straight)) ||
|
||||
(hasTurnType(step, TurnType::Continue) &&
|
||||
hasModifier(step, DirectionModifier::Straight));
|
||||
};
|
||||
|
||||
// check if the first part is the actual turn
|
||||
const auto transferring_from_non_turn = is_non_turn(transfer_from_step);
|
||||
|
||||
// or if the maneuver location does not perform an actual turn
|
||||
const auto maneuver_at_non_turn = is_non_turn(step_at_turn_location) ||
|
||||
hasTurnType(step_at_turn_location, TurnType::Suppressed);
|
||||
|
||||
// creating turns if the original instrution wouldn't be a maneuver (also for turn straights)`
|
||||
if (transferring_from_non_turn || maneuver_at_non_turn)
|
||||
{
|
||||
if (hasTurnType(step_at_turn_location, TurnType::Suppressed))
|
||||
{
|
||||
if (new_modifier == DirectionModifier::Straight)
|
||||
setInstructionType(step_at_turn_location, TurnType::NewName);
|
||||
else
|
||||
step_at_turn_location.maneuver.instruction.type =
|
||||
haveSameName(step_prior_to_intersection, transfer_from_step)
|
||||
? TurnType::Continue
|
||||
: TurnType::Turn;
|
||||
}
|
||||
else if (hasTurnType(step_at_turn_location, TurnType::NewName) &&
|
||||
hasTurnType(transfer_from_step, TurnType::Suppressed) &&
|
||||
new_modifier != DirectionModifier::Straight)
|
||||
{
|
||||
setInstructionType(step_at_turn_location, TurnType::Turn);
|
||||
}
|
||||
else if (hasTurnType(step_at_turn_location, TurnType::Continue) &&
|
||||
!haveSameName(step_prior_to_intersection, transfer_from_step))
|
||||
{
|
||||
setInstructionType(step_at_turn_location, TurnType::Turn);
|
||||
}
|
||||
else if (hasTurnType(step_at_turn_location, TurnType::Turn) &&
|
||||
haveSameName(step_prior_to_intersection, transfer_from_step))
|
||||
{
|
||||
setInstructionType(step_at_turn_location, TurnType::Continue);
|
||||
}
|
||||
}
|
||||
// if we are turning onto a ramp, we carry the ramp (e.g. a turn onto a ramp that is modelled
|
||||
// later only)
|
||||
else if (hasTurnType(transfer_from_step, TurnType::OnRamp))
|
||||
{
|
||||
setInstructionType(step_at_turn_location, TurnType::OnRamp);
|
||||
}
|
||||
// switch two turns to a single continue, if necessary
|
||||
else if (hasTurnType(step_at_turn_location, TurnType::Turn) &&
|
||||
hasTurnType(transfer_from_step, TurnType::Turn) &&
|
||||
haveSameName(step_prior_to_intersection, transfer_from_step))
|
||||
{
|
||||
setInstructionType(step_at_turn_location, TurnType::Continue);
|
||||
}
|
||||
// switch continue to turn, if possible
|
||||
else if (hasTurnType(step_at_turn_location, TurnType::Continue) &&
|
||||
hasTurnType(transfer_from_step, TurnType::Turn) &&
|
||||
!haveSameName(step_prior_to_intersection, transfer_from_step))
|
||||
{
|
||||
setInstructionType(step_at_turn_location, TurnType::Turn);
|
||||
}
|
||||
|
||||
// finally set our new modifier
|
||||
step_at_turn_location.maneuver.instruction.direction_modifier = new_modifier;
|
||||
}
|
||||
|
||||
StaggeredTurnStrategy::StaggeredTurnStrategy(const RouteStep &step_prior_to_intersection)
|
||||
: step_prior_to_intersection(step_prior_to_intersection)
|
||||
{
|
||||
}
|
||||
|
||||
void StaggeredTurnStrategy::operator()(RouteStep &step_at_turn_location,
|
||||
const RouteStep &transfer_from_step) const
|
||||
{
|
||||
step_at_turn_location.maneuver.instruction.direction_modifier = DirectionModifier::Straight;
|
||||
step_at_turn_location.maneuver.instruction.type =
|
||||
haveSameName(step_prior_to_intersection, transfer_from_step) ? TurnType::Suppressed
|
||||
: TurnType::NewName;
|
||||
}
|
||||
|
||||
SetFixedInstructionStrategy::SetFixedInstructionStrategy(
|
||||
const extractor::guidance::TurnInstruction instruction)
|
||||
: instruction(instruction)
|
||||
{
|
||||
}
|
||||
|
||||
void SetFixedInstructionStrategy::operator()(RouteStep &step_at_turn_location,
|
||||
const RouteStep &) const
|
||||
{
|
||||
step_at_turn_location.maneuver.instruction = instruction;
|
||||
}
|
||||
|
||||
void TransferSignageStrategy::operator()(RouteStep &step_at_turn_location,
|
||||
const RouteStep &transfer_from_step) const
|
||||
{
|
||||
step_at_turn_location.AdaptStepSignage(transfer_from_step);
|
||||
step_at_turn_location.rotary_name = transfer_from_step.rotary_name;
|
||||
step_at_turn_location.rotary_pronunciation = transfer_from_step.rotary_pronunciation;
|
||||
}
|
||||
|
||||
void TransferLanesStrategy::operator()(RouteStep &step_at_turn_location,
|
||||
const RouteStep &transfer_from_step) const
|
||||
{
|
||||
step_at_turn_location.intersections.front().lanes =
|
||||
transfer_from_step.intersections.front().lanes;
|
||||
step_at_turn_location.intersections.front().lane_description =
|
||||
transfer_from_step.intersections.front().lane_description;
|
||||
}
|
||||
|
||||
void suppressStep(RouteStep &step_at_turn_location, RouteStep &step_after_turn_location)
|
||||
{
|
||||
return combineRouteSteps(step_at_turn_location,
|
||||
step_after_turn_location,
|
||||
NoModificationStrategy(),
|
||||
NoModificationStrategy(),
|
||||
NoModificationStrategy());
|
||||
}
|
||||
|
||||
// OTHER IMPLEMENTATIONS
|
||||
OSRM_ATTR_WARN_UNUSED
|
||||
RouteSteps collapseTurnInstructions(RouteSteps steps)
|
||||
{
|
||||
// make sure we can safely iterate over all steps (has depart/arrive with TurnType::NoTurn)
|
||||
BOOST_ASSERT(!hasTurnType(steps.front()) && !hasTurnType(steps.back()));
|
||||
BOOST_ASSERT(hasWaypointType(steps.front()) && hasWaypointType(steps.back()));
|
||||
|
||||
if (steps.size() <= 2)
|
||||
return steps;
|
||||
|
||||
// start of with no-op
|
||||
for (auto current_step = steps.begin() + 1; current_step + 1 != steps.end(); ++current_step)
|
||||
{
|
||||
if (entersRoundabout(current_step->maneuver.instruction) ||
|
||||
staysOnRoundabout(current_step->maneuver.instruction))
|
||||
{
|
||||
// Skip over all instructions within the roundabout
|
||||
for (; current_step + 1 != steps.end(); ++current_step)
|
||||
if (leavesRoundabout(current_step->maneuver.instruction))
|
||||
break;
|
||||
|
||||
// are we done for good?
|
||||
if (current_step + 1 == steps.end())
|
||||
break;
|
||||
else
|
||||
continue;
|
||||
}
|
||||
|
||||
// only operate on actual turns
|
||||
if (!hasTurnType(*current_step))
|
||||
continue;
|
||||
|
||||
// handle all situations involving the sliproad turn type
|
||||
if (hasTurnType(*current_step, TurnType::Sliproad))
|
||||
{
|
||||
handleSliproad(current_step);
|
||||
continue;
|
||||
}
|
||||
|
||||
// don't collapse next step if it is a waypoint alread
|
||||
const auto next_step = findNextTurn(current_step);
|
||||
if (hasWaypointType(*next_step))
|
||||
break;
|
||||
|
||||
const auto previous_step = findPreviousTurn(current_step);
|
||||
|
||||
// don't collapse anything that does change modes
|
||||
if (current_step->mode != next_step->mode)
|
||||
continue;
|
||||
|
||||
// handle staggered intersections:
|
||||
// a staggered intersection describes to turns in rapid succession that go in opposite
|
||||
// directions (e.g. right + left) with a very short segment in between
|
||||
if (isStaggeredIntersection(previous_step, current_step, next_step))
|
||||
{
|
||||
combineRouteSteps(*current_step,
|
||||
*next_step,
|
||||
StaggeredTurnStrategy(*previous_step),
|
||||
TransferSignageStrategy(),
|
||||
NoModificationStrategy());
|
||||
}
|
||||
else if (isUTurn(previous_step, current_step, next_step))
|
||||
{
|
||||
combineRouteSteps(
|
||||
*current_step,
|
||||
*next_step,
|
||||
SetFixedInstructionStrategy({TurnType::Continue, DirectionModifier::UTurn}),
|
||||
TransferSignageStrategy(),
|
||||
NoModificationStrategy());
|
||||
}
|
||||
else if (isNameOszillation(previous_step, current_step, next_step))
|
||||
{
|
||||
// first deactivate the second name switch
|
||||
suppressStep(*current_step, *next_step);
|
||||
// and then the first (to ensure both iterators to be valid)
|
||||
suppressStep(*previous_step, *current_step);
|
||||
}
|
||||
else if (maneuverPreceededByNameChange(previous_step, current_step, next_step) ||
|
||||
maneuverPreceededBySuppressedDirection(current_step, next_step))
|
||||
{
|
||||
const auto strategy = AdjustToCombinedTurnStrategy(*previous_step);
|
||||
strategy(*next_step, *current_step);
|
||||
// suppress previous step
|
||||
suppressStep(*previous_step, *current_step);
|
||||
}
|
||||
else if (maneuverSucceededByNameChange(current_step, next_step) ||
|
||||
nameChangeImmediatelyAfterSuppressed(current_step, next_step) ||
|
||||
maneuverSucceededBySuppressedDirection(current_step, next_step) ||
|
||||
closeChoicelessTurnAfterTurn(current_step, next_step))
|
||||
{
|
||||
combineRouteSteps(*current_step,
|
||||
*next_step,
|
||||
AdjustToCombinedTurnStrategy(*previous_step),
|
||||
TransferSignageStrategy(),
|
||||
NoModificationStrategy());
|
||||
}
|
||||
else if (straightTurnFollowedByChoiceless(current_step, next_step))
|
||||
{
|
||||
combineRouteSteps(*current_step,
|
||||
*next_step,
|
||||
AdjustToCombinedTurnStrategy(*previous_step),
|
||||
TransferSignageStrategy(),
|
||||
NoModificationStrategy());
|
||||
}
|
||||
else if (suppressedStraightBetweenTurns(previous_step, current_step, next_step))
|
||||
{
|
||||
const auto far_back_step = findPreviousTurn(previous_step);
|
||||
previous_step->ElongateBy(*current_step);
|
||||
current_step->Invalidate();
|
||||
combineRouteSteps(*previous_step,
|
||||
*next_step,
|
||||
AdjustToCombinedTurnStrategy(*far_back_step),
|
||||
TransferSignageStrategy(),
|
||||
NoModificationStrategy());
|
||||
}
|
||||
// if the current collapsing triggers, we can check for advanced scenarios that only are
|
||||
// possible after an inital collapse step (e.g. name change right after a u-turn)
|
||||
//
|
||||
// f - e - d
|
||||
// | |
|
||||
// a - b - c
|
||||
//
|
||||
// In this scenario, bc and de might belong to a different road than a-b and f-e (since
|
||||
// there are no fix conventions how to label them in segregated intersections). These steps
|
||||
// might only become apparent after some initial collapsing
|
||||
const auto new_next_step = findNextTurn(current_step);
|
||||
if (doubleChoiceless(current_step, new_next_step))
|
||||
{
|
||||
combineRouteSteps(*current_step,
|
||||
*new_next_step,
|
||||
AdjustToCombinedTurnStrategy(*previous_step),
|
||||
TransferSignageStrategy(),
|
||||
NoModificationStrategy());
|
||||
}
|
||||
if (!hasWaypointType(*previous_step))
|
||||
{
|
||||
const auto far_back_step = findPreviousTurn(previous_step);
|
||||
// due to name changes, we can find u-turns a bit late. Thats why we check far back as
|
||||
// well
|
||||
if (isUTurn(far_back_step, previous_step, current_step))
|
||||
{
|
||||
combineRouteSteps(
|
||||
*previous_step,
|
||||
*current_step,
|
||||
SetFixedInstructionStrategy({TurnType::Continue, DirectionModifier::UTurn}),
|
||||
TransferSignageStrategy(),
|
||||
NoModificationStrategy());
|
||||
}
|
||||
}
|
||||
}
|
||||
return steps;
|
||||
}
|
||||
|
||||
} // namespace guidance
|
||||
} // namespace engine
|
||||
} // namespace osrm
|
||||
@@ -2,17 +2,14 @@
|
||||
#include "util/group_by.hpp"
|
||||
|
||||
#include "extractor/guidance/turn_instruction.hpp"
|
||||
#include "engine/guidance/post_processing.hpp"
|
||||
#include "engine/guidance/collapsing_utility.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <unordered_set>
|
||||
#include <utility>
|
||||
|
||||
using TurnInstruction = osrm::extractor::guidance::TurnInstruction;
|
||||
namespace TurnType = osrm::extractor::guidance::TurnType;
|
||||
namespace DirectionModifier = osrm::extractor::guidance::DirectionModifier;
|
||||
|
||||
using osrm::extractor::guidance::TurnInstruction;
|
||||
using osrm::extractor::guidance::isLeftTurn;
|
||||
using osrm::extractor::guidance::isRightTurn;
|
||||
|
||||
|
||||
@@ -5,13 +5,13 @@
|
||||
#include "engine/guidance/assemble_steps.hpp"
|
||||
#include "engine/guidance/lane_processing.hpp"
|
||||
|
||||
#include "engine/guidance/collapsing_utility.hpp"
|
||||
#include "util/bearing.hpp"
|
||||
#include "util/guidance/name_announcements.hpp"
|
||||
#include "util/guidance/turn_lanes.hpp"
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/numeric/conversion/cast.hpp>
|
||||
#include <boost/range/algorithm_ext/erase.hpp>
|
||||
#include <boost/range/iterator_range.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
@@ -20,9 +20,6 @@
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
using TurnInstruction = osrm::extractor::guidance::TurnInstruction;
|
||||
namespace TurnType = osrm::extractor::guidance::TurnType;
|
||||
namespace DirectionModifier = osrm::extractor::guidance::DirectionModifier;
|
||||
using osrm::util::angularDeviation;
|
||||
using osrm::extractor::guidance::getTurnDirection;
|
||||
using osrm::extractor::guidance::hasRampType;
|
||||
@@ -38,67 +35,6 @@ namespace guidance
|
||||
|
||||
namespace
|
||||
{
|
||||
const constexpr std::size_t MIN_END_OF_ROAD_INTERSECTIONS = std::size_t{2};
|
||||
const constexpr double MAX_COLLAPSE_DISTANCE = 30;
|
||||
|
||||
// check if at least one of the turns is actually a maneuver
|
||||
inline bool hasManeuver(const RouteStep &first, const RouteStep &second)
|
||||
{
|
||||
return (first.maneuver.instruction.type != TurnType::Suppressed ||
|
||||
second.maneuver.instruction.type != TurnType::Suppressed) &&
|
||||
(first.maneuver.instruction.type != TurnType::NoTurn &&
|
||||
second.maneuver.instruction.type != TurnType::NoTurn);
|
||||
}
|
||||
|
||||
inline bool choiceless(const RouteStep &step, const RouteStep &previous)
|
||||
{
|
||||
// if the next turn is choiceless, we consider longer turn roads collapsable than usually
|
||||
// accepted. We might need to improve this to find out whether we merge onto a through-street.
|
||||
BOOST_ASSERT(!step.intersections.empty());
|
||||
const auto is_without_choice = previous.distance < 4 * MAX_COLLAPSE_DISTANCE &&
|
||||
1 >= std::count(step.intersections.front().entry.begin(),
|
||||
step.intersections.front().entry.end(),
|
||||
true);
|
||||
|
||||
return is_without_choice && step.maneuver.instruction.type != TurnType::EndOfRoad;
|
||||
}
|
||||
|
||||
// List of types that can be collapsed, if all other restrictions pass
|
||||
bool isCollapsableInstruction(const TurnInstruction instruction)
|
||||
{
|
||||
return instruction.type == TurnType::NewName ||
|
||||
(instruction.type == TurnType::Suppressed &&
|
||||
instruction.direction_modifier == DirectionModifier::Straight) ||
|
||||
(instruction.type == TurnType::Turn &&
|
||||
instruction.direction_modifier == DirectionModifier::Straight) ||
|
||||
(instruction.type == TurnType::Continue &&
|
||||
instruction.direction_modifier == DirectionModifier::Straight) ||
|
||||
(instruction.type == TurnType::Merge);
|
||||
}
|
||||
|
||||
bool compatible(const RouteStep &lhs, const RouteStep &rhs) { return lhs.mode == rhs.mode; }
|
||||
|
||||
// Checks if name change happens the user wants to know about.
|
||||
// Treats e.g. "Name (Ref)" -> "Name" changes still as same name.
|
||||
bool isNoticeableNameChange(const RouteStep &lhs, const RouteStep &rhs)
|
||||
{
|
||||
// TODO: rotary_name is not handled at the moment.
|
||||
return util::guidance::requiresNameAnnounced(
|
||||
lhs.name, lhs.ref, lhs.pronunciation, rhs.name, rhs.ref, rhs.pronunciation);
|
||||
}
|
||||
|
||||
double nameSegmentLength(std::size_t at, const std::vector<RouteStep> &steps)
|
||||
{
|
||||
BOOST_ASSERT(at < steps.size());
|
||||
|
||||
double result = steps[at].distance;
|
||||
while (at + 1 < steps.size() && !isNoticeableNameChange(steps[at], steps[at + 1]))
|
||||
{
|
||||
at += 1;
|
||||
result += steps[at].distance;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void fixFinalRoundabout(std::vector<RouteStep> &steps)
|
||||
{
|
||||
@@ -282,588 +218,8 @@ void closeOffRoundabout(const bool on_roundabout,
|
||||
}
|
||||
}
|
||||
|
||||
bool bearingsAreReversed(const double bearing_in, const double bearing_out)
|
||||
{
|
||||
// Nearly perfectly reversed angles have a difference close to 180 degrees (straight)
|
||||
const double left_turn_angle = [&]() {
|
||||
if (0 <= bearing_out && bearing_out <= bearing_in)
|
||||
return bearing_in - bearing_out;
|
||||
return bearing_in + 360 - bearing_out;
|
||||
}();
|
||||
return angularDeviation(left_turn_angle, 180) <= 35;
|
||||
}
|
||||
|
||||
bool isLinkroad(const RouteStep &pre_link_step,
|
||||
const RouteStep &link_step,
|
||||
const RouteStep &post_link_step)
|
||||
{
|
||||
const constexpr double MAX_LINK_ROAD_LENGTH = 60.0;
|
||||
return link_step.distance <= MAX_LINK_ROAD_LENGTH && link_step.name_id == EMPTY_NAMEID &&
|
||||
pre_link_step.name_id != EMPTY_NAMEID && post_link_step.name_id != EMPTY_NAMEID;
|
||||
}
|
||||
|
||||
bool isUTurn(const RouteStep &in_step, const RouteStep &out_step, const RouteStep &pre_in_step)
|
||||
{
|
||||
const bool is_possible_candidate =
|
||||
in_step.distance <= MAX_COLLAPSE_DISTANCE || choiceless(out_step, in_step) ||
|
||||
(isLinkroad(pre_in_step, in_step, out_step) && out_step.name_id != EMPTY_NAMEID &&
|
||||
pre_in_step.name_id != EMPTY_NAMEID && !isNoticeableNameChange(pre_in_step, out_step));
|
||||
const bool takes_u_turn = bearingsAreReversed(
|
||||
util::bearing::reverse(
|
||||
in_step.intersections.front().bearings[in_step.intersections.front().in]),
|
||||
out_step.intersections.front().bearings[out_step.intersections.front().out]);
|
||||
|
||||
return is_possible_candidate && takes_u_turn && compatible(in_step, out_step);
|
||||
}
|
||||
|
||||
double findTotalTurnAngle(const RouteStep &entry_step, const RouteStep &exit_step)
|
||||
{
|
||||
const auto exit_intersection = exit_step.intersections.front();
|
||||
const auto exit_step_exit_bearing = exit_intersection.bearings[exit_intersection.out];
|
||||
const auto exit_step_entry_bearing =
|
||||
util::bearing::reverse(exit_intersection.bearings[exit_intersection.in]);
|
||||
|
||||
const auto entry_intersection = entry_step.intersections.front();
|
||||
const auto entry_step_entry_bearing =
|
||||
util::bearing::reverse(entry_intersection.bearings[entry_intersection.in]);
|
||||
const auto entry_step_exit_bearing = entry_intersection.bearings[entry_intersection.out];
|
||||
|
||||
const auto exit_angle =
|
||||
util::bearing::angleBetween(exit_step_entry_bearing, exit_step_exit_bearing);
|
||||
const auto entry_angle =
|
||||
util::bearing::angleBetween(entry_step_entry_bearing, entry_step_exit_bearing);
|
||||
|
||||
const double total_angle =
|
||||
util::bearing::angleBetween(entry_step_entry_bearing, exit_step_exit_bearing);
|
||||
// We allow for minor deviations from a straight line
|
||||
if (((entry_step.distance < MAX_COLLAPSE_DISTANCE && exit_step.intersections.size() == 1) ||
|
||||
(entry_angle <= 185 && exit_angle <= 185) || (entry_angle >= 175 && exit_angle >= 175)) &&
|
||||
angularDeviation(total_angle, 180) > 20)
|
||||
{
|
||||
// both angles are in the same direction, the total turn gets increased
|
||||
//
|
||||
// a ---- b
|
||||
// \
|
||||
// c
|
||||
// |
|
||||
// d
|
||||
//
|
||||
// Will be considered just like
|
||||
// a -----b
|
||||
// |
|
||||
// c
|
||||
// |
|
||||
// d
|
||||
return total_angle;
|
||||
}
|
||||
else
|
||||
{
|
||||
// to prevent ignoring angles like
|
||||
// a -- b
|
||||
// |
|
||||
// c -- d
|
||||
// We don't combine both turn angles here but keep the very first turn angle.
|
||||
// We choose the first one, since we consider the first maneuver in a merge range the
|
||||
// important one
|
||||
return entry_angle;
|
||||
}
|
||||
}
|
||||
|
||||
std::size_t getPreviousIndex(std::size_t index, const std::vector<RouteStep> &steps)
|
||||
{
|
||||
BOOST_ASSERT(index > 0);
|
||||
BOOST_ASSERT(index < steps.size());
|
||||
--index;
|
||||
while (index > 0 && steps[index].maneuver.instruction.type == TurnType::NoTurn)
|
||||
--index;
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
void collapseUTurn(std::vector<RouteStep> &steps,
|
||||
const std::size_t two_back_index,
|
||||
const std::size_t one_back_index,
|
||||
const std::size_t step_index)
|
||||
{
|
||||
BOOST_ASSERT(two_back_index < steps.size());
|
||||
BOOST_ASSERT(step_index < steps.size());
|
||||
BOOST_ASSERT(one_back_index < steps.size());
|
||||
const auto ¤t_step = steps[step_index];
|
||||
|
||||
// the simple case is a u-turn that changes directly into the in-name again
|
||||
const bool direct_u_turn = !isNoticeableNameChange(steps[two_back_index], current_step);
|
||||
|
||||
// however, we might also deal with a dual-collapse scenario in which we have to
|
||||
// additionall collapse a name-change as well
|
||||
const auto next_step_index = step_index + 1;
|
||||
const bool continues_with_name_change =
|
||||
(next_step_index < steps.size()) && compatible(steps[step_index], steps[next_step_index]) &&
|
||||
((steps[next_step_index].maneuver.instruction.type == TurnType::UseLane &&
|
||||
steps[next_step_index].maneuver.instruction.direction_modifier ==
|
||||
DirectionModifier::Straight) ||
|
||||
isCollapsableInstruction(steps[next_step_index].maneuver.instruction));
|
||||
const bool u_turn_with_name_change =
|
||||
continues_with_name_change && steps[next_step_index].name_id != EMPTY_NAMEID &&
|
||||
!isNoticeableNameChange(steps[two_back_index], steps[next_step_index]);
|
||||
|
||||
if (direct_u_turn || u_turn_with_name_change)
|
||||
{
|
||||
steps[one_back_index].ElongateBy(steps[step_index]);
|
||||
steps[step_index].Invalidate();
|
||||
if (u_turn_with_name_change)
|
||||
{
|
||||
BOOST_ASSERT_MSG(compatible(steps[one_back_index], steps[next_step_index]),
|
||||
"Compatibility should be transitive");
|
||||
steps[one_back_index].ElongateBy(steps[next_step_index]);
|
||||
steps[next_step_index].Invalidate(); // will be skipped due to the
|
||||
// continue statement at the
|
||||
// beginning of this function
|
||||
}
|
||||
steps[one_back_index].AdaptStepSignage(steps[two_back_index]);
|
||||
steps[one_back_index].maneuver.instruction.type = TurnType::Continue;
|
||||
steps[one_back_index].maneuver.instruction.direction_modifier = DirectionModifier::UTurn;
|
||||
}
|
||||
}
|
||||
|
||||
void collapseTurnAt(std::vector<RouteStep> &steps,
|
||||
const std::size_t two_back_index,
|
||||
const std::size_t one_back_index,
|
||||
const std::size_t step_index)
|
||||
{
|
||||
BOOST_ASSERT(step_index < steps.size());
|
||||
BOOST_ASSERT(one_back_index < steps.size());
|
||||
const auto ¤t_step = steps[step_index];
|
||||
const auto &one_back_step = steps[one_back_index];
|
||||
// Don't collapse roundabouts
|
||||
if (entersRoundabout(current_step.maneuver.instruction) ||
|
||||
entersRoundabout(one_back_step.maneuver.instruction))
|
||||
return;
|
||||
|
||||
// This function assumes driving on the right hand side of the streat
|
||||
BOOST_ASSERT(!one_back_step.intersections.empty() && !current_step.intersections.empty());
|
||||
|
||||
if (!hasManeuver(one_back_step, current_step))
|
||||
return;
|
||||
|
||||
// A maneuver is preceded by a name change if the instruction just before can be collapsed
|
||||
// normally or the instruction itself is collapsable and does not actually present a choice
|
||||
const auto maneuverPrecededByNameChange = [](const RouteStep &turning_point,
|
||||
const RouteStep &possible_name_change_location,
|
||||
const RouteStep &preceeding_step) {
|
||||
// the check against merge is a workaround for motorways
|
||||
if (possible_name_change_location.maneuver.instruction.type == TurnType::Merge ||
|
||||
!compatible(possible_name_change_location, preceeding_step))
|
||||
return false;
|
||||
|
||||
return collapsable(possible_name_change_location, turning_point) ||
|
||||
(isCollapsableInstruction(possible_name_change_location.maneuver.instruction) &&
|
||||
choiceless(possible_name_change_location, preceeding_step));
|
||||
};
|
||||
|
||||
// check if the actual turn we wan't to announce is delayed. This situation describes a turn
|
||||
// that is expressed by two turns,
|
||||
const auto isDelayedTurn = [](
|
||||
const RouteStep &opening_turn, const RouteStep &finishing_turn, const RouteStep &pre_turn) {
|
||||
// only possible if both are compatible
|
||||
if (!compatible(opening_turn, finishing_turn))
|
||||
return false;
|
||||
else
|
||||
{
|
||||
const auto is_short_and_collapsable =
|
||||
opening_turn.distance <= MAX_COLLAPSE_DISTANCE &&
|
||||
isCollapsableInstruction(finishing_turn.maneuver.instruction);
|
||||
|
||||
const auto without_choice = choiceless(finishing_turn, opening_turn);
|
||||
|
||||
const auto is_not_too_long_and_choiceless =
|
||||
opening_turn.distance <= 2 * MAX_COLLAPSE_DISTANCE && without_choice;
|
||||
|
||||
// for ramps we allow longer stretches, since they are often on some major brides/large
|
||||
// roads. A combined distance of of 4 intersections would be to long for a normal
|
||||
// collapse. In case of a ramp though, we also account for situations that have the ramp
|
||||
// tagged late
|
||||
const auto is_delayed_turn_onto_a_ramp =
|
||||
opening_turn.distance <= 4 * MAX_COLLAPSE_DISTANCE && without_choice &&
|
||||
hasRampType(finishing_turn.maneuver.instruction);
|
||||
|
||||
const auto linkroad = isLinkroad(pre_turn, opening_turn, finishing_turn);
|
||||
|
||||
return !hasRampType(opening_turn.maneuver.instruction) &&
|
||||
(is_short_and_collapsable || is_not_too_long_and_choiceless || linkroad ||
|
||||
is_delayed_turn_onto_a_ramp);
|
||||
}
|
||||
};
|
||||
|
||||
// Handle possible u-turns
|
||||
if (isUTurn(one_back_step, current_step, steps[two_back_index]))
|
||||
collapseUTurn(steps, two_back_index, one_back_index, step_index);
|
||||
// Very Short New Name that will be suppressed. Turn location remains at current_step
|
||||
else if (maneuverPrecededByNameChange(current_step, one_back_step, steps[two_back_index]))
|
||||
{
|
||||
BOOST_ASSERT(two_back_index < steps.size());
|
||||
BOOST_ASSERT(!one_back_step.intersections.empty());
|
||||
if (TurnType::Merge == current_step.maneuver.instruction.type)
|
||||
{
|
||||
steps[step_index].maneuver.instruction.direction_modifier =
|
||||
mirrorDirectionModifier(steps[step_index].maneuver.instruction.direction_modifier);
|
||||
steps[step_index].maneuver.instruction.type = TurnType::Turn;
|
||||
}
|
||||
else
|
||||
{
|
||||
const bool continue_or_suppressed =
|
||||
(TurnType::Continue == current_step.maneuver.instruction.type ||
|
||||
(TurnType::Suppressed == current_step.maneuver.instruction.type &&
|
||||
current_step.maneuver.instruction.direction_modifier !=
|
||||
DirectionModifier::Straight));
|
||||
|
||||
const bool turning_name =
|
||||
(TurnType::NewName == current_step.maneuver.instruction.type &&
|
||||
current_step.maneuver.instruction.direction_modifier !=
|
||||
DirectionModifier::Straight &&
|
||||
one_back_step.intersections.front().bearings.size() > 2);
|
||||
|
||||
if (continue_or_suppressed)
|
||||
steps[step_index].maneuver.instruction.type = TurnType::Turn;
|
||||
else if (turning_name)
|
||||
steps[step_index].maneuver.instruction.type = TurnType::Turn;
|
||||
else if (TurnType::UseLane == current_step.maneuver.instruction.type &&
|
||||
current_step.maneuver.instruction.direction_modifier !=
|
||||
DirectionModifier::Straight &&
|
||||
one_back_step.intersections.front().bearings.size() > 2)
|
||||
steps[step_index].maneuver.instruction.type = TurnType::Turn;
|
||||
|
||||
// A new name with a continue/turning suppressed/name requires the adaption of the
|
||||
// direction modifier. The combination of the in-bearing and the out bearing gives the
|
||||
// new modifier for the turn
|
||||
if (continue_or_suppressed || turning_name)
|
||||
{
|
||||
const auto in_bearing = [](const RouteStep &step) {
|
||||
return util::bearing::reverse(
|
||||
step.intersections.front().bearings[step.intersections.front().in]);
|
||||
};
|
||||
const auto out_bearing = [](const RouteStep &step) {
|
||||
return step.intersections.front().bearings[step.intersections.front().out];
|
||||
};
|
||||
|
||||
const auto first_angle = util::bearing::angleBetween(in_bearing(one_back_step),
|
||||
out_bearing(one_back_step));
|
||||
const auto second_angle = util::bearing::angleBetween(in_bearing(current_step),
|
||||
out_bearing(current_step));
|
||||
const auto bearing_turn_angle = util::bearing::angleBetween(
|
||||
in_bearing(one_back_step), out_bearing(current_step));
|
||||
|
||||
// When looking at an intersection, some angles, even though present, feel more like
|
||||
// a straight turn. This happens most often at segregated intersections.
|
||||
// We consider two cases
|
||||
// I) a shift in the road:
|
||||
//
|
||||
// a g h
|
||||
// . | |
|
||||
// b ---- c
|
||||
// | | .
|
||||
// f e d
|
||||
//
|
||||
// Where a-d technicall continues straight, even though the shift models it as a
|
||||
// slight left and a slight right turn.
|
||||
//
|
||||
// II) A curved road
|
||||
//
|
||||
// g h
|
||||
// | |
|
||||
// b ---- c
|
||||
// . | | .
|
||||
// a f e d
|
||||
//
|
||||
// where a-d is a curve passing by an intersection.
|
||||
//
|
||||
// We distinguish this case from other bearings though where the interpretation as
|
||||
// straight would end up disguising turns.
|
||||
|
||||
// check if there is another similar turn next to the turn itself
|
||||
const auto hasSimilarAngle = [&](const std::size_t index,
|
||||
const std::vector<short> &bearings) {
|
||||
return (angularDeviation(bearings[index],
|
||||
bearings[(index + 1) % bearings.size()]) <
|
||||
extractor::guidance::NARROW_TURN_ANGLE) ||
|
||||
(angularDeviation(
|
||||
bearings[index],
|
||||
bearings[(index + bearings.size() - 1) % bearings.size()]) <
|
||||
extractor::guidance::NARROW_TURN_ANGLE);
|
||||
};
|
||||
|
||||
const auto is_shift_or_curve = [&]() -> bool {
|
||||
// since we move an intersection modifier from a slight turn to a straight, we
|
||||
// need to make sure that there is not a similar angle which could prevent this
|
||||
// perception of angles to be true.
|
||||
if (hasSimilarAngle(one_back_step.intersections.front().in,
|
||||
one_back_step.intersections.front().bearings) ||
|
||||
hasSimilarAngle(current_step.intersections.front().out,
|
||||
current_step.intersections.front().bearings))
|
||||
return false;
|
||||
|
||||
// Check if we are on a potential curve, both angles go in the same direction
|
||||
if (angularDeviation(first_angle, second_angle) <
|
||||
extractor::guidance::FUZZY_ANGLE_DIFFERENCE)
|
||||
{
|
||||
// We limit perceptive angles to narrow turns. If the total turn is going to
|
||||
// be not-narrow, we assume it to be more than a simple curve.
|
||||
return angularDeviation(bearing_turn_angle,
|
||||
extractor::guidance::STRAIGHT_ANGLE) <
|
||||
extractor::guidance::NARROW_TURN_ANGLE;
|
||||
}
|
||||
// if one of the angles is a left turn and the other one is a right turn, we
|
||||
// nearly reverse the angle
|
||||
else if ((first_angle > extractor::guidance::STRAIGHT_ANGLE) !=
|
||||
(second_angle > extractor::guidance::STRAIGHT_ANGLE))
|
||||
{
|
||||
// since we are not in a curve, we can check for a shift. If we are going
|
||||
// nearly straight, we call it a shift.
|
||||
return angularDeviation(bearing_turn_angle,
|
||||
extractor::guidance::STRAIGHT_ANGLE) <
|
||||
extractor::guidance::NARROW_TURN_ANGLE;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}();
|
||||
|
||||
// if the angles continue similar, it looks like we might be in a normal curve
|
||||
if (is_shift_or_curve)
|
||||
steps[step_index].maneuver.instruction.direction_modifier =
|
||||
DirectionModifier::Straight;
|
||||
else
|
||||
steps[step_index].maneuver.instruction.direction_modifier =
|
||||
getTurnDirection(bearing_turn_angle);
|
||||
|
||||
// if the total direction of this turn is now straight, we can keep it suppressed/as
|
||||
// a new name. Else we have to interpret it as a turn.
|
||||
if (!is_shift_or_curve)
|
||||
steps[step_index].maneuver.instruction.type = TurnType::Turn;
|
||||
else
|
||||
steps[step_index].maneuver.instruction.type = TurnType::NewName;
|
||||
}
|
||||
else
|
||||
{
|
||||
const auto total_angle = findTotalTurnAngle(steps[one_back_index], current_step);
|
||||
steps[step_index].maneuver.instruction.direction_modifier =
|
||||
getTurnDirection(total_angle);
|
||||
}
|
||||
}
|
||||
|
||||
steps[two_back_index].ElongateBy(one_back_step);
|
||||
// If the previous instruction asked to continue, the name change will have to
|
||||
// be changed into a turn
|
||||
steps[one_back_index].Invalidate();
|
||||
}
|
||||
// very short segment after turn, turn location remains at one_back_step
|
||||
else if (isDelayedTurn(
|
||||
one_back_step, current_step, steps[two_back_index])) // checks for compatibility
|
||||
{
|
||||
steps[one_back_index].ElongateBy(steps[step_index]);
|
||||
// TODO check for lanes (https://github.com/Project-OSRM/osrm-backend/issues/2553)
|
||||
if (TurnType::Continue == one_back_step.maneuver.instruction.type &&
|
||||
isNoticeableNameChange(steps[two_back_index], current_step))
|
||||
{
|
||||
if (current_step.maneuver.instruction.type == TurnType::OnRamp ||
|
||||
current_step.maneuver.instruction.type == TurnType::OffRamp)
|
||||
steps[one_back_index].maneuver.instruction.type =
|
||||
current_step.maneuver.instruction.type;
|
||||
else
|
||||
steps[one_back_index].maneuver.instruction.type = TurnType::Turn;
|
||||
}
|
||||
else if (TurnType::Turn == one_back_step.maneuver.instruction.type &&
|
||||
!isNoticeableNameChange(steps[two_back_index], current_step))
|
||||
{
|
||||
steps[one_back_index].maneuver.instruction.type = TurnType::Continue;
|
||||
|
||||
const auto getBearing = [](bool in, const RouteStep &step) {
|
||||
const auto index =
|
||||
in ? step.intersections.front().in : step.intersections.front().out;
|
||||
return step.intersections.front().bearings[index];
|
||||
};
|
||||
|
||||
// If we Merge onto the same street, we end up with a u-turn in some cases
|
||||
if (bearingsAreReversed(util::bearing::reverse(getBearing(true, one_back_step)),
|
||||
getBearing(false, current_step)))
|
||||
{
|
||||
steps[one_back_index].maneuver.instruction.direction_modifier =
|
||||
DirectionModifier::UTurn;
|
||||
}
|
||||
steps[one_back_index].AdaptStepSignage(current_step);
|
||||
}
|
||||
else if (TurnType::NewName == one_back_step.maneuver.instruction.type ||
|
||||
(TurnType::NewName == current_step.maneuver.instruction.type &&
|
||||
steps[one_back_index].maneuver.instruction.type == TurnType::Suppressed))
|
||||
steps[one_back_index].maneuver.instruction.type = TurnType::Turn;
|
||||
|
||||
if (TurnType::Merge == one_back_step.maneuver.instruction.type &&
|
||||
current_step.maneuver.instruction.type !=
|
||||
TurnType::Suppressed) // This suppressed is a check for highways. We might
|
||||
// need a highway-suppressed to get the turn onto a
|
||||
// highway...
|
||||
{
|
||||
steps[one_back_index].maneuver.instruction.direction_modifier = mirrorDirectionModifier(
|
||||
steps[one_back_index].maneuver.instruction.direction_modifier);
|
||||
}
|
||||
// on non merge-types, we check for a combined turn angle
|
||||
else if (TurnType::Merge != one_back_step.maneuver.instruction.type)
|
||||
{
|
||||
const auto combined_angle = findTotalTurnAngle(one_back_step, current_step);
|
||||
steps[one_back_index].maneuver.instruction.direction_modifier =
|
||||
getTurnDirection(combined_angle);
|
||||
}
|
||||
|
||||
steps[one_back_index].name = current_step.name;
|
||||
steps[one_back_index].name_id = current_step.name_id;
|
||||
steps[step_index].Invalidate();
|
||||
}
|
||||
else if (TurnType::Suppressed == current_step.maneuver.instruction.type &&
|
||||
!isNoticeableNameChange(one_back_step, current_step) &&
|
||||
compatible(one_back_step, current_step))
|
||||
{
|
||||
steps[one_back_index].ElongateBy(current_step);
|
||||
const auto angle = findTotalTurnAngle(one_back_step, current_step);
|
||||
steps[one_back_index].maneuver.instruction.direction_modifier = getTurnDirection(angle);
|
||||
steps[step_index].Invalidate();
|
||||
}
|
||||
else if (TurnType::Turn == one_back_step.maneuver.instruction.type &&
|
||||
TurnType::OnRamp == current_step.maneuver.instruction.type &&
|
||||
compatible(one_back_step, current_step))
|
||||
{
|
||||
// turning onto a ramp makes the first turn into a ramp
|
||||
steps[one_back_index].ElongateBy(current_step);
|
||||
steps[one_back_index].maneuver.instruction.type = TurnType::OnRamp;
|
||||
const auto angle = findTotalTurnAngle(one_back_step, current_step);
|
||||
steps[one_back_index].maneuver.instruction.direction_modifier = getTurnDirection(angle);
|
||||
|
||||
steps[one_back_index].AdaptStepSignage(current_step);
|
||||
steps[step_index].Invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
// Staggered intersection are very short zig-zags of a few meters.
|
||||
// We do not want to announce these short left-rights or right-lefts:
|
||||
//
|
||||
// * -> b a -> *
|
||||
// | or | becomes a -> b
|
||||
// a -> * * -> b
|
||||
//
|
||||
bool isStaggeredIntersection(const std::vector<RouteStep> &steps,
|
||||
const std::size_t ¤t_index,
|
||||
const std::size_t &previous_index)
|
||||
{
|
||||
const RouteStep previous = steps[previous_index];
|
||||
const RouteStep current = steps[current_index];
|
||||
|
||||
// don't touch roundabouts
|
||||
if (entersRoundabout(previous.maneuver.instruction) ||
|
||||
entersRoundabout(current.maneuver.instruction))
|
||||
return false;
|
||||
// Base decision on distance since the zig-zag is a visual clue.
|
||||
// If adjusted, make sure to check validity of the is_right/is_left classification below
|
||||
const constexpr auto MAX_STAGGERED_DISTANCE = 3; // debatable, but keep short to be on safe side
|
||||
|
||||
const auto angle = [](const RouteStep &step) {
|
||||
const auto &intersection = step.intersections.front();
|
||||
const auto entry_bearing = intersection.bearings[intersection.in];
|
||||
const auto exit_bearing = intersection.bearings[intersection.out];
|
||||
return util::bearing::angleBetween(entry_bearing, exit_bearing);
|
||||
};
|
||||
|
||||
// Instead of using turn modifiers (e.g. as in isRightTurn) we want to be more strict here.
|
||||
// We do not want to trigger e.g. on sharp uturn'ish turns or going straight "turns".
|
||||
// Therefore we use the turn angle to derive 90 degree'ish right / left turns.
|
||||
// This more closely resembles what we understand as Staggered Intersection.
|
||||
// We have to be careful in cases with larger MAX_STAGGERED_DISTANCE values. If the distance
|
||||
// gets large, sharper angles might be not obvious enough to consider them a staggered
|
||||
// intersection. We might need to consider making the decision here dependent on the actual turn
|
||||
// angle taken. To do so, we could scale the angle-limits by a factor depending on the distance
|
||||
// between the turns.
|
||||
const auto is_right = [](const double angle) { return angle > 45 && angle < 135; };
|
||||
const auto is_left = [](const double angle) { return angle > 225 && angle < 315; };
|
||||
|
||||
const auto left_right = is_left(angle(previous)) && is_right(angle(current));
|
||||
const auto right_left = is_right(angle(previous)) && is_left(angle(current));
|
||||
|
||||
// A RouteStep holds distance/duration from the maneuver to the subsequent step.
|
||||
// We are only interested in the distance between the first and the second.
|
||||
const auto is_short = previous.distance < MAX_STAGGERED_DISTANCE;
|
||||
|
||||
auto intermediary_mode_change = false;
|
||||
if (current_index > 1)
|
||||
{
|
||||
const auto &two_back_index = getPreviousIndex(previous_index, steps);
|
||||
const auto two_back_step = steps[two_back_index];
|
||||
intermediary_mode_change =
|
||||
two_back_step.mode == current.mode && previous.mode != current.mode;
|
||||
}
|
||||
|
||||
// previous step maneuver intersections should be length 1 to indicate that
|
||||
// there are no intersections between the two potentially collapsible turns
|
||||
const auto no_intermediary_intersections = previous.intersections.size() == 1;
|
||||
|
||||
return is_short && (left_right || right_left) && !intermediary_mode_change &&
|
||||
no_intermediary_intersections;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// A check whether two instructions can be treated as one. This is only the case for very short
|
||||
// maneuvers that can, in some form, be seen as one. Lookahead of one step.
|
||||
bool collapsable(const RouteStep &step, const RouteStep &next)
|
||||
{
|
||||
const auto is_short_step = step.distance < MAX_COLLAPSE_DISTANCE;
|
||||
const auto instruction_can_be_collapsed = isCollapsableInstruction(step.maneuver.instruction);
|
||||
|
||||
const auto is_use_lane = step.maneuver.instruction.type == TurnType::UseLane;
|
||||
const auto lanes_dont_change =
|
||||
step.intersections.front().lanes == next.intersections.front().lanes;
|
||||
|
||||
if (is_short_step && instruction_can_be_collapsed)
|
||||
return true;
|
||||
|
||||
// Prevent collapsing away important lane change steps
|
||||
if (is_short_step && is_use_lane && lanes_dont_change)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Post processing can invalidate some instructions. For example StayOnRoundabout
|
||||
// is turned into exit counts. These instructions are removed by the following function
|
||||
|
||||
std::vector<RouteStep> removeNoTurnInstructions(std::vector<RouteStep> steps)
|
||||
{
|
||||
// finally clean up the post-processed instructions.
|
||||
// Remove all invalid instructions from the set of instructions.
|
||||
// An instruction is invalid, if its NO_TURN and has WaypointType::None.
|
||||
// Two valid NO_TURNs exist in each leg in the form of Depart/Arrive
|
||||
|
||||
// keep valid instructions
|
||||
const auto not_is_valid = [](const RouteStep &step) {
|
||||
return step.maneuver.instruction == TurnInstruction::NO_TURN() &&
|
||||
step.maneuver.waypoint_type == WaypointType::None;
|
||||
};
|
||||
|
||||
boost::remove_erase_if(steps, not_is_valid);
|
||||
|
||||
// the steps should still include depart and arrive at least
|
||||
BOOST_ASSERT(steps.size() >= 2);
|
||||
|
||||
BOOST_ASSERT(steps.front().intersections.size() >= 1);
|
||||
BOOST_ASSERT(steps.front().intersections.front().bearings.size() == 1);
|
||||
BOOST_ASSERT(steps.front().intersections.front().entry.size() == 1);
|
||||
BOOST_ASSERT(steps.front().maneuver.waypoint_type == WaypointType::Depart);
|
||||
|
||||
BOOST_ASSERT(steps.back().intersections.size() == 1);
|
||||
BOOST_ASSERT(steps.back().intersections.front().bearings.size() == 1);
|
||||
BOOST_ASSERT(steps.back().intersections.front().entry.size() == 1);
|
||||
BOOST_ASSERT(steps.back().maneuver.waypoint_type == WaypointType::Arrive);
|
||||
|
||||
return steps;
|
||||
}
|
||||
|
||||
// Every Step Maneuver consists of the information until the turn.
|
||||
// This list contains a set of instructions, called silent, which should
|
||||
// not be part of the final output.
|
||||
@@ -942,259 +298,6 @@ std::vector<RouteStep> postProcess(std::vector<RouteStep> steps)
|
||||
return removeNoTurnInstructions(std::move(steps));
|
||||
}
|
||||
|
||||
// Post Processing to collapse unnecessary sets of combined instructions into a single one
|
||||
std::vector<RouteStep> collapseTurns(std::vector<RouteStep> steps)
|
||||
{
|
||||
if (steps.size() <= 2)
|
||||
return steps;
|
||||
|
||||
const auto getPreviousNameIndex = [&steps](std::size_t index) {
|
||||
BOOST_ASSERT(index > 0);
|
||||
BOOST_ASSERT(index < steps.size());
|
||||
--index; // make sure to skip the current name
|
||||
while (index > 0 && steps[index].name_id == EMPTY_NAMEID)
|
||||
{
|
||||
--index;
|
||||
}
|
||||
return index;
|
||||
};
|
||||
|
||||
// a series of turns is only possible to collapse if its only name changes and suppressed turns.
|
||||
const auto canCollapseAll = [&steps](std::size_t index, const std::size_t end_index) {
|
||||
BOOST_ASSERT(end_index <= steps.size());
|
||||
if (!compatible(steps[index], steps[index + 1]))
|
||||
return false;
|
||||
++index;
|
||||
for (; index < end_index; ++index)
|
||||
{
|
||||
if (steps[index].maneuver.instruction.type != TurnType::Suppressed &&
|
||||
steps[index].maneuver.instruction.type != TurnType::NewName)
|
||||
return false;
|
||||
if (index + 1 < end_index && !compatible(steps[index], steps[index + 1]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
// first and last instructions are waypoints that cannot be collapsed
|
||||
for (std::size_t step_index = 1; step_index + 1 < steps.size(); ++step_index)
|
||||
{
|
||||
const auto ¤t_step = steps[step_index];
|
||||
const auto next_step_index = step_index + 1;
|
||||
const auto one_back_index = getPreviousIndex(step_index, steps);
|
||||
|
||||
BOOST_ASSERT(one_back_index < steps.size());
|
||||
|
||||
const auto &one_back_step = steps[one_back_index];
|
||||
if (hasRoundaboutType(current_step.maneuver.instruction) ||
|
||||
hasRoundaboutType(one_back_step.maneuver.instruction))
|
||||
continue;
|
||||
|
||||
if (!hasManeuver(one_back_step, current_step))
|
||||
continue;
|
||||
|
||||
// how long has a name change to be so that we announce it, even as a bridge?
|
||||
const constexpr auto name_segment_cutoff_length = 100;
|
||||
const auto isBasicNameChange = [](const RouteStep &step) {
|
||||
return step.intersections.size() == 1 &&
|
||||
step.intersections.front().bearings.size() == 2 &&
|
||||
DirectionModifier::Straight == step.maneuver.instruction.direction_modifier;
|
||||
};
|
||||
|
||||
// Handle sliproads from motorways in urban areas, save from modifying depart, since
|
||||
// TurnType::Sliproad != TurnType::NoTurn
|
||||
if (one_back_step.maneuver.instruction.type == TurnType::Sliproad)
|
||||
{
|
||||
if (current_step.maneuver.instruction.type == TurnType::Suppressed &&
|
||||
compatible(one_back_step, current_step) && current_step.intersections.size() == 1 &&
|
||||
current_step.intersections.front().entry.size() == 2)
|
||||
{
|
||||
// Traffic light on the sliproad, the road itself will be handled in the next
|
||||
// iteration, when one-back-index again points to the sliproad.
|
||||
steps[one_back_index].ElongateBy(steps[step_index]);
|
||||
steps[step_index].Invalidate();
|
||||
}
|
||||
else
|
||||
{
|
||||
// Handle possible u-turns between highways that look like slip-roads
|
||||
if (steps[getPreviousIndex(one_back_index, steps)].name_id ==
|
||||
steps[step_index].name_id &&
|
||||
steps[step_index].name_id != EMPTY_NAMEID)
|
||||
{
|
||||
steps[one_back_index].maneuver.instruction.type = TurnType::Continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
steps[one_back_index].maneuver.instruction.type = TurnType::Turn;
|
||||
}
|
||||
if (compatible(one_back_step, current_step))
|
||||
{
|
||||
// Turn Types in the response depend on whether we find the same road name
|
||||
// (sliproad indcating a u-turn) or if we are turning onto a different road, in
|
||||
// which case we use a turn.
|
||||
if (!isNoticeableNameChange(steps[getPreviousIndex(one_back_index, steps)],
|
||||
current_step) &&
|
||||
current_step.name_id != EMPTY_NAMEID)
|
||||
steps[one_back_index].maneuver.instruction.type = TurnType::Continue;
|
||||
else
|
||||
steps[one_back_index].maneuver.instruction.type = TurnType::Turn;
|
||||
|
||||
steps[one_back_index].ElongateBy(steps[step_index]);
|
||||
|
||||
steps[one_back_index].AdaptStepSignage(steps[step_index]);
|
||||
// the turn lanes for this turn are on the sliproad itself, so we have to
|
||||
// remember them
|
||||
steps[one_back_index].intersections.front().lanes =
|
||||
current_step.intersections.front().lanes;
|
||||
steps[one_back_index].intersections.front().lane_description =
|
||||
current_step.intersections.front().lane_description;
|
||||
|
||||
const auto angle = findTotalTurnAngle(one_back_step, current_step);
|
||||
steps[one_back_index].maneuver.instruction.direction_modifier =
|
||||
getTurnDirection(angle);
|
||||
steps[step_index].Invalidate();
|
||||
}
|
||||
else
|
||||
{
|
||||
// the sliproad turn is incompatible. So we handle it as a turn
|
||||
steps[one_back_index].maneuver.instruction.type = TurnType::Turn;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Due to empty segments, we can get name-changes from A->A
|
||||
// These have to be handled in post-processing
|
||||
else if (isCollapsableInstruction(current_step.maneuver.instruction) &&
|
||||
current_step.maneuver.instruction.type != TurnType::Suppressed &&
|
||||
!isNoticeableNameChange(steps[getPreviousNameIndex(step_index)], current_step) &&
|
||||
// canCollapseAll is also checking for compatible(step,step+1) for all indices
|
||||
canCollapseAll(getPreviousNameIndex(step_index), next_step_index))
|
||||
{
|
||||
BOOST_ASSERT(step_index > 0);
|
||||
const std::size_t last_available_name_index = getPreviousNameIndex(step_index);
|
||||
|
||||
for (std::size_t index = last_available_name_index + 1; index <= step_index; ++index)
|
||||
{
|
||||
steps[last_available_name_index].ElongateBy(steps[index]);
|
||||
steps[index].Invalidate();
|
||||
}
|
||||
}
|
||||
// If we look at two consecutive name changes, we can check for a name oscillation.
|
||||
// A name oscillation changes from name A shortly to name B and back to A.
|
||||
// In these cases, the name change will be suppressed.
|
||||
else if (one_back_index > 0 && compatible(current_step, one_back_step) &&
|
||||
((isCollapsableInstruction(current_step.maneuver.instruction) &&
|
||||
isCollapsableInstruction(one_back_step.maneuver.instruction)) ||
|
||||
isStaggeredIntersection(steps, step_index, one_back_index)))
|
||||
{
|
||||
const auto two_back_index = getPreviousIndex(one_back_index, steps);
|
||||
BOOST_ASSERT(two_back_index < steps.size());
|
||||
// valid, since one_back is collapsable or a turn and therefore not depart:
|
||||
if (!isNoticeableNameChange(steps[two_back_index], current_step))
|
||||
{
|
||||
if (compatible(one_back_step, steps[two_back_index]))
|
||||
{
|
||||
steps[two_back_index]
|
||||
.ElongateBy(steps[one_back_index])
|
||||
.ElongateBy(steps[step_index]);
|
||||
steps[one_back_index].Invalidate();
|
||||
steps[step_index].Invalidate();
|
||||
}
|
||||
// TODO discuss: we could think about changing the new-name to a pure notification
|
||||
// about mode changes
|
||||
}
|
||||
else if (nameSegmentLength(one_back_index, steps) < name_segment_cutoff_length &&
|
||||
isBasicNameChange(one_back_step) && isBasicNameChange(current_step))
|
||||
{
|
||||
if (compatible(steps[two_back_index], steps[one_back_index]))
|
||||
{
|
||||
steps[two_back_index].ElongateBy(steps[one_back_index]);
|
||||
steps[one_back_index].Invalidate();
|
||||
if (nameSegmentLength(step_index, steps) < name_segment_cutoff_length &&
|
||||
compatible(steps[two_back_index], steps[step_index]))
|
||||
{
|
||||
steps[two_back_index].ElongateBy(steps[step_index]);
|
||||
steps[step_index].Invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (step_index + 2 < steps.size() &&
|
||||
current_step.maneuver.instruction.type == TurnType::NewName &&
|
||||
steps[next_step_index].maneuver.instruction.type == TurnType::NewName &&
|
||||
!isNoticeableNameChange(one_back_step, steps[next_step_index]))
|
||||
{
|
||||
if (compatible(steps[step_index], steps[next_step_index]))
|
||||
{
|
||||
// if we are crossing an intersection and go immediately after into a name
|
||||
// change,
|
||||
// we don't wan't to collapse the initial intersection.
|
||||
// a - b ---BRIDGE -- c
|
||||
steps[one_back_index]
|
||||
.ElongateBy(steps[step_index])
|
||||
.ElongateBy(steps[next_step_index]);
|
||||
steps[step_index].Invalidate();
|
||||
steps[next_step_index].Invalidate();
|
||||
}
|
||||
}
|
||||
else if (choiceless(current_step, one_back_step) ||
|
||||
one_back_step.distance <= MAX_COLLAPSE_DISTANCE)
|
||||
{
|
||||
// check for one of the multiple collapse scenarios and, if possible, collapse the
|
||||
// turn
|
||||
const auto two_back_index = getPreviousIndex(one_back_index, steps);
|
||||
BOOST_ASSERT(two_back_index < steps.size());
|
||||
collapseTurnAt(steps, two_back_index, one_back_index, step_index);
|
||||
}
|
||||
}
|
||||
else if (one_back_index > 0 &&
|
||||
(one_back_step.distance <= MAX_COLLAPSE_DISTANCE ||
|
||||
choiceless(current_step, one_back_step) ||
|
||||
isLinkroad(
|
||||
steps[getPreviousIndex(one_back_index, steps)], one_back_step, current_step)))
|
||||
{
|
||||
// check for one of the multiple collapse scenarios and, if possible, collapse the turn
|
||||
const auto two_back_index = getPreviousIndex(one_back_index, steps);
|
||||
BOOST_ASSERT(two_back_index < steps.size());
|
||||
// all turns that are handled lower down are also compatible
|
||||
collapseTurnAt(steps, two_back_index, one_back_index, step_index);
|
||||
}
|
||||
|
||||
if (steps[step_index].maneuver.instruction.type == TurnType::Turn)
|
||||
{
|
||||
const auto u_turn_one_back_index = getPreviousIndex(step_index, steps);
|
||||
if (u_turn_one_back_index > 0)
|
||||
{
|
||||
const auto u_turn_two_back_index = getPreviousIndex(u_turn_one_back_index, steps);
|
||||
if (isUTurn(steps[u_turn_one_back_index],
|
||||
steps[step_index],
|
||||
steps[u_turn_two_back_index]))
|
||||
{
|
||||
collapseUTurn(steps, u_turn_two_back_index, u_turn_one_back_index, step_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handle final sliproad
|
||||
if (steps.size() >= 3 &&
|
||||
steps[getPreviousIndex(steps.size() - 1, steps)].maneuver.instruction.type ==
|
||||
TurnType::Sliproad)
|
||||
{
|
||||
steps[getPreviousIndex(steps.size() - 1, steps)].maneuver.instruction.type = TurnType::Turn;
|
||||
}
|
||||
|
||||
BOOST_ASSERT(steps.front().intersections.size() >= 1);
|
||||
BOOST_ASSERT(steps.front().intersections.front().bearings.size() == 1);
|
||||
BOOST_ASSERT(steps.front().intersections.front().entry.size() == 1);
|
||||
BOOST_ASSERT(steps.front().maneuver.waypoint_type == WaypointType::Depart);
|
||||
|
||||
BOOST_ASSERT(steps.back().intersections.size() == 1);
|
||||
BOOST_ASSERT(steps.back().intersections.front().bearings.size() == 1);
|
||||
BOOST_ASSERT(steps.back().intersections.front().entry.size() == 1);
|
||||
BOOST_ASSERT(steps.back().maneuver.waypoint_type == WaypointType::Arrive);
|
||||
|
||||
return removeNoTurnInstructions(std::move(steps));
|
||||
}
|
||||
|
||||
// Doing this step in post-processing provides a few challenges we cannot overcome.
|
||||
// The removal of an initial step imposes some copy overhead in the steps, moving all later
|
||||
// steps to the front. In addition, we cannot reduce the travel time that is accumulated at a
|
||||
@@ -1483,7 +586,7 @@ std::vector<RouteStep> buildIntersections(std::vector<RouteStep> steps)
|
||||
const auto instruction = step.maneuver.instruction;
|
||||
if (instruction.type == TurnType::Suppressed)
|
||||
{
|
||||
BOOST_ASSERT(compatible(steps[last_valid_instruction], step));
|
||||
BOOST_ASSERT(steps[last_valid_instruction].mode == step.mode);
|
||||
// count intersections. We cannot use exit, since intersections can follow directly
|
||||
// after a roundabout
|
||||
steps[last_valid_instruction].ElongateBy(step);
|
||||
@@ -1514,51 +617,6 @@ std::vector<RouteStep> buildIntersections(std::vector<RouteStep> steps)
|
||||
return removeNoTurnInstructions(std::move(steps));
|
||||
}
|
||||
|
||||
// `useLane` steps are only returned on `straight` maneuvers when there
|
||||
// are surrounding lanes also tagged as `straight`. If there are no other `straight`
|
||||
// lanes, it is not an ambiguous maneuver, and we can collapse the `useLane` step.
|
||||
std::vector<RouteStep> collapseUseLane(std::vector<RouteStep> steps)
|
||||
{
|
||||
const auto containsTag = [](const extractor::guidance::TurnLaneType::Mask mask,
|
||||
const extractor::guidance::TurnLaneType::Mask tag) {
|
||||
return (mask & tag) != extractor::guidance::TurnLaneType::empty;
|
||||
};
|
||||
|
||||
const auto canCollapseUseLane = [containsTag](const RouteStep &step) {
|
||||
// the lane description is given left to right, lanes are counted from the right.
|
||||
// Therefore we access the lane description using the reverse iterator
|
||||
|
||||
auto right_most_lanes = step.LanesToTheRight();
|
||||
if (!right_most_lanes.empty() && containsTag(right_most_lanes.front(),
|
||||
(extractor::guidance::TurnLaneType::straight |
|
||||
extractor::guidance::TurnLaneType::none)))
|
||||
return false;
|
||||
|
||||
auto left_most_lanes = step.LanesToTheLeft();
|
||||
if (!left_most_lanes.empty() && containsTag(left_most_lanes.back(),
|
||||
(extractor::guidance::TurnLaneType::straight |
|
||||
extractor::guidance::TurnLaneType::none)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
for (std::size_t step_index = 1; step_index < steps.size(); ++step_index)
|
||||
{
|
||||
const auto &step = steps[step_index];
|
||||
if (step.maneuver.instruction.type == TurnType::UseLane && canCollapseUseLane(step))
|
||||
{
|
||||
const auto previous = getPreviousIndex(step_index, steps);
|
||||
if (compatible(steps[previous], step))
|
||||
{
|
||||
steps[previous].ElongateBy(steps[step_index]);
|
||||
steps[step_index].Invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
return removeNoTurnInstructions(std::move(steps));
|
||||
}
|
||||
|
||||
} // namespace guidance
|
||||
} // namespace engine
|
||||
} // namespace osrm
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
#include "engine/guidance/verbosity_reduction.hpp"
|
||||
#include "engine/guidance/collapsing_utility.hpp"
|
||||
|
||||
#include <boost/assert.hpp>
|
||||
#include <iterator>
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace engine
|
||||
{
|
||||
namespace guidance
|
||||
{
|
||||
std::vector<RouteStep> suppressShortNameSegments(std::vector<RouteStep> steps)
|
||||
{
|
||||
// guard against empty routes, even though they shouldn't happen
|
||||
if (steps.empty())
|
||||
return steps;
|
||||
|
||||
// we remove only name changes that don't offer additional information
|
||||
const auto name_change_without_lanes = [](const RouteStep &step) {
|
||||
return hasTurnType(step, TurnType::NewName) && !hasLanes(step);
|
||||
};
|
||||
|
||||
// check if the next step is not important enough to announce
|
||||
const auto can_be_extended_to = [](const RouteStep &step) {
|
||||
const auto is_not_arrive = !hasWaypointType(step);
|
||||
const auto is_silent = !hasTurnType(step) || hasTurnType(step, TurnType::Suppressed);
|
||||
|
||||
return is_not_arrive && is_silent;
|
||||
};
|
||||
|
||||
const auto suppress = [](RouteStep &from_step, RouteStep &onto_step) {
|
||||
from_step.ElongateBy(onto_step);
|
||||
onto_step.Invalidate();
|
||||
};
|
||||
|
||||
// suppresses name segments that announce already known names or announce a name that will be
|
||||
// only available for a very short time
|
||||
const auto reduce_verbosity_if_possible = [suppress, can_be_extended_to](
|
||||
RouteStepIterator ¤t_turn_itr, RouteStepIterator &previous_turn_itr) {
|
||||
if (haveSameName(*previous_turn_itr, *current_turn_itr))
|
||||
suppress(*previous_turn_itr, *current_turn_itr);
|
||||
else
|
||||
{
|
||||
// remember the location of the name change so we can advance the previous turn
|
||||
const auto location_of_name_change = current_turn_itr;
|
||||
auto distance = current_turn_itr->distance;
|
||||
// sum up all distances that can be relevant to the name change
|
||||
while (can_be_extended_to(*(current_turn_itr + 1)) &&
|
||||
distance < NAME_SEGMENT_CUTOFF_LENGTH)
|
||||
{
|
||||
++current_turn_itr;
|
||||
distance += current_turn_itr->distance;
|
||||
}
|
||||
|
||||
if (distance < NAME_SEGMENT_CUTOFF_LENGTH)
|
||||
suppress(*previous_turn_itr, *current_turn_itr);
|
||||
else
|
||||
previous_turn_itr = location_of_name_change;
|
||||
}
|
||||
};
|
||||
|
||||
BOOST_ASSERT(!hasTurnType(steps.back()) && hasWaypointType(steps.back()));
|
||||
for (auto previous_turn_itr = steps.begin(), current_turn_itr = std::next(previous_turn_itr);
|
||||
!hasWaypointType(*current_turn_itr);
|
||||
++current_turn_itr)
|
||||
{
|
||||
BOOST_ASSERT(hasTurnType(*current_turn_itr) &&
|
||||
!hasTurnType(*current_turn_itr, TurnType::Suppressed));
|
||||
if (name_change_without_lanes(*current_turn_itr) &&
|
||||
haveSameMode(*previous_turn_itr, *current_turn_itr))
|
||||
{
|
||||
// check if the name can be reduced, also sets previous_turn_itr if update is necessary
|
||||
reduce_verbosity_if_possible(current_turn_itr, previous_turn_itr);
|
||||
}
|
||||
else
|
||||
{
|
||||
// remember the current (non-suppressed) item as a new start of a segment
|
||||
previous_turn_itr = current_turn_itr;
|
||||
}
|
||||
}
|
||||
return removeNoTurnInstructions(std::move(steps));
|
||||
}
|
||||
|
||||
// `useLane` steps are only returned on `straight` maneuvers when there
|
||||
// are surrounding lanes also tagged as `straight`. If there are no other `straight`
|
||||
// lanes, it is not an ambiguous maneuver, and we can collapse the `useLane` step.
|
||||
std::vector<RouteStep> collapseUseLane(std::vector<RouteStep> steps)
|
||||
{
|
||||
const auto containsTag = [](const extractor::guidance::TurnLaneType::Mask mask,
|
||||
const extractor::guidance::TurnLaneType::Mask tag) {
|
||||
return (mask & tag) != extractor::guidance::TurnLaneType::empty;
|
||||
};
|
||||
|
||||
const auto canCollapseUseLane = [containsTag](const RouteStep &step) {
|
||||
// the lane description is given left to right, lanes are counted from the right.
|
||||
// Therefore we access the lane description using the reverse iterator
|
||||
|
||||
auto right_most_lanes = step.LanesToTheRight();
|
||||
if (!right_most_lanes.empty() && containsTag(right_most_lanes.front(),
|
||||
(extractor::guidance::TurnLaneType::straight |
|
||||
extractor::guidance::TurnLaneType::none)))
|
||||
return false;
|
||||
|
||||
auto left_most_lanes = step.LanesToTheLeft();
|
||||
if (!left_most_lanes.empty() && containsTag(left_most_lanes.back(),
|
||||
(extractor::guidance::TurnLaneType::straight |
|
||||
extractor::guidance::TurnLaneType::none)))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
BOOST_ASSERT(steps.size() > 1);
|
||||
for (auto step_itr = steps.begin() + 1; step_itr != steps.end(); ++step_itr)
|
||||
{
|
||||
if (step_itr->maneuver.instruction.type == TurnType::UseLane &&
|
||||
canCollapseUseLane(*step_itr))
|
||||
{
|
||||
auto previous_turn_itr = findPreviousTurn(step_itr);
|
||||
if (haveSameMode(*previous_turn_itr, *step_itr))
|
||||
{
|
||||
previous_turn_itr->ElongateBy(*step_itr);
|
||||
step_itr->Invalidate();
|
||||
}
|
||||
}
|
||||
}
|
||||
return removeNoTurnInstructions(std::move(steps));
|
||||
}
|
||||
|
||||
} // namespace guidance
|
||||
} // namespace engine
|
||||
} // namespace osrm
|
||||
Reference in New Issue
Block a user