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