plot_util3D.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.clear()
  61. # drawing objects
  62. draw_Spheres(ax, initparams.env.balls)
  63. draw_block_list(ax, initparams.env.blocks)
  64. draw_block_list(ax, np.array([initparams.env.boundary]),alpha=0)
  65. # draw_line(ax,edges,visibility=0.25)
  66. draw_line(ax,Path,color='r')
  67. ax.scatter3D(V[:, 0], V[:, 1], V[:, 2], s=2, color='g',)
  68. ax.plot(start[0:1], start[1:2], start[2:], 'go', markersize=7, markeredgecolor='k')
  69. ax.plot(goal[0:1], goal[1:2], goal[2:], 'ro', markersize=7, markeredgecolor='k')
  70. # adjust the aspect ratio
  71. xmin, xmax = initparams.env.boundary[0], initparams.env.boundary[3]
  72. ymin, ymax = initparams.env.boundary[1], initparams.env.boundary[4]
  73. zmin, zmax = initparams.env.boundary[2], initparams.env.boundary[5]
  74. dx, dy, dz = xmax-xmin, ymax-ymin, zmax-zmin
  75. ax.get_proj = make_get_proj(ax,1*dx, 1*dy, 2*dy)
  76. plt.xlabel('x')
  77. plt.ylabel('y')
  78. plt.pause(0.0001)
  79. def make_get_proj(self, rx, ry, rz):
  80. '''
  81. Return a variation on :func:`~mpl_toolkit.mplot2d.axes3d.Axes3D.getproj` that
  82. makes the box aspect ratio equal to *rx:ry:rz*, using an axes object *self*.
  83. '''
  84. rm = max(rx, ry, rz)
  85. kx = rm / rx; ky = rm / ry; kz = rm / rz
  86. # Copied directly from mpl_toolkit/mplot3d/axes3d.py. New or modified lines are
  87. # marked by ##
  88. def get_proj():
  89. relev, razim = np.pi * self.elev/180, np.pi * self.azim/180
  90. xmin, xmax = self.get_xlim3d()
  91. ymin, ymax = self.get_ylim3d()
  92. zmin, zmax = self.get_zlim3d()
  93. # transform to uniform world coordinates 0-1.0,0-1.0,0-1.0
  94. worldM = proj3d.world_transformation(xmin, xmax,
  95. ymin, ymax,
  96. zmin, zmax)
  97. ratio = 0.5
  98. # adjust the aspect ratio ##
  99. aspectM = proj3d.world_transformation(-kx + 1, kx, ##
  100. -ky + 1, ky, ##
  101. -kz + 1, kz) ##
  102. # look into the middle of the new coordinates
  103. R = np.array([0.5, 0.5, 0.5])
  104. xp = R[0] + np.cos(razim) * np.cos(relev) * self.dist *ratio
  105. yp = R[1] + np.sin(razim) * np.cos(relev) * self.dist *ratio
  106. zp = R[2] + np.sin(relev) * self.dist *ratio
  107. E = np.array((xp, yp, zp))
  108. self.eye = E
  109. self.vvec = R - E
  110. self.vvec = self.vvec / np.linalg.norm(self.vvec)
  111. if abs(relev) > np.pi/2:
  112. # upside down
  113. V = np.array((0, 0, -1))
  114. else:
  115. V = np.array((0, 0, 1))
  116. zfront, zback = -self.dist *ratio, self.dist *ratio
  117. viewM = proj3d.view_transformation(E, R, V)
  118. perspM = proj3d.persp_transformation(zfront, zback)
  119. M0 = np.dot(viewM, np.dot(aspectM, worldM)) ##
  120. M = np.dot(perspM, M0)
  121. return M
  122. return get_proj