I'd like to take the geometric difference of two geometric objects with Boost Geometry. The first geometry is:
I always have line segments making up the geometry. I have the coordinates and the segments:
{{-1., -1.}, {1., -1.}, {1., 1.}, {-1., 1.}, {0.6, -1}, {0.6,1.}};
{Line[{{1, 5}, {5, 2}, {2, 3}, {3, 6}, {6, 4}, {4, 1}}], Line[{{5, 6}}]};
were each index in the lines refers to a coordinates. Either of the geometries can have internal boundaries (and holes).
The second geometry:
And I am expecting to obtain this:
I have started with polygon but I do not understand how I could include the interior boundary of the first geometric object. While one can have interior holes I am missing how to have a 'degenerated' polygon like a line. Also it's not clear to me which of the two polygons then would be the outer polygon. Then I thought about line, but there I can not add the internal segment. Can Boost Geometry be used find geometric Boolean operations for geometries with internal boundaries and it so, how would I do that?
The code for the polygon approach that misses the internal boundary; commented out is the line type approach; again with out the internal boundary in first geometric object.
#include <iostream>
#include <fstream>
#include <list>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/foreach.hpp>
int main()
{
typedef boost::geometry::model::d2::point_xy<double> point_type;
typedef boost::geometry::model::polygon<point_type > polygon;
//typedef boost::geometry::model::linestring<point_type > line;
polygon p1, p2;
//line l1, l2;
boost::geometry::read_wkt(
"POLYGON((-1 -1, 0.6 -1, 1 -1, 1 1, 0.6 1, -1 1))", p1);
boost::geometry::read_wkt(
"POLYGON((-0.5 -0.5, 2 -0.5, 2 0.5, -0.5 0.5))", p2);
/*
boost::geometry::read_wkt(
"LINESTRING(-1 -1, 0.6 -1, 1 -1, 1 1, 0.6 1, -1 1)", l1);
boost::geometry::read_wkt(
"LINESTRING(-0.5 -0.5, 2 -0.5, 2 0.5, -0.5 0.5)", l2);
*/
std::list<polygon> output;
boost::geometry::difference(p2, p1, output);
//std::list<line> loutput;
//boost::geometry::difference(l2, l1, loutput);
std::ofstream svg("my_map.svg");
boost::geometry::svg_mapper<point_type> mapper(svg, 400, 400);
int i = 0;
BOOST_FOREACH(polygon const& p, output)
{
mapper.add(p);
mapper.map(p, "fill-opacity:0.3;fill:rgb(51,151,53);stroke:rgb(51,151,53);stroke-width:2");
}
/*
int i = 0;
BOOST_FOREACH(line const& l, loutput)
{
mapper.add(l);
mapper.map(l, "opacity:0.4;fill:none;stroke:rgb(212,0,0);stroke-width:5");
}
*/
return 0;
}
POLYGON((-1 -1, 0.6 -1, 0.6 1, 0.6 -1, 1 -1, 1 1, -1 1))
– coproc