2006-03-31 12:32:02 +02:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
|
|
|
* This file is part of Mapnik (c++ mapping toolkit)
|
|
|
|
*
|
2006-02-27 22:25:25 +01:00
|
|
|
* Copyright (C) 2006 Artem Pavlenko
|
|
|
|
*
|
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
|
|
|
//$Id$
|
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
#ifndef QUAD_TREE_HPP
|
2006-02-27 22:25:25 +01:00
|
|
|
#define QUAD_TREE_HPP
|
2006-10-04 13:22:18 +02:00
|
|
|
// stl
|
|
|
|
#include <vector>
|
|
|
|
// boost
|
2006-02-27 22:25:25 +01:00
|
|
|
#include <boost/ptr_container/ptr_vector.hpp>
|
|
|
|
#include <boost/noncopyable.hpp>
|
2006-10-04 13:22:18 +02:00
|
|
|
// mapnik
|
|
|
|
#include <mapnik/envelope.hpp>
|
2006-02-27 22:25:25 +01:00
|
|
|
|
|
|
|
namespace mapnik
|
|
|
|
{
|
2007-05-01 09:53:20 +02:00
|
|
|
template <typename T>
|
|
|
|
class quad_tree : boost::noncopyable
|
|
|
|
{
|
|
|
|
struct node
|
|
|
|
{
|
|
|
|
typedef T value_t;
|
|
|
|
typedef std::vector<T> cont_t;
|
|
|
|
typedef typename cont_t::iterator iterator;
|
|
|
|
typedef typename cont_t::const_iterator const_iterator;
|
|
|
|
Envelope<double> extent_;
|
|
|
|
cont_t cont_;
|
|
|
|
node * children_[4];
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
explicit node(Envelope<double> const& ext)
|
|
|
|
: extent_(ext)
|
|
|
|
{
|
|
|
|
std::memset(children_,0,4*sizeof(node*));
|
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
Envelope<double> const& extent() const
|
|
|
|
{
|
|
|
|
return extent_;
|
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
iterator begin()
|
|
|
|
{
|
|
|
|
return cont_.begin();
|
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
const_iterator begin() const
|
|
|
|
{
|
|
|
|
return cont_.begin();
|
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
iterator end()
|
|
|
|
{
|
|
|
|
return cont_.end();
|
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
const_iterator end() const
|
|
|
|
{
|
|
|
|
return cont_.end();
|
|
|
|
}
|
|
|
|
~node () {}
|
|
|
|
};
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
typedef boost::ptr_vector<node> nodes_t;
|
|
|
|
typedef typename node::cont_t cont_t;
|
|
|
|
typedef typename cont_t::iterator node_data_iterator;
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
nodes_t nodes_;
|
|
|
|
node * root_;
|
2007-05-12 14:01:43 +02:00
|
|
|
const unsigned int max_depth_;
|
2007-05-01 09:53:20 +02:00
|
|
|
const double ratio_;
|
|
|
|
public:
|
|
|
|
typedef typename nodes_t::iterator iterator;
|
|
|
|
typedef typename nodes_t::const_iterator const_iterator;
|
|
|
|
typedef typename boost::ptr_vector<T,boost::view_clone_allocator> result_t;
|
|
|
|
typedef typename result_t::iterator query_iterator;
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
result_t query_result_;
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-12 14:01:43 +02:00
|
|
|
explicit quad_tree(Envelope<double> const& ext,
|
|
|
|
unsigned int max_depth = 8,
|
|
|
|
double ratio = 0.55)
|
|
|
|
: max_depth_(max_depth),
|
|
|
|
ratio_(ratio)
|
2007-05-01 09:53:20 +02:00
|
|
|
{
|
2006-07-24 22:08:32 +02:00
|
|
|
nodes_.push_back(new node(ext));
|
|
|
|
root_ = &nodes_[0];
|
2007-05-01 09:53:20 +02:00
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
void insert(T data, Envelope<double> const& box)
|
|
|
|
{
|
2007-05-12 14:01:43 +02:00
|
|
|
unsigned int depth=0;
|
|
|
|
do_insert_data(data,box,root_,depth);
|
2007-05-01 09:53:20 +02:00
|
|
|
}
|
2007-05-12 14:01:43 +02:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
query_iterator query_in_box(Envelope<double> const& box)
|
|
|
|
{
|
2006-07-24 22:08:32 +02:00
|
|
|
query_result_.clear();
|
|
|
|
query_node(box,query_result_,root_);
|
|
|
|
return query_result_.begin();
|
2007-05-01 09:53:20 +02:00
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
query_iterator query_end()
|
|
|
|
{
|
2006-07-24 22:08:32 +02:00
|
|
|
return query_result_.end();
|
2007-05-01 09:53:20 +02:00
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
iterator begin()
|
|
|
|
{
|
2006-07-24 22:08:32 +02:00
|
|
|
return nodes_.begin();
|
2007-05-01 09:53:20 +02:00
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
const_iterator begin() const
|
|
|
|
{
|
2006-07-24 22:08:32 +02:00
|
|
|
return nodes_.begin();
|
2007-05-01 09:53:20 +02:00
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
iterator end()
|
|
|
|
{
|
2006-07-24 22:08:32 +02:00
|
|
|
return nodes_.end();
|
2007-05-01 09:53:20 +02:00
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
const_iterator end() const
|
|
|
|
{
|
2006-07-24 22:08:32 +02:00
|
|
|
return nodes_.end();
|
2007-05-01 09:53:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void clear ()
|
|
|
|
{
|
|
|
|
Envelope<double> ext = root_->extent_;
|
|
|
|
nodes_.clear();
|
|
|
|
nodes_.push_back(new node(ext));
|
|
|
|
root_ = &nodes_[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void query_node(Envelope<double> const& box, result_t & result, node * node_) const
|
|
|
|
{
|
2006-07-24 22:08:32 +02:00
|
|
|
if (node_)
|
|
|
|
{
|
2007-05-01 09:53:20 +02:00
|
|
|
Envelope<double> const& node_extent = node_->extent();
|
|
|
|
if (box.intersects(node_extent))
|
|
|
|
{
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
}
|
2006-07-24 22:08:32 +02:00
|
|
|
}
|
2007-05-01 09:53:20 +02:00
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-12 14:01:43 +02:00
|
|
|
void do_insert_data(T data, Envelope<double> const& box, node * n, unsigned int& depth)
|
2007-05-01 09:53:20 +02:00
|
|
|
{
|
2007-05-12 14:01:43 +02:00
|
|
|
if (++depth >= max_depth_)
|
|
|
|
{
|
|
|
|
n->cont_.push_back(data);
|
|
|
|
}
|
|
|
|
else
|
2006-07-24 22:08:32 +02:00
|
|
|
{
|
2007-05-01 09:53:20 +02:00
|
|
|
Envelope<double> const& node_extent = n->extent();
|
|
|
|
Envelope<double> ext[4];
|
|
|
|
split_box(node_extent,ext);
|
|
|
|
for (int i=0;i<4;++i)
|
|
|
|
{
|
|
|
|
if (ext[i].contains(box))
|
|
|
|
{
|
|
|
|
if (!n->children_[i])
|
|
|
|
{
|
|
|
|
nodes_.push_back(new node(ext[i]));
|
|
|
|
n->children_[i]=&nodes_.back();
|
|
|
|
}
|
2007-05-12 14:01:43 +02:00
|
|
|
do_insert_data(data,box,n->children_[i],depth);
|
2007-05-01 09:53:20 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n->cont_.push_back(data);
|
2006-07-24 22:08:32 +02:00
|
|
|
}
|
2007-05-01 09:53:20 +02:00
|
|
|
}
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2007-05-01 09:53:20 +02:00
|
|
|
void split_box(Envelope<double> const& node_extent,Envelope<double> * ext)
|
|
|
|
{
|
2006-07-24 22:08:32 +02:00
|
|
|
coord2d c=node_extent.center();
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2006-07-24 22:08:32 +02:00
|
|
|
double width=node_extent.width();
|
|
|
|
double height=node_extent.height();
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2006-07-24 22:08:32 +02:00
|
|
|
double lox=node_extent.minx();
|
|
|
|
double loy=node_extent.miny();
|
|
|
|
double hix=node_extent.maxx();
|
|
|
|
double hiy=node_extent.maxy();
|
2006-02-27 22:25:25 +01:00
|
|
|
|
2006-07-24 22:08:32 +02:00
|
|
|
ext[0]=Envelope<double>(lox,loy,lox + width * ratio_,loy + height * ratio_);
|
|
|
|
ext[1]=Envelope<double>(hix - width * ratio_,loy,hix,loy + height * ratio_);
|
|
|
|
ext[2]=Envelope<double>(lox,hiy - height*ratio_,lox + width * ratio_,hiy);
|
|
|
|
ext[3]=Envelope<double>(hix - width * ratio_,hiy - height*ratio_,hix,hiy);
|
2007-05-01 09:53:20 +02:00
|
|
|
}
|
|
|
|
};
|
2006-02-27 22:25:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|