Boost logo

Geometry :

Subject: [ggl] Unioning many polygons
From: Barend Gehrels (barend)
Date: 2011-10-09 23:49:36


hi John,

On 8-10-2011 16:42, John Swensen wrote:
> I am finally getting back to my iphone game that will use Boost.Geometry.

Welcome back ;-)

> I have all the splitting and mirroring of polygons working, but am having trouble unioning polygons back together. I don't quite get why the results of the boost::geometry::union_ operation are placed in a vector.

The result of a union is either a vector of polygons, or a multi_polygon
(which is essentially a vector of polygons).

The union process does not know beforehand that the result is only one
polygon, so that is the reason. But there might be one (or zero, or
more) polygons in the vector.

The input takes two geometries. If you want to union an array of
geometries, you can add them iteratively to the result, or do it by
divide-and-conquer (partition), this is described a.o. here (for
intersection):

<http://boost-geometry.203548.n3.nabble.com/intersection-of-two-vectors-of-polygons-td2875513.html>

Regards, Barend

>
> I would like to do something like the following
>
> // Split the polygon along the line
> std::vector<polygon_2d> splits = splitPolygon (line, a);
> std::vector<polygon_2d> mirrors;
>
> // Mirror every polygon
> BOOST_FOREACH(polygon_2d poly, splits)
> {
> polygon_2d tmp = mirrorPolygon (line, poly);
> mirrors.push_back(tmp);
> }
>
> // Union all the split polygons and their mirrors together
> std::vector<polygon_2d> unionPoly;
> BOOST_FOREACH(polygon_2d poly, splits)
> {
> boost::geometry::union_ (unionPoly, poly, unionPoly);
> }
>
> BOOST_FOREACH(polygon_2d poly, mirrors)
> {
> boost::geometry::union_ (unionPoly, poly, unionPoly);
> }
>
> Of course this doesn't work because union_ takes polygon_2d in my case, not a std::vector<polygon_2d> as its first argument.
>
> What is the correct way of unioning a bunch of polygons together?
>

Actually this is nearly right, you're doing it iteratively here. But you
should use the multi_polygon<polygon_2d> instead of std::vector, because
the first argument needs to be a geometry.

Regards, Barend


Geometry list run by mateusz at loskot.net