mapnik/utils/shapeindex/quadtree.hpp

298 lines
7.7 KiB
C++
Raw Normal View History

2006-03-31 12:32:02 +02:00
/*****************************************************************************
2011-11-14 04:54:32 +01:00
*
2006-03-31 12:32:02 +02:00
* This file is part of Mapnik (c++ mapping toolkit)
2005-06-14 17:06:59 +02:00
*
2015-06-16 12:49:16 +02:00
* Copyright (C) 2015 Artem Pavlenko
2005-06-14 17:06:59 +02: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.
*
* This library is distributed in the hope that it will be useful,
2005-06-14 17:06:59 +02: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.
2005-06-14 17:06:59 +02: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
*
*****************************************************************************/
2005-06-14 17:06:59 +02:00
#ifndef QUADTREE_HPP
#define QUADTREE_HPP
// stl
2011-11-14 04:54:32 +01:00
#include <cstring>
2005-06-14 17:06:59 +02:00
#include <vector>
#include <fstream>
#include <iostream>
// mapnik
2009-12-16 21:02:06 +01:00
#include <mapnik/box2d.hpp>
2005-06-14 17:06:59 +02:00
2009-12-16 21:02:06 +01:00
using mapnik::box2d;
using mapnik::coord2d;
2005-06-14 17:06:59 +02:00
template <typename T>
struct quadtree_node
{
2010-06-02 13:03:30 +02:00
box2d<double> ext_;
std::vector<T> data_;
quadtree_node<T>* children_[4];
quadtree_node(const box2d<double>& ext)
: ext_(ext),data_()
{
2014-01-29 00:05:10 +01:00
std::memset(children_,0,sizeof(quadtree_node<T>*)*4);
2010-06-02 13:03:30 +02:00
}
2005-06-14 17:06:59 +02:00
2011-11-14 04:54:32 +01:00
~quadtree_node()
2010-06-02 13:03:30 +02:00
{
2011-11-14 04:54:32 +01:00
for (int i=0;i<4;++i)
2010-06-02 13:03:30 +02:00
{
2011-11-14 04:54:32 +01:00
if (children_[i])
2006-05-12 18:35:36 +02:00
{
2010-06-02 13:03:30 +02:00
delete children_[i],children_[i]=0;
2006-05-12 18:35:36 +02:00
}
2010-06-02 13:03:30 +02:00
}
}
2005-06-14 17:06:59 +02:00
2011-11-14 04:54:32 +01:00
int num_subnodes() const
2010-06-02 13:03:30 +02:00
{
int count=0;
2011-11-14 04:54:32 +01:00
for (int i=0;i<4;++i)
2010-06-02 13:03:30 +02:00
{
2011-11-14 04:54:32 +01:00
if (children_[i])
2006-05-12 18:35:36 +02:00
{
2010-06-02 13:03:30 +02:00
++count;
2006-05-12 18:35:36 +02:00
}
2010-06-02 13:03:30 +02:00
}
return count;
2011-11-14 04:54:32 +01:00
}
2005-06-14 17:06:59 +02:00
};
template <typename T>
class quadtree
{
private:
quadtree_node<T>* root_;
const int maxdepth_;
const double ratio_;
public:
2009-12-16 21:02:06 +01:00
quadtree(const box2d<double>& extent,int maxdepth,double ratio)
2006-05-12 18:35:36 +02:00
: root_(new quadtree_node<T>(extent)),
maxdepth_(maxdepth),
ratio_(ratio) {}
2005-06-14 17:06:59 +02:00
~quadtree()
{
2006-05-12 18:35:36 +02:00
if (root_) delete root_;
2005-06-14 17:06:59 +02:00
}
2011-11-14 04:54:32 +01:00
2009-12-16 21:02:06 +01:00
void insert(const T& data,const box2d<double>& item_ext)
2005-06-14 17:06:59 +02:00
{
2006-05-12 18:35:36 +02:00
insert(data,item_ext,root_,maxdepth_);
2005-06-14 17:06:59 +02:00
}
2011-11-14 04:54:32 +01:00
2005-06-14 17:06:59 +02:00
int count() const
{
2006-05-12 18:35:36 +02:00
return count_nodes(root_);
2005-06-14 17:06:59 +02:00
}
2011-11-14 04:54:32 +01:00
int count_items() const
2005-06-14 17:06:59 +02:00
{
2006-05-12 18:35:36 +02:00
int count=0;
count_items(root_,count);
return count;
2005-06-14 17:06:59 +02:00
}
2011-11-14 04:54:32 +01:00
2005-06-14 17:06:59 +02:00
void print() const
{
2006-05-12 18:35:36 +02:00
print(root_);
2005-06-14 17:06:59 +02:00
}
2011-11-14 04:54:32 +01:00
void trim()
2005-06-14 17:06:59 +02:00
{
2006-05-12 18:35:36 +02:00
trim_tree(root_);
2011-11-14 04:54:32 +01:00
}
2005-06-14 17:06:59 +02:00
void write(std::ostream& out)
{
2006-05-12 18:35:36 +02:00
char header[16];
2014-01-29 00:05:10 +01:00
std::memset(header,0,16);
2006-05-12 18:35:36 +02:00
header[0]='m';
header[1]='a';
header[2]='p';
2007-08-01 10:52:18 +02:00
header[3]='n';
header[4]='i';
header[5]='k';
2006-05-12 18:35:36 +02:00
out.write(header,16);
write_node(out,root_);
2005-06-14 17:06:59 +02:00
}
private:
void trim_tree(quadtree_node<T>*& node)
2011-11-14 04:54:32 +01:00
{
if (node)
2006-05-12 18:35:36 +02:00
{
for (int i=0;i<4;++i)
2011-11-14 04:54:32 +01:00
{
trim_tree(node->children_[i]);
2006-05-12 18:35:36 +02:00
}
2005-06-14 17:06:59 +02:00
2006-05-12 18:35:36 +02:00
if (node->num_subnodes()==1 && node->data_.size()==0)
{
2011-11-14 04:54:32 +01:00
for (int i=0;i<4;++i)
2006-05-12 18:35:36 +02:00
{
if (node->children_[i])
2011-11-14 04:54:32 +01:00
{
2006-05-12 18:35:36 +02:00
node=node->children_[i];
2011-11-14 04:54:32 +01:00
break;
2006-05-12 18:35:36 +02:00
}
}
2011-11-14 04:54:32 +01:00
}
2006-05-12 18:35:36 +02:00
}
2005-06-14 17:06:59 +02:00
}
int count_nodes(const quadtree_node<T>* node) const
{
2006-05-12 18:35:36 +02:00
if (!node)
{
return 0;
}
else
{
int count = 1;
for (int i=0;i<4;++i)
{
count += count_nodes(node->children_[i]);
}
return count;
}
2005-06-14 17:06:59 +02:00
}
void count_items(const quadtree_node<T>* node,int& count) const
{
2006-05-12 18:35:36 +02:00
if (node)
{
2011-11-14 04:54:32 +01:00
count += node->data_.size();
2006-05-12 18:35:36 +02:00
for (int i=0;i<4;++i)
{
count_items(node->children_[i],count);
2011-11-14 04:54:32 +01:00
}
2006-05-12 18:35:36 +02:00
}
2005-06-14 17:06:59 +02:00
}
int subnode_offset(const quadtree_node<T>* node) const
{
2006-05-12 18:35:36 +02:00
int offset=0;
for (int i=0;i<4;i++)
{
if (node->children_[i])
{
2009-12-16 21:02:06 +01:00
offset +=sizeof(box2d<double>)+(node->children_[i]->data_.size()*sizeof(T))+3*sizeof(int);
2006-05-12 18:35:36 +02:00
offset +=subnode_offset(node->children_[i]);
}
}
return offset;
2005-06-14 17:06:59 +02:00
}
void write_node(std::ostream& out,const quadtree_node<T>* node) const
{
2006-05-12 18:35:36 +02:00
if (node)
{
int offset=subnode_offset(node);
int shape_count=node->data_.size();
2009-12-16 21:02:06 +01:00
int recsize=sizeof(box2d<double>) + 3 * sizeof(int) + shape_count * sizeof(T);
2006-05-12 18:35:36 +02:00
char* node_record=new char[recsize];
2014-01-29 00:05:10 +01:00
std::memset(node_record,0,recsize);
std::memcpy(node_record,&offset,4);
std::memcpy(node_record+4,&node->ext_,sizeof(box2d<double>));
std::memcpy(node_record+36,&shape_count,4);
2006-05-12 18:35:36 +02:00
for (int i=0;i<shape_count;++i)
{
memcpy(node_record + 40 + i * sizeof(T),&(node->data_[i]),sizeof(T));
}
int num_subnodes=0;
for (int i=0;i<4;++i)
{
if (node->children_[i])
{
++num_subnodes;
}
}
2014-01-29 00:05:10 +01:00
std::memcpy(node_record + 40 + shape_count * sizeof(T),&num_subnodes,4);
2006-05-12 18:35:36 +02:00
out.write(node_record,recsize);
delete [] node_record;
2005-06-14 17:06:59 +02:00
2006-05-12 18:35:36 +02:00
for (int i=0;i<4;++i)
{
write_node(out,node->children_[i]);
}
}
2005-06-14 17:06:59 +02:00
}
void print(const quadtree_node<T>* node,int level=0) const
{
2006-05-12 18:35:36 +02:00
if (node)
{
typename std::vector<T>::const_iterator itr=node->data_.begin();
std::string pad;
for (int i=0;i<level;++i)
{
pad+=" ";
}
std::clog<<pad<<"node "<<node<<" extent:"<<node->ext_<<std::endl;
std::clog<<pad;
while(itr!=node->data_.end())
{
std::clog<<*itr<<" ";
++itr;
}
std::clog<<std::endl;
for (int i=0;i<4;++i)
{
print(node->children_[i],level+4);
}
}
2005-06-14 17:06:59 +02:00
}
2009-12-16 21:02:06 +01:00
void insert(const T& data,const box2d<double>& item_ext,quadtree_node<T>* node,int maxdepth)
2005-06-14 17:06:59 +02:00
{
2006-05-12 18:35:36 +02:00
if (node && node->ext_.contains(item_ext))
{
double width=node->ext_.width();
double height=node->ext_.height();
2005-06-14 17:06:59 +02:00
2006-05-12 18:35:36 +02:00
double lox=node->ext_.minx();
double loy=node->ext_.miny();
double hix=node->ext_.maxx();
double hiy=node->ext_.maxy();
2005-06-14 17:06:59 +02:00
2009-12-16 21:02:06 +01:00
box2d<double> ext[4];
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);
2005-06-14 17:06:59 +02:00
2006-05-12 18:35:36 +02:00
if (maxdepth > 1)
{
for (int i=0;i<4;++i)
{
if (ext[i].contains(item_ext))
{
if (!node->children_[i])
{
node->children_[i]=new quadtree_node<T>(ext[i]);
}
insert(data,item_ext,node->children_[i],maxdepth-1);
return;
}
}
}
node->data_.push_back(data);
}
2005-06-14 17:06:59 +02:00
}
};
#endif //QUADTREE_HPP