| 1 |
|
|---|
| 2 | #include <boost/geometry.hpp>
|
|---|
| 3 | #include <boost/geometry/geometries/point_xy.hpp>
|
|---|
| 4 | #include <boost/geometry/geometries/polygon.hpp>
|
|---|
| 5 | #include <iostream>
|
|---|
| 6 |
|
|---|
| 7 | using namespace boost::geometry;
|
|---|
| 8 |
|
|---|
| 9 | typedef model::d2::point_xy<float> point;
|
|---|
| 10 | typedef model::polygon<point, true, false> polygon;
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 | void print( const polygon &poly )
|
|---|
| 14 | {
|
|---|
| 15 | bool first = true;
|
|---|
| 16 | std::cout << "POLYGON(( ";
|
|---|
| 17 | for( const point &p : poly.outer() ) {
|
|---|
| 18 | if( first ) {
|
|---|
| 19 | first = false;
|
|---|
| 20 | }
|
|---|
| 21 | else {
|
|---|
| 22 | std::cout << ", ";
|
|---|
| 23 | }
|
|---|
| 24 | std::cout << p.x() << " " << p.y();
|
|---|
| 25 | }
|
|---|
| 26 | std::cout << " ))" << std::endl;
|
|---|
| 27 | }
|
|---|
| 28 | void print( const std::vector< polygon > &out )
|
|---|
| 29 | {
|
|---|
| 30 | for( const polygon &poly : out ) {
|
|---|
| 31 | std::cout << "\t";
|
|---|
| 32 | print( poly );
|
|---|
| 33 | }
|
|---|
| 34 | std::cout << std::endl;
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | void testDiff( const polygon &a, const polygon &b )
|
|---|
| 38 | {
|
|---|
| 39 | std::vector< polygon > out;
|
|---|
| 40 | boost::geometry::difference( a, b, out );
|
|---|
| 41 |
|
|---|
| 42 | print( out );
|
|---|
| 43 | }
|
|---|
| 44 | void testSymDiff( const polygon &a, const polygon &b )
|
|---|
| 45 | {
|
|---|
| 46 | std::vector< polygon > out;
|
|---|
| 47 | boost::geometry::sym_difference( a, b, out );
|
|---|
| 48 | print( out );
|
|---|
| 49 | }
|
|---|
| 50 | void testIntersection( const polygon &a, const polygon &b )
|
|---|
| 51 | {
|
|---|
| 52 | std::vector< polygon > out;
|
|---|
| 53 | boost::geometry::intersection( a, b, out );
|
|---|
| 54 | print( out );
|
|---|
| 55 | }
|
|---|
| 56 | void testUnion( const polygon &a, const polygon &b )
|
|---|
| 57 | {
|
|---|
| 58 | std::vector< polygon > out;
|
|---|
| 59 | boost::geometry::union_( a, b, out );
|
|---|
| 60 | print( out );
|
|---|
| 61 | }
|
|---|
| 62 | void test( const polygon &a, const polygon &b )
|
|---|
| 63 | {
|
|---|
| 64 | std::cout << "a: ";
|
|---|
| 65 | print( a );
|
|---|
| 66 | std::cout << "b: ";
|
|---|
| 67 | print( b );
|
|---|
| 68 | std::cout << std::endl;
|
|---|
| 69 |
|
|---|
| 70 | std::cout << "a - b:" << std::endl;
|
|---|
| 71 | testDiff( a, b );
|
|---|
| 72 | std::cout << "b - a:" << std::endl;
|
|---|
| 73 | testDiff( b, a );
|
|---|
| 74 | std::cout << "b ^ a:" << std::endl;
|
|---|
| 75 | testSymDiff( a, b );
|
|---|
| 76 | std::cout << "b & a:" << std::endl;
|
|---|
| 77 | testIntersection( b, a );
|
|---|
| 78 | std::cout << "b | a:" << std::endl;
|
|---|
| 79 | testUnion( b, a );
|
|---|
| 80 | std::cout << "disjoint: " << boost::geometry::disjoint( a, b ) << std::endl;
|
|---|
| 81 | std::cout << "intersects: " << boost::geometry::intersects( a, b ) << std::endl;
|
|---|
| 82 | }
|
|---|
| 83 | void test( const std::string &aStr, const std::string &bStr )
|
|---|
| 84 | {
|
|---|
| 85 | polygon a, b;
|
|---|
| 86 |
|
|---|
| 87 | boost::geometry::read_wkt( aStr, a);
|
|---|
| 88 | boost::geometry::correct( a );
|
|---|
| 89 | boost::geometry::read_wkt( bStr, b );
|
|---|
| 90 | boost::geometry::correct( b );
|
|---|
| 91 |
|
|---|
| 92 | test( a, b );
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | int main( )
|
|---|
| 96 | {
|
|---|
| 97 | std::cout.precision( 15 );
|
|---|
| 98 | std::cout << std::fixed;
|
|---|
| 99 |
|
|---|
| 100 | test( "POLYGON(( -2.559375047683716 -0.615625500679016, -2.559375047683716 0.384374797344208, 7.940625190734863 0.384374588727951, 7.940625190734863 -0.615625441074371 ))",
|
|---|
| 101 | "POLYGON(( 1.000000000000000 0.384374707937241, 1.000000000000000 0.000000000000000, 0.000000000000000 0.000000000000000, 0.000000000000000 0.384374737739563 ))" );
|
|---|
| 102 | std::cout << " = = = = = " << std::endl << std::endl;
|
|---|
| 103 |
|
|---|
| 104 | test( "POLYGON(( 0.000000000000000 0.373437941074371, 1.000000000000000 0.373437792062759, 1.000000000000000 0.000000000000000, 0.000000000000000 0.000000000000000 ))",
|
|---|
| 105 | "POLYGON(( -2.592187881469727 -0.626561701297760, -2.592187643051147 0.373438000679016, 7.907812595367432 0.373437851667404, 7.907812595367432 -0.626561224460602 ))" );
|
|---|
| 106 | std::cout << " = = = = = " << std::endl << std::endl;
|
|---|
| 107 |
|
|---|
| 108 | test( "POLYGON(( 1.000000000000000 2.531045913696289, 1.000000000000000 3.000000000000000, 2.000000000000000 3.000000000000000, 2.000000000000000 2.531045913696289 ))",
|
|---|
| 109 | "POLYGON(( 5.204249382019043 3.531043529510498, 5.204247951507568 2.531045675277710, -5.295750617980957 2.531046152114868, -5.295751094818115 3.531045913696289 ))" );
|
|---|
| 110 | std::cout << " = = = = = " << std::endl << std::endl;
|
|---|
| 111 |
|
|---|
| 112 | std::cout << "This one is working as expected...." << std::endl;
|
|---|
| 113 | test( "POLYGON(( 1. 2., 1. 3., 2. 3., 2. 2. ))",
|
|---|
| 114 | "POLYGON(( 5. 3., 5. 2., -5. 2., -5. 3. ))" );
|
|---|
| 115 |
|
|---|
| 116 | /*
|
|---|
| 117 |
|
|---|
| 118 | test( "POLYGON(( ))",
|
|---|
| 119 | "POLYGON(( ))" );
|
|---|
| 120 | std::cout << " = = = = = " << std::endl << std::endl;
|
|---|
| 121 | */
|
|---|
| 122 | return 0;
|
|---|
| 123 | }
|
|---|