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)
|
|
|
|
*
|
2014-11-20 15:25:50 +01:00
|
|
|
* Copyright (C) 2014 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>
|
2012-12-17 03:19:52 +01:00
|
|
|
#include <mapnik/noncopyable.hpp>
|
2011-10-23 16:09:47 +02:00
|
|
|
|
2006-10-04 13:22:18 +02:00
|
|
|
// boost
|
2014-10-22 01:37:27 +02:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-local-typedef"
|
2006-02-27 22:25:25 +01:00
|
|
|
#include <boost/ptr_container/ptr_vector.hpp>
|
2014-10-22 01:37:27 +02:00
|
|
|
#pragma GCC diagnostic pop
|
2011-10-23 16:09:47 +02:00
|
|
|
|
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>
|
2006-02-27 22:25:25 +01:00
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
template <typename T>
|
2012-12-17 03:19:52 +01:00
|
|
|
class quad_tree : mapnik::noncopyable
|
2010-06-02 13:03:30 +02:00
|
|
|
{
|
|
|
|
struct node
|
|
|
|
{
|
2014-07-07 19:23:15 +02:00
|
|
|
using value_t = T;
|
|
|
|
using cont_t = std::vector<T>;
|
|
|
|
using iterator = typename cont_t::iterator;
|
|
|
|
using const_iterator = typename cont_t::const_iterator;
|
2010-06-02 13:03:30 +02:00
|
|
|
box2d<double> extent_;
|
|
|
|
cont_t cont_;
|
|
|
|
node * children_[4];
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
explicit node(box2d<double> const& ext)
|
|
|
|
: 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
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
box2d<double> const& extent() const
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
~node () {}
|
|
|
|
};
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2014-07-07 19:23:15 +02:00
|
|
|
using nodes_t = boost::ptr_vector<node>;
|
|
|
|
using cont_t = typename node::cont_t;
|
|
|
|
using node_data_iterator = typename cont_t::iterator;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
public:
|
2014-07-07 19:23:15 +02:00
|
|
|
using iterator = typename nodes_t::iterator;
|
|
|
|
using const_iterator = typename nodes_t::const_iterator;
|
|
|
|
using result_t = typename boost::ptr_vector<T,boost::view_clone_allocator>;
|
|
|
|
using query_iterator = typename result_t::iterator;
|
2012-02-02 02:53:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
explicit quad_tree(box2d<double> const& ext,
|
|
|
|
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
|
|
|
{
|
|
|
|
nodes_.push_back(new node(ext));
|
|
|
|
root_ = &nodes_[0];
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void insert(T data, box2d<double> const& box)
|
|
|
|
{
|
|
|
|
unsigned int depth=0;
|
|
|
|
do_insert_data(data,box,root_,depth);
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
query_iterator query_in_box(box2d<double> const& box)
|
|
|
|
{
|
|
|
|
query_result_.clear();
|
|
|
|
query_node(box,query_result_,root_);
|
|
|
|
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
|
|
|
{
|
|
|
|
box2d<double> ext = root_->extent_;
|
|
|
|
nodes_.clear();
|
|
|
|
nodes_.push_back(new node(ext));
|
|
|
|
root_ = &nodes_[0];
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-11-22 12:25:36 +01:00
|
|
|
box2d<double> const& extent() const
|
|
|
|
{
|
|
|
|
return root_->extent_;
|
|
|
|
}
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
private:
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void query_node(box2d<double> const& box, result_t & result, node * node_) const
|
|
|
|
{
|
|
|
|
if (node_)
|
|
|
|
{
|
|
|
|
box2d<double> const& node_extent = node_->extent();
|
|
|
|
if (box.intersects(node_extent))
|
2007-05-12 14:01:43 +02:00
|
|
|
{
|
2010-06-02 13:03:30 +02:00
|
|
|
node_data_iterator i=node_->begin();
|
|
|
|
node_data_iterator end=node_->end();
|
|
|
|
while ( i!=end)
|
|
|
|
{
|
|
|
|
result.push_back(&(*i));
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
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
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void do_insert_data(T data, box2d<double> const& box, node * n, unsigned int& depth)
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
|
|
|
box2d<double> const& node_extent = n->extent();
|
|
|
|
box2d<double> ext[4];
|
2012-02-02 02:53:35 +01:00
|
|
|
split_box(node_extent,ext);
|
2010-06-02 13:03:30 +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])
|
|
|
|
{
|
2007-05-01 09:53:20 +02:00
|
|
|
nodes_.push_back(new node(ext[i]));
|
|
|
|
n->children_[i]=&nodes_.back();
|
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
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
void split_box(box2d<double> const& node_extent,box2d<double> * ext)
|
|
|
|
{
|
2011-04-01 07:32:25 +02:00
|
|
|
//coord2d c=node_extent.center();
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
double width=node_extent.width();
|
|
|
|
double height=node_extent.height();
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
double lox=node_extent.minx();
|
|
|
|
double loy=node_extent.miny();
|
|
|
|
double hix=node_extent.maxx();
|
|
|
|
double hiy=node_extent.maxy();
|
2012-02-02 02:53:35 +01:00
|
|
|
|
2010-06-02 13:03:30 +02:00
|
|
|
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);
|
|
|
|
}
|
2012-04-17 06:57:24 +02:00
|
|
|
|
|
|
|
const unsigned int max_depth_;
|
|
|
|
const double ratio_;
|
|
|
|
result_t query_result_;
|
|
|
|
nodes_t nodes_;
|
|
|
|
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
|