1 |
/**
|
2 |
* ZANavi, Zoff Android Navigation system.
|
3 |
* Copyright (C) 2011-2012 Zoff <zoff@zoff.cc>
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
|
20 |
/**
|
21 |
* Navit, a modular navigation system.
|
22 |
* Copyright (C) 2005-2011 Navit Team
|
23 |
*
|
24 |
* This program is free software; you can redistribute it and/or
|
25 |
* modify it under the terms of the GNU General Public License
|
26 |
* version 2 as published by the Free Software Foundation.
|
27 |
*
|
28 |
* This program is distributed in the hope that it will be useful,
|
29 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
30 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
31 |
* GNU General Public License for more details.
|
32 |
*
|
33 |
* You should have received a copy of the GNU General Public License
|
34 |
* along with this program; if not, write to the
|
35 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
36 |
* Boston, MA 02110-1301, USA.
|
37 |
*/
|
38 |
#include "maptool.h"
|
39 |
#include "debug.h"
|
40 |
|
41 |
static int distance_from_ll(struct coord *c, struct rect *bbox)
|
42 |
{
|
43 |
int dist = 0;
|
44 |
if (c->x == bbox->l.x)
|
45 |
return dist + c->y - bbox->l.y;
|
46 |
dist += bbox->h.y - bbox->l.y;
|
47 |
if (c->y == bbox->h.y)
|
48 |
return dist + c->x - bbox->l.x;
|
49 |
dist += bbox->h.x - bbox->l.x;
|
50 |
if (c->x == bbox->h.x)
|
51 |
return dist + bbox->h.y - c->y;
|
52 |
dist += bbox->h.y - bbox->l.y;
|
53 |
if (c->y == bbox->l.y)
|
54 |
return dist + bbox->h.x - c->x;
|
55 |
return -1;
|
56 |
}
|
57 |
|
58 |
static struct geom_poly_segment *
|
59 |
find_next(struct rect *bbox, GList *segments, struct coord *c, int exclude, struct coord *ci)
|
60 |
{
|
61 |
int min = INT_MAX, search = distance_from_ll(c, bbox) + (exclude ? 1 : 0);
|
62 |
GList *curr;
|
63 |
int i;
|
64 |
struct geom_poly_segment *ret = NULL;
|
65 |
int dbgl = 1;
|
66 |
|
67 |
for (i = 0; i < 2; i++)
|
68 |
{
|
69 |
curr = segments;
|
70 |
dbg(dbgl, "search distance %d\n", search);
|
71 |
while (curr)
|
72 |
{
|
73 |
struct geom_poly_segment *seg = curr->data;
|
74 |
int dist = distance_from_ll(seg->first, bbox);
|
75 |
dbg(dbgl, "0x%x 0x%x dist %d\n", seg->first->x, seg->first->y, dist);
|
76 |
if (dist != -1 && seg->first != seg->last && dist < min && (dist >= search))
|
77 |
{
|
78 |
min = dist;
|
79 |
ci[0] = *seg->first;
|
80 |
ci[1] = *seg->last;
|
81 |
ret = seg;
|
82 |
}
|
83 |
curr = g_list_next(curr);
|
84 |
}
|
85 |
if (ret || !search)
|
86 |
break;
|
87 |
search = 0;
|
88 |
}
|
89 |
return ret;
|
90 |
}
|
91 |
|
92 |
static void close_polygon(struct item_bin *ib, struct coord *from, struct coord *to, int dir, struct rect *bbox, int *edges)
|
93 |
{
|
94 |
int i, e, dist, fromdist, todist;
|
95 |
int full = (bbox->h.x - bbox->l.x + bbox->h.y - bbox->l.y) * 2;
|
96 |
int corners = 0, first_corner = 0;
|
97 |
struct coord c;
|
98 |
if (dir > 0)
|
99 |
{
|
100 |
fromdist = distance_from_ll(from, bbox);
|
101 |
todist = distance_from_ll(to, bbox);
|
102 |
}
|
103 |
else
|
104 |
{
|
105 |
fromdist = distance_from_ll(to, bbox);
|
106 |
todist = distance_from_ll(from, bbox);
|
107 |
}
|
108 |
#if 0
|
109 |
fprintf(stderr,"close_polygon fromdist %d todist %d full %d dir %d\n", fromdist, todist, full, dir);
|
110 |
#endif
|
111 |
if (fromdist > todist)
|
112 |
todist += full;
|
113 |
#if 0
|
114 |
fprintf(stderr,"close_polygon corrected fromdist %d todist %d full %d dir %d\n", fromdist, todist, full, dir);
|
115 |
#endif
|
116 |
for (i = 0; i < 8; i++)
|
117 |
{
|
118 |
if (dir > 0)
|
119 |
e = i;
|
120 |
else
|
121 |
e = 7 - i;
|
122 |
switch (e % 4)
|
123 |
{
|
124 |
case 0:
|
125 |
c = bbox->l;
|
126 |
break;
|
127 |
case 1:
|
128 |
c.x = bbox->l.x;
|
129 |
c.y = bbox->h.y;
|
130 |
break;
|
131 |
case 2:
|
132 |
c = bbox->h;
|
133 |
break;
|
134 |
case 3:
|
135 |
c.x = bbox->h.x;
|
136 |
c.y = bbox->l.y;
|
137 |
break;
|
138 |
}
|
139 |
dist = distance_from_ll(&c, bbox);
|
140 |
if (e & 4)
|
141 |
dist += full;
|
142 |
#if 0
|
143 |
fprintf(stderr,"dist %d %d\n",e,dist);
|
144 |
#endif
|
145 |
if (dist > fromdist && dist < todist)
|
146 |
{
|
147 |
item_bin_add_coord(ib, &c, 1);
|
148 |
#if 0
|
149 |
fprintf(stderr,"add\n");
|
150 |
#endif
|
151 |
}
|
152 |
if (dist >= fromdist && dist <= todist)
|
153 |
{
|
154 |
if (!corners)
|
155 |
first_corner = e;
|
156 |
corners++;
|
157 |
}
|
158 |
}
|
159 |
while (corners >= 2)
|
160 |
{
|
161 |
*edges |= 1 << (first_corner % 4);
|
162 |
first_corner++;
|
163 |
corners--;
|
164 |
}
|
165 |
}
|
166 |
|
167 |
struct coastline_tile_data
|
168 |
{
|
169 |
struct item_bin_sink_func *sink;
|
170 |
GHashTable *tile_edges;
|
171 |
int level;
|
172 |
};
|
173 |
|
174 |
static GList *
|
175 |
tile_data_to_segments(int *tile_data)
|
176 |
{
|
177 |
int *end = tile_data + tile_data[0];
|
178 |
int *curr = tile_data + 1;
|
179 |
GList *segments = NULL;
|
180 |
int count = 0;
|
181 |
|
182 |
while (curr < end)
|
183 |
{
|
184 |
struct item_bin *ib = (struct item_bin *) curr;
|
185 |
segments = g_list_prepend(segments, item_bin_to_poly_segment(ib, geom_poly_segment_type_way_right_side));
|
186 |
curr += ib->len + 1;
|
187 |
count++;
|
188 |
}
|
189 |
#if 0
|
190 |
fprintf(stderr,"%d segments\n",count);
|
191 |
#endif
|
192 |
return segments;
|
193 |
}
|
194 |
|
195 |
static void tile_collector_process_tile(char *tile, int *tile_data, struct coastline_tile_data *data)
|
196 |
{
|
197 |
|
198 |
//fprintf(stderr,"tile_collector_process_tile:Enter\n");
|
199 |
|
200 |
int poly_start_valid, tile_start_valid, exclude, search = 0;
|
201 |
struct rect bbox;
|
202 |
struct coord cn[2], end, poly_start, tile_start;
|
203 |
struct geom_poly_segment *first;
|
204 |
struct item_bin *ib = NULL;
|
205 |
struct item_bin_sink *out = data->sink->priv_data[1];
|
206 |
int dbgl = 1;
|
207 |
int edges = 0, flags;
|
208 |
GList *sorted_segments, *curr;
|
209 |
#if 0
|
210 |
if (strncmp(tile,"bcdbdcabddddba",7))
|
211 |
return;
|
212 |
#endif
|
213 |
#if 0
|
214 |
if (strncmp(tile,"bcdbdcaaaaddba",14))
|
215 |
return;
|
216 |
#endif
|
217 |
#if 0
|
218 |
fprintf(stderr,"tile %s of size %d\n", tile, *tile_data);
|
219 |
#endif
|
220 |
tile_bbox(tile, &bbox, 0);
|
221 |
sorted_segments = geom_poly_segments_sort(tile_data_to_segments(tile_data), geom_poly_segment_type_way_right_side);
|
222 |
#if 0
|
223 |
{
|
224 |
GList *sort_segments=sorted_segments;
|
225 |
int count=0;
|
226 |
while (sort_segments)
|
227 |
{
|
228 |
struct geom_poly_segment *seg=sort_segments->data;
|
229 |
struct item_bin *ib=(struct item_bin *)buffer;
|
230 |
char *text=g_strdup_printf("segment %d type %d %p %s area "LONGLONG_FMT,count++,seg->type,sort_segments,coord_is_equal(*seg->first, *seg->last) ? "closed":"open",geom_poly_area(seg->first,seg->last-seg->first+1));
|
231 |
item_bin_init(ib, type_rg_segment);
|
232 |
item_bin_add_coord(ib, seg->first, seg->last-seg->first+1);
|
233 |
item_bin_add_attr_string(ib, attr_debug, text);
|
234 |
// fprintf(stderr,"%s\n",text);
|
235 |
g_free(text);
|
236 |
// item_bin_dump(ib, stderr);
|
237 |
item_bin_write_to_sink(ib, out, NULL);
|
238 |
sort_segments=g_list_next(sort_segments);
|
239 |
}
|
240 |
}
|
241 |
#endif
|
242 |
flags = 0;
|
243 |
curr = sorted_segments;
|
244 |
while (curr)
|
245 |
{
|
246 |
struct geom_poly_segment *seg = curr->data;
|
247 |
switch (seg->type)
|
248 |
{
|
249 |
case geom_poly_segment_type_way_inner:
|
250 |
flags |= 1;
|
251 |
break;
|
252 |
case geom_poly_segment_type_way_outer:
|
253 |
flags |= 2;
|
254 |
break;
|
255 |
default:
|
256 |
flags |= 4;
|
257 |
break;
|
258 |
}
|
259 |
curr = g_list_next(curr);
|
260 |
}
|
261 |
|
262 |
if (flags == 1)
|
263 |
{
|
264 |
//fprintf(stderr,"tile_collector_process_tile:poly, flags==1\n");
|
265 |
|
266 |
ib = init_item(type_poly_water_tiled, 0);
|
267 |
item_bin_bbox(ib, &bbox);
|
268 |
item_bin_write_to_sink(ib, out, NULL);
|
269 |
g_hash_table_insert(data->tile_edges, g_strdup(tile), (void *) 15);
|
270 |
return;
|
271 |
}
|
272 |
|
273 |
#if 1
|
274 |
end = bbox.l;
|
275 |
tile_start_valid = 0;
|
276 |
poly_start_valid = 0;
|
277 |
exclude = 0;
|
278 |
poly_start.x = 0;
|
279 |
poly_start.y = 0;
|
280 |
tile_start.x = 0;
|
281 |
tile_start.y = 0;
|
282 |
for (;;)
|
283 |
{
|
284 |
search++;
|
285 |
// item_bin_write_debug_point_to_sink(out, &end, "Search %d",search);
|
286 |
dbg(dbgl, "searching next polygon from 0x%x 0x%x\n", end.x, end.y);
|
287 |
first = find_next(&bbox, sorted_segments, &end, exclude, cn);
|
288 |
exclude = 1;
|
289 |
if (!first)
|
290 |
break;
|
291 |
if (!tile_start_valid)
|
292 |
{
|
293 |
tile_start = cn[0];
|
294 |
tile_start_valid = 1;
|
295 |
}
|
296 |
else
|
297 |
{
|
298 |
if (cn[0].x == tile_start.x && cn[0].y == tile_start.y)
|
299 |
{
|
300 |
dbg(dbgl, "end of tile reached\n");
|
301 |
break;
|
302 |
}
|
303 |
}
|
304 |
if (first->type == geom_poly_segment_type_none)
|
305 |
{
|
306 |
end = cn[0];
|
307 |
continue;
|
308 |
}
|
309 |
poly_start_valid = 0;
|
310 |
dbg(dbgl, "start of polygon 0x%x 0x%x\n", cn[0].x, cn[0].y);
|
311 |
for (;;)
|
312 |
{
|
313 |
if (!poly_start_valid)
|
314 |
{
|
315 |
//fprintf(stderr,"tile_collector_process_tile:init item:poly_water_tiled\n");
|
316 |
|
317 |
poly_start = cn[0];
|
318 |
poly_start_valid = 1;
|
319 |
ib = init_item(type_poly_water_tiled, 0);
|
320 |
}
|
321 |
else
|
322 |
{
|
323 |
close_polygon(ib, &end, &cn[0], 1, &bbox, &edges);
|
324 |
if (cn[0].x == poly_start.x && cn[0].y == poly_start.y)
|
325 |
{
|
326 |
dbg(dbgl, "poly end reached\n");
|
327 |
item_bin_write_to_sink(ib, out, NULL);
|
328 |
end = cn[0];
|
329 |
break;
|
330 |
}
|
331 |
}
|
332 |
if (first->type == geom_poly_segment_type_none)
|
333 |
break;
|
334 |
item_bin_add_coord(ib, first->first, first->last - first->first + 1);
|
335 |
first->type = geom_poly_segment_type_none;
|
336 |
end = cn[1];
|
337 |
if (distance_from_ll(&end, &bbox) == -1)
|
338 |
{
|
339 |
dbg(dbgl, "incomplete\n");
|
340 |
break;
|
341 |
}
|
342 |
first = find_next(&bbox, sorted_segments, &end, 1, cn);
|
343 |
dbg(dbgl, "next segment of polygon 0x%x 0x%x\n", cn[0].x, cn[0].y);
|
344 |
}
|
345 |
if (search > 55)
|
346 |
break;
|
347 |
}
|
348 |
#endif
|
349 |
|
350 |
#if 0
|
351 |
{
|
352 |
int *end=tile_data+tile_data[0];
|
353 |
int *curr=tile_data+1;
|
354 |
while (curr < end)
|
355 |
{
|
356 |
struct item_bin *ib=(struct item_bin *)curr;
|
357 |
// item_bin_dump(ib);
|
358 |
ib->type=type_rg_segment;
|
359 |
item_bin_write_to_sink(ib, out, NULL);
|
360 |
curr+=ib->len+1;
|
361 |
#if 0
|
362 |
{
|
363 |
struct coord *c[2];
|
364 |
int i;
|
365 |
char *s;
|
366 |
c[0]=(struct coord *)(ib+1);
|
367 |
c[1]=c[0]+ib->clen/2-1;
|
368 |
for (i = 0; i < 2; i++)
|
369 |
{
|
370 |
s=coord_to_str(c[i]);
|
371 |
item_bin_write_debug_point_to_sink(out, c[i], "%s",s);
|
372 |
g_free(s);
|
373 |
}
|
374 |
|
375 |
}
|
376 |
#endif
|
377 |
}
|
378 |
}
|
379 |
#endif
|
380 |
g_hash_table_insert(data->tile_edges, g_strdup(tile), (void *) edges);
|
381 |
#if 0
|
382 |
item_bin_init(ib, type_border_country);
|
383 |
item_bin_bbox(ib, &bbox);
|
384 |
item_bin_add_attr_string(ib, attr_debug, tile);
|
385 |
item_bin_write_to_sink(ib, out, NULL);
|
386 |
#endif
|
387 |
#if 0
|
388 |
c.x=(bbox.l.x+bbox.h.x)/2;
|
389 |
c.y=(bbox.l.y+bbox.h.y)/2;
|
390 |
item_bin_write_debug_point_to_sink(out, &c, "%s %d",tile,edges);
|
391 |
#endif
|
392 |
}
|
393 |
|
394 |
static void ocean_tile(GHashTable *hash, char *tile, char c, struct item_bin_sink *out)
|
395 |
{
|
396 |
int len = strlen(tile);
|
397 |
char *tile2 = g_alloca(sizeof(char) * (len + 1));
|
398 |
struct rect bbox;
|
399 |
struct item_bin *ib;
|
400 |
struct coord co;
|
401 |
|
402 |
//fprintf(stderr,"ocean_tile:Enter\n");
|
403 |
|
404 |
strcpy(tile2, tile);
|
405 |
tile2[len - 1] = c;
|
406 |
|
407 |
//fprintf(stderr,"Testing %s\n",tile2);
|
408 |
|
409 |
if (g_hash_table_lookup_extended(hash, tile2, NULL, NULL))
|
410 |
{
|
411 |
return;
|
412 |
}
|
413 |
|
414 |
//fprintf(stderr,"%s ok\n",tile2);
|
415 |
|
416 |
tile_bbox(tile2, &bbox, 0);
|
417 |
ib = init_item(type_poly_water_tiled, 0);
|
418 |
item_bin_bbox(ib, &bbox);
|
419 |
item_bin_write_to_sink(ib, out, NULL);
|
420 |
g_hash_table_insert(hash, g_strdup(tile2), (void *) 15);
|
421 |
#if 0
|
422 |
item_bin_init(ib, type_border_country);
|
423 |
item_bin_bbox(ib, &bbox);
|
424 |
item_bin_add_attr_string(ib, attr_debug, tile2);
|
425 |
item_bin_write_to_sink(ib, out, NULL);
|
426 |
#endif
|
427 |
co.x = (bbox.l.x + bbox.h.x) / 2;
|
428 |
co.y = (bbox.l.y + bbox.h.y) / 2;
|
429 |
//item_bin_write_debug_point_to_sink(out, &co, "%s 15",tile2);
|
430 |
|
431 |
}
|
432 |
|
433 |
/* ba */
|
434 |
/* dc */
|
435 |
|
436 |
static void tile_collector_add_siblings(char *tile, void *edgesp, struct coastline_tile_data *data)
|
437 |
{
|
438 |
int len = strlen(tile);
|
439 |
char t = tile[len - 1];
|
440 |
struct item_bin_sink *out = data->sink->priv_data[1];
|
441 |
int edges = (int) edgesp;
|
442 |
int debug = 0;
|
443 |
|
444 |
if (len != data->level)
|
445 |
return;
|
446 |
#if 0
|
447 |
if (!strncmp(tile,"bcacccaadbdcd",10))
|
448 |
debug=1;
|
449 |
#endif
|
450 |
if (debug)
|
451 |
fprintf(stderr, "%s (%c) has %d edges active\n", tile, t, edges);
|
452 |
if (t == 'a' && (edges & 1))
|
453 |
ocean_tile(data->tile_edges, tile, 'b', out);
|
454 |
if (t == 'a' && (edges & 8))
|
455 |
ocean_tile(data->tile_edges, tile, 'c', out);
|
456 |
if (t == 'b' && (edges & 4))
|
457 |
ocean_tile(data->tile_edges, tile, 'a', out);
|
458 |
if (t == 'b' && (edges & 8))
|
459 |
ocean_tile(data->tile_edges, tile, 'd', out);
|
460 |
if (t == 'c' && (edges & 1))
|
461 |
ocean_tile(data->tile_edges, tile, 'd', out);
|
462 |
if (t == 'c' && (edges & 2))
|
463 |
ocean_tile(data->tile_edges, tile, 'a', out);
|
464 |
if (t == 'd' && (edges & 4))
|
465 |
ocean_tile(data->tile_edges, tile, 'c', out);
|
466 |
if (t == 'd' && (edges & 2))
|
467 |
ocean_tile(data->tile_edges, tile, 'b', out);
|
468 |
}
|
469 |
|
470 |
static int tile_sibling_edges(GHashTable *hash, char *tile, char c)
|
471 |
{
|
472 |
int len = strlen(tile);
|
473 |
int ret;
|
474 |
char *tile2 = g_alloca(sizeof(char) * (len + 1));
|
475 |
void *data;
|
476 |
strcpy(tile2, tile);
|
477 |
tile2[len - 1] = c;
|
478 |
if (!g_hash_table_lookup_extended(hash, tile2, NULL, &data))
|
479 |
ret = 15;
|
480 |
else
|
481 |
ret = (int) data;
|
482 |
//fprintf(stderr,"checking '%s' with %d edges active\n",tile2,ret);
|
483 |
|
484 |
return ret;
|
485 |
}
|
486 |
|
487 |
static void ocean_tile2(struct rect *r, int dx, int dy, int wf, int hf, struct item_bin_sink *out)
|
488 |
{
|
489 |
//fprintf(stderr,"ocean_tile2:Enter\n");
|
490 |
|
491 |
struct item_bin *ib;
|
492 |
int w = r->h.x - r->l.x;
|
493 |
int h = r->h.y - r->l.y;
|
494 |
char tile2[32];
|
495 |
struct rect bbox;
|
496 |
struct coord co;
|
497 |
bbox.l.x = r->l.x + dx * w;
|
498 |
bbox.l.y = r->l.y + dy * h;
|
499 |
bbox.h.x = bbox.l.x + w * wf;
|
500 |
bbox.h.y = bbox.l.y + h * hf;
|
501 |
//fprintf(stderr,"0x%x,0x%x-0x%x,0x%x -> 0x%x,0x%x-0x%x,0x%x\n",r->l.x,r->l.y,r->h.x,r->h.y,bbox.l.x,bbox.l.y,bbox.h.x,bbox.h.y);
|
502 |
ib = init_item(type_poly_water_tiled, 0);
|
503 |
item_bin_bbox(ib, &bbox);
|
504 |
item_bin_write_to_sink(ib, out, NULL);
|
505 |
#if 0
|
506 |
item_bin_init(ib, type_border_country);
|
507 |
item_bin_bbox(ib, &bbox);
|
508 |
item_bin_add_attr_string(ib, attr_debug, tile2);
|
509 |
item_bin_write_to_sink(ib, out, NULL);
|
510 |
#endif
|
511 |
tile(&bbox, NULL, tile2, 32, 0, NULL);
|
512 |
co.x = (bbox.l.x + bbox.h.x) / 2;
|
513 |
co.y = (bbox.l.y + bbox.h.y) / 2;
|
514 |
//item_bin_write_debug_point_to_sink(out, &co, "%s 15",tile2);
|
515 |
}
|
516 |
|
517 |
static void tile_collector_add_siblings2(char *tile, void *edgesp, struct coastline_tile_data *data)
|
518 |
{
|
519 |
int edges = (int) edgesp;
|
520 |
int pedges = 0;
|
521 |
int debug = 0;
|
522 |
int len = strlen(tile);
|
523 |
char *tile2 = g_alloca(sizeof(char) * (len + 1));
|
524 |
char t = tile[len - 1];
|
525 |
strcpy(tile2, tile);
|
526 |
tile2[len - 1] = '\0';
|
527 |
#if 0
|
528 |
if (!strncmp(tile,"bcacccaadbdcd",10))
|
529 |
debug=1;
|
530 |
#endif
|
531 |
if (debug)
|
532 |
fprintf(stderr, "len of %s %d vs %d\n", tile, len, data->level);
|
533 |
if (len != data->level)
|
534 |
return;
|
535 |
|
536 |
if (debug)
|
537 |
fprintf(stderr, "checking siblings of '%s' with %d edges active\n", tile, edges);
|
538 |
if (t == 'b' && (edges & 1) && (tile_sibling_edges(data->tile_edges, tile, 'd') & 1))
|
539 |
pedges |= 1;
|
540 |
if (t == 'd' && (edges & 2) && (tile_sibling_edges(data->tile_edges, tile, 'b') & 1))
|
541 |
pedges |= 1;
|
542 |
if (t == 'a' && (edges & 2) && (tile_sibling_edges(data->tile_edges, tile, 'b') & 2))
|
543 |
pedges |= 2;
|
544 |
if (t == 'b' && (edges & 2) && (tile_sibling_edges(data->tile_edges, tile, 'a') & 2))
|
545 |
pedges |= 2;
|
546 |
if (t == 'a' && (edges & 4) && (tile_sibling_edges(data->tile_edges, tile, 'c') & 4))
|
547 |
pedges |= 4;
|
548 |
if (t == 'c' && (edges & 4) && (tile_sibling_edges(data->tile_edges, tile, 'a') & 4))
|
549 |
pedges |= 4;
|
550 |
if (t == 'd' && (edges & 8) && (tile_sibling_edges(data->tile_edges, tile, 'c') & 8))
|
551 |
pedges |= 8;
|
552 |
if (t == 'c' && (edges & 8) && (tile_sibling_edges(data->tile_edges, tile, 'd') & 8))
|
553 |
pedges |= 8;
|
554 |
if (debug)
|
555 |
fprintf(stderr, "result '%s' %d old %d\n", tile2, pedges, (int) g_hash_table_lookup(data->tile_edges, tile2));
|
556 |
g_hash_table_insert(data->tile_edges, g_strdup(tile2), (void *) ((int) g_hash_table_lookup(data->tile_edges, tile2) | pedges));
|
557 |
}
|
558 |
|
559 |
static int tile_collector_finish(struct item_bin_sink_func *tile_collector)
|
560 |
{
|
561 |
struct coastline_tile_data data;
|
562 |
int i;
|
563 |
GHashTable *hash;
|
564 |
data.sink = tile_collector;
|
565 |
data.tile_edges = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
|
566 |
hash = tile_collector->priv_data[0];
|
567 |
if (verbose_mode) fprintf(stderr, "tile_collector_finish\n");
|
568 |
g_hash_table_foreach(hash, (GHFunc) tile_collector_process_tile, &data);
|
569 |
if (verbose_mode) fprintf(stderr, "tile_collector_finish foreach done\n");
|
570 |
g_hash_table_destroy(hash);
|
571 |
//fprintf(stderr,"tile_collector_finish destroy done\n");
|
572 |
for (i = 14; i > 0; i--)
|
573 |
{
|
574 |
if (verbose_mode) fprintf(stderr, "Level=%d\n", i);
|
575 |
data.level = i;
|
576 |
g_hash_table_foreach(data.tile_edges, (GHFunc) tile_collector_add_siblings, &data);
|
577 |
if (verbose_mode) fprintf(stderr, "*");
|
578 |
g_hash_table_foreach(data.tile_edges, (GHFunc) tile_collector_add_siblings, &data);
|
579 |
if (verbose_mode) fprintf(stderr, "*");
|
580 |
g_hash_table_foreach(data.tile_edges, (GHFunc) tile_collector_add_siblings, &data);
|
581 |
if (verbose_mode) fprintf(stderr, "*");
|
582 |
g_hash_table_foreach(data.tile_edges, (GHFunc) tile_collector_add_siblings, &data);
|
583 |
if (verbose_mode) fprintf(stderr, "*");
|
584 |
g_hash_table_foreach(data.tile_edges, (GHFunc) tile_collector_add_siblings2, &data);
|
585 |
if (verbose_mode) fprintf(stderr, "*\n");
|
586 |
g_hash_table_foreach(data.tile_edges, (GHFunc) tile_collector_add_siblings2, &data);
|
587 |
if (verbose_mode) fprintf(stderr, "*\n");
|
588 |
g_hash_table_foreach(data.tile_edges, (GHFunc) tile_collector_add_siblings2, &data);
|
589 |
if (verbose_mode) fprintf(stderr, "*\n");
|
590 |
g_hash_table_foreach(data.tile_edges, (GHFunc) tile_collector_add_siblings2, &data);
|
591 |
if (verbose_mode) fprintf(stderr, "*\n");
|
592 |
}
|
593 |
#if 0
|
594 |
data.level=13;
|
595 |
g_hash_table_foreach(data.tile_edges, tile_collector_add_siblings, &data);
|
596 |
g_hash_table_foreach(data.tile_edges, tile_collector_add_siblings, &data);
|
597 |
g_hash_table_foreach(data.tile_edges, tile_collector_add_siblings2, &data);
|
598 |
data.level=12;
|
599 |
g_hash_table_foreach(data.tile_edges, tile_collector_add_siblings, &data);
|
600 |
g_hash_table_foreach(data.tile_edges, tile_collector_add_siblings, &data);
|
601 |
#endif
|
602 |
item_bin_sink_func_destroy(tile_collector);
|
603 |
//fprintf(stderr,"tile_collector_finish done\n");
|
604 |
return 0;
|
605 |
}
|
606 |
|
607 |
static int coastline_processor_process(struct item_bin_sink_func *func, struct item_bin *ib, struct tile_data *tile_data)
|
608 |
{
|
609 |
#if 0
|
610 |
int i;
|
611 |
struct coord *c=(struct coord *)(ib+1);
|
612 |
for (i = 0; i < 19; i++)
|
613 |
{
|
614 |
c[i]=c[i+420];
|
615 |
}
|
616 |
ib->clen=(i-1)*2;
|
617 |
#endif
|
618 |
item_bin_write_clipped(ib, func->priv_data[0], func->priv_data[1]);
|
619 |
return 0;
|
620 |
}
|
621 |
|
622 |
static struct item_bin_sink_func *
|
623 |
coastline_processor_new(struct item_bin_sink *out)
|
624 |
{
|
625 |
struct item_bin_sink_func *coastline_processor = item_bin_sink_func_new(coastline_processor_process);
|
626 |
struct item_bin_sink *tiles = item_bin_sink_new();
|
627 |
struct item_bin_sink_func *tile_collector = tile_collector_new(out);
|
628 |
struct tile_parameter *param=g_new0(struct tile_parameter, 1);
|
629 |
|
630 |
//fprintf(stderr,"new:out=%p\n",out);
|
631 |
param->min = 14;
|
632 |
param->max = 14;
|
633 |
param->overlap = 0;
|
634 |
|
635 |
item_bin_sink_add_func(tiles, tile_collector);
|
636 |
coastline_processor->priv_data[0] = param;
|
637 |
coastline_processor->priv_data[1] = tiles;
|
638 |
coastline_processor->priv_data[2] = tile_collector;
|
639 |
return coastline_processor;
|
640 |
}
|
641 |
|
642 |
static void coastline_processor_finish(struct item_bin_sink_func *coastline_processor)
|
643 |
{
|
644 |
struct tile_parameter *param = coastline_processor->priv_data[0];
|
645 |
struct item_bin_sink *tiles = coastline_processor->priv_data[1];
|
646 |
struct item_bin_sink_func *tile_collector = coastline_processor->priv_data[2];
|
647 |
g_free(param);
|
648 |
tile_collector_finish(tile_collector);
|
649 |
item_bin_sink_destroy(tiles);
|
650 |
item_bin_sink_func_destroy(coastline_processor);
|
651 |
}
|
652 |
|
653 |
void process_coastlines(FILE *in, FILE *out)
|
654 |
{
|
655 |
struct item_bin_sink *reader = file_reader_new(in, 1000000, 0);
|
656 |
struct item_bin_sink_func *file_writer = file_writer_new(out);
|
657 |
struct item_bin_sink *result = item_bin_sink_new();
|
658 |
struct item_bin_sink_func *coastline_processor = coastline_processor_new(result);
|
659 |
item_bin_sink_add_func(reader, coastline_processor);
|
660 |
item_bin_sink_add_func(result, file_writer);
|
661 |
file_reader_finish(reader);
|
662 |
coastline_processor_finish(coastline_processor);
|
663 |
file_writer_finish(file_writer);
|
664 |
item_bin_sink_destroy(result);
|
665 |
}
|
666 |
|