-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
510 lines (309 loc) · 15.2 KB
/
main.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
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
import argparse
import sys
# parser = argparse.ArgumentParser()
# parser.add_argument("--map", "-m", type = str, default = "map1", help = "Location of the map image")
# parser.add_argument("--startX", "-sx", type = float, default = "50", help = "Integer value of strating X position")
# parser.add_argument("--startY", "-sy", type = float, default = "50", help = "Integer value of strating Y position")
# parser.add_argument("--goalX", "-gx", type = float, default = "500", help = "Integer value of goal X position")
# parser.add_argument("--goalY", "-gy", type = float, default = "400", help = "Integer value of goal Y position")
# args = parser.parse_args()
map_img = cv.imread("maps/" + sys.argv[1] + ".png")
max_x = map_img.shape[1]
max_y = map_img.shape[0]
startx = int(sys.argv[2])
starty = int(sys.argv[3])
goalx = int(sys.argv[4])
goaly = int(sys.argv[5])
starting_point = (startx, starty, 0)
goal_point = (goalx, goaly)
# plt.imshow(map_img)
# print("Showing the initial map")
# plt.show()
print("Max area is (X, Y) =", (max_x, max_y))
example_x = 513
example_y = 403
# print(map_img.max(), map_img.min())
#print("example point value", map_img[example_y][example_x][0])
# if map_img[example_y][example_x][0] == 0:
# print("Example point is in black region")
# else:
# print("Example point is in white region")
# Draw the starting point and the ending points
#print("Shape of the map is", map_img.shape)
# plt.imshow(map_img)
# print("Showing the initial map with start and goal")
# plt.show()
class RandomTree():
def __init__(self, starting_point) -> None:
self.start_x = starting_point[0]
self.start_y = starting_point[1]
self.tree = {starting_point[2] : []}
self.location_map = {starting_point[2] : (self.start_x, self.start_y)}
def addtotree(self, point):
point_x = point[0]
point_y = point[1]
point_id = point[2]
self.tree[point_id] = []
self.location_map[point_id] = (point_x, point_y)
def addalink(self, id_1, id_2, dist): #(self, to, from)
# point1_id = point1[2]
# point2_id = point2[2]
# If the assertion fails, then print the Tree
assert id_1 in self.tree and id_2 in self.tree, "Assertion Failed so printing the tree and then location map" + "\n" + str(self.tree) + "\n" + str(self.location_map)
self.tree[id_1].append((id_2, dist))
#self.tree[id_2].add((id_1, dist))
def draw_path(map_img, new_node, goal_node):
print("Finding the optimal path")
map_img = cv.line(map_img, (new_node[0], new_node[1]), (goal_node[0], goal_node[1]), (255, 0, 0), 2)
new_node_id = new_node[2]
#print("Tree looks like", random_tree.tree)
while new_node_id != 0:
parent_node = random_tree.tree[new_node_id] # Tuple of (parent node id, distance)
#print("New node id is", new_node_id, "Parent node looks like", parent_node)
parent_node_id = parent_node[0][0]
parent_node_dist = parent_node[0][1]
#print("Drawing line between nodes", new_node_id, "and", parent_node_id)
map_img = cv.line(map_img, (random_tree.location_map[new_node_id][0], random_tree.location_map[new_node_id][1]), (random_tree.location_map[parent_node_id][0], random_tree.location_map[parent_node_id][1]), (255, 0, 0), 2)
new_node_id = parent_node_id
return map_img
print("Initializing the tree and location map")
random_tree = RandomTree(starting_point)
#print("Tree looks like", random_tree.tree)
#print("Location map looks like", random_tree.location_map)
# Obstacle map
obstacle_map = []
print("Building the obstacle map")
for i in range(map_img.shape[0]):
for j in range(map_img.shape[1]):
if map_img[j][i][0] == 0:
obstacle_map.append([i, j])
#print("Obstacle Map's length is", len(obstacle_map))
#print("Last obstacle location", obstacle_map[-1])
# plt.imshow(map_img)
# print("Testing before obstacles")
# plt.show()
# Testing the obstacle map
# Testing the
# for obs in obstacle_map:
# tempx = obs[0]
# tempy = obs[1]
#map_img = cv.circle(map_img, (tempx, tempy), 1, (255, 0, 0), 1)
map_img = cv.circle(map_img, (starting_point[0], starting_point[1]), 5, (0, 0, 255), 5)
map_img = cv.circle(map_img, (goal_point[0], goal_point[1]), 5, (0, 0, 255), 5)
# plt.imshow(map_img)
# print("Testing obstacles")
# plt.show()
# Testing the RandomTree class
test_tree = False
if test_tree == True:
testing_point = (300, 400, 1)
random_tree.addtotree(testing_point)
random_tree.addalink(starting_point[2], testing_point[2])
map_img = cv.circle(map_img, (testing_point[0], testing_point[1]), 2, (255, 0, 0), 5)
print("After testing, tree looks like", random_tree.tree)
print("After testing, location map looks like", random_tree.location_map)
randx = np.random.randint(low = 0, high = max_x)
randy = np.random.randint(low = 0, high = max_y)
count = 2
new_rand_point = (randx, randy, count)
print("Random generator", randx, randy)
map_img = cv.circle(map_img, (new_rand_point[0], new_rand_point[1]), 2, (255, 255, 0), 5)
print("New Random point is", new_rand_point)
min_temp_dist = float('inf')
min_id = -1
for k, v in random_tree.tree.items():
# if k == count:
# continue
print(random_tree.location_map[k][0])
print(random_tree.location_map[k][1])
#temp_dist = np.linalg.norm(np.array((new_rand_point[0], random_tree.location_map[k][0])) - np.array((new_rand_point[1], random_tree.location_map[k][1])))
x2_m_x1 = np.power(new_rand_point[0] - random_tree.location_map[k][0], 2)
y2_m_y1 = np.power(new_rand_point[1] - random_tree.location_map[k][1], 2)
temp_dist = np.sqrt(x2_m_x1 + y2_m_y1)
print("Distance is", temp_dist)
if temp_dist < min_temp_dist:
min_temp_dist = temp_dist
min_id = k
print("ID of the nearest node is", min_id)
print("Distance between nearest node and random point is", min_temp_dist)
active_node_id = min_id
active_node = (int(random_tree.location_map[active_node_id][0]), int(random_tree.location_map[active_node_id][1]), active_node_id)
# new node between active node and random point
new_node_x = (new_rand_point[0] + random_tree.location_map[active_node_id][0]) / 2
new_node_y = (new_rand_point[1] + random_tree.location_map[active_node_id][1]) / 2
# if map_img[new_node_y][new_node_x][0] == 0:
# # That means black region
# continue
new_node = (int(new_node_x), int(new_node_y), count)
print("New node location is", new_node)
map_img = cv.circle(map_img, (int(new_node_x), int(new_node_y)), 1, (255, 0, 0), 3)
# EQUATION OF LINE
x1 = new_node[0]
y1 = new_node[1]
x2 = active_node[0]
y2 = active_node[1]
slope = (y2 - y1) / (x2 - x1)
y_intercept = y2 - (slope * x2)
print("Slope and Y-Intercept are", slope, y_intercept)
# Obstacle between active node and new node
low_lim = 0.995
high_lim = 1.015
for points in obstacle_map:
x_point = points[0]
y_point = points[1]
# Condition of limit check
if (x_point > max(active_node[0], new_node[0]) or x_point < min(active_node[0], new_node[0])) or (y_point > max(active_node[1], new_node[1]) or y_point < min(active_node[1], new_node[1])):
print("Obstacle is outside the two nodes so ignoring it")
continue
if ((slope * x_point) + y_intercept) / y_point > low_lim and ((slope * x_point) + y_intercept) / y_point < high_lim:
print("There is obstacle in the middle")
print("Value is", ((slope * x_point) + y_intercept) / y_point)
# Two lines debug
# Line between obstacle and new node
map_img = cv.line(map_img, (x_point, y_point), (new_node[0], new_node[1]), (0, 0, 255), 2)
# Line between active node and new node
map_img = cv.line(map_img, (active_node[0], active_node[1]), (new_node[0], new_node[1]), (255, 0, 255), 2)
# plt.imshow(map_img)
# print("DEBUGGGGG")
# plt.show()
plt.imshow(map_img)
plt.show()
# cv.imshow("Final Output", map_img)
# cv.waitKey(0)
# cv.destroyAllWindows()
path_found = False
# This is the count for ID of the node
# It will incremenent with every node
count = 1
dist_threshold = 200
print("Building the Rapidly-exploring Random Tree")
while not path_found:
# Taking a random point
# And I have compensated for the x and y interchange
randx = np.random.randint(low = 0, high = max_x)
randy = np.random.randint(low = 0, high = max_y)
#print("New Random number is", new_rand_point)
if map_img[randy][randx][0] == 0:
# That means black region
continue
# Random poinnt generate din the white region
new_rand_point = (randx, randy, count)
count += 1
min_temp_dist = float('inf')
# Find the nearest node from the new point
for k, v in random_tree.tree.items():
# Basically here the key is an ID of the node
# Value is the set of connected nodes
x2_m_x1 = np.power(new_rand_point[0] - random_tree.location_map[k][0], 2)
y2_m_y1 = np.power(new_rand_point[1] - random_tree.location_map[k][1], 2)
temp_dist = np.sqrt(x2_m_x1 + y2_m_y1)
if temp_dist < min_temp_dist:
min_temp_dist = temp_dist
min_id = k
if min_temp_dist > dist_threshold:
# Starting over
continue
active_node_id = min_id
#print("Active node is", active_node_id)
active_node = (int(random_tree.location_map[active_node_id][0]), int(random_tree.location_map[active_node_id][1]), active_node_id)
new_node_x = (new_rand_point[0] + random_tree.location_map[active_node_id][0]) / 2
new_node_y = (new_rand_point[1] + random_tree.location_map[active_node_id][1]) / 2
new_node = (int(new_node_x), int(new_node_y), count)
if map_img[new_node[1]][new_node[0]][0] == 0:
# That means black region
continue
# Equation of line between active node and new node
x1 = new_node[0]
y1 = new_node[1]
x2 = active_node[0]
y2 = active_node[1]
if x2 - x1 == 0.0:
continue
slope = (y2 - y1) / (x2 - x1)
y_intercept = y2 - (slope * x2)
#print("Slope and Y-Intercept of active-new line are", slope, y_intercept)
# HAVE TO CHECK IF THERE IS NO OBSTACLE BETWEEEN ACTIVE NODE AND NEW_NODE
# Basically rhe tolerance for line intersection
low_lim = 0.98
high_lim = 1.02
obstacle_found = False
#print("Finding if there is obstacle between active and new nodes")
for points in obstacle_map:
x_point = points[0]
y_point = points[1]
if (x_point > max(active_node[0], new_node[0]) or x_point < min(active_node[0], new_node[0])) or (y_point > max(active_node[1], new_node[1]) or y_point < min(active_node[1], new_node[1])):
# print("Obstacle is outside the two nodes so ignoring it")
continue
if ((slope * x_point) + y_intercept) / y_point > low_lim and ((slope * x_point) + y_intercept) / y_point < high_lim:
#print("This is the obstacle tolerance value", ((slope * x_point) + y_intercept) / y_point)
#print("There is obstacle in the middle, hence breaking")
obstacle_found = True
break
if obstacle_found:
#print("Breaking because found abstacle between two nodes")
continue
map_img = cv.rectangle(map_img, (new_node[0] - 2, new_node[1] - 2), (new_node[0] + 2, new_node[1] + 2), (0, 0, 255), -1)
# Add to the tree and add connection
random_tree.addtotree(new_node)
random_tree.addalink(new_node[2], active_node_id, min_temp_dist)
#print("New node added successfully")
# Drawing link lines
map_img = cv.line(map_img, (int(new_node[0]), int(new_node[1])), (random_tree.location_map[active_node_id][0], random_tree.location_map[active_node_id][1]), (0, 255, 0), 1)
# if count == 50:
# break
# Equation of line between goal node and new node
#(using the same equations)
x1 = goal_point[0]
y1 = goal_point[1]
x2 = new_node[0]
y2 = new_node[1]
if x2- x1 == 0.0:
continue
slope = (y2 - y1) / (x2 - x1)
y_intercept = y2 - (slope * x2)
#print("Slope and Y-Intercept of goal-new line are", slope, y_intercept)
# HAVE TO CHECK IF THE NEW NODE CAN BE CONNECTED TO GOAL
low_lim = 0.98
high_lim = 1.02
obstacle_found = False
for points in obstacle_map:
x_point = points[0]
y_point = points[1]
if (x_point > max(goal_point[0], new_node[0]) or x_point < min(goal_point[0], new_node[0])) or (y_point > max(goal_point[1], new_node[1]) or y_point < min(goal_point[1], new_node[1])):
#print("Obstacle is outside the two nodes GOAL so ignoring it")
continue
if ((slope * x_point) + y_intercept) / y_point > low_lim and ((slope * x_point) + y_intercept) / y_point < high_lim:
obstacle_found = True
#print("Cannot reach goal (0.99<gt<1.01))", ((slope * x_point) + y_intercept) / y_point)
# Cannot reach the goal from this new point
#print("This is the goal tolerance value (0.99<gt<1.01))", ((slope * x_point) + y_intercept) / y_point)
break
if not obstacle_found:
print("Finished Building the Rapidly-exploring Random Tree")
path_found = True
#map_img = cv.line(map_img, (new_node[0], new_node[1]), (goal_point[0], goal_point[1]), (0, 255, 0), 2)
#print("Location map looks like", random_tree.location_map, "\n")
#print("Tree looks like", random_tree.tree)
map_img = draw_path(map_img, new_node, goal_point)
# if count % 5 == 0:
# plt.imshow(map_img)
# plt.show()
# Checking if the distance between new point and the goal point is less
# x2_m_x1 = np.power(new_node[0] - goal_point[0], 2)
# y2_m_y1 = np.power(new_node[1] - goal_point[1], 2)
# temp_dist = np.sqrt(x2_m_x1 + y2_m_y1)
# if temp_dist < 30.0:
# path_found = True
# # This will break the loop
# plt.imshow(map_img)
# print("Showing the final map")
# plt.show()
#print(sys.argv[1])
cv.imwrite(sys.argv[1] + ".png", map_img)
cv.imshow("Final Output", map_img)
#print("Tree looks like", random_tree.tree)
cv.waitKey(0)
cv.destroyAllWindows()