Boost logo

Geometry :

Subject: Re: [geometry] union gives an empty multi-polygon
From: Romain Texier (romain.texier_at_[hidden])
Date: 2012-12-27 13:30:57


You have solved my problem :)
My polygons are open. I modify the typedef:

typedef boost::geometry::model::polygon<BoostPoint,false,false>
BoostPolygon;
typedef boost::geometry::model::ring<BoostPoint,false,false> BoostRing;

The unions of 2 triangles works, but...
Boost:geometry send an exception: overlay_invalid_input_exception
(file has_self_intersections.hpp, line 103, boost version 1.52)

How can I have invalid multi-polygon ? I create my multi-polygon with
union_ of triangles.

void BoostShape::AddTriangle (Triangle2D const & NewTriangle,
BoostPolygons& p_InOutPolygon)
{
BoostRing outerRingTriangle;
outerRingTriangle.push_back(BoostPoint(NewTriangle.m_Vertex0.X,NewTriangle.m_Vertex0.Y));
outerRingTriangle.push_back(BoostPoint(NewTriangle.m_Vertex1.X,NewTriangle.m_Vertex1.Y));
outerRingTriangle.push_back(BoostPoint(NewTriangle.m_Vertex2.X,NewTriangle.m_Vertex2.Y));

BoostPolygon triangleForBoost;
triangleForBoost.outer() = outerRingTriangle;
 boost::geometry::correct(p_InOutPolygon); //no need ?

dprintf("\np_InOutPolygon");
displayPolygon( p_InOutPolygon );
dprintf("\nTriangle");
displayListPoints( outerRingTriangle );

BoostPolygons Result;
boost::geometry::union_(p_InOutPolygon, triangleForBoost, Result);
p_InOutPolygon = Result;
}

p_InOutPolygon
multi-polygons of 7 polygon.
Outer:
-0.224681 -0.365764, -0.224666 -0.365949, -0.224640 -0.365833
Inners (0 rings)
Outer:
-0.213254 -0.308864, -0.213202 -0.308727, -0.213587 -0.309283
Inners (0 rings)
Outer:
-0.175735 -0.472076, -0.171028 -0.462562, -0.193918 -0.449594
Inners (0 rings)
Outer:
-0.221273 -0.448986, -0.222565 -0.441324, -0.225446 -0.435824, -0.229420
-0.429482
Inners (0 rings)
Outer:
-0.187878 -0.469588, -0.181198 -0.476493, -0.173994 -0.484181, -0.173781
-0.484409,
-0.185430 -0.479707, -0.172860 -0.485391, -0.165541 -0.488701, -0.166452
-0.487366,
-0.176400 -0.473783, -0.176154 -0.473152, -0.175735 -0.472076, -0.192115
-0.451824,
-0.190885 -0.465624, -0.190846 -0.466058, -0.190832 -0.466211, -0.190801
-0.466566
Inners (0 rings)
Outer:
-0.171415 -0.448794, -0.168636 -0.462456, -0.171394 -0.448805, -0.171397
-0.448793,
Inners (0 rings)
Outer:
-0.177856 -0.360414, -0.175695 -0.349651, -0.180607 -0.370833, -0.181211
-0.376689,
-0.181453 -0.374482, -0.181027 -0.372658, -0.182435 -0.374140, -0.183843
-0.377663,
-0.191645 -0.397212, -0.188740 -0.398608, -0.187580 -0.399165, -0.178250
-0.403648,
-0.179510 -0.392176, -0.181204 -0.376751, -0.180983 -0.375982
Inners (0 rings)

Triangle
-0.224512 -0.365852, -0.224640 -0.365833, -0.224666 -0.365949

My class of Triangle2D and Vertex2, if you need it

struct Triangle2D
{
Vertex2 m_Vertex0;
Vertex2 m_Vertex1;
Vertex2 m_Vertex2;
Triangle2D (Vertex2 P0, Vertex2 P1, Vertex2 P2) : m_Vertex0(P0),
m_Vertex1(P1), m_Vertex2(P2) {} ;

// return true if counter-clockwise
bool Orientation () { return ((m_Vertex1.X - m_Vertex0.X)*(m_Vertex2.Y -
m_Vertex0.Y) > (m_Vertex2.X - m_Vertex0.X)*(m_Vertex1.Y - m_Vertex0.Y)) ; }
;
} ;

class Vertex2
{
public :
float X, Y ;
Vertex2 () : X(0.0f), Y(0.0f) { }
Vertex2 (float fX, float fY) : X(fX), Y(fY) { }
 inline Vertex2& operator += (const Vertex2 &v) { X += v.X ; Y += v.Y ;
return *this ; }
inline Vertex2& operator -= (const Vertex2 &v) { X -= v.X ; Y -= v.Y ;
return *this ; }
inline Vertex2& operator *= (float s) { X *= s ; Y *= s ; return *this ; }
};

Regards,
Romain

2012/12/27 Barend Gehrels <barend_at_[hidden]>

> Hi Romain,
>
> Welcome to the list.
>
>
> On 27-12-2012 15:11, Romain Texier wrote:
>
> Hi,
>
> I want to make real time union and clipping of many polygons.
> I have tried the clipper library and I would like to try the boost
> geometry library.
>
> But I have a problem: The union of two triangles is an empty
> multi-polygon.
> Could you help me ?
>
>
> typedef boost::geometry::model::d2::point_xy<float> BoostPoint;
> typedef boost::geometry::model::polygon<BoostPoint,false> BoostPolygon;
> //false because my triangles are counter-clockwise.
> typedef boost::geometry::model::multi_polygon< BoostPolygon >
> BoostPolygons;
>
>
> I did not (yet) test the code, because I don't have the definitions of
> your triangles, but I'm pretty sure that your polygons are not closed. You
> defined them counterclockwise, but you should also define them as "open" if
> you want them to use this way. So the definition should be BoostPoint,
> false, false>.
>
> See also this link:
> http://www.boost.org/doc/libs/1_52_0/libs/geometry/doc/html/geometry/reference/models/model_polygon.html
>
>
>
> void BoostShape::createNewPoly(Triangle2D const & NewTriangle,
> BoostPolygons& p_InOutPolygon)
> {
> model::ring<BoostPoint,false> outerRingTriangle;
>
> outerRingTriangle.push_back(BoostPoint(NewTriangle.m_Vertex0.X,NewTriangle.m_Vertex0.Y));
>
> outerRingTriangle.push_back(BoostPoint(NewTriangle.m_Vertex1.X,NewTriangle.m_Vertex1.Y));
>
> outerRingTriangle.push_back(BoostPoint(NewTriangle.m_Vertex2.X,NewTriangle.m_Vertex2.Y));
>
>
>
> What also helps is calling boost::geometry::correct (which would close
> them in this code).
>
> HTH, Barend
>
>
> _______________________________________________
> Geometry mailing list
> Geometry_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/geometry
>
>



Geometry list run by mateusz at loskot.net