summaryrefslogtreecommitdiff
path: root/navit/routech.c
blob: 853b89b0e989e5f1f7e01bd7dea8712db4bb538e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
#include <glib.h>
#include <stdio.h>
#include "item.h"
#include "coord.h"
#include "navit.h"
#include "transform.h"
#include "profile.h"
#include "mapset.h"
#include "map.h"

FILE *routefile;

void routech_test(struct navit *navit);

struct ch_edge {
	int flags;
	int weight;
	struct item_id target,middle;
};

struct routech_search {
	struct pq *pq;
	GHashTable *hash;
	int finished;
	int dir;
	unsigned int upper;
	struct item_id *via;
};


struct pq_element {
	struct item_id *node_id;
	struct item_id *parent_node_id;
	int stalled;
	int key;
	int heap_element;
};

struct pq_heap_element {
	int key;
	int element;
};

struct pq {
	int capacity;
	int size;
	int step;
	int elements_capacity;
	int elements_size;
	int elements_step;
	struct pq_element *elements;
	struct pq_heap_element *heap_elements;
};

static struct pq *
pq_new(void)
{
	struct pq *ret=g_new(struct pq, 1);
	ret->step=10;
	ret->capacity=0;
	ret->size=1;
	ret->elements_step=10;
	ret->elements_capacity=0;
	ret->elements_size=1;
	ret->elements=NULL;
	ret->heap_elements=NULL;
	return ret;
}

static int
pq_insert(struct pq *pq, int key, struct item_id *node_id)
{
	int element,i;
	if (pq->size >= pq->capacity) {
		pq->capacity += pq->step;
		pq->heap_elements=g_renew(struct pq_heap_element, pq->heap_elements, pq->capacity);
	}
	if (pq->elements_size >= pq->elements_capacity) {
		pq->elements_capacity += pq->elements_step;
		pq->elements=g_renew(struct pq_element, pq->elements, pq->elements_capacity);
	}
	element=pq->elements_size++;
	pq->elements[element].node_id=node_id;
	i=pq->size++;
	while (i > 1 && pq->heap_elements[i/2].key > key) {
		pq->heap_elements[i]=pq->heap_elements[i/2];
		pq->elements[pq->heap_elements[i].element].heap_element=i;
		i/=2;
	}
	pq->heap_elements[i].key=key;
	pq->heap_elements[i].element=element;
	pq->elements[element].heap_element=i;
	pq->elements[element].key=key;
	return element;
}

static int
pq_get_key(struct pq *pq, int element, int *key)
{
	*key=pq->elements[element].key;
	return 1;
}

static void
pq_set_parent(struct pq *pq, int element, struct item_id *node_id, int stalled)
{
	pq->elements[element].parent_node_id=node_id;
	pq->elements[element].stalled=stalled;
}

static struct item_id *
pq_get_parent_node_id(struct pq *pq, int element)
{
	return pq->elements[element].parent_node_id;
}

static void
pq_set_stalled(struct pq *pq, int element, int stalled)
{
	pq->elements[element].stalled=stalled;
}

static int
pq_get_stalled(struct pq *pq, int element)
{
	return pq->elements[element].stalled;
}

static int
pq_is_deleted(struct pq *pq, int element)
{
	return (pq->elements[element].heap_element == 0);
}

static int
pq_min(struct pq *pq)
{
	return (pq->heap_elements[1].key);
}

static void
pq_decrease_key(struct pq *pq, int element, int key)
{
	int i=pq->elements[element].heap_element;
	while (i > 1 && pq->heap_elements[i/2].key > key) {
		pq->heap_elements[i]=pq->heap_elements[i/2];
		pq->elements[pq->heap_elements[i].element].heap_element=i;
		i/=2;
	}
	pq->heap_elements[i].element=element;
	pq->heap_elements[i].key=key;
	pq->elements[element].heap_element=i;
	pq->elements[element].key=key;
}

static int
pq_delete_min(struct pq *pq, struct item_id **node_id, int *key, int *element)
{
	struct pq_heap_element min, last;
	int i=1,j;
	if (pq->size <= 1)
		return 0;
	min=pq->heap_elements[1];
	if (node_id)
		*node_id=pq->elements[min.element].node_id;
	if (key)
		*key=min.key;
	if (element)
		*element=min.element;
	pq->elements[min.element].heap_element=0;
	min.element=0;
	last=pq->heap_elements[--pq->size];
	while (i <= pq->size / 2) {
		j=2*i;
		if (j < pq->size && pq->heap_elements[j].key > pq->heap_elements[j+1].key)
			j++;
		if (pq->heap_elements[j].key >= last.key)
			break;
		pq->heap_elements[i]=pq->heap_elements[j];
		pq->elements[pq->heap_elements[i].element].heap_element=i;
		i=j;
	}
	pq->heap_elements[i]=last;
	pq->elements[last.element].heap_element=i;
	return 1;
}

