12 #include "lab-bitmaps.h"
13 #include "lab-utils.h"
14 #include "lab-evolution.h"
16 static void make_bitmap_icon(struct variant *v, struct sym_icon *si);
17 static void make_bitmap_point(struct variant *v, struct sym_point *sp);
18 static void make_bitmap_label(struct variant *v, struct sym_text *text);
20 static int overlaps(struct placement *p1, struct placement *p2);
22 static struct map_part **get_map_parts(struct placement *p);
23 static struct placement **get_overlapping(struct placement *p);
26 void make_bitmap(struct variant *v, struct symbol *sym)
28 v->offset_x = v->offset_y = 0;
32 case SYMBOLIZER_POINT:
33 make_bitmap_point(v, (struct sym_point *) sym);
36 make_bitmap_icon(v, (struct sym_icon *) sym);
39 make_bitmap_label(v, (struct sym_text *) sym);
42 ASSERT(sym->type != SYMBOLIZER_INVALID);
46 static void make_bitmap_icon(struct variant *v, struct sym_icon *si)
48 v->width = si->sir.width + 1;
49 v->height = si->sir.height + 1;
50 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
51 for (int i=0; i<v->width*v->height; i++) v->bitmap[i] = 1;
54 static void make_bitmap_point(struct variant *v, struct sym_point *sp)
56 v->width = v->height = sp->size + 1;
57 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
58 // FIXME: Okay, memset would be much nicer here
59 for (int i=0; i<sp->size*sp->size; i++) v->bitmap[i] = 1;
62 static void make_bitmap_label(struct variant *v, struct sym_text *text)
64 int tw = ceil(text->tw);
65 int th = ceil(text->th);
67 double rotate_rad = convert_to_rad(text->rotate);
69 // Initially, ll = [0; 0], lr = [tw, 0], ul = [0, th], ur = [tw, th]
70 // They could be declared before but should not be initialized in code
71 // as reassigning x-coordinate affects computation of y-cordinate
75 int lrx = tw * cos(rotate_rad);
76 int lry = tw * sin(rotate_rad);
78 int ulx = th * sin(rotate_rad);
79 int uly = th * cos(rotate_rad);
81 int urx = tw * cos(rotate_rad) + th * sin(rotate_rad);
82 int ury = tw * sin(rotate_rad) + th * cos(rotate_rad);
84 int min_x = min4(llx, lrx, ulx, urx);
85 int min_y = min4(lly, lry, uly, ury);
86 int max_x = max4(llx, lrx, ulx, urx);
87 int max_y = max4(lly, lry, uly, ury);
89 v->width = max_x - min_x + 1;
90 v->height = max_y - min_y + 1;
91 v->bitmap = xmalloc(v->width * v->height * sizeof(bool));
92 memset(v->bitmap, 0, v->width * v->height * sizeof(bool));
94 for (int i=0; i<th; i++)
96 for (int j=0; j<tw; j++)
98 int nx = j*cos(rotate_rad) + i*sin(rotate_rad);
99 int ny = j*sin(rotate_rad) + i*cos(rotate_rad);
100 v->bitmap[(ny-min_y)*v->width + (nx-min_x)] = 1;
105 void dump_bitmaps(struct individual *individual)
107 bool *bitmap = xmalloc(page_width_int * page_height_int * sizeof(bool));
108 printf("Bitmap size is %d\n", page_width_int * page_height_int);
109 for (int i=0; i<page_height_int; i++)
110 for (int j=0; j<page_width_int; j++)
111 bitmap[i*page_width_int + j] = 0;
114 for (uns i=0; i<GARY_SIZE(individual->placements); i++)
116 if (individual->placements[i].variant_used == -1) continue;
118 struct placement *p = &(individual->placements[i]);
119 struct variant *v = NULL;
121 switch (p->request->type)
123 case REQUEST_SEGMENT: ;
124 case REQUEST_POINT: ;
126 v = &(p->request->variants[p->variant_used]);
129 ASSERT(p->request->type != REQUEST_INVALID);
133 int base_x = p->x; int base_y = p->y;
134 for (int dr = max2(0, 0-p->y); dr < v->height; dr++)
136 for (int dc = max2(0, 0-p->x); dc < v->width; dc++)
138 if (v->bitmap[dr * v->width + dc])
140 if (bitmap[(base_y + dr) * page_width_int + (base_x + dc)]) total += 1;
141 bitmap[(base_y + dr) * page_width_int + (base_x + dc)] = 1;
146 DEBUG(dbg_overlaps, VERBOSITY_GENERAL, "There were %d collisions during bitmap dump\n", total);
148 FILE *fd_dump = fopen("dump.pbm", "w");
149 fprintf(fd_dump, "P1\n");
150 fprintf(fd_dump, "%d %d\n", page_width_int, page_height_int);
151 for (int i=0; i<page_height_int; i++)
153 for (int j=0; j<page_width_int; j++)
155 fprintf(fd_dump, "%d", bitmap[(int) (i*page_width_int + j)] ? 1 : 0);
157 fprintf(fd_dump, "\n");
164 static struct map_part **get_map_parts(struct placement *p)
166 if (p->variant_used < 0) return NULL;
168 struct map_part **buffer;
169 GARY_INIT(buffer, 0);
171 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);
174 switch (p->request->type)
177 case REQUEST_SEGMENT:
179 v = p->request->variants[p->variant_used];
182 DEBUG(dbg_map_parts, VERBOSITY_ALL, "Skipping unsupported request type (%d)\n", p->request->type);
186 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Bitmap is %d x %d\n", v.width, v.height);
188 int x_min = max2(0, p->x) / conf_map_part_width;
189 // CHECK ME: Is rounding needed?
190 int x_max = min2(page_width_int, (p->x + v.width)) / conf_map_part_width;
191 int y_min = max2(0, p->y) / conf_map_part_height;
192 // CHECK ME: Is rounding needed?
193 int y_max = min2(page_height_int, (p->y + v.height)) / conf_map_part_height;
195 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Cells between [%d; %d] and [%d; %d] generated\n", x_min, y_min, x_max, y_max);
197 for (int y=y_min; y<=y_max; y++)
198 for (int x=x_min; x<=x_max; x++)
200 struct map_part **m = GARY_PUSH(buffer);
201 DEBUG(dbg_map_parts, VERBOSITY_ALL, "Asking for %d of %zu\n", y * num_map_parts_row + x, GARY_SIZE(p->individual->map));
202 *m = p->individual->map[y * num_map_parts_row + x];
205 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Returning %zu map parts potentially containing the symbol\n", GARY_SIZE(buffer));
210 void update_map_parts_delete(struct placement *p)
212 struct map_placement *mp = p->map_links;
215 mp->prev_in_map->next_in_map = mp->next_in_map;
217 mp->next_in_map->prev_in_map = mp->prev_in_map;
219 struct map_placement *tmp = mp;
220 mp = mp->next_in_placement;
226 void update_map_parts_create(struct placement *p)
228 struct map_part **parts = get_map_parts(p);
229 if (parts == NULL) return;
231 for (uns i=0; i<GARY_SIZE(parts); i++)
233 struct map_placement *mp = xmalloc(sizeof(struct map_placement));
237 mp->next_in_map = parts[i]->placement->next_in_map;
238 mp->prev_in_map = parts[i]->placement;
239 parts[i]->placement->next_in_map = mp;
240 if (mp->next_in_map) mp->next_in_map->prev_in_map = mp;
242 mp->next_in_placement = p->map_links;
243 mp->prev_in_placement = NULL;
250 void update_map_parts(struct placement *p)
252 update_map_parts_delete(p);
253 update_map_parts_create(p);
256 struct placement **get_closure(struct placement *placement)
258 struct placement **closure;
259 GARY_INIT(closure, 0);
260 bool *chosen = xmalloc(GARY_SIZE(placement->individual->placements) * sizeof(bool));
261 for (uns i=0; i<GARY_SIZE(placement->individual->placements); i++) { chosen[i] = 0; }
262 chosen[placement->request->ind] = 1;
264 struct placement **p = GARY_PUSH(closure); *p = placement;
267 while (first < GARY_SIZE(closure))
269 DEBUG(dbg_breeding, VERBOSITY_ALL, "Iterating, first is %u of current %zu\n", first, GARY_SIZE(closure));
270 struct placement **overlapping = get_overlapping(placement);
271 if (! overlapping) { first++; continue; }
273 struct placement **filtered = filter(overlapping, &chosen);
274 DEBUG(dbg_breeding, VERBOSITY_ALL, "There are %zu new overlapping symbols\n", GARY_SIZE(filtered));
275 GARY_FREE(overlapping);
276 overlapping = filtered;
277 for (uns j=0; j<GARY_SIZE(overlapping); j++)
279 if (! chosen[overlapping[j]->request->ind])
281 if (overlaps(*p, overlapping[j]))
283 p = GARY_PUSH(closure); *p = overlapping[j];
284 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);
285 chosen[overlapping[j]->request->ind] = 1;
289 GARY_FREE(overlapping);
298 static struct placement **get_overlapping(struct placement *p)
300 struct placement **buffer;
301 GARY_INIT(buffer, 0);
303 struct map_part **parts = get_map_parts(p);
304 if (! parts) return NULL;
306 for (uns i=0; i<GARY_SIZE(parts); i++)
308 struct map_placement *mp = parts[i]->placement->next_in_map;
311 if (p->variant_used >= 0)
313 struct placement **p = GARY_PUSH(buffer);
316 mp = mp->next_in_map;
321 DEBUG(dbg_map_parts, VERBOSITY_PLACEMENT, "Returning %zu potentially overlapping placements\n", GARY_SIZE(buffer));
326 static int overlaps(struct placement *p1, struct placement *p2)
328 if (p1->request->type != REQUEST_POINT &&
329 p1->request->type != REQUEST_SEGMENT &&
330 p1->request->type != REQUEST_AREA)
333 if (p2->request->type != REQUEST_POINT &&
334 p2->request->type != REQUEST_SEGMENT &&
335 p2->request->type != REQUEST_AREA)
338 if (p1->variant_used == -1 || p2->variant_used == -1)
341 struct variant *v1, *v2;
343 v1 = &(p1->request->variants[p1->variant_used]);
344 v2 = &(p2->request->variants[p2->variant_used]);
346 // FIXME: This doesn't fully respect offset which it probably should
347 int p1x = p1->x; int p1y = p1->y;
348 int p2x = p2->x; int p2y = p2->y;
351 for (int y=max2(0, max2(p1y, p2y)); y<min2(page_height_int, min2(p1y+v1->height, p2y+v2->height)); y++)
352 for (int x=max2(0, max2(p1x, p2x)); x<min2(page_width_int, min2(p1x+v1->width, p2x+v2->width)); x++)
354 if (v1->bitmap[(y-p1y)*v1->width + (x-p1x)] &&
355 v2->bitmap[(y-p2y)*v2->width + (x-p2x)])
362 int get_overlap(struct placement *p, int **planned_ptr, int iteration)
364 int *planned = *planned_ptr;
366 if (p->variant_used == -1) return 0;
368 struct map_part **parts = get_map_parts(p);
371 DEBUG(dbg_overlaps, VERBOSITY_PLACEMENT, "Placement of request %d seems not to be placed\n", p->request->ind);
375 struct placement **others;
377 planned[p->request->ind] = iteration;
378 GARY_INIT(others, 0);
380 //printf("Iterating over parts of placement %d (at [%.2f; %.2f] / %d)\n", p->ind, p->x, p->y, p->variant_used);
381 for (uns i=0; i<GARY_SIZE(parts); i++)
383 //printf("%d:\t", parts[i]->ind);
384 //dump_part_links(parts[i]);
385 struct map_placement *mp = parts[i]->placement->next_in_map;
388 if (planned[mp->placement->request->ind] != iteration)
390 struct placement **p = GARY_PUSH(others);
392 planned[mp->placement->request->ind] = iteration;
394 mp = mp->next_in_map;
399 for (uns i=0; i<GARY_SIZE(others); i++)
401 overlap += overlaps(p, others[i]);
407 if (dbg_overlaps >= VERBOSITY_PLACEMENT)
409 printf("Placement %d (of request %d) add %d to overlaps", p->ind, p->request->ind, overlap);
410 //dump_placement_links(p);
413 if (p->x < 0) overlap += 0 - p->x;
414 if (p->x + p->request->variants[p->variant_used].width > page_width_int)
415 overlap += p->x + p->request->variants[p->variant_used].width - page_width_int;
417 if (p->y < 0) overlap += 0 - p->y;
418 if (p->y + p->request->variants[p->variant_used].height > page_height_int)
419 overlap += p->y + p->request->variants[p->variant_used].height - page_height_int;
424 void dump_label(struct symbol *sym)
428 case SYMBOLIZER_TEXT: ;
429 struct sym_text *st = (struct sym_text *) sym;
430 printf("%s\n", osm_val_decode(st->text));