quad_tree - make more generic by templating on bounding box type

This commit is contained in:
artemp 2016-03-31 11:20:41 +02:00
parent 5017740fbb
commit 4faa9896bb

View file

@ -36,26 +36,27 @@
namespace mapnik
{
template <typename T>
template <typename T0, typename T1 = box2d<double>>
class quad_tree : util::noncopyable
{
using value_type = T;
using value_type = T0;
using bbox_type = T1;
struct node
{
using cont_type = std::vector<T>;
using cont_type = std::vector<T0>;
using iterator = typename cont_type::iterator;
using const_iterator = typename cont_type::const_iterator;
box2d<double> extent_;
bbox_type extent_;
cont_type cont_;
node * children_[4];
explicit node(box2d<double> const& ext)
explicit node(bbox_type const& ext)
: extent_(ext)
{
std::fill(children_, children_ + 4, nullptr);
}
box2d<double> const& extent() const
bbox_type const& extent() const
{
return extent_;
}
@ -99,10 +100,10 @@ class quad_tree : util::noncopyable
public:
using iterator = typename nodes_type::iterator;
using const_iterator = typename nodes_type::const_iterator;
using result_type = typename std::vector<std::reference_wrapper<T> >;
using result_type = typename std::vector<std::reference_wrapper<value_type> >;
using query_iterator = typename result_type::iterator;
explicit quad_tree(box2d<double> const& ext,
explicit quad_tree(bbox_type const& ext,
unsigned int max_depth = 8,
double ratio = 0.55)
: max_depth_(max_depth),
@ -114,13 +115,13 @@ public:
root_ = nodes_[0].get();
}
void insert(T data, box2d<double> const& box)
void insert(value_type data, bbox_type const& box)
{
unsigned int depth=0;
do_insert_data(data,box,root_,depth);
unsigned int depth = 0;
do_insert_data(data, box, root_, depth);
}
query_iterator query_in_box(box2d<double> const& box)
query_iterator query_in_box(bbox_type const& box)
{
query_result_.clear();
query_node(box, query_result_, root_);
@ -145,13 +146,13 @@ public:
void clear ()
{
box2d<double> ext = root_->extent_;
bbox_type ext = root_->extent_;
nodes_.clear();
nodes_.push_back(std::make_unique<node>(ext));
root_ = nodes_[0].get();
}
box2d<double> const& extent() const
bbox_type const& extent() const
{
return root_->extent_;
}
@ -185,11 +186,11 @@ public:
}
private:
void query_node(box2d<double> const& box, result_type & result, node * node_) const
void query_node(bbox_type const& box, result_type & result, node * node_) const
{
if (node_)
{
box2d<double> const& node_extent = node_->extent();
bbox_type const& node_extent = node_->extent();
if (box.intersects(node_extent))
{
for (auto & n : *node_)
@ -204,7 +205,7 @@ private:
}
}
void do_insert_data(T data, box2d<double> const& box, node * n, unsigned int& depth)
void do_insert_data(value_type data, bbox_type const& box, node * n, unsigned int& depth)
{
if (++depth >= max_depth_)
{
@ -212,8 +213,8 @@ private:
}
else
{
box2d<double> const& node_extent = n->extent();
box2d<double> ext[4];
bbox_type const& node_extent = n->extent();
bbox_type ext[4];
split_box(node_extent,ext);
for (int i = 0; i < 4; ++i)
{
@ -232,20 +233,19 @@ private:
}
}
void split_box(box2d<double> const& node_extent,box2d<double> * ext)
void split_box(bbox_type const& node_extent,bbox_type * ext)
{
double width=node_extent.width();
double height=node_extent.height();
typename bbox_type::value_type width = node_extent.width();
typename bbox_type::value_type height = node_extent.height();
typename bbox_type::value_type lox = node_extent.minx();
typename bbox_type::value_type loy = node_extent.miny();
typename bbox_type::value_type hix = node_extent.maxx();
typename bbox_type::value_type hiy = node_extent.maxy();
double lox=node_extent.minx();
double loy=node_extent.miny();
double hix=node_extent.maxx();
double hiy=node_extent.maxy();
ext[0]=box2d<double>(lox,loy,lox + width * ratio_,loy + height * ratio_);
ext[1]=box2d<double>(hix - width * ratio_,loy,hix,loy + height * ratio_);
ext[2]=box2d<double>(lox,hiy - height*ratio_,lox + width * ratio_,hiy);
ext[3]=box2d<double>(hix - width * ratio_,hiy - height*ratio_,hix,hiy);
ext[0] = bbox_type(lox, loy, lox + width * ratio_, loy + height * ratio_);
ext[1] = bbox_type(hix - width * ratio_, loy, hix, loy + height * ratio_);
ext[2] = bbox_type(lox, hiy - height * ratio_, lox + width * ratio_, hiy);
ext[3] = bbox_type(hix - width * ratio_, hiy - height * ratio_, hix, hiy);
}
void trim_tree(node *& n)
@ -304,7 +304,7 @@ private:
{
if (n->children_[i])
{
offset +=sizeof(box2d<double>) + (n->children_[i]->cont_.size() * sizeof(value_type)) + 3 * sizeof(int);
offset +=sizeof(bbox_type) + (n->children_[i]->cont_.size() * sizeof(value_type)) + 3 * sizeof(int);
offset +=subnode_offset(n->children_[i]);
}
}
@ -316,17 +316,17 @@ private:
{
if (n)
{
int offset=subnode_offset(n);
int shape_count=n->cont_.size();
int recsize=sizeof(box2d<double>) + 3 * sizeof(int) + shape_count * sizeof(value_type);
int offset = subnode_offset(n);
int shape_count = n->cont_.size();
int recsize = sizeof(bbox_type) + 3 * sizeof(int) + shape_count * sizeof(value_type);
std::unique_ptr<char[]> node_record(new char[recsize]);
std::memset(node_record.get(), 0, recsize);
std::memcpy(node_record.get(), &offset, 4);
std::memcpy(node_record.get() + 4, &n->extent_, sizeof(box2d<double>));
std::memcpy(node_record.get() + 4, &n->extent_, sizeof(bbox_type));
std::memcpy(node_record.get() + 36, &shape_count, 4);
for (int i=0; i < shape_count; ++i)
{
memcpy(node_record.get() + 40 + i * sizeof(value_type), &(n->cont_[i]),sizeof(value_type));
memcpy(node_record.get() + 40 + i * sizeof(value_type), &(n->cont_[i]), sizeof(value_type));
}
int num_subnodes=0;
for (int i = 0; i < 4; ++i)
@ -336,7 +336,7 @@ private:
++num_subnodes;
}
}
std::memcpy(node_record.get() + 40 + shape_count * sizeof(value_type),&num_subnodes,4);
std::memcpy(node_record.get() + 40 + shape_count * sizeof(value_type), &num_subnodes, 4);
out.write(node_record.get(),recsize);
for (int i = 0; i < 4; ++i)
{