-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfc_new.py
2470 lines (2145 loc) · 100 KB
/
sfc_new.py
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
# Authors: Aayush Gohil, Swadhin Agrawal
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import copy as cp
import pickle as pkl
import os
from random import randint,random,sample
from shapely.geometry import LineString
sed = 19778167 # 5 spike 19778167: 38lg
# sed = 27819661 # 7 spike 27819661 38lg
np.random.seed(sed)
class Edge:
def __init__(self,l,r):
self.l_nei = None
self.r_nei = None
self.l = l
self.r = r
def get_loop(self,edges):
boundary = np.array([self.r])
if self.r_nei != None:
boundary = np.concatenate((boundary, edges[self.r_nei].get_loop(edges)),axis=0)
else:
return boundary
return boundary
class Grid:
def __init__(self,tl,bl,tr,br):
self.l_nei = None
self.t_nei = None
self.r_nei = None
self.b_nei = None
self.tl = tl
self.bl = bl
self.tr = tr
self.br = br
self.path_pre = None
self.path_post = None
self.centroid = (self.tl+self.bl+self.tr+self.br)/4.0
self.robot_home = False
self.passage = 0
self.passage_l = 0
self.passage_r = 0
self.passage_t = 0
self.passage_b = 0
self.rectangle_num = None
self.border_grid = 0
self.path_connector = None
def get_centroid(self):
self.centroid = (self.tl+self.bl+self.tr+self.br)/4.0
def get_x(self):
return np.array([self.bl[0],self.br[0],self.tr[0],self.tl[0],self.bl[0]])
def get_y(self):
return np.array([self.bl[1],self.br[1],self.tr[1],self.tl[1],self.bl[1]])
def get_corners(self):
return np.array([self.bl,self.br,self.tr,self.tl])
def Inflate_Cut_algorithm(num_vertices,g_size=10,ax=None):
'''
Input: Num_vertices (Must be even and >=4)
Returns: Array of vertices forming the random simple rectilinear region
'''
def polygon_boundary(cells,ax):
def get_edge(cell,side,one_vertex = 1):
if side == 't':
if one_vertex:
return [list(cell.tl)]
else:
return [list(cell.tr),list(cell.tl)]
elif side == 'b':
if one_vertex:
return [list(cell.br)]
else:
return [list(cell.bl),list(cell.br)]
elif side == 'r':
if one_vertex:
return [list(cell.tr)]
else:
return [list(cell.br),list(cell.tr)]
elif side == 'l':
if one_vertex:
return [list(cell.bl)]
else:
return [list(cell.tl),list(cell.bl)]
grids = cp.copy(cells)
if len(grids) == 1:
boundary = None
for g in grids:
boundary = grids[g].get_corners()
boundary = np.insert(boundary,len(boundary),boundary[0],axis=0)
return boundary
delete_these = []
for i in grids:
marker = [0,0,0,0] # l,r,t,b
if grids[i].l_nei is not None:
marker[0] = 1
if grids[i].r_nei is not None:
marker[1] = 1
if grids[i].t_nei is not None:
marker[2] = 1
if grids[i].b_nei is not None:
marker[3] = 1
if np.sum(marker) == 4:
if grids[grids[i].t_nei].l_nei is not None and grids[grids[i].t_nei].r_nei is not None and grids[grids[i].b_nei].l_nei is not None and grids[grids[i].b_nei].r_nei is not None:
delete_these.append(i)
for i in delete_these:
del grids[i]
# for g in grids:
# x = grids[g].get_x()
# y = grids[g].get_y()
# ax.plot(x,y,color='maroon')
# plt.show()
boundary = []
edges = {}
g = list(grids.items())[0][0]
done = []
start_edge = None
end_edge = None
backup = []
while True:
# x = grids[g].get_x()
# y = grids[g].get_y()
# ax.plot(x,y,color='yellow')
# ax.scatter(grids[g].centroid[0],grids[g].centroid[1])
# plt.show()
marker = [0,0,0,0] # ['r','t','l','b']
if grids[g].l_nei is not None:
marker[2] = 1
if grids[g].r_nei is not None:
marker[0] = 1
if grids[g].t_nei is not None:
marker[1] = 1
if grids[g].b_nei is not None:
marker[3] = 1
sides = ['r','t','l','b']
if len(boundary)==0 or (g != list(grids.items())[0][0] and g not in done):
for s in range(4):
if marker[s]==0:
edge = get_edge(grids[g],sides[s],one_vertex=0)
if len(edges) == 0:
start_edge = str(len(edges))
end_edge = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[0]),r=np.array(edge[1]))
else:
for e in [start_edge,end_edge]:
ll = np.linalg.norm(np.array(edge[0])-edges[e].l)
lr = np.linalg.norm(np.array(edge[0])-edges[e].r)
rl = np.linalg.norm(np.array(edge[1])-edges[e].l)
rr = np.linalg.norm(np.array(edge[1])-edges[e].r)
if ll == 0:
start_edge = str(len(edges))
edges[e].l_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[1]),r=np.array(edge[0]))
edges[str(len(edges)-1)].r_nei = e
break
elif lr == 0:
end_edge = str(len(edges))
edges[e].r_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[0]),r=np.array(edge[1]))
edges[str(len(edges)-1)].l_nei = e
break
elif rl == 0:
start_edge = str(len(edges))
edges[e].l_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[0]),r=np.array(edge[1]))
edges[str(len(edges)-1)].r_nei = e
break
elif rr == 0:
end_edge = str(len(edges))
edges[e].r_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[1]),r=np.array(edge[0]))
edges[str(len(edges)-1)].l_nei = e
break
elif e == end_edge and ll!=0 and rl!=0 and lr!=0 and rr!=0:
backup.append(edge)
break
if len(backup)!=0:
for i in range(len(backup)-1,-1,-1):
edge = backup[i]
for e in [start_edge,end_edge]:
ll = np.linalg.norm(np.array(edge[0])-edges[e].l)
lr = np.linalg.norm(np.array(edge[0])-edges[e].r)
rl = np.linalg.norm(np.array(edge[1])-edges[e].l)
rr = np.linalg.norm(np.array(edge[1])-edges[e].r)
if ll == 0:
start_edge = str(len(edges))
edges[e].l_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[1]),r=np.array(edge[0]))
edges[str(len(edges)-1)].r_nei = e
del backup[i]
break
elif lr == 0:
end_edge = str(len(edges))
edges[e].r_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[0]),r=np.array(edge[1]))
edges[str(len(edges)-1)].l_nei = e
del backup[i]
break
elif rl == 0:
start_edge = str(len(edges))
edges[e].l_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[0]),r=np.array(edge[1]))
edges[str(len(edges)-1)].r_nei = e
del backup[i]
break
elif rr == 0:
end_edge = str(len(edges))
edges[e].r_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[1]),r=np.array(edge[0]))
edges[str(len(edges)-1)].l_nei = e
del backup[i]
break
boundary = edges[start_edge].get_loop(edges)
# ax.plot(np.array(boundary)[:,0],np.array(boundary)[:,1],color='blue')
# plt.show()
if len(backup)!=0:
for i in range(len(backup)-1,-1,-1):
edge = backup[i]
for e in [start_edge,end_edge]:
ll = np.linalg.norm(np.array(edge[0])-edges[e].l)
lr = np.linalg.norm(np.array(edge[0])-edges[e].r)
rl = np.linalg.norm(np.array(edge[1])-edges[e].l)
rr = np.linalg.norm(np.array(edge[1])-edges[e].r)
if ll == 0:
start_edge = str(len(edges))
edges[e].l_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[1]),r=np.array(edge[0]))
edges[str(len(edges)-1)].r_nei = e
del backup[i]
break
elif lr == 0:
end_edge = str(len(edges))
edges[e].r_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[0]),r=np.array(edge[1]))
edges[str(len(edges)-1)].l_nei = e
del backup[i]
break
elif rl == 0:
start_edge = str(len(edges))
edges[e].l_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[0]),r=np.array(edge[1]))
edges[str(len(edges)-1)].r_nei = e
del backup[i]
break
elif rr == 0:
end_edge = str(len(edges))
edges[e].r_nei = str(len(edges))
edges[str(len(edges))] = Edge(l=np.array(edge[1]),r=np.array(edge[0]))
edges[str(len(edges)-1)].l_nei = e
del backup[i]
break
done.append(g)
sequence = [-1,-1,-1,-1]
values = [grids[g].r_nei,grids[g].t_nei,grids[g].l_nei,grids[g].b_nei]
if grids[g].r_nei != None and grids[g].r_nei in done:
sequence[0] = np.where(np.array(done)==grids[g].r_nei)[0][-1]
elif grids[g].r_nei != None and grids[g].r_nei not in done:
pass
else:
sequence[0] = np.Inf
if grids[g].t_nei != None and grids[g].t_nei in done:
sequence[1] = np.where(np.array(done)==grids[g].t_nei)[0][-1]
elif grids[g].t_nei != None and grids[g].t_nei not in done:
pass
else:
sequence[1] = np.Inf
if grids[g].l_nei != None and grids[g].l_nei in done:
sequence[2] = np.where(np.array(done)==grids[g].l_nei)[0][-1]
elif grids[g].l_nei != None and grids[g].l_nei not in done:
pass
else:
sequence[2] = np.Inf
if grids[g].b_nei != None and grids[g].b_nei in done:
sequence[3] = np.where(np.array(done)==grids[g].b_nei)[0][-1]
elif grids[g].b_nei != None and grids[g].b_nei not in done:
pass
else:
sequence[3] = np.Inf
seq_ind = sequence.index(min(sequence))
g1 = values[seq_ind]
while g1 not in grids:
sequence[sequence.index(min(sequence))] = np.Inf
seq_ind = sequence.index(min(sequence))
g1 = values[seq_ind]
if g1 == grids[g].r_nei:
g = grids[g].r_nei
elif g1 == grids[g].t_nei:
g = grids[g].t_nei
elif g1 == grids[g].l_nei:
g = grids[g].l_nei
elif g1 == grids[g].b_nei:
g = grids[g].b_nei
if np.linalg.norm(edges[start_edge].l-edges[str(int(end_edge))].r)==0:
break
boundary = np.concatenate((boundary,np.array([boundary[0]])),axis=0)
# ax.clear()
# ax.plot(np.array(boundary)[:,0],np.array(boundary)[:,1],color='blue')
# plt.show()
# ax.clear()
if ax is not None:
ax.plot(boundary[:,0],boundary[:,1],color = 'black')
# ax.scatter(boundary[:,0],boundary[:,1],color = 'black')
plt.show()
return boundary, edges, start_edge, end_edge
def Cut(p,c,ax=None):
C_tr = p[str(c)].tr
boundary, _,_,_ = polygon_boundary(p,ax)
def get_neighbour(cel,loc,s):
if s == 'r':
return cel[loc].r_nei
elif s == 'l':
return cel[loc].l_nei
elif s == 't':
return cel[loc].t_nei
elif s == 'b':
return cel[loc].b_nei
def set_neighbour(cells,loc,s,nei):
if s == 'r':
cells[loc].r_nei = nei
elif s == 'l':
cells[loc].l_nei = nei
elif s == 't':
cells[loc].t_nei = nei
elif s == 'b':
cells[loc].b_nei = nei
return cells
def get_area_vertices(array):
array = np.array(array)
done = 0
pts = len(array)-2
while not done:
if pts<len(array)-1:
this = array[pts]
pre = array[pts-1]
post = array[pts+1]
if (pre[0] == this[0] == post[0]) or (pre[1] == this[1] == post[1]):
array = np.delete(array,pts,axis=0)
pts -= 1
if pts<1:
done = 1
return array
def get_opposite_side(s):
if s == 'r':
return 'l'
elif s == 'l':
return 'r'
elif s == 't':
return 'b'
elif s == 'b':
return 't'
def get_perpendicular_sides(s):
if s == 'r' or s == 'l':
return ['t','b']
elif s == 't' or s == 'b':
return ['r','l']
def get_region_vertex(i,j):
if i+j == 'bl' or j+i == 'bl':
return 'bl'
elif i+j == 'br' or j+i == 'br':
return 'br'
elif i+j == 'tl' or j+i == 'tl':
return 'tl'
elif i+j == 'tr' or j+i == 'tr':
return 'tr'
def get_corner(i,j,grid):
if i+j == 'bl' or j+i == 'bl':
return grid.bl
elif i+j == 'br' or j+i == 'br':
return grid.br
elif i+j == 'tl' or j+i == 'tl':
return grid.tl
elif i+j == 'tr' or j+i == 'tr':
return grid.tr
def check_presence_inside(i,j,vm,p2,C):
if i+j == 'bl' or j+i == 'bl':
a = p2[0]>vm[0] and p2[1]>vm[1] and p2[0]<C[0] and p2[1]<C[1]
b = p2[0]>=vm[0] and p2[1]>vm[1] and p2[0]<C[0] and p2[1]<C[1]
c = p2[0]>vm[0] and p2[1]>=vm[1] and p2[0]<C[0] and p2[1]<C[1]
d = p2[0]>vm[0] and p2[1]>vm[1] and p2[0]<=C[0] and p2[1]<C[1]
e = p2[0]>vm[0] and p2[1]>vm[1] and p2[0]<C[0] and p2[1]<=C[1]
return a or b or c or d or e
elif i+j == 'br' or j+i == 'br':
a = p2[0]<vm[0] and p2[1]>vm[1] and p2[0]>C[0] and p2[1]<C[1]
b = p2[0]<=vm[0] and p2[1]>vm[1] and p2[0]>C[0] and p2[1]<C[1]
c = p2[0]<vm[0] and p2[1]>=vm[1] and p2[0]>C[0] and p2[1]<C[1]
d = p2[0]<vm[0] and p2[1]>vm[1] and p2[0]>=C[0] and p2[1]<C[1]
e = p2[0]<vm[0] and p2[1]>vm[1] and p2[0]>C[0] and p2[1]<=C[1]
return a or b or c or d or e
elif i+j == 'tl' or j+i == 'tl':
a = p2[0]>vm[0] and p2[1]<vm[1] and p2[0]<C[0] and p2[1]>C[1]
b = p2[0]>=vm[0] and p2[1]<vm[1] and p2[0]<C[0] and p2[1]>C[1]
c = p2[0]>vm[0] and p2[1]<=vm[1] and p2[0]<C[0] and p2[1]>C[1]
d = p2[0]>vm[0] and p2[1]<vm[1] and p2[0]<=C[0] and p2[1]>C[1]
e = p2[0]>vm[0] and p2[1]<vm[1] and p2[0]<C[0] and p2[1]>=C[1]
return a or b or c or d or e
elif i+j == 'tr' or j+i == 'tr':
a = p2[0]<vm[0] and p2[1]<vm[1] and p2[0]>C[0] and p2[1]>C[1]
b = p2[0]<=vm[0] and p2[1]<vm[1] and p2[0]>C[0] and p2[1]>C[1]
c = p2[0]<vm[0] and p2[1]<=vm[1] and p2[0]>C[0] and p2[1]>C[1]
d = p2[0]<vm[0] and p2[1]<vm[1] and p2[0]>=C[0] and p2[1]>C[1]
e = p2[0]<vm[0] and p2[1]<vm[1] and p2[0]>C[0] and p2[1]>=C[1]
return a or b or c or d or e
def get_v_m_tilda(i,j,p,start,stop,start_vertex):
grid_list = []
if isinstance(stop, type(None)):
next_ = get_neighbour(p,start,i)
grid_list.append(start)
# x = p[grid_list[-1]].get_x()
# y = p[grid_list[-1]].get_y()
# ax.plot(x,y)
while next_ != stop:
grid_list.append(next_)
next_ = get_neighbour(p,next_,i)
# x = p[grid_list[-1]].get_x()
# y = p[grid_list[-1]].get_y()
# ax.plot(x,y)
next_ = get_neighbour(p,grid_list[-1],j)
while next_ != stop:
grid_list.append(next_)
next_ = get_neighbour(p,next_,j)
# x = p[grid_list[-1]].get_x()
# y = p[grid_list[-1]].get_y()
# ax.plot(x,y)
else:
next_nei = get_neighbour(p,start,i)
grid_list.append(start)
# x = p[grid_list[-1]].get_x()
# y = p[grid_list[-1]].get_y()
# ax.plot(x,y)
if next_nei != None:
while check_presence_inside(i,j,stop,p[next_nei].centroid,start_vertex):
grid_list.append(next_nei)
next_nei = get_neighbour(p,next_nei,i)
# x = p[grid_list[-1]].get_x()
# y = p[grid_list[-1]].get_y()
# ax.plot(x,y)
if next_nei == None:
break
next_nei = get_neighbour(p,grid_list[-1],j)
if next_nei != None:
while check_presence_inside(i,j,stop,p[next_nei].centroid,start_vertex):
grid_list.append(next_nei)
next_nei = get_neighbour(p,next_nei,j)
# x = p[grid_list[-1]].get_x()
# y = p[grid_list[-1]].get_y()
# ax.plot(x,y)
if next_nei == None:
break
v_m_tilda = get_corner(i,j,p[grid_list[-1]])
stop = v_m_tilda
side = get_opposite_side(i)
next_nei = get_neighbour(p,grid_list[-1],side)
if next_nei != None:
while check_presence_inside(i,j,stop,p[next_nei].centroid,start_vertex):
grid_list.append(next_nei)
# x = p[grid_list[-1]].get_x()
# y = p[grid_list[-1]].get_y()
# ax.plot(x,y)
side_in = get_opposite_side(j)
next_nei_in = get_neighbour(p,grid_list[-1],side_in)
if next_nei_in != None:
while check_presence_inside(i,j,stop,p[next_nei_in].centroid,start_vertex):
grid_list.append(next_nei_in)
next_nei_in = get_neighbour(p,next_nei_in,side_in)
# x = p[grid_list[-1]].get_x()
# y = p[grid_list[-1]].get_y()
# ax.plot(x,y)
if next_nei_in == None:
break
next_nei = get_neighbour(p,next_nei,side)
if next_nei == None:
break
return v_m_tilda, grid_list
region = get_area_vertices(boundary)
sides = ['r','l','t','b']
region_vertex = {'bl': [], 'br': [], 'tl': [], 'tr': []}
for r in region:
if r[0]>C_tr[0] and r[1]>C_tr[1]:
region_vertex['tr'].append(r)
elif r[0]<C_tr[0] and r[1]>C_tr[1]:
region_vertex['tl'].append(r)
elif r[0]>C_tr[0] and r[1]<C_tr[1]:
region_vertex['br'].append(r)
elif r[0]<C_tr[0] and r[1]<C_tr[1]:
region_vertex['bl'].append(r)
cut_success = {}
# ax.scatter(C_tr[0],C_tr[1])
# plt.show()
for i in sides:
both = get_perpendicular_sides(i)
for j in both:
key = i+j
C_tr_grid = str(c)
if key == 'lt' or key == 'tl':
C_tr_grid = p[C_tr_grid].t_nei
elif key == 'rt' or key == 'tr':
C_tr_grid = p[p[C_tr_grid].t_nei].r_nei
elif key == 'rb' or key == 'br':
C_tr_grid = p[C_tr_grid].r_nei
vertex_inside = True
v_m_tilda, grid_list = get_v_m_tilda(i,j,p,C_tr_grid,None,C_tr)
stop_grid = v_m_tilda
region_vertices = region_vertex[get_region_vertex(i,j)]
count = -1
while vertex_inside:
vertex_inside_list = []
for r in region_vertices:
vertex_inside_list.append(check_presence_inside(i,j,v_m_tilda,r,C_tr))
vertex_inside = np.sum(vertex_inside_list)
stop_grid = v_m_tilda
v_m_tilda, grid_list = get_v_m_tilda(i,j,p,C_tr_grid,stop_grid,C_tr)
if len(grid_list) == 1:
marker = [0,0,0,0] # ['r','t','l','b']
if p[grid_list[-1]].l_nei is not None:
marker[2] = 1
if p[grid_list[-1]].r_nei is not None:
marker[0] = 1
if p[grid_list[-1]].t_nei is not None:
marker[1] = 1
if p[grid_list[-1]].b_nei is not None:
marker[3] = 1
if not vertex_inside:
if len(grid_list) == 1:
if (i+j == 'bl' or j+i =='bl') and (marker[3]+marker[2] == 2):
grid_list = []
elif (i+j == 'br' or j+i =='br') and (marker[0]+marker[3] == 2):
grid_list = []
elif (i+j == 'tl' or j+i =='tl') and (marker[1]+marker[2] == 2):
grid_list = []
elif (i+j == 'tr' or j+i =='tr') and (marker[1]+marker[0] == 2):
grid_list = []
break
elif vertex_inside:
count += 1
v_m_tilda = region_vertices[count]
vertex_exists = False
corner_points = np.array([C_tr,np.array([C_tr[0],v_m_tilda[1]]),np.array([v_m_tilda[0],C_tr[1]])])
for r in region:
for point in corner_points:
if np.linalg.norm(r-point)==0:
vertex_exists = True
break
if vertex_exists:
break
if not vertex_exists:
cut_success[key] = np.unique(grid_list)
else:
cut_success[key] = []
cutting = False
cutting_loc = None
while len(cut_success) != 0 and cutting != True:
checker = False
random_ = np.random.randint(0,len(cut_success))
for i,k in enumerate(cut_success):
if i == random_ and len(cut_success[k])>0:
cutting = True
cutting_loc = k
break
elif i == random_ and len(cut_success[k])==0:
checker = True
break
if checker:
del cut_success[k]
if cutting_loc is not None:
for g in cut_success[cutting_loc]:
if g in cut_success[cutting_loc]:
if p[g].t_nei != None:
p[p[g].t_nei].b_nei = None
if p[g].b_nei != None:
p[p[g].b_nei].t_nei = None
if p[g].r_nei != None:
p[p[g].r_nei].l_nei = None
if p[g].l_nei != None:
p[p[g].l_nei].r_nei = None
# x = p[g].get_x()
# y = p[g].get_y()
# ax.plot(x,y,color='red')
# plt.show()
del p[g]
return p,cutting
def Inflate(p,c,g_size=10,ax=None):
C_tr = p[str(c)].tr
# ax.clear()
# for pp in p:
# x = p[pp].get_x()
# y = p[pp].get_y()
# ax.plot(x,y,color='red')
# plt.show()
# x = p[str(c)].get_x()
# y = p[str(c)].get_y()
# ax.plot(x,y,color='green')
# plt.show()
new_cells = {}
for ind in range(len(p)):
grid = list(p.items())[ind][0]
# Quadrant marking
# x = p[str(grid)].get_x()
# y = p[str(grid)].get_y()
# ax.plot(x,y,color='black')
# plt.show()
marker = np.zeros((4,4))
grid_corners = p[grid].get_corners()
for vertex in range(len(grid_corners)):
if grid_corners[vertex,0] < C_tr[0] and grid_corners[vertex,1] < C_tr[1]:
marker[vertex,0] = 1
elif grid_corners[vertex,0] < C_tr[0] and grid_corners[vertex,1] > C_tr[1]:
marker[vertex,3] = 1
elif grid_corners[vertex,0] > C_tr[0] and grid_corners[vertex,1] < C_tr[1]:
marker[vertex,1] = 1
elif grid_corners[vertex,0] > C_tr[0] and grid_corners[vertex,1] > C_tr[1]:
marker[vertex,2] = 1
else:
pass
# Shifting and Inserting cells
a = None
if np.sum(marker[:,3]) == 4:
# NW
for vertex in range(len(grid_corners)):
grid_corners[vertex,1] += g_size
if np.sum(marker[:,1]) == 4:
# SE
for vertex in range(len(grid_corners)):
grid_corners[vertex,0] += g_size
if np.sum(marker[:,2]) >= 1:
# NE
for vertex in range(len(grid_corners)):
grid_corners[vertex] = grid_corners[vertex] + np.array([g_size,g_size])
if grid_corners[2,1] == C_tr[1] and grid_corners[3,1] == C_tr[1] and grid_corners[0,0] >= C_tr[0]:
# +x bottom all
for vertex in range(len(grid_corners)):
grid_corners[vertex,0] += g_size
t = Grid(bl = grid_corners[3], br = grid_corners[2], tl = grid_corners[3] + np.array([0,g_size]), tr = grid_corners[2] + np.array([0,g_size]))
t.b_nei = grid
if p[grid].t_nei is not None:
t.t_nei = p[grid].t_nei
p[t.t_nei].b_nei = str(int(list(p.items())[-1][0])+1)
p[grid].t_nei = str(int(list(p.items())[-1][0])+1)
new_cells[str(int(list(p.items())[-1][0])+1)] = t
p.update(new_cells)
# x = t.get_x()
# y = t.get_y()
# ax.plot(x,y,color='blue')
# plt.show()
if grid_corners[2,1] == C_tr[1] and grid_corners[3,1] == C_tr[1] and grid_corners[0,0] < C_tr[0]:
# -x bottom all
a = Grid(bl = grid_corners[3], br = grid_corners[2], tl = grid_corners[3] + np.array([0,g_size]), tr = grid_corners[2] + np.array([0,g_size]))
a.b_nei = grid
if p[grid].t_nei is not None:
a.t_nei = p[grid].t_nei
p[p[grid].t_nei].b_nei = str(int(list(p.items())[-1][0])+1)
p[grid].t_nei = str(int(list(p.items())[-1][0])+1)
new_cells[str(int(list(p.items())[-1][0])+1)] = a
p.update(new_cells)
# x = a.get_x()
# y = a.get_y()
# ax.plot(x,y,color='blue')
# plt.show()
if grid_corners[1,0] == C_tr[0] and grid_corners[2,0] == C_tr[0] and grid_corners[3,1] <= C_tr[1]:
# -y left all
a = Grid(bl = grid_corners[1], br = grid_corners[1] + np.array([g_size,0]), tl = grid_corners[2], tr = grid_corners[2] + np.array([g_size,0]))
a.l_nei = grid
if p[grid].r_nei is not None:
a.r_nei = p[grid].r_nei
p[p[grid].r_nei].l_nei = str(int(list(p.items())[-1][0])+1)
p[grid].r_nei = str(int(list(p.items())[-1][0])+1)
new_cells[str(int(list(p.items())[-1][0])+1)] = a
p.update(new_cells)
# x = a.get_x()
# y = a.get_y()
# ax.plot(x,y,color='blue')
# plt.show()
if grid_corners[3,1] == C_tr[1]:
a = Grid(bl = grid_corners[2], br = grid_corners[2] + np.array([g_size,0]), tl = grid_corners[2] + np.array([0,g_size]), tr = grid_corners[2] + np.array([g_size,g_size]))
a.l_nei = p[grid].t_nei
a.b_nei = p[grid].r_nei
p[p[grid].t_nei].r_nei = str(int(list(p.items())[-1][0])+1)
p[p[grid].r_nei].t_nei = str(int(list(p.items())[-1][0])+1)
new_cells[str(int(list(p.items())[-1][0])+1)] = a
p.update(new_cells)
# x = a.get_x()
# y = a.get_y()
# ax.plot(x,y,color='blue')
# plt.show()
if grid_corners[0,0] == C_tr[0] and grid_corners[3,0] == C_tr[0] and grid_corners[3,1] < C_tr[1]:
# -y right except first
for vertex in range(len(grid_corners)):
grid_corners[vertex,0] += g_size
if grid_corners[0,1] == C_tr[1] and grid_corners[1,1] == C_tr[1] and grid_corners[2,0] < C_tr[0]:
# -x top except first
for vertex in range(len(grid_corners)):
grid_corners[vertex,1] += g_size
if grid_corners[1,0] == C_tr[0] and grid_corners[2,0] == C_tr[0] and grid_corners[1,1] >= C_tr[1]:
# +y left excluding first cell
for vertex in range(len(grid_corners)):
grid_corners[vertex,1] += g_size
a = Grid(bl = grid_corners[1], br = grid_corners[1] + np.array([g_size,0]), tl = grid_corners[2], tr = grid_corners[2] + np.array([g_size,0]))
a.l_nei = grid
if p[grid].r_nei is not None:
a.r_nei = p[grid].r_nei
p[p[grid].r_nei].l_nei = str(int(list(p.items())[-1][0])+1)
p[grid].r_nei = str(int(list(p.items())[-1][0])+1)
new_cells[str(int(list(p.items())[-1][0])+1)] = a
p.update(new_cells)
# x = a.get_x()
# y = a.get_y()
# ax.plot(x,y,color='blue')
# plt.show()
p[grid].bl = grid_corners[0]
p[grid].br = grid_corners[1]
p[grid].tr = grid_corners[2]
p[grid].tl = grid_corners[3]
p[grid].get_centroid()
# ax.clear()
# for pp in p:
# x = p[pp].get_x()
# y = p[pp].get_y()
# ax.plot(x,y,color='red')
# plt.show()
# x = p[str(c)].get_x()
# y = p[str(c)].get_y()
# ax.plot(x,y,color='green')
# plt.show()
# for pp in new_cells:
# x = new_cells[pp].get_x()
# y = new_cells[pp].get_y()
# ax.plot(x,y,color='blue')
# plt.show()
p.update(new_cells)
# ax.clear()
# for pp in p:
# x = p[pp].get_x()
# y = p[pp].get_y()
# ax.plot(x,y,color='red')
# plt.show()
# x = p[str(c)].get_x()
# y = p[str(c)].get_y()
# ax.plot(x,y,color='green')
# plt.show()
# for pp in new_cells:
# x = new_cells[pp].get_x()
# y = new_cells[pp].get_y()
# ax.plot(x,y,color='blue')
# plt.show()
for _,grid in enumerate(new_cells):
# x = p[grid].get_x()
# y = p[grid].get_y()
# ax.plot(x,y,color='pink')
# plt.show()
if p[grid].l_nei is None:
if p[grid].b_nei is not None:
if p[p[grid].b_nei].l_nei is not None:
if p[p[p[grid].b_nei].l_nei].t_nei is not None:
p[grid].l_nei = p[p[p[grid].b_nei].l_nei].t_nei
if p[p[p[grid].b_nei].l_nei].t_nei is not None:
p[p[p[p[grid].b_nei].l_nei].t_nei].r_nei = grid
elif p[grid].t_nei is not None:
if p[p[grid].t_nei].l_nei is not None:
p[grid].l_nei = p[p[p[grid].t_nei].l_nei].b_nei
if p[p[p[grid].t_nei].l_nei].b_nei is not None:
p[p[p[p[grid].t_nei].l_nei].b_nei].r_nei = grid
elif p[grid].t_nei is not None:
if p[p[grid].t_nei].l_nei is not None:
p[grid].l_nei = p[p[p[grid].t_nei].l_nei].b_nei
if p[p[p[grid].t_nei].l_nei].b_nei is not None:
p[p[p[p[grid].t_nei].l_nei].b_nei].r_nei = grid
if p[grid].r_nei is None:
if p[grid].b_nei is not None:
if p[p[grid].b_nei].r_nei is not None:
if p[p[p[grid].b_nei].r_nei].t_nei is not None:
p[grid].r_nei = p[p[p[grid].b_nei].r_nei].t_nei
if p[p[p[grid].b_nei].r_nei].t_nei is not None:
p[p[p[p[grid].b_nei].r_nei].t_nei].l_nei = grid
elif p[grid].t_nei is not None:
if p[p[grid].t_nei].r_nei is not None:
p[grid].r_nei = p[p[p[grid].t_nei].r_nei].b_nei
if p[p[p[grid].t_nei].r_nei].b_nei is not None:
p[p[p[p[grid].t_nei].r_nei].b_nei].l_nei = grid
elif p[grid].t_nei is not None:
if p[p[grid].t_nei].r_nei is not None:
p[grid].r_nei = p[p[p[grid].t_nei].r_nei].b_nei
if p[p[p[grid].t_nei].r_nei].b_nei is not None:
p[p[p[p[grid].t_nei].r_nei].b_nei].l_nei = grid
if p[grid].t_nei is None:
if p[grid].r_nei is not None:
if p[p[grid].r_nei].t_nei is not None:
if p[p[p[grid].r_nei].t_nei].l_nei is not None:
p[grid].t_nei = p[p[p[grid].r_nei].t_nei].l_nei
if p[p[p[grid].r_nei].t_nei].l_nei is not None:
p[p[p[p[grid].r_nei].t_nei].l_nei].b_nei = grid
elif p[grid].l_nei is not None:
if p[p[grid].l_nei].t_nei is not None:
p[grid].t_nei = p[p[p[grid].l_nei].t_nei].r_nei
if p[p[p[grid].l_nei].t_nei].r_nei is not None:
p[p[p[p[grid].l_nei].t_nei].r_nei].b_nei = grid
elif p[grid].l_nei is not None:
if p[p[grid].l_nei].t_nei is not None:
p[grid].t_nei = p[p[p[grid].l_nei].t_nei].r_nei
if p[p[p[grid].l_nei].t_nei].r_nei is not None:
p[p[p[p[grid].l_nei].t_nei].r_nei].b_nei = grid
if p[grid].b_nei is None:
if p[grid].r_nei is not None:
if p[p[grid].r_nei].b_nei is not None:
if p[p[p[grid].r_nei].b_nei].l_nei is not None:
p[grid].b_nei = p[p[p[grid].r_nei].b_nei].l_nei
if p[p[p[grid].r_nei].b_nei].l_nei is not None:
p[p[p[p[grid].r_nei].b_nei].l_nei].t_nei = grid
elif p[grid].l_nei is not None:
if p[p[grid].l_nei].b_nei is not None:
p[grid].b_nei = p[p[p[grid].l_nei].b_nei].r_nei
if p[p[p[grid].l_nei].b_nei].r_nei is not None:
p[p[p[p[grid].l_nei].b_nei].r_nei].t_nei = grid
elif p[grid].l_nei is not None:
if p[p[grid].l_nei].b_nei is not None:
p[grid].b_nei = p[p[p[grid].l_nei].b_nei].r_nei
if p[p[p[grid].l_nei].b_nei].r_nei is not None:
p[p[p[p[grid].l_nei].b_nei].r_nei].t_nei = grid
# if p[grid].t_nei is not None:
# ax.plot([p[grid].centroid[0],p[p[grid].t_nei].centroid[0]],[p[grid].centroid[1],p[p[grid].t_nei].centroid[1]])
# if p[grid].b_nei is not None:
# ax.plot([p[grid].centroid[0],p[p[grid].b_nei].centroid[0]],[p[grid].centroid[1],p[p[grid].b_nei].centroid[1]])
# if p[grid].l_nei is not None:
# ax.plot([p[grid].centroid[0],p[p[grid].l_nei].centroid[0]],[p[grid].centroid[1],p[p[grid].l_nei].centroid[1]])
# if p[grid].r_nei is not None:
# ax.plot([p[grid].centroid[0],p[p[grid].r_nei].centroid[0]],[p[grid].centroid[1],p[p[grid].r_nei].centroid[1]])
# plt.show()
return p
r = (num_vertices/2) - 2
P = {'0': Grid(bl = np.array([50,50]), br = np.array([50+g_size,50]), tr = np.array([50+g_size,50+g_size]), tl = np.array([50,50+g_size]))} # Unit square
plt.ion()
# fig,ax = plt.subplots()
if ax == None:
fig1,ax1 = plt.subplots()
else:
ax1 = ax
while r>0:
cut_success = False
# polygon_boundary(P,ax)
while not cut_success:
p_trial = cp.copy(P)
random_c = np.random.randint(0,len(p_trial))
random_c = int(list(p_trial.items())[random_c][0])
p_trial = Inflate(p_trial,random_c,g_size=g_size)
p_trial, cut_success = Cut(p_trial,random_c)
P = p_trial
ax1.clear()
polygon_boundary(P,ax1)
r -= 1
boundary,edges,start_edge,end_edge = polygon_boundary(P,ax)
return boundary,edges,start_edge,end_edge , P
# Grid graph construction:
def is_grid_in_poly(polygon,grid,min_x,min_y,max_x,max_y):
c = grid.centroid
l = np.array([c[0]-(max_x-min_x),c[1]])
r = np.array([c[0]+(max_x-min_x),c[1]])
t = np.array([c[0],c[1]+(max_y-min_y)])
b = np.array([c[0],c[1]-(max_y-min_y)])
cl = LineString([tuple(c),tuple(l)])
cr = LineString([tuple(c),tuple(r)])
ct = LineString([tuple(c),tuple(t)])
cb = LineString([tuple(c),tuple(b)])
B_l = []
B_r = []
B_t = []
B_b = []
for v in range(-1,len(polygon)-1):
e = LineString([polygon[v],polygon[v+1]])
B_l.append(e.intersects(cl))
B_r.append(e.intersects(cr))
B_t.append(e.intersects(ct))
B_b.append(e.intersects(cb))
if np.sum(B_l)%2==1 or np.sum(B_r)%2==1 or np.sum(B_t)%2==1 or np.sum(B_b)%2==1:
return 1
else:
return 0
def Grid_graph(polygon,grid_size,ax):
min_x = min(polygon[:,0])
max_x = max(polygon[:,0])
min_y = min(polygon[:,1])
max_y = max(polygon[:,1])
grids = {}
for i in range(min_x,max_x,grid_size):
for j in range(min_y,max_y,grid_size):
tl = np.array([i,j+grid_size])
tr = np.array([i+grid_size,j+grid_size])
bl = np.array([i,j])
br = np.array([i+grid_size,j])
g = Grid(tl=tl,bl=bl,tr=tr,br=br)
if is_grid_in_poly(polygon,g,min_x,min_y,max_x,max_y):
if str(bl - np.array([grid_size,0])) in grids:
g.l_nei = str(bl - np.array([grid_size,0]))
grids[str(bl - np.array([grid_size,0]))].r_nei = str(bl)
ax.plot([g.centroid[0],grids[str(bl - np.array([grid_size,0]))].centroid[0]],[g.centroid[1],grids[str(bl - np.array([grid_size,0]))].centroid[1]],color='maroon',linewidth=0.5)
if str(br) in grids:
g.r_nei = str(br)
grids[str(br)].l_nei = str(bl)
ax.plot([g.centroid[0],grids[str(br)].centroid[0]],[g.centroid[1],grids[str(br)].centroid[1]],color='maroon',linewidth=0.5)
if str(tl) in grids:
g.t_nei = str(tl)
grids[str(tl)].b_nei = str(bl)
ax.plot([g.centroid[0],grids[str(tl)].centroid[0]],[g.centroid[1],grids[str(tl)].centroid[1]],color='maroon',linewidth=0.5)
if str(bl - np.array([0,grid_size])) in grids:
g.b_nei = str(bl - np.array([0,grid_size]))