Files
PathPlanning/Sampling_based_Planning/rrt_2D/plotting.py
T

118 lines
3.6 KiB
Python
Raw Normal View History

2020-06-24 13:00:25 -07:00
"""
2020-08-13 20:01:06 -07:00
Plotting tools for Sampling-based algorithms
2020-06-24 13:00:25 -07:00
@author: huiming zhou
"""
2020-06-23 19:37:10 -07:00
import matplotlib.pyplot as plt
import matplotlib.patches as patches
2020-06-24 13:43:39 -07:00
import os
import sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) +
2020-07-30 10:02:26 -07:00
"/../../Sampling_based_Planning/")
2020-06-24 13:43:39 -07:00
2020-07-30 10:02:26 -07:00
from Sampling_based_Planning.rrt_2D import env
2020-06-23 19:37:10 -07:00
class Plotting:
2020-06-24 12:55:13 -07:00
def __init__(self, x_start, x_goal):
self.xI, self.xG = x_start, x_goal
2020-06-23 19:37:10 -07:00
self.env = env.Env()
self.obs_bound = self.env.obs_boundary
self.obs_circle = self.env.obs_circle
self.obs_rectangle = self.env.obs_rectangle
2020-06-25 12:14:28 -07:00
def animation(self, nodelist, path, name, animation=False):
self.plot_grid(name)
2020-06-23 19:37:10 -07:00
self.plot_visited(nodelist, animation)
self.plot_path(path)
2020-07-24 13:40:44 -07:00
def animation_connect(self, V1, V2, path, name):
self.plot_grid(name)
self.plot_visited_connect(V1, V2)
self.plot_path(path)
2020-06-23 19:37:10 -07:00
def plot_grid(self, name):
fig, ax = plt.subplots()
for (ox, oy, w, h) in self.obs_bound:
ax.add_patch(
patches.Rectangle(
(ox, oy), w, h,
edgecolor='black',
facecolor='black',
fill=True
)
)
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:
ax.add_patch(
patches.Circle(
(ox, oy), r,
edgecolor='black',
facecolor='gray',
fill=True
)
)
plt.plot(self.xI[0], self.xI[1], "bs", linewidth=3)
plt.plot(self.xG[0], self.xG[1], "gs", linewidth=3)
2020-06-24 12:55:13 -07:00
2020-06-23 19:37:10 -07:00
plt.title(name)
plt.axis("equal")
@staticmethod
def plot_visited(nodelist, animation):
if animation:
2020-06-24 19:41:29 -07:00
count = 0
2020-06-23 19:37:10 -07:00
for node in nodelist:
2020-06-24 19:41:29 -07:00
count += 1
2020-06-23 19:37:10 -07:00
if node.parent:
plt.plot([node.parent.x, node.x], [node.parent.y, node.y], "-g")
plt.gcf().canvas.mpl_connect('key_release_event',
2020-07-27 15:58:52 -07:00
lambda event:
[exit(0) if event.key == 'escape' else None])
2020-07-24 13:40:44 -07:00
if count % 10 == 0:
plt.pause(0.001)
2020-06-23 19:37:10 -07:00
else:
for node in nodelist:
if node.parent:
plt.plot([node.parent.x, node.x], [node.parent.y, node.y], "-g")
2020-07-24 13:40:44 -07:00
@staticmethod
def plot_visited_connect(V1, V2):
len1, len2 = len(V1), len(V2)
for k in range(max(len1, len2)):
if k < len1:
if V1[k].parent:
plt.plot([V1[k].x, V1[k].parent.x], [V1[k].y, V1[k].parent.y], "-g")
if k < len2:
if V2[k].parent:
plt.plot([V2[k].x, V2[k].parent.x], [V2[k].y, V2[k].parent.y], "-g")
plt.gcf().canvas.mpl_connect('key_release_event',
lambda event: [exit(0) if event.key == 'escape' else None])
if k % 2 == 0:
plt.pause(0.001)
plt.pause(0.01)
2020-06-23 19:37:10 -07:00
@staticmethod
def plot_path(path):
2020-07-27 15:58:52 -07:00
if len(path) != 0:
plt.plot([x[0] for x in path], [x[1] for x in path], '-r', linewidth=2)
plt.pause(0.01)
2020-06-24 12:55:13 -07:00
plt.show()