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