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

Contents of /navit/navit/item.c

Parent Directory Parent Directory | Revision Log Revision Log


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

   
Visit the ZANavi Wiki