static int
pq_is_empty(struct pq *pq)
{
	return pq->size <= 1;
}

static void
pq_check(struct pq *pq)
{
	int i;
	for (i = 2 ; i < pq->size ; i++) {
		if (pq->heap_elements[i/2].key > pq->heap_elements[i].key) {
			printf("%d vs %d\n", pq->heap_elements[i/2].key, pq->heap_elements[i].key);
			return;
		}
	}
	for (i = 1 ; i < pq->size ; i++) {
		if (i != pq->elements[pq->heap_elements[i].element].heap_element) {
			printf("Error: heap_element %d points to element %d, but that points to %d\n",i,pq->heap_elements[i].element,pq->elements[pq->heap_elements[i].element].heap_element);
		}
	}
}

static struct routech_search *
routech_search_new(int dir)
{
	struct routech_search *ret=g_new0(struct routech_search, 1);
	ret->pq=pq_new();
	ret->hash=g_hash_table_new_full(item_id_hash, item_id_equal, g_free, NULL);
	ret->upper=UINT_MAX;
	ret->dir=dir;

	return ret;
}

static int
routech_insert_node(struct routech_search *search, struct item_id **id, int val)
{
	struct item_id *ret;
	int e;
	if (g_hash_table_lookup_extended(search->hash, *id, (gpointer)&ret, (gpointer)&e)) {
		int oldval;
		pq_get_key(search->pq, e, &oldval);
		// printf("Node = %d\n",node);
		if (oldval > val) {
			pq_decrease_key(search->pq, e, val);
			*id=ret;
			return e;
		}
		return 0;
	}
	ret=g_new(struct item_id, 1);
	*ret=**id;
	e=pq_insert(search->pq, val, ret);
	g_hash_table_insert(search->hash, ret, GINT_TO_POINTER(e));
	*id=ret;
	return e;
}


static int
routech_find_nearest(struct mapset *ms, struct coord *c, struct item_id *id, struct map **map_ret)
{
	int dst=50;
	int dstsq=dst*dst;
	int ret=0;
	struct map_selection sel;
	struct map_rect *mr;
	struct mapset_handle *msh;
	struct map *map;
	struct item *item;
	sel.next=NULL;
	sel.order=18;
	sel.range.min=type_ch_node;
	sel.range.max=type_ch_node;
	sel.u.c_rect.lu.x=c->x-dst;
	sel.u.c_rect.lu.y=c->y+dst;
	sel.u.c_rect.rl.x=c->x+dst;
	sel.u.c_rect.rl.y=c->y-dst;
	printf("0x%x,0x%x-0x%x,0x%x\n",sel.u.c_rect.lu.x,sel.u.c_rect.lu.y,sel.u.c_rect.rl.x,sel.u.c_rect.rl.y);
	msh=mapset_open(ms);
	while ((map=mapset_next(msh, 1))) {
		mr=map_rect_new(map, &sel);
		if (!mr)
			continue;
		while ((item=map_rect_get_item(mr))) {
			struct coord cn;
			if (item->type == type_ch_node && item_coord_get(item, &cn, 1)) {
				int dist=transform_distance_sq(&cn, c);
				if (dist < dstsq) {	
					dstsq=dist;
					id->id_hi=item->id_hi;
					id->id_lo=item->id_lo;
					*map_ret=map;
					ret=1;
				}
			}
		}
		map_rect_destroy(mr);
	}
	mapset_close(msh);
	dbg_assert(ret==1);
	return ret;
}

static int
routech_edge_valid(struct ch_edge *edge, int dir)
{
	if (edge->flags & (1 << dir))
		return 1;
	return 0;
}

static void
routech_stall(struct map_rect *mr, struct routech_search *curr, struct item_id *id, int key)
{
	struct stall_element {
		struct item_id id;
		int key;
	} *se;
	GList *list=NULL;
	struct item *item;
	struct attr edge_attr;

	int index=GPOINTER_TO_INT(g_hash_table_lookup(curr->hash, id));
	pq_set_stalled(curr->pq, index, key);
	se=g_new(struct stall_element, 1);
	se->id=*id;
	se->key=key;
	list=g_list_append(list, se);
	while (list) {
		se=list->data;
		key=se->key;
		item=map_rect_get_item_byid(mr, se->id.id_hi, se->id.id_lo);
		while (item_attr_get(item, attr_ch_edge, &edge_attr)) {
			struct ch_edge *edge=edge_attr.u.data;
			if (routech_edge_valid(edge, curr->dir)) {
				int index=GPOINTER_TO_INT(g_hash_table_lookup(curr->hash, &edge->target));
				if (index) {
					int newkey=key+edge->weight;
					int target_key;
					pq_get_key(curr->pq, index, &target_key);
					if (newkey < target_key) {
						if (!pq_get_stalled(curr->pq, index)) {
							pq_set_stalled(curr->pq, index, newkey);
							se=g_new(struct stall_element, 1);
							se->id=edge->target;
							se->key=newkey;
							list=g_list_append(list, se);
						}	
					}
				}
			}
		}
		list=g_list_remove(list, se);
		g_free(se);
	}
}

static void
routech_relax(struct map_rect **mr, struct routech_search *curr, struct routech_search *opposite)
{
	int val,element;
	struct item_id *id;
	struct item *item;
	struct attr edge_attr;
	int opposite_element;
	
	if (!pq_delete_min(curr->pq, &id, &val, &element)) {
		return;
	}
	pq_check(curr->pq);
	opposite_element=GPOINTER_TO_INT(g_hash_table_lookup(opposite->hash, id));
	if (opposite_element && pq_is_deleted(opposite->pq, opposite_element)) {
		int opposite_val;
		pq_get_key(opposite->pq, opposite_element, &opposite_val);
		if (val+opposite_val < curr->upper) {
			curr->upper=opposite->upper=val+opposite_val;
			printf("%d path found: 0x%x,0x%x ub = %d\n",curr->dir,id->id_hi,id->id_lo,curr->upper);
			curr->via=opposite->via=id;
		}
	}
	if (pq_get_stalled(curr->pq, element)) 
		return;
	item=map_rect_get_item_byid(mr[0], id->id_hi, id->id_lo);
	while (item_attr_get(item, attr_ch_edge, &edge_attr)) {
		struct ch_edge *edge=edge_attr.u.data;
		struct item_id *target_id=&edge->target;
		int element;
		if (routech_edge_valid(edge, curr->dir)) {
			int index=GPOINTER_TO_INT(g_hash_table_lookup(curr->hash, target_id));
			if (index && routech_edge_valid(edge, 1-curr->dir)) {
				int newkey,stallkey;
				stallkey=pq_get_stalled(curr->pq, index);
				if (stallkey)
					newkey=stallkey;
				else
					pq_get_key(curr->pq, index, &newkey);
				newkey+=edge->weight;
				if (newkey < val) {
					routech_stall(mr[1], curr, id, newkey);
					return;
				}
			}
			element=routech_insert_node(curr, &target_id, edge->weight+val);
			if (element) {
				pq_set_parent(curr->pq, element, id, 0);
			}
		}
	}
}

static void
routech_print_coord(struct coord *c, FILE *out)
{
	int x=c->x;
	int y=c->y;
	char *sx="";
	char *sy="";
	if (x < 0) {
		sx="-";
		x=-x;
	}
	if (y < 0) {
		sy="-";
		y=-y;
	}
	fprintf(out,"%s0x%x %s0x%x\n",sx,x,sy,y);
}

static void
routech_resolve_route(struct map_rect *mr, struct item_id *id, int flags, int dir)
{
	int i,count,max=16384;
	struct coord *ca=g_alloca(sizeof(struct coord)*(max));
	struct item *item;
	int rev=0;
	if (!(flags & 8) == dir)
		rev=1;
	item=map_rect_get_item_byid(mr, id->id_hi, id->id_lo);
	dbg_assert(item->type >= type_line && item->type < type_area);
	item->type=type_street_route;
	
        count=item_coord_get(item, ca, max);
        item_dump_attr(item, item->map, routefile);
	fprintf(routefile,"debug=\"flags=%d dir=%d rev=%d\"",flags,dir,rev);
        fprintf(routefile,"\n");
	if (rev) {
		for (i = count-1 ; i >= 0 ; i--)
			routech_print_coord(&ca[i], routefile);
	} else {
		for (i = 0 ; i < count ; i++)
			routech_print_coord(&ca[i], routefile);
	}
}

static int
routech_find_edge(struct map_rect *mr, struct item_id *from, struct item_id *to, struct item_id *middle)
{
	struct item *item=map_rect_get_item_byid(mr, from->id_hi, from->id_lo);
	struct attr edge_attr;
	dbg_assert(item->type == type_ch_node);
	dbg(lvl_debug,"type %s\n",item_to_name(item->type));
	dbg(lvl_debug,"segment item=%p\n",item);
	while (item_attr_get(item, attr_ch_edge, &edge_attr)) {
		struct ch_edge *edge=edge_attr.u.data;
		dbg(lvl_debug,"flags=%d\n",edge->flags);
		if (edge->target.id_hi == to->id_hi && edge->target.id_lo == to->id_lo) {
			*middle=edge->middle;
			return edge->flags;
		}
	}
	dbg(lvl_debug,"** not found\n");
	return 0;
}

static void
routech_resolve(struct map_rect *mr, struct item_id *from, struct item_id *to, int dir)
{
	struct item_id middle_node;
	int res;
	if (dir) 
		res=routech_find_edge(mr, to, from, &middle_node);
	else
		res=routech_find_edge(mr, from, to, &middle_node);
	dbg(lvl_debug,"res=%d\n",res);
	if (res & 4) {
		routech_resolve(mr, from, &middle_node, 1);
		routech_resolve(mr, &middle_node, to, 0);
	} else 
		routech_resolve_route(mr, &middle_node, res, dir);
}

static void
routech_find_path(struct map_rect *mr, struct routech_search *search)
{
	struct item_id *curr_node=search->via;
	GList *i,*n,*list=NULL;
	dbg(lvl_debug,"node %p\n",curr_node);
	for (;;) {
		int element=GPOINTER_TO_INT(g_hash_table_lookup(search->hash, curr_node));
		struct item_id *next_node=pq_get_parent_node_id(search->pq,element);
		if (search->dir) 
			list=g_list_append(list, curr_node);
		else
			list=g_list_prepend(list, curr_node);
		dbg(lvl_debug,"element %d\n",element);
		dbg(lvl_debug,"next node %p\n",next_node);
		if (!next_node)
			break;
		curr_node=next_node;
	}
	i=list;
	while (i && (n=g_list_next(i))) {
		routech_resolve(mr, i->data, n->data, search->dir);
		i=n;
	}
}

void
routech_test(struct navit *navit)
{
	struct attr mapset;
	struct coord src={0x3fd661,0x727146};
	struct coord dst={0xfff07fc2,0x4754c9};
	struct item_id id[2],*id_ptr;
	struct routech_search *search[2],*curr,*opposite;
	struct map *map[2];
	struct map_rect *mr[2];
	int element;
	int k;
	int search_id=0;
	int i;

	navit_get_attr(navit, attr_mapset, &mapset, NULL);
	routech_find_nearest(mapset.u.mapset, &src, &id[0], &map[0]);
	routech_find_nearest(mapset.u.mapset, &dst, &id[1], &map[1]);
	for (k = 0 ; k < 2 ; k++) {
	profile(0,"start\n");
	search[0]=routech_search_new(0);
	search[1]=routech_search_new(1);
	printf("Start 0x%x,0x%x\n",id[0].id_hi,id[0].id_lo);
	printf("End 0x%x,0x%x\n",id[1].id_hi,id[1].id_lo);
	id_ptr=&id[0];
	element=routech_insert_node(search[0], &id_ptr, 0);
	pq_set_parent(search[0]->pq, element, NULL, 0);

	id_ptr=&id[1];
	element=routech_insert_node(search[1], &id_ptr, 0);
	pq_set_parent(search[1]->pq, element, NULL, 0);

	mr[0]=map_rect_new(map[0], NULL);
	mr[1]=map_rect_new(map[0], NULL);
	for (i=0 ; i < 5000 ; i++) {
		if (pq_is_empty(search[0]->pq) && pq_is_empty(search[1]->pq)) 
			break;
		if (!pq_is_empty(search[1-search_id]->pq)) {
			search_id=1-search_id;
		}
		if (search[0]->finished)
			search_id=1;
		if (search[1]->finished)
			search_id=0;
		curr=search[search_id];
		opposite=search[1-search_id];
		if (pq_is_empty(curr->pq)) {
			dbg(lvl_debug,"empty\n");
			break;
		}
		routech_relax(mr, curr, opposite);
		if (pq_min(curr->pq) > curr->upper) {
			dbg(lvl_debug,"min %d upper %d\n",pq_min(curr->pq), curr->upper);
			curr->finished=1;
		}
		if (curr->finished && opposite->finished) {
			dbg(lvl_debug,"finished\n");
			break;
		}
	}
	
	printf("finished %d\n",search[0]->upper);
	profile(0,"finished\n");
	}
	routefile=fopen("route.txt","w");
	routech_find_path(mr[0], search[0]);
	routech_find_path(mr[0], search[1]);
	fclose(routefile);
        printf("Heap size %d vs %d\n",search[0]->pq->size,search[1]->pq->size);
        printf("Element size %d vs %d\n",search[0]->pq->elements_size,search[1]->pq->elements_size);

}