Boost logo

Geometry :

Subject: [ggl] Unioning many polygons
From: John Swensen (jpswensen)
Date: 2011-10-10 16:50:10


On Oct 9, 2011, at 4:31 PM, Barend Gehrels wrote:

> 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

Thanks, that was exactly the solution. I still don't know if this is the "right" way, but it works great

BOOST_FOREACH(polygon_2d poly, splits)
    {
        polygon_set tmp_union;
        boost::geometry::union_ (unionMultiPoly, poly, tmp_union);
        unionMultiPoly = tmp_union;
    }

I now have one more question about how unique and simplification work (and exactly how to create my own simplification algorithm. As an example, let's say I have two polygons (actually rectangles) with the following coordinates:
POLY1: (0,0);(0,8.5);(10,8.5);(10,0);(0,0)
POLY2: (10,8.5);(0,8.5);(0,10);(10,10);(10,8.5)

After I union these together, peform simplify, and perform unique, I get the following polygon:
POLY_UNION: (10,8.5);(10,0);(0,0);(0,10);(10,10);(10,8.5)

This is the right shape for the final polygon, but three co-linear lines (i.e. the (10,10) to (10,8.5) to (10,0)) was no simplified to a single line segment. I'm assuming this was somehow because the (10,8.5) is the start and end point of the polygon. Is there a way to "rotate" the start point of the polygon and then re-run simplify/unique to get this to be a single segment? If not is there (1) another algorithm that already exists or (2) and example of how an algorithm is implemented so I can write my own?

John


Geometry list run by mateusz at loskot.net