vispyHelper.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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.carPlot = PlotMesh(vertices, faces, shading='smooth',
  59. vertex_colors=vertex_colors, parent=self.view.scene)
  60. #self.carPlot = PlotCube((1,1.5,0.5), edge_color=(1,1,1,1), face_colors=vertex_colors, parent=self.view.scene)
  61. self.carPlot.transform = visuals.transforms.MatrixTransform()
  62. object.setLayout(QVBoxLayout())
  63. object.layout().addWidget(self.canvas.native)
  64. self.follow = follow
  65. def update(self, data3d, rot):
  66. rotX, rotY, rotZ = rot
  67. self.dataPlot.set_data(data3d, width=2.0, color='red', marker_size=0,face_color=(0.2, 0.2, 1, 0.8))
  68. self.carPlot.transform.reset()
  69. self.carPlot.transform.rotate(rotX, (1,0,0))
  70. self.carPlot.transform.rotate(rotY, (0,1,0))
  71. self.carPlot.transform.rotate(rotZ, (0,0,1))
  72. self.carPlot.transform.translate(data3d[-1])
  73. if self.follow:
  74. self.view.camera.center = tuple(np.add(data3d[-1], [5,5,5]))
  75. class viewRot():
  76. def __init__(self, grid, follow = True):
  77. self.grid = grid
  78. self.view = self.grid.add_view(row=0,col=0)
  79. self.view.camera = scene.cameras.TurntableCamera(parent=self.view.scene)
  80. self.dataPlot = Plot3DLine(np.array([[0,0,0]], np.float32), parent=self.view.scene)
  81. #self.gridlines = scene.visuals.GridLines(scale=(10,10), color=(0,1,0), parent=self.view.scene)
  82. self.carPlot = PlotMesh(vertices, faces,
  83. vertex_colors=vertex_colors, shading='smooth', parent=self.view.scene)
  84. self.carPlot.transform = visuals.transforms.MatrixTransform()
  85. self.follow = follow
  86. def update(self, data3d, rot):
  87. rotX, rotY, rotZ = rot
  88. self.dataPlot.set_data(data3d, width=2.0, color='red', marker_size=0)
  89. self.carPlot.transform.reset()
  90. self.carPlot.transform.rotate(rotX, (1,0,0))
  91. self.carPlot.transform.rotate(rotY, (0,1,0))
  92. self.carPlot.transform.rotate(rotZ, (0,0,1))
  93. self.carPlot.transform.translate(data3d[-1])
  94. if self.follow:
  95. self.view.camera.center = tuple(data3d[-1])
  96. class viewOrtho():
  97. def __init__(self, grid, up=3, follow = True):
  98. self.follow = follow
  99. self.up = up
  100. self.grid = grid
  101. if self.up == 3: #top
  102. la='Y'
  103. lb='X'
  104. ti="Top"
  105. elif self.up == 2:
  106. la='Y'
  107. lb='Z'
  108. ti="Front"
  109. else:
  110. la='X'
  111. lb='Z'
  112. ti="Side"
  113. title = scene.Label(ti, color='white')
  114. title.height_max = 40
  115. self.grid.add_widget(title, row=0, col=0, col_span=2)
  116. yaxis = scene.AxisWidget(orientation='left',
  117. axis_label=lb+' Axis',
  118. axis_font_size=12,
  119. axis_label_margin=50,
  120. tick_label_margin=5)
  121. yaxis.width_max = 80
  122. self.grid.add_widget(yaxis, row=1,col=0)
  123. xaxis = scene.AxisWidget(orientation='bottom',
  124. axis_label=la+' Axis',
  125. axis_font_size=12,
  126. axis_label_margin=50,
  127. tick_label_margin=5)
  128. xaxis.height_max = 80
  129. self.grid.add_widget(xaxis, row=2, col=1)
  130. right_padding = self.grid.add_widget(row=1, col=2, row_span=1)
  131. right_padding.width_max = 50
  132. self.view = self.grid.add_view(row=1,col=1)
  133. self.view.camera = scene.cameras.PanZoomCamera(rect=(-5,-5,5,5), aspect=1, parent=self.view.scene)#flip=(0,1,0),
  134. yaxis.link_view(self.view)
  135. xaxis.link_view(self.view)
  136. self.gridlines = scene.visuals.GridLines(scale=(1,1), color=(0,1,0), parent=self.view.scene)
  137. self.dataPlot = Plot3DLine(np.array([[0,0]], np.float32), parent=self.view.scene)
  138. self.carPlot = PlotMesh(vertices, faces,
  139. vertex_colors=vertex_colors, shading='smooth', parent=self.view.scene)
  140. self.carPlot.transform = visuals.transforms.MatrixTransform()
  141. def update(self, data3d, rot):
  142. rotX, rotY, rotZ = rot
  143. if self.up == 3: #top
  144. data2d = data3d[:,:2] # X Y
  145. elif self.up == 2: #front
  146. data2d = data3d[:,0:3:2] # X Z
  147. else: #side
  148. data2d = data3d[:,1:] # Y Z
  149. self.dataPlot.set_data(data2d, width=2.0, color='red', marker_size=0)
  150. self.carPlot.transform.reset()
  151. self.carPlot.transform.set_ortho(-1, 1, -1, 1, -1, 1)
  152. rotX, rotY, rotZ = (-rotX), (-rotY), (rotZ)
  153. self.carPlot.transform.rotate(rotX, (1,0,0))
  154. self.carPlot.transform.rotate(rotY, (0,1,0))
  155. self.carPlot.transform.rotate(rotZ, (0,0,1))
  156. if self.up <= 2:#front
  157. self.carPlot.transform.rotate(90, (1,0,0))
  158. if self.up == 1:#side
  159. self.carPlot.transform.rotate(90, (0,1,0))
  160. self.carPlot.transform.translate(data2d[-1])
  161. if self.follow:
  162. self.view.camera.center = tuple(data2d[-1])