From 6fa7ffdc7501d3f8ca852f0005a3283b8c76fd48 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Tue, 30 Aug 2016 11:43:00 -0700 Subject: [PATCH] Pass approach/exit speed values to turn_function. --- profiles/bicycle.lua | 2 +- profiles/car.lua | 2 - profiles/turnbot.lua | 2 +- src/extractor/edge_based_graph_factory.cpp | 41 ++++++++++++++++++--- src/extractor/scripting_environment_lua.cpp | 2 + 5 files changed, 40 insertions(+), 9 deletions(-) diff --git a/profiles/bicycle.lua b/profiles/bicycle.lua index 9e68365e888..cae0f316b1a 100644 --- a/profiles/bicycle.lua +++ b/profiles/bicycle.lua @@ -401,7 +401,7 @@ function way_function (way, result) limit( result, maxspeed, maxspeed_forward, maxspeed_backward ) end -function turn_function (angle) +function turn_function (angle, approach_road_speed, exit_road_speed) -- compute turn penalty as angle^2, with a left/right bias -- multiplying by 10 converts to deci-seconds see issue #1318 k = 10*turn_penalty/(90.0*90.0) diff --git a/profiles/car.lua b/profiles/car.lua index 38c5268b6bb..4b29dd4c2c5 100644 --- a/profiles/car.lua +++ b/profiles/car.lua @@ -591,8 +591,6 @@ function way_function (way, result) result.is_startpoint = result.forward_mode == mode.driving or result.backward_mode == mode.driving end --- a detailed turn function as an alternative to turn function, offering more properties to decide on a good penalty --- returns penalty in seconds function turn_function(angle, turn_properties, intersection_properties, approach_segment, exit_segment) local penalty = 0; if turn_properties.angle>=0 then diff --git a/profiles/turnbot.lua b/profiles/turnbot.lua index 7629515d130..5053b6284df 100644 --- a/profiles/turnbot.lua +++ b/profiles/turnbot.lua @@ -3,7 +3,7 @@ require 'testbot' -function turn_function (angle) +function turn_function (angle, approach_road_speed, exit_road_speed) -- multiplying by 10 converts to deci-seconds see issue #1318 return 10*20*math.abs(angle)/180 end diff --git a/src/extractor/edge_based_graph_factory.cpp b/src/extractor/edge_based_graph_factory.cpp index 48f9321798f..c3955b936f5 100644 --- a/src/extractor/edge_based_graph_factory.cpp +++ b/src/extractor/edge_based_graph_factory.cpp @@ -424,8 +424,6 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges( bool crosses_through_traffic = false; const EdgeData &edge_data_from_u = m_node_based_graph->GetEdgeData(edge_from_u); - const TurnSegment approach_segment = {static_cast(edge_data_from_u.distance), - static_cast(edge_data_from_u.distance)}; const IntersectionProperties intersection_properties = { crosses_traffic_light, false, false}; for (const auto turn : possible_turns) @@ -470,12 +468,45 @@ void EdgeBasedGraphFactory::GenerateEdgeExpandedEdges( const TurnProperties turn_properties = { 180. - turn.angle, turn.angle, crosses_through_traffic, requires_announcement}; - const TurnSegment exit_segment = {static_cast(edge_data2.distance), - static_cast(edge_data2.distance)}; + + const bool isTrivial = m_compressed_edge_container.IsTrivial(edge_from_u); + + const auto &approach_node = + isTrivial + ? m_node_info_list[node_u] + : m_node_info_list[m_compressed_edge_container.GetLastEdgeSourceID( + edge_from_u)]; + const auto &intersection_node = + m_node_info_list[m_compressed_edge_container.GetLastEdgeTargetID( + edge_from_u)]; + + const auto &exit_node = + m_node_info_list[m_compressed_edge_container.GetFirstEdgeTargetID( + turn.eid)]; + + const double approach_segment_length = + util::coordinate_calculation::greatCircleDistance(approach_node, intersection_node); + const double exit_segment_length = + util::coordinate_calculation::greatCircleDistance(intersection_node, exit_node); + + const auto approach_road_segments = + m_compressed_edge_container.GetBucketReference(edge_from_u); + const auto exit_road_segments = + m_compressed_edge_container.GetBucketReference(turn.eid); + + // segment_length is in metres, weight is in deciseconds, this converts those + // to km/h + const double approach_speed = approach_segment_length / (approach_road_segments.back().weight/10) * 3.6; // km/h + const double exit_speed = exit_segment_length / (exit_road_segments.front().weight/10) * 3.6; // km/h + + const TurnSegment approach_segment = {approach_segment_length,approach_speed}; + const TurnSegment exit_segment = {exit_segment_length,exit_speed}; + + const auto turn_instruction = turn.instruction; const int32_t turn_penalty = scripting_environment.GetTurnPenalty( turn_properties, intersection_properties, approach_segment, exit_segment); - const auto turn_instruction = turn.instruction; + if (turn_instruction.direction_modifier == guidance::DirectionModifier::UTurn) { diff --git a/src/extractor/scripting_environment_lua.cpp b/src/extractor/scripting_environment_lua.cpp index cd0d224dbbf..6a36e93d15d 100644 --- a/src/extractor/scripting_environment_lua.cpp +++ b/src/extractor/scripting_environment_lua.cpp @@ -395,6 +395,8 @@ std::int32_t LuaScriptingEnvironment::GetTurnPenalty( boost::cref(intersection_properties), boost::cref(approach_segment), boost::cref(exit_segment)); + BOOST_ASSERT(penalty < std::numeric_limits::max()); + BOOST_ASSERT(penalty > std::numeric_limits::min()); return boost::numeric_cast(penalty); } catch (const luabind::error &er)