-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnect.c
1186 lines (1086 loc) · 30.7 KB
/
connect.c
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ----------------------------------------------------------------//
// Filename : connect.c
// Source code for all pair shortest path and single source shortest
// path. Also some global varibales for use
//
// Author : Xiao Zigang
// Modifed: < Tue Mar 3 22:44:30 HKT 2009 >
// ----------------------------------------------------------------//
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include "util.h"
#include "ds.h"
#include "connect.h"
// ----------------------------------------------------------------//
// global variables
const static char *dir_string[]={"-","L","R","U","D"}; // for output
//static int precision=2; // controls double type output precision
static int width=10; // controls output width
// variables for graph
UINT ** g =NULL; // a matrix of graph, order = g_size
DIRECTION ** dirs=NULL; // the move directions between two point
NODE * g_node=NULL; // [0..static_num-1]=block corners,(rest)=sinks
int * mapping=NULL; // maps index of g node to its actual index
BOOL * use_corner=NULL; // mark if a corner of a block is usable
BOOL * g_occupy=NULL; // mark if g_node is a valid node
NODE * sink_node=NULL; // points to the first sink node in g_node
BLOCKAGE * pBlock=NULL; // pointer to blockage list
int block_num=0; // number of blockages
int static_num=0; // number of static nodes (shoulb be block_num*4)
int sink_num=0; // number of sinks
int g_size=0; // total size of g(total nodes available)
int g_num=0; // number of nodes in g currently
// mark if the graph has been constructed and shortest path has been calculated
BOOL dirty=TRUE;
// stores the edges of blockages
VSEG * vlist=NULL; // vertical list
HSEG * hlist=NULL; // horizontal list
int v_size=0; // size of vlist
int h_size=0; // size of hlist
// stores some forbidden edges
VSEG * vfbd = NULL; //
HSEG * hfbd = NULL; //
int vfbd_size=0;
int hfbd_size=0;
// stores the forbidden static node
BOOL ** fbdnode;
// variables for dijkstra
UINT * shortest=NULL; // shortest path shortest vector, size = g_size
int * via=NULL; // backtrack vector, size = g_size;
BOOL * mark=NULL; // mark if a node is visited
// used for disjoint set in order to find reverse path of a single source
int * disjoint_parent;
int * disjoint_height;
// variables for floyd, use swtich array technique
// size should be 2 * g_size * g_size
UINT ** pairs;
int ** parents;
// !!for internal use of floyd
UINT ** shortest_pair[2]={NULL,NULL}; // shortest pair matrix
int ** backtrack_pair[2]={NULL,NULL}; // backmatrix
// ----------------------------------------------------------------//
// functions operate on struct
// set the value of a vertical segment
void setvseg(VSEG * v,UINT xx,UINT yy1,UINT yy2){
v->x = xx; v->y1 = yy1; v->y2 = yy2;
}
// set the value of a horizontal segment
void sethseg(HSEG * h,UINT yy,UINT xx1,UINT xx2){
h->y = yy; h->x1 = xx1; h->x2 = xx2;
}
// determin if two rectilinear segment intersects
// hor : the horizontal segment
// ver : the vertical segment
// note: for hor, x1<x2
// for ver, y1<y2
inline BOOL intersect(HSEG hor,VSEG ver){
//if( DOUBLE_GT(ver.x,hor.x1) && DOUBLE_LT(ver.x,hor.x2) &&
// DOUBLE_GT(hor.y,ver.y1) && DOUBLE_LT(hor.y,ver.y2) )
if( ver.x > hor.x1 && ver.x < hor.x2 &&
hor.y > ver.y1 && hor.y < ver.y2)
return TRUE;
return FALSE;
}
// determins if a segment is in block(including partially in)
inline BOOL inBlock(BOX * pb, void * pSeg, int segtype){
HSEG *ph;
VSEG *pv;
switch(segtype){
case H:
ph = (HSEG*)pSeg;
if( ph->y > pb->ll.y && ph->y < pb->ur.y )
if( (ph->x1 > pb->ll.x && ph->x1 < pb->ur.x) ||
(ph->x2 > pb->ll.x && ph->x2 < pb->ur.x) ||
(ph->x1 >=pb->ll.x && ph->x2 <=pb->ur.x) )
return TRUE;
break;
case V:
pv = (VSEG*)pSeg;
if( pv->x > pb->ll.x && pv->x < pb->ur.x )
if( (pv->y1 > pb->ll.y && pv->y1 < pb->ur.y) ||
(pv->y2 > pb->ll.y && pv->y2 < pb->ur.y) ||
(pv->y1 >=pb->ll.y && pv->y2 <=pb->ur.y) )
return TRUE;
break;
}
return FALSE;
}
// determine if two vertical segment overlaps each other
inline int ver_overlap(VSEG v1,VSEG v2){
if( v1.x != v2.x || v1.y2<=v2.y1 || v1.y1>=v2.y2 ) return 0;
return 1;
}
// determine if two horizontal segment overlaps each other
inline int hor_overlap(HSEG h1,HSEG h2){
if( h1.y != h2.y || h1.x2<=h2.x1 || h1.x1>=h2.x2) return 0;
return 1;
}
// ----------------------------------------------------------------//
// functions
// construct the whole graph with a list of blockages and sinks
void construct_g_all(BLOCKAGE * blocks,BOX * frame, SINK * sink){
free_all();
// first construct the static part of graph(blockage corners)
pBlock = blocks;
block_num = blocks->num;
sink_num = sink->num;
static_num = block_num*4;
g_num = static_num; // only have static_num nodes now
allocate_g(static_num+sink_num+2); // allocate space for all(2 for extra space)
init_g(); // initialize the elements
gen_block_node(blocks); // generate a set of blockage corners
gen_segments(blocks); // generate segments from blockages
constructg(blocks,frame);
// now copy the sinks into g_node
copy_sink(sink);
int i;
// add every sink point into the graph
//for(i=0;i<g_size;i++) printf("g_occupy[%d]=%d\n",i,(int)g_occupy[i]);//
for(i=0;i<sink_num;i++){
//printf("inserting %d\n",i);
insertpt(sink_node[i],i);
}
}
// determines if a node is in a blockage
// including the case that the node is in the bounding box of rect
// true if they intersects
BOOL pt_in_rect(NODE * node,BOX * b){
int x = node->x,y=node->y;
NODE * ll = &b->ll;
NODE * ur = &b->ur;
if( (x<ur->x && x>ll->x) &&
(y<ur->y && y>ll->y) ){
#ifdef DEBUG
printf("(%d,%d) sec (%d %d),(%d %d)\n",x,y,
ll->x,ll->y,
ur->x,ur->y);
#endif
return TRUE;
}
return FALSE;
}
// useful macro, in order to shorten the code...
#define setfbdnode(a,b,value) {fbdnode[(a)][(b)]=fbdnode[(b)][(a)]=(value);}
#define setgnode(a,b,value) {g[(a)][(b)]=g[(b)][(a)]=(value);}
#define set_g_fbd(a,b) {setfbdnode((a),(b),TRUE);setgnode((a),(b),INFINITE);}
// check the adjacency edges of blockages, mark it as forbidden
// note that the cases are very complicated and the code should be
// modified carefully
int mark_forbidden(BLOCKAGE * block,BOX * frame){
NODE *nodei,*nodej;
BOX * boxi,*boxj;
int b_i,b_j;
for(b_i=0;b_i<block->num;b_i++){
boxi = &block->pool[b_i];
nodei = g_node + b_i*4;
for(b_j=b_i+1;b_j<block->num;b_j++){
boxj = &block->pool[b_j];
nodej = g_node+b_j*4;
// check if vertical adjacent
BOX *p1=boxi,*p2=boxj;
int l=b_i*4;
int r=b_j*4;
if( boxi->ll.x > boxj->ll.x ){
// ensures p1 has smaller x
p1=boxj;
p2=boxi;
l=b_j*4;
r=b_i*4;
}
VSEG v1,v2;
setvseg(&v1,p1->ur.x,p1->ll.y,p1->ur.y);
setvseg(&v2,p2->ll.x,p2->ll.y,p2->ur.y);
// check if horizontal adjacent
if( v1.x != v2.x || v1.y2 <= v2.y1 || v2.y2 <= v1.y1){
; // do nothing
}
else{// they share a vertical edge
VSEG v;
v.x =v1.x;
v.y1=MAX(v1.y1,v2.y1);
v.y2=MIN(v1.y2,v2.y2);
set_g_fbd(l+1,l+2);
set_g_fbd(r+0,r+3);
set_g_fbd(l+1,r+3);
set_g_fbd(l+2,r+0);
set_g_fbd(l+1,r+3);
set_g_fbd(l+2,r+0);
if( v1.y1 < v2.y1 ){// (3,0)
set_g_fbd(l+3,r+0);
if( v1.y2 < v2.y2 ){//(2,1)
set_g_fbd(l+2,r+1);
}
else if( v1.y2 > v2.y2 ){//(0,3)
set_g_fbd(l+0,r+3);
}
}
else if( v1.y1 > v2.y1 ){// (1,2)
set_g_fbd(l+1,r+2);
if( v1.y2 > v2.y2 ){// (0,3)
set_g_fbd(l+0,r+3);
}
else if( v1.y2 < v2.y2 ){// (2,1)
set_g_fbd(l+2,r+1);
}
}
vfbd[vfbd_size++] = v;
}
p1=boxi,p2=boxj;
int d=b_i*4;
int u=b_j*4;
if(boxi->ll.y > boxj->ll.y){
p1=boxj;
p2=boxi;
d=b_j*4;
u=b_i*4;
}
HSEG h1,h2;
sethseg(&h1,p1->ur.y,p1->ll.x,p1->ur.x);
sethseg(&h2,p2->ll.y,p2->ll.x,p2->ur.x);
if( h1.y != h2.y || h1.x2 <= h2.x1 || h2.x2 <= h1.x1){
;
}
else{// they share a horizontal edge
HSEG h;
h.y =h1.y;
h.x1=MAX(h1.x1,h2.x1);
h.x2=MIN(h1.x2,h2.x2);
set_g_fbd(d+2,d+3);
set_g_fbd(u+0,u+1);
set_g_fbd(d+2,u+0);
set_g_fbd(d+3,u+1);
set_g_fbd(d+2,u+0);
set_g_fbd(d+3,u+1);
if( h1.x1 < h2.x1 ){
set_g_fbd(d+1,u+0);
if( h1.x2 < h2.x2 ){
set_g_fbd(d+2,u+3);
}
else if( h1.x2 > h2.x2 ){
set_g_fbd(d+0,u+1);
}
}
else if( h1.x1 > h2.x1 ){
set_g_fbd(d+3,u+3);
if( h1.x2 > h2.x2 ){
set_g_fbd(d+0,u+1);
}
else if( h1.x2 < h2.x2 ){
set_g_fbd(d+2,u+3);
}
}
hfbd[hfbd_size++] = h;
}// end of share horizontal edge
}// end of for b_j
}//end of for b_i
// at last, add the boundary of board into list.
add_frame_forbid(frame);
return 0;
}// end of mark_forbidden
// add the boundary of board into forbidden list.
void add_frame_forbid(BOX * frame){
int i;
for(i=0;i<v_size;i++){
if( vlist[i].x == frame->ll.x ||
vlist[i].x == frame->ur.x )
vfbd[vfbd_size++]=vlist[i];
}
for(i=0;i<h_size;i++){
if( hlist[i].y == frame->ll.y ||
hlist[i].y == frame->ur.y )
hfbd[hfbd_size++]=hlist[i];
}
}
// take a list of blockage, construct a graph for shortest path computation
// REQUIRE: external storage g
// list : pointer to BLOCKAGE
int constructg(BLOCKAGE * block,BOX * frame){
// start to construct the graph
mark_forbidden(block,frame);
int b_i,b_j;
int cor_i,cor_j;
// for each corner of each blockage,
// determine what corners it can each
// (in the sense of manhattan distance) : 4 for-loop
NODE *nodei,*nodej;
BOX * boxi,*boxj;
for(b_i=0;b_i<block->num;b_i++){
boxi = &block->pool[b_i];
nodei = g_node + b_i*4;
for(b_j=b_i+1;b_j<block->num;b_j++){
boxj = &block->pool[b_j];
nodej = g_node+b_j*4;
// generate the 4 nodes of each blockage
// 3--2
// | |
// 0--1
for(cor_i=0;cor_i<4;cor_i++){
// handle the intersection case
if( pt_in_rect(&nodei[cor_i],boxj) ){
// mark it unusable
use_corner[b_i*4+cor_i] = FALSE;
continue;
}
for(cor_j=0;cor_j<4;cor_j++){
if( pt_in_rect(&nodej[cor_j],boxi) ){
use_corner[b_j*4+cor_j] = FALSE;
continue;
}
// marked as not connectable
// need not to calculate the same point
int idx1 = b_i*4+cor_i;
int idx2 = b_j*4+cor_j;
if( fbdnode[idx1][idx2] ) continue;
reach( nodei[cor_i], nodej[cor_j],
idx1,idx2);
}// end of for cor_j
}// end of for cor_i
}//end of for b_j
// for block b_i, connect its four points(self connect)
reach(nodei[0],nodei[1],b_i*4+0,b_i*4+1);
reach(nodei[1],nodei[2],b_i*4+1,b_i*4+2);
reach(nodei[2],nodei[3],b_i*4+2,b_i*4+3);
reach(nodei[3],nodei[0],b_i*4+3,b_i*4+0);
}// end of for b_i
return 0;
}// end of constructg;
// allocate space for graph, directions, shortest path, via,
// g_node and mark, floyd and backtrack
// set all members of the graph matrix to INFINITE except the diagnal
// n : number of max nodes
void allocate_g(int size){
g_size = size;
g = (UINT**) malloc((g_size)*sizeof(UINT*));
g_node = (NODE*) malloc((g_size)*sizeof(NODE));
mapping = (int*) malloc((g_size)*sizeof(int));
use_corner = (BOOL*) malloc((g_size)*sizeof(BOOL));
sink_node = g_node+static_num; // points to first sink node
g_occupy = (BOOL*) malloc((g_size)*sizeof(BOOL));
dirs = (DIRECTION**) malloc((g_size)*sizeof(DIRECTION*));
fbdnode = (BOOL**) malloc((g_size)*sizeof(BOOL*));
// for dijkstra
shortest = (UINT *) malloc(g_size * sizeof(UINT));
via = (int *) malloc(g_size * sizeof(int));
mark = (BOOL*) malloc(g_size * sizeof(BOOL));
disjoint_parent = (int *) malloc(g_size * sizeof(int));
disjoint_height = (int *) malloc(g_size * sizeof(int));
// for floyd
int x,y;
for(x=0;x<2;x++){// dimension 2 allocation
shortest_pair[x] = malloc(sizeof(UINT**) * g_size);
backtrack_pair[x] = malloc(sizeof(int**) * g_size);
for(y=0;y<g_size;y++){// dimension 3 allocation
shortest_pair[x][y] = malloc(sizeof(UINT*) * g_size);
backtrack_pair[x][y] = malloc(sizeof(int*) * g_size);
}
}
int i;
for(i=0;i<g_size;i++){// 2-dimension allocation
g[i] = (UINT *) malloc(g_size*sizeof(UINT));
dirs[i] = (DIRECTION *) malloc(g_size*sizeof(DIRECTION));
fbdnode[i] = (BOOL*) malloc(g_size*sizeof(UINT));
}
}
// initialize the graph to be not connected
// the directions between points are INVALID
void init_g(){
int i,j;
for(i=0;i<g_size;i++){
for(j=0;j<g_size;j++){
if(i==j) g[i][j] = 0;
else g[i][j] = INFINITE;
dirs[i][j] = INVALID;
fbdnode[i][j] = FALSE;
}
g_occupy[i]=FALSE;
use_corner[i]=TRUE;
mapping[i] = -1;
}
}
// generate all nodes of blocakges and stores it
// REQUIRE: a list of blockages
BOOL gen_block_node(BLOCKAGE * blockage){
int i;
for(i=0;i<block_num;i++) gen_node(&blockage->pool[i],&g_node[i*4]);
// allocate [0..static_num] to the blockage corner nodes
for(i=0;i<static_num;i++) g_occupy[i] = TRUE;
return TRUE;
}
// generate four nodes from a block
// ll=0, lr=1, ur=2, ul=3
// node: store the four nodes, must have 4 elements
// b : the block
void gen_node(BOX * b,NODE * node){
node[LL] = b->ll;
node[UR] = b->ur;
node[LR].x = b->ur.x;
node[LR].y = b->ll.y;
node[UL].x = b->ll.x;
node[UL].y = b->ur.y;
// NOTE : for each node, adjust one unit
/*
--node[LL].x;
--node[UR].y;
++node[LR].x;
--node[LR].y;
--node[UL].x;
++node[UL].y;
++node[UR].x;
++node[UR].y;
*/
}
// sort the vertical list (ascending x)
int sort_vseg(const void *v1,const void *v2){
return ((VSEG*)v1)->x - ((VSEG*)v2)->x;
}
int sort_hseg(const void *h1,const void *h2){
return ((HSEG*)h1)->y - ((HSEG*)h2)->y;
}
// generate horizontal and vertical list
// the list ensures that:
// for ver: y1<y2
// for hor: x1<x2
// NOTE : if some block has a width/height < L,
// it is not considered a block in vertical/horizontal
// REQUIRE: a list of blockage
int gen_segments(BLOCKAGE * block){
// for each block, there will be 2 vertical and 2 horizontal seg
int size = block->num * 2;
hlist = malloc(size * sizeof(HSEG));
vlist = malloc(size * sizeof(VSEG));
hfbd = malloc(size * sizeof(HSEG));
vfbd = malloc(size * sizeof(HSEG));
h_size=v_size=0;
hfbd_size = vfbd_size = 0;
int i;
for(i=0;i<block->num;i++){
BOX * pb = &block->pool[i];
int p=h_size;
int q=p+1;
// add horizontal segment
//if( ABS(pb->ll.y - pb->ur.y) > _L_ ){// height
hlist[p].y = pb->ll.y;
hlist[q].y = pb->ur.y;
hlist[p].x1 = hlist[q].x1 = pb->ll.x;
hlist[p].x2 = hlist[q].x2 = pb->ur.x;
h_size+=2;
//}
// add vertical segment
p=v_size;
q=p+1;
//if( ABS(pb->ll.x - pb->ur.x) > _L_ ){// width
vlist[p].x = pb->ll.x;
vlist[q].x = pb->ur.x;
vlist[p].y1 = vlist[q].y1 = pb->ll.y;
vlist[p].y2 = vlist[q].y2 = pb->ur.y;
v_size+=2;
//}
}
// may sort the block from left to right, low to up
qsort(vlist,v_size,sizeof(VSEG),sort_vseg);
qsort(hlist,h_size,sizeof(HSEG),sort_hseg);
return 0;
}
// copy a list of sink nodes into g_node;
void copy_sink(SINK * sink){
int i;
for(i=0;i<sink->num;i++){
sink_node[i].x = sink->pool[i].x;
sink_node[i].y = sink->pool[i].y;
mapping[i] = i;
}
}
// determine if two points are reach in the sense of manhattan distance
// REQUIRE: a list of vertical segments
// a list of horizontal segments
// RESULT : update their distance in g
// and the moving direction from a to b
// if not, return FALSE
BOOL reach(NODE a,NODE b,int idx_a,int idx_b){
// for each path, traverse all the line segments to see if
// there is intersections
// first gen the four segments:
// 2
// .---------.
// 3 | | 4
// `---------`
// 1
// there are two different manhattan paths in {14,32,31,24}
DIRECTION * a2b = &dirs[idx_a][idx_b];
DIRECTION * b2a = &dirs[idx_b][idx_a];
HSEG hor1,hor2;
VSEG ver3,ver4;
// NOTE: MIN/MAX does not consider precision
hor1.x1 = hor2.x1 = ver3.x = MIN(a.x,b.x);
hor1.x2 = hor2.x2 = ver4.x = MAX(a.x,b.x);
hor1.y = ver3.y1 = ver4.y1 = MIN(a.y,b.y);
hor2.y = ver3.y2 = ver4.y2 = MAX(a.y,b.y);
// hor[0] and ver[0] is the hor-move first path
// ver[1] and hor[1] is the ver-move first path
HSEG hor[2];
VSEG ver[2];
int relative;
// determine the relative position of two points
// and decide which two paths to use
if( a.x<b.x ){// a is left to b
if( a.y<b.y ){// a is low-left to b
relative = LL;
hor[0] = hor1;
ver[0] = ver4;
ver[1] = ver3;
hor[1] = hor2;
}
else{// a is up-left to b
relative = UL;
hor[0] = hor2;
ver[0] = ver4;
ver[1] = ver3;
hor[1] = hor1;
}
}
else{// a is right to b
if( a.y<b.y ){// a is low-right to b
relative = LR;
hor[0] = hor1;
ver[0] = ver3;
ver[1] = ver4;
hor[1] = hor2;
}
else{// a is up-right to b
relative = UR;
hor[0] = hor2;
ver[0] = ver3;
ver[1] = ver4;
hor[1] = hor1;
}
}
// check if either of the path is valid
// if yes, get MHT and set direction
int i,j;
// try path 1: hor first then ver
// check if horizontal path cut by some block
for(i=0;i<v_size;i++)
if( intersect(hor[0],vlist[i]) )
goto CHECK_PATH2;
// check if it passes some forbidden vertical segment
for(i=0;i<hfbd_size;i++)
if( hor_overlap(hor[0],hfbd[i]) )
goto CHECK_PATH2;
// check if vertical path cut by some block
for(j=0;j<h_size;j++)
if( intersect(hlist[j],ver[0]) )
goto CHECK_PATH2;
// check if it passes some forbidden horizontal segment
for(j=0;j<vfbd_size;j++){
if( ver_overlap(ver[0],vfbd[j]) )
goto CHECK_PATH2;
}
// finally... check if it is inside blockages
for(i=0;i<pBlock->num;i++){
if( inBlock(&pBlock->pool[i],(void*)&hor[0],H) ||
inBlock(&pBlock->pool[i],(void*)&ver[0],V) )
goto CHECK_PATH2;
}
// path 1 succeed
// ** judge the moving directions **
switch(relative){
case LL:
*a2b = RIGHT; *b2a = DOWN;
break;
case UL:
*a2b = RIGHT; *b2a = UP;
break;
case LR:
*a2b = LEFT; *b2a = DOWN;
break;
case UR:
*a2b = LEFT; *b2a = UP;
break;
}
g[idx_a][idx_b] = g[idx_b][idx_a] = MHT(a,b);
#ifdef DEBUG
printf("UPDATE : (%lu,%lu) -> (%lu,%lu) : %lu\n",
a.x,a.y,b.x,b.y,g[idx_a][idx_b]);
#endif
goto SUCCEED;
CHECK_PATH2:
// try path 2: ver first then hor
// check if horizontal path cut by some block
for(i=0;i<v_size;i++)
if( intersect(hor[1],vlist[i]) )
goto FAIL;
// check if it passes some forbidden vertical segment
for(i=0;i<hfbd_size;i++)
if( hor_overlap(hor[1],hfbd[i]) )
goto FAIL;
// check if vertical path cut by some block
for(j=0;j<h_size;j++)
if( intersect(hlist[j],ver[1]) )
goto FAIL;
// check if it passes some forbidden horizontal segment
for(j=0;j<vfbd_size;j++)
if( ver_overlap(ver[1],vfbd[j]) )
goto FAIL;
for(i=0;i<pBlock->num;i++){
if( inBlock(&pBlock->pool[i],(void*)&hor[1],H) ||
inBlock(&pBlock->pool[i],(void*)&ver[1],V) )
goto FAIL;
}
switch(relative){
case LL:
*a2b = UP; *b2a = LEFT;
break;
case LR:
*a2b = UP; *b2a = RIGHT;
break;
case UL:
*a2b = DOWN; *b2a = LEFT;
break;
case UR:
*a2b = DOWN; *b2a = RIGHT;
break;
}
g[idx_a][idx_b] = g[idx_b][idx_a] = MHT(a,b);
#ifdef DEBUG
printf("UPDATE : (%lu,%lu) -> (%lu,%lu) : %lu\n",
a.x,a.y,b.x,b.y,g[idx_a][idx_b]);
#endif
SUCCEED:
return TRUE;
FAIL:
return FALSE;// neither succeed
}// end of reach
// allocate for available position
// return the index of in g_node
// return -1 if can not found( the g_node is full )
int allocate_node(){
// search from the starting position of sink node
int i;
//printf("static_num=%d,g_size=%d\n",static_num,g_size);
for(i=static_num;i<g_size;i++)
if( g_occupy[i] == FALSE ){
g_occupy[i] = TRUE;
return i;
}
return -1;
}
// add a point to the constructed graph
// return : the index of the point in g_node
int insertpt(NODE pt,int id){
// search for an available position
int pt_idx = allocate_node();
if(pt_idx == -1)
report_exit("Can not find available position for inserting!");
// update the graph
#ifdef DEBUG
printf("allocated:%d\n",pt_idx);
#endif
mapping[pt_idx] = id; // mark its id
int i;
g_node[pt_idx] = pt;
for(i=0;i<g_size;i++){
if( i!=pt_idx && g_occupy[i] == TRUE &&
use_corner[i] == TRUE ){
//printf("reaching:%d -> %d\n",pt_idx,i);
//BOOL result;
reach(pt,g_node[i],pt_idx,i);
//printf("RESULT = %d\n",result);
}
}
++g_num;
dirty=TRUE;
return pt_idx;
}
// remove a sink to the constructed graph
// REQUIRE : the point's index can not be index of blockage corners
// pt_idx : the index of the point in the graph(g_node)
// return : TRUE if successfully removed
BOOL removept(int pt_idx){
if( (pt_idx<static_num) || (g_occupy[pt_idx] != TRUE) )
report_exit("removept: invalid");
int i;
g_occupy[pt_idx] = FALSE;
// update the graph
for(i=0;i<g_size;i++){
g[pt_idx][i] = g[i][pt_idx] = INFINITE;
dirs[pt_idx][i] = dirs[i][pt_idx] = INVALID;
}
--g_num;
dirty=TRUE;
return TRUE;
}
// add a point to the constructed graph from blockage list `l'
// note that this function only compute's the 4*n point of blockage
// NOT regarding another point to be added
// REQUIRE : the constructed graph
// pt : the point to add
// index : this point's index
// NOTE : this function is obsolete now
void addpt(NODE pt,int index,BLOCKAGE * list){
int i,j;
NODE corners[4];
for(i=0;i<(list->num);i++){
BOX * box = &list->pool[i];
gen_node(box,corners);
for(j=0;j<4;j++){
int corner_idx = i*4+j;
BOOL result;
result = reach(pt,corners[j],index,corner_idx);
#ifdef DEBUG
if( result ) printf("(%d,%d) -> (%d,%d) added into graph\n",
pt.x,pt.y,
corners[j].x,corners[j].y);
#endif
}
}
}
// delete a point from the constructed graph from blockage list
// pt_idx : the point's index in the graph
void delpt(int pt_idx,BLOCKAGE * list){
int i;
for(i=0;i<g_size;i++){
g[pt_idx][i] = g[i][pt_idx] = INFINITE;
dirs[pt_idx][i] = dirs[i][pt_idx] = INVALID;
g[i][i] = 0;
}
}
// add the given two point into the constructed graph
// note that if they are connected, there is a one-bend manhattan path
// s,t : the two point to be added
// list : the blockages
// NOTE : this function is obsolete now
void add2pt(NODE s,NODE t,BLOCKAGE * list){
int s_idx = 4*list->num;
int t_idx = s_idx+1;
// add two point first
addpt(s,s_idx,list);
addpt(t,t_idx,list);
// update their connectivity
reach(s,t,s_idx,t_idx);
}
// initialize floyd
void init_all_pair(){
// initialize shortest_pair[0][i][j] to original graph
int i,j;
for(i=0;i<g_size;i++){
for(j=0;j<g_size;j++){
shortest_pair[0][i][j] = g[i][j];
/*
if(g[i][j] == INFINITE)
printf("%10s","-");
else
printf("%10d",shortest_pair[0][i][j]);
*/
if( (i == j) || (g[i][j] == INFINITE) )
backtrack_pair[0][i][j] = -1;
else
backtrack_pair[0][i][j] = i;
}
//printf("\n");
}
}
// wrapper for floyd
// after called, can use the variable pairs and parents.
int all_pair_shortest(){
int i = floyd();
pairs=shortest_pair[i];
parents=backtrack_pair[i];
dirty=FALSE;
return i;
}
// use floyd to compute all pair's shortest path
// RETURN : an integer to indicate which array to use
int floyd(){
init_all_pair();
// p for current updating one, q for previous one
int p=1,q=0;
int i,j,k;
UINT *** d = shortest_pair; // for simplication
int *** bt = backtrack_pair; // NOT SURE whether there is problem
for(k=0;k<g_size;k++){
if( g_occupy[k] == FALSE ) continue;
for(i=0;i<g_size;i++){
// skip if not occupied by any node
if( g_occupy[i] == FALSE ) continue;
for(j=0;j<g_size;j++){
if( g_occupy[j] == FALSE ) continue;
UINT usep = d[q][i][k]+d[q][k][j];
if( d[q][i][j] <= usep ){
d[p][i][j] = d[q][i][j];
bt[p][i][j] = bt[q][i][j];
}
else{
d[p][i][j] = usep;
bt[p][i][j] = bt[q][k][j];
}
}
}
// IMPORTANT: update switch
q=p;
p=(p+1)%2;
}
return q;
}
// initialize the shortest distance,via,mark vector from source point
void init_single_source(int src_idx){
int i;
for(i=0;i<g_size;i++){
shortest[i] = INFINITE;
via[i] = -1;
mark[i] = FALSE;
shortest[i] = g[src_idx][i];
if( i!=src_idx && shortest[i] != INFINITE )
via[i] = src_idx;
}
mark[src_idx] = TRUE;
}
// for a single source shortest path tree
// find each node's representative element
// i.e. the one which parents are source node
int zip_path(int i){
if(disjoint_parent[i] == -1 ) return i;
else return (disjoint_parent[i]= zip_path(disjoint_parent[i]));
}
// after calling dijkstra, need to update that node's
// symetric entry in floyd's distance matrix and parent matrix
void update_dist(BLOCKAGE * list, int src){
// first calculate SSSP
dijkstra(list,src);
// update the pairs, parents;
int i;
for(i=0;i<g_size;i++){
pairs[src][i]=pairs[i][src]=shortest[i];
parents[src][i] = via[i];
}
// init
//memset(disjoint_parent,-1,sizeof(int)*g_size);
//memset(disjoint_height,0,sizeof(int)*g_size);
for(i=0;i<g_size;i++){
if( g_occupy[i] == FALSE ) continue;
if( parents[src][i] == src )
disjoint_parent[i] = -1;
else
disjoint_parent[i] = parents[src][i];
}
for(i=0;i<g_size;i++){
if( g_occupy[i] == FALSE ) continue;
zip_path(i);
}
// copy to parents[i][src]
for(i=0;i<g_size;i++){
if( g_occupy[i] == FALSE ) continue;
if( disjoint_parent[i] == -1 )
parents[i][src] = i;
else
parents[i][src] = disjoint_parent[i];
}
dirty=FALSE;
}
/*
void disjoint_merge(int i,int j){
i=find(i);
j=find(j);
if( disjoint_height[i] > disjoint_height[j])
disjoint_parent[j] = i;
else{
disjoint_parent[i] = j;
if( disjoint_height[i] == disjoint_height[j] )
disjoint_height[j]++;
}
}
*/
// find the shortest path using dijkstra algorithm
// the result is stored shortest path vector and backtrack vector
// REQUIRE: the g has been constructed, with g_num node
// RETURN :
void dijkstra(BLOCKAGE * list,int src_idx){
init_single_source(src_idx);
int i,j;
for(i=0;i<g_num-1;i++){// iteratively add (g_num-1) point
UINT now_dist = INFINITE;
int index = -1;
for(j=0;j<g_size;j++){// search next point to add
if( g_occupy[j] == TRUE && // skip empty position in g_node
mark[j] != TRUE &&
shortest[j] < now_dist ){
now_dist = shortest[j];
index = j;
}
}
if( index == -1 ){ // the graph is not connected?
return;
// printf("%d %d",g_node[src_idx].x,g_node[src_idx].y);
report_exit("Cannot find node to connect in dijkstra");
}