vispyHelper - Kopie.py 6.3 KB

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