1 | #include <vector>
|
---|
2 | #include <boost/polygon/polygon.hpp>
|
---|
3 |
|
---|
4 | #include <stdio.h>
|
---|
5 |
|
---|
6 | // Define some types used here from boost::polygon
|
---|
7 | namespace bpl = boost::polygon; // bpl = boost polygon library
|
---|
8 | using namespace bpl::operators; // +, -, =, ...
|
---|
9 |
|
---|
10 | // Definitions needed by boost::polygon
|
---|
11 | typedef int coordinate_type;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * KI_POLYGON defines a single polygon ( boost::polygon_data type.
|
---|
15 | * When holes are created in a KPolygon, they are
|
---|
16 | * linked to main outline by overlapping segments,
|
---|
17 | * so there is always one polygon and one list of corners
|
---|
18 | * coordinates are int
|
---|
19 | */
|
---|
20 | typedef bpl::polygon_data<int> KI_POLYGON;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * KI_POLYGON_SET defines a set of single KI_POLYGON.
|
---|
24 | * A KI_POLYGON_SET is used to store a set of polygons
|
---|
25 | * when performing operations between 2 polygons
|
---|
26 | * or 2 sets of polygons
|
---|
27 | * The result of operations like and, xor... between 2 polygons
|
---|
28 | * is always stored in a KI_POLYGON_SET, because these operations
|
---|
29 | * can create many polygons
|
---|
30 | */
|
---|
31 | typedef std::vector<KI_POLYGON> KI_POLYGON_SET;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * KI_POLY_POINT defines a point for boost::polygon.
|
---|
35 | * KI_POLY_POINT store x and y coordinates (int)
|
---|
36 | */
|
---|
37 | typedef bpl::point_data<int> KI_POLY_POINT;
|
---|
38 |
|
---|
39 |
|
---|
40 | // This polygon creates a 0 corner polygon
|
---|
41 | // After polys_data_holes is substracted.
|
---|
42 | int polys_data_main_bad_results[]=
|
---|
43 | {
|
---|
44 | 62611000, 88265000, 0,
|
---|
45 | 44450000, 88265000, 0,
|
---|
46 | 44450000, 75692000, 0,
|
---|
47 | 44391996, 75400395, 0,
|
---|
48 | 44226815, 75153185, 0,
|
---|
49 | 43979605, 74988004, 0,
|
---|
50 | 43688000, 74930000, 0,
|
---|
51 | 38683924, 74930000, 0,
|
---|
52 | 38683924, 74360520, 0,
|
---|
53 | 38655146, 73830802, 0,
|
---|
54 | 38507798, 73475072, 0,
|
---|
55 | 38272186, 73413419, 0,
|
---|
56 | 38092581, 73593024, 0,
|
---|
57 | 38092581, 73233814, 0,
|
---|
58 | 38030928, 72998202, 0,
|
---|
59 | 37530520, 72822076, 0,
|
---|
60 | 37000802, 72850854, 0,
|
---|
61 | 36645072, 72998202, 0,
|
---|
62 | 36583419, 73233814, 0,
|
---|
63 | 37338000, 73988395, 0,
|
---|
64 | 38092581, 73233814, 0,
|
---|
65 | 38092581, 73593024, 0,
|
---|
66 | 37517605, 74168000, 0,
|
---|
67 | 38272186, 74922581, 0,
|
---|
68 | 38507798, 74860928, 0,
|
---|
69 | 38683924, 74360520, 0,
|
---|
70 | 38683924, 74930000, 0,
|
---|
71 | 37920395, 74930000, 0,
|
---|
72 | 37338000, 74347605, 0,
|
---|
73 | 37158395, 74527209, 0,
|
---|
74 | 37158395, 74168000, 0,
|
---|
75 | 36403814, 73413419, 0,
|
---|
76 | 36168202, 73475072, 0,
|
---|
77 | 35992076, 73975480, 0,
|
---|
78 | 36020854, 74505198, 0,
|
---|
79 | 36168202, 74860928, 0,
|
---|
80 | 36403814, 74922581, 0,
|
---|
81 | 37158395, 74168000, 0,
|
---|
82 | 37158395, 74527209, 0,
|
---|
83 | 36755604, 74930000, 0,
|
---|
84 | 36068000, 74930000, 0,
|
---|
85 | 35776395, 74988004, 0,
|
---|
86 | 35529185, 75153185, 0,
|
---|
87 | 35364004, 75400395, 0,
|
---|
88 | 35306000, 75692000, 0,
|
---|
89 | 35364004, 75983605, 0,
|
---|
90 | 35529185, 76230815, 0,
|
---|
91 | 35776395, 76395996, 0,
|
---|
92 | 36068000, 76454000, 0,
|
---|
93 | 43688000, 76454000, 0,
|
---|
94 | 43979605, 76395996, 0,
|
---|
95 | 44226815, 76230815, 0,
|
---|
96 | 44391996, 75983605, 0,
|
---|
97 | 44450000, 75692000, 0,
|
---|
98 | 44450000, 88265000, 0,
|
---|
99 | 27305000, 88265000, 0,
|
---|
100 | 27305000, 67691000, 0,
|
---|
101 | 62611000, 67691000, 0,
|
---|
102 | 62611000, 88265000, 2,
|
---|
103 | };
|
---|
104 |
|
---|
105 | // This polygon creates a memory error (crash
|
---|
106 | // when polys_data_holes is substracted.
|
---|
107 | int polys_data_main_crash[]=
|
---|
108 | {
|
---|
109 | 62611000, 88265000, 0,
|
---|
110 | 53923924, 88265000, 0,
|
---|
111 | 53923924, 74360520, 0,
|
---|
112 | 53895146, 73830802, 0,
|
---|
113 | 53747798, 73475072, 0,
|
---|
114 | 53512186, 73413419, 0,
|
---|
115 | 53332581, 73593024, 0,
|
---|
116 | 53332581, 73233814, 0,
|
---|
117 | 53270928, 72998202, 0,
|
---|
118 | 52770520, 72822076, 0,
|
---|
119 | 52240802, 72850854, 0,
|
---|
120 | 51885072, 72998202, 0,
|
---|
121 | 51823419, 73233814, 0,
|
---|
122 | 52578000, 73988395, 0,
|
---|
123 | 53332581, 73233814, 0,
|
---|
124 | 53332581, 73593024, 0,
|
---|
125 | 52757605, 74168000, 0,
|
---|
126 | 53512186, 74922581, 0,
|
---|
127 | 53747798, 74860928, 0,
|
---|
128 | 53923924, 74360520, 0,
|
---|
129 | 53923924, 88265000, 0,
|
---|
130 | 53332581, 88265000, 0,
|
---|
131 | 53332581, 75102186, 0,
|
---|
132 | 52578000, 74347605, 0,
|
---|
133 | 52398395, 74527210, 0,
|
---|
134 | 52398395, 74168000, 0,
|
---|
135 | 51643814, 73413419, 0,
|
---|
136 | 51408202, 73475072, 0,
|
---|
137 | 51232076, 73975480, 0,
|
---|
138 | 51260854, 74505198, 0,
|
---|
139 | 51408202, 74860928, 0,
|
---|
140 | 51643814, 74922581, 0,
|
---|
141 | 52398395, 74168000, 0,
|
---|
142 | 52398395, 74527210, 0,
|
---|
143 | 51823419, 75102186, 0,
|
---|
144 | 51885072, 75337798, 0,
|
---|
145 | 52385480, 75513924, 0,
|
---|
146 | 52915198, 75485146, 0,
|
---|
147 | 53270928, 75337798, 0,
|
---|
148 | 53332581, 75102186, 0,
|
---|
149 | 53332581, 88265000, 0,
|
---|
150 | 44450000, 88265000, 0,
|
---|
151 | 44450000, 75692000, 0,
|
---|
152 | 44391996, 75400395, 0,
|
---|
153 | 44226815, 75153185, 0,
|
---|
154 | 43979605, 74988004, 0,
|
---|
155 | 43688000, 74930000, 0,
|
---|
156 | 38683924, 74930000, 0,
|
---|
157 | 38683924, 74360520, 0,
|
---|
158 | 38655146, 73830802, 0,
|
---|
159 | 38507798, 73475072, 0,
|
---|
160 | 38272186, 73413419, 0,
|
---|
161 | 38092581, 73593024, 0,
|
---|
162 | 38092581, 73233814, 0,
|
---|
163 | 38030928, 72998202, 0,
|
---|
164 | 37530520, 72822076, 0,
|
---|
165 | 37000802, 72850854, 0,
|
---|
166 | 36645072, 72998202, 0,
|
---|
167 | 36583419, 73233814, 0,
|
---|
168 | 37338000, 73988395, 0,
|
---|
169 | 38092581, 73233814, 0,
|
---|
170 | 38092581, 73593024, 0,
|
---|
171 | 37517605, 74168000, 0,
|
---|
172 | 38272186, 74922581, 0,
|
---|
173 | 38507798, 74860928, 0,
|
---|
174 | 38683924, 74360520, 0,
|
---|
175 | 38683924, 74930000, 0,
|
---|
176 | 37920395, 74930000, 0,
|
---|
177 | 37338000, 74347605, 0,
|
---|
178 | 37158395, 74527209, 0,
|
---|
179 | 37158395, 74168000, 0,
|
---|
180 | 36403814, 73413419, 0,
|
---|
181 | 36168202, 73475072, 0,
|
---|
182 | 35992076, 73975480, 0,
|
---|
183 | 36020854, 74505198, 0,
|
---|
184 | 36168202, 74860928, 0,
|
---|
185 | 36403814, 74922581, 0,
|
---|
186 | 37158395, 74168000, 0,
|
---|
187 | 37158395, 74527209, 0,
|
---|
188 | 36755604, 74930000, 0,
|
---|
189 | 36068000, 74930000, 0,
|
---|
190 | 35776395, 74988004, 0,
|
---|
191 | 35529185, 75153185, 0,
|
---|
192 | 35364004, 75400395, 0,
|
---|
193 | 35306000, 75692000, 0,
|
---|
194 | 35364004, 75983605, 0,
|
---|
195 | 35529185, 76230815, 0,
|
---|
196 | 35776395, 76395996, 0,
|
---|
197 | 36068000, 76454000, 0,
|
---|
198 | 43688000, 76454000, 0,
|
---|
199 | 43979605, 76395996, 0,
|
---|
200 | 44226815, 76230815, 0,
|
---|
201 | 44391996, 75983605, 0,
|
---|
202 | 44450000, 75692000, 0,
|
---|
203 | 44450000, 88265000, 0,
|
---|
204 | 27305000, 88265000, 0,
|
---|
205 | 27305000, 67691000, 0,
|
---|
206 | 62611000, 67691000, 0,
|
---|
207 | 62611000, 88265000, 2,
|
---|
208 | };
|
---|
209 |
|
---|
210 | // polyset polygons count 2
|
---|
211 | int polys_data_holes[]=
|
---|
212 | {
|
---|
213 | // polygon 0
|
---|
214 | 38118035, 75307245, 0,
|
---|
215 | 38477245, 74948035, 0,
|
---|
216 | 37607408, 74078197, 0,
|
---|
217 | 37248197, 74437408, 1,
|
---|
218 |
|
---|
219 | // polygon 1
|
---|
220 | 36198755, 74948035, 0,
|
---|
221 | 36557965, 75307245, 0,
|
---|
222 | 37427803, 74437408, 0,
|
---|
223 | 37068592, 74078197, 2,
|
---|
224 | };
|
---|
225 |
|
---|
226 | void CopyPolygons( KI_POLYGON_SET& aKiPolyList, int polyset[] )
|
---|
227 | {
|
---|
228 | std::vector<KI_POLY_POINT> cornerslist;
|
---|
229 |
|
---|
230 | KI_POLYGON poly;
|
---|
231 | for( int ii = 0; ; ii++ )
|
---|
232 | {
|
---|
233 | int jj = ii * 3;
|
---|
234 | cornerslist.push_back( KI_POLY_POINT( polyset[jj], polyset[jj+1] ) );
|
---|
235 |
|
---|
236 | int flg = polyset[jj+2];
|
---|
237 | if( flg ) // end of the current polygon
|
---|
238 | {
|
---|
239 | bpl::set_points( poly, cornerslist.begin(), cornerslist.end() );
|
---|
240 | aKiPolyList.push_back( poly );
|
---|
241 | cornerslist.clear();
|
---|
242 | }
|
---|
243 | if( flg > 1) // end of list
|
---|
244 | break;
|
---|
245 | }
|
---|
246 |
|
---|
247 | }
|
---|
248 |
|
---|
249 | // Debug function: dump a KI_POLYGON_SET to stdout
|
---|
250 | // in a C style form
|
---|
251 | // each corner is output under <xcoord>, <ycoord, <flg>
|
---|
252 | // flg = 0 for all corner but the last of the current poly.
|
---|
253 | // flg = 1 for the last of the current poly.
|
---|
254 | // flg = 2 for the very last corner.
|
---|
255 | void dumpPolySet( const KI_POLYGON_SET& polyset, const char * polyname )
|
---|
256 | {
|
---|
257 | printf("// polyset polygons count %d\n", polyset.size());
|
---|
258 | printf("int polyset_%s[]=\n{\n", polyname);
|
---|
259 | int count = 0;
|
---|
260 | for( unsigned kk = 0; kk < polyset.size(); kk++ )
|
---|
261 | {
|
---|
262 | const KI_POLYGON& poly = polyset[kk];
|
---|
263 | if( polyset.size() > 1 )
|
---|
264 | printf("// polygon %d\n", kk);
|
---|
265 | for( unsigned ij = 0; ij < poly.size(); ij++ )
|
---|
266 | {
|
---|
267 | int flg = 0;
|
---|
268 | if( ij == poly.size() -1 )
|
---|
269 | {
|
---|
270 | flg = 1;
|
---|
271 | if( kk == polyset.size() -1 )
|
---|
272 | flg = 2;
|
---|
273 | }
|
---|
274 | printf( " %d, %d, %d,\n", (poly.begin() + ij)->x(),
|
---|
275 | (poly.begin() + ij)->y(), flg );
|
---|
276 | count++;
|
---|
277 | }
|
---|
278 | printf("\n");
|
---|
279 | }
|
---|
280 | printf("}; // %d points\n", count);
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | int main()
|
---|
285 | {
|
---|
286 | KI_POLYGON_SET polys_main;
|
---|
287 | // Here, use polys_data_main_bad_results or polys_data_main_crash
|
---|
288 | CopyPolygons( polys_main, polys_data_main_crash );
|
---|
289 |
|
---|
290 | KI_POLYGON_SET polys_holes;
|
---|
291 | CopyPolygons( polys_holes, polys_data_holes );
|
---|
292 |
|
---|
293 | // When this line is not commented, susbtract works file
|
---|
294 | // becuse the shape of polys_holes is slightly modified
|
---|
295 | // KI_POLYGON_SET dummy; polys_holes += dummy;
|
---|
296 |
|
---|
297 | polys_main -= polys_holes;
|
---|
298 |
|
---|
299 | // Print result
|
---|
300 | dumpPolySet( polys_main, "result" );
|
---|
301 |
|
---|
302 | return 0;
|
---|
303 | }
|
---|