tools.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @author: huiming zhou
  5. """
  6. import matplotlib.pyplot as plt
  7. def extract_path(xI, xG, parent, actions):
  8. """
  9. Extract the path based on the relationship of nodes.
  10. :param xI: Starting node
  11. :param xG: Goal node
  12. :param parent: Relationship between nodes
  13. :param actions: Action needed for transfer between two nodes
  14. :return: The planning path
  15. """
  16. path_back = [xG]
  17. acts_back = [actions[xG]]
  18. x_current = xG
  19. while True:
  20. x_current = parent[x_current]
  21. path_back.append(x_current)
  22. acts_back.append(actions[x_current])
  23. if x_current == xI: break
  24. return list(reversed(path_back)), list(reversed(acts_back))
  25. def show_map(xI, xG, obs_map, name):
  26. obs_x = [obs_map[i][0] for i in range(len(obs_map))]
  27. obs_y = [obs_map[i][1] for i in range(len(obs_map))]
  28. plt.plot(xI[0], xI[1], "bs")
  29. plt.plot(xG[0], xG[1], "gs")
  30. plt.plot(obs_x, obs_y, "sk")
  31. plt.title(name, fontdict=None)
  32. plt.axis("equal")
  33. def showPath(xI, xG, path):
  34. """
  35. Plot the path.
  36. :param xI: Starting node
  37. :param xG: Goal node
  38. :param path: Planning path
  39. :return: A plot
  40. """
  41. path.remove(xI)
  42. path.remove(xG)
  43. path_x = [path[i][0] for i in range(len(path))]
  44. path_y = [path[i][1] for i in range(len(path))]
  45. plt.plot(path_x, path_y, linewidth='5', color='r', linestyle='-')
  46. plt.pause(0.001)
  47. plt.show()
  48. def plot_dots(x, length):
  49. plt.plot(x[0], x[1], linewidth='3', color='#808080', marker='o')
  50. plt.gcf().canvas.mpl_connect('key_release_event',
  51. lambda event: [exit(0) if event.key == 'escape' else None])
  52. if length % 15 == 0: plt.pause(0.001)