-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPATHPLANNING.py
280 lines (265 loc) · 9.25 KB
/
PATHPLANNING.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
import cv2
import numpy as np
from math import sqrt
from math import pow
from copy import copy
import os
import colorsys
globx=globx2=0; globy=globy2=0
#for 1.png, set it to 1e-12, for 2.png set it to -14
distThresh=0
def hsv2rgb(h,s,v):
return tuple(int(i * 255) for i in colorsys.hsv_to_rgb(h,s,v))
def plotLine(x1,y1,x2,y2):
x=x1
count=0.005
try:
m=float(y2-y1)/(x2-x1)
c=y2-m*x2
if x2<x1: count*=-1
while (True):
x+=count
y=m*x+c
if (count>0 and x>=x2) or (count<0 and x<=x2):
return
centroid[int(y),int(x)]=[255,255,0]
except:
y=y1
if (y2<y1): count*=-1
while (True):
y+=count
if (count>0 and y>=y2) or (count<0 and y<=y2):
return
centroid[int(y),int(x)]=[255,255,0]
def Disth(item):
dist=sqrt(((item[0]-town[0])**2)+((item[1]-town[1])**2))
if item[2]=="food":
return dist*0.75
else:
return dist
def Distn(item):
print str(globx2)+"sdgfidsfdskjjdkjdsfkjdsbfjdfdsfjdsfdsjdsfdsfjdsjhjdsbvjfdsvbfcjfdsvjhcvdsbjcdshvcjdsvcj"
global globx2,globy2
dist=sqrt(((globx2-item[0])**2)+((globy2-item[1])**2))
return dist
def Dist(x1,y1,x2,y2):
return (sqrt((x1-x2)**2+(y1-y2)**2))
def clearance(x1,y1,x2,y2):
x=x1
count=0.005
try:
m=float(y2-y1)/(x2-x1)
c=y2-m*x2
if x2<x1: count*=-1
while (True):
x+=count
y=m*x+c
if (count>0 and x>=x2) or (count<0 and x<=x2):
return 1
if (centroid[int(y),int(x)][0]==255 and centroid[int(y),int(x)][1]==255 and centroid[int(y),int(x)][2]==255):
return 0
except:
y=y1
if (y2<y1): count*=-1
while (True):
y+=count
if (count>0 and y>=y2) or (count<0 and y<=y2):
return 1
if (centroid[int(y),int(x)][0]==255 and centroid[int(y),int(x)][1]==255 and centroid[int(y),int(x)][2]==255):
return 0
def pathPlanning():
global foodList, woodList, townList, riverList, town, points, points2
global globx2,globy2
netList=foodList+woodList
netList.sort(key=Disth)
print "\nNet: "+str(netList)
print "eurhfuehfiehfkefkejfiej"+str(len(netList))
print "otherwise: "+str(len(woodList))
xorg=int(town[0]); yorg=int(town[1]); flag=0
counted=1
#riverList.remove([xorg,yorg])
print str(xorg)+","+str(yorg)
for i in netList:
print counted; counted+=1
print i
flag=0
#if (int(i[0])>=480): continue
pointstemp=[]; riverHere=copy(riverList)
xfin=i[0]; yfin=i[1]
globx2=xfin; globy2=yfin
riverHere.sort(key=Distn)
while(True):
print "\n"+str(riverHere)
if flag==0:
place=clearance(town[0],town[1],xfin,yfin)
else:
place=clearance(pointstemp[len(pointstemp)-1][0],pointstemp[len(pointstemp)-1][1],xfin,yfin)
if ( (place) == 1):
flag=0
else:
#globx=place[0]; globy=place[1]
flaghere=1
for j in riverHere:
if (len(pointstemp)>0):
if pointstemp[len(pointstemp)-1][0]==j[0] and pointstemp[len(pointstemp)-1][1]==j[1] :
continue
check=clearance(pointstemp[len(pointstemp)-1][0],pointstemp[len(pointstemp)-1][1],j[0],j[1])
else:
check=clearance(town[0],town[1],j[0],j[1])
if ( (check) == 1):
flaghere=0
#globx=j[0]; globy=j[1]
#print "Yo!"
pointstemp.append([j[0],j[1],0])
riverHere.remove(j)
flag=1
break
if (flaghere==1): break
if flag==0: break
p=1; q=0
while(True):
try:
print str(pointstemp[q])+" "+ str(pointstemp[q+1])
x=clearance(town[0],town[1],pointstemp[q][0],pointstemp[q][1])
xnext=clearance(town[0],town[1],pointstemp[q+1][0],pointstemp[q+1][1])
print str(x)+" "+str(xnext)
if (x == 1) and (xnext == 1):
pointstemp.remove(pointstemp[q])
q-=1
except:
break
q+=1
points.extend([[xorg,yorg,0]]+pointstemp+[[i[0],i[1],1],[i[0],i[1],0]]+pointstemp[::-1]+[[xorg,yorg,1]])
points.extend([[xorg,yorg,0]]+pointstemp+[[i[0],i[1],1],[i[0],i[1],0]]+pointstemp[::-1]+[[xorg,yorg,1]])
points2.extend([[xorg,yorg,0]]+pointstemp+[[i[0],i[1],1],[i[0],i[1],0]]+pointstemp[::-1]+[[xorg,yorg,1]])
def draw():
global riverListhere, area, centroid, riverList
s=int(sqrt(area)/2)
print ("Hello")
for i in riverListhere:
print "Hello again!"
x=int(i[0])
y=int(i[1])
try:
centroid[y+(s+10),x+(s+10)]=[0,0,255]
riverList.append([x+s+10,y+s+10])
except: pass
try: centroid[y+(s+10),x-(s+10)]=[0,0,255]; riverList.append([x-(s+10),y+(s+10)])
except: pass
try: centroid[y-(s+10),x+(s+10)]=[0,0,255]; riverList.append([x+(s+10),y-(s+10)])
except: pass
try: centroid[y-(s+10),x-(s+10)]=[0,0,255]; riverList.append([x-(s+10),y-(s+10)])
except: pass
try: centroid[y+(s-10),x+(s-10)]=[0,0,255]; riverList.append([x+(s-10),y+(s-10)])
except: pass
try: centroid[y+(s-10),x-(s-10)]=[0,0,255]; riverList.append([x-(s-10),y+(s-10)])
except: pass
try: centroid[y-(s-10),x+(s-10)]=[0,0,255]; riverList.append([x+(s-10),y-(s-10)])
except: pass
try: centroid[y-(s-10),x-(s-10)]=[0,0,255]; riverList.append([x-(s-10),y-(s-10)])
except: pass
for i in xrange(-s,s):
for j in xrange(-s,s):
try:
centroid[y+j,x+i]=[255,255,255]
except:
pass
def plot():
global woodList, foodList, town
print woodList;
for i in woodList:
centroid[int(i[1]),int(i[0])]=[0,255,0]
centroid[int(i[1])+1,int(i[0])+1]=[0,255,0]
centroid[int(i[1])+1,int(i[0])]=[0,255,0]
centroid[int(i[1]),int(i[0]+1)]=[0,255,0]
for i in foodList:
centroid[int(i[1]),int(i[0])]=[255,255,0]
centroid[int(i[1])+1,int(i[0])+1]=[255,255,0]
centroid[int(i[1])+1,int(i[0])]=[255,255,0]
centroid[int(i[1]),int(i[0]+1)]=[255,255,0]
centroid[int(town[1]),int(town[0])]=[255,255,0]
centroid[int(town[1])+1,int(town[0])+1]=[255,255,0]
centroid[int(town[1])+1,int(town[0])]=[255,255,0]
centroid[int(town[1]),int(town[0]+1)]=[255,255,0]
'''
riverListhere=[[458, 366], [458, 341], [434, 341], [411, 341], [458, 315], [508, 287], [485, 287], [458, 287], [158, 177], [158, 152], [207, 129], [184, 129], [157, 129], [207, 104], [207, 80], [206, 53]]
riverList=[]
woodList=[[236.0, 468.2773132324219, 'wood'], [204.0, 468.2773132324219, 'wood'], [171.0, 468.2773132324219, 'wood'], [486.95709228515625, 359.95709228515625, 'wood'], [281.0, 110.27731323242188, 'wood'], [279.0, 89.27731323242188, 'wood'], [279.0, 69.27731323242188, 'wood']]
foodList=[[73.5, 339.0, 'food'], [487.0, 330.0, 'food'], [73.5, 309.5, 'food'], [142.0, 44.5, 'food'], [119.0, 44.5, 'food'], [92.0, 44.5, 'food']]
'''
townList=[]
points=[]; points2=[]
riverListhere=[]
riverList=[]
woodList=[]
foodList=[]
cap = cv2.VideoCapture(0)
img=cap.read()
while(1):
a,img=cap.read()
cv2.imshow("video",img)
if cv2.waitKey(1) & 0xFF == 27: break
height, width, channels=img.shape
centroid=np.zeros((height,width,channels), np.uint8)
try:
myfile=open("wood.txt","r")
while(1):
line=myfile.readline()
if not line: break
woodList.append([float(line.split(',')[0]),float(line.split(',')[1][:-1]),"wood"])
myfile.close()
except:
print "Wood is missing."
try:
myfile = open("food.txt","r")
while(1):
line=myfile.readline()
if not line: break
foodList.append([float(line.split(',')[0]),float(line.split(',')[1][:-1]),"food"])
myfile.close()
except:
print "Food is missing."
try:
myfile= open("town.txt","r")
while(1):
line=myfile.readline()
print line
if not line: break
townList.append([float(line.split(',')[0]),float(line.split(',')[1][:-1])])
myfile.close()
except:
print "Town is missing."
sumx=0; sumy=0
for i in townList:
sumx+=int((i[0]))
sumy+=int((i[1]))
print townList
try:
town=[int(sumx/len(townList)),int(sumy/len(townList))]
except:
print "Hey! There's no town!"
cv2.waitKey(1000)
town=[191.5, 253.5]
area=int(raw_input("Enter area: "))
draw()
print "drawn"
plot()
print "plotted"
pathPlanning()
points+=points2
print "\nPath: "+str(points)
j=1
colorinc=50
while(1):
if (j>=len(points)):
break
plotLine(points[j-1][0],points[j-1][1],points[j][0],points[j][1])
j+=1
cv2.imshow("Path",centroid)
cv2.waitKey(0)
myfile=open("list.txt","w")
for i in points:
myfile.write(str(i[0])+"\n")
myfile.write(str(i[1])+"\n")
myfile.write(str(i[2])+"\n")
myfile.close()