Files
PathPlanning/Search_based_Planning/Search_2D/plotting.py
T

163 lines
4.4 KiB
Python
Raw Normal View History

2020-06-27 00:38:38 -07:00
"""
Plot tools 2D
@author: huiming zhou
"""
import os
import sys
2020-06-20 12:46:49 -07:00
import matplotlib.pyplot as plt
2020-06-27 00:38:38 -07:00
sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
2020-07-30 10:02:26 -07:00
"/../../Search_based_Planning/")
2020-06-27 00:38:38 -07:00
from Search_2D import env
2020-06-20 12:46:49 -07:00
2020-06-21 21:50:11 -07:00
2020-06-25 14:21:36 -07:00
class Plotting:
2020-06-20 19:12:34 -07:00
def __init__(self, xI, xG):
self.xI, self.xG = xI, xG
self.env = env.Env()
self.obs = self.env.obs_map()
2020-06-20 12:46:49 -07:00
2020-06-20 19:12:34 -07:00
def animation(self, path, visited, name):
self.plot_grid(name)
self.plot_visited(visited)
self.plot_path(path)
2020-06-27 00:21:29 -07:00
plt.show()
2020-06-20 19:12:34 -07:00
2020-06-28 20:41:44 -07:00
def animation_lrta(self, path, visited, name):
self.plot_grid(name)
cl = self.color_list_2()
path_combine = []
for k in range(len(path)):
self.plot_visited(visited[k], cl[k])
plt.pause(0.2)
self.plot_path(path[k])
path_combine += path[k]
plt.pause(0.2)
2020-06-28 22:41:52 -07:00
if self.xI in path_combine:
path_combine.remove(self.xI)
2020-06-28 20:41:44 -07:00
self.plot_path(path_combine)
plt.show()
def animation_ara_star(self, path, visited, name):
self.plot_grid(name)
cl_v, cl_p = self.color_list()
for k in range(len(path)):
self.plot_visited(visited[k], cl_v[k])
self.plot_path(path[k], cl_p[k], True)
plt.pause(0.5)
plt.show()
def animation_bi_astar(self, path, v_fore, v_back, name):
self.plot_grid(name)
self.plot_visited_bi(v_fore, v_back)
self.plot_path(path)
plt.show()
2020-06-20 19:12:34 -07:00
def plot_grid(self, name):
2020-06-30 14:12:07 -07:00
obs_x = [x[0] for x in self.obs]
obs_y = [x[1] for x in self.obs]
2020-06-20 19:12:34 -07:00
plt.plot(self.xI[0], self.xI[1], "bs")
plt.plot(self.xG[0], self.xG[1], "gs")
plt.plot(obs_x, obs_y, "sk")
plt.title(name)
plt.axis("equal")
2020-06-27 00:21:29 -07:00
def plot_visited(self, visited, cl='gray'):
if self.xI in visited:
visited.remove(self.xI)
if self.xG in visited:
visited.remove(self.xG)
2020-06-20 19:12:34 -07:00
count = 0
for x in visited:
count += 1
2020-07-04 18:23:30 -07:00
plt.plot(x[0], x[1], color=cl, marker='o')
2020-06-25 15:43:47 -07:00
plt.gcf().canvas.mpl_connect('key_release_event',
lambda event: [exit(0) if event.key == 'escape' else None])
2020-06-20 19:12:34 -07:00
if count < len(visited) / 3:
2020-07-04 18:23:30 -07:00
length = 20
2020-06-20 19:12:34 -07:00
elif count < len(visited) * 2 / 3:
2020-07-02 11:36:07 -07:00
length = 30
2020-07-04 18:23:30 -07:00
else:
length = 40
#
# length = 15
2020-06-27 00:21:29 -07:00
if count % length == 0:
plt.pause(0.001)
plt.pause(0.01)
def plot_path(self, path, cl='r', flag=False):
2020-06-20 19:12:34 -07:00
path_x = [path[i][0] for i in range(len(path))]
path_y = [path[i][1] for i in range(len(path))]
2020-06-27 00:21:29 -07:00
if not flag:
2020-07-04 18:23:30 -07:00
plt.plot(path_x, path_y, linewidth='3', color='r')
2020-06-27 00:21:29 -07:00
else:
2020-07-04 18:23:30 -07:00
plt.plot(path_x, path_y, linewidth='3', color=cl)
plt.plot(self.xI[0], self.xI[1], "bs")
plt.plot(self.xG[0], self.xG[1], "gs")
2020-06-27 00:21:29 -07:00
2020-06-20 19:12:34 -07:00
plt.pause(0.01)
2020-06-27 00:21:29 -07:00
2020-06-27 17:08:55 -07:00
def plot_visited_bi(self, v_fore, v_back):
if self.xI in v_fore:
2020-07-02 11:36:07 -07:00
v_fore.remove(self.xI)
2020-06-27 17:08:55 -07:00
if self.xG in v_back:
2020-07-02 11:36:07 -07:00
v_back.remove(self.xG)
2020-06-27 17:08:55 -07:00
len_fore, len_back = len(v_fore), len(v_back)
for k in range(max(len_fore, len_back)):
if k < len_fore:
plt.plot(v_fore[k][0], v_fore[k][1], linewidth='3', color='gray', marker='o')
if k < len_back:
plt.plot(v_back[k][0], v_back[k][1], linewidth='3', color='cornflowerblue', marker='o')
plt.gcf().canvas.mpl_connect('key_release_event',
lambda event: [exit(0) if event.key == 'escape' else None])
if k % 10 == 0:
plt.pause(0.001)
plt.pause(0.01)
2020-06-27 00:21:29 -07:00
@staticmethod
def color_list():
2020-06-28 20:41:44 -07:00
cl_v = ['silver',
'wheat',
'lightskyblue',
2020-06-29 10:26:52 -07:00
'royalblue',
2020-06-28 20:41:44 -07:00
'slategray']
cl_p = ['gray',
'orange',
'deepskyblue',
'red',
'm']
2020-06-27 00:21:29 -07:00
return cl_v, cl_p
2020-06-28 20:41:44 -07:00
@staticmethod
def color_list_2():
cl = ['silver',
'steelblue',
'dimgray',
'cornflowerblue',
'dodgerblue',
'royalblue',
'plum',
'mediumslateblue',
'mediumpurple',
'blueviolet',
]
return cl