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)
|
|
|
|
*
|
2021-01-05 15:39:07 +01:00
|
|
|
* Copyright (C) 2021 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
|
2017-01-26 09:51:37 +01:00
|
|
|
#include <mapnik/geometry/box2d.hpp>
|
2015-01-07 13:11:09 +01:00
|
|
|
#include <mapnik/util/noncopyable.hpp>
|
2016-08-26 11:09:22 +02:00
|
|
|
|
2007-10-08 20:10:31 +02:00
|
|
|
// stl
|
2016-08-26 11:09:22 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <new>
|
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>
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
namespace mapnik {
|
|
|
|
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_;
|
2022-01-26 10:43:31 +01: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
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
bbox_type const& extent() const { return extent_; }
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
iterator begin() { return cont_.begin(); }
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
const_iterator begin() const { return cont_.begin(); }
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
iterator end() { return cont_.end(); }
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
const_iterator end() const { return cont_.end(); }
|
2015-09-29 13:19:19 +02:00
|
|
|
|
|
|
|
int num_subnodes() const
|
|
|
|
{
|
2016-08-26 11:09:22 +02:00
|
|
|
int _count = 0;
|
2015-09-29 13:19:19 +02:00
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
if (children_[i])
|
|
|
|
++_count;
|
2015-09-29 13:19:19 +02:00
|
|
|
}
|
2016-08-26 11:09:22 +02:00
|
|
|
return _count;
|
2015-09-29 13:19:19 +02:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
~node() {}
|
2010-06-02 13:03:30 +02:00
|
|
|
};
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
using nodes_type = std::vector<std::unique_ptr<node>>;
|
2015-09-29 13:19:19 +02:00
|
|
|
using cont_type = typename node::cont_type;
|
|
|
|
using node_data_iterator = typename cont_type::iterator;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
public:
|
2015-09-29 13:19:19 +02:00
|
|
|
using iterator = typename nodes_type::iterator;
|
|
|
|
using const_iterator = typename nodes_type::const_iterator;
|
2022-01-26 10:43:31 +01: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
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
explicit quad_tree(bbox_type const& ext, unsigned int max_depth = 8, double ratio = 0.55)
|
|
|
|
: max_depth_(max_depth)
|
|
|
|
, 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
|
|
|
|
2017-08-15 12:13:11 +02:00
|
|
|
void insert(value_type const& 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
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
query_iterator query_end() { return query_result_.end(); }
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
const_iterator begin() const { return nodes_.begin(); }
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
const_iterator end() const { return nodes_.end(); }
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +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
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
bbox_type const& extent() const { return root_->extent_; }
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
int count() const { return count_nodes(root_); }
|
2015-09-29 13:19:19 +02:00
|
|
|
|
|
|
|
int count_items() const
|
|
|
|
{
|
2016-08-26 11:09:22 +02:00
|
|
|
int _count = 0;
|
|
|
|
count_items(root_, _count);
|
|
|
|
return _count;
|
2015-09-29 13:19:19 +02:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
void trim() { trim_tree(root_); }
|
2015-09-29 13:19:19 +02:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename OutputStream>
|
|
|
|
void write(OutputStream& out)
|
2015-09-29 13:19:19 +02:00
|
|
|
{
|
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];
|
2022-01-26 10:43:31 +01:00
|
|
|
std::memset(header, 0, 16);
|
|
|
|
std::strcpy(header, "mapnik-index");
|
|
|
|
out.write(header, 16);
|
|
|
|
write_node(out, root_);
|
2015-09-29 13:19:19 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
private:
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2022-01-26 10:43:31 +01: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)
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
query_node(box, result, node_->children_[k]);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2007-05-12 14:01:43 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
void do_insert_data(value_type const& data, bbox_type const& box, node* n, unsigned int& depth)
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
2017-08-15 12:13:11 +02:00
|
|
|
if (++depth < max_depth_)
|
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];
|
2022-01-26 10:43:31 +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]));
|
2017-08-15 12:13:11 +02:00
|
|
|
n->children_[i] = nodes_.back().get();
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2017-08-15 12:13:11 +02:00
|
|
|
do_insert_data(data, box, n->children_[i], depth);
|
2010-06-02 13:03:30 +02:00
|
|
|
return;
|
|
|
|
}
|
2006-07-24 22:08:32 +02:00
|
|
|
}
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2017-08-15 12:13:11 +02:00
|
|
|
n->cont_.push_back(data);
|
2010-06-02 13:03:30 +02:00
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2022-01-26 10:43:31 +01: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
|
|
|
|
2022-01-26 10:43:31 +01: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
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
if (!n)
|
|
|
|
return 0;
|
2015-09-29 13:19:19 +02:00
|
|
|
else
|
|
|
|
{
|
2016-08-26 11:09:22 +02:00
|
|
|
int _count = 1;
|
2015-09-29 13:19:19 +02:00
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
2016-08-26 11:09:22 +02:00
|
|
|
_count += count_nodes(n->children_[i]);
|
2015-09-29 13:19:19 +02:00
|
|
|
}
|
2016-08-26 11:09:22 +02:00
|
|
|
return _count;
|
2015-09-29 13:19:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-26 11:09:22 +02:00
|
|
|
void count_items(node const* n, int& _count) const
|
2015-09-29 13:19:19 +02:00
|
|
|
{
|
|
|
|
if (n)
|
|
|
|
{
|
2016-08-26 11:09:22 +02:00
|
|
|
_count += n->cont_.size();
|
2015-09-29 13:19:19 +02:00
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
count_items(n->children_[i], _count);
|
2015-09-29 13:19:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int subnode_offset(node const* n) const
|
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
for (int i = 0; i < 4; i++)
|
|
|
|
{
|
|
|
|
if (n->children_[i])
|
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
offset += sizeof(bbox_type) + (n->children_[i]->cont_.size() * sizeof(value_type)) + 3 * sizeof(int);
|
|
|
|
offset += subnode_offset(n->children_[i]);
|
2015-09-29 13:19:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2022-01-26 10:43:31 +01:00
|
|
|
template<typename OutputStream>
|
|
|
|
void write_node(OutputStream& out, node const* n) const
|
2015-09-29 13:19:19 +02:00
|
|
|
{
|
|
|
|
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));
|
2016-03-31 12:40:34 +02:00
|
|
|
std::memcpy(node_record.get() + 4 + sizeof(bbox_type), &shape_count, 4);
|
2022-01-26 10:43:31 +01:00
|
|
|
for (int i = 0; i < shape_count; ++i)
|
2015-09-29 13:19:19 +02:00
|
|
|
{
|
2022-01-26 10:43:31 +01:00
|
|
|
memcpy(node_record.get() + 8 + sizeof(bbox_type) + i * sizeof(value_type),
|
|
|
|
&(n->cont_[i]),
|
|
|
|
sizeof(value_type));
|
2015-09-29 13:19:19 +02:00
|
|
|
}
|
2022-01-26 10:43:31 +01:00
|
|
|
int num_subnodes = 0;
|
2015-09-29 13:19:19 +02:00
|
|
|
for (int i = 0; i < 4; ++i)
|
|
|
|
{
|
|
|
|
if (n->children_[i])
|
|
|
|
{
|
|
|
|
++num_subnodes;
|
|
|
|
}
|
|
|
|
}
|
2016-03-31 12:40:34 +02:00
|
|
|
std::memcpy(node_record.get() + 8 + sizeof(bbox_type) + shape_count * sizeof(value_type), &num_subnodes, 4);
|
2022-01-26 10:43:31 +01:00
|
|
|
out.write(node_record.get(), recsize);
|
2015-09-29 13:19:19 +02:00
|
|
|
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_;
|
2022-01-26 10:43:31 +01:00
|
|
|
node* root_;
|
2012-02-02 02:53:35 +01:00
|
|
|
};
|
2022-01-26 10:43:31 +01:00
|
|
|
} // namespace mapnik
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2011-10-23 16:09:47 +02:00
|
|
|
#endif // MAPNIK_QUAD_TREE_HPP
|