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

Contents of /navit/navit/item.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 27 - (hide annotations) (download)
Mon Apr 9 21:27:36 2012 UTC (12 years ago) by zoff99
File MIME type: text/plain
File size: 8465 byte(s)
lots of new stuff, tranlsations, bug fixes ...
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 zoff99 27 struct item_name
30     {
31     enum item_type item;
32     char *name;
33 zoff99 2 };
34    
35 zoff99 27 struct item_range item_range_all =
36     { type_none, type_last };
37 zoff99 2
38 zoff99 27 struct default_flags
39     {
40 zoff99 2 enum item_type type;
41     int flags;
42     };
43    
44     struct item busy_item;
45    
46 zoff99 27 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 zoff99 2
78 zoff99 27 struct item_name item_names[] =
79     {
80 zoff99 2 #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 zoff99 27 };
86 zoff99 2
87     static GHashTable *default_flags_hash;
88    
89     int *
90     item_get_default_flags(enum item_type type)
91     {
92 zoff99 27 if (!default_flags_hash)
93     {
94 zoff99 2 int i;
95 zoff99 27 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 zoff99 2 }
100     }
101 zoff99 27 return g_hash_table_lookup(default_flags_hash, (void *) (long) type);
102 zoff99 2 }
103    
104 zoff99 27 void item_cleanup(void)
105 zoff99 2 {
106     if (default_flags_hash)
107     g_hash_table_destroy(default_flags_hash);
108     }
109    
110 zoff99 27 void item_coord_rewind(struct item *it)
111 zoff99 2 {
112     it->meth->item_coord_rewind(it->priv_data);
113     }
114    
115 zoff99 27 int item_coord_get(struct item *it, struct coord *c, int count)
116 zoff99 2 {
117     return it->meth->item_coord_get(it->priv_data, c, count);
118     }
119    
120 zoff99 27 int item_coord_set(struct item *it, struct coord *c, int count, enum change_mode mode)
121 zoff99 2 {
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 zoff99 27 int item_coord_get_within_selection(struct item *it, struct coord *c, int count, struct map_selection *sel)
128 zoff99 2 {
129 zoff99 27 int i, ret = it->meth->item_coord_get(it->priv_data, c, count);
130 zoff99 2 struct coord_rect r;
131     struct map_selection *curr;
132     if (ret <= 0 || !sel)
133     return ret;
134 zoff99 27 r.lu = c[0];
135     r.rl = c[0];
136     for (i = 1; i < ret; i++)
137     {
138 zoff99 2 if (r.lu.x > c[i].x)
139 zoff99 27 r.lu.x = c[i].x;
140 zoff99 2 if (r.rl.x < c[i].x)
141 zoff99 27 r.rl.x = c[i].x;
142 zoff99 2 if (r.rl.y > c[i].y)
143 zoff99 27 r.rl.y = c[i].y;
144 zoff99 2 if (r.lu.y < c[i].y)
145 zoff99 27 r.lu.y = c[i].y;
146 zoff99 2 }
147 zoff99 27 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 zoff99 2 return ret;
153 zoff99 27 curr = curr->next;
154 zoff99 2 }
155 zoff99 27 return 0;
156 zoff99 2 }
157    
158 zoff99 27 int item_coord_get_pro(struct item *it, struct coord *c, int count, enum projection to)
159 zoff99 2 {
160 zoff99 27 int ret = item_coord_get(it, c, count);
161 zoff99 2 int i;
162 zoff99 27 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 zoff99 2 return ret;
167     }
168    
169 zoff99 27 int item_coord_is_node(struct item *it)
170 zoff99 2 {
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 zoff99 27 void item_attr_rewind(struct item *it)
177 zoff99 2 {
178     it->meth->item_attr_rewind(it->priv_data);
179     }
180    
181 zoff99 27 int item_attr_get(struct item *it, enum attr_type attr_type, struct attr *attr)
182 zoff99 2 {
183     if (it->meth)
184     {
185     return it->meth->item_attr_get(it->priv_data, attr_type, attr);
186     }
187     else
188     {
189 zoff99 27 dbg(0, "not method found\n");
190 zoff99 2 return 0;
191     }
192     }
193    
194 zoff99 27 int item_attr_set(struct item *it, struct attr *attr, enum change_mode mode)
195 zoff99 2 {
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 zoff99 27 enum item_type item_from_name(const char *name)
213 zoff99 2 {
214     int i;
215    
216 zoff99 27 for (i = 0; i < sizeof(item_names) / sizeof(struct item_name); i++)
217     {
218     if (!strcmp(item_names[i].name, name))
219 zoff99 2 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 zoff99 27 for (i = 0; i < sizeof(item_names) / sizeof(struct item_name); i++)
230     {
231 zoff99 2 if (item_names[i].item == item)
232     return item_names[i].name;
233     }
234 zoff99 27 return NULL;
235 zoff99 2 }
236    
237 zoff99 27 struct item_hash
238     {
239 zoff99 2 GHashTable *h;
240     };
241    
242 zoff99 27 static guint item_hash_hash(gconstpointer key)
243 zoff99 2 {
244 zoff99 27 const struct item *itm = key;
245     gconstpointer hashkey = (gconstpointer) GINT_TO_POINTER(itm->id_hi ^ itm->id_lo ^ (GPOINTER_TO_INT(itm->map)));
246 zoff99 2 return g_direct_hash(hashkey);
247     }
248    
249 zoff99 27 static gboolean item_hash_equal(gconstpointer a, gconstpointer b)
250 zoff99 2 {
251 zoff99 27 const struct item *itm_a = a;
252     const struct item *itm_b = b;
253 zoff99 2 if (item_is_equal(*itm_a, *itm_b))
254     return TRUE;
255     return FALSE;
256     }
257    
258 zoff99 27 unsigned int item_id_hash(const void *key)
259 zoff99 2 {
260 zoff99 27 const struct item_id *id = key;
261     return id->id_hi ^ id->id_lo;
262 zoff99 2 }
263    
264 zoff99 27 int item_id_equal(const void *a, const void *b)
265 zoff99 2 {
266 zoff99 27 const struct item_id *id_a = a;
267     const struct item_id *id_b = b;
268 zoff99 2 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 zoff99 27 ret->h = g_hash_table_new_full(item_hash_hash, item_hash_equal, g_free, NULL);
277 zoff99 2 return ret;
278     }
279    
280 zoff99 27 void item_hash_insert(struct item_hash *h, struct item *item, void *val)
281 zoff99 2 {
282     struct item *hitem=g_new(struct item, 1);
283 zoff99 27 *hitem = *item;
284     dbg(2, "inserting (0x%x,0x%x) into %p\n", item->id_hi, item->id_lo, h->h);
285 zoff99 2 g_hash_table_insert(h->h, hitem, val);
286     }
287    
288 zoff99 27 int item_hash_remove(struct item_hash *h, struct item *item)
289 zoff99 2 {
290     int ret;
291    
292 zoff99 27 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 zoff99 2
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 zoff99 27 void item_hash_destroy(struct item_hash *h)
306 zoff99 2 {
307     g_hash_table_destroy(h->h);
308     g_free(h);
309     }
310    
311 zoff99 27 int item_range_intersects_range(struct item_range *range1, struct item_range *range2)
312 zoff99 2 {
313     if (range1->max < range2->min)
314     return 0;
315     if (range1->min > range2->max)
316     return 0;
317     return 1;
318     }
319 zoff99 27 int item_range_contains_item(struct item_range *range, enum item_type type)
320 zoff99 2 {
321     if (type >= range->min && type <= range->max)
322     return 1;
323     return 0;
324     }
325    
326 zoff99 27 void item_dump_attr_stdout(struct item *item, struct map *map)
327 zoff99 2 {
328     struct attr attr;
329 zoff99 27 dbg(0, "type=%s\n", item_to_name(item->type));
330 zoff99 2 while (item_attr_get(item, attr_any, &attr))
331     {
332 zoff99 27 dbg(0, " %s='%s'", attr_to_name(attr.type), attr_to_text(&attr, map, 1));
333 zoff99 2 // dbg(0," %s\n", attr_to_name(attr.type));
334     }
335     }
336    
337 zoff99 27 void item_dump_attr(struct item *item, struct map *map, FILE *out)
338 zoff99 2 {
339     struct attr attr;
340 zoff99 27 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 zoff99 2 }
344    
345 zoff99 27 void item_dump_filedesc(struct item *item, struct map *map, FILE *out)
346 zoff99 2 {
347    
348 zoff99 27 int i, count, max = 16384;
349     struct coord *ca = g_alloca(sizeof(struct coord) * max);
350 zoff99 2
351 zoff99 27 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 zoff99 2 item_dump_attr(item, map, out);
355 zoff99 27 fprintf(out, "\n");
356 zoff99 2 if (item->type >= type_line)
357 zoff99 27 for (i = 0; i < count; i++)
358     fprintf(out, "mg:0x%x 0x%x\n", ca[i].x, ca[i].y);
359 zoff99 2 }

   
Visit the ZANavi Wiki