Files
PathPlanning/Sampling-based Planning/plotting.py
T

80 lines
2.4 KiB
Python
Raw Normal View History

2020-06-22 11:41:03 -07:00
import matplotlib.pyplot as plt
2020-06-22 20:48:47 -07:00
import matplotlib.patches as patches
2020-06-22 11:41:03 -07:00
import env
class Plotting:
def __init__(self, xI, xG):
self.xI, self.xG = xI, xG
self.env = env.Env()
2020-06-22 20:48:47 -07:00
self.obs_bound = self.env.obs_boundary
2020-06-23 18:46:44 -07:00
self.obs_circle = self.env.obs_circle
self.obs_rectangle = self.env.obs_rectangle
2020-06-22 11:41:03 -07:00
2020-06-23 18:46:44 -07:00
def animation(self, nodelist, path, animation=False):
2020-06-22 22:53:28 -07:00
if path is None:
print("No path found!")
return
2020-06-23 18:46:44 -07:00
self.plot_grid("RRT")
self.plot_visited(nodelist, animation)
2020-06-22 22:53:28 -07:00
self.plot_path(path)
2020-06-22 11:41:03 -07:00
def plot_grid(self, name):
2020-06-22 20:48:47 -07:00
fig, ax = plt.subplots()
2020-06-22 22:53:28 -07:00
2020-06-23 18:46:44 -07:00
for (ox, oy, w, h) in self.obs_bound:
2020-06-22 20:48:47 -07:00
ax.add_patch(
patches.Rectangle(
2020-06-23 18:46:44 -07:00
(ox, oy), w, h,
2020-06-22 20:48:47 -07:00
edgecolor='black',
facecolor='black',
fill=True
)
)
2020-06-23 18:46:44 -07:00
for (ox, oy, w, h) in self.obs_rectangle:
ax.add_patch(
patches.Rectangle(
(ox, oy), w, h,
edgecolor='black',
facecolor='gray',
fill=True
)
)
for (ox, oy, r) in self.obs_circle:
2020-06-22 20:48:47 -07:00
ax.add_patch(
patches.Circle(
2020-06-23 18:46:44 -07:00
(ox, oy), r,
2020-06-22 22:53:28 -07:00
edgecolor='black',
2020-06-22 20:48:47 -07:00
facecolor='gray',
fill=True
)
)
2020-06-22 22:53:28 -07:00
plt.plot(self.xI[0], self.xI[1], "bs", linewidth=3)
plt.plot(self.xG[0], self.xG[1], "gs", linewidth=3)
2020-06-22 11:41:03 -07:00
plt.title(name)
plt.axis("equal")
2020-06-22 22:53:28 -07:00
@staticmethod
2020-06-23 18:46:44 -07:00
def plot_visited(nodelist, animation):
if animation:
for node in nodelist:
if node.parent:
plt.plot([node.parent.x, node.x], [node.parent.y, node.y], "-g")
plt.gcf().canvas.mpl_connect('key_release_event',
lambda event: [exit(0) if event.key == 'escape' else None])
plt.pause(0.001)
else:
for node in nodelist:
if node.parent:
plt.plot([node.parent.x, node.x], [node.parent.y, node.y], "-g")
2020-06-22 11:41:03 -07:00
2020-06-22 22:53:28 -07:00
@staticmethod
def plot_path(path):
plt.plot([x[0] for x in path], [x[1] for x in path], '-r', linewidth=2)
2020-06-22 11:41:03 -07:00
plt.pause(0.01)
plt.show()