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