plotting.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import matplotlib.pyplot as plt
  2. import matplotlib.patches as patches
  3. from rrt_2D import env
  4. class Plotting:
  5. def __init__(self, x_start, x_goal):
  6. self.xI, self.xG = x_start, x_goal
  7. self.env = env.Env()
  8. self.obs_bound = self.env.obs_boundary
  9. self.obs_circle = self.env.obs_circle
  10. self.obs_rectangle = self.env.obs_rectangle
  11. def animation(self, nodelist, path, animation=False):
  12. self.plot_grid("RRT")
  13. self.plot_visited(nodelist, animation)
  14. self.plot_path(path)
  15. def plot_grid(self, name):
  16. fig, ax = plt.subplots()
  17. for (ox, oy, w, h) in self.obs_bound:
  18. ax.add_patch(
  19. patches.Rectangle(
  20. (ox, oy), w, h,
  21. edgecolor='black',
  22. facecolor='black',
  23. fill=True
  24. )
  25. )
  26. for (ox, oy, w, h) in self.obs_rectangle:
  27. ax.add_patch(
  28. patches.Rectangle(
  29. (ox, oy), w, h,
  30. edgecolor='black',
  31. facecolor='gray',
  32. fill=True
  33. )
  34. )
  35. for (ox, oy, r) in self.obs_circle:
  36. ax.add_patch(
  37. patches.Circle(
  38. (ox, oy), r,
  39. edgecolor='black',
  40. facecolor='gray',
  41. fill=True
  42. )
  43. )
  44. plt.plot(self.xI[0], self.xI[1], "bs", linewidth=3)
  45. plt.plot(self.xG[0], self.xG[1], "gs", linewidth=3)
  46. plt.title(name)
  47. plt.axis("equal")
  48. @staticmethod
  49. def plot_visited(nodelist, animation):
  50. if animation:
  51. for node in nodelist:
  52. if node.parent:
  53. plt.plot([node.parent.x, node.x], [node.parent.y, node.y], "-g")
  54. plt.gcf().canvas.mpl_connect('key_release_event',
  55. lambda event: [exit(0) if event.key == 'escape' else None])
  56. plt.pause(0.001)
  57. else:
  58. for node in nodelist:
  59. if node.parent:
  60. plt.plot([node.parent.x, node.x], [node.parent.y, node.y], "-g")
  61. @staticmethod
  62. def plot_path(path):
  63. plt.plot([x[0] for x in path], [x[1] for x in path], '-r', linewidth=2)
  64. plt.pause(0.01)
  65. plt.show()