Make intersection backwards compatible

For `depart` and `arrive` `step.intersections[0].{bearings|entry}` will have
only one entry.
This commit is contained in:
Patrick Niklaus
2016-05-18 17:53:05 +02:00
parent 3b37769624
commit 95af72c70c
10 changed files with 272 additions and 235 deletions
+41 -58
View File
@@ -78,63 +78,6 @@ util::json::Array coordinateToLonLat(const util::Coordinate coordinate)
return array;
}
util::json::Object
getIntersection(const guidance::Intersection &intersection, bool locate_before, bool locate_after)
{
util::json::Object result;
util::json::Array bearings;
util::json::Array entry;
const auto &available_bearings = intersection.bearing_class.getAvailableBearings();
for (std::size_t i = 0; i < available_bearings.size(); ++i)
{
bearings.values.push_back(available_bearings[i]);
entry.values.push_back(intersection.entry_class.allowsEntry(i) ? "true" : "false");
}
result.values["location"] = detail::coordinateToLonLat(intersection.location);
if (locate_before)
{
// bearings are oriented in the direction of driving. For the in-bearing, we actually need
// to
// find the bearing from the view of the intersection. This means we have to rotate the
// bearing
// by 180 degree.
const auto rotated_bearing_before = (intersection.bearing_before >= 180.0)
? (intersection.bearing_before - 180.0)
: (intersection.bearing_before + 180.0);
result.values["in"] =
intersection.bearing_class.findMatchingBearing(rotated_bearing_before);
}
if (locate_after)
{
result.values["out"] =
intersection.bearing_class.findMatchingBearing(intersection.bearing_after);
}
result.values["bearings"] = bearings;
result.values["entry"] = entry;
return result;
}
util::json::Array getIntersection(const guidance::RouteStep &step)
{
util::json::Array result;
bool first = true;
for (const auto &intersection : step.intersections)
{
// on waypoints, the first/second bearing is invalid. In these cases, we cannot locate the
// bearing as part of the available bearings at the intersection.
result.values.push_back(getIntersection(
intersection, !first || step.maneuver.waypoint_type != guidance::WaypointType::Depart,
!first || step.maneuver.waypoint_type != guidance::WaypointType::Arrive));
first = false;
}
return result;
}
// FIXME this actually needs to be configurable from the profiles
std::string modeToString(const extractor::TravelMode mode)
{
@@ -198,12 +141,46 @@ util::json::Object makeStepManeuver(const guidance::StepManeuver &maneuver)
step_maneuver.values["modifier"] =
detail::instructionModifierToString(maneuver.instruction.direction_modifier);
step_maneuver.values["location"] = detail::coordinateToLonLat(maneuver.location);
step_maneuver.values["bearing_before"] = std::round(maneuver.bearing_before);
step_maneuver.values["bearing_after"] = std::round(maneuver.bearing_after);
if (maneuver.exit != 0)
step_maneuver.values["exit"] = maneuver.exit;
return step_maneuver;
}
util::json::Object
makeIntersection(const guidance::Intersection &intersection)
{
util::json::Object result;
util::json::Array bearings;
util::json::Array entry;
bearings.values.reserve(intersection.bearings.size());
std::copy(intersection.bearings.begin(), intersection.bearings.end(), std::back_inserter(bearings.values));
entry.values.reserve(intersection.entry.size());
std::transform(intersection.entry.begin(), intersection.entry.end(),
std::back_inserter(entry.values), [](const bool has_entry) -> util::json::Value
{
if (has_entry)
return util::json::True();
else
return util::json::False();
});
result.values["location"] = detail::coordinateToLonLat(intersection.location);
result.values["bearings"] = bearings;
result.values["entry"] = entry;
if (intersection.in != guidance::Intersection::NO_INDEX)
result.values["in"] = intersection.in;
if (intersection.out != guidance::Intersection::NO_INDEX)
result.values["out"] = intersection.out;
return result;
}
util::json::Object makeRouteStep(guidance::RouteStep step, util::json::Value geometry)
{
util::json::Object route_step;
@@ -216,7 +193,13 @@ util::json::Object makeRouteStep(guidance::RouteStep step, util::json::Value geo
route_step.values["mode"] = detail::modeToString(std::move(step.mode));
route_step.values["maneuver"] = makeStepManeuver(std::move(step.maneuver));
route_step.values["geometry"] = std::move(geometry);
route_step.values["intersections"] = detail::getIntersection(step);
util::json::Array intersections;
intersections.values.reserve(step.intersections.size());
std::transform(step.intersections.begin(), step.intersections.end(),
std::back_inserter(intersections.values), makeIntersection);
route_step.values["intersections"] = std::move(intersections);
return route_step;
}
+11 -45
View File
@@ -13,11 +13,8 @@ namespace guidance
{
namespace detail
{
namespace
{
void fillInIntermediate(Intersection &intersection,
const LegGeometry &leg_geometry,
const std::size_t segment_index)
std::pair<short, short> getIntermediateBearings(const LegGeometry &leg_geometry,
const std::size_t segment_index)
{
auto turn_index = leg_geometry.BackIndex(segment_index);
BOOST_ASSERT(turn_index > 0);
@@ -28,58 +25,27 @@ void fillInIntermediate(Intersection &intersection,
const auto turn_coordinate = leg_geometry.locations[turn_index];
const auto post_turn_coordinate = leg_geometry.locations[turn_index + 1];
intersection.bearing_before =
util::coordinate_calculation::bearing(pre_turn_coordinate, turn_coordinate);
intersection.bearing_after =
util::coordinate_calculation::bearing(turn_coordinate, post_turn_coordinate);
intersection.location = turn_coordinate;
return std::make_pair<short, short>(
std::round(util::coordinate_calculation::bearing(pre_turn_coordinate, turn_coordinate)),
std::round(util::coordinate_calculation::bearing(turn_coordinate, post_turn_coordinate)));
}
void fillInDepart(Intersection &intersection, const LegGeometry &leg_geometry)
std::pair<short, short> getDepartBearings(const LegGeometry &leg_geometry)
{
BOOST_ASSERT(leg_geometry.locations.size() >= 2);
const auto turn_coordinate = leg_geometry.locations.front();
const auto post_turn_coordinate = *(leg_geometry.locations.begin() + 1);
intersection.location = turn_coordinate;
intersection.bearing_before = 0;
intersection.bearing_after =
util::coordinate_calculation::bearing(turn_coordinate, post_turn_coordinate);
return std::make_pair<short, short>(
0, std::round(util::coordinate_calculation::bearing(turn_coordinate, post_turn_coordinate)));
}
void fillInArrive(Intersection &intersection, const LegGeometry &leg_geometry)
std::pair<short, short> getArriveBearings(const LegGeometry &leg_geometry)
{
BOOST_ASSERT(leg_geometry.locations.size() >= 2);
const auto turn_coordinate = leg_geometry.locations.back();
const auto pre_turn_coordinate = *(leg_geometry.locations.end() - 2);
intersection.location = turn_coordinate;
intersection.bearing_before =
util::coordinate_calculation::bearing(pre_turn_coordinate, turn_coordinate);
intersection.bearing_after = 0;
}
} // namespace
Intersection intersectionFromGeometry(const WaypointType waypoint_type,
const double segment_duration,
const LegGeometry &leg_geometry,
const std::size_t segment_index)
{
Intersection intersection;
intersection.duration = segment_duration;
intersection.distance = leg_geometry.segment_distances[segment_index];
switch (waypoint_type)
{
case WaypointType::None:
fillInIntermediate(intersection, leg_geometry, segment_index);
break;
case WaypointType::Depart:
fillInDepart(intersection, leg_geometry);
break;
case WaypointType::Arrive:
fillInArrive(intersection, leg_geometry);
break;
}
return intersection;
return std::make_pair<short, short>(
std::round(util::coordinate_calculation::bearing(pre_turn_coordinate, turn_coordinate)), 0);
}
} // ns detail
} // ns engine
+124 -39
View File
@@ -54,11 +54,12 @@ void print(const std::vector<RouteStep> &steps)
for (const auto &intersection : step.intersections)
{
std::cout << "(" << intersection.duration << " " << intersection.distance << " "
<< " Bearings: " << intersection.bearing_before << " "
<< intersection.bearing_after << " -- ";
for( auto bearing : intersection.bearing_class.getAvailableBearings() )
std::cout << "(bearings:";
for( auto bearing : intersection.bearings)
std:: cout << " " << bearing;
std::cout << ", entry: ";
for( auto entry : intersection.entry)
std:: cout << " " << entry;
std::cout << ")";
}
@@ -86,6 +87,7 @@ RouteStep forwardInto(RouteStep destination, const RouteStep &source)
destination.geometry_begin = std::min(destination.geometry_begin, source.geometry_begin);
destination.geometry_end = std::max(destination.geometry_end, source.geometry_end);
destination.maneuver.exit = destination.intersections.size() - 1;
return destination;
}
@@ -205,7 +207,8 @@ void closeOffRoundabout(const bool on_roundabout,
// removal.
std::vector<std::size_t> intermediate_steps;
BOOST_ASSERT(!steps[step_index].intersections.empty());
const auto exit_bearing = steps[step_index].intersections.back().bearing_after;
const auto exit_intersection = steps[step_index].intersections.back();
const auto exit_bearing = exit_intersection.bearings[exit_intersection.out];
if (step_index > 1)
{
// The very first route-step is head, so we cannot iterate past that one
@@ -218,6 +221,7 @@ void closeOffRoundabout(const bool on_roundabout,
{
propagation_step.maneuver.exit = step.maneuver.exit;
propagation_step.geometry_end = step.geometry_end;
const auto entry_intersection = propagation_step.intersections.front();
// remember rotary name
if (propagation_step.maneuver.instruction.type == TurnType::EnterRotary ||
@@ -259,7 +263,7 @@ void closeOffRoundabout(const bool on_roundabout,
const auto angle = 540 - rotated_exit;
return angle > 360 ? angle - 360 : angle;
}(propagation_step.intersections.front().bearing_before, exit_bearing);
}(util::bearing::reverseBearing(entry_intersection.bearings[entry_intersection.in]), exit_bearing);
propagation_step.maneuver.instruction.direction_modifier =
::osrm::util::guidance::getTurnDirection(angle);
@@ -376,8 +380,8 @@ void collapseTurnAt(std::vector<RouteStep> &steps,
}
}
// Potential U-Turn
else if (bearingsAreReversed(one_back_step.intersections.front().bearing_before,
current_step.intersections.front().bearing_after))
else if (bearingsAreReversed(util::bearing::reverseBearing(one_back_step.intersections.front().bearings[one_back_step.intersections.front().in]),
current_step.intersections.front().bearings[current_step.intersections.front().out]))
{
BOOST_ASSERT(two_back_index < steps.size());
@@ -433,6 +437,17 @@ std::vector<RouteStep> removeNoTurnInstructions(std::vector<RouteStep> steps)
};
boost::remove_erase_if(steps, not_is_valid);
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;
}
@@ -442,7 +457,6 @@ std::vector<RouteStep> removeNoTurnInstructions(std::vector<RouteStep> steps)
// They are required for maintenance purposes. We can calculate the number
// of exits to pass in a roundabout and the number of intersections
// that we come across.
std::vector<RouteStep> postProcess(std::vector<RouteStep> steps)
{
// the steps should always include the first/last step in form of a location
@@ -507,6 +521,7 @@ std::vector<RouteStep> postProcess(std::vector<RouteStep> steps)
last_valid_instruction = step_index;
}
}
// unterminated roundabout
// Move backwards through the instructions until the start and remove the exit number
// A roundabout without exit translates to enter-roundabout.
@@ -515,6 +530,16 @@ std::vector<RouteStep> postProcess(std::vector<RouteStep> steps)
fixFinalRoundabout(steps);
}
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));
}
@@ -603,18 +628,29 @@ std::vector<RouteStep> collapseTurns(std::vector<RouteStep> steps)
collapseTurnAt(steps, two_back_index, one_back_index, step_index);
}
}
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
// different location.
// As a direct implication, we have to keep the time of the initial/final turns (which adds a
// few seconds of inaccuracy at both ends. This is acceptable, however, since the turn should
// usually not be as relevant.
void trimShortSegments(std::vector<RouteStep> &steps, LegGeometry &geometry)
{
// 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
// different location.
// As a direct implication, we have to keep the time of the initial/final turns (which adds a
// few seconds of inaccuracy at both ends. This is acceptable, however, since the turn should
// usually not be as relevant.
if (steps.size() < 2 || geometry.locations.size() <= 2)
return;
@@ -669,20 +705,29 @@ void trimShortSegments(std::vector<RouteStep> &steps, LegGeometry &geometry)
// update initial turn direction/bearings. Due to the duplicated first coordinate,
// the initial bearing is invalid
designated_depart.maneuver = {TurnInstruction::NO_TURN(), WaypointType::Depart, 0};
designated_depart.maneuver.waypoint_type = WaypointType::Depart;
designated_depart.maneuver.bearing_before = 0;
designated_depart.maneuver.instruction = TurnInstruction::NO_TURN();
// we need to make this conform with the intersection format for the first intersection
auto& first_intersection = designated_depart.intersections.front();
first_intersection.bearings = {first_intersection.bearings[first_intersection.out]};
first_intersection.entry = {true};
first_intersection.in = Intersection::NO_INDEX;
first_intersection.out = 0;
// finally remove the initial (now duplicated move)
steps.erase(steps.begin());
}
else
{
// we need to make this at least 1 because we will substract 1
// from all offsets at the end of the loop.
steps.front().geometry_begin = 1;
// reduce all offsets by one (inplace)
std::transform(geometry.segment_offsets.begin(), geometry.segment_offsets.end(),
geometry.segment_offsets.begin(),
[](const std::size_t val) { return val - 1; });
steps.front().maneuver = {TurnInstruction::NO_TURN(), WaypointType::Depart, 0};
}
// and update the leg geometry indices for the removed entry
@@ -690,12 +735,32 @@ void trimShortSegments(std::vector<RouteStep> &steps, LegGeometry &geometry)
--step.geometry_begin;
--step.geometry_end;
});
auto& first_step = steps.front();
// we changed the geometry, we need to recalculate the bearing
auto bearing = std::round(util::coordinate_calculation::bearing(
geometry.locations[first_step.geometry_begin],
geometry.locations[first_step.geometry_begin+1]));
std::cout << geometry.locations[first_step.geometry_begin] << geometry.locations[first_step.geometry_begin+1] << std::endl;
first_step.maneuver.bearing_after = bearing;
first_step.intersections.front().bearings.front() = bearing;
}
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);
// make sure we still have enough segments
if (steps.size() < 2 || geometry.locations.size() == 2)
return;
BOOST_ASSERT(geometry.locations.size() >= steps.size());
auto &next_to_last_step = *(steps.end() - 2);
// in the end, the situation with the roundabout cannot occur. As a result, we can remove
@@ -708,9 +773,15 @@ void trimShortSegments(std::vector<RouteStep> &steps, LegGeometry &geometry)
BOOST_ASSERT(geometry.segment_distances.back() < 1);
geometry.segment_distances.pop_back();
next_to_last_step.maneuver = {TurnInstruction::NO_TURN(), WaypointType::Arrive, 0};
BOOST_ASSERT(!next_to_last_step.intersections.empty());
next_to_last_step.intersections.front().bearing_after = 0;
next_to_last_step.maneuver.waypoint_type = WaypointType::Arrive;
next_to_last_step.maneuver.instruction = TurnInstruction::NO_TURN();
next_to_last_step.maneuver.bearing_after = 0;
BOOST_ASSERT(next_to_last_step.intersections.size() == 1);
auto& last_intersection = next_to_last_step.intersections.back();
last_intersection.bearings = {last_intersection.bearings[last_intersection.in]};
last_intersection.entry = {true};
last_intersection.out = Intersection::NO_INDEX;
last_intersection.in = 0;
steps.pop_back();
// Because we eliminated a really short segment, it was probably
@@ -738,10 +809,29 @@ void trimShortSegments(std::vector<RouteStep> &steps, LegGeometry &geometry)
BOOST_ASSERT(next_to_last_step.geometry_end == steps.back().geometry_begin + 1);
BOOST_ASSERT(next_to_last_step.geometry_begin < next_to_last_step.geometry_end);
next_to_last_step.geometry_end--;
steps.back().geometry_begin--;
steps.back().geometry_end--;
steps.back().maneuver = {TurnInstruction::NO_TURN(), WaypointType::Arrive, 0};
auto& last_step = steps.back();
last_step.geometry_begin--;
last_step.geometry_end--;
BOOST_ASSERT(next_to_last_step.geometry_end == last_step.geometry_begin + 1);
BOOST_ASSERT(last_step.geometry_begin == last_step.geometry_end-1);
BOOST_ASSERT(next_to_last_step.geometry_end >= 2);
// we changed the geometry, we need to recalculate the bearing
auto bearing = std::round(util::coordinate_calculation::bearing(
geometry.locations[next_to_last_step.geometry_end - 2],
geometry.locations[last_step.geometry_begin]));
last_step.maneuver.bearing_before = bearing;
last_step.intersections.front().bearings.front() = util::bearing::reverseBearing(bearing);
}
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);
}
// assign relative locations to depart/arrive instructions
@@ -765,12 +855,6 @@ std::vector<RouteStep> assignRelativeLocations(std::vector<RouteStep> steps,
: extractor::guidance::DirectionModifier::UTurn;
steps.front().maneuver.instruction.direction_modifier = initial_modifier;
BOOST_ASSERT(!steps.front().intersections.empty());
steps.front().intersections.front().bearing_before = 0;
steps.front().intersections.front().bearing_after =
util::coordinate_calculation::bearing(leg_geometry.locations[0], leg_geometry.locations[1]);
steps.front().intersections.front() = util::guidance::setIntersectionClasses(
std::move(steps.front().intersections.front()), source_node);
const auto distance_from_end = util::coordinate_calculation::haversineDistance(
target_node.input_location, leg_geometry.locations.back());
@@ -784,15 +868,16 @@ std::vector<RouteStep> assignRelativeLocations(std::vector<RouteStep> steps,
: extractor::guidance::DirectionModifier::UTurn;
steps.back().maneuver.instruction.direction_modifier = final_modifier;
BOOST_ASSERT(steps.back().intersections.size() == 1);
BOOST_ASSERT(!steps.back().intersections.empty());
steps.back().intersections.front().bearing_before = util::coordinate_calculation::bearing(
leg_geometry.locations[leg_geometry.locations.size() - 2],
leg_geometry.locations[leg_geometry.locations.size() - 1]);
steps.back().intersections.front().bearing_after = 0;
steps.back().intersections.front() = util::guidance::setIntersectionClasses(
std::move(steps.back().intersections.front()), target_node);
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;
}