1 |
/**
|
2 |
* Navit, a modular navigation system.
|
3 |
* Copyright (C) 2005-2008 Navit Team
|
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 |
#include <stdio.h>
|
21 |
#include <string.h>
|
22 |
#include <glib.h>
|
23 |
#include "coord.h"
|
24 |
#include "debug.h"
|
25 |
#include "item.h"
|
26 |
#include "map.h"
|
27 |
#include "transform.h"
|
28 |
|
29 |
struct item_name
|
30 |
{
|
31 |
enum item_type item;
|
32 |
char *name;
|
33 |
};
|
34 |
|
35 |
struct item_range item_range_all =
|
36 |
{ type_none, type_last };
|
37 |
|
38 |
struct default_flags
|
39 |
{
|
40 |
enum item_type type;
|
41 |
int flags;
|
42 |
};
|
43 |
|
44 |
struct item busy_item;
|
45 |
|
46 |
struct default_flags default_flags2[] =
|
47 |
{
|
48 |
{ type_street_nopass, AF_PBH },
|
49 |
{ type_street_0, AF_ALL },
|
50 |
{ type_street_1_city, AF_ALL },
|
51 |
{ type_street_2_city, AF_ALL },
|
52 |
{ type_street_3_city, AF_ALL },
|
53 |
{ type_street_4_city, AF_ALL },
|
54 |
{ type_highway_city, AF_MOTORIZED_FAST },
|
55 |
{ type_street_1_land, AF_ALL },
|
56 |
{ type_street_2_land, AF_ALL },
|
57 |
{ type_street_3_land, AF_ALL },
|
58 |
{ type_street_4_land, AF_ALL },
|
59 |
{ type_street_n_lanes, AF_MOTORIZED_FAST },
|
60 |
{ type_highway_land, AF_MOTORIZED_FAST },
|
61 |
{ type_ramp, AF_MOTORIZED_FAST },
|
62 |
{ type_roundabout, AF_ALL },
|
63 |
{ type_ferry, AF_ALL },
|
64 |
{ type_cycleway, AF_PBH },
|
65 |
{ type_track_paved, AF_ALL },
|
66 |
{ type_track_gravelled, AF_ALL },
|
67 |
{ type_track_unpaved, AF_ALL },
|
68 |
{ type_track_ground, AF_ALL },
|
69 |
{ type_track_grass, AF_ALL },
|
70 |
{ type_footway, AF_PBH },
|
71 |
{ type_living_street, AF_ALL },
|
72 |
{ type_street_service, AF_ALL },
|
73 |
{ type_bridleway, AF_PBH },
|
74 |
{ type_path, AF_PBH },
|
75 |
{ type_steps, AF_PBH },
|
76 |
{ type_street_pedestrian, AF_PBH }, };
|
77 |
|
78 |
struct item_name item_names[] =
|
79 |
{
|
80 |
#define ITEM2(x,y) ITEM(y)
|
81 |
#define ITEM(x) { type_##x, #x },
|
82 |
#include "item_def.h"
|
83 |
#undef ITEM2
|
84 |
#undef ITEM
|
85 |
};
|
86 |
|
87 |
static GHashTable *default_flags_hash;
|
88 |
|
89 |
int *
|
90 |
item_get_default_flags(enum item_type type)
|
91 |
{
|
92 |
if (!default_flags_hash)
|
93 |
{
|
94 |
int i;
|
95 |
default_flags_hash = g_hash_table_new(NULL, NULL);
|
96 |
for (i = 0; i < sizeof(default_flags2) / sizeof(struct default_flags); i++)
|
97 |
{
|
98 |
g_hash_table_insert(default_flags_hash, (void *) (long) default_flags2[i].type, &default_flags2[i].flags);
|
99 |
}
|
100 |
}
|
101 |
return g_hash_table_lookup(default_flags_hash, (void *) (long) type);
|
102 |
}
|
103 |
|
104 |
void item_cleanup(void)
|
105 |
{
|
106 |
if (default_flags_hash)
|
107 |
g_hash_table_destroy(default_flags_hash);
|
108 |
}
|
109 |
|
110 |
void item_coord_rewind(struct item *it)
|
111 |
{
|
112 |
it->meth->item_coord_rewind(it->priv_data);
|
113 |
}
|
114 |
|
115 |
int item_coord_get(struct item *it, struct coord *c, int count)
|
116 |
{
|
117 |
return it->meth->item_coord_get(it->priv_data, c, count);
|
118 |
}
|
119 |
|
120 |
int item_coord_set(struct item *it, struct coord *c, int count, enum change_mode mode)
|
121 |
{
|
122 |
if (!it->meth->item_coord_set)
|
123 |
return 0;
|
124 |
return it->meth->item_coord_set(it->priv_data, c, count, mode);
|
125 |
}
|
126 |
|
127 |
int item_coord_get_within_selection(struct item *it, struct coord *c, int count, struct map_selection *sel)
|
128 |
{
|
129 |
int i, ret = it->meth->item_coord_get(it->priv_data, c, count);
|
130 |
struct coord_rect r;
|
131 |
struct map_selection *curr;
|
132 |
if (ret <= 0 || !sel)
|
133 |
return ret;
|
134 |
r.lu = c[0];
|
135 |
r.rl = c[0];
|
136 |
for (i = 1; i < ret; i++)
|
137 |
{
|
138 |
if (r.lu.x > c[i].x)
|
139 |
r.lu.x = c[i].x;
|
140 |
if (r.rl.x < c[i].x)
|
141 |
r.rl.x = c[i].x;
|
142 |
if (r.rl.y > c[i].y)
|
143 |
r.rl.y = c[i].y;
|
144 |
if (r.lu.y < c[i].y)
|
145 |
r.lu.y = c[i].y;
|
146 |
}
|
147 |
curr = sel;
|
148 |
while (curr)
|
149 |
{
|
150 |
struct coord_rect *sr = &curr->u.c_rect;
|
151 |
if (r.lu.x <= sr->rl.x && r.rl.x >= sr->lu.x && r.lu.y >= sr->rl.y && r.rl.y <= sr->lu.y)
|
152 |
return ret;
|
153 |
curr = curr->next;
|
154 |
}
|
155 |
return 0;
|
156 |
}
|
157 |
|
158 |
int item_coord_get_pro(struct item *it, struct coord *c, int count, enum projection to)
|
159 |
{
|
160 |
int ret = item_coord_get(it, c, count);
|
161 |
int i;
|
162 |
enum projection from = map_projection(it->map);
|
163 |
if (from != to)
|
164 |
for (i = 0; i < count; i++)
|
165 |
transform_from_to(c + i, from, c + i, to);
|
166 |
return ret;
|
167 |
}
|
168 |
|
169 |
int item_coord_is_node(struct item *it)
|
170 |
{
|
171 |
if (it->meth->item_coord_is_node)
|
172 |
return it->meth->item_coord_is_node(it->priv_data);
|
173 |
return 0;
|
174 |
}
|
175 |
|
176 |
void item_attr_rewind(struct item *it)
|
177 |
{
|
178 |
it->meth->item_attr_rewind(it->priv_data);
|
179 |
}
|
180 |
|
181 |
int item_attr_get(struct item *it, enum attr_type attr_type, struct attr *attr)
|
182 |
{
|
183 |
if (it->meth)
|
184 |
{
|
185 |
return it->meth->item_attr_get(it->priv_data, attr_type, attr);
|
186 |
}
|
187 |
else
|
188 |
{
|
189 |
dbg(0, "not method found\n");
|
190 |
return 0;
|
191 |
}
|
192 |
}
|
193 |
|
194 |
int item_attr_set(struct item *it, struct attr *attr, enum change_mode mode)
|
195 |
{
|
196 |
if (!it->meth->item_attr_set)
|
197 |
return 0;
|
198 |
return it->meth->item_attr_set(it->priv_data, attr, mode);
|
199 |
}
|
200 |
|
201 |
struct item * item_new(char *type, int zoom)
|
202 |
{
|
203 |
struct item * it;
|
204 |
|
205 |
it = g_new0(struct item, 1);
|
206 |
|
207 |
/* FIXME evaluate arguments */
|
208 |
|
209 |
return it;
|
210 |
}
|
211 |
|
212 |
enum item_type item_from_name(const char *name)
|
213 |
{
|
214 |
int i;
|
215 |
|
216 |
for (i = 0; i < sizeof(item_names) / sizeof(struct item_name); i++)
|
217 |
{
|
218 |
if (!strcmp(item_names[i].name, name))
|
219 |
return item_names[i].item;
|
220 |
}
|
221 |
return type_none;
|
222 |
}
|
223 |
|
224 |
char *
|
225 |
item_to_name(enum item_type item)
|
226 |
{
|
227 |
int i;
|
228 |
|
229 |
for (i = 0; i < sizeof(item_names) / sizeof(struct item_name); i++)
|
230 |
{
|
231 |
if (item_names[i].item == item)
|
232 |
return item_names[i].name;
|
233 |
}
|
234 |
return NULL;
|
235 |
}
|
236 |
|
237 |
struct item_hash
|
238 |
{
|
239 |
GHashTable *h;
|
240 |
};
|
241 |
|
242 |
static guint item_hash_hash(gconstpointer key)
|
243 |
{
|
244 |
const struct item *itm = key;
|
245 |
gconstpointer hashkey = (gconstpointer) GINT_TO_POINTER(itm->id_hi ^ itm->id_lo ^ (GPOINTER_TO_INT(itm->map)));
|
246 |
return g_direct_hash(hashkey);
|
247 |
}
|
248 |
|
249 |
static gboolean item_hash_equal(gconstpointer a, gconstpointer b)
|
250 |
{
|
251 |
const struct item *itm_a = a;
|
252 |
const struct item *itm_b = b;
|
253 |
if (item_is_equal(*itm_a, *itm_b))
|
254 |
return TRUE;
|
255 |
return FALSE;
|
256 |
}
|
257 |
|
258 |
unsigned int item_id_hash(const void *key)
|
259 |
{
|
260 |
const struct item_id *id = key;
|
261 |
return id->id_hi ^ id->id_lo;
|
262 |
}
|
263 |
|
264 |
int item_id_equal(const void *a, const void *b)
|
265 |
{
|
266 |
const struct item_id *id_a = a;
|
267 |
const struct item_id *id_b = b;
|
268 |
return (id_a->id_hi == id_b->id_hi && id_a->id_lo == id_b->id_lo);
|
269 |
}
|
270 |
|
271 |
struct item_hash *
|
272 |
item_hash_new(void)
|
273 |
{
|
274 |
struct item_hash *ret=g_new(struct item_hash, 1);
|
275 |
|
276 |
ret->h = g_hash_table_new_full(item_hash_hash, item_hash_equal, g_free, NULL);
|
277 |
return ret;
|
278 |
}
|
279 |
|
280 |
void item_hash_insert(struct item_hash *h, struct item *item, void *val)
|
281 |
{
|
282 |
struct item *hitem=g_new(struct item, 1);
|
283 |
*hitem = *item;
|
284 |
dbg(2, "inserting (0x%x,0x%x) into %p\n", item->id_hi, item->id_lo, h->h);
|
285 |
g_hash_table_insert(h->h, hitem, val);
|
286 |
}
|
287 |
|
288 |
int item_hash_remove(struct item_hash *h, struct item *item)
|
289 |
{
|
290 |
int ret;
|
291 |
|
292 |
dbg(2, "removing (0x%x,0x%x) from %p\n", item->id_hi, item->id_lo, h->h);
|
293 |
ret = g_hash_table_remove(h->h, item);
|
294 |
dbg(2, "ret=%d\n", ret);
|
295 |
|
296 |
return ret;
|
297 |
}
|
298 |
|
299 |
void *
|
300 |
item_hash_lookup(struct item_hash *h, struct item *item)
|
301 |
{
|
302 |
return g_hash_table_lookup(h->h, item);
|
303 |
}
|
304 |
|
305 |
void item_hash_destroy(struct item_hash *h)
|
306 |
{
|
307 |
g_hash_table_destroy(h->h);
|
308 |
g_free(h);
|
309 |
}
|
310 |
|
311 |
int item_range_intersects_range(struct item_range *range1, struct item_range *range2)
|
312 |
{
|
313 |
if (range1->max < range2->min)
|
314 |
return 0;
|
315 |
if (range1->min > range2->max)
|
316 |
return 0;
|
317 |
return 1;
|
318 |
}
|
319 |
int item_range_contains_item(struct item_range *range, enum item_type type)
|
320 |
{
|
321 |
if (type >= range->min && type <= range->max)
|
322 |
return 1;
|
323 |
return 0;
|
324 |
}
|
325 |
|
326 |
void item_dump_attr_stdout(struct item *item, struct map *map)
|
327 |
{
|
328 |
struct attr attr;
|
329 |
dbg(0, "type=%s\n", item_to_name(item->type));
|
330 |
while (item_attr_get(item, attr_any, &attr))
|
331 |
{
|
332 |
dbg(0, " %s='%s'", attr_to_name(attr.type), attr_to_text(&attr, map, 1));
|
333 |
// dbg(0," %s\n", attr_to_name(attr.type));
|
334 |
}
|
335 |
}
|
336 |
|
337 |
void item_dump_attr(struct item *item, struct map *map, FILE *out)
|
338 |
{
|
339 |
struct attr attr;
|
340 |
fprintf(out, "type=%s", item_to_name(item->type));
|
341 |
while (item_attr_get(item, attr_any, &attr))
|
342 |
fprintf(out, " %s='%s'", attr_to_name(attr.type), attr_to_text(&attr, map, 1));
|
343 |
}
|
344 |
|
345 |
void item_dump_filedesc(struct item *item, struct map *map, FILE *out)
|
346 |
{
|
347 |
|
348 |
int i, count, max = 16384;
|
349 |
struct coord *ca = g_alloca(sizeof(struct coord) * max);
|
350 |
|
351 |
count = item_coord_get(item, ca, item->type < type_line ? 1 : max);
|
352 |
if (item->type < type_line)
|
353 |
fprintf(out, "mg:0x%x 0x%x ", ca[0].x, ca[0].y);
|
354 |
item_dump_attr(item, map, out);
|
355 |
fprintf(out, "\n");
|
356 |
if (item->type >= type_line)
|
357 |
for (i = 0; i < count; i++)
|
358 |
fprintf(out, "mg:0x%x 0x%x\n", ca[i].x, ca[i].y);
|
359 |
}
|