2014-11-28 06:13:18 -05:00
|
|
|
#ifndef STATIC_RTREE_HPP
|
|
|
|
#define STATIC_RTREE_HPP
|
2013-06-26 09:43:13 -04:00
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "util/deallocating_vector.hpp"
|
|
|
|
#include "util/hilbert_value.hpp"
|
|
|
|
#include "util/rectangle.hpp"
|
|
|
|
#include "util/shared_memory_vector_wrapper.hpp"
|
2013-09-23 12:03:07 -04:00
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "util/bearing.hpp"
|
|
|
|
#include "util/integer_range.hpp"
|
|
|
|
#include "util/mercator.hpp"
|
|
|
|
#include "util/osrm_exception.hpp"
|
|
|
|
#include "util/typedefs.hpp"
|
2013-06-26 09:43:13 -04:00
|
|
|
|
2016-01-02 11:13:44 -05:00
|
|
|
#include "osrm/coordinate.hpp"
|
2013-12-17 11:59:44 -05:00
|
|
|
|
2013-06-26 09:43:13 -04:00
|
|
|
#include <boost/assert.hpp>
|
2013-08-09 11:47:11 -04:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2013-06-26 09:43:13 -04:00
|
|
|
|
2014-05-20 17:59:30 -04:00
|
|
|
#include <tbb/parallel_for.h>
|
2014-05-22 13:07:29 -04:00
|
|
|
#include <tbb/parallel_sort.h>
|
2014-05-20 17:59:30 -04:00
|
|
|
|
2014-08-28 04:50:42 -04:00
|
|
|
#include <variant/variant.hpp>
|
|
|
|
|
2013-06-26 09:43:13 -04:00
|
|
|
#include <algorithm>
|
2014-05-06 09:20:31 -04:00
|
|
|
#include <array>
|
2013-10-02 05:22:42 -04:00
|
|
|
#include <limits>
|
2014-05-09 10:47:42 -04:00
|
|
|
#include <memory>
|
2013-06-26 09:43:13 -04:00
|
|
|
#include <queue>
|
2013-07-22 10:32:19 -04:00
|
|
|
#include <string>
|
2013-06-26 09:43:13 -04:00
|
|
|
#include <vector>
|
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
|
|
|
|
2015-12-03 14:04:23 -05:00
|
|
|
// Static RTree for serving nearest neighbour queries
|
2014-06-23 10:54:31 -04:00
|
|
|
template <class EdgeDataT,
|
2014-05-06 09:20:31 -04:00
|
|
|
class CoordinateListT = std::vector<FixedPointCoordinate>,
|
2014-06-27 10:59:11 -04:00
|
|
|
bool UseSharedMemory = false,
|
2015-01-27 11:44:46 -05:00
|
|
|
uint32_t BRANCHING_FACTOR = 64,
|
|
|
|
uint32_t LEAF_NODE_SIZE = 1024>
|
2014-05-07 12:39:16 -04:00
|
|
|
class StaticRTree
|
2014-05-06 09:20:31 -04:00
|
|
|
{
|
|
|
|
public:
|
2015-12-03 14:04:23 -05:00
|
|
|
using Rectangle = RectangleInt2D;
|
|
|
|
using EdgeData = EdgeDataT;
|
|
|
|
using CoordinateList = CoordinateListT;
|
2014-08-29 06:37:07 -04:00
|
|
|
|
2015-12-03 14:04:23 -05:00
|
|
|
static constexpr std::size_t MAX_CHECKED_ELEMENTS = 4 * LEAF_NODE_SIZE;
|
2013-06-26 09:43:13 -04:00
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
struct TreeNode
|
|
|
|
{
|
2014-05-09 10:48:58 -04:00
|
|
|
TreeNode() : child_count(0), child_is_on_disk(false) {}
|
2015-12-03 14:04:23 -05:00
|
|
|
Rectangle minimum_bounding_rectangle;
|
2014-05-06 09:20:31 -04:00
|
|
|
uint32_t child_count : 31;
|
|
|
|
bool child_is_on_disk : 1;
|
2014-06-27 10:59:11 -04:00
|
|
|
uint32_t children[BRANCHING_FACTOR];
|
2013-09-24 06:07:34 -04:00
|
|
|
};
|
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
private:
|
|
|
|
struct WrappedInputElement
|
|
|
|
{
|
2014-05-29 11:16:24 -04:00
|
|
|
explicit WrappedInputElement(const uint64_t _hilbert_value, const uint32_t _array_index)
|
|
|
|
: m_hilbert_value(_hilbert_value), m_array_index(_array_index)
|
2014-05-06 09:20:31 -04:00
|
|
|
{
|
|
|
|
}
|
2013-06-27 10:57:40 -04:00
|
|
|
|
2014-05-29 11:16:24 -04:00
|
|
|
WrappedInputElement() : m_hilbert_value(0), m_array_index(UINT_MAX) {}
|
2013-06-26 09:43:13 -04:00
|
|
|
|
|
|
|
uint64_t m_hilbert_value;
|
2014-05-29 11:16:24 -04:00
|
|
|
uint32_t m_array_index;
|
2013-06-26 09:43:13 -04:00
|
|
|
|
2014-10-21 12:34:50 -04:00
|
|
|
inline bool operator<(const WrappedInputElement &other) const
|
2014-05-06 09:20:31 -04:00
|
|
|
{
|
2013-06-26 09:43:13 -04:00
|
|
|
return m_hilbert_value < other.m_hilbert_value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
struct LeafNode
|
|
|
|
{
|
2014-06-27 10:59:11 -04:00
|
|
|
LeafNode() : object_count(0), objects() {}
|
2013-06-26 09:43:13 -04:00
|
|
|
uint32_t object_count;
|
2014-06-27 10:59:11 -04:00
|
|
|
std::array<EdgeDataT, LEAF_NODE_SIZE> objects;
|
2013-06-26 09:43:13 -04:00
|
|
|
};
|
|
|
|
|
2015-12-03 14:04:23 -05:00
|
|
|
using QueryNodeType = mapbox::util::variant<TreeNode, EdgeDataT>;
|
2014-05-06 09:20:31 -04:00
|
|
|
struct QueryCandidate
|
|
|
|
{
|
2014-10-21 12:34:50 -04:00
|
|
|
inline bool operator<(const QueryCandidate &other) const
|
2014-05-06 09:20:31 -04:00
|
|
|
{
|
2014-05-29 09:36:14 -04:00
|
|
|
// Attn: this is reversed order. std::pq is a max pq!
|
|
|
|
return other.min_dist < min_dist;
|
2013-06-26 09:43:13 -04:00
|
|
|
}
|
2014-06-10 11:26:05 -04:00
|
|
|
|
2014-06-23 10:54:31 -04:00
|
|
|
float min_dist;
|
2015-12-03 14:04:23 -05:00
|
|
|
QueryNodeType node;
|
2014-06-23 10:54:31 -04:00
|
|
|
};
|
2014-06-10 11:26:05 -04:00
|
|
|
|
2013-09-23 12:03:07 -04:00
|
|
|
typename ShM<TreeNode, UseSharedMemory>::vector m_search_tree;
|
2013-06-26 09:43:13 -04:00
|
|
|
uint64_t m_element_count;
|
2013-07-22 10:32:19 -04:00
|
|
|
const std::string m_leaf_node_filename;
|
2014-05-09 10:47:42 -04:00
|
|
|
std::shared_ptr<CoordinateListT> m_coordinate_list;
|
2014-06-27 10:25:55 -04:00
|
|
|
boost::filesystem::ifstream leaves_stream;
|
2014-02-21 10:55:41 -05:00
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
public:
|
2014-05-07 12:39:16 -04:00
|
|
|
StaticRTree() = delete;
|
|
|
|
StaticRTree(const StaticRTree &) = delete;
|
|
|
|
|
2015-12-03 14:04:23 -05:00
|
|
|
template <typename CoordinateT>
|
2014-05-06 09:20:31 -04:00
|
|
|
// Construct a packed Hilbert-R-Tree with Kamel-Faloutsos algorithm [1]
|
2015-04-21 04:43:02 -04:00
|
|
|
explicit StaticRTree(const std::vector<EdgeDataT> &input_data_vector,
|
2015-09-08 17:34:20 -04:00
|
|
|
const std::string &tree_node_filename,
|
|
|
|
const std::string &leaf_node_filename,
|
2015-12-03 14:04:23 -05:00
|
|
|
const std::vector<CoordinateT> &coordinate_list)
|
2014-07-20 18:24:40 -04:00
|
|
|
: m_element_count(input_data_vector.size()), m_leaf_node_filename(leaf_node_filename)
|
2013-06-27 10:57:40 -04:00
|
|
|
{
|
2014-07-20 18:24:40 -04:00
|
|
|
std::vector<WrappedInputElement> input_wrapper_vector(m_element_count);
|
2013-06-26 09:43:13 -04:00
|
|
|
|
2014-03-17 10:28:42 -04:00
|
|
|
HilbertCode get_hilbert_number;
|
|
|
|
|
2014-05-20 17:59:30 -04:00
|
|
|
// generate auxiliary vector of hilbert-values
|
2014-06-23 10:54:31 -04:00
|
|
|
tbb::parallel_for(
|
2014-07-20 18:24:40 -04:00
|
|
|
tbb::blocked_range<uint64_t>(0, m_element_count),
|
2015-12-03 14:04:23 -05:00
|
|
|
[&input_data_vector, &input_wrapper_vector, &get_hilbert_number,
|
|
|
|
&coordinate_list](const tbb::blocked_range<uint64_t> &range)
|
2014-05-20 17:59:30 -04:00
|
|
|
{
|
2015-09-14 19:13:31 -04:00
|
|
|
for (uint64_t element_counter = range.begin(), end = range.end();
|
|
|
|
element_counter != end; ++element_counter)
|
2014-05-20 17:59:30 -04:00
|
|
|
{
|
|
|
|
WrappedInputElement ¤t_wrapper = input_wrapper_vector[element_counter];
|
|
|
|
current_wrapper.m_array_index = element_counter;
|
|
|
|
|
2014-06-23 10:54:31 -04:00
|
|
|
EdgeDataT const ¤t_element = input_data_vector[element_counter];
|
2014-05-20 17:59:30 -04:00
|
|
|
|
|
|
|
// Get Hilbert-Value for centroid in mercartor projection
|
2014-06-23 10:54:31 -04:00
|
|
|
FixedPointCoordinate current_centroid = EdgeDataT::Centroid(
|
|
|
|
FixedPointCoordinate(coordinate_list.at(current_element.u).lat,
|
|
|
|
coordinate_list.at(current_element.u).lon),
|
|
|
|
FixedPointCoordinate(coordinate_list.at(current_element.v).lat,
|
|
|
|
coordinate_list.at(current_element.v).lon));
|
2014-05-20 17:59:30 -04:00
|
|
|
current_centroid.lat =
|
2015-01-27 11:44:46 -05:00
|
|
|
COORDINATE_PRECISION *
|
2016-01-11 08:06:49 -05:00
|
|
|
mercator::latToY(current_centroid.lat / COORDINATE_PRECISION);
|
2014-05-20 17:59:30 -04:00
|
|
|
|
|
|
|
current_wrapper.m_hilbert_value = get_hilbert_number(current_centroid);
|
|
|
|
}
|
2014-06-23 10:54:31 -04:00
|
|
|
});
|
2013-11-22 12:05:47 -05:00
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
// open leaf file
|
2013-08-09 11:47:11 -04:00
|
|
|
boost::filesystem::ofstream leaf_node_file(leaf_node_filename, std::ios::binary);
|
2014-07-20 18:24:40 -04:00
|
|
|
leaf_node_file.write((char *)&m_element_count, sizeof(uint64_t));
|
2013-06-26 09:43:13 -04:00
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
// sort the hilbert-value representatives
|
2015-09-10 05:36:02 -04:00
|
|
|
tbb::parallel_sort(input_wrapper_vector.begin(), input_wrapper_vector.end());
|
2013-06-26 09:43:13 -04:00
|
|
|
std::vector<TreeNode> tree_nodes_in_level;
|
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
// pack M elements into leaf node and write to leaf file
|
2013-06-26 09:43:13 -04:00
|
|
|
uint64_t processed_objects_count = 0;
|
2014-07-20 18:24:40 -04:00
|
|
|
while (processed_objects_count < m_element_count)
|
2014-05-06 09:20:31 -04:00
|
|
|
{
|
2013-06-26 09:43:13 -04:00
|
|
|
|
|
|
|
LeafNode current_leaf;
|
|
|
|
TreeNode current_node;
|
2014-06-27 10:59:11 -04:00
|
|
|
for (uint32_t current_element_index = 0; LEAF_NODE_SIZE > current_element_index;
|
2014-05-06 09:20:31 -04:00
|
|
|
++current_element_index)
|
|
|
|
{
|
2014-07-20 18:24:40 -04:00
|
|
|
if (m_element_count > (processed_objects_count + current_element_index))
|
2014-05-06 09:20:31 -04:00
|
|
|
{
|
|
|
|
uint32_t index_of_next_object =
|
|
|
|
input_wrapper_vector[processed_objects_count + current_element_index]
|
|
|
|
.m_array_index;
|
|
|
|
current_leaf.objects[current_element_index] =
|
|
|
|
input_data_vector[index_of_next_object];
|
2013-06-26 09:43:13 -04:00
|
|
|
++current_leaf.object_count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
// generate tree node that resemble the objects in leaf and store it for next level
|
2015-01-27 11:44:46 -05:00
|
|
|
InitializeMBRectangle(current_node.minimum_bounding_rectangle, current_leaf.objects,
|
|
|
|
current_leaf.object_count, coordinate_list);
|
2013-06-26 09:43:13 -04:00
|
|
|
current_node.child_is_on_disk = true;
|
|
|
|
current_node.children[0] = tree_nodes_in_level.size();
|
2014-05-06 09:20:31 -04:00
|
|
|
tree_nodes_in_level.emplace_back(current_node);
|
2013-06-26 09:43:13 -04:00
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
// write leaf_node to leaf node file
|
|
|
|
leaf_node_file.write((char *)¤t_leaf, sizeof(current_leaf));
|
2013-06-26 09:43:13 -04:00
|
|
|
processed_objects_count += current_leaf.object_count;
|
|
|
|
}
|
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
// close leaf file
|
2013-06-26 09:43:13 -04:00
|
|
|
leaf_node_file.close();
|
|
|
|
|
|
|
|
uint32_t processing_level = 0;
|
2014-05-06 09:20:31 -04:00
|
|
|
while (1 < tree_nodes_in_level.size())
|
|
|
|
{
|
2013-06-26 09:43:13 -04:00
|
|
|
std::vector<TreeNode> tree_nodes_in_next_level;
|
|
|
|
uint32_t processed_tree_nodes_in_level = 0;
|
2014-05-06 09:20:31 -04:00
|
|
|
while (processed_tree_nodes_in_level < tree_nodes_in_level.size())
|
|
|
|
{
|
2013-06-26 09:43:13 -04:00
|
|
|
TreeNode parent_node;
|
2014-06-27 10:59:11 -04:00
|
|
|
// pack BRANCHING_FACTOR elements into tree_nodes each
|
2014-05-06 09:20:31 -04:00
|
|
|
for (uint32_t current_child_node_index = 0;
|
2015-01-27 11:44:46 -05:00
|
|
|
BRANCHING_FACTOR > current_child_node_index; ++current_child_node_index)
|
2014-05-06 09:20:31 -04:00
|
|
|
{
|
|
|
|
if (processed_tree_nodes_in_level < tree_nodes_in_level.size())
|
|
|
|
{
|
|
|
|
TreeNode ¤t_child_node =
|
|
|
|
tree_nodes_in_level[processed_tree_nodes_in_level];
|
|
|
|
// add tree node to parent entry
|
2014-07-20 18:24:40 -04:00
|
|
|
parent_node.children[current_child_node_index] = m_search_tree.size();
|
|
|
|
m_search_tree.emplace_back(current_child_node);
|
2014-06-19 11:52:59 -04:00
|
|
|
// merge MBRs
|
2014-05-29 09:36:14 -04:00
|
|
|
parent_node.minimum_bounding_rectangle.MergeBoundingBoxes(
|
2014-05-06 09:20:31 -04:00
|
|
|
current_child_node.minimum_bounding_rectangle);
|
|
|
|
// increase counters
|
2013-06-26 09:43:13 -04:00
|
|
|
++parent_node.child_count;
|
|
|
|
++processed_tree_nodes_in_level;
|
|
|
|
}
|
|
|
|
}
|
2014-05-06 09:20:31 -04:00
|
|
|
tree_nodes_in_next_level.emplace_back(parent_node);
|
2013-06-26 09:43:13 -04:00
|
|
|
}
|
|
|
|
tree_nodes_in_level.swap(tree_nodes_in_next_level);
|
|
|
|
++processing_level;
|
|
|
|
}
|
|
|
|
BOOST_ASSERT_MSG(1 == tree_nodes_in_level.size(), "tree broken, more than one root node");
|
2014-05-06 09:20:31 -04:00
|
|
|
// last remaining entry is the root node, store it
|
2014-07-20 18:24:40 -04:00
|
|
|
m_search_tree.emplace_back(tree_nodes_in_level[0]);
|
2013-06-26 09:43:13 -04:00
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
// reverse and renumber tree to have root at index 0
|
2014-07-20 18:24:40 -04:00
|
|
|
std::reverse(m_search_tree.begin(), m_search_tree.end());
|
2013-11-22 12:05:47 -05:00
|
|
|
|
2014-07-20 18:24:40 -04:00
|
|
|
uint32_t search_tree_size = m_search_tree.size();
|
2014-05-20 17:59:30 -04:00
|
|
|
tbb::parallel_for(tbb::blocked_range<uint32_t>(0, search_tree_size),
|
2014-07-20 18:24:40 -04:00
|
|
|
[this, &search_tree_size](const tbb::blocked_range<uint32_t> &range)
|
|
|
|
{
|
2015-09-14 19:13:31 -04:00
|
|
|
for (uint32_t i = range.begin(), end = range.end(); i != end; ++i)
|
2015-01-27 11:44:46 -05:00
|
|
|
{
|
|
|
|
TreeNode ¤t_tree_node = this->m_search_tree[i];
|
|
|
|
for (uint32_t j = 0; j < current_tree_node.child_count; ++j)
|
|
|
|
{
|
|
|
|
const uint32_t old_id = current_tree_node.children[j];
|
|
|
|
const uint32_t new_id = search_tree_size - old_id - 1;
|
|
|
|
current_tree_node.children[j] = new_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2013-06-26 09:43:13 -04:00
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
// open tree file
|
|
|
|
boost::filesystem::ofstream tree_node_file(tree_node_filename, std::ios::binary);
|
2013-08-09 11:47:11 -04:00
|
|
|
|
2014-07-20 18:24:40 -04:00
|
|
|
uint32_t size_of_tree = m_search_tree.size();
|
2013-06-26 09:43:13 -04:00
|
|
|
BOOST_ASSERT_MSG(0 < size_of_tree, "tree empty");
|
2014-07-20 18:24:40 -04:00
|
|
|
tree_node_file.write((char *)&size_of_tree, sizeof(uint32_t));
|
|
|
|
tree_node_file.write((char *)&m_search_tree[0], sizeof(TreeNode) * size_of_tree);
|
2014-05-06 09:20:31 -04:00
|
|
|
// close tree node file.
|
2013-06-26 09:43:13 -04:00
|
|
|
tree_node_file.close();
|
|
|
|
}
|
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
explicit StaticRTree(const boost::filesystem::path &node_file,
|
|
|
|
const boost::filesystem::path &leaf_file,
|
2014-05-09 10:47:42 -04:00
|
|
|
const std::shared_ptr<CoordinateListT> coordinate_list)
|
2014-05-06 09:20:31 -04:00
|
|
|
: m_leaf_node_filename(leaf_file.string())
|
|
|
|
{
|
|
|
|
// open tree node file and load into RAM.
|
2014-02-21 10:55:41 -05:00
|
|
|
m_coordinate_list = coordinate_list;
|
2013-08-09 11:47:11 -04:00
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
if (!boost::filesystem::exists(node_file))
|
|
|
|
{
|
2016-01-05 10:51:13 -05:00
|
|
|
throw exception("ram index file does not exist");
|
2013-08-09 11:47:11 -04:00
|
|
|
}
|
2014-05-06 09:20:31 -04:00
|
|
|
if (0 == boost::filesystem::file_size(node_file))
|
|
|
|
{
|
2016-01-05 10:51:13 -05:00
|
|
|
throw exception("ram index file is empty");
|
2013-08-09 11:47:11 -04:00
|
|
|
}
|
2014-05-06 09:20:31 -04:00
|
|
|
boost::filesystem::ifstream tree_node_file(node_file, std::ios::binary);
|
2013-08-09 11:47:11 -04:00
|
|
|
|
2013-06-26 09:43:13 -04:00
|
|
|
uint32_t tree_size = 0;
|
2014-05-06 09:20:31 -04:00
|
|
|
tree_node_file.read((char *)&tree_size, sizeof(uint32_t));
|
2013-11-22 12:05:47 -05:00
|
|
|
|
2013-06-26 09:43:13 -04:00
|
|
|
m_search_tree.resize(tree_size);
|
2014-06-05 13:14:36 -04:00
|
|
|
if (tree_size > 0)
|
|
|
|
{
|
|
|
|
tree_node_file.read((char *)&m_search_tree[0], sizeof(TreeNode) * tree_size);
|
|
|
|
}
|
2013-06-26 09:43:13 -04:00
|
|
|
tree_node_file.close();
|
2014-05-06 09:20:31 -04:00
|
|
|
// open leaf node file and store thread specific pointer
|
|
|
|
if (!boost::filesystem::exists(leaf_file))
|
|
|
|
{
|
2016-01-05 10:51:13 -05:00
|
|
|
throw exception("mem index file does not exist");
|
2013-09-23 12:03:07 -04:00
|
|
|
}
|
2014-05-06 09:20:31 -04:00
|
|
|
if (0 == boost::filesystem::file_size(leaf_file))
|
|
|
|
{
|
2016-01-05 10:51:13 -05:00
|
|
|
throw exception("mem index file is empty");
|
2013-09-23 12:03:07 -04:00
|
|
|
}
|
|
|
|
|
2014-06-27 10:25:55 -04:00
|
|
|
leaves_stream.open(leaf_file, std::ios::binary);
|
|
|
|
leaves_stream.read((char *)&m_element_count, sizeof(uint64_t));
|
2013-09-23 12:03:07 -04:00
|
|
|
}
|
|
|
|
|
2014-05-06 09:20:31 -04:00
|
|
|
explicit StaticRTree(TreeNode *tree_node_ptr,
|
2014-07-15 09:25:44 -04:00
|
|
|
const uint64_t number_of_nodes,
|
2014-05-06 09:20:31 -04:00
|
|
|
const boost::filesystem::path &leaf_file,
|
2014-05-09 10:47:42 -04:00
|
|
|
std::shared_ptr<CoordinateListT> coordinate_list)
|
2014-05-06 09:20:31 -04:00
|
|
|
: m_search_tree(tree_node_ptr, number_of_nodes), m_leaf_node_filename(leaf_file.string()),
|
2015-08-18 06:56:34 -04:00
|
|
|
m_coordinate_list(std::move(coordinate_list))
|
2013-09-24 06:07:34 -04:00
|
|
|
{
|
2014-05-06 09:20:31 -04:00
|
|
|
// open leaf node file and store thread specific pointer
|
|
|
|
if (!boost::filesystem::exists(leaf_file))
|
|
|
|
{
|
2016-01-05 10:51:13 -05:00
|
|
|
throw exception("mem index file does not exist");
|
2013-08-09 11:47:11 -04:00
|
|
|
}
|
2014-05-06 09:20:31 -04:00
|
|
|
if (0 == boost::filesystem::file_size(leaf_file))
|
|
|
|
{
|
2016-01-05 10:51:13 -05:00
|
|
|
throw exception("mem index file is empty");
|
2013-08-09 11:47:11 -04:00
|
|
|
}
|
|
|
|
|
2014-06-27 10:25:55 -04:00
|
|
|
leaves_stream.open(leaf_file, std::ios::binary);
|
|
|
|
leaves_stream.read((char *)&m_element_count, sizeof(uint64_t));
|
2013-08-26 08:16:34 -04:00
|
|
|
}
|
|
|
|
|
2015-12-03 14:04:23 -05:00
|
|
|
// Override filter and terminator for the desired behaviour.
|
|
|
|
std::vector<EdgeDataT> Nearest(const FixedPointCoordinate &input_coordinate,
|
2016-01-05 06:04:04 -05:00
|
|
|
const std::size_t max_results)
|
2015-09-21 21:34:37 -04:00
|
|
|
{
|
2015-12-03 14:04:23 -05:00
|
|
|
return Nearest(input_coordinate,
|
|
|
|
[](const EdgeDataT &)
|
|
|
|
{
|
|
|
|
return std::make_pair(true, true);
|
|
|
|
},
|
|
|
|
[max_results](const std::size_t num_results, const float)
|
|
|
|
{
|
|
|
|
return num_results >= max_results;
|
|
|
|
});
|
2015-09-21 21:34:37 -04:00
|
|
|
}
|
|
|
|
|
2015-12-03 14:04:23 -05:00
|
|
|
// Override filter and terminator for the desired behaviour.
|
|
|
|
template <typename FilterT, typename TerminationT>
|
|
|
|
std::vector<EdgeDataT> Nearest(const FixedPointCoordinate &input_coordinate,
|
2016-01-05 06:04:04 -05:00
|
|
|
const FilterT filter,
|
|
|
|
const TerminationT terminate)
|
2014-06-10 11:26:05 -04:00
|
|
|
{
|
2015-12-03 14:04:23 -05:00
|
|
|
std::vector<EdgeDataT> results;
|
2015-01-27 11:44:46 -05:00
|
|
|
std::pair<double, double> projected_coordinate = {
|
2016-01-11 08:06:49 -05:00
|
|
|
mercator::latToY(input_coordinate.lat / COORDINATE_PRECISION),
|
2015-01-27 11:44:46 -05:00
|
|
|
input_coordinate.lon / COORDINATE_PRECISION};
|
2015-01-20 10:24:49 -05:00
|
|
|
|
2014-06-23 10:54:31 -04:00
|
|
|
// initialize queue with root element
|
2015-12-03 14:04:23 -05:00
|
|
|
std::priority_queue<QueryCandidate> traversal_queue;
|
2016-01-05 06:04:04 -05:00
|
|
|
traversal_queue.push(QueryCandidate{0.f, m_search_tree[0]});
|
2014-06-23 10:54:31 -04:00
|
|
|
|
|
|
|
while (!traversal_queue.empty())
|
|
|
|
{
|
2015-12-03 14:04:23 -05:00
|
|
|
const QueryCandidate current_query_node = traversal_queue.top();
|
|
|
|
if (terminate(results.size(), current_query_node.min_dist))
|
2015-05-14 16:24:07 -04:00
|
|
|
{
|
2015-12-03 14:04:23 -05:00
|
|
|
traversal_queue = std::priority_queue<QueryCandidate>{};
|
2015-05-14 16:24:07 -04:00
|
|
|
break;
|
|
|
|
}
|
2014-12-30 07:06:25 -05:00
|
|
|
|
2014-07-23 13:25:09 -04:00
|
|
|
traversal_queue.pop();
|
|
|
|
|
2014-09-23 12:46:14 -04:00
|
|
|
if (current_query_node.node.template is<TreeNode>())
|
|
|
|
{ // current object is a tree node
|
2015-01-27 11:44:46 -05:00
|
|
|
const TreeNode ¤t_tree_node =
|
|
|
|
current_query_node.node.template get<TreeNode>();
|
2014-07-23 13:25:09 -04:00
|
|
|
if (current_tree_node.child_is_on_disk)
|
|
|
|
{
|
2015-12-03 14:04:23 -05:00
|
|
|
ExploreLeafNode(current_tree_node.children[0], input_coordinate,
|
|
|
|
projected_coordinate, traversal_queue);
|
2014-07-23 13:25:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-12-03 14:04:23 -05:00
|
|
|
ExploreTreeNode(current_tree_node, input_coordinate, traversal_queue);
|
2014-07-23 13:25:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-12-03 14:04:23 -05:00
|
|
|
{
|
2014-07-23 13:25:09 -04:00
|
|
|
// inspecting an actual road segment
|
2015-12-03 14:04:23 -05:00
|
|
|
const auto ¤t_segment = current_query_node.node.template get<EdgeDataT>();
|
2014-07-23 13:25:09 -04:00
|
|
|
|
2015-12-03 14:04:23 -05:00
|
|
|
auto use_segment = filter(current_segment);
|
|
|
|
if (!use_segment.first && !use_segment.second)
|
2014-12-08 17:46:31 -05:00
|
|
|
{
|
2015-09-21 21:34:37 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-09-23 12:46:14 -04:00
|
|
|
// store phantom node in result vector
|
2015-12-09 16:34:22 -05:00
|
|
|
results.push_back(std::move(current_segment));
|
2014-12-23 11:18:32 -05:00
|
|
|
|
2015-12-03 14:04:23 -05:00
|
|
|
if (!use_segment.first)
|
2014-12-23 11:18:32 -05:00
|
|
|
{
|
2015-12-03 14:04:23 -05:00
|
|
|
results.back().forward_edge_based_node_id = SPECIAL_NODEID;
|
2014-12-23 11:18:32 -05:00
|
|
|
}
|
2015-12-03 14:04:23 -05:00
|
|
|
else if (!use_segment.second)
|
2014-12-23 11:18:32 -05:00
|
|
|
{
|
2015-12-03 14:04:23 -05:00
|
|
|
results.back().reverse_edge_based_node_id = SPECIAL_NODEID;
|
2014-12-23 11:18:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 14:04:23 -05:00
|
|
|
return results;
|
2014-12-23 11:18:32 -05:00
|
|
|
}
|
2014-06-23 10:54:31 -04:00
|
|
|
|
|
|
|
private:
|
2015-12-03 14:04:23 -05:00
|
|
|
template <typename QueueT>
|
|
|
|
void ExploreLeafNode(const std::uint32_t leaf_id,
|
|
|
|
const FixedPointCoordinate &input_coordinate,
|
|
|
|
const std::pair<double, double> &projected_coordinate,
|
|
|
|
QueueT &traversal_queue)
|
2014-06-23 10:54:31 -04:00
|
|
|
{
|
2015-12-03 14:04:23 -05:00
|
|
|
LeafNode current_leaf_node;
|
|
|
|
LoadLeafFromDisk(leaf_id, current_leaf_node);
|
2014-06-23 10:54:31 -04:00
|
|
|
|
2015-12-03 14:04:23 -05:00
|
|
|
// current object represents a block on disk
|
2016-01-05 10:51:13 -05:00
|
|
|
for (const auto i : irange(0u, current_leaf_node.object_count))
|
2015-01-27 11:44:46 -05:00
|
|
|
{
|
2015-12-03 14:04:23 -05:00
|
|
|
auto ¤t_edge = current_leaf_node.objects[i];
|
|
|
|
const float current_perpendicular_distance =
|
2016-01-04 07:30:03 -05:00
|
|
|
coordinate_calculation::perpendicularDistanceFromProjectedCoordinate(
|
2015-12-03 14:04:23 -05:00
|
|
|
m_coordinate_list->at(current_edge.u), m_coordinate_list->at(current_edge.v),
|
|
|
|
input_coordinate, projected_coordinate);
|
|
|
|
// distance must be non-negative
|
|
|
|
BOOST_ASSERT(0.f <= current_perpendicular_distance);
|
|
|
|
|
2016-01-05 06:04:04 -05:00
|
|
|
traversal_queue.push(
|
|
|
|
QueryCandidate{current_perpendicular_distance, std::move(current_edge)});
|
2015-01-27 11:44:46 -05:00
|
|
|
}
|
2013-06-26 09:43:13 -04:00
|
|
|
}
|
2013-08-26 08:16:34 -04:00
|
|
|
|
2014-05-29 09:36:14 -04:00
|
|
|
template <class QueueT>
|
2015-12-03 14:04:23 -05:00
|
|
|
void ExploreTreeNode(const TreeNode &parent,
|
|
|
|
const FixedPointCoordinate &input_coordinate,
|
|
|
|
QueueT &traversal_queue)
|
2014-05-29 09:36:14 -04:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < parent.child_count; ++i)
|
|
|
|
{
|
|
|
|
const int32_t child_id = parent.children[i];
|
2015-12-03 14:04:23 -05:00
|
|
|
const auto &child_tree_node = m_search_tree[child_id];
|
|
|
|
const auto &child_rectangle = child_tree_node.minimum_bounding_rectangle;
|
2014-05-29 09:36:14 -04:00
|
|
|
const float lower_bound_to_element = child_rectangle.GetMinDist(input_coordinate);
|
2016-01-05 06:04:04 -05:00
|
|
|
traversal_queue.push(QueryCandidate{lower_bound_to_element, m_search_tree[child_id]});
|
2014-05-29 09:36:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-21 12:34:50 -04:00
|
|
|
inline void LoadLeafFromDisk(const uint32_t leaf_id, LeafNode &result_node)
|
2014-05-06 09:20:31 -04:00
|
|
|
{
|
2014-06-27 10:25:55 -04:00
|
|
|
if (!leaves_stream.is_open())
|
2014-05-06 09:20:31 -04:00
|
|
|
{
|
2014-06-27 10:25:55 -04:00
|
|
|
leaves_stream.open(m_leaf_node_filename, std::ios::in | std::ios::binary);
|
2014-05-06 09:20:31 -04:00
|
|
|
}
|
2014-06-27 10:25:55 -04:00
|
|
|
if (!leaves_stream.good())
|
2014-05-06 09:20:31 -04:00
|
|
|
{
|
2016-01-05 10:51:13 -05:00
|
|
|
throw exception("Could not read from leaf file.");
|
2013-06-26 09:43:13 -04:00
|
|
|
}
|
2014-05-26 11:37:44 -04:00
|
|
|
const uint64_t seek_pos = sizeof(uint64_t) + leaf_id * sizeof(LeafNode);
|
2014-06-27 10:25:55 -04:00
|
|
|
leaves_stream.seekg(seek_pos);
|
2015-01-27 11:44:46 -05:00
|
|
|
BOOST_ASSERT_MSG(leaves_stream.good(), "Seeking to position in leaf file failed.");
|
2014-06-27 10:25:55 -04:00
|
|
|
leaves_stream.read((char *)&result_node, sizeof(LeafNode));
|
|
|
|
BOOST_ASSERT_MSG(leaves_stream.good(), "Reading from leaf file failed.");
|
2013-06-26 09:43:13 -04:00
|
|
|
}
|
|
|
|
|
2015-12-03 14:04:23 -05:00
|
|
|
template <typename CoordinateT>
|
|
|
|
void InitializeMBRectangle(Rectangle &rectangle,
|
|
|
|
const std::array<EdgeDataT, LEAF_NODE_SIZE> &objects,
|
|
|
|
const uint32_t element_count,
|
|
|
|
const std::vector<CoordinateT> &coordinate_list)
|
2014-10-28 19:33:43 -04:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < element_count; ++i)
|
|
|
|
{
|
2015-01-27 11:44:46 -05:00
|
|
|
rectangle.min_lon =
|
|
|
|
std::min(rectangle.min_lon, std::min(coordinate_list.at(objects[i].u).lon,
|
|
|
|
coordinate_list.at(objects[i].v).lon));
|
|
|
|
rectangle.max_lon =
|
|
|
|
std::max(rectangle.max_lon, std::max(coordinate_list.at(objects[i].u).lon,
|
|
|
|
coordinate_list.at(objects[i].v).lon));
|
|
|
|
|
|
|
|
rectangle.min_lat =
|
|
|
|
std::min(rectangle.min_lat, std::min(coordinate_list.at(objects[i].u).lat,
|
|
|
|
coordinate_list.at(objects[i].v).lat));
|
|
|
|
rectangle.max_lat =
|
|
|
|
std::max(rectangle.max_lat, std::max(coordinate_list.at(objects[i].u).lat,
|
|
|
|
coordinate_list.at(objects[i].v).lat));
|
2014-10-28 19:33:43 -04:00
|
|
|
}
|
|
|
|
BOOST_ASSERT(rectangle.min_lat != std::numeric_limits<int>::min());
|
|
|
|
BOOST_ASSERT(rectangle.min_lon != std::numeric_limits<int>::min());
|
|
|
|
BOOST_ASSERT(rectangle.max_lat != std::numeric_limits<int>::min());
|
|
|
|
BOOST_ASSERT(rectangle.max_lon != std::numeric_limits<int>::min());
|
|
|
|
}
|
2013-06-26 09:43:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
//[1] "On Packing R-Trees"; I. Kamel, C. Faloutsos; 1993; DOI: 10.1145/170088.170403
|
|
|
|
//[2] "Nearest Neighbor Queries", N. Roussopulos et al; 1995; DOI: 10.1145/223784.223794
|
2014-06-23 10:54:31 -04:00
|
|
|
//[3] "Distance Browsing in Spatial Databases"; G. Hjaltason, H. Samet; 1999; ACM Trans. DB Sys
|
|
|
|
// Vol.24 No.2, pp.265-318
|
2016-01-05 10:51:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-28 06:13:18 -05:00
|
|
|
#endif // STATIC_RTREE_HPP
|