mapnik/tests/cxx/vertex_adapter.cpp

267 lines
6.2 KiB
C++
Raw Normal View History

#include "catch.hpp"
#include <mapnik/vertex_adapters.hpp>
#include <mapnik/geometry_correct.hpp>
TEST_CASE("vertex_adapters") {
SECTION("polygon") {
mapnik::geometry::polygon g;
g.exterior_ring.add_coord(1,1);
g.exterior_ring.add_coord(2,2);
g.exterior_ring.add_coord(100,100);
g.exterior_ring.add_coord(1,1);
mapnik::geometry::polygon_vertex_adapter va(g);
double x,y;
unsigned cmd;
// 0,0
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_MOVETO );
REQUIRE( x == 1 );
REQUIRE( y == 1 );
// 1,1
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == 2 );
REQUIRE( y == 2 );
// 100,100
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == 100 );
REQUIRE( y == 100 );
// last point should be same as first
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == 1 );
REQUIRE( y == 1 );
// close
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_CLOSE );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
// end
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_END );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
}
SECTION("polygon with hole") {
mapnik::geometry::polygon g;
g.exterior_ring.add_coord(0,0);
g.exterior_ring.add_coord(-10,0);
g.exterior_ring.add_coord(-10,10);
g.exterior_ring.add_coord(0,10);
g.exterior_ring.add_coord(0,0);
std::vector<mapnik::geometry::linear_ring> interior_rings;
mapnik::geometry::linear_ring hole;
hole.add_coord(-7,7);
hole.add_coord(-7,3);
hole.add_coord(-3,3);
hole.add_coord(-3,7);
hole.add_coord(-7,7);
g.add_hole(std::move(hole));
mapnik::geometry::linear_ring hole_in_hole;
hole_in_hole.add_coord(-6,4);
hole_in_hole.add_coord(-6,6);
hole_in_hole.add_coord(-4,6);
hole_in_hole.add_coord(-4,4);
hole_in_hole.add_coord(-6,4);
g.add_hole(std::move(hole_in_hole));
mapnik::geometry::polygon_vertex_adapter va(g);
double x,y;
unsigned cmd;
// exterior ring
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_MOVETO );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -10 );
REQUIRE( y == 0 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -10 );
REQUIRE( y == 10 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == 0 );
REQUIRE( y == 10 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_CLOSE );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
// first hole
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_MOVETO );
REQUIRE( x == -7 );
REQUIRE( y == 7 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -7 );
REQUIRE( y == 3 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -3 );
REQUIRE( y == 3 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -3 );
REQUIRE( y == 7 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -7 );
REQUIRE( y == 7 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_CLOSE );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
// second hole
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_MOVETO );
REQUIRE( x == -6 );
REQUIRE( y == 4 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -6 );
REQUIRE( y == 6 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -4 );
REQUIRE( y == 6 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -4 );
REQUIRE( y == 4 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -6 );
REQUIRE( y == 4 );
mapnik::geometry::correct(g);
va.rewind(0);
// exterior ring: flipped winding order from correct
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_MOVETO );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == 0 );
REQUIRE( y == 10 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -10 );
REQUIRE( y == 10 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -10 );
REQUIRE( y == 0 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_CLOSE );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
// first hole: flipped winding order from correct
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_MOVETO );
REQUIRE( x == -7 );
REQUIRE( y == 7 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -3 );
REQUIRE( y == 7 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -3 );
REQUIRE( y == 3 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -7 );
REQUIRE( y == 3 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -7 );
REQUIRE( y == 7 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_CLOSE );
REQUIRE( x == 0 );
REQUIRE( y == 0 );
// second hole: correct appears not to have changed winding order
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_MOVETO );
REQUIRE( x == -6 );
REQUIRE( y == 4 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -6 );
REQUIRE( y == 6 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -4 );
REQUIRE( y == 6 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -4 );
REQUIRE( y == 4 );
cmd = va.vertex(&x,&y);
REQUIRE( cmd == mapnik::SEG_LINETO );
REQUIRE( x == -6 );
REQUIRE( y == 4 );
}
}