/[zanavi_public1]/navit/navit/item.c
ZANavi

Contents of /navit/navit/item.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (hide annotations) (download)
Mon Feb 4 17:41:59 2013 UTC (11 years, 2 months ago) by zoff99
File MIME type: text/plain
File size: 9681 byte(s)
new map version, lots of fixes and experimental new features
1 zoff99 2 /**
2 zoff99 31 * 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 zoff99 2 * 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 zoff99 27 struct item_name
49     {
50     enum item_type item;
51     char *name;
52 zoff99 2 };
53    
54 zoff99 31 struct item_range item_range_all = { type_none, type_last };
55 zoff99 2
56 zoff99 27 struct default_flags
57     {
58 zoff99 2 enum item_type type;
59     int flags;
60     };
61    
62     struct item busy_item;
63    
64 zoff99 31 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 zoff99 2
66 zoff99 31 struct item_name item_names[] = {
67 zoff99 2 #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 zoff99 27 };
73 zoff99 2
74     static GHashTable *default_flags_hash;
75    
76     int *
77     item_get_default_flags(enum item_type type)
78     {
79 zoff99 27 if (!default_flags_hash)
80     {
81 zoff99 2 int i;
82 zoff99 27 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 zoff99 2 }
87     }
88 zoff99 27 return g_hash_table_lookup(default_flags_hash, (void *) (long) type);
89 zoff99 2 }
90    
91 zoff99 27 void item_cleanup(void)
92 zoff99 2 {
93     if (default_flags_hash)
94     g_hash_table_destroy(default_flags_hash);
95     }
96    
97 zoff99 27 void item_coord_rewind(struct item *it)
98 zoff99 2 {
99     it->meth->item_coord_rewind(it->priv_data);
100     }
101    
102 zoff99 27 int item_coord_get(struct item *it, struct coord *c, int count)
103 zoff99 2 {
104     return it->meth->item_coord_get(it->priv_data, c, count);
105     }
106    
107 zoff99 27 int item_coord_set(struct item *it, struct coord *c, int count, enum change_mode mode)
108 zoff99 2 {
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 zoff99 27 int item_coord_get_within_selection(struct item *it, struct coord *c, int count, struct map_selection *sel)
115 zoff99 2 {
116 zoff99 27 int i, ret = it->meth->item_coord_get(it->priv_data, c, count);
117 zoff99 2 struct coord_rect r;
118     struct map_selection *curr;
119     if (ret <= 0 || !sel)
120     return ret;
121 zoff99 27 r.lu = c[0];
122     r.rl = c[0];
123     for (i = 1; i < ret; i++)
124     {
125 zoff99 2 if (r.lu.x > c[i].x)
126 zoff99 27 r.lu.x = c[i].x;
127 zoff99 2 if (r.rl.x < c[i].x)
128 zoff99 27 r.rl.x = c[i].x;
129 zoff99 2 if (r.rl.y > c[i].y)
130 zoff99 27 r.rl.y = c[i].y;
131 zoff99 2 if (r.lu.y < c[i].y)
132 zoff99 27 r.lu.y = c[i].y;
133 zoff99 2 }
134 zoff99 27 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 zoff99 2 return ret;
140 zoff99 27 curr = curr->next;
141 zoff99 2 }
142 zoff99 27 return 0;
143 zoff99 2 }
144    
145 zoff99 27 int item_coord_get_pro(struct item *it, struct coord *c, int count, enum projection to)
146 zoff99 2 {
147 zoff99 27 int ret = item_coord_get(it, c, count);
148 zoff99 2 int i;
149 zoff99 27 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 zoff99 2 return ret;
154     }
155    
156 zoff99 27 int item_coord_is_node(struct item *it)
157 zoff99 2 {
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 zoff99 27 void item_attr_rewind(struct item *it)
164 zoff99 2 {
165     it->meth->item_attr_rewind(it->priv_data);
166     }
167    
168 zoff99 27 int item_attr_get(struct item *it, enum attr_type attr_type, struct attr *attr)
169 zoff99 2 {
170     if (it->meth)
171     {
172     return it->meth->item_attr_get(it->priv_data, attr_type, attr);
173     }
174     else
175     {
176 zoff99 27 dbg(0, "not method found\n");
177 zoff99 2 return 0;
178     }
179     }
180    
181 zoff99 27 int item_attr_set(struct item *it, struct attr *attr, enum change_mode mode)
182 zoff99 2 {
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 zoff99 29 // not working yet!!!!! ------
200     /*
201 zoff99 31 void item_dup(struct item *src, struct item *dst)
202     {
203     int size;
204     void *priv_data;
205 zoff99 29
206 zoff99 31 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 zoff99 29
211 zoff99 31 dst->priv_data=g_malloc(size);
212     memcpy(dst->priv_data, src->priv_data, size);
213     // int len = (ib->len + 1) * 4;
214 zoff99 29
215 zoff99 31 }
216     */
217 zoff99 29 // not working yet!!!!! ------
218    
219    
220 zoff99 27 enum item_type item_from_name(const char *name)
221 zoff99 2 {
222     int i;
223    
224 zoff99 27 for (i = 0; i < sizeof(item_names) / sizeof(struct item_name); i++)
225     {
226     if (!strcmp(item_names[i].name, name))
227 zoff99 2 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 zoff99 27 for (i = 0; i < sizeof(item_names) / sizeof(struct item_name); i++)
238     {
239 zoff99 2 if (item_names[i].item == item)
240     return item_names[i].name;
241     }
242 zoff99 27 return NULL;
243 zoff99 2 }
244    
245 zoff99 27 struct item_hash
246     {
247 zoff99 2 GHashTable *h;
248     };
249    
250 zoff99 27 static guint item_hash_hash(gconstpointer key)
251 zoff99 2 {
252 zoff99 27 const struct item *itm = key;
253     gconstpointer hashkey = (gconstpointer) GINT_TO_POINTER(itm->id_hi ^ itm->id_lo ^ (GPOINTER_TO_INT(itm->map)));
254 zoff99 2 return g_direct_hash(hashkey);
255     }
256    
257 zoff99 27 static gboolean item_hash_equal(gconstpointer a, gconstpointer b)
258 zoff99 2 {
259 zoff99 27 const struct item *itm_a = a;
260     const struct item *itm_b = b;
261 zoff99 2 if (item_is_equal(*itm_a, *itm_b))
262     return TRUE;
263     return FALSE;
264     }
265    
266 zoff99 27 unsigned int item_id_hash(const void *key)
267 zoff99 2 {
268 zoff99 27 const struct item_id *id = key;
269     return id->id_hi ^ id->id_lo;
270 zoff99 2 }
271    
272 zoff99 27 int item_id_equal(const void *a, const void *b)
273 zoff99 2 {
274 zoff99 27 const struct item_id *id_a = a;
275     const struct item_id *id_b = b;
276 zoff99 2 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 zoff99 31 ret->h = g_hash_table_new_full(item_hash_hash, item_hash_equal, g_free_func, NULL);
285 zoff99 2 return ret;
286     }
287    
288 zoff99 27 void item_hash_insert(struct item_hash *h, struct item *item, void *val)
289 zoff99 2 {
290     struct item *hitem=g_new(struct item, 1);
291 zoff99 27 *hitem = *item;
292 zoff99 29 //dbg(2, "inserting (0x%x,0x%x) into %p\n", item->id_hi, item->id_lo, h->h);
293 zoff99 2 g_hash_table_insert(h->h, hitem, val);
294     }
295    
296 zoff99 27 int item_hash_remove(struct item_hash *h, struct item *item)
297 zoff99 2 {
298     int ret;
299    
300 zoff99 29 //dbg(2, "removing (0x%x,0x%x) from %p\n", item->id_hi, item->id_lo, h->h);
301 zoff99 27 ret = g_hash_table_remove(h->h, item);
302 zoff99 29 //dbg(2, "ret=%d\n", ret);
303 zoff99 2
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 zoff99 27 void item_hash_destroy(struct item_hash *h)
314 zoff99 2 {
315     g_hash_table_destroy(h->h);
316     g_free(h);
317     }
318    
319 zoff99 27 int item_range_intersects_range(struct item_range *range1, struct item_range *range2)
320 zoff99 2 {
321     if (range1->max < range2->min)
322     return 0;
323     if (range1->min > range2->max)
324     return 0;
325     return 1;
326     }
327 zoff99 27 int item_range_contains_item(struct item_range *range, enum item_type type)
328 zoff99 2 {
329     if (type >= range->min && type <= range->max)
330     return 1;
331     return 0;
332     }
333    
334 zoff99 27 void item_dump_attr_stdout(struct item *item, struct map *map)
335 zoff99 2 {
336     struct attr attr;
337 zoff99 31 dbg(0, "type=%d:%s\n", item->type, item_to_name(item->type));
338 zoff99 2 while (item_attr_get(item, attr_any, &attr))
339     {
340 zoff99 31 dbg(0, " %d:%s='%s'", attr.type, attr_to_name(attr.type), attr_to_text(&attr, map, 1));
341 zoff99 2 // dbg(0," %s\n", attr_to_name(attr.type));
342     }
343     }
344    
345 zoff99 27 void item_dump_attr(struct item *item, struct map *map, FILE *out)
346 zoff99 2 {
347     struct attr attr;
348 zoff99 27 fprintf(out, "type=%s", item_to_name(item->type));
349     while (item_attr_get(item, attr_any, &attr))
350 zoff99 31 {
351 zoff99 27 fprintf(out, " %s='%s'", attr_to_name(attr.type), attr_to_text(&attr, map, 1));
352 zoff99 31 }
353 zoff99 2 }
354    
355 zoff99 27 void item_dump_filedesc(struct item *item, struct map *map, FILE *out)
356 zoff99 2 {
357    
358 zoff99 27 int i, count, max = 16384;
359     struct coord *ca = g_alloca(sizeof(struct coord) * max);
360 zoff99 2
361 zoff99 27 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 zoff99 2 item_dump_attr(item, map, out);
365 zoff99 27 fprintf(out, "\n");
366 zoff99 2 if (item->type >= type_line)
367 zoff99 27 for (i = 0; i < count; i++)
368     fprintf(out, "mg:0x%x 0x%x\n", ca[i].x, ca[i].y);
369 zoff99 2 }

   
Visit the ZANavi Wiki