Move splitting candidates to plugin

This commit is contained in:
Patrick Niklaus 2015-02-09 00:55:12 +01:00
parent 0637215b85
commit 66d7a073d3
2 changed files with 53 additions and 58 deletions

View File

@ -95,24 +95,15 @@ template <class DataFacadeT> class MapMatchingPlugin : public BasePlugin
return label_with_confidence; return label_with_confidence;
} }
int HandleRequest(const RouteParameters &route_parameters, JSON::Object &json_result) final bool get_candiates(const std::vector<FixedPointCoordinate>& input_coords, double& trace_length, Matching::CandidateLists& candidates_lists)
{ {
// check number of parameters
if (!check_all_coordinates(route_parameters.coordinates))
{
return 400;
}
const auto& input_coords = route_parameters.coordinates;
Matching::CandidateLists candidate_lists;
std::vector<bool> uturn_indicators(false, input_coords.size());
double last_distance = coordinate_calculation::great_circle_distance( double last_distance = coordinate_calculation::great_circle_distance(
input_coords[0], input_coords[0],
input_coords[1]); input_coords[1]);
double trace_length = 0; trace_length = 0;
for (const auto current_coordinate : osrm::irange<std::size_t>(0, input_coords.size())) for (const auto current_coordinate : osrm::irange<std::size_t>(0, input_coords.size()))
{ {
bool allow_uturn = false;
if (0 < current_coordinate) if (0 < current_coordinate)
{ {
last_distance = coordinate_calculation::great_circle_distance( last_distance = coordinate_calculation::great_circle_distance(
@ -131,7 +122,7 @@ template <class DataFacadeT> class MapMatchingPlugin : public BasePlugin
// sharp turns indicate a possible uturn // sharp turns indicate a possible uturn
if (turn_angle < 100.0 || turn_angle > 260.0) if (turn_angle < 100.0 || turn_angle > 260.0)
{ {
uturn_indicators[current_coordinate] = true; allow_uturn = true;
} }
} }
@ -142,18 +133,61 @@ template <class DataFacadeT> class MapMatchingPlugin : public BasePlugin
last_distance/2.0, last_distance/2.0,
5, 5,
20)) 20))
{
return false;
}
if (allow_uturn)
{
candidates_lists.push_back(candidates);
}
else
{
unsigned compact_size = candidates.size();
for (const auto i : osrm::irange(0u, compact_size))
{
// Split edge if it is bidirectional and append reverse direction to end of list
if (candidates[i].first.forward_node_id != SPECIAL_NODEID
&& candidates[i].first.reverse_node_id != SPECIAL_NODEID)
{
PhantomNode reverse_node(candidates[i].first);
reverse_node.forward_node_id = SPECIAL_NODEID;
candidates.push_back(std::make_pair(reverse_node, candidates[i].second));
candidates[i].first.reverse_node_id = SPECIAL_NODEID;
}
}
candidates_lists.push_back(candidates);
}
}
return true;
}
int HandleRequest(const RouteParameters &route_parameters, JSON::Object &json_result) final
{
// check number of parameters
if (!check_all_coordinates(route_parameters.coordinates))
{ {
return 400; return 400;
} }
candidate_lists.push_back(candidates); double trace_length;
Matching::CandidateLists candidates_lists;
const auto& input_coords = route_parameters.coordinates;
bool found_candidates = get_candiates(input_coords, trace_length, candidates_lists);
if (!found_candidates)
{
return 400;
} }
// call the actual map matching // call the actual map matching
std::vector<PhantomNode> matched_nodes;
JSON::Object debug_info; JSON::Object debug_info;
search_engine_ptr->map_matching(candidate_lists, input_coords, uturn_indicators, matched_nodes, debug_info); std::vector<PhantomNode> matched_nodes;
search_engine_ptr->map_matching(candidates_lists, input_coords, matched_nodes, debug_info);
// classify result
TraceClassification classification = classify(trace_length, matched_nodes, input_coords.size() - matched_nodes.size()); TraceClassification classification = classify(trace_length, matched_nodes, input_coords.size() - matched_nodes.size());
if (classification.first == ClassifierT::ClassLabel::POSITIVE) if (classification.first == ClassifierT::ClassLabel::POSITIVE)
{ {
@ -164,6 +198,7 @@ template <class DataFacadeT> class MapMatchingPlugin : public BasePlugin
json_result.values["confidence"] = 1-classification.second; json_result.values["confidence"] = 1-classification.second;
} }
// run shortest path routing to obtain geometry
InternalRouteResult raw_route; InternalRouteResult raw_route;
PhantomNodes current_phantom_node_pair; PhantomNodes current_phantom_node_pair;
for (unsigned i = 0; i < matched_nodes.size() - 1; ++i) for (unsigned i = 0; i < matched_nodes.size() - 1; ++i)

View File

@ -247,47 +247,11 @@ template <class DataFacadeT> class MapMatching final
{ {
} }
// TODO optimize: a lot of copying that could probably be avoided void operator()(const Matching::CandidateLists &timestamp_list,
void expandCandidates(const Matching::CandidateLists &candidates_lists,
Matching::CandidateLists &expanded_lists,
const std::vector<bool>& uturn_indicators) const
{
// expand list of PhantomNodes to be single-directional
expanded_lists.resize(candidates_lists.size());
for (const auto i : osrm::irange(0lu, candidates_lists.size()))
{
for (const auto& candidate : candidates_lists[i])
{
// bi-directional edge, split phantom node if we don't expect a uturn
if (!uturn_indicators[i] && candidate.first.forward_node_id != SPECIAL_NODEID && candidate.first.reverse_node_id != SPECIAL_NODEID)
{
PhantomNode forward_node(candidate.first);
PhantomNode reverse_node(candidate.first);
forward_node.reverse_node_id = SPECIAL_NODEID;
reverse_node.forward_node_id = SPECIAL_NODEID;
expanded_lists[i].emplace_back(forward_node, candidate.second);
expanded_lists[i].emplace_back(reverse_node, candidate.second);
}
else
{
expanded_lists[i].push_back(candidate);
}
}
}
}
void operator()(const Matching::CandidateLists &candidates_lists,
const std::vector<FixedPointCoordinate> coordinate_list, const std::vector<FixedPointCoordinate> coordinate_list,
const std::vector<bool>& uturn_indicators,
std::vector<PhantomNode>& matched_nodes, std::vector<PhantomNode>& matched_nodes,
JSON::Object& _debug_info) const JSON::Object& _debug_info) const
{ {
BOOST_ASSERT(candidates_lists.size() == coordinate_list.size());
BOOST_ASSERT(candidates_lists.size() == uturn_indicators.size());
Matching::CandidateLists timestamp_list;
expandCandidates(candidates_lists, timestamp_list, uturn_indicators);
std::vector<bool> breakage(timestamp_list.size(), true); std::vector<bool> breakage(timestamp_list.size(), true);
BOOST_ASSERT(timestamp_list.size() > 0); BOOST_ASSERT(timestamp_list.size() > 0);
@ -462,10 +426,6 @@ template <class DataFacadeT> class MapMatching final
matched_nodes[i] = timestamp_list[timestamp_index][location_index].first; matched_nodes[i] = timestamp_list[timestamp_index][location_index].first;
} }
unsigned removed = candidates_lists.size() - matched_nodes.size();
if (removed > 10)
SimpleLogger().Write(logWARNING) << "Warning: removed " << removed << " candiates.";
JSON::Array _debug_chosen_candidates; JSON::Array _debug_chosen_candidates;
auto _debug_candidate_iter = reconstructed_indices.begin(); auto _debug_candidate_iter = reconstructed_indices.begin();
for (auto i = 0u; i < timestamp_list.size(); ++i) for (auto i = 0u; i < timestamp_list.size(); ++i)