-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFinal_Voronoi_New_and_Short.py
362 lines (319 loc) · 12.7 KB
/
Final_Voronoi_New_and_Short.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
import matplotlib.pyplot as plt
from scipy.spatial import Voronoi, voronoi_plot_2d
import numpy as np
from shapely.geometry import Point,Polygon #used to chk pt in or out of poly
import time
X = [];Y = [];Pi = [];PS = [];Xn = []; S = [];Yx = [];Yn = []; Yy = []; Pout = []
MP = []; Ym = [];Yp = [];Poly = []; YN = [];m = []; Pc = []; Pcc = [];Yx = []
''' To find the scan locations on the voronoi diagram for any polygon P, please assign the list of co-ordinates of any polygon to a variable "P" as
shown in the test examples below'''
''' Following are the two test example polygons'''
P = [(24970,19250),(23600,19250),(20740,22110),(22790,24160),(19395,27554),\
(17345,25504),(15560,27289),(15560,30215),(11165,30215),(11165,27915),\
(12435,27915),(15220,24415),(12445,21630),(16865,17210),(19650,19995),\
(23600,16045),(24970,16045)]
P = [(24970,19250),(23600,19250),(20740,22110),(22790,24160),(19395,27554)\
,(17345,25504),(15560,27289),(15560,30215),(16490,30215),(16490,31500)\
,(20670,31500),(20670,33700),(23370,33700),(23370,31150),(25785,31150)\
,(25785,41415),(16740,41416),(16740,39400),(10060,39400),(10060,41415)\
,(4315,41415),(4315,39400),(1300,39400),(1300,31300),(3545,31300)\
,(3545,34300),(6245,34300),(6245,29085),(4785,29085),(4785,26570)\
,(2085,26570),(2085,28615),(0,28615),(0,21110),(11925,21110),(12445,21630)\
,(16865,17210),(14600,14946),(16407,13139),(14498,11230),(12691,13036)\
,(9085,9430),(11430,7085),(11430,4800),(19590,4800),(19590,9250),(26255,9250)\
,(26255,7085),(32000,7085),(32000,9720),(36510,9720),(36510,15050)\
,(34330,15050),(34330,12850),(31430,12850),(31430,19250),(34330,19250)\
,(34330,17050),(37480,17050),(37480,23430),(34330,23430),(34330,26060)\
,(28385,26060),(28385,24260),(24970,24260)]
points = np.array(P)
''' The function voronoi_finite_polygons_2d gives the voronoi regions and vertices, for a given polygon'''
def voronoi_finite_polygons_2d(P, radius=None):
"""
Reconstruct infinite voronoi regions in a 2D diagram to finite
regions.
Parameters
----------
vor : Voronoi
Input diagram
radius : float, optional
Distance to 'points at infinity'.
Returns
-------
regions : list of tuples
Indices of vertices in each revised Voronoi regions.
vertices : list of tuples
Coordinates for revised Voronoi vertices. Same as coordinates
of input vertices, with 'points at infinity' appended to the
end.
"""
vor = Voronoi(np.array(P))
if vor.points.shape[1] != 2:
raise ValueError("Requires 2D input")
new_regions = []
new_vertices = vor.vertices.tolist()
center = vor.points.mean(axis=0)
if radius is None:
radius = vor.points.ptp().max()
# Construct a map containing all ridges for a given point
all_ridges = {}
for (p1, p2), (v1, v2) in zip(vor.ridge_points, vor.ridge_vertices):
all_ridges.setdefault(p1, []).append((p2, v1, v2))
all_ridges.setdefault(p2, []).append((p1, v1, v2))
# Reconstruct infinite regions
for p1, region in enumerate(vor.point_region):
vertices = vor.regions[region]
if all(v >= 0 for v in vertices):
# finite region
new_regions.append(vertices)
continue
# reconstruct a non-finite region
ridges = all_ridges[p1]
new_region = [v for v in vertices if v >= 0]
for p2, v1, v2 in ridges:
if v2 < 0:
v1, v2 = v2, v1
if v1 >= 0:
# finite ridge: already in the region
continue
# Compute the missing endpoint of an infinite ridge
t = vor.points[p2] - vor.points[p1] # tangent
t /= np.linalg.norm(t)
n = np.array([-t[1], t[0]]) # normal
midpoint = vor.points[[p1, p2]].mean(axis=0)
direction = np.sign(np.dot(midpoint - center, n)) * n
far_point = vor.vertices[v2] + direction * radius
new_region.append(len(new_vertices))
new_vertices.append(far_point.tolist())
# sort region counterclockwise
vs = np.asarray([new_vertices[v] for v in new_region])
c = vs.mean(axis=0)
angles = np.arctan2(vs[:,1] - c[1], vs[:,0] - c[0])
new_region = np.array(new_region)[np.argsort(angles)]
# finish
new_regions.append(new_region.tolist())
return new_regions, np.asarray(new_vertices)
fig = voronoi_plot_2d(Voronoi(points))
''' The function round_off just round of the float values'''
def round_off(P):
points = np.array(P)
regions, vertices = voronoi_finite_polygons_2d(points)
vert = []
for i in range(len(vertices)):
Vx = [] ;Vy = []
Vx = round(vertices[i][0])
Vy = round(vertices[i][1])
V = (Vx,Vy)
vert.append(V)
return vert
P.append(P[0]);Pc = round_off(points);Pc.append(Pc[0]);Vor_Vert = round_off(points)
Start = time.time() #starting the time
''' The function Sorting sorts the list'''
def Sorting(lst):
lst2 = sorted(lst, key=len, reverse = True)
return lst2
''' The orientation function: To check the orientation on points (x1,y1),(x2,y2),(x3,y3)'''
def orientation(x1,y1,x2,y2,x3,y3):
val = (float((y2-y1)*(x3-x2)))-(float((x2-x1)*(y3-y2)))
if (val>0):
return 1 #clockwise
elif (val<0):
return 2 #counterclockwise
else:
return 0 #collinear
''' point_in_seg_area function: To check if the point lies in segment area'''
def point_in_seg_area(x1,y1,x2,y2,x3,y3):
if ((x2<=max(x1,x3)) and (x2>=min(x1,x3))\
and (y2<=max(y1,y3)) and (y2>=min(y1,y3))):
return True
return False
''' check_intersection function: To check if the line formed by points (x1,y1) and (x2,y2) intersects line
formed by (x3,y3) and (x4,y4)'''
def check_intersection(x1,y1,x2,y2,x3,y3,x4,y4):
o1 = orientation(x1,y1,x2,y2,x3,y3)
o2 = orientation(x1,y1,x2,y2,x4,y4)
o3 = orientation(x3,y3,x4,y4,x1,y1)
o4 = orientation(x3,y3,x4,y4,x2,y2)
if ((o1 == 0) and point_in_seg_area(x1,y1,x3,y3,x2,y2)): #both are neede to tell if the point is on the segment
return False
if ((o2 == 0) and point_in_seg_area(x1,y1,x4,y4,x2,y2)):
return False
if ((o3 == 0) and point_in_seg_area(x3,y3,x1,y1,x4,y4)):
return False
if ((o4 == 0) and point_in_seg_area(x3,y3,x1,y1,x4,y4)):
return False
if ((o1!=o2) and (o3!=o4)):
return True
return False
'''The function create_point_pair creates edges from points'''
def create_point_pair(P):
Pb = []
for i in range(len(P)-1):
Pa = []
Pa.append(P[i])
Pa.append(P[i+1])
Pb.append(Pa)
return Pb
Pb = create_point_pair(P)
'''The function find_dist finds the distance between two points'''
def find_dist(A,B,C,D):
d = ((C-A)^2 + (D-B)^2)^(1/2)
return d
''' The function non_intersecting_diag creates non intersecting diagonals in the polygon.
Non intersecting diagonals do not intersect with the exterior of the polygon'''
def non_intersecting_diag(Pc,P):
for i in range(len(Pc)-1):
S = []
for j in range(len(P)-1):
Pi = []
Pi.append(Pc[i])
Pi.append(P[j])
S.append(Pi)
PS.append(S)
#print("Bhai PS:",PS)
for n in range(len(PS)):
for k in range(len(PS[n])):
Xn = []
for l in range(len(P)-1):
if (check_intersection(PS[n][k][0][0],PS[n][k][0][1],PS[n][k][1][0]\
,PS[n][k][1][1],P[l][0],P[l][1],P[l+1][0],P[l+1][1])==True)\
: #chek on this, error
continue
else:
Xn.append(PS[n][k][0])
Xn.append(PS[n][k][1])
Y = []
if len(Xn) == 2*(len(P)-1): #no intersection with any polygon side
Y.append(Xn[0])
Y.append(Xn[1])
if Y == []:
continue
else:
Yx.append(Y)
#print("Yx is:",Yx)
for m in range(len(Yx)):
px = float((Yx[m][0][0]+Yx[m][1][0])/2)
py = float((Yx[m][0][1]+Yx[m][1][1])/2)
mp = (px,py)
if not (Point(mp).within(Polygon(P))): #chk point in or out changed AP to P
Pout.append(Yx[m])
MP.append(mp)
#print("The list of outer lines:",Pout)
for n in range(len(Pout)):
if Pout[n] in Yx:
Yx.remove(Pout[n])
return Yx
Yx = non_intersecting_diag(Pc,P)
''' The function mini_chk_pts implements the proposed algorithm and returns the list of the scan locations' diagonals'''
def mini_chk_pts(Pb,Pc,P,Yx):
Yn=[];M=[];Ys1=[];Yk1=[];Yy1=[];Yf1 = [];Ye1 = []; R = []
for r in range(len(Pc)-1):#this is important for arranging the diagonals.
Yy1 = []
for s in range(len(Yx)):
if Pc[r] == Yx[s][0]:
Yy1.append(Yx[s])
if not Yy1 == []:
Yy1.append(Yy1[0])
Ys1.append(Yy1)
Yk1 = Sorting(Ys1) #sorting in descending order of length of sub-list.
#print("The list Yk1 is:",Yk1)
for b in range(len(Yk1)):
Yg = []
for c in range(len(Yk1[b])-1):
for a in range(len(P)-1):
Yf = []
if ((P[a] == Yk1[b][c][1]) and (P[a+1] == Yk1[b][c+1][1])):
Yf.append(Yk1[b][c])
Yf.append(Yk1[b][c+1])
Yg.append(Yf)
if not Yg == []:
Ye1.append(Yg)
Yf1 = Sorting(Ye1)
F = Pb
Yf2 = []
while F != []:
Yy = []; Ys = []; M = []
for a in range(len(Yf1)):
Yy = []
for b in range(len(Yf1[a])):
for c in range(len(F)):
if (F[c][0] in Yf1[a][b][0]) and (F[c][1] in Yf1[a][b][1])\
and (Yf1[a][b][0][1] in F[c]) and Yf1[a][b][1][1] in F[c]:
Yy.append(Yf1[a][b])
Ys.append(Yy)
Yf2 = Sorting(Ys)
A2 = Yf2[0]
for i in range(len(Yf2[0])):
Yn.append(Yf2[0][i])
Yf2.remove(Yf2[0])
for j in range(len(F)):
for k in range(len(A2)):
if (F[j][0] == A2[k][0][1]) and (F[j][1] == A2[k][1][1]):
M.append(F[j])
else:
continue
F2 = []
for l in range(len(F)):
if not F[l] in M:
F2.append(F[l])
else:
continue
Yf1 = Yf2
F = F2
return Yn
Yn = mini_chk_pts(Pb,Pc,P,Yx)
''' The function clean_up_final cleans up the Yn list (list of the diagonals of the scan locations)'''
def clean_up_final(Yn):
final = [];R = [];r = []
for i in Yn:
if not i in final:
final.append(i)
for p in range(len(final)): #solution for adjecent points
for q in range(len(final)): #this is a big change!!!!!!!!!
for r in range(len(Pc)-1):
if (final[p][0][0] or final[p][1][0]) == Pc[r]:
if (Pc[r+1] or Pc[r-1])==(final[q][0][1] or final[q][1][1]):
R.append(final[q])
for r in range(len(R)):#PREVENT REPITITION
if R[r] in final:
final.remove(R[r])
return final
Final_Diagonals = clean_up_final(Yn)
Yn = Final_Diagonals
''' The function Guards given the final list of the scan locations '''
def Guards(Final_Diagonals):
Guards = []
for i in range(len(Final_Diagonals)):
if not Final_Diagonals[i][0][0] in Guards:
Guards.append(Final_Diagonals[i][0][0])
return Guards
''' The function plt_plot plots the polygon and scan locations with diagonals'''
def plt_plot(P,Yn,vert):
plot_lstx = list()
plot_lsty = list()
Px = [];Py = [];Dx = [];Dy = [];Sx = [];Sy = [];Tx = [];Ty = [];Pcx = [];Pcy = []
for i in range(len(P)):
Px.append(P[i][0])
Py.append(P[i][1])
for j in range(len(Yn)):
Dx=[];Dy=[]
Dx.append(Yn[j][0][0][0])
Dy.append(Yn[j][0][0][1])
Dx.append(Yn[j][0][1][0])
Dy.append(Yn[j][0][1][1])
Dx.append(Yn[j][1][0][0])
Dy.append(Yn[j][1][0][1])
Dx.append(Yn[j][1][1][0])
Dy.append(Yn[j][1][1][1])
Sx.append(Yn[j][0][0][0])
Sy.append(Yn[j][0][0][1])
# plt.plot(Dx,Dy, color = 'g')
for k in range(len(vert)):
Pcx.append(vert[k][0])
Pcy.append(vert[k][1])
#plt.scatter(Pcx,Pcy,s = 200, marker = '.', color = 'r')
plt.plot(Px,Py,color = 'b')
plt.scatter(Sx,Sy,s = 700,marker = '.',color = 'k')
End = time.time()
return plt.show()
print(Guards(Final_Diagonals))
plt_plot(P,Yn,Vor_Vert)