#include #include #include #include using namespace boost::geometry; typedef model::d2::point_xy point; typedef model::polygon polygon; void print( const polygon &poly ) { bool first = true; std::cout << "POLYGON(( "; for( const point &p : poly.outer() ) { if( first ) { first = false; } else { std::cout << ", "; } std::cout << p.x() << " " << p.y(); } std::cout << " ))" << std::endl; } void print( const std::vector< polygon > &out ) { for( const polygon &poly : out ) { std::cout << "\t"; print( poly ); } std::cout << std::endl; } void testDiff( const polygon &a, const polygon &b ) { std::vector< polygon > out; boost::geometry::difference( a, b, out ); print( out ); } void testSymDiff( const polygon &a, const polygon &b ) { std::vector< polygon > out; boost::geometry::sym_difference( a, b, out ); print( out ); } void testIntersection( const polygon &a, const polygon &b ) { std::vector< polygon > out; boost::geometry::intersection( a, b, out ); print( out ); } void testUnion( const polygon &a, const polygon &b ) { std::vector< polygon > out; boost::geometry::union_( a, b, out ); print( out ); } void test( const polygon &a, const polygon &b ) { std::cout << "a: "; print( a ); std::cout << "b: "; print( b ); std::cout << std::endl; std::cout << "a - b:" << std::endl; testDiff( a, b ); std::cout << "b - a:" << std::endl; testDiff( b, a ); std::cout << "b ^ a:" << std::endl; testSymDiff( a, b ); std::cout << "b & a:" << std::endl; testIntersection( b, a ); std::cout << "b | a:" << std::endl; testUnion( b, a ); std::cout << "disjoint: " << boost::geometry::disjoint( a, b ) << std::endl; std::cout << "intersects: " << boost::geometry::intersects( a, b ) << std::endl; } void test( const std::string &aStr, const std::string &bStr ) { polygon a, b; boost::geometry::read_wkt( aStr, a); boost::geometry::correct( a ); boost::geometry::read_wkt( bStr, b ); boost::geometry::correct( b ); test( a, b ); } int main( ) { std::cout.precision( 15 ); std::cout << std::fixed; test( "POLYGON(( -2.559375047683716 -0.615625500679016, -2.559375047683716 0.384374797344208, 7.940625190734863 0.384374588727951, 7.940625190734863 -0.615625441074371 ))", "POLYGON(( 1.000000000000000 0.384374707937241, 1.000000000000000 0.000000000000000, 0.000000000000000 0.000000000000000, 0.000000000000000 0.384374737739563 ))" ); std::cout << " = = = = = " << std::endl << std::endl; test( "POLYGON(( 0.000000000000000 0.373437941074371, 1.000000000000000 0.373437792062759, 1.000000000000000 0.000000000000000, 0.000000000000000 0.000000000000000 ))", "POLYGON(( -2.592187881469727 -0.626561701297760, -2.592187643051147 0.373438000679016, 7.907812595367432 0.373437851667404, 7.907812595367432 -0.626561224460602 ))" ); std::cout << " = = = = = " << std::endl << std::endl; test( "POLYGON(( 1.000000000000000 2.531045913696289, 1.000000000000000 3.000000000000000, 2.000000000000000 3.000000000000000, 2.000000000000000 2.531045913696289 ))", "POLYGON(( 5.204249382019043 3.531043529510498, 5.204247951507568 2.531045675277710, -5.295750617980957 2.531046152114868, -5.295751094818115 3.531045913696289 ))" ); std::cout << " = = = = = " << std::endl << std::endl; std::cout << "This one is working as expected...." << std::endl; test( "POLYGON(( 1. 2., 1. 3., 2. 3., 2. 2. ))", "POLYGON(( 5. 3., 5. 2., -5. 2., -5. 3. ))" ); /* test( "POLYGON(( ))", "POLYGON(( ))" ); std::cout << " = = = = = " << std::endl << std::endl; */ return 0; }