-
Notifications
You must be signed in to change notification settings - Fork 0
/
voronoi-pageseg.cc
executable file
·1781 lines (1535 loc) · 62.9 KB
/
voronoi-pageseg.cc
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
/*
$Date: 1999/10/15 12:40:27 $
$Revision: 1.1.1.1 $
$Author: kise $
main.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "defs.h"
#include "const.h"
#include "function.h"
#include <limits.h>
namespace voronoi{
#define LINE_C 192 // blue color in range 0-255
#define WIDTH 5
BlackPixel *bpx; /* Coordinates of black pixels and their labels */
Shape *shape;
Zone *zone;
Neighborhood *neighbor; /* Characteristic quantity between adjacent connected components */
LineSegment *lineseg; /* Coordinates and labels of start and end points */
LineSegment *lineseg_edge;
HashTable *hashtable[M1+M2];
/* Hash table for labels of adjacent connected components */
EndPoint *endp; /* End point of line segment */
NumPixel BPnbr; /* Number of black pixels */
Label LABELnbr; /* Number of connected components */
unsigned int NEIGHnbr; /* Number of adjacent connected component sets */
unsigned int LINEnbr; /* Number of line segments before removal Voronoi side */
unsigned int ZONEnbr;
unsigned int Enbr; /* Number of connected component sets from which Voronoi sides are removed */
long SiteMax; /* Maximum number of Voronoi points */
int noise_max = NOISE_MAX; /* Number of pixels of connected component to remove */
int sample_rate = SAMPLE_RATE; /* Sampling with boundary tracking */
/* Percentage */
float freq_rate = FREQ_RATE;
int Ta = Ta_CONST;
int Ts = Ts_CONST;
unsigned int sample_pix; /* Pictures obtained by sampling */
/* A prime number */
unsigned int point_edge; /* Point Voronoi number of sides */
unsigned int edge_nbr; /* Area after removal Voronoi side */
/* Number of line segments */
int *area; /* Label in the area of the connected component attached */
// Modification by Faisal Shafait
// keep track of noise components to remove them
// from the output image
bool *noise_comp;
unsigned int nconcomp_inc=50;
unsigned int nconcomp_size=0;
// End of Modification
#ifdef TIME
float b_s_time=0;
float v_time=0;
float e_time=0;
float o_time=0;
clock_t start, end;
#endif /* TIME */
float xmin, xmax, ymin, ymax, deltax, deltay;
struct Site *sites;
int nsites;
int siteidx;
int sqrt_nsites;
int nvertices;
struct Freelist sfl;
struct Site *bottomsite;
int nedges;
struct Freelist efl;
struct Freelist hfl;
struct Halfedge *ELleftend, *ELrightend;
int ELhashsize;
struct Halfedge **ELhash;
int PQhashsize;
struct Halfedge *PQhash;
int PQcount;
int PQmin;
/* Äɲõ¡Ç½ÍÑ */
int smwind = SMWIND;
/* ÄɲÃʬ */
char output_points = NO;
char output_pvor = NO;
char output_avor = NO;
char display_parameters = NO;
float max ( float a, float b ) { return a > b ? a : b; }
float min ( float a, float b ) { return a < b ? a : b; }
void printProgress (double percentage)
{
int val = (int) (percentage * 100);
int lpad = (int) (percentage * PBWIDTH);
int rpad = PBWIDTH - lpad;
printf ("\r\b%3d%% [%.*s%*s]", val, lpad, PBSTR, rpad, "");
fflush (stdout);
}
long getMemoryUsage()
{
struct rusage usage;
if(0 == getrusage(RUSAGE_SELF, &usage))
return usage.ru_maxrss; // kilobytes
else
return 0;
}
/* Function to remove duplicates from a
unsorted linked list */
void removeDuplicates(struct CC *start)
{
struct CC *ptr1, *ptr2, *dup;
ptr1 = start;
/* Pick elements one by one */
while (ptr1 != NULL && ptr1->next != NULL)
{
ptr2 = ptr1;
/* Compare the picked element with rest
of the elements */
while (ptr2->next != NULL)
{
/* If duplicate then delete it */
if (ptr1->lab == ptr2->next->lab)
{
/* sequence of steps is important here */
dup = ptr2->next;
ptr2->next = ptr2->next->next;
delete(dup);
}
else /* This is tricky */
ptr2 = ptr2->next;
}
ptr1 = ptr1->next;
}
}
// A utility function to create a new Min Heap Node
struct MinHeapNode* newMinHeapNode(int v, int dist)
{
struct MinHeapNode* minHeapNode =
(struct MinHeapNode*) myalloc(sizeof(struct MinHeapNode));
minHeapNode->v = v;
minHeapNode->dist = dist;
return minHeapNode;
}
// A utility function to create a Min Heap
struct MinHeap* createMinHeap(int capacity)
{
struct MinHeap* minHeap =
(struct MinHeap*) myalloc(sizeof(struct MinHeap));
minHeap->pos = (int *)myalloc(capacity * sizeof(int));
minHeap->size = 0;
minHeap->capacity = capacity;
minHeap->array =
(struct MinHeapNode**) myalloc(capacity * sizeof(struct MinHeapNode*));
return minHeap;
}
// A utility function to swap two nodes of min heap. Needed for min heapify
void swapMinHeapNode(struct MinHeapNode** a, struct MinHeapNode** b)
{
struct MinHeapNode* t = *a;
*a = *b;
*b = t;
}
// A standard function to heapify at given idx
// This function also updates position of nodes when they are swapped.
// Position is needed for decreaseKey()
void minHeapify(struct MinHeap* minHeap, int idx)
{
int smallest, left, right;
smallest = idx;
left = 2 * idx + 1;
right = 2 * idx + 2;
if (left < minHeap->size &&
minHeap->array[left]->dist < minHeap->array[smallest]->dist )
smallest = left;
if (right < minHeap->size &&
minHeap->array[right]->dist < minHeap->array[smallest]->dist )
smallest = right;
if (smallest != idx)
{
// The nodes to be swapped in min heap
MinHeapNode *smallestNode = minHeap->array[smallest];
MinHeapNode *idxNode = minHeap->array[idx];
// Swap positions
minHeap->pos[smallestNode->v] = idx;
minHeap->pos[idxNode->v] = smallest;
// Swap nodes
swapMinHeapNode(&minHeap->array[smallest], &minHeap->array[idx]);
minHeapify(minHeap, smallest);
}
}
// A utility function to check if the given minHeap is ampty or not
int isEmpty(struct MinHeap* minHeap)
{
return minHeap->size == 0;
}
// Standard function to extract minimum node from heap
struct MinHeapNode* extractMin(struct MinHeap* minHeap)
{
if (isEmpty(minHeap))
return NULL;
// Store the root node
struct MinHeapNode* root = minHeap->array[0];
// Replace root node with last node
struct MinHeapNode* lastNode = minHeap->array[minHeap->size - 1];
minHeap->array[0] = lastNode;
// Update position of last node
minHeap->pos[root->v] = minHeap->size-1;
minHeap->pos[lastNode->v] = 0;
// Reduce heap size and heapify root
--minHeap->size;
minHeapify(minHeap, 0);
return root;
}
// Function to decreasy dist value of a given vertex v. This function
// uses pos[] of min heap to get the current index of node in min heap
void decreaseKey(struct MinHeap* minHeap, int v, int dist)
{
// Get the index of v in heap array
int i = minHeap->pos[v];
// Get the node and update its dist value
minHeap->array[i]->dist = dist;
// Travel up while the complete tree is not hepified.
// This is a O(Logn) loop
while (i && minHeap->array[i]->dist < minHeap->array[(i - 1) / 2]->dist)
{
// Swap this node with its parent
minHeap->pos[minHeap->array[i]->v] = (i-1)/2;
minHeap->pos[minHeap->array[(i-1)/2]->v] = i;
swapMinHeapNode(&minHeap->array[i], &minHeap->array[(i - 1) / 2]);
// move to parent index
i = (i - 1) / 2;
}
}
// A utility function to check if a given vertex
// 'v' is in min heap or not
bool isInMinHeap(struct MinHeap *minHeap, int v)
{
bool DEBUG = false;
if(DEBUG) printf("[isInMinHeap] Start...\n");
if(DEBUG) printf("[isInMinHeap] minHeap->pos[%d]...\n",v);
if (minHeap->pos[v] < minHeap->size)
return true;
return false;
}
// A utility function used to print the solution
void printArr(int dist[], int n)
{
printf("Vertex Distance from Source\n");
for (int i = 0; i < n; ++i)
printf("%d \t\t %d\n", i, dist[i]);
}
//
// for a
// for b
// comp(a,b)
bool sameZone(Zone query)
{
bool DEBUG = false;
bool sameLen = false;
struct AdjListNode* zCrawl;
struct AdjListNode* qCrawl;
if(DEBUG){
printf("**CURR ZONE BOARD**\n");
for(int i=0 ; i<ZONEnbr ; i++)
{
zCrawl = zone[i].head;
printf("zone[%d] \n head \n",i);
while(zCrawl)
{
printf("-> %d ",zCrawl->lineseg_idx);
zCrawl = zCrawl->next;
}
printf("\n");
}
}
for(int i=0 ; i<ZONEnbr-1 ; i++)
{
zCrawl = zone[i].head;
qCrawl = query.head;
if(DEBUG) printf("\t[sameZone] comparing zone[%d].len:%d ... query.len:%d\n",i,zone[i].len,query.len);
if(zone[i].len != query.len)
continue;
else{
// Loop query sequence
while(qCrawl)
{
if(DEBUG) printf("\t\t[sameZone] qCrawl:%d to ",qCrawl->lineseg_idx);
zCrawl = zone[i].head;
bool newSeq = true;
// Loop previously found zone's lineseg sequence
// if newly found zone's sequence contains at least one new lineseg, return False.
while(zCrawl)
{
if(DEBUG) printf("%d ",zCrawl->lineseg_idx);
if(qCrawl->lineseg_idx==zCrawl->lineseg_idx)
{
if(DEBUG) printf("... exist! \n",zCrawl->lineseg_idx,i);
newSeq = false;
break;
}
zCrawl = zCrawl->next;
}
// Sequence is found, early termination.
if(qCrawl->next==NULL and newSeq==false){
return true;
}
qCrawl = qCrawl->next;
}
}
}
if(DEBUG) printf("\t\t[sameZone] no same sequence is found ... return false\n");
return false;
}
void assignZone(struct Graph* graph, int dist[], int path[], int n, int src, int tar, int lineseg_idx_src_tar)
{
bool DEBUG = false;
if(DEBUG) printf("[assignZone] src:%d tar:%d\n",src,tar);
int parent = tar;
int lineseg_idx;
struct AdjListNode* pCrawl;
struct AdjListNode* newNode;
struct CC* newCC;
// Get path[tar-...]
while(path[parent]!=src)
{
pCrawl = graph->array[parent].head;
while(pCrawl)
{
if(pCrawl->dest==path[parent])
{
// path is found!
lineseg_idx = pCrawl->lineseg_idx;
break;
}
pCrawl = pCrawl->next;
}
if(DEBUG) printf("[tar-...] lineseg[%d](lab:%d-lab:%d): %d -> %d \n",lineseg_idx,lineseg[lineseg_idx].lab1,lineseg[lineseg_idx].lab2,parent,path[parent]);
newNode = newAdjListNode(-2,lineseg_idx,-2);
newNode->next = zone[ZONEnbr].head;
zone[ZONEnbr].head = newNode;
zone[ZONEnbr].len++;
newCC = (struct CC*) myalloc(sizeof(struct CC));
newCC->lab = lineseg[lineseg_idx].lab1;
newCC->next = zone[ZONEnbr].cc_head;
zone[ZONEnbr].cc_head = newCC;
newCC = (struct CC*) myalloc(sizeof(struct CC));
newCC->lab = lineseg[lineseg_idx].lab2;
newCC->next = zone[ZONEnbr].cc_head;
zone[ZONEnbr].cc_head = newCC;
parent = path[parent];
}
// Get path[...-src]
pCrawl = graph->array[parent].head;
while(pCrawl)
{
if(pCrawl->dest==path[parent])
{
lineseg_idx = pCrawl->lineseg_idx;
// path is found!
break;
}
pCrawl = pCrawl->next;
}
if(DEBUG) printf("[...-src] lineseg[%d](lab:%d-lab:%d): %d -> %d \n",lineseg_idx,lineseg[lineseg_idx].lab1,lineseg[lineseg_idx].lab2,parent,path[parent]);
newNode = newAdjListNode(-2,lineseg_idx,-2);
newNode->next = zone[ZONEnbr].head;
zone[ZONEnbr].head = newNode;
zone[ZONEnbr].len++;
newCC = (struct CC*) myalloc(sizeof(struct CC));
newCC->lab = lineseg[lineseg_idx].lab1;
newCC->next = zone[ZONEnbr].cc_head;
zone[ZONEnbr].cc_head = newCC;
newCC = (struct CC*) myalloc(sizeof(struct CC));
newCC->lab = lineseg[lineseg_idx].lab2;
newCC->next = zone[ZONEnbr].cc_head;
zone[ZONEnbr].cc_head = newCC;
// Get path[src-tar]
if(DEBUG) printf("[src-tar] lineseg[%d](lab:%d-lab:%d): %d -> %d \n",lineseg_idx_src_tar,lineseg[lineseg_idx_src_tar].lab1,lineseg[lineseg_idx_src_tar].lab2,src,tar);
newNode = newAdjListNode(-2,lineseg_idx_src_tar,-2);
newNode->next = zone[ZONEnbr].head;
zone[ZONEnbr].head = newNode;
zone[ZONEnbr].len++;
newCC = (struct CC*) myalloc(sizeof(struct CC));
newCC->lab = lineseg[lineseg_idx_src_tar].lab1;
newCC->next = zone[ZONEnbr].cc_head;
zone[ZONEnbr].cc_head = newCC;
newCC = (struct CC*) myalloc(sizeof(struct CC));
newCC->lab = lineseg[lineseg_idx_src_tar].lab2;
newCC->next = zone[ZONEnbr].cc_head;
zone[ZONEnbr].cc_head = newCC;
ZONEnbr++;
if(DEBUG) printf("Found %d ZONE(s) \n\n",ZONEnbr-1);
if(DEBUG) printf("NEXT ZONE NBR:%d \n",ZONEnbr);
if(ZONEnbr>1 and sameZone(zone[ZONEnbr-1]))
{
if(DEBUG) printf("\t[WARNING] This zone seems to be visited ... so deleting lastly added zone...\n");
zone[ZONEnbr-1].head = NULL;
zone[ZONEnbr-1].len = 0;
ZONEnbr--;
}
}
// Given three colinear points p, q, r, the function checks if
// point q lies on line segment 'pr'
bool onSegment(Point p, Point q, Point r)
{
if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y))
return true;
return false;
}
// To find orientation of ordered triplet (p, q, r).
// The function returns following values
// 0 --> p, q and r are colinear
// 1 --> Clockwise
// 2 --> Counterclockwise
int orientation(Point p, Point q, Point r)
{
int val = (q.y - p.y) * (r.x - q.x) -
(q.x - p.x) * (r.y - q.y);
if (val == 0) return 0; // colinear
return (val > 0)? 1: 2; // clock or counterclock wise
}
// The function that returns true if line segment 'p1q1'
// and 'p2q2' intersect.
bool doIntersect(Point p1, Point q1, Point p2, Point q2)
{
bool DEBUG = false;
// Find the four orientations needed for general and
// special cases
int o1 = orientation(p1, q1, p2);
int o2 = orientation(p1, q1, q2);
int o3 = orientation(p2, q2, p1);
int o4 = orientation(p2, q2, q1);
// General case
if (o1 != o2 && o3 != o4) {
if(DEBUG) printf("[doIntersect] general case ... p1q1:(%f,%f)-(%f,%f) p2q2:(%f,%f)-(%f,%f)\n",p1.x,p1.y,q1.x,q1.y,p2.x,p2.y,q2.x,q2.y);
return true;
}
// Special Cases
// p1, q1 and p2 are colinear and p2 lies on segment p1q1
if (o1 == 0 && onSegment(p1, p2, q1)){
if(DEBUG) printf("[doIntersect] colinear ... (%f,%f) lies on segment (%f,%f)-(%f,%f)\n",p2.x,p2.y,p1.x,p1.y,q1.x,q1.y);
return true;
}
// p1, q1 and p2 are colinear and q2 lies on segment p1q1
if (o2 == 0 && onSegment(p1, q2, q1)){
if(DEBUG) printf("[doIntersect] colinear ... (%f,%f) lies on segment (%f,%f)-(%f,%f)\n",q2.x,q2.y,p1.x,p1.y,q1.x,q1.y);
return true;
}
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
if (o3 == 0 && onSegment(p2, p1, q2)){
if(DEBUG) printf("[doIntersect] colinear ... (%f,%f) lies on segment (%f,%f)-(%f,%f)\n",p1.x,p1.y,p2.x,p2.y,q2.x,q2.y);
return true;
}
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
if (o4 == 0 && onSegment(p2, q1, q2)){
if(DEBUG) printf("[doIntersect] colinear ... (%f,%f) lies on segment (%f,%f)-(%f,%f)\n",q1.x,q1.y,p2.x,p2.y,q2.x,q2.y);
return true;
}
if(DEBUG) printf("[doIntersect] none ... p1q1:(%f,%f)-(%f,%f) p2q2:(%f,%f)-(%f,%f)\n",p1.x,p1.y,q1.x,q1.y,p2.x,p2.y,q2.x,q2.y);
return false; // Doesn't fall in any of the above cases
}
// Returns true if the point p lies inside the polygon[] with n vertices
bool isInside(Point* polygon, int n, Point p)
{
bool DEBUG = false;
// There must be at least 3 vertices in polygon[]
if (n < 3) return false;
// Create a point for line segment from p to infinite
Point extreme = {IMG_IMAX, p.y};
// Count intersections of the above line with sides of polygon
int count = 0, i = 0;
do
{
int next = (i+1)%n;
// Check if the line segment from 'p' to 'extreme' intersects
// with the line segment from 'polygon[i]' to 'polygon[next]'
if (doIntersect(*(polygon+i), *(polygon+next), p, extreme))
{
// If the point 'p' is colinear with line segment 'i-next',
// then check if it lies on segment. If it lies, return true,
// otherwise false
if (orientation(*(polygon+i), p, *(polygon+next)) == 0)
return onSegment(*(polygon+i), p, *(polygon+next));
if(DEBUG) printf("[isInside] intersect!\n");
count++;
}
i = next;
} while (i != 0);
// Return true if count is odd, false otherwise
return count&1; // Same as (count%2 == 1)
}
void CountCCsInZone(int zone_idx, Point p)
{
bool DEBUG = false;
/*
int i = 0;
int n = 4;//zone[zone_idx].len;
Point p = {5, 5};
Point* polygon = (Point*) malloc(n * sizeof(Point));
(polygon+i)->x = 0;
(polygon+i)->y = 0;
i++;
(polygon+i)->x = 0;
(polygon+i)->y = 10;
i++;
(polygon+i)->x = 10;
(polygon+i)->y = 10;
i++;
(polygon+i)->x = 10;
(polygon+i)->y = 0;
i++;
*/
/*
int i = 0;
int n = 3;
Point p = {4, 4};
Point* polygon = (Point*) malloc(n * sizeof(Point));
(polygon+i)->x = 0;
(polygon+i)->y = 0;
i++;
(polygon+i)->x = 5;
(polygon+i)->y = 5;
i++;
(polygon+i)->x = 5;
(polygon+i)->y = 0;
*/
int i =0;
int n = zone[zone_idx].len;
int s_or_e = -1; // 0:start 1:end
//Point p = {215, 168};
Point* polygon = (Point*) malloc(n * sizeof(Point));
struct AdjListNode* pCrawl = zone[zone_idx].head;
if(DEBUG) printf("\n[CountCCsInZone] point ... (%f,%f)\n",p.x,p.y);
while(pCrawl)
{
if(DEBUG) printf("[CountCCsInZone] lineseg[%d] (%d,%d), (%d,%d)\n",pCrawl->lineseg_idx,lineseg[pCrawl->lineseg_idx].xs,lineseg[pCrawl->lineseg_idx].ys,lineseg[pCrawl->lineseg_idx].xe,lineseg[pCrawl->lineseg_idx].ye);
// To prevent segment fault
if(pCrawl->next != NULL)
{
// case 1: curr_lineseg.start == next_lineseg.start or next_lineseg.end
if((lineseg[pCrawl->lineseg_idx].xs==lineseg[pCrawl->next->lineseg_idx].xs
and lineseg[pCrawl->lineseg_idx].ys==lineseg[pCrawl->next->lineseg_idx].ys)
or
(lineseg[pCrawl->lineseg_idx].xs==lineseg[pCrawl->next->lineseg_idx].xe
and lineseg[pCrawl->lineseg_idx].ys==lineseg[pCrawl->next->lineseg_idx].ye))
{
s_or_e = 0;
}
// case 2: curr_lineseg.end == next_lineseg.start or next_lineseg.end
else
{
s_or_e = 1;
}
}
// case 3: special case: last lineseg.
if(i==n-1)
{
(polygon+i)->x = lineseg[pCrawl->lineseg_idx].xs;
(polygon+i)->y = lineseg[pCrawl->lineseg_idx].ys;
i++;
(polygon+i)->x = lineseg[pCrawl->lineseg_idx].xe;
(polygon+i)->y = lineseg[pCrawl->lineseg_idx].ye;
i++;
/*
(polygon+i)->x = 123;
(polygon+i)->y = 123;
i++;
(polygon+i)->x = 123;
(polygon+i)->y = 123;
i++;
*/
}
else
{
if(s_or_e==0)
{
(polygon+i)->x = lineseg[pCrawl->lineseg_idx].xs;
(polygon+i)->y = lineseg[pCrawl->lineseg_idx].ys;
i++;
}
else if (s_or_e==1)
{
(polygon+i)->x = lineseg[pCrawl->lineseg_idx].xe;
(polygon+i)->y = lineseg[pCrawl->lineseg_idx].ye;
i++;
}
}
pCrawl = pCrawl->next;
}
if(DEBUG)
{
printf("[CountCCsInZone] polygon ... \n");
for(int i=0 ; i<n ; i++)
{
printf("[CountCCsInZone] (%f,%f)\n",(polygon+i)->x,(polygon+i)->y);
}
}
if(isInside(polygon, n, p))
{
if(DEBUG) printf("Yes \n");
zone[zone_idx].numOfCCs++;
}
else
{
if(DEBUG) printf("No \n");
}
}
// The function that checking if there are more than 3 Voronoi-edges (lineseg) attached to a single Voronoi-point (site).
bool isForked(struct Graph* graph, int src, int tar)
{
int numEdges = 0;
struct AdjListNode* pCrawl = graph->array[src].head;
while(pCrawl)
{
numEdges++;
pCrawl = pCrawl->next;
}
if(numEdges<3)
return false;
else
return true;
}
// The main function that calulates distances of shortest paths from src to all
// vertices. It is a O(ELogV) function
void dijkstra(struct Graph* graph, int src, int tar, int lineseg_idx)
{
bool DEBUG = false;
int V = graph->V;// Get the number of vertices in graph
int dist[V]; // dist values used to pick minimum weight edge in cut
int path[V];
// minHeap represents set E
struct MinHeap* minHeap = createMinHeap(V);
// Initialize min heap with all vertices. dist value of all vertices
if(DEBUG) printf("[dijstra] START...\n");
for (int v = 0; v < V; ++v)
{
dist[v] = INT_MAX;
minHeap->array[v] = newMinHeapNode(v, dist[v]);
minHeap->pos[v] = v;
}
if(DEBUG) printf("[dijstra] minHeap is initialized...\n");
// Make dist value of src vertex as 0 so that it is extracted first
minHeap->array[src] = NULL;
minHeap->array[src] = newMinHeapNode(src, dist[src]);
minHeap->pos[src] = src;
dist[src] = 0;
path[src] = src;
decreaseKey(minHeap, src, dist[src]);
// Initially size of min heap is equal to V
minHeap->size = V;
if(DEBUG) printf("[dijstra] src vertex is initialized...\n");
// In the followin loop, min heap contains all nodes
// whose shortest distance is not yet finalized.
while (!isEmpty(minHeap))
{
if(DEBUG) printf("[dijstra] outter while loop...\n");
// Extract the vertex with minimum distance value
struct MinHeapNode* minHeapNode = extractMin(minHeap);
int u = minHeapNode->v; // Store the extracted vertex number
// Traverse through all adjacent vertices of u (the extracted
// vertex) and update their distance values
struct AdjListNode* pCrawl = graph->array[u].head;
while (pCrawl != NULL)
{
int v = pCrawl->dest;
if(DEBUG) printf("[dijstra] u:%d - v:%d ...\n",u,v);
// If shortest distance to v is not finalized yet, and distance to v
// through u is less than its previously calculated distance
if (isInMinHeap(minHeap, v) && dist[u] != INT_MAX &&
pCrawl->weight + dist[u] < dist[v])
{
if(DEBUG) printf("[dijstra] inner while loop...1\n");
dist[v] = dist[u] + pCrawl->weight;
//printf("v:%d ... u:%d\n",v,u);
path[v] = u;
// update distance value in min heap also
decreaseKey(minHeap, v, dist[v]);
}
pCrawl = pCrawl->next;
}
//free(minHeapNode);
//free(pCrawl);
}
// print the calculated shortest distances
if(DEBUG) printf("[dijstra] shorest paths are found...\n");
if(DEBUG) printArr(dist, V);
/*
printf("\n\nPATH:\n");
for (int q=0 ; q<V ; q++){
printf("\t%d \t\t %d\n",q,path[q]);
}
*/
if(dist[tar]!=INT_MAX){
assignZone(graph,dist,path,V,src,tar,lineseg_idx);
return;
}
else
if(DEBUG) printf("From %d to %d is unreachable.\n",src,tar);
/*
for (int v = 0; v < V; ++v)
free(minHeap->array[v]);
*/
// Also might need to free minHeap->array[src] ... but it gives an error somehow...
/*
free(minHeap->array);
free(minHeap->pos);
free(minHeap);
*/
}
// A utility function to create a new adjacency list node
struct AdjListNode* newAdjListNode(int dest, int lineseg_idx, int weight)
{
struct AdjListNode* newNode =
(struct AdjListNode*) myalloc(sizeof(struct AdjListNode));
newNode->dest = dest;
newNode->weight = weight;
newNode->lineseg_idx = lineseg_idx;
newNode->next = NULL;
return newNode;
}
// A utility function that creates a graph of V vertices
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) myalloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of
// array will be V
graph->array =
(struct AdjList*) myalloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by
// making head as NULL
int i;
for (i = 0; i < V; ++i)
graph->array[i].head = NULL;
return graph;
}
// A utility function to print the adjacency list
// representation of graph
void printGraph(struct Graph* graph)
{
int v;
for (v = 0; v < graph->V; ++v)
{
struct AdjListNode* pCrawl = graph->array[v].head;
printf("\n Adjacency list of vertex %d \n head ", v);
while (pCrawl)
{
printf("-> %d (lineseg:%d)", pCrawl->dest,pCrawl->lineseg_idx);
pCrawl = pCrawl->next;
}
printf("\n");
}
}
// Adds an edge to an undirected graph
void addEdge(struct Graph* graph, int src, int dest, int lineseg_idx, int weight)
{
// Add an edge from src to dest. A new node is
// added to the adjacency list of src. The node
// is added at the begining
struct AdjListNode* newNode = newAdjListNode(dest,lineseg_idx,weight);
newNode->next = graph->array[src].head;
graph->array[src].head = newNode;
// Since graph is undirected, add an edge from
// dest to src also
newNode = newAdjListNode(src,lineseg_idx,weight);
newNode->next = graph->array[dest].head;
graph->array[dest].head = newNode;
}
void delEdge(struct Graph* graph, int src, int dest)
{
bool DEBUG = false;
struct AdjListNode* pCrawl;
struct AdjListNode* temp;
struct AdjListNode* pDebug;
// unlink src-dest
pCrawl = graph->array[src].head;
if(DEBUG){
printf("[delEdge] src:%d dest:%d\n",src,dest);
//printf("[delEdge] \tpCrawl->next->dest:%d pCrawl->dest:%d\n",pCrawl->next->dest,pCrawl->dest);
pDebug = graph->array[src].head;
printf("[delEdge] BEFORE DELETION ... graph[%d].head ",src);
while(pDebug)
{
printf("-> %d ",pDebug->dest);
pDebug = pDebug->next;
}
printf("\n");
}
while (pCrawl)
{
if (DEBUG) printf("[delEdge] \tpCrawl: %d\n",pCrawl->dest);
if(graph->array[src].head->dest==dest){
// hmm... should I free something here?
temp = graph->array[src].head;
graph->array[src].head = graph->array[src].head->next;
//free(temp);
//graph->array[src].head = graph->array[src].head->next;
if (DEBUG) printf("[delEdge] \tdeleted.\n");
break;
}
else if (pCrawl->next->dest==dest){
pCrawl->next = pCrawl->next->next;
temp = pCrawl->next;
//free(temp);
if (DEBUG) printf("[delEdge] \tdeleted.\n");
break;
}
else{
if (DEBUG) printf("[delEdge] \tgetNext()\n");
pCrawl = pCrawl->next;
//free(temp);
}
}
if(DEBUG){
pDebug = graph->array[src].head;
printf("[delEdge] AFTER DELETION ... graph[%d].head ",src);
while(pDebug)
{
printf("-> %d ",pDebug->dest);
pDebug = pDebug->next;
}
printf("\n");
}
}
int compare_struct_x(const void *s1, const void *s2)
{
struct Site *_s1 = (struct Site *)s1;
struct Site *_s2 = (struct Site *)s2;
/*
// sort by x coordinate
if(xory=='x')
return _s1->coord.x - _s2->coord.x;
else
return _s1->coord.y - _s2->coord.y;
*/
return _s1->coord.x - _s2->coord.x;
}
int compare_struct_y(const void *s1, const void *s2)
{
struct Site *_s1 = (struct Site *)s1;
struct Site *_s2 = (struct Site *)s2;
/*
// sort by x coordinate
if(xory=='x')
return _s1->coord.x - _s2->coord.x;
else
return _s1->coord.y - _s2->coord.y;
*/
return _s1->coord.y - _s2->coord.y;
}
void voronoi_pageseg(LineSegment **mlineseg,
unsigned int *nlines,
ImageData *imgd1) {
bool DEBUG = false;
point_edge = 0;
edge_nbr = 0;
BPnbr = LABELnbr = NEIGHnbr = LINEnbr = Enbr = SiteMax = 0;
/* displaying parameters */
if(display_parameters == YES)
dparam();
/* Set 1 pixels surrounding image to 0 */
frame(imgd1,1,0);
/* ¹õ²èÁÇbpx ¤ÎÎΰè³ÎÊÝ */
bpx=(BlackPixel *)myalloc(sizeof(BlackPixel)* INITPIXEL);
shape=(Shape * )myalloc(sizeof(Shape)* INITPIXEL);
zone =(Zone *)myalloc(sizeof(Zone)* ZONEMAX);
for (int i = 0; i < ZONEMAX; ++i) {
zone[i].head = NULL;
zone[i].cc_head = NULL;