/* * This sample calculates the diff of 2 polygons. * It works fine when compiled with g++ 4.4 and 4.6 with optimization -O2 * When compiled with g++ 4.7 : * with optimization -O2 the result is false (void polygon) * with optimization -O1 or -00 the result is OK * * Note also the same issue exists with the sum of these 2 polygons. */ #include #include #include // Define some types used here from boost::polygon namespace bpl = boost::polygon; // bpl = boost polygon library using namespace bpl::operators; // +, -, =, ... // Definitions needed by boost::polygon typedef int coordinate_type; /** * KI_POLYGON defines a single polygon ( boost::polygon_data type. */ typedef bpl::polygon_data KI_POLYGON; /** * KI_POLYGON_SET defines a set of single KI_POLYGON. * A KI_POLYGON_SET is used to store a set of polygons * when performing operations between 2 polygons * or 2 sets of polygons * The result of operations like and, xor... between 2 polygons * is always stored in a KI_POLYGON_SET, because these operations * can create many polygons */ typedef std::vector KI_POLYGON_SET; /** * KI_POLY_POINT defines a point for boost::polygon. * KI_POLY_POINT store x and y coordinates (int) */ typedef bpl::point_data KI_POLY_POINT; int polyset_main[]= { -10, -10, -10, 10, 10, 10, 10, -10 }; int polyset_main_corner_count = 4; // polyset polygons count 2 int polyset_holes[]= { -5, -5, -5, 5, 5, 5, 5, -5 }; int polyset_holes_corner_count = 4; void CopyPolygons( KI_POLYGON_SET& aKiPolyList, int polyset[], int corners_count ) { std::vector cornerslist; KI_POLYGON poly; for( int ii = 0; ii < corners_count; ii++ ) { int jj = ii * 2; cornerslist.push_back( KI_POLY_POINT( polyset[jj], polyset[jj+1] ) ); } bpl::set_points( poly, cornerslist.begin(), cornerslist.end() ); aKiPolyList.push_back( poly ); } // Debug function: dump a KI_POLYGON_SET to stdout // each corner is output under , void dumpPolySet( const KI_POLYGON_SET& polyset ) { printf("// polyset polygons count %d\n", polyset.size()); for( unsigned kk = 0; kk < polyset.size(); kk++ ) { const KI_POLYGON& poly = polyset[kk]; printf("**** polygon %d, corners count %d\n", kk, poly.size() ); for( unsigned ij = 0; ij < poly.size(); ij++ ) { printf( " %d, %d\n", (poly.begin() + ij)->x(), (poly.begin() + ij)->y() ); } printf("\n"); } } int main() { KI_POLYGON_SET polys_main; CopyPolygons( polys_main, polyset_main, polyset_main_corner_count ); KI_POLYGON_SET polys_holes; CopyPolygons( polys_holes, polyset_holes, polyset_holes_corner_count ); polys_main -= polys_holes; dumpPolySet( polys_main ); return 0; }