plot_util3D.py 5.3 KB

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