add vertex_adapter to work per polygon ring

This commit is contained in:
Dane Springmeyer 2015-04-13 18:47:51 +02:00
parent 87e978a6b9
commit 1dc77443ab
2 changed files with 85 additions and 1 deletions

View file

@ -178,6 +178,57 @@ private:
mutable bool start_loop_;
};
struct ring_vertex_adapter
{
using value_type = typename point::value_type;
ring_vertex_adapter(linear_ring const& ring)
: ring_(ring),
current_index_(0),
end_index_(ring_.size()),
start_loop_(true) {}
void rewind(unsigned) const
{
current_index_ = 0;
end_index_ = ring_.size();
start_loop_ = true;
}
unsigned vertex(value_type * x, value_type * y) const
{
if (current_index_ < end_index_)
{
auto const& coord = ring_[current_index_++];
*x = coord.x;
*y = coord.y;
if (start_loop_)
{
start_loop_= false;
return mapnik::SEG_MOVETO;
}
if (current_index_ == end_index_)
{
*x = 0;
*y = 0;
return mapnik::SEG_CLOSE;
}
return mapnik::SEG_LINETO;
}
return mapnik::SEG_END;
}
inline geometry_types type () const
{
return geometry_types::Polygon;
}
private:
linear_ring const& ring_;
mutable std::size_t current_index_;
mutable std::size_t end_index_;
mutable bool start_loop_;
};
template <typename T>
struct vertex_adapter_traits{};

View file

@ -98,7 +98,40 @@ SECTION("polygon with hole") {
REQUIRE( x == 0 );
REQUIRE( y == 0 );
// first hole
// exterior ring via ring_vertex_adapter
mapnik::geometry::ring_vertex_adapter va2(g.exterior_ring);
cmd = va2.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_MOVETO );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
cmd = va2.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -10 );
REQUIRE( y == 0 );
cmd = va2.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -10 );
REQUIRE( y == 10 );
cmd = va2.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == 0 );
REQUIRE( y == 10 );
cmd = va2.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_CLOSE );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
// since ring adapter is only for exterior, next should be END
cmd = va2.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_END );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
// first hole for polygon_adapter
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_MOVETO );
REQUIRE( x == -7 );