-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdraw.py
More file actions
79 lines (63 loc) · 3.25 KB
/
draw.py
File metadata and controls
79 lines (63 loc) · 3.25 KB
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
import math
import heapq
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from time import time
from PIL import Image, ImageDraw
from heapq import heappop, heappush
def draw(grid_map, start = None, goal = None, path = None, nodes_opened = None, nodes_expanded = None, nodes_reexpanded = None, pt = False):
'''
Auxiliary function that visualizes the environment, the path and
the open/expanded/re-expanded nodes.
The function assumes that nodes_opened/nodes_expanded/nodes_reexpanded
are iterable collestions of SearchNodes
'''
k = 5
height, width = grid_map.get_size()
h_im = height * k
w_im = width * k
im = Image.new('RGB', (w_im, h_im), color = 'white')
draw = ImageDraw.Draw(im)
points = [[0,1], [1, 0], [1, 1], [-1, -1], [-1, 1], [1, -1], [0, -1], [-1, 0],
[0,2], [2, 0], [2, 2], [-2, -2], [-2, 2], [2, -2], [0, -2], [-2, 0]]
for i in range(height):
for j in range(width):
if not grid_map.traversable(i, j):
draw.rectangle((j * k, i * k, (j + 1) * k - 1, (i + 1) * k - 1), fill=( 0, 0, 0 ))
if nodes_opened is not None:
for node in nodes_opened:
draw.rectangle((node.j * k, node.i * k, (node.j + 1) * k - 1, (node.i + 1) * k - 1), fill=(213, 219, 219), width=0)
if nodes_expanded is not None:
for node in nodes_expanded:
draw.rectangle((node.j * k, node.i * k, (node.j + 1) * k - 1, (node.i + 1) * k - 1), fill=(131, 145, 146), width=0)
if nodes_reexpanded is not None:
for node in nodes_reexpanded:
draw.rectangle((node.j * k, node.i * k, (node.j + 1) * k - 1, (node.i + 1) * k - 1), fill=(255, 145, 146), width=0)
if path is not None:
for step in path:
if (step is not None):
if (grid_map.traversable(step.i, step.j)):
draw.rectangle((step.j * k, step.i * k, (step.j + 1) * k - 1, (step.i + 1) * k - 1), fill=(231, 76, 60), width=0)
else:
draw.rectangle((step.j * k, step.i * k, (step.j + 1) * k - 1, (step.i + 1) * k - 1), fill=(230, 126, 34), width=0)
if (start is not None) and (grid_map.traversable(start.i, start.j)):
draw.rectangle((start.j * k, start.i * k, (start.j + 1) * k - 1, (start.i + 1) * k - 1), fill=(40, 180, 99), width=0)
if (goal is not None) and (grid_map.traversable(goal.i, goal.j)):
draw.rectangle((goal.j * k, goal.i * k, (goal.j + 1) * k - 1, (goal.i + 1) * k - 1), fill=(255, 0, 0), width=0)
if pt:
for point in points:
if grid_map.in_bounds(start.i + point[0], start.j + point[1]):
i = start.i + point[0]
j = start.j + point[1]
draw.rectangle((j * k, i * k, (j + 1) * k - 1, (i + 1) * k - 1), fill=(231, 76, 60))
if grid_map.in_bounds(goal.i + point[0], goal.j + point[1]):
i = goal.i + point[0]
j = goal.j + point[1]
draw.rectangle((j * k, i * k, (j + 1) * k - 1, (i + 1) * k - 1), fill=(231, 76, 60))
_, ax = plt.subplots(dpi=150)
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
plt.imshow(np.asarray(im))
plt.show()