pyvtksample.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # -*- coding: utf-8 -*-
  2. import numpy
  3. from PyQt5 import QtCore
  4. from PyQt5.QtWidgets import QWidget, QVBoxLayout
  5. import vtk
  6. from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
  7. from numpy import random
  8. import threading
  9. class VtkPointCloud:
  10. def __init__(self, zMin=-10.0, zMax=10.0, maxNumPoints=1e7):
  11. self.maxNumPoints = maxNumPoints
  12. self.vtkPolyData = vtk.vtkPolyData()
  13. self.clearPoints()
  14. mapper = vtk.vtkPolyDataMapper()
  15. mapper.SetInputData(self.vtkPolyData)
  16. mapper.SetColorModeToDefault()
  17. mapper.SetScalarRange(zMin, zMax)
  18. mapper.SetScalarVisibility(1)
  19. self.vtkActor = vtk.vtkActor()
  20. self.vtkActor.SetMapper(mapper)
  21. self.vtkActor.GetProperty().SetPointSize(1)
  22. def addPoint(self, point):
  23. if self.vtkPoints.GetNumberOfPoints() < self.maxNumPoints:
  24. pointId = self.vtkPoints.InsertNextPoint(point[:])
  25. self.vtkDepth.InsertNextValue(point[2])
  26. self.vtkCells.InsertNextCell(1)
  27. self.vtkCells.InsertCellPoint(pointId)
  28. else:
  29. r = random.randint(0, self.maxNumPoints)
  30. self.vtkPoints.SetPoint(r, point[:])
  31. self.vtkCells.Modified()
  32. self.vtkPoints.Modified()
  33. self.vtkDepth.Modified()
  34. def clearPoints(self):
  35. self.vtkPoints = vtk.vtkPoints()
  36. self.vtkCells = vtk.vtkCellArray()
  37. self.vtkDepth = vtk.vtkDoubleArray()
  38. self.vtkDepth.SetName('DepthArray')
  39. self.vtkPolyData.SetPoints(self.vtkPoints)
  40. self.vtkPolyData.SetVerts(self.vtkCells)
  41. self.vtkPolyData.GetPointData().SetScalars(self.vtkDepth)
  42. self.vtkPolyData.GetPointData().SetActiveScalars('DepthArray')
  43. class VtkPointCloudCanvas(QWidget):
  44. def __init__(self, *args, **kwargs):
  45. super(VtkPointCloudCanvas, self).__init__(*args, **kwargs)
  46. self.lock_=threading.RLock()
  47. self._layout = QVBoxLayout()
  48. self.setLayout(self._layout)
  49. self._vtk_widget = QVTKRenderWindowInteractor(self)
  50. self._vtk_widget.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera()) # 设置交互方式==常用的方式 移动摄像机
  51. self._layout.addWidget(self._vtk_widget)
  52. self._render = vtk.vtkRenderer()
  53. self._vtk_widget.GetRenderWindow().AddRenderer(self._render)
  54. self._iren = self._vtk_widget.GetRenderWindow().GetInteractor()
  55. self._point_cloud = VtkPointCloud()
  56. self._render.AddActor(self._point_cloud.vtkActor)
  57. self.show()
  58. self._iren.Initialize()
  59. #self.timer = QtCore.QTimer()
  60. #self.timer.timeout.connect(window.updatePointCloud)
  61. #self.timer.start(1000)
  62. def ShowCloud(self,cloud:numpy.array):
  63. self.lock_.acquire()
  64. self._point_cloud.clearPoints()
  65. for point in cloud:
  66. self._point_cloud.addPoint(point)
  67. self.lock_.release()
  68. def updatePointCloud(self):
  69. pass
  70. '''
  71. self.lock_.acquire()
  72. self._vtk_widget.GetRenderWindow().Render()
  73. self.lock_.release()'''
  74. if __name__ == '__main__':
  75. from PyQt5.QtWidgets import QApplication
  76. import sys
  77. app = QApplication(sys.argv)
  78. window = VtkPointCloudCanvas()
  79. cloud=[]
  80. for k in range(1000000):
  81. point = 20 * (random.rand(3) - 0.5)
  82. cloud.append(point)
  83. window.ShowCloud(numpy.array(cloud))
  84. sys.exit(app.exec_())