2013-11-19 21:23:37 +01:00
|
|
|
#include "bench_framework.hpp"
|
|
|
|
#include "compare_images.hpp"
|
|
|
|
#include <mapnik/vertex.hpp>
|
2014-08-28 11:59:08 +02:00
|
|
|
#include <mapnik/transform_path_adapter.hpp>
|
2014-08-28 11:29:04 +02:00
|
|
|
#include <mapnik/view_transform.hpp>
|
2013-11-19 21:23:37 +01:00
|
|
|
#include <mapnik/wkt/wkt_factory.hpp>
|
2014-07-23 07:40:39 +02:00
|
|
|
#include <mapnik/projection.hpp>
|
2013-11-19 21:23:37 +01:00
|
|
|
#include <mapnik/proj_transform.hpp>
|
|
|
|
#include <mapnik/util/fs.hpp>
|
2015-03-25 02:43:29 +01:00
|
|
|
#include <mapnik/geometry.hpp>
|
2015-03-26 17:31:31 +01:00
|
|
|
#include <mapnik/vertex_adapters.hpp>
|
2015-08-19 12:03:16 +02:00
|
|
|
#include <mapnik/geometry.hpp>
|
2017-03-27 17:14:51 +02:00
|
|
|
#include <mapnik/geometry/boost_adapters.hpp>
|
|
|
|
#include <mapnik/geometry/envelope.hpp>
|
|
|
|
#include <mapnik/geometry/correct.hpp>
|
|
|
|
#include <mapnik/geometry/is_empty.hpp>
|
2014-08-28 11:59:08 +02:00
|
|
|
#include <mapnik/image_util.hpp>
|
2015-03-13 23:28:22 +01:00
|
|
|
#include <mapnik/color.hpp>
|
2015-08-19 12:03:16 +02:00
|
|
|
// boost geometry
|
2015-05-06 07:04:49 +02:00
|
|
|
#include <boost/geometry.hpp>
|
2013-11-19 21:23:37 +01:00
|
|
|
// agg
|
|
|
|
#include "agg_conv_clip_polygon.h"
|
|
|
|
// clipper
|
|
|
|
#include "agg_path_storage.h"
|
|
|
|
// rendering
|
|
|
|
#include "agg_basics.h"
|
|
|
|
#include "agg_rendering_buffer.h"
|
|
|
|
#include "agg_pixfmt_rgba.h"
|
|
|
|
#include "agg_rasterizer_scanline_aa.h"
|
|
|
|
#include "agg_scanline_u.h"
|
|
|
|
#include "agg_renderer_scanline.h"
|
2014-08-28 11:59:08 +02:00
|
|
|
|
|
|
|
// stl
|
2014-05-17 04:30:35 +02:00
|
|
|
#include <fstream>
|
2015-02-12 07:42:31 +01:00
|
|
|
#include <iostream>
|
2015-02-12 00:53:09 +01:00
|
|
|
#include <cstdlib>
|
2013-11-19 21:23:37 +01:00
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
void render(mapnik::geometry::multi_polygon<double> const& geom,
|
2013-11-19 21:23:37 +01:00
|
|
|
mapnik::box2d<double> const& extent,
|
|
|
|
std::string const& name)
|
|
|
|
{
|
2015-04-10 14:17:36 +02:00
|
|
|
using path_type = mapnik::transform_path_adapter<mapnik::view_transform,mapnik::geometry::polygon_vertex_adapter<double>>;
|
2014-07-07 19:23:15 +02:00
|
|
|
using ren_base = agg::renderer_base<agg::pixfmt_rgba32_plain>;
|
|
|
|
using renderer = agg::renderer_scanline_aa_solid<ren_base>;
|
2015-01-22 03:31:02 +01:00
|
|
|
mapnik::image_rgba8 im(256,256);
|
2015-01-21 01:30:10 +01:00
|
|
|
mapnik::fill(im, mapnik::color("white"));
|
2015-03-25 02:43:29 +01:00
|
|
|
mapnik::box2d<double> padded_extent(155,134,665,466);//extent;
|
2013-11-19 21:23:37 +01:00
|
|
|
padded_extent.pad(10);
|
2014-08-28 11:17:15 +02:00
|
|
|
mapnik::view_transform tr(im.width(),im.height(),padded_extent,0,0);
|
2015-05-04 12:49:11 +02:00
|
|
|
agg::rendering_buffer buf(im.bytes(),im.width(),im.height(), im.row_size());
|
2013-11-19 21:23:37 +01:00
|
|
|
agg::pixfmt_rgba32_plain pixf(buf);
|
|
|
|
ren_base renb(pixf);
|
|
|
|
renderer ren(renb);
|
|
|
|
mapnik::proj_transform prj_trans(mapnik::projection("+init=epsg:4326"),mapnik::projection("+init=epsg:4326"));
|
2015-03-25 02:47:44 +01:00
|
|
|
ren.color(agg::rgba8(127,127,127,255));
|
|
|
|
agg::rasterizer_scanline_aa<> ras;
|
2015-03-25 02:43:29 +01:00
|
|
|
for (auto const& poly : geom)
|
|
|
|
{
|
2015-04-10 14:17:36 +02:00
|
|
|
mapnik::geometry::polygon_vertex_adapter<double> va(poly);
|
2015-03-25 02:43:29 +01:00
|
|
|
path_type path(tr,va,prj_trans);
|
|
|
|
ras.add_path(path);
|
|
|
|
}
|
2015-03-25 02:47:44 +01:00
|
|
|
agg::scanline_u8 sl;
|
|
|
|
agg::render_scanlines(ras, sl, ren);
|
2015-01-21 01:30:10 +01:00
|
|
|
mapnik::save_to_file(im,name);
|
2013-11-19 21:23:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
class test1 : public benchmark::test_case
|
|
|
|
{
|
|
|
|
std::string wkt_in_;
|
|
|
|
mapnik::box2d<double> extent_;
|
|
|
|
std::string expected_;
|
|
|
|
public:
|
2015-04-10 14:17:36 +02:00
|
|
|
using conv_clip = agg::conv_clip_polygon<mapnik::geometry::polygon_vertex_adapter<double>>;
|
2013-11-19 21:23:37 +01:00
|
|
|
test1(mapnik::parameters const& params,
|
|
|
|
std::string const& wkt_in,
|
|
|
|
mapnik::box2d<double> const& extent)
|
|
|
|
: test_case(params),
|
|
|
|
wkt_in_(wkt_in),
|
|
|
|
extent_(extent),
|
|
|
|
expected_("./benchmark/data/polygon_clipping_agg") {}
|
|
|
|
bool validate() const
|
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::geometry<double> geom;
|
2015-03-25 02:43:29 +01:00
|
|
|
if (!mapnik::from_wkt(wkt_in_, geom))
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse WKT");
|
|
|
|
}
|
2015-03-25 02:43:29 +01:00
|
|
|
if (mapnik::geometry::is_empty(geom))
|
|
|
|
{
|
|
|
|
std::clog << "empty geom!\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
if (!geom.is<mapnik::geometry::polygon<double>>())
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
2015-03-25 02:43:29 +01:00
|
|
|
std::clog << "not a polygon!\n";
|
2013-11-19 21:23:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::polygon<double> const& poly = mapnik::util::get<mapnik::geometry::polygon<double>>(geom);
|
2015-04-10 14:17:36 +02:00
|
|
|
mapnik::geometry::polygon_vertex_adapter<double> va(poly);
|
2015-03-26 17:31:31 +01:00
|
|
|
|
|
|
|
|
2015-02-12 00:53:09 +01:00
|
|
|
conv_clip clipped(va);
|
2013-11-19 21:23:37 +01:00
|
|
|
clipped.clip_box(
|
|
|
|
extent_.minx(),
|
|
|
|
extent_.miny(),
|
|
|
|
extent_.maxx(),
|
|
|
|
extent_.maxy());
|
2015-03-26 17:31:31 +01:00
|
|
|
|
|
|
|
|
2015-02-15 22:01:57 +01:00
|
|
|
clipped.rewind(0);
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::polygon<double> poly2;
|
|
|
|
mapnik::geometry::linear_ring<double> ring;
|
2015-03-26 17:31:31 +01:00
|
|
|
// exterior ring
|
|
|
|
unsigned cmd;
|
|
|
|
double x, y, x0, y0;
|
|
|
|
while ((cmd = clipped.vertex(&x, &y)) != mapnik::SEG_END)
|
|
|
|
{
|
|
|
|
if (cmd == mapnik::SEG_MOVETO)
|
|
|
|
{
|
|
|
|
x0 = x; y0 = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmd == mapnik::SEG_CLOSE)
|
|
|
|
{
|
2016-06-27 11:23:13 +02:00
|
|
|
ring.emplace_back(x0, y0);
|
2015-03-26 17:31:31 +01:00
|
|
|
break;
|
|
|
|
}
|
2016-06-27 11:23:13 +02:00
|
|
|
ring.emplace_back(x,y);
|
2013-11-19 21:23:37 +01:00
|
|
|
}
|
2016-06-29 13:37:32 +02:00
|
|
|
poly2.push_back(std::move(ring));
|
2015-03-26 17:31:31 +01:00
|
|
|
// interior rings
|
|
|
|
ring.clear();
|
|
|
|
while ((cmd = clipped.vertex(&x, &y)) != mapnik::SEG_END)
|
|
|
|
{
|
|
|
|
if (cmd == mapnik::SEG_MOVETO)
|
|
|
|
{
|
|
|
|
x0 = x; y0 = y;
|
|
|
|
}
|
|
|
|
else if (cmd == mapnik::SEG_CLOSE)
|
|
|
|
{
|
2016-06-27 11:23:13 +02:00
|
|
|
ring.emplace_back(x0,y0);
|
2016-06-29 13:37:32 +02:00
|
|
|
poly2.push_back(std::move(ring));
|
2015-03-26 17:31:31 +01:00
|
|
|
ring.clear();
|
|
|
|
continue;
|
|
|
|
}
|
2016-06-27 11:23:13 +02:00
|
|
|
ring.emplace_back(x,y);
|
2015-03-26 17:31:31 +01:00
|
|
|
}
|
|
|
|
|
2013-11-19 21:23:37 +01:00
|
|
|
std::string expect = expected_+".png";
|
|
|
|
std::string actual = expected_+"_actual.png";
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::multi_polygon<double> mp;
|
2015-03-25 02:43:29 +01:00
|
|
|
mp.emplace_back(poly2);
|
|
|
|
auto env = mapnik::geometry::envelope(mp);
|
2015-02-12 00:53:09 +01:00
|
|
|
if (!mapnik::util::exists(expect) || (std::getenv("UPDATE") != nullptr))
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
|
|
|
std::clog << "generating expected image: " << expect << "\n";
|
2015-03-25 02:43:29 +01:00
|
|
|
render(mp,env,expect);
|
2013-11-19 21:23:37 +01:00
|
|
|
}
|
2015-03-25 02:43:29 +01:00
|
|
|
render(mp,env,actual);
|
2013-11-19 21:23:37 +01:00
|
|
|
return benchmark::compare_images(actual,expect);
|
|
|
|
}
|
2014-12-12 17:59:45 +01:00
|
|
|
bool operator()() const
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::geometry<double> geom;
|
2015-03-25 02:43:29 +01:00
|
|
|
if (!mapnik::from_wkt(wkt_in_, geom))
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse WKT");
|
|
|
|
}
|
2015-03-25 02:43:29 +01:00
|
|
|
if (mapnik::geometry::is_empty(geom))
|
|
|
|
{
|
|
|
|
std::clog << "empty geom!\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
if (!geom.is<mapnik::geometry::polygon<double>>())
|
2015-03-25 02:43:29 +01:00
|
|
|
{
|
|
|
|
std::clog << "not a polygon!\n";
|
2015-03-26 17:31:31 +01:00
|
|
|
return false;
|
2015-03-25 02:43:29 +01:00
|
|
|
}
|
2015-02-15 21:59:55 +01:00
|
|
|
bool valid = true;
|
2013-11-19 21:23:37 +01:00
|
|
|
for (unsigned i=0;i<iterations_;++i)
|
|
|
|
{
|
2015-02-15 21:59:55 +01:00
|
|
|
unsigned count = 0;
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::polygon<double> const& poly = mapnik::util::get<mapnik::geometry::polygon<double>>(geom);
|
2015-04-10 14:17:36 +02:00
|
|
|
mapnik::geometry::polygon_vertex_adapter<double> va(poly);
|
2015-03-25 02:43:29 +01:00
|
|
|
conv_clip clipped(va);
|
|
|
|
clipped.clip_box(
|
|
|
|
extent_.minx(),
|
|
|
|
extent_.miny(),
|
|
|
|
extent_.maxx(),
|
|
|
|
extent_.maxy());
|
|
|
|
unsigned cmd;
|
|
|
|
double x,y;
|
|
|
|
// NOTE: this rewind is critical otherwise
|
|
|
|
// agg_conv_adapter_vpgen will give garbage
|
|
|
|
// values for the first vertex
|
|
|
|
clipped.rewind(0);
|
|
|
|
while ((cmd = clipped.vertex(&x, &y)) != mapnik::SEG_END) {
|
|
|
|
count++;
|
2013-11-19 21:23:37 +01:00
|
|
|
}
|
2015-05-01 00:07:02 +02:00
|
|
|
unsigned expected_count = 30;
|
2015-02-15 21:59:55 +01:00
|
|
|
if (count != expected_count) {
|
|
|
|
std::clog << "test1: clipping failed: processed " << count << " verticies but expected " << expected_count << "\n";
|
|
|
|
valid = false;
|
|
|
|
}
|
2015-02-12 07:42:31 +01:00
|
|
|
}
|
|
|
|
return valid;
|
2013-11-19 21:23:37 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class test3 : public benchmark::test_case
|
|
|
|
{
|
|
|
|
std::string wkt_in_;
|
|
|
|
mapnik::box2d<double> extent_;
|
|
|
|
std::string expected_;
|
|
|
|
public:
|
|
|
|
test3(mapnik::parameters const& params,
|
|
|
|
std::string const& wkt_in,
|
|
|
|
mapnik::box2d<double> const& extent)
|
|
|
|
: test_case(params),
|
|
|
|
wkt_in_(wkt_in),
|
|
|
|
extent_(extent),
|
|
|
|
expected_("./benchmark/data/polygon_clipping_boost") {}
|
|
|
|
bool validate() const
|
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::geometry<double> geom;
|
2015-03-25 02:43:29 +01:00
|
|
|
if (!mapnik::from_wkt(wkt_in_, geom))
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse WKT");
|
|
|
|
}
|
2015-03-25 02:43:29 +01:00
|
|
|
if (mapnik::geometry::is_empty(geom))
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
2015-03-25 02:43:29 +01:00
|
|
|
std::clog << "empty geom!\n";
|
2013-11-19 21:23:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
if (!geom.is<mapnik::geometry::polygon<double> >())
|
2015-03-25 02:43:29 +01:00
|
|
|
{
|
|
|
|
std::clog << "not a polygon!\n";
|
|
|
|
return false;
|
2013-11-19 21:23:37 +01:00
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::polygon<double> & poly = mapnik::util::get<mapnik::geometry::polygon<double> >(geom);
|
2015-03-25 02:43:29 +01:00
|
|
|
mapnik::geometry::correct(poly);
|
2015-04-10 14:52:40 +02:00
|
|
|
|
2015-08-19 12:03:16 +02:00
|
|
|
mapnik::geometry::linear_ring<double> bbox;
|
2016-06-27 11:23:13 +02:00
|
|
|
bbox.emplace_back(extent_.minx(), extent_.miny());
|
|
|
|
bbox.emplace_back(extent_.minx(), extent_.maxy());
|
|
|
|
bbox.emplace_back(extent_.maxx(), extent_.maxy());
|
|
|
|
bbox.emplace_back(extent_.maxx(), extent_.miny());
|
|
|
|
bbox.emplace_back(extent_.minx(), extent_.miny());
|
2015-08-19 12:03:16 +02:00
|
|
|
|
2015-04-09 22:22:51 +02:00
|
|
|
std::deque<mapnik::geometry::polygon<double> > result;
|
2015-08-19 12:03:16 +02:00
|
|
|
boost::geometry::intersection(bbox, poly, result);
|
2015-04-10 14:52:40 +02:00
|
|
|
|
2013-11-19 21:23:37 +01:00
|
|
|
std::string expect = expected_+".png";
|
|
|
|
std::string actual = expected_+"_actual.png";
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::multi_polygon<double> mp;
|
2015-06-12 23:51:18 +02:00
|
|
|
for (auto const& _geom: result)
|
2015-03-25 02:43:29 +01:00
|
|
|
{
|
|
|
|
//std::clog << boost::geometry::dsv(geom) << "\n";
|
2015-06-12 23:51:18 +02:00
|
|
|
mp.emplace_back(_geom);
|
2015-03-25 02:43:29 +01:00
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::geometry<double> geom2(mp);
|
2015-03-25 02:43:29 +01:00
|
|
|
auto env = mapnik::geometry::envelope(geom2);
|
2015-02-12 00:53:09 +01:00
|
|
|
if (!mapnik::util::exists(expect) || (std::getenv("UPDATE") != nullptr))
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
|
|
|
std::clog << "generating expected image: " << expect << "\n";
|
2015-03-25 02:43:29 +01:00
|
|
|
render(mp,env,expect);
|
2013-11-19 21:23:37 +01:00
|
|
|
}
|
2015-03-25 02:43:29 +01:00
|
|
|
render(mp,env,actual);
|
2013-11-19 21:23:37 +01:00
|
|
|
return benchmark::compare_images(actual,expect);
|
|
|
|
}
|
2014-12-12 17:59:45 +01:00
|
|
|
bool operator()() const
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::geometry<double> geom;
|
2015-03-25 02:43:29 +01:00
|
|
|
if (!mapnik::from_wkt(wkt_in_, geom))
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse WKT");
|
|
|
|
}
|
2015-03-25 02:43:29 +01:00
|
|
|
if (mapnik::geometry::is_empty(geom))
|
|
|
|
{
|
|
|
|
std::clog << "empty geom!\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
if (!geom.is<mapnik::geometry::polygon<double> >())
|
2015-03-25 02:43:29 +01:00
|
|
|
{
|
|
|
|
std::clog << "not a polygon!\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::polygon<double> & poly = mapnik::util::get<mapnik::geometry::polygon<double> >(geom);
|
2015-03-25 02:43:29 +01:00
|
|
|
mapnik::geometry::correct(poly);
|
2015-04-10 14:52:40 +02:00
|
|
|
|
2015-08-19 12:03:16 +02:00
|
|
|
mapnik::geometry::linear_ring<double> bbox;
|
2016-06-27 11:23:13 +02:00
|
|
|
bbox.emplace_back(extent_.minx(), extent_.miny());
|
|
|
|
bbox.emplace_back(extent_.minx(), extent_.maxy());
|
|
|
|
bbox.emplace_back(extent_.maxx(), extent_.maxy());
|
|
|
|
bbox.emplace_back(extent_.maxx(), extent_.miny());
|
|
|
|
bbox.emplace_back(extent_.minx(), extent_.miny());
|
2015-08-19 12:03:16 +02:00
|
|
|
|
2015-02-15 21:59:55 +01:00
|
|
|
bool valid = true;
|
2013-11-19 21:23:37 +01:00
|
|
|
for (unsigned i=0;i<iterations_;++i)
|
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
std::deque<mapnik::geometry::polygon<double> > result;
|
2015-08-19 12:03:16 +02:00
|
|
|
boost::geometry::intersection(bbox, poly, result);
|
2015-02-15 21:59:55 +01:00
|
|
|
unsigned count = 0;
|
2015-06-12 23:51:18 +02:00
|
|
|
for (auto const& _geom : result)
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
2015-06-12 23:51:18 +02:00
|
|
|
mapnik::geometry::polygon_vertex_adapter<double> va(_geom);
|
2013-11-19 21:23:37 +01:00
|
|
|
unsigned cmd;
|
|
|
|
double x,y;
|
2015-03-25 02:43:29 +01:00
|
|
|
while ((cmd = va.vertex(&x, &y)) != mapnik::SEG_END) {
|
2015-03-26 17:31:31 +01:00
|
|
|
++count;
|
2015-02-12 07:42:31 +01:00
|
|
|
}
|
2015-03-26 17:31:31 +01:00
|
|
|
unsigned expected_count = 29;
|
2015-03-25 02:43:29 +01:00
|
|
|
if (count != expected_count) {
|
|
|
|
std::clog << "test3: clipping failed: processed " << count << " verticies but expected " << expected_count << "\n";
|
|
|
|
valid = false;
|
|
|
|
}
|
2015-02-15 21:59:55 +01:00
|
|
|
}
|
2015-02-12 07:42:31 +01:00
|
|
|
}
|
|
|
|
return valid;
|
2013-11-19 21:23:37 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-02 21:14:21 +02:00
|
|
|
/*
|
|
|
|
Commented out section because clipper moved out of mapnik core.
|
2015-05-01 00:29:30 +02:00
|
|
|
inline void process_polynode_branch(ClipperLib::PolyNode* polynode,
|
|
|
|
mapnik::geometry::multi_polygon<double> & mp)
|
|
|
|
{
|
|
|
|
mapnik::geometry::polygon<double> polygon;
|
|
|
|
mapnik::geometry::linear_ring<double> outer;
|
|
|
|
for (auto const& pt : polynode->Contour)
|
|
|
|
{
|
|
|
|
outer.emplace_back(static_cast<double>(pt.x),static_cast<double>(pt.y));
|
|
|
|
}
|
|
|
|
if (outer.front() != outer.back())
|
|
|
|
{
|
|
|
|
outer.emplace_back(outer.front().x, outer.front().y);
|
|
|
|
}
|
|
|
|
polygon.set_exterior_ring(std::move(outer));
|
|
|
|
for (auto * ring : polynode->Childs)
|
|
|
|
{
|
|
|
|
mapnik::geometry::linear_ring<double> inner;
|
|
|
|
for (auto const& pt : ring->Contour)
|
|
|
|
{
|
|
|
|
inner.emplace_back(static_cast<double>(pt.x),static_cast<double>(pt.y));
|
|
|
|
}
|
|
|
|
if (inner.front() != inner.back())
|
|
|
|
{
|
|
|
|
inner.emplace_back(inner.front().x, inner.front().y);
|
|
|
|
}
|
|
|
|
polygon.add_hole(std::move(inner));
|
|
|
|
}
|
|
|
|
mp.emplace_back(std::move(polygon));
|
|
|
|
for (auto * ring : polynode->Childs)
|
|
|
|
{
|
|
|
|
for (auto * sub_ring : ring->Childs)
|
|
|
|
{
|
|
|
|
process_polynode_branch(sub_ring, mp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-02 18:19:06 +02:00
|
|
|
class test4 : public benchmark::test_case
|
|
|
|
{
|
|
|
|
std::string wkt_in_;
|
|
|
|
mapnik::box2d<double> extent_;
|
|
|
|
std::string expected_;
|
|
|
|
public:
|
|
|
|
test4(mapnik::parameters const& params,
|
|
|
|
std::string const& wkt_in,
|
|
|
|
mapnik::box2d<double> const& extent)
|
|
|
|
: test_case(params),
|
|
|
|
wkt_in_(wkt_in),
|
|
|
|
extent_(extent),
|
|
|
|
expected_("./benchmark/data/polygon_clipping_clipper") {}
|
|
|
|
bool validate() const
|
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::geometry<double> geom;
|
2015-04-02 18:19:06 +02:00
|
|
|
if (!mapnik::from_wkt(wkt_in_, geom))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse WKT");
|
|
|
|
}
|
|
|
|
if (mapnik::geometry::is_empty(geom))
|
|
|
|
{
|
|
|
|
std::clog << "empty geom!\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
if (!geom.is<mapnik::geometry::polygon<double> >())
|
2015-04-02 18:19:06 +02:00
|
|
|
{
|
|
|
|
std::clog << "not a polygon!\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::polygon<double> & poly = mapnik::util::get<mapnik::geometry::polygon<double> >(geom);
|
2015-04-02 18:19:06 +02:00
|
|
|
mapnik::geometry::correct(poly);
|
|
|
|
ClipperLib::Clipper clipper;
|
|
|
|
|
2015-04-21 17:29:22 +02:00
|
|
|
mapnik::geometry::line_string<std::int64_t> path;
|
2015-04-02 18:19:06 +02:00
|
|
|
for (auto const& pt : poly.exterior_ring)
|
|
|
|
{
|
|
|
|
double x = pt.x;
|
|
|
|
double y = pt.y;
|
|
|
|
path.emplace_back(static_cast<ClipperLib::cInt>(x),static_cast<ClipperLib::cInt>(y));
|
|
|
|
}
|
2015-05-01 00:29:30 +02:00
|
|
|
double area = ClipperLib::Area(path);
|
|
|
|
if (area > 0)
|
|
|
|
{
|
|
|
|
std::reverse(path.begin(), path.end());
|
2015-05-04 10:57:57 +02:00
|
|
|
}
|
2015-04-02 18:19:06 +02:00
|
|
|
if (!clipper.AddPath(path, ClipperLib::ptSubject, true))
|
|
|
|
{
|
|
|
|
std::clog << "ptSubject ext failed!\n";
|
|
|
|
}
|
|
|
|
for (auto const& ring : poly.interior_rings)
|
|
|
|
{
|
|
|
|
path.clear();
|
|
|
|
for (auto const& pt : ring)
|
|
|
|
{
|
|
|
|
double x = pt.x;
|
|
|
|
double y = pt.y;
|
|
|
|
path.emplace_back(static_cast<ClipperLib::cInt>(x),static_cast<ClipperLib::cInt>(y));
|
|
|
|
}
|
2015-05-01 00:29:30 +02:00
|
|
|
area = ClipperLib::Area(path);
|
|
|
|
if (area < 0)
|
|
|
|
{
|
|
|
|
std::reverse(path.begin(), path.end());
|
2015-05-04 10:57:57 +02:00
|
|
|
}
|
2015-04-02 18:19:06 +02:00
|
|
|
if (!clipper.AddPath(path, ClipperLib::ptSubject, true))
|
|
|
|
{
|
|
|
|
std::clog << "ptSubject ext failed!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cerr << "path size=" << path.size() << std::endl;
|
2015-04-21 17:29:22 +02:00
|
|
|
mapnik::geometry::line_string<std::int64_t> clip_box;
|
2015-04-02 18:19:06 +02:00
|
|
|
clip_box.emplace_back(static_cast<ClipperLib::cInt>(extent_.minx()),static_cast<ClipperLib::cInt>(extent_.miny()));
|
|
|
|
clip_box.emplace_back(static_cast<ClipperLib::cInt>(extent_.maxx()),static_cast<ClipperLib::cInt>(extent_.miny()));
|
|
|
|
clip_box.emplace_back(static_cast<ClipperLib::cInt>(extent_.maxx()),static_cast<ClipperLib::cInt>(extent_.maxy()));
|
|
|
|
clip_box.emplace_back(static_cast<ClipperLib::cInt>(extent_.minx()),static_cast<ClipperLib::cInt>(extent_.maxy()));
|
|
|
|
clip_box.emplace_back(static_cast<ClipperLib::cInt>(extent_.minx()),static_cast<ClipperLib::cInt>(extent_.miny()));
|
|
|
|
|
|
|
|
if (!clipper.AddPath( clip_box, ClipperLib::ptClip, true ))
|
|
|
|
{
|
|
|
|
std::clog << "ptClip failed!\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
ClipperLib::PolyTree polygons;
|
2015-05-01 00:29:30 +02:00
|
|
|
clipper.Execute(ClipperLib::ctIntersection, polygons);// ClipperLib::pftNonZero);
|
2015-04-02 18:19:06 +02:00
|
|
|
clipper.Clear();
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::multi_polygon<double> mp;
|
2015-05-01 00:29:30 +02:00
|
|
|
for (auto * polynode : polygons.Childs)
|
2015-04-02 18:19:06 +02:00
|
|
|
{
|
2015-05-01 00:29:30 +02:00
|
|
|
process_polynode_branch(polynode, mp);
|
2015-04-02 18:19:06 +02:00
|
|
|
}
|
|
|
|
std::string expect = expected_+".png";
|
|
|
|
std::string actual = expected_+"_actual.png";
|
2015-05-01 00:29:30 +02:00
|
|
|
//mapnik::geometry::geometry<double> geom2(mp);
|
|
|
|
auto env = mapnik::geometry::envelope(mp);
|
2015-04-02 18:19:06 +02:00
|
|
|
if (!mapnik::util::exists(expect) || (std::getenv("UPDATE") != nullptr))
|
|
|
|
{
|
|
|
|
std::clog << "generating expected image: " << expect << "\n";
|
|
|
|
render(mp,env,expect);
|
|
|
|
}
|
|
|
|
render(mp,env,actual);
|
|
|
|
return benchmark::compare_images(actual,expect);
|
|
|
|
}
|
|
|
|
bool operator()() const
|
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::geometry<double> geom;
|
2015-04-02 18:19:06 +02:00
|
|
|
if (!mapnik::from_wkt(wkt_in_, geom))
|
|
|
|
{
|
|
|
|
throw std::runtime_error("Failed to parse WKT");
|
|
|
|
}
|
|
|
|
if (mapnik::geometry::is_empty(geom))
|
|
|
|
{
|
|
|
|
std::clog << "empty geom!\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
if (!geom.is<mapnik::geometry::polygon<double> >())
|
2015-04-02 18:19:06 +02:00
|
|
|
{
|
|
|
|
std::clog << "not a polygon!\n";
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-09 22:22:51 +02:00
|
|
|
mapnik::geometry::polygon<double> & poly = mapnik::util::get<mapnik::geometry::polygon<double> >(geom);
|
2015-04-02 18:19:06 +02:00
|
|
|
mapnik::geometry::correct(poly);
|
2015-04-10 14:52:40 +02:00
|
|
|
|
2015-04-02 18:19:06 +02:00
|
|
|
bool valid = true;
|
|
|
|
for (unsigned i=0;i<iterations_;++i)
|
|
|
|
{
|
2015-04-09 22:22:51 +02:00
|
|
|
std::deque<mapnik::geometry::polygon<double> > result;
|
2015-04-09 12:33:31 +02:00
|
|
|
boost::geometry::intersection(extent_,poly,result);
|
2015-04-10 14:52:40 +02:00
|
|
|
|
2015-04-02 18:19:06 +02:00
|
|
|
unsigned count = 0;
|
|
|
|
for (auto const& geom : result)
|
|
|
|
{
|
2015-04-10 14:17:36 +02:00
|
|
|
mapnik::geometry::polygon_vertex_adapter<double> va(geom);
|
2015-04-02 18:19:06 +02:00
|
|
|
unsigned cmd;
|
|
|
|
double x,y;
|
|
|
|
while ((cmd = va.vertex(&x, &y)) != mapnik::SEG_END) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
unsigned expected_count = 29;
|
|
|
|
if (count != expected_count) {
|
|
|
|
std::clog << "test3: clipping failed: processed " << count << " verticies but expected " << expected_count << "\n";
|
|
|
|
valid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return valid;
|
|
|
|
}
|
|
|
|
};
|
2015-06-02 21:14:21 +02:00
|
|
|
*/
|
2015-04-02 18:19:06 +02:00
|
|
|
|
2013-11-19 21:23:37 +01:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
mapnik::parameters params;
|
|
|
|
benchmark::handle_args(argc,argv,params);
|
|
|
|
|
|
|
|
// polygon/rect clipping
|
|
|
|
// IN : POLYGON ((155 203, 233 454, 315 340, 421 446, 463 324, 559 466, 665 253, 528 178, 394 229, 329 138, 212 134, 183 228, 200 264, 155 203),(313 190, 440 256, 470 248, 510 305, 533 237, 613 263, 553 397, 455 262, 405 378, 343 287, 249 334, 229 191, 313 190))
|
|
|
|
// RECT : POLYGON ((181 106, 181 470, 631 470, 631 106, 181 106))
|
|
|
|
// OUT (expected)
|
|
|
|
// POLYGON ((181 286.6666666666667, 233 454, 315 340, 421 446, 463 324, 559 466, 631 321.3207547169811, 631 234.38686131386862, 528 178, 394 229, 329 138, 212 134, 183 228, 200 264, 181 238.24444444444444, 181 286.6666666666667),(313 190, 440 256, 470 248, 510 305, 533 237, 613 263, 553 397, 455 262, 405 378, 343 287, 249 334, 229 191, 313 190))
|
2016-06-29 13:37:32 +02:00
|
|
|
#if 0
|
2013-11-19 21:23:37 +01:00
|
|
|
mapnik::box2d<double> clipping_box(181,106,631,470);
|
|
|
|
std::string filename_("./benchmark/data/polygon.wkt");
|
|
|
|
std::ifstream in(filename_.c_str(),std::ios_base::in | std::ios_base::binary);
|
|
|
|
if (!in.is_open())
|
|
|
|
throw std::runtime_error("could not open: '" + filename_ + "'");
|
|
|
|
std::string wkt_in( (std::istreambuf_iterator<char>(in) ),
|
|
|
|
(std::istreambuf_iterator<char>()) );
|
2015-09-14 15:21:51 +02:00
|
|
|
int return_value = 0;
|
2013-11-19 21:23:37 +01:00
|
|
|
{
|
|
|
|
test1 test_runner(params,wkt_in,clipping_box);
|
2015-09-14 15:21:51 +02:00
|
|
|
return_value = return_value | run(test_runner,"clipping polygon with agg");
|
2013-11-19 21:23:37 +01:00
|
|
|
}
|
|
|
|
{
|
|
|
|
test3 test_runner(params,wkt_in,clipping_box);
|
2015-09-14 15:21:51 +02:00
|
|
|
return_value = return_value | run(test_runner,"clipping polygon with boost");
|
2013-11-19 21:23:37 +01:00
|
|
|
}
|
2015-06-02 21:14:21 +02:00
|
|
|
/*
|
2015-04-02 18:19:06 +02:00
|
|
|
{
|
|
|
|
test4 test_runner(params,wkt_in,clipping_box);
|
2015-09-14 15:21:51 +02:00
|
|
|
return_value = return_value | run(test_runner,"clipping polygon with clipper_tree");
|
2015-04-02 18:19:06 +02:00
|
|
|
}
|
2015-06-02 21:14:21 +02:00
|
|
|
*/
|
2016-06-29 13:37:32 +02:00
|
|
|
#endif
|
|
|
|
return 0;// return_value;
|
2013-11-19 21:23:37 +01:00
|
|
|
}
|