Example5 – Boolean Operations

Boolean Operations on Polygons

We use the term Zone to denote polygonal areas in a triangulation. Fade provides functions to combine Zones through the boolean set operations

  • Union
  • Intersection
  • Difference
  • Symmetric Difference

Preparing the initial situation

We start similarly as in example4 creating two polygons (ConstraintGraph2 objects). But this time the constraint graphs pCG1 and pCG2 do intersect each other.

	// 1) Insert 4 points
	Fade_2D dt;
	dt.insert(Point2(-100,-100));
	dt.insert(Point2(+100,-100));
	dt.insert(Point2(+100,+270));
	dt.insert(Point2(-100,+270));

	// 2) Prepare and insert two constraint graphs
	std::vector<Point2> vConstraintPoints;
	vConstraintPoints.push_back(Point2(80,0));
	vConstraintPoints.push_back(Point2(49.8,62.5));
	vConstraintPoints.push_back(Point2(-17.8,78));
	vConstraintPoints.push_back(Point2(-72,34.7));
	vConstraintPoints.push_back(Point2(-72,-34.7));
	vConstraintPoints.push_back(Point2(-17.8,-78));
	vConstraintPoints.push_back(Point2(49.8,-62.5));

	std::vector<Segment2> vSegments1;
	std::vector<Segment2> vSegments2;
	for(size_t i=0;i<vConstraintPoints.size();++i)
	{
		Point2& p0(vConstraintPoints[i]);
		Point2& p1(vConstraintPoints[(i+1)%vConstraintPoints.size()]);
		vSegments1.push_back(Segment2(p0,p1));

		Point2 p0a(p0.x(),p0.y()+90);
		Point2 p1a(p1.x(),p1.y()+90);
		vSegments2.push_back(Segment2(p0a,p1a));
	}

	ConstraintGraph2* pCG1(NULL);
	ConstraintGraph2* pCG2(NULL);
	pCG1=dt.createConstraint(vSegments1,CIS_CONSTRAINED_DELAUNAY);
	pCG2=dt.createConstraint(vSegments2,CIS_CONSTRAINED_DELAUNAY);
	// 3) Apply and show the triangulation and constraints
	dt.applyConstraintsAndZones();
	dt.show("example5_constraints.ps");

	// Make sure pCG1 and pCG2 are closed
	if(!pCG1->isPolygon() || !pCG2->isPolygon() )
	{
		cout<<"pCG1 and pCG2 must be closed polygons, stop"<<endl;
		return 1;
	}
	// Create two inside zones
	Zone2* pZone0(dt.createZone(pCG1,ZL_INSIDE));
	Zone2* pZone1(dt.createZone(pCG2,ZL_INSIDE));

Below the intersecting polygons and the two zones are shown.

Intersecting polygons

Intersecting polygons


Zone0

Zone0


Zone1

Zone1


Boolean operations with polygons (zones)

A boolean operation on two polygons requires just one line of code.

	//    a) Union operation
	Zone2* pZoneUnion(zoneUnion(pZone0,pZone1));

	//    b) Intersection operation
	Zone2* pZoneIntersection(zoneIntersection(pZone0,pZone1));

	//    c) Difference operation
	Zone2* pZoneDifference(zoneDifference(pZone0,pZone1));

	//    d) Symmetric Difference operation
	Zone2* pZoneSymDifference(zoneSymmetricDifference(pZone0,pZone1));
Union operation

Union operation


Intersection operation

Intersection operation


Difference

Difference


Symmetric Difference

Symmetric Difference


The triangles of these zones can be conveniently extracted. But you can also use the existing zones to generate high quality triangular meshes. See Example6 – Mesh Generation and the article about Advanced Mesh Generation.

Spread the word. Share this post!

Leave A Reply

Your email address will not be published. Required fields are marked *

*

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close