added quad_tree implementaion
This commit is contained in:
parent
efc23b9217
commit
3818da6bc5
6 changed files with 243 additions and 7 deletions
|
@ -52,7 +52,9 @@ void export_color ()
|
|||
.add_property("a",&Color::alpha)
|
||||
.def(self == self)
|
||||
.def_pickle(color_pickle_suite())
|
||||
.def("fromString",&create_from_string)
|
||||
.staticmethod("fromString")
|
||||
;
|
||||
def("Color",&create_from_string);
|
||||
//def("Color",&create_from_string);
|
||||
}
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ namespace mapnik
|
|||
T & pixmap_;
|
||||
CoordTransform t_;
|
||||
face_manager<freetype_engine> font_manager_;
|
||||
label_collision_detector detector_;
|
||||
label_collision_detector2 detector_;
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -385,7 +385,6 @@ namespace mapnik
|
|||
|
||||
//angle_ = sin ( 0.1 * count++);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
private:
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
#if !defined LABEL_COLLISION_DETECTOR
|
||||
#define LABEL_COLLISION_DETECTOR
|
||||
|
||||
#include "envelope.hpp"
|
||||
#include "quad_tree.hpp"
|
||||
#include <vector>
|
||||
|
||||
namespace mapnik
|
||||
|
@ -34,7 +34,7 @@ namespace mapnik
|
|||
{
|
||||
typedef std::vector<Envelope<double> > label_placements;
|
||||
|
||||
bool allowed_to_render(Envelope<double> const& box)
|
||||
bool has_plasement(Envelope<double> const& box)
|
||||
{
|
||||
label_placements::const_iterator itr=labels_.begin();
|
||||
for( ; itr !=labels_.end();++itr)
|
||||
|
@ -51,6 +51,34 @@ namespace mapnik
|
|||
|
||||
label_placements labels_;
|
||||
};
|
||||
|
||||
// quad_tree based label collision detector
|
||||
class label_collision_detector2 : boost::noncopyable
|
||||
{
|
||||
typedef quad_tree<Envelope<double> > tree_t;
|
||||
tree_t tree_;
|
||||
public:
|
||||
|
||||
explicit label_collision_detector2(Envelope<double> const& extent)
|
||||
: tree_(extent) {}
|
||||
|
||||
bool has_placement(Envelope<double> const& box)
|
||||
{
|
||||
tree_t::query_iterator itr = tree_.query_in_box(box);
|
||||
tree_t::query_iterator end = tree_.query_end();
|
||||
|
||||
for ( ;itr != end; ++itr)
|
||||
{
|
||||
if (itr->intersects(box))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
tree_.insert(box,box);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
206
include/quad_tree.hpp
Normal file
206
include/quad_tree.hpp
Normal file
|
@ -0,0 +1,206 @@
|
|||
/* This file is part of Mapnik (c++ mapping toolkit)
|
||||
* Copyright (C) 2006 Artem Pavlenko
|
||||
*
|
||||
* Mapnik is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
//$Id$
|
||||
|
||||
#if !defined QUAD_TREE_HPP
|
||||
#define QUAD_TREE_HPP
|
||||
|
||||
#include <boost/ptr_container/ptr_vector.hpp>
|
||||
#include <boost/noncopyable.hpp>
|
||||
#include <vector>
|
||||
#include <mapnik/envelope.hpp>
|
||||
|
||||
|
||||
namespace mapnik
|
||||
{
|
||||
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];
|
||||
|
||||
explicit node(Envelope<double> const& ext)
|
||||
: extent_(ext)
|
||||
{
|
||||
std::memset(children_,0,4*sizeof(node*));
|
||||
}
|
||||
|
||||
Envelope<double> const& extent() const
|
||||
{
|
||||
return extent_;
|
||||
}
|
||||
|
||||
iterator begin()
|
||||
{
|
||||
return cont_.begin();
|
||||
}
|
||||
|
||||
const_iterator begin() const
|
||||
{
|
||||
return cont_.begin();
|
||||
}
|
||||
|
||||
iterator end()
|
||||
{
|
||||
return cont_.end();
|
||||
}
|
||||
|
||||
const_iterator end() const
|
||||
{
|
||||
return cont_.end();
|
||||
}
|
||||
~node () {}
|
||||
};
|
||||
|
||||
typedef boost::ptr_vector<node> nodes_t;
|
||||
typedef typename node::cont_t cont_t;
|
||||
typedef typename cont_t::iterator node_data_iterator;
|
||||
|
||||
nodes_t nodes_;
|
||||
node * root_;
|
||||
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;
|
||||
|
||||
result_t query_result_;
|
||||
|
||||
explicit quad_tree(Envelope<double> const& ext,double ratio=0.55)
|
||||
: ratio_(ratio)
|
||||
{
|
||||
nodes_.push_back(new node(ext));
|
||||
root_ = &nodes_[0];
|
||||
}
|
||||
|
||||
void insert(T data, Envelope<double> const& box)
|
||||
{
|
||||
do_insert_data(data,box,root_);
|
||||
}
|
||||
|
||||
query_iterator query_in_box(Envelope<double> const& box)
|
||||
{
|
||||
query_result_.clear();
|
||||
query_node(box,query_result_,root_);
|
||||
return query_result_.begin();
|
||||
}
|
||||
|
||||
query_iterator query_end()
|
||||
{
|
||||
return query_result_.end();
|
||||
}
|
||||
|
||||
iterator begin()
|
||||
{
|
||||
return nodes_.begin();
|
||||
}
|
||||
|
||||
const_iterator begin() const
|
||||
{
|
||||
return nodes_.begin();
|
||||
}
|
||||
|
||||
iterator end()
|
||||
{
|
||||
return nodes_.end();
|
||||
}
|
||||
|
||||
const_iterator end() const
|
||||
{
|
||||
return nodes_.end();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void query_node(Envelope<double> const& box, result_t & result, node * node_) const
|
||||
{
|
||||
if (node_)
|
||||
{
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void do_insert_data(T data, Envelope<double> const& box, node * n)
|
||||
{
|
||||
if (n)
|
||||
{
|
||||
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();
|
||||
}
|
||||
do_insert_data(data,box,n->children_[i]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
n->cont_.push_back(data);
|
||||
}
|
||||
}
|
||||
|
||||
void split_box(Envelope<double> const& node_extent,Envelope<double> * ext)
|
||||
{
|
||||
coord2d c=node_extent.center();
|
||||
|
||||
double width=node_extent.width();
|
||||
double height=node_extent.height();
|
||||
|
||||
double lox=node_extent.minx();
|
||||
double loy=node_extent.miny();
|
||||
double hix=node_extent.maxx();
|
||||
double hiy=node_extent.maxy();
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
|
@ -82,7 +82,8 @@ namespace mapnik
|
|||
agg_renderer<T>::agg_renderer(Map const& m, T & pixmap)
|
||||
: feature_style_processor<agg_renderer>(m),
|
||||
pixmap_(pixmap),
|
||||
t_(m.getWidth(),m.getHeight(),m.getCurrentExtent())
|
||||
t_(m.getWidth(),m.getHeight(),m.getCurrentExtent()),
|
||||
detector_(Envelope<double>(0,0,m.getWidth(),m.getHeight()))
|
||||
{
|
||||
Color const& bg=m.getBackground();
|
||||
pixmap_.setBackground(bg);
|
||||
|
@ -276,7 +277,7 @@ namespace mapnik
|
|||
t_.forward_y(&y);
|
||||
int w=data.width();
|
||||
int h=data.height();
|
||||
if (detector_.allowed_to_render(Envelope<double>(x - 0.5 * w,
|
||||
if (detector_.has_placement(Envelope<double>(x - 0.5 * w,
|
||||
y - 0.5 * h,
|
||||
x + 0.5 * w,
|
||||
y + 0.5 * h)))
|
||||
|
|
Loading…
Add table
Reference in a new issue