]> mj.ucw.cz Git - leo.git/blob - labeller.c
Labelling: Bugfixes in get_closure
[leo.git] / labeller.c
1 #include <stdio.h>
2 #include <math.h>
3 #include <ucw/lib.h>
4 #include <ucw/conf.h>
5
6 #include "leo.h"
7 #include "sym.h"
8 #include "map.h"
9 #include "labeller.h"
10 #include "lab-bitmaps.h"
11 #include "lab-utils.h"
12 #include "lab-evolution.h"
13 #include "lab-lines.h"
14
15 struct request_point *requests_point = NULL;
16 struct request_line *requests_line = NULL;
17 struct request_area *requests_area = NULL;
18
19 struct longline *longlines = NULL;
20 struct buffer_line *buffer_line = NULL;
21 struct buffer_linelabel *buffer_linelabel = NULL;
22
23 int dbg_segments = VERBOSITY_NONE;
24 int dbg_plan = VERBOSITY_NONE;
25 int dbg_requests = VERBOSITY_NONE;
26 int dbg_graph = VERBOSITY_NONE;
27 int dbg_bfs = VERBOSITY_NONE;
28 int dbg_map_parts = VERBOSITY_NONE;
29 int dbg_movement = VERBOSITY_NONE;
30 int dbg_init = VERBOSITY_NONE;
31 int dbg_overlaps = VERBOSITY_GENERAL;
32 int dbg_rank = VERBOSITY_NONE;
33 int dbg_evolution = VERBOSITY_POPULATION;
34 int dbg_mutation = VERBOSITY_NONE;
35 int dbg_breeding = VERBOSITY_NONE;
36
37 int page_width_int = 0;
38 int page_height_int = 0;
39
40 int num_requests = 0;
41 int num_placements = 0;
42
43 // In milimeters
44 int conf_map_part_width = 5;
45 int conf_map_part_height = 5;
46
47 uns num_map_parts_row = 0;
48 uns num_map_parts_col = 0;
49 uns num_map_parts = 0;
50
51 static struct cf_section labelling_cf = {
52   CF_ITEMS {
53     CF_DOUBLE("MaxSectionLenght", &conf_max_section_length),
54     CF_DOUBLE("MaxSectionOverlay", &conf_max_section_overlay),
55     CF_DOUBLE("BitmapGranularity", &bitmap_granularity),
56     CF_END
57   }
58 };
59
60 static void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex);
61 static void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex);
62
63 static void compute_sizes(void);
64
65 static void compute_sizes(void)
66 {
67   page_width_int = floor(page_width);
68   page_height_int = floor(page_height);
69
70   page_width_gran = page_width * bitmap_granularity;
71   page_height_gran = page_height * bitmap_granularity;
72
73   num_map_parts_row = (page_width_int + conf_map_part_width) / conf_map_part_width;
74   num_map_parts_col = (page_height_int + conf_map_part_height) / conf_map_part_height;
75   num_map_parts = num_map_parts_row * num_map_parts_col;
76
77 }
78
79 void labeller_conf(void)
80 {
81   cf_declare_section("Labelling", &labelling_cf, 0);
82   evolution_conf();
83 }
84
85 void labeller_init(void)
86 {
87   compute_sizes();
88
89   GARY_INIT(requests_point, 0);
90   GARY_INIT(requests_line, 0);
91   GARY_INIT(requests_area, 0);
92   GARY_INIT(buffer_line, 0);
93   GARY_INIT(buffer_linelabel, 0);
94   evolution_init();
95 }
96
97 void labeller_add_point(struct symbol *sym, struct osm_object *object, z_index_t zindex)
98 {
99   DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Adding point\n");
100   if (object->type != OSM_TYPE_NODE)
101   {
102     printf("Warning: Point label requested on non-point object\n");
103     return;
104   }
105
106   struct request_point *r = GARY_PUSH(requests_point);
107
108   r->request.type = REQUEST_POINT;
109   r->request.ind = num_requests++;
110
111   r->sym = sym;
112   r->zindex = zindex;
113
114   GARY_INIT(r->request.variants, 0);
115
116   struct variant *v = GARY_PUSH(r->request.variants);
117
118   struct osm_node *n = (struct osm_node *) object; // FIXME: Compiler warning
119   r->x = n->x;
120   r->y = n->y;
121   make_bitmap(v, sym);
122   switch (sym->type)
123   {
124     case SYMBOLIZER_ICON:
125       // FIXME: Really?
126       r->x = ((struct sym_icon *)sym)->sir.x;
127       r->y = ((struct sym_icon *)sym)->sir.y;
128       break;
129     default:
130       // FIXME
131       return;
132   }
133
134   DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Inited point to [%.2f; %.2f] on %u\n", r->x, r->y, r->zindex);
135 }
136
137 void labeller_notify_line(struct symbol *sym, z_index_t zindex)
138 {
139   DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Adding line on %u\n", zindex);
140   struct buffer_line *b = GARY_PUSH(buffer_line);
141   b->line = (struct sym_line *) sym;
142 }
143
144 void labeller_add_label(struct symbol *sym, struct osm_object *o, z_index_t zindex)
145 {
146   switch (o->type)
147   {
148     case OSM_TYPE_WAY:
149       if (osm_way_cyclic_p((struct osm_way *) o))
150         labeller_add_arealabel(sym, o, zindex);
151       else
152         labeller_add_linelabel(sym, o, zindex);
153       break;
154     default:
155       ;
156   }
157 }
158
159 static void labeller_add_linelabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
160 {
161   if (o->type != OSM_TYPE_WAY)
162   {
163     printf("Linelabel request on object which is not way\n");
164     return;
165   }
166
167   DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Labelling way %ju on %u\n", o->id, zindex);
168   struct buffer_linelabel *ll = GARY_PUSH(buffer_linelabel);
169   ll->way = (struct osm_way *) o;
170   ll->label = sym;
171   ll->zindex = zindex;
172 }
173
174 static void labeller_add_arealabel(struct symbol *sym, struct osm_object *o, z_index_t zindex)
175 {
176   DEBUG(dbg_requests, VERBOSITY_PLACEMENT, "Adding area on %u\n", zindex);
177   struct request_area *r = GARY_PUSH(requests_area);
178
179   r->request.type = REQUEST_AREA;
180   r->request.ind = num_requests++;
181
182   r->o = (struct osm_multipolygon *) o;
183   r->zindex = zindex;
184   r->label = sym;
185
186   osm_obj_center(o, &(r->cx), &(r->cy));
187
188   GARY_INIT(r->request.variants, 0);
189   struct variant *v = GARY_PUSH(r->request.variants);
190   make_bitmap(v, sym);
191 }
192
193 void labeller_label(void)
194 {
195   segment_lines();
196   evolve();
197
198   labeller_cleanup();
199 }
200
201 void labeller_cleanup(void)
202 {
203   lines_cleanup();
204   GARY_FREE(longlines);
205
206   GARY_FREE(requests_point);
207   GARY_FREE(requests_area);
208
209   for (uns i=0; i<GARY_SIZE(requests_line); i++)
210   {
211     for (uns j=0; j<GARY_SIZE(requests_line[i].sections); j++)
212     {
213       GARY_FREE(requests_line[i].sections[j].segments);
214     }
215     GARY_FREE(requests_line[i].sections);
216   }
217   GARY_FREE(requests_line);
218 }