-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathplotting.py
114 lines (95 loc) · 3.44 KB
/
plotting.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
"""
Plotting tools for Sampling-based algorithms
@author: huiming zhou
"""
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import os
import sys
import env
class Plotting:
def __init__(self, x_start, x_goal):
self.xI, self.xG = x_start, x_goal
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
def animation(self, nodelist, path, name, animation=False):
self.plot_grid(name)
self.plot_visited(nodelist, animation)
self.plot_path(path)
def animation_online(self, nodelist, name, animation=False):
self.plot_grid(name)
self.plot_visited(nodelist, animation)
plt.pause(1.0)
plt.close()
def animation_connect(self, V1, V2, path, name):
self.plot_grid(name)
self.plot_visited_connect(V1, V2)
self.plot_path(path)
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], "rs", linewidth=3)
plt.title(name)
plt.axis("equal")
@staticmethod
def plot_visited(nodelist, animation):
if animation:
count = 0
for node in nodelist:
count += 1
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],
)
if count % 10 == 0:
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")
@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)
@staticmethod
def plot_path(path):
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)
plt.savefig("LQR-CBF_result.PNG")
plt.show()