tools.py 1.9 KB

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