mapnik/include/mapnik/label_collision_detector.hpp

231 lines
5.8 KiB
C++
Raw Normal View History

2006-03-31 12:32:02 +02:00
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* 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-03-31 12:32:02 +02:00
* This library is distributed in the hope that it will be useful,
* 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.
*
* 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-03-31 12:32:02 +02:00
*****************************************************************************/
//$Id$
#ifndef LABEL_COLLISION_DETECTOR_HPP
#define LABEL_COLLISION_DETECTOR_HPP
// mapnik
#include <mapnik/quad_tree.hpp>
2007-10-08 20:10:31 +02:00
// stl
#include <vector>
2008-02-18 22:40:34 +01:00
#include <unicode/unistr.h>
namespace mapnik
{
2010-06-02 13:03:30 +02:00
//this needs to be tree structure
//as a proof of a concept _only_ we use sequential scan
2010-06-02 13:03:30 +02:00
struct label_collision_detector
{
typedef std::vector<box2d<double> > label_placements;
2010-06-02 13:03:30 +02:00
bool has_plasement(box2d<double> const& box)
{
label_placements::const_iterator itr=labels_.begin();
for( ; itr !=labels_.end();++itr)
{
2008-11-21 14:12:27 +01:00
if (itr->intersects(box))
{
2010-06-02 13:03:30 +02:00
return false;
}
2010-06-02 13:03:30 +02:00
}
labels_.push_back(box);
return true;
}
void clear()
{
labels_.clear();
}
2010-06-02 13:03:30 +02:00
private:
2010-06-02 13:03:30 +02:00
label_placements labels_;
};
2006-02-27 22:25:25 +01:00
2010-06-02 13:03:30 +02:00
// quad_tree based label collision detector
class label_collision_detector2 : boost::noncopyable
{
typedef quad_tree<box2d<double> > tree_t;
tree_t tree_;
public:
2010-06-02 13:03:30 +02:00
explicit label_collision_detector2(box2d<double> const& extent)
: tree_(extent) {}
bool has_placement(box2d<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)
{
2008-11-21 14:12:27 +01:00
if (itr->intersects(box))
{
2010-06-02 13:03:30 +02:00
return false;
}
2010-06-02 13:03:30 +02:00
}
tree_.insert(box,box);
return true;
}
2010-06-02 13:03:30 +02:00
void clear()
{
tree_.clear();
}
2010-06-02 13:03:30 +02:00
};
2010-06-02 13:03:30 +02:00
// quad_tree based label collision detector with seperate check/insert
class label_collision_detector3 : boost::noncopyable
{
typedef quad_tree< box2d<double> > tree_t;
tree_t tree_;
public:
explicit label_collision_detector3(box2d<double> const& extent)
: tree_(extent) {}
bool has_placement(box2d<double> const& box)
{
tree_t::query_iterator itr = tree_.query_in_box(box);
tree_t::query_iterator end = tree_.query_end();
2010-06-02 13:03:30 +02:00
for ( ;itr != end; ++itr)
{
2008-11-21 14:12:27 +01:00
if (itr->intersects(box))
{
2010-06-02 13:03:30 +02:00
return false;
}
2010-06-02 13:03:30 +02:00
}
2008-11-21 14:12:27 +01:00
2010-06-02 13:03:30 +02:00
return true;
}
2010-06-02 13:03:30 +02:00
void insert(box2d<double> const& box)
{
tree_.insert(box, box);
}
2010-06-02 13:03:30 +02:00
void clear()
{
tree_.clear();
}
};
2010-06-02 13:03:30 +02:00
//quad tree based label collission detector so labels dont appear within a given distance
class label_collision_detector4 : boost::noncopyable
{
struct label
{
label(box2d<double> const& b) : box(b) {}
label(box2d<double> const& b, UnicodeString const& t) : box(b), text(t) {}
2008-01-21 21:07:32 +01:00
2010-06-02 13:03:30 +02:00
box2d<double> box;
UnicodeString text;
};
2008-01-21 21:07:32 +01:00
2010-06-02 13:03:30 +02:00
typedef quad_tree< label > tree_t;
tree_t tree_;
2008-01-21 21:07:32 +01:00
2010-06-02 13:03:30 +02:00
public:
explicit label_collision_detector4(box2d<double> const& extent)
: tree_(extent) {}
2010-06-02 13:03:30 +02:00
bool has_placement(box2d<double> const& box)
{
tree_t::query_iterator itr = tree_.query_in_box(box);
tree_t::query_iterator end = tree_.query_end();
2010-06-02 13:03:30 +02:00
for ( ;itr != end; ++itr)
{
2008-11-21 14:12:27 +01:00
if (itr->box.intersects(box))
{
2010-06-02 13:03:30 +02:00
return false;
}
2010-06-02 13:03:30 +02:00
}
2010-06-02 13:03:30 +02:00
return true;
}
2010-06-02 13:03:30 +02:00
bool has_placement(box2d<double> const& box, UnicodeString const& text, double distance)
{
box2d<double> bigger_box(box.minx() - distance, box.miny() - distance, box.maxx() + distance, box.maxy() + distance);
tree_t::query_iterator itr = tree_.query_in_box(bigger_box);
tree_t::query_iterator end = tree_.query_end();
2010-06-02 13:03:30 +02:00
for ( ;itr != end; ++itr)
{
2008-11-21 14:12:27 +01:00
if (itr->box.intersects(box) || (text == itr->text && itr->box.intersects(bigger_box)))
{
2010-06-02 13:03:30 +02:00
return false;
}
2010-06-02 13:03:30 +02:00
}
return true;
}
2010-06-02 13:03:30 +02:00
bool has_point_placement(box2d<double> const& box, double distance)
{
box2d<double> bigger_box(box.minx() - distance, box.miny() - distance, box.maxx() + distance, box.maxy() + distance);
tree_t::query_iterator itr = tree_.query_in_box(bigger_box);
tree_t::query_iterator end = tree_.query_end();
2010-06-02 13:03:30 +02:00
for ( ;itr != end; ++itr)
{
if (itr->box.intersects(bigger_box))
{
2010-06-02 13:03:30 +02:00
return false;
}
2010-06-02 13:03:30 +02:00
}
return true;
}
2010-06-02 13:03:30 +02:00
void insert(box2d<double> const& box)
{
tree_.insert(label(box), box);
}
2010-06-02 13:03:30 +02:00
void insert(box2d<double> const& box, UnicodeString const& text)
{
tree_.insert(label(box, text), box);
}
2010-06-02 13:03:30 +02:00
void clear()
{
tree_.clear();
}
2008-11-21 14:12:27 +01:00
2010-06-02 13:03:30 +02:00
box2d<double> const& extent() const
{
return tree_.extent();
2010-06-02 13:03:30 +02:00
}
};
}
#endif // LABEL_COLLISION_DETECTOR_HPP