#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. * When holes are created in a KPolygon, they are * linked to main outline by overlapping segments, * so there is always one polygon and one list of corners * coordinates are int */ 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; // This polygon creates a 0 corner polygon // After polys_data_holes is substracted. int polys_data_main_bad_results[]= { 62611000, 88265000, 0, 44450000, 88265000, 0, 44450000, 75692000, 0, 44391996, 75400395, 0, 44226815, 75153185, 0, 43979605, 74988004, 0, 43688000, 74930000, 0, 38683924, 74930000, 0, 38683924, 74360520, 0, 38655146, 73830802, 0, 38507798, 73475072, 0, 38272186, 73413419, 0, 38092581, 73593024, 0, 38092581, 73233814, 0, 38030928, 72998202, 0, 37530520, 72822076, 0, 37000802, 72850854, 0, 36645072, 72998202, 0, 36583419, 73233814, 0, 37338000, 73988395, 0, 38092581, 73233814, 0, 38092581, 73593024, 0, 37517605, 74168000, 0, 38272186, 74922581, 0, 38507798, 74860928, 0, 38683924, 74360520, 0, 38683924, 74930000, 0, 37920395, 74930000, 0, 37338000, 74347605, 0, 37158395, 74527209, 0, 37158395, 74168000, 0, 36403814, 73413419, 0, 36168202, 73475072, 0, 35992076, 73975480, 0, 36020854, 74505198, 0, 36168202, 74860928, 0, 36403814, 74922581, 0, 37158395, 74168000, 0, 37158395, 74527209, 0, 36755604, 74930000, 0, 36068000, 74930000, 0, 35776395, 74988004, 0, 35529185, 75153185, 0, 35364004, 75400395, 0, 35306000, 75692000, 0, 35364004, 75983605, 0, 35529185, 76230815, 0, 35776395, 76395996, 0, 36068000, 76454000, 0, 43688000, 76454000, 0, 43979605, 76395996, 0, 44226815, 76230815, 0, 44391996, 75983605, 0, 44450000, 75692000, 0, 44450000, 88265000, 0, 27305000, 88265000, 0, 27305000, 67691000, 0, 62611000, 67691000, 0, 62611000, 88265000, 2, }; // This polygon creates a memory error (crash // when polys_data_holes is substracted. int polys_data_main_crash[]= { 62611000, 88265000, 0, 53923924, 88265000, 0, 53923924, 74360520, 0, 53895146, 73830802, 0, 53747798, 73475072, 0, 53512186, 73413419, 0, 53332581, 73593024, 0, 53332581, 73233814, 0, 53270928, 72998202, 0, 52770520, 72822076, 0, 52240802, 72850854, 0, 51885072, 72998202, 0, 51823419, 73233814, 0, 52578000, 73988395, 0, 53332581, 73233814, 0, 53332581, 73593024, 0, 52757605, 74168000, 0, 53512186, 74922581, 0, 53747798, 74860928, 0, 53923924, 74360520, 0, 53923924, 88265000, 0, 53332581, 88265000, 0, 53332581, 75102186, 0, 52578000, 74347605, 0, 52398395, 74527210, 0, 52398395, 74168000, 0, 51643814, 73413419, 0, 51408202, 73475072, 0, 51232076, 73975480, 0, 51260854, 74505198, 0, 51408202, 74860928, 0, 51643814, 74922581, 0, 52398395, 74168000, 0, 52398395, 74527210, 0, 51823419, 75102186, 0, 51885072, 75337798, 0, 52385480, 75513924, 0, 52915198, 75485146, 0, 53270928, 75337798, 0, 53332581, 75102186, 0, 53332581, 88265000, 0, 44450000, 88265000, 0, 44450000, 75692000, 0, 44391996, 75400395, 0, 44226815, 75153185, 0, 43979605, 74988004, 0, 43688000, 74930000, 0, 38683924, 74930000, 0, 38683924, 74360520, 0, 38655146, 73830802, 0, 38507798, 73475072, 0, 38272186, 73413419, 0, 38092581, 73593024, 0, 38092581, 73233814, 0, 38030928, 72998202, 0, 37530520, 72822076, 0, 37000802, 72850854, 0, 36645072, 72998202, 0, 36583419, 73233814, 0, 37338000, 73988395, 0, 38092581, 73233814, 0, 38092581, 73593024, 0, 37517605, 74168000, 0, 38272186, 74922581, 0, 38507798, 74860928, 0, 38683924, 74360520, 0, 38683924, 74930000, 0, 37920395, 74930000, 0, 37338000, 74347605, 0, 37158395, 74527209, 0, 37158395, 74168000, 0, 36403814, 73413419, 0, 36168202, 73475072, 0, 35992076, 73975480, 0, 36020854, 74505198, 0, 36168202, 74860928, 0, 36403814, 74922581, 0, 37158395, 74168000, 0, 37158395, 74527209, 0, 36755604, 74930000, 0, 36068000, 74930000, 0, 35776395, 74988004, 0, 35529185, 75153185, 0, 35364004, 75400395, 0, 35306000, 75692000, 0, 35364004, 75983605, 0, 35529185, 76230815, 0, 35776395, 76395996, 0, 36068000, 76454000, 0, 43688000, 76454000, 0, 43979605, 76395996, 0, 44226815, 76230815, 0, 44391996, 75983605, 0, 44450000, 75692000, 0, 44450000, 88265000, 0, 27305000, 88265000, 0, 27305000, 67691000, 0, 62611000, 67691000, 0, 62611000, 88265000, 2, }; // polyset polygons count 2 int polys_data_holes[]= { // polygon 0 38118035, 75307245, 0, 38477245, 74948035, 0, 37607408, 74078197, 0, 37248197, 74437408, 1, // polygon 1 36198755, 74948035, 0, 36557965, 75307245, 0, 37427803, 74437408, 0, 37068592, 74078197, 2, }; void CopyPolygons( KI_POLYGON_SET& aKiPolyList, int polyset[] ) { std::vector cornerslist; KI_POLYGON poly; for( int ii = 0; ; ii++ ) { int jj = ii * 3; cornerslist.push_back( KI_POLY_POINT( polyset[jj], polyset[jj+1] ) ); int flg = polyset[jj+2]; if( flg ) // end of the current polygon { bpl::set_points( poly, cornerslist.begin(), cornerslist.end() ); aKiPolyList.push_back( poly ); cornerslist.clear(); } if( flg > 1) // end of list break; } } // Debug function: dump a KI_POLYGON_SET to stdout // in a C style form // each corner is output under , // flg = 0 for all corner but the last of the current poly. // flg = 1 for the last of the current poly. // flg = 2 for the very last corner. void dumpPolySet( const KI_POLYGON_SET& polyset, const char * polyname ) { printf("// polyset polygons count %d\n", polyset.size()); printf("int polyset_%s[]=\n{\n", polyname); int count = 0; for( unsigned kk = 0; kk < polyset.size(); kk++ ) { const KI_POLYGON& poly = polyset[kk]; if( polyset.size() > 1 ) printf("// polygon %d\n", kk); for( unsigned ij = 0; ij < poly.size(); ij++ ) { int flg = 0; if( ij == poly.size() -1 ) { flg = 1; if( kk == polyset.size() -1 ) flg = 2; } printf( " %d, %d, %d,\n", (poly.begin() + ij)->x(), (poly.begin() + ij)->y(), flg ); count++; } printf("\n"); } printf("}; // %d points\n", count); } int main() { KI_POLYGON_SET polys_main; // Here, use polys_data_main_bad_results or polys_data_main_crash CopyPolygons( polys_main, polys_data_main_crash ); KI_POLYGON_SET polys_holes; CopyPolygons( polys_holes, polys_data_holes ); // When this line is not commented, susbtract works file // becuse the shape of polys_holes is slightly modified // KI_POLYGON_SET dummy; polys_holes += dummy; polys_main -= polys_holes; // Print result dumpPolySet( polys_main, "result" ); return 0; }