Last active
July 26, 2017 19:47
-
-
Save TimSC/d79edbcd2acb101a4676c1c7415de1cd to your computer and use it in GitHub Desktop.
Testing union function of boost::geometry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <string> | |
| using namespace std; | |
| // boost::geometry | |
| #include <boost/geometry.hpp> | |
| #include <boost/geometry/algorithms/intersection.hpp> | |
| #include <boost/geometry/geometries/geometries.hpp> | |
| #include <boost/geometry/geometries/point_xy.hpp> | |
| typedef boost::geometry::model::d2::point_xy<double> Point; | |
| typedef boost::geometry::model::linestring<Point> Linestring; | |
| typedef boost::geometry::model::polygon<Point> Polygon; | |
| typedef boost::geometry::model::multi_polygon<Polygon> MultiPolygon; | |
| typedef boost::geometry::model::multi_linestring<Linestring> MultiLinestring; | |
| namespace bg = boost::geometry; | |
| void RandomPoly(Polygon &out) | |
| { | |
| double x = rand() % 10000 / 100.0; | |
| double y = rand() % 10000 / 100.0; | |
| int n = 3 + rand() % 50; | |
| out.clear(); | |
| for(int i=0; i<n; i++) | |
| { | |
| double ang = (double)i * (M_PI * 2.0) / (double)n; | |
| double dist = 10.0 + rand() % 10000 / 100.0; | |
| Point pt(x+dist*cos(ang), y-dist*sin(ang)); | |
| //cout << ang << "," << pt.x() << "," << pt.y() << "," << atan2(pt.y(), pt.x()) << endl; | |
| bg::append(out.outer(), pt); | |
| } | |
| bg::append(out.outer(), out.outer()[0]); | |
| } | |
| int main() | |
| { | |
| MultiPolygon mp1; | |
| mp1.resize(1); | |
| RandomPoly(mp1[0]); | |
| string reason; | |
| if(!bg::is_valid(mp1, reason)) | |
| { | |
| cout << reason << endl; | |
| exit(0); | |
| } | |
| int64_t count = 0; | |
| while(true) | |
| { | |
| MultiPolygon mp2; | |
| mp2.resize(1); | |
| RandomPoly(mp2[0]); | |
| if(!bg::is_valid(mp2, reason)) | |
| { | |
| cout << "Error creating polygon:" << reason << endl; | |
| bg::correct(mp2); | |
| if(!bg::is_valid(mp2, reason)) | |
| { | |
| cout << "Failed to correct" << endl; | |
| exit(0); | |
| } | |
| } | |
| MultiPolygon gTmp; | |
| bg::union_(mp1, mp2, gTmp); | |
| mp1 = gTmp; | |
| if(!bg::is_valid(mp1, reason)) | |
| { | |
| cout << "Error performing union:" << reason << endl; | |
| bg::correct(mp1); | |
| if(!bg::is_valid(mp1, reason)) | |
| { | |
| cout << "Failed to correct" << endl; | |
| exit(0); | |
| } | |
| } | |
| count ++; | |
| if(count%100==0) | |
| { | |
| //Reset current state | |
| mp1.resize(1); | |
| RandomPoly(mp1[0]); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment