plot_util3D.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # plotting
  2. import matplotlib.pyplot as plt
  3. from mpl_toolkits.mplot3d import Axes3D
  4. from mpl_toolkits.mplot3d.art3d import Poly3DCollection
  5. import mpl_toolkits.mplot3d as plt3d
  6. from mpl_toolkits.mplot3d import proj3d
  7. import numpy as np
  8. def CreateSphere(center,r):
  9. u = np.linspace(0,2* np.pi,30)
  10. v = np.linspace(0,np.pi,30)
  11. x = np.outer(np.cos(u),np.sin(v))
  12. y = np.outer(np.sin(u),np.sin(v))
  13. z = np.outer(np.ones(np.size(u)),np.cos(v))
  14. x, y, z = r*x + center[0], r*y + center[1], r*z + center[2]
  15. return (x,y,z)
  16. def draw_Spheres(ax,balls):
  17. for i in balls:
  18. (xs,ys,zs) = CreateSphere(i[0:3],i[-1])
  19. ax.plot_wireframe(xs, ys, zs, alpha=0.15,color="b")
  20. def draw_block_list(ax, blocks ,color=None,alpha=0.15):
  21. '''
  22. drawing the blocks on the graph
  23. '''
  24. v = np.array([[0, 0, 0], [1, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 1], [1, 0, 1], [1, 1, 1], [0, 1, 1]],
  25. dtype='float')
  26. f = np.array([[0, 1, 5, 4], [1, 2, 6, 5], [2, 3, 7, 6], [3, 0, 4, 7], [0, 1, 2, 3], [4, 5, 6, 7]])
  27. n = blocks.shape[0]
  28. d = blocks[:, 3:6] - blocks[:, :3]
  29. vl = np.zeros((8 * n, 3))
  30. fl = np.zeros((6 * n, 4), dtype='int64')
  31. for k in range(n):
  32. vl[k * 8:(k + 1) * 8, :] = v * d[k] + blocks[k, :3]
  33. fl[k * 6:(k + 1) * 6, :] = f + k * 8
  34. if type(ax) is Poly3DCollection:
  35. ax.set_verts(vl[fl])
  36. else:
  37. pc = Poly3DCollection(vl[fl], alpha=alpha, linewidths=1, edgecolors='k')
  38. pc.set_facecolor(color)
  39. h = ax.add_collection3d(pc)
  40. return h
  41. def draw_line(ax,SET,visibility=1,color=None):
  42. if SET != []:
  43. for i in SET:
  44. xs = i[0][0], i[1][0]
  45. ys = i[0][1], i[1][1]
  46. zs = i[0][2], i[1][2]
  47. line = plt3d.art3d.Line3D(xs, ys, zs, alpha=visibility, color=color)
  48. ax.add_line(line)
  49. def visualization(initparams):
  50. if initparams.ind % 20 == 0 or initparams.done:
  51. V = np.array(list(initparams.V))
  52. # E = initparams.E
  53. Path = np.array(initparams.Path)
  54. start = initparams.env.start
  55. goal = initparams.env.goal
  56. # edges = E.get_edge()
  57. # generate axis objects
  58. ax = plt.subplot(111, projection='3d')
  59. #ax.view_init(elev=0.+ 0.03*initparams.ind/(2*np.pi), azim=90 + 0.03*initparams.ind/(2*np.pi))
  60. ax.view_init(elev=0., azim=90)
  61. ax.clear()
  62. # drawing objects
  63. draw_Spheres(ax, initparams.env.balls)
  64. draw_block_list(ax, initparams.env.blocks)
  65. draw_block_list(ax, np.array([initparams.env.boundary]),alpha=0)
  66. # draw_line(ax,edges,visibility=0.25)
  67. draw_line(ax,Path,color='r')
  68. if len(V) > 0:
  69. ax.scatter3D(V[:, 0], V[:, 1], V[:, 2], s=2, color='g',)
  70. ax.plot(start[0:1], start[1:2], start[2:], 'go', markersize=7, markeredgecolor='k')
  71. ax.plot(goal[0:1], goal[1:2], goal[2:], 'ro', markersize=7, markeredgecolor='k')
  72. # adjust the aspect ratio
  73. xmin, xmax = initparams.env.boundary[0], initparams.env.boundary[3]
  74. ymin, ymax = initparams.env.boundary[1], initparams.env.boundary[4]
  75. zmin, zmax = initparams.env.boundary[2], initparams.env.boundary[5]
  76. dx, dy, dz = xmax-xmin, ymax-ymin, zmax-zmin
  77. ax.get_proj = make_get_proj(ax,1*dx, 1*dy, 2*dy)
  78. plt.xlabel('x')
  79. plt.ylabel('y')
  80. plt.pause(0.0001)
  81. def make_get_proj(self, rx, ry, rz):
  82. '''
  83. Return a variation on :func:`~mpl_toolkit.mplot2d.axes3d.Axes3D.getproj` that
  84. makes the box aspect ratio equal to *rx:ry:rz*, using an axes object *self*.
  85. '''
  86. rm = max(rx, ry, rz)
  87. kx = rm / rx; ky = rm / ry; kz = rm / rz
  88. # Copied directly from mpl_toolkit/mplot3d/axes3d.py. New or modified lines are
  89. # marked by ##
  90. def get_proj():
  91. relev, razim = np.pi * self.elev/180, np.pi * self.azim/180
  92. xmin, xmax = self.get_xlim3d()
  93. ymin, ymax = self.get_ylim3d()
  94. zmin, zmax = self.get_zlim3d()
  95. # transform to uniform world coordinates 0-1.0,0-1.0,0-1.0
  96. worldM = proj3d.world_transformation(xmin, xmax,
  97. ymin, ymax,
  98. zmin, zmax)
  99. ratio = 0.5
  100. # adjust the aspect ratio ##
  101. aspectM = proj3d.world_transformation(-kx + 1, kx, ##
  102. -ky + 1, ky, ##
  103. -kz + 1, kz) ##
  104. # look into the middle of the new coordinates
  105. R = np.array([0.5, 0.5, 0.5])
  106. xp = R[0] + np.cos(razim) * np.cos(relev) * self.dist *ratio
  107. yp = R[1] + np.sin(razim) * np.cos(relev) * self.dist *ratio
  108. zp = R[2] + np.sin(relev) * self.dist *ratio
  109. E = np.array((xp, yp, zp))
  110. self.eye = E
  111. self.vvec = R - E
  112. self.vvec = self.vvec / np.linalg.norm(self.vvec)
  113. if abs(relev) > np.pi/2:
  114. # upside down
  115. V = np.array((0, 0, -1))
  116. else:
  117. V = np.array((0, 0, 1))
  118. zfront, zback = -self.dist *ratio, self.dist *ratio
  119. viewM = proj3d.view_transformation(E, R, V)
  120. perspM = proj3d.persp_transformation(zfront, zback)
  121. M0 = np.dot(viewM, np.dot(aspectM, worldM)) ##
  122. M = np.dot(perspM, M0)
  123. return M
  124. return get_proj