Add skeleton code for matching
This commit is contained in:
committed by
Patrick Niklaus
parent
7e00a86bb4
commit
2259bce05f
@@ -31,6 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include "search_engine_data.hpp"
|
||||
#include "../routing_algorithms/alternative_path.hpp"
|
||||
#include "../routing_algorithms/many_to_many.hpp"
|
||||
#include "../routing_algorithms/map_matching.hpp"
|
||||
#include "../routing_algorithms/shortest_path.hpp"
|
||||
|
||||
#include <type_traits>
|
||||
@@ -45,10 +46,14 @@ template <class DataFacadeT> class SearchEngine
|
||||
ShortestPathRouting<DataFacadeT> shortest_path;
|
||||
AlternativeRouting<DataFacadeT> alternative_path;
|
||||
ManyToManyRouting<DataFacadeT> distance_table;
|
||||
MapMatching<DataFacadeT> map_matching;
|
||||
|
||||
explicit SearchEngine(DataFacadeT *facade)
|
||||
: facade(facade), shortest_path(facade, engine_working_data),
|
||||
alternative_path(facade, engine_working_data), distance_table(facade, engine_working_data)
|
||||
: facade(facade),
|
||||
shortest_path(facade, engine_working_data),
|
||||
alternative_path(facade, engine_working_data),
|
||||
distance_table(facade, engine_working_data),
|
||||
map_matching(facade, engine_working_data)
|
||||
{
|
||||
static_assert(!std::is_pointer<DataFacadeT>::value, "don't instantiate with ptr type");
|
||||
static_assert(std::is_object<DataFacadeT>::value,
|
||||
|
||||
@@ -745,6 +745,7 @@ class StaticRTree
|
||||
// check if it is smaller than what we had before
|
||||
float current_ratio = 0.f;
|
||||
FixedPointCoordinate foot_point_coordinate_on_segment;
|
||||
|
||||
// const float current_perpendicular_distance =
|
||||
coordinate_calculation::perpendicular_distance_from_projected_coordinate(
|
||||
m_coordinate_list->at(current_segment.u),
|
||||
@@ -796,20 +797,24 @@ class StaticRTree
|
||||
return !result_phantom_node_vector.empty();
|
||||
}
|
||||
|
||||
// implementation of the Hjaltason/Samet query [3], a BFS traversal of the tree
|
||||
bool IncrementalFindPhantomNodeForCoordinateWithDistance(
|
||||
const FixedPointCoordinate &input_coordinate,
|
||||
std::vector<std::pair<PhantomNode, double>> &result_phantom_node_vector,
|
||||
const unsigned number_of_results,
|
||||
const unsigned max_checked_segments = 4 * LEAF_NODE_SIZE)
|
||||
const unsigned max_number_of_phantom_nodes,
|
||||
const unsigned max_checked_elements = 4 * LEAF_NODE_SIZE)
|
||||
{
|
||||
std::vector<float> min_found_distances(number_of_results,
|
||||
std::numeric_limits<float>::max());
|
||||
unsigned inspected_elements = 0;
|
||||
unsigned number_of_elements_from_big_cc = 0;
|
||||
unsigned number_of_elements_from_tiny_cc = 0;
|
||||
|
||||
unsigned number_of_results_found_in_big_cc = 0;
|
||||
unsigned number_of_results_found_in_tiny_cc = 0;
|
||||
unsigned pruned_elements = 0;
|
||||
|
||||
unsigned inspected_segments = 0;
|
||||
std::pair<double, double> projected_coordinate = {
|
||||
mercator::lat2y(input_coordinate.lat / COORDINATE_PRECISION),
|
||||
input_coordinate.lon / COORDINATE_PRECISION};
|
||||
|
||||
// upper bound pruning technique
|
||||
upper_bound<float> pruning_bound(max_number_of_phantom_nodes);
|
||||
|
||||
// initialize queue with root element
|
||||
std::priority_queue<IncrementalQueryCandidate> traversal_queue;
|
||||
@@ -820,42 +825,43 @@ class StaticRTree
|
||||
const IncrementalQueryCandidate current_query_node = traversal_queue.top();
|
||||
traversal_queue.pop();
|
||||
|
||||
const float current_min_dist = min_found_distances[number_of_results - 1];
|
||||
|
||||
if (current_query_node.min_dist > current_min_dist)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (current_query_node.RepresentsTreeNode())
|
||||
{
|
||||
if (current_query_node.node.template is<TreeNode>())
|
||||
{ // current object is a tree node
|
||||
const TreeNode ¤t_tree_node =
|
||||
current_query_node.node.template get<TreeNode>();
|
||||
if (current_tree_node.child_is_on_disk)
|
||||
{
|
||||
LeafNode current_leaf_node;
|
||||
LoadLeafFromDisk(current_tree_node.children[0], current_leaf_node);
|
||||
// Add all objects from leaf into queue
|
||||
for (uint32_t i = 0; i < current_leaf_node.object_count; ++i)
|
||||
|
||||
// current object represents a block on disk
|
||||
for (const auto i : osrm::irange(0u, current_leaf_node.object_count))
|
||||
{
|
||||
const auto ¤t_edge = current_leaf_node.objects[i];
|
||||
const float current_perpendicular_distance =
|
||||
coordinate_calculation::perpendicular_distance(
|
||||
const float current_perpendicular_distance = coordinate_calculation::
|
||||
perpendicular_distance_from_projected_coordinate(
|
||||
m_coordinate_list->at(current_edge.u),
|
||||
m_coordinate_list->at(current_edge.v), input_coordinate);
|
||||
m_coordinate_list->at(current_edge.v), input_coordinate,
|
||||
projected_coordinate);
|
||||
// distance must be non-negative
|
||||
BOOST_ASSERT(0. <= current_perpendicular_distance);
|
||||
BOOST_ASSERT(0.f <= current_perpendicular_distance);
|
||||
|
||||
if (current_perpendicular_distance < current_min_dist)
|
||||
if (pruning_bound.get() >= current_perpendicular_distance ||
|
||||
current_edge.is_in_tiny_cc())
|
||||
{
|
||||
pruning_bound.insert(current_perpendicular_distance);
|
||||
traversal_queue.emplace(current_perpendicular_distance, current_edge);
|
||||
}
|
||||
else
|
||||
{
|
||||
++pruned_elements;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// for each child mbr
|
||||
for (uint32_t i = 0; i < current_tree_node.child_count; ++i)
|
||||
// for each child mbr get a lower bound and enqueue it
|
||||
for (const auto i : osrm::irange(0u, current_tree_node.child_count))
|
||||
{
|
||||
const int32_t child_id = current_tree_node.children[i];
|
||||
const TreeNode &child_tree_node = m_search_tree[child_id];
|
||||
@@ -863,99 +869,89 @@ class StaticRTree
|
||||
child_tree_node.minimum_bounding_rectangle;
|
||||
const float lower_bound_to_element =
|
||||
child_rectangle.GetMinDist(input_coordinate);
|
||||
BOOST_ASSERT(0.f <= lower_bound_to_element);
|
||||
|
||||
// TODO - enough elements found, i.e. nearest distance > maximum distance?
|
||||
// ie. some measure of 'confidence of accuracy'
|
||||
|
||||
// check if it needs to be explored by mindist
|
||||
if (lower_bound_to_element < current_min_dist)
|
||||
{
|
||||
traversal_queue.emplace(lower_bound_to_element, child_tree_node);
|
||||
}
|
||||
traversal_queue.emplace(lower_bound_to_element, child_tree_node);
|
||||
}
|
||||
// SimpleLogger().Write(logDEBUG) << "added " << current_tree_node.child_count
|
||||
// << " mbrs into queue of " << traversal_queue.size();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
++inspected_segments;
|
||||
{ // current object is a leaf node
|
||||
++inspected_elements;
|
||||
// inspecting an actual road segment
|
||||
const EdgeDataT ¤t_segment =
|
||||
current_query_node.node.template get<EdgeDataT>();
|
||||
|
||||
// don't collect too many results from small components
|
||||
if (number_of_results_found_in_big_cc == number_of_results &&
|
||||
!current_segment.is_in_tiny_cc)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// don't collect too many results from big components
|
||||
if (number_of_results_found_in_tiny_cc == number_of_results &&
|
||||
current_segment.is_in_tiny_cc)
|
||||
// continue searching for the first segment from a big component
|
||||
if (number_of_elements_from_big_cc == 0 &&
|
||||
number_of_elements_from_tiny_cc >= max_number_of_phantom_nodes &&
|
||||
current_segment.is_in_tiny_cc())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
// check if it is smaller than what we had before
|
||||
float current_ratio = 0.;
|
||||
float current_ratio = 0.f;
|
||||
FixedPointCoordinate foot_point_coordinate_on_segment;
|
||||
|
||||
const float current_perpendicular_distance =
|
||||
coordinate_calculation::perpendicular_distance(
|
||||
m_coordinate_list->at(current_segment.u),
|
||||
m_coordinate_list->at(current_segment.v), input_coordinate,
|
||||
foot_point_coordinate_on_segment, current_ratio);
|
||||
coordinate_calculation::perpendicular_distance_from_projected_coordinate(
|
||||
m_coordinate_list->at(current_segment.u),
|
||||
m_coordinate_list->at(current_segment.v), input_coordinate,
|
||||
projected_coordinate, foot_point_coordinate_on_segment, current_ratio);
|
||||
|
||||
BOOST_ASSERT(0. <= current_perpendicular_distance);
|
||||
// store phantom node in result vector
|
||||
result_phantom_node_vector.emplace_back(
|
||||
PhantomNode( current_segment.forward_edge_based_node_id,
|
||||
current_segment.reverse_edge_based_node_id, current_segment.name_id,
|
||||
current_segment.forward_weight, current_segment.reverse_weight,
|
||||
current_segment.forward_offset, current_segment.reverse_offset,
|
||||
current_segment.packed_geometry_id, current_segment.component_id,
|
||||
foot_point_coordinate_on_segment, current_segment.fwd_segment_position,
|
||||
current_segment.forward_travel_mode, current_segment.backward_travel_mode),
|
||||
current_perpendicular_distance);
|
||||
|
||||
if ((current_perpendicular_distance < current_min_dist) &&
|
||||
!osrm::epsilon_compare(current_perpendicular_distance, current_min_dist))
|
||||
{
|
||||
// store phantom node in result vector
|
||||
result_phantom_node_vector.emplace_back(current_segment,
|
||||
foot_point_coordinate_on_segment,
|
||||
current_perpendicular_distance);
|
||||
// Hack to fix rounding errors and wandering via nodes.
|
||||
FixUpRoundingIssue(input_coordinate, result_phantom_node_vector.back().first);
|
||||
|
||||
// Hack to fix rounding errors and wandering via nodes.
|
||||
FixUpRoundingIssue(input_coordinate, result_phantom_node_vector.back());
|
||||
// set forward and reverse weights on the phantom node
|
||||
SetForwardAndReverseWeightsOnPhantomNode(current_segment,
|
||||
result_phantom_node_vector.back().first);
|
||||
|
||||
// set forward and reverse weights on the phantom node
|
||||
SetForwardAndReverseWeightsOnPhantomNode(current_segment,
|
||||
result_phantom_node_vector.back());
|
||||
|
||||
// do we have results only in a small scc
|
||||
if (current_segment.is_in_tiny_cc)
|
||||
{
|
||||
++number_of_results_found_in_tiny_cc;
|
||||
}
|
||||
else
|
||||
{
|
||||
// found an element in a large component
|
||||
min_found_distances[number_of_results_found_in_big_cc] =
|
||||
current_perpendicular_distance;
|
||||
++number_of_results_found_in_big_cc;
|
||||
// SimpleLogger().Write(logDEBUG) << std::setprecision(8) <<
|
||||
// foot_point_coordinate_on_segment << " at " <<
|
||||
// current_perpendicular_distance;
|
||||
}
|
||||
// update counts on what we found from which result class
|
||||
if (current_segment.is_in_tiny_cc())
|
||||
{ // found an element in tiny component
|
||||
++number_of_elements_from_tiny_cc;
|
||||
}
|
||||
else
|
||||
{ // found an element in a big component
|
||||
++number_of_elements_from_big_cc;
|
||||
}
|
||||
}
|
||||
|
||||
// TODO add indicator to prune if maxdist > threshold
|
||||
if (number_of_results == number_of_results_found_in_big_cc ||
|
||||
inspected_segments >= max_checked_segments)
|
||||
// stop the search by flushing the queue
|
||||
if ((result_phantom_node_vector.size() >= max_number_of_phantom_nodes &&
|
||||
number_of_elements_from_big_cc > 0) ||
|
||||
inspected_elements >= max_checked_elements)
|
||||
{
|
||||
// SimpleLogger().Write(logDEBUG) << "flushing queue of " << traversal_queue.size()
|
||||
// << " elements";
|
||||
// work-around for traversal_queue.clear();
|
||||
traversal_queue = std::priority_queue<IncrementalQueryCandidate>{};
|
||||
}
|
||||
}
|
||||
// SimpleLogger().Write() << "result_phantom_node_vector.size(): " <<
|
||||
// result_phantom_node_vector.size();
|
||||
// SimpleLogger().Write() << "max_number_of_phantom_nodes: " << max_number_of_phantom_nodes;
|
||||
// SimpleLogger().Write() << "number_of_elements_from_big_cc: " <<
|
||||
// number_of_elements_from_big_cc;
|
||||
// SimpleLogger().Write() << "number_of_elements_from_tiny_cc: " <<
|
||||
// number_of_elements_from_tiny_cc;
|
||||
// SimpleLogger().Write() << "inspected_elements: " << inspected_elements;
|
||||
// SimpleLogger().Write() << "max_checked_elements: " << max_checked_elements;
|
||||
// SimpleLogger().Write() << "pruned_elements: " << pruned_elements;
|
||||
|
||||
return !result_phantom_node_vector.empty();
|
||||
}
|
||||
|
||||
|
||||
bool FindPhantomNodeForCoordinate(const FixedPointCoordinate &input_coordinate,
|
||||
PhantomNode &result_phantom_node,
|
||||
const unsigned zoom_level)
|
||||
|
||||
Reference in New Issue
Block a user