Ralv

Untitled

Jan 24th, 2019
294
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Modified Python 3 VTK script to Display 3D xyz data
  3. Credits to : https://sukhbinder.wordpress.com/2013/09/17/python-vtk-script-to-display-3d-xyz-data/
  4. '''
  5.  
  6. import vtk
  7. from numpy import random,genfromtxt,size
  8.      
  9. class VtkPointCloud:
  10.     def __init__(self, zMin=-10.0, zMax=10.0, maxNumPoints=1e6):
  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.  
  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.  
  35.     def clearPoints(self):
  36.         self.vtkPoints = vtk.vtkPoints()
  37.         self.vtkCells = vtk.vtkCellArray()
  38.         self.vtkDepth = vtk.vtkDoubleArray()
  39.         self.vtkDepth.SetName('DepthArray')
  40.         self.vtkPolyData.SetPoints(self.vtkPoints)
  41.         self.vtkPolyData.SetVerts(self.vtkCells)
  42.         self.vtkPolyData.GetPointData().SetScalars(self.vtkDepth)
  43.         self.vtkPolyData.GetPointData().SetActiveScalars('DepthArray')
  44.  
  45. def load_data(filename,pointCloud):
  46.     data = genfromtxt(filename,dtype=float,usecols=[0,1,2])
  47.      
  48.     for k in range(size(data,0)):
  49.         point = data[k] #20*(random.rand(3)-0.5)
  50.         pointCloud.addPoint(point)
  51.          
  52.     return pointCloud
  53.  
  54.  
  55. if __name__ == '__main__':
  56.     import sys
  57.  
  58.  
  59.     if (len(sys.argv) < 2):
  60.          print ('Usage: xyzviewer.py itemfile')
  61.          sys.exit()
  62.     pointCloud = VtkPointCloud()
  63.     pointCloud=load_data(sys.argv[1],pointCloud)
  64.  
  65.  
  66. # Renderer
  67.     renderer = vtk.vtkRenderer()
  68.     renderer.AddActor(pointCloud.vtkActor)
  69. #renderer.SetBackground(.2, .3, .4)
  70.     renderer.SetBackground(0.0, 0.0, 0.0)
  71.     renderer.ResetCamera()
  72.  
  73. # Render Window
  74.     renderWindow = vtk.vtkRenderWindow()
  75.     renderWindow.AddRenderer(renderer)
  76.  
  77. # Interactor
  78.     renderWindowInteractor = vtk.vtkRenderWindowInteractor()
  79.     renderWindowInteractor.SetRenderWindow(renderWindow)
  80.  
  81. # Begin Interaction
  82.     renderWindow.Render()
  83.     renderWindow.SetWindowName("XYZ Data Viewer"+sys.argv[1])
  84.     renderWindowInteractor.Start()
RAW Paste Data Copied