2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
2012-02-02 02:53:35 +01:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2015-06-16 12:49:16 +02:00
|
|
|
* Copyright (C) 2015 Artem Pavlenko
|
2006-02-27 22:25:25 +01:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2006-02-27 22:25:25 +01:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
2006-02-27 22:25:25 +01:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2006-03-31 12:32:02 +02:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2006-02-27 22:25:25 +01:00
|
|
|
*
|
2006-03-31 12:32:02 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2011-10-23 16:09:47 +02:00
|
|
|
#ifndef MAPNIK_QUAD_TREE_HPP
|
|
|
|
#define MAPNIK_QUAD_TREE_HPP
|
|
|
|
|
2007-10-08 20:10:31 +02:00
|
|
|
// mapnik
|
2009-12-16 21:02:06 +01:00
|
|
|
#include <mapnik/box2d.hpp>
|
2015-01-07 13:11:09 +01:00
|
|
|
#include <mapnik/util/noncopyable.hpp>
|
2015-06-19 10:45:43 +02:00
|
|
|
#include <mapnik/make_unique.hpp>
|
2007-10-08 20:10:31 +02:00
|
|
|
// stl
|
2014-10-21 16:52:01 +02:00
|
|
|
#include <algorithm>
|
2007-10-08 20:10:31 +02:00
|
|
|
#include <vector>
|
2015-09-29 16:44:57 +02:00
|
|
|
#include <type_traits>
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2015-10-01 21:56:57 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
2006-02-27 22:25:25 +01:00
|
|
|
namespace mapnik
|
|
|
|
{
|
2016-03-31 11:20:41 +02:00
|
|
|
template <typename T0, typename T1 = box2d<double>>
|
2015-01-07 13:11:09 +01:00
|
|
|
class quad_tree : util::noncopyable
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2016-03-31 11:20:41 +02:00
|
|
|
using value_type = T0;
|
|
|
|
using bbox_type = T1;
|
2010-06-02 13:03:30 +02:00
|
|
|
struct node
|
|
|
|
{
|
2016-03-31 11:20:41 +02:00
|
|
|
using cont_type = std::vector<T0>;
|
2015-09-29 13:19:19 +02:00
|
|
|
using iterator = typename cont_type::iterator;
|
|
|
|
using const_iterator = typename cont_type::const_iterator;
|
2016-03-31 11:20:41 +02:00
|
|
|
bbox_type extent_;
|
2015-09-29 13:19:19 +02:00
|
|
|
cont_type cont_;
|
2010-06-02 13:03:30 +02:00
|
|
|
node * children_[4];
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2016-03-31 11:20:41 +02:00
|
|
|
explicit node(bbox_type const& ext)
|
2010-06-02 13:03:30 +02:00
|
|
|
: extent_(ext)
|
|
|
|
{
|
2014-10-21 16:52:01 +02:00
|
|
|
std::fill(children_, children_ + 4, nullptr);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2016-03-31 11:20:41 +02:00
|
|
|
bbox_type const& extent() const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return extent_;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
iterator begin()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return cont_.begin();
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
const_iterator begin() const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return cont_.begin();
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
iterator end()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return cont_.end();
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
const_iterator end() const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
return cont_.end();
|
|
|
|
}
|
2015-09-29 13:19:19 +02:00
|
|
|
|
|
|
|
int num_subnodes() const
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
if (children_[i]) ++count;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
~node () {}
|
|
|
|
};
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2015-09-29 13:19:19 +02:00
|
|
|
using nodes_type = std::vector<std::unique_ptr<node> >;
|
|
|
|
using cont_type = typename node::cont_type;
|
|
|
|
using node_data_iterator = typename cont_type::iterator;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
public:
|
2015-09-29 13:19:19 +02:00
|
|
|
using iterator = typename nodes_type::iterator;
|
|
|
|
using const_iterator = typename nodes_type::const_iterator;
|
2016-03-31 11:20:41 +02:00
|
|
|
using result_type = typename std::vector<std::reference_wrapper<value_type> >;
|
2015-09-29 13:19:19 +02:00
|
|
|
using query_iterator = typename result_type::iterator;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2016-03-31 11:20:41 +02:00
|
|
|
explicit quad_tree(bbox_type const& ext,
|
2012-02-02 02:53:35 +01:00
|
|
|
unsigned int max_depth = 8,
|
|
|
|
double ratio = 0.55)
|
2010-06-02 13:03:30 +02:00
|
|
|
: max_depth_(max_depth),
|
2012-04-17 06:57:24 +02:00
|
|
|
ratio_(ratio),
|
|
|
|
query_result_(),
|
|
|
|
nodes_()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2015-06-19 10:45:43 +02:00
|
|
|
nodes_.push_back(std::make_unique<node>(ext));
|
|
|
|
root_ = nodes_[0].get();
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2016-03-31 11:20:41 +02:00
|
|
|
void insert(value_type data, bbox_type const& box)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2016-03-31 11:20:41 +02:00
|
|
|
unsigned int depth = 0;
|
|
|
|
do_insert_data(data, box, root_, depth);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2016-03-31 11:20:41 +02:00
|
|
|
query_iterator query_in_box(bbox_type const& box)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
query_result_.clear();
|
2015-06-19 10:45:43 +02:00
|
|
|
query_node(box, query_result_, root_);
|
2010-06-02 13:03:30 +02:00
|
|
|
return query_result_.begin();
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
query_iterator query_end()
|
|
|
|
{
|
|
|
|
return query_result_.end();
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
const_iterator begin() const
|
|
|
|
{
|
|
|
|
return nodes_.begin();
|
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
const_iterator end() const
|
|
|
|
{
|
|
|
|
return nodes_.end();
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
void clear ()
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2016-03-31 11:20:41 +02:00
|
|
|
bbox_type ext = root_->extent_;
|
2010-06-02 13:03:30 +02:00
|
|
|
nodes_.clear();
|
2015-06-19 10:45:43 +02:00
|
|
|
nodes_.push_back(std::make_unique<node>(ext));
|
|
|
|
root_ = nodes_[0].get();
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2016-03-31 11:20:41 +02:00
|
|
|
bbox_type const& extent() const
|
2010-11-22 12:25:36 +01:00
|
|
|
{
|
|
|
|
return root_->extent_;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2015-09-29 13:19:19 +02:00
|
|
|
int count() const
|
|
|
|
{
|
|
|
|
return count_nodes(root_);
|
|
|
|
}
|
|
|
|
|
|
|
|
int count_items() const
|
|
|
|
{
|
|
|
|
int count = 0;
|
|
|
|
count_items(root_, count);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
void trim()
|
|
|
|
{
|
|
|
|
trim_tree(root_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename OutputStream>
|
|
|
|
void write(OutputStream & out)
|
|
|
|
{
|
2015-09-29 16:44:57 +02:00
|
|
|
static_assert(std::is_standard_layout<value_type>::value,
|
|
|
|
"Values stored in quad-tree must be standard layout types to allow serialisation");
|
2015-09-29 13:19:19 +02:00
|
|
|
char header[16];
|
|
|
|
std::memset(header,0,16);
|
2016-01-05 11:20:21 +01:00
|
|
|
std::strcpy(header,"mapnik-index");
|
2015-09-29 13:19:19 +02:00
|
|
|
out.write(header,16);
|
|
|
|
write_node(out,root_);
|
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
private:
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2016-03-31 11:20:41 +02:00
|
|
|
void query_node(bbox_type const& box, result_type & result, node * node_) const
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
if (node_)
|
|
|
|
{
|
2016-03-31 11:20:41 +02:00
|
|
|
bbox_type const& node_extent = node_->extent();
|
2010-06-02 13:03:30 +02:00
|
|
|
if (box.intersects(node_extent))
|
2007-05-12 14:01:43 +02:00
|
|
|
{
|
2015-06-19 10:45:43 +02:00
|
|
|
for (auto & n : *node_)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2015-06-19 10:45:43 +02:00
|
|
|
result.push_back(std::ref(n));
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
for (int k = 0; k < 4; ++k)
|
|
|
|
{
|
|
|
|
query_node(box,result,node_->children_[k]);
|
|
|
|
}
|
2007-05-12 14:01:43 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2016-03-31 11:20:41 +02:00
|
|
|
void do_insert_data(value_type data, bbox_type const& box, node * n, unsigned int& depth)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
if (++depth >= max_depth_)
|
|
|
|
{
|
|
|
|
n->cont_.push_back(data);
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
else
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2016-03-31 11:20:41 +02:00
|
|
|
bbox_type const& node_extent = n->extent();
|
|
|
|
bbox_type ext[4];
|
2012-02-02 02:53:35 +01:00
|
|
|
split_box(node_extent,ext);
|
2015-06-19 10:45:43 +02:00
|
|
|
for (int i = 0; i < 4; ++i)
|
2006-07-24 22:08:32 +02:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
if (ext[i].contains(box))
|
|
|
|
{
|
|
|
|
if (!n->children_[i])
|
|
|
|
{
|
2015-06-19 10:45:43 +02:00
|
|
|
nodes_.push_back(std::make_unique<node>(ext[i]));
|
|
|
|
n->children_[i]=nodes_.back().get();
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
do_insert_data(data,box,n->children_[i],depth);
|
|
|
|
return;
|
|
|
|
}
|
2006-07-24 22:08:32 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
n->cont_.push_back(data);
|
|
|
|
}
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2016-03-31 11:20:41 +02:00
|
|
|
void split_box(bbox_type const& node_extent,bbox_type * ext)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2016-03-31 11:20:41 +02:00
|
|
|
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();
|
|
|
|
|
|
|
|
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);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-04-17 06:57:24 +02:00
|
|
|
|
2015-10-05 12:48:39 +02:00
|
|
|
void trim_tree(node *& n)
|
2015-09-29 13:19:19 +02:00
|
|
|
{
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
trim_tree(n->children_[i]);
|
|
|
|
}
|
|
|
|
if (n->num_subnodes() == 1 && n->cont_.size() == 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
if (n->children_[i])
|
|
|
|
{
|
|
|
|
n = n->children_[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int count_nodes(node const* n) const
|
|
|
|
{
|
|
|
|
if (!n) return 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int count = 1;
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
count += count_nodes(n->children_[i]);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void count_items(node const* n,int& count) const
|
|
|
|
{
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
count += n->cont_.size();
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
count_items(n->children_[i],count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int subnode_offset(node const* n) const
|
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if (n->children_[i])
|
|
|
|
{
|
2016-03-31 11:20:41 +02:00
|
|
|
offset +=sizeof(bbox_type) + (n->children_[i]->cont_.size() * sizeof(value_type)) + 3 * sizeof(int);
|
2015-09-29 13:19:19 +02:00
|
|
|
offset +=subnode_offset(n->children_[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename OutputStream>
|
|
|
|
void write_node(OutputStream & out, node const* n) const
|
|
|
|
{
|
|
|
|
if (n)
|
|
|
|
{
|
2016-03-31 11:20:41 +02:00
|
|
|
int offset = subnode_offset(n);
|
|
|
|
int shape_count = n->cont_.size();
|
|
|
|
int recsize = sizeof(bbox_type) + 3 * sizeof(int) + shape_count * sizeof(value_type);
|
2015-09-29 13:19:19 +02:00
|
|
|
std::unique_ptr<char[]> node_record(new char[recsize]);
|
|
|
|
std::memset(node_record.get(), 0, recsize);
|
|
|
|
std::memcpy(node_record.get(), &offset, 4);
|
2016-03-31 11:20:41 +02:00
|
|
|
std::memcpy(node_record.get() + 4, &n->extent_, sizeof(bbox_type));
|
2015-09-29 13:19:19 +02:00
|
|
|
std::memcpy(node_record.get() + 36, &shape_count, 4);
|
|
|
|
for (int i=0; i < shape_count; ++i)
|
|
|
|
{
|
2016-03-31 11:20:41 +02:00
|
|
|
memcpy(node_record.get() + 40 + i * sizeof(value_type), &(n->cont_[i]), sizeof(value_type));
|
2015-09-29 13:19:19 +02:00
|
|
|
}
|
|
|
|
int num_subnodes=0;
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
if (n->children_[i])
|
|
|
|
{
|
|
|
|
++num_subnodes;
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 11:20:41 +02:00
|
|
|
std::memcpy(node_record.get() + 40 + shape_count * sizeof(value_type), &num_subnodes, 4);
|
2015-09-29 13:19:19 +02:00
|
|
|
out.write(node_record.get(),recsize);
|
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
write_node(out, n->children_[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-17 06:57:24 +02:00
|
|
|
const unsigned int max_depth_;
|
|
|
|
const double ratio_;
|
2015-09-29 13:19:19 +02:00
|
|
|
result_type query_result_;
|
|
|
|
nodes_type nodes_;
|
2012-04-17 06:57:24 +02:00
|
|
|
node * root_;
|
|
|
|
|
2012-02-02 02:53:35 +01:00
|
|
|
};
|
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2011-10-23 16:09:47 +02:00
|
|
|
#endif // MAPNIK_QUAD_TREE_HPP
|