vispyHelper.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. from PyQt5.QtWidgets import *
  4. from vispy import app, visuals, scene, io
  5. import numpy as np
  6. from colorsys import hsv_to_rgb
  7. PlotAxis = scene.visuals.create_visual_node(visuals.AxisVisual)
  8. Plot3DLine = scene.visuals.create_visual_node(visuals.LinePlotVisual)
  9. Plot3DSurface = scene.visuals.create_visual_node(visuals.SurfacePlotVisual)
  10. PlotCube = scene.visuals.create_visual_node(visuals.CubeVisual)
  11. PlotMesh = scene.visuals.create_visual_node(visuals.MeshVisual)
  12. vertices, faces, normals, texcoords = io.read_mesh("gondel2.obj")
  13. arrowVert = np.array([[0,0,0],[10,0,0],[0,10,0],[0,0,10]])
  14. arrowConn = np.array([[0,1],[0,2],[0,3]])
  15. arrows = np.array([[0,0,0,10,0,0],[0,0,0,0,10,0],[0,0,0,0,0,10]])
  16. #vertex_colors = np.random.random(12)
  17. vertex_colors = np.linspace(0, 1, len(vertices))
  18. vertex_colors = np.array([hsv_to_rgb(c, 1, 1) for c in vertex_colors])
  19. class viewQuad():
  20. def __init__(self, object, follow = True):
  21. self.follow = follow
  22. self.canvas = scene.SceneCanvas(keys='interactive', show=False)
  23. self.grid = self.canvas.central_widget.add_grid(margin=10)
  24. self.views = []
  25. self.views.append(viewOrtho(self.grid.add_grid(row=0, col=0), 3, self.follow))
  26. self.views.append(viewOrtho(self.grid.add_grid(row=1, col=0), 2, self.follow))
  27. self.views.append(viewOrtho(self.grid.add_grid(row=1, col=1), 1, self.follow))
  28. self.views.append(viewRot(self.grid.add_grid(row=0,col=1), self.follow))
  29. for view in self.views:
  30. view.grid.border_color = (0.5, 0.5, 0.5, 1)
  31. object.setLayout(QVBoxLayout())
  32. object.layout().addWidget(self.canvas.native)
  33. def update(self, data3d, rot):
  34. for view in self.views:
  35. view.update(data3d, rot)
  36. class view3D():
  37. def __init__(self, object, follow = False):
  38. self.canvas = scene.SceneCanvas(keys='interactive', show=False)
  39. self.view = self.canvas.central_widget.add_view()
  40. self.view.camera = scene.cameras.FlyCamera(parent=self.view.scene, fov=60, name='Fly')
  41. size = 100000
  42. s = np.linspace(-size, size, 50)
  43. YY, XX = np.meshgrid(s, s)
  44. z = -0.1 * np.sqrt(YY*YY + XX*XX)
  45. c=np.zeros(shape=(50,50,4))
  46. for x in range(50):
  47. for y in range(50):
  48. if x==25 and y>=25:
  49. c[x][y] = np.array([0,1,0,1])
  50. elif x>=25 and y==25:
  51. c[x][y] = np.array([1,0,0,1])
  52. else:
  53. c[x][y] = np.array([0,0.3,1,0.5])
  54. #print(c)
  55. # colors=c,
  56. self.groundPlane = Plot3DSurface(x=s, y=s, z=z, parent=self.view.scene)
  57. self.dataPlot = Plot3DLine(np.array([[0,0,0]], np.float32), parent=self.view.scene)
  58. self.dataPlot2 = Plot3DLine(np.array([[0,0,0]], np.float32), parent=self.view.scene)
  59. self.carPlot = PlotMesh(vertices, faces, shading='smooth',
  60. vertex_colors=vertex_colors, parent=self.view.scene)
  61. #self.carPlot = PlotCube((1,1.5,0.5), edge_color=(1,1,1,1), face_colors=vertex_colors, parent=self.view.scene)
  62. self.carPlot.transform = visuals.transforms.MatrixTransform()
  63. object.setLayout(QVBoxLayout())
  64. object.layout().addWidget(self.canvas.native)
  65. self.follow = follow
  66. def update(self, data3d, rot):
  67. rotX, rotY, rotZ = rot
  68. self.dataPlot.set_data(data3d[0], width=2.0, color='red', marker_size=0,face_color=(0.2, 0.2, 1, 0.8))
  69. self.dataPlot2.set_data(data3d[1], width=2.0, color='green', marker_size=0,face_color=(0.2, 0.2, 1, 0.8))
  70. self.carPlot.transform.reset()
  71. self.carPlot.transform.rotate(rotX, (1,0,0))
  72. self.carPlot.transform.rotate(rotY, (0,1,0))
  73. self.carPlot.transform.rotate(rotZ, (0,0,1))
  74. self.carPlot.transform.translate(data3d[0][-1])
  75. if self.follow:
  76. self.view.camera.center = tuple(np.add(data3d[0][-1], [5,5,5]))
  77. class viewRot():
  78. def __init__(self, grid, follow = True):
  79. self.grid = grid
  80. self.view = self.grid.add_view(row=0,col=0)
  81. self.view.camera = scene.cameras.TurntableCamera(parent=self.view.scene)
  82. self.dataPlot = Plot3DLine(np.array([[0,0,0]], np.float32), parent=self.view.scene)
  83. self.dataPlot2 = Plot3DLine(np.array([[0,0,0]], np.float32), parent=self.view.scene)
  84. #self.gridlines = scene.visuals.GridLines(scale=(10,10), color=(0,1,0), parent=self.view.scene)
  85. self.carPlot = PlotMesh(vertices, faces,
  86. vertex_colors=vertex_colors, shading='smooth', parent=self.view.scene)
  87. self.carPlot.transform = visuals.transforms.MatrixTransform()
  88. self.follow = follow
  89. def update(self, data3d, rot):
  90. rotX, rotY, rotZ = rot
  91. self.dataPlot.set_data(data3d[0], width=2.0, color='red', marker_size=0)
  92. self.dataPlot2.set_data(data3d[1], width=2.0, color='green', marker_size=0)
  93. print(data3d[1])
  94. self.carPlot.transform.reset()
  95. self.carPlot.transform.rotate(rotX, (1,0,0))
  96. self.carPlot.transform.rotate(rotY, (0,1,0))
  97. self.carPlot.transform.rotate(rotZ, (0,0,1))
  98. self.carPlot.transform.translate(data3d[0][-1])
  99. if self.follow:
  100. self.view.camera.center = tuple(data3d[0][-1])
  101. class viewOrtho():
  102. def __init__(self, grid, up=3, follow = True):
  103. self.follow = follow
  104. self.up = up
  105. self.grid = grid
  106. if self.up == 3: #top
  107. la='Y'
  108. lb='X'
  109. ti="Top"
  110. elif self.up == 2:
  111. la='Y'
  112. lb='Z'
  113. ti="Front"
  114. else:
  115. la='X'
  116. lb='Z'
  117. ti="Side"
  118. title = scene.Label(ti, color='white')
  119. title.height_max = 40
  120. self.grid.add_widget(title, row=0, col=0, col_span=2)
  121. yaxis = scene.AxisWidget(orientation='left',
  122. axis_label=lb+' Axis',
  123. axis_font_size=12,
  124. axis_label_margin=50,
  125. tick_label_margin=5)
  126. yaxis.width_max = 80
  127. self.grid.add_widget(yaxis, row=1,col=0)
  128. xaxis = scene.AxisWidget(orientation='bottom',
  129. axis_label=la+' Axis',
  130. axis_font_size=12,
  131. axis_label_margin=50,
  132. tick_label_margin=5)
  133. xaxis.height_max = 80
  134. self.grid.add_widget(xaxis, row=2, col=1)
  135. right_padding = self.grid.add_widget(row=1, col=2, row_span=1)
  136. right_padding.width_max = 50
  137. self.view = self.grid.add_view(row=1,col=1)
  138. self.view.camera = scene.cameras.PanZoomCamera(rect=(-5,-5,5,5), aspect=1, parent=self.view.scene)#flip=(0,1,0),
  139. yaxis.link_view(self.view)
  140. xaxis.link_view(self.view)
  141. self.gridlines = scene.visuals.GridLines(scale=(1,1), color=(0,1,0), parent=self.view.scene)
  142. self.dataPlot = Plot3DLine(np.array([[0,0]], np.float32), parent=self.view.scene)
  143. self.dataPlot2 = Plot3DLine(np.array([[0,0]], np.float32), parent=self.view.scene)
  144. self.carPlot = PlotMesh(vertices, faces,
  145. vertex_colors=vertex_colors, shading='smooth', parent=self.view.scene)
  146. self.carPlot.transform = visuals.transforms.MatrixTransform()
  147. def update(self, data3d, rot):
  148. rotX, rotY, rotZ = rot
  149. if self.up == 3: #top
  150. data2d = data3d[0][:,:2] # X Y
  151. data2d2 = data3d[1][:,:2] # X Y
  152. elif self.up == 2: #front
  153. data2d = data3d[0][:,0:3:2] # X Z
  154. data2d2 = data3d[1][:,0:3:2] # X Z
  155. else: #side
  156. data2d = data3d[0][:,1:] # Y Z
  157. data2d2 = data3d[1][:,1:] # Y Z
  158. self.dataPlot.set_data(data2d, width=2.0, color='red', marker_size=0)
  159. self.dataPlot2.set_data(data2d2, width=2.0, color='green', marker_size=0)
  160. self.carPlot.transform.reset()
  161. self.carPlot.transform.set_ortho(-1, 1, -1, 1, -1, 1)
  162. rotX, rotY, rotZ = (-rotX), (-rotY), (rotZ)
  163. self.carPlot.transform.rotate(rotX, (1,0,0))
  164. self.carPlot.transform.rotate(rotY, (0,1,0))
  165. self.carPlot.transform.rotate(rotZ, (0,0,1))
  166. if self.up <= 2:#front
  167. self.carPlot.transform.rotate(90, (1,0,0))
  168. if self.up == 1:#side
  169. self.carPlot.transform.rotate(90, (0,1,0))
  170. self.carPlot.transform.translate(data2d[-1])
  171. if self.follow:
  172. self.view.camera.center = tuple(data2d[-1])