| 1 | /*
|
|---|
| 2 | * This sample calculates the diff of 2 polygons.
|
|---|
| 3 | * It works fine when compiled with g++ 4.4 and 4.6 with optimization -O2
|
|---|
| 4 | * When compiled with g++ 4.7 :
|
|---|
| 5 | * with optimization -O2 the result is false (void polygon)
|
|---|
| 6 | * with optimization -O1 or -00 the result is OK
|
|---|
| 7 | *
|
|---|
| 8 | * Note also the same issue exists with the sum of these 2 polygons.
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | #include <vector>
|
|---|
| 12 | #include <boost/polygon/polygon.hpp>
|
|---|
| 13 |
|
|---|
| 14 | #include <stdio.h>
|
|---|
| 15 |
|
|---|
| 16 | // Define some types used here from boost::polygon
|
|---|
| 17 | namespace bpl = boost::polygon; // bpl = boost polygon library
|
|---|
| 18 | using namespace bpl::operators; // +, -, =, ...
|
|---|
| 19 |
|
|---|
| 20 | // Definitions needed by boost::polygon
|
|---|
| 21 | typedef int coordinate_type;
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * KI_POLYGON defines a single polygon ( boost::polygon_data type.
|
|---|
| 25 | */
|
|---|
| 26 | typedef bpl::polygon_data<int> KI_POLYGON;
|
|---|
| 27 |
|
|---|
| 28 | /**
|
|---|
| 29 | * KI_POLYGON_SET defines a set of single KI_POLYGON.
|
|---|
| 30 | * A KI_POLYGON_SET is used to store a set of polygons
|
|---|
| 31 | * when performing operations between 2 polygons
|
|---|
| 32 | * or 2 sets of polygons
|
|---|
| 33 | * The result of operations like and, xor... between 2 polygons
|
|---|
| 34 | * is always stored in a KI_POLYGON_SET, because these operations
|
|---|
| 35 | * can create many polygons
|
|---|
| 36 | */
|
|---|
| 37 | typedef std::vector<KI_POLYGON> KI_POLYGON_SET;
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * KI_POLY_POINT defines a point for boost::polygon.
|
|---|
| 41 | * KI_POLY_POINT store x and y coordinates (int)
|
|---|
| 42 | */
|
|---|
| 43 | typedef bpl::point_data<int> KI_POLY_POINT;
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | int polyset_main[]=
|
|---|
| 47 | {
|
|---|
| 48 | -10, -10,
|
|---|
| 49 | -10, 10,
|
|---|
| 50 | 10, 10,
|
|---|
| 51 | 10, -10
|
|---|
| 52 | };
|
|---|
| 53 | int polyset_main_corner_count = 4;
|
|---|
| 54 |
|
|---|
| 55 | // polyset polygons count 2
|
|---|
| 56 | int polyset_holes[]=
|
|---|
| 57 | {
|
|---|
| 58 | -5, -5,
|
|---|
| 59 | -5, 5,
|
|---|
| 60 | 5, 5,
|
|---|
| 61 | 5, -5
|
|---|
| 62 | };
|
|---|
| 63 | int polyset_holes_corner_count = 4;
|
|---|
| 64 |
|
|---|
| 65 | void CopyPolygons( KI_POLYGON_SET& aKiPolyList, int polyset[], int corners_count )
|
|---|
| 66 | {
|
|---|
| 67 | std::vector<KI_POLY_POINT> cornerslist;
|
|---|
| 68 |
|
|---|
| 69 | KI_POLYGON poly;
|
|---|
| 70 | for( int ii = 0; ii < corners_count; ii++ )
|
|---|
| 71 | {
|
|---|
| 72 | int jj = ii * 2;
|
|---|
| 73 | cornerslist.push_back( KI_POLY_POINT( polyset[jj], polyset[jj+1] ) );
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | bpl::set_points( poly, cornerslist.begin(), cornerslist.end() );
|
|---|
| 77 | aKiPolyList.push_back( poly );
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | // Debug function: dump a KI_POLYGON_SET to stdout
|
|---|
| 81 | // each corner is output under <xcoord>, <ycoord>
|
|---|
| 82 | void dumpPolySet( const KI_POLYGON_SET& polyset )
|
|---|
| 83 | {
|
|---|
| 84 | printf("// polyset polygons count %d\n", polyset.size());
|
|---|
| 85 | for( unsigned kk = 0; kk < polyset.size(); kk++ )
|
|---|
| 86 | {
|
|---|
| 87 | const KI_POLYGON& poly = polyset[kk];
|
|---|
| 88 | printf("**** polygon %d, corners count %d\n", kk, poly.size() );
|
|---|
| 89 | for( unsigned ij = 0; ij < poly.size(); ij++ )
|
|---|
| 90 | {
|
|---|
| 91 | printf( " %d, %d\n", (poly.begin() + ij)->x(),
|
|---|
| 92 | (poly.begin() + ij)->y() );
|
|---|
| 93 | }
|
|---|
| 94 | printf("\n");
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | int main()
|
|---|
| 100 | {
|
|---|
| 101 | KI_POLYGON_SET polys_main;
|
|---|
| 102 | CopyPolygons( polys_main, polyset_main, polyset_main_corner_count );
|
|---|
| 103 |
|
|---|
| 104 | KI_POLYGON_SET polys_holes;
|
|---|
| 105 | CopyPolygons( polys_holes, polyset_holes, polyset_holes_corner_count );
|
|---|
| 106 |
|
|---|
| 107 | polys_main -= polys_holes;
|
|---|
| 108 |
|
|---|
| 109 | dumpPolySet( polys_main );
|
|---|
| 110 |
|
|---|
| 111 | return 0;
|
|---|
| 112 | }
|
|---|