12 #include "lab-bitmaps.h"
13 #include "lab-utils.h"
15 static void make_bitmap_icon(struct variant *v, struct sym_icon *si);
16 static void make_bitmap_point(struct variant *v, struct sym_point *sp);
17 static void make_bitmap_label(struct variant *v, struct sym_text *text);
19 static int overlaps(struct placement *p1, struct placement *p2);
21 static struct map_part **get_map_parts(struct placement *p);
22 static struct placement **get_overlapping(struct placement *p);
25 void make_bitmap(struct variant *v, struct symbol *sym)
27 v->offset_x = v->offset_y = 0;
31 case SYMBOLIZER_POINT:
32 make_bitmap_point(v, (struct sym_point *) sym);
35 make_bitmap_icon(v, (struct sym_icon *) sym);
38 make_bitmap_label(v, (struct sym_text *) sym);
41 ASSERT(sym->type != SYMBOLIZER_INVALID);
45 static void make_bitmap_icon(struct variant *v, struct sym_icon *si)
47 v->width = si->sir.width + 1;
48 v->height = si->sir.height + 1;
49 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
50 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
53 static void make_bitmap_point(struct variant *v, struct sym_point *sp)
55 v->width = v->height = sp->size + 1;
56 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
57 // FIXME: Okay, memset would be much nicer here
58 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
61 static void make_bitmap_label(struct variant *v, struct sym_text *text)
63 int tw = ceil(text->tw);
64 int th = ceil(text->th);
66 double rotate_rad = convert_to_rad(text->rotate);
68 // Initially, ll = [0; 0], lr = [tw, 0], ul = [0, th], ur = [tw, th]
69 // They could be declared before but should not be initialized in code
70 // as reassigning x-coordinate affects computation of y-cordinate
74 int lrx = tw * cos(rotate_rad);
75 int lry = tw * sin(rotate_rad);
77 int ulx = th * sin(rotate_rad);
78 int uly = th * cos(rotate_rad);
80 int urx = tw * cos(rotate_rad) + th * sin(rotate_rad);
81 int ury = tw * sin(rotate_rad) + th * cos(rotate_rad);
83 int min_x = min4(llx, lrx, ulx, urx);
84 int min_y = min4(lly, lry, uly, ury);
85 int max_x = max4(llx, lrx, ulx, urx);
86 int max_y = max4(lly, lry, uly, ury);
88 v->width = max_x - min_x + 1;
89 v->height = max_y - min_y + 1;
90 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
91 memset(v->bitmap, 0, v->width * v->height * sizeof(bool));
93 for (int i=0; i<th; i++)
95 for (int j=0; j<tw; j++)
97 int nx = j*cos(rotate_rad) + i*sin(rotate_rad);
98 int ny = j*sin(rotate_rad) + i*cos(rotate_rad);
99 v->bitmap[(ny-min_y)*v->width + (nx-min_x)] = 1;
104 void dump_bitmaps(struct individual *individual)
106 bool *bitmap = xmalloc(page_width_int * page_height_int * sizeof(bool));
107 printf("Bitmap size is %d\n", page_width_int * page_height_int);
108 for (int i=0; i<page_height_int; i++)
109 for (int j=0; j<page_width_int; j++)
110 bitmap[i*page_width_int + j] = 0;
113 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
115 if (individual->placements[i].variant_used == -1) continue;
117 struct placement *p = &(individual->placements[i]);
118 struct variant *v = NULL;
120 switch (p->request->type)
122 case REQUEST_SEGMENT: ;
123 case REQUEST_POINT: ;
125 v = &(p->request->variants[p->variant_used]);
128 ASSERT(p->request->type != REQUEST_INVALID);
132 int base_x = p->x; int base_y = p->y;
133 for (int dr = max2(0, 0-p->y); dr < v->height; dr++)
135 for (int dc = max2(0, 0-p->x); dc < v->width; dc++)
137 if (v->bitmap[dr * v->width + dc])
139 if (bitmap[(base_y + dr) * page_width_int + (base_x + dc)]) total += 1;
140 bitmap[(base_y + dr) * page_width_int + (base_x + dc)] = 1;
145 DEBUG(dbg_overlaps, VERBOSITY_GENERAL, "There were %d collisions during bitmap dump\n", total);
147 FILE *fd_dump = fopen("dump.pbm", "w");
148 fprintf(fd_dump, "P1\n");
149 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
150 for (int i=0; i<page_height_int; i++)
152 for (int j=0; j<page_width_int; j++)
154 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
156 fprintf(fd_dump, "\n");
163 static struct map_part **get_map_parts(struct placement *p)
165 if (p->variant_used < 0) return NULL;
167 struct map_part **buffer;
168 GARY_INIT(buffer, 0);
170 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Looking for map parts containing placement of request %d, placed at [%.2f; %.2f]\n", p->request->ind, p->x, p->y);
173 switch (p->request->type)
176 case REQUEST_SEGMENT:
178 v = p->request->variants[p->variant_used];
181 DEBUG(dbg_map_parts, VERBOSITY_ALL, "Skipping unsupported request type (%d)\n", p->request->type);
185 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Bitmap is %d x %d\n", v.width, v.height);
187 int x_min = max2(0, p->x) / conf_map_part_width;
188 // CHECK ME: Is rounding needed?
189 int x_max = min2(page_width_int, (p->x + v.width)) / conf_map_part_width;
190 int y_min = max2(0, p->y) / conf_map_part_height;
191 // CHECK ME: Is rounding needed?
192 int y_max = min2(page_height_int, (p->y + v.height)) / conf_map_part_height;
194 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
196 for (int y=y_min; y<=y_max; y++)
197 for (int x=x_min; x<=x_max; x++)
199 struct map_part **m = GARY_PUSH(buffer);
200 DEBUG(dbg_map_parts, VERBOSITY_ALL, "Asking for %d of %zu\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
201 *m = p->individual->map[y * num_map_parts_row + x];
204 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Returning %zu map parts potentially containing the symbol\n", GARY_SIZE(buffer));
209 void update_map_parts_delete(struct placement *p)
211 struct map_placement *mp = p->map_links;
214 mp->prev_in_map->next_in_map = mp->next_in_map;
216 mp->next_in_map->prev_in_map = mp->prev_in_map;
218 struct map_placement *tmp = mp;
219 mp = mp->next_in_placement;
225 void update_map_parts_create(struct placement *p)
227 struct map_part **parts = get_map_parts(p);
228 if (parts == NULL) return;
230 for (uns i=0; i<GARY_SIZE(parts); i++)
232 struct map_placement *mp = xmalloc(sizeof(struct map_placement));
236 mp->next_in_map = parts[i]->placement->next_in_map;
237 mp->prev_in_map = parts[i]->placement;
238 parts[i]->placement->next_in_map = mp;
239 if (mp->next_in_map) mp->next_in_map->prev_in_map = mp;
241 mp->next_in_placement = p->map_links;
242 mp->prev_in_placement = NULL;
249 void update_map_parts(struct placement *p)
251 update_map_parts_delete(p);
252 update_map_parts_create(p);
255 struct placement **get_closure(struct placement *placement)
257 struct placement **closure;
258 GARY_INIT(closure, 0);
259 bool *chosen = xmalloc(GARY_SIZE(placement->individual->placements) * sizeof(bool));
260 for (uns i=0; i<GARY_SIZE(placement->individual->placements); i++) { chosen[i] = 0; }
261 chosen[placement->request->ind] = 1;
263 struct placement **p = GARY_PUSH(closure); *p = placement;
266 while (first < GARY_SIZE(closure))
268 DEBUG(dbg_breeding, VERBOSITY_ALL, "Iterating, first is %u of current %zu\n", first, GARY_SIZE(closure));
269 struct placement **overlapping = get_overlapping(placement);
270 if (! overlapping) { first++; continue; }
272 struct placement **filtered = filter(overlapping, &chosen);
273 DEBUG(dbg_breeding, VERBOSITY_ALL, "There are %zu new overlapping symbols\n", GARY_SIZE(filtered));
274 GARY_FREE(overlapping);
275 overlapping = filtered;
276 for (uns j=0; j<GARY_SIZE(overlapping); j++)
278 if (! chosen[overlapping[j]->request->ind])
280 if (overlaps(*p, overlapping[j]))
282 p = GARY_PUSH(closure); *p = overlapping[j];
283 DEBUG(dbg_breeding, VERBOSITY_ALL, "Adding placement of request %d (in fact at [%.2f; %.2f] of size %d x %d)\n", overlapping[j]->request->ind, overlapping[j]->x, overlapping[j]->y, overlapping[j]->request->variants[overlapping[j]->variant_used].width, overlapping[j]->request->variants[overlapping[j]->variant_used].height);
284 chosen[overlapping[j]->request->ind] = 1;
288 GARY_FREE(overlapping);
297 static struct placement **get_overlapping(struct placement *p)
299 struct placement **buffer;
300 GARY_INIT(buffer, 0);
302 struct map_part **parts = get_map_parts(p);
303 if (! parts) return NULL;
305 for (uns i=0; i<GARY_SIZE(parts); i++)
307 struct map_placement *mp = parts[i]->placement->next_in_map;
310 if (p->variant_used >= 0)
312 struct placement **p = GARY_PUSH(buffer);
315 mp = mp->next_in_map;
320 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Returning %zu potentially overlapping placements\n", GARY_SIZE(buffer));
325 static int overlaps(struct placement *p1, struct placement *p2)
327 if (p1->request->type != REQUEST_POINT &&
328 p1->request->type != REQUEST_SEGMENT &&
329 p1->request->type != REQUEST_AREA)
332 if (p2->request->type != REQUEST_POINT &&
333 p2->request->type != REQUEST_SEGMENT &&
334 p2->request->type != REQUEST_AREA)
337 if (p1->variant_used == -1 || p2->variant_used == -1)
340 struct variant *v1, *v2;
342 v1 = &(p1->request->variants[p1->variant_used]);
343 v2 = &(p2->request->variants[p2->variant_used]);
345 // FIXME: This doesn't fully respect offset which it probably should
346 int p1x = p1->x; int p1y = p1->y;
347 int p2x = p2->x; int p2y = p2->y;
350 for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
351 for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
353 if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
354 v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
361 int get_overlap(struct placement *p, int **planned_ptr, int iteration)
363 int *planned = *planned_ptr;
365 if (p->variant_used == -1) return 0;
367 struct map_part **parts = get_map_parts(p);
370 DEBUG(dbg_overlaps, VERBOSITY_PLACEMENT, "Placement of request %d seems not to be placed\n", p->request->ind);
374 struct placement **others;
376 planned[p->request->ind] = iteration;
377 GARY_INIT(others, 0);
379 //printf("Iterating over parts of placement %d (at [%.2f; %.2f] / %d)\n", p->ind, p->x, p->y, p->variant_used);
380 for (uns i=0; i<GARY_SIZE(parts); i++)
382 //printf("%d:\t", parts[i]->ind);
383 //dump_part_links(parts[i]);
384 struct map_placement *mp = parts[i]->placement->next_in_map;
387 if (planned[mp->placement->request->ind] != iteration)
389 struct placement **p = GARY_PUSH(others);
391 planned[mp->placement->request->ind] = iteration;
393 mp = mp->next_in_map;
398 for (uns i=0; i<GARY_SIZE(others); i++)
400 overlap += overlaps(p, others[i]);
406 if (dbg_overlaps >= VERBOSITY_PLACEMENT)
408 printf("Placement %d (of request %d) add %d to overlaps", p->ind, p->request->ind, overlap);
409 //dump_placement_links(p);
412 if (p->x < 0) overlap += 0 - p->x;
413 if (p->x + p->request->variants[p->variant_used].width > page_width_int)
414 overlap += p->x + p->request->variants[p->variant_used].width - page_width_int;
416 if (p->y < 0) overlap += 0 - p->y;
417 if (p->y + p->request->variants[p->variant_used].height > page_height_int)
418 overlap += p->y + p->request->variants[p->variant_used].height - page_height_int;
423 void dump_label(struct symbol *sym)
427 case SYMBOLIZER_TEXT: ;
428 struct sym_text *st = (struct sym_text *) sym;
429 printf("%s\n", osm_val_decode(st->text));