Advertisement
bolverk

visualise_3d_voronoi.py

May 25th, 2016
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.80 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # This example demonstrates how to visualise 3d voronoi cells
  4.  
  5. import vtk
  6. import pyvoro
  7. import numpy
  8.  
  9. mgp = 8*numpy.random.rand(100,3)
  10. tessellation = pyvoro.compute_voronoi(mgp,
  11.                                       [[0,10]]*3,
  12.                                       2.0)
  13.  
  14. actor_list = []
  15. for cell in tessellation:
  16.     vc_points = vtk.vtkPoints()
  17.     for point in cell['vertices']:
  18.         vc_points.InsertNextPoint(point)
  19.     vc_faces = vtk.vtkIdList()
  20.     vc_faces.InsertNextId(len(cell['faces']))
  21.     for face in cell['faces']:
  22.         vc_faces.InsertNextId(len(face['vertices']))
  23.         [vc_faces.InsertNextId(i) for i in face['vertices']]
  24.     vc_grid = vtk.vtkUnstructuredGrid()
  25.     vc_grid.InsertNextCell(vtk.VTK_POLYHEDRON, vc_faces)
  26.     vc_grid.SetPoints(vc_points)
  27.     vc_mapper = vtk.vtkDataSetMapper()
  28.     vc_mapper.SetInputData(vc_grid)
  29.     vc_actor = vtk.vtkActor()
  30.     vc_actor.SetMapper(vc_mapper)
  31.     vc_actor.GetProperty().SetDiffuseColor(numpy.random.rand(),numpy.random.rand(),numpy.random.rand())
  32.     vc_actor.GetProperty().SetOpacity(0.5)
  33.     actor_list.append(vc_actor)
  34.  
  35. # Create the usual rendering stuff.
  36. ren = vtk.vtkRenderer()
  37. renWin = vtk.vtkRenderWindow()
  38. renWin.AddRenderer(ren)
  39. renWin.SetSize(600, 600)
  40. iren = vtk.vtkRenderWindowInteractor()
  41. iren.SetRenderWindow(renWin)
  42.  
  43. ren.SetBackground(.1, .2, .4)
  44.  
  45. for actor in actor_list:
  46.     ren.AddActor(actor)
  47. ren.SetBackground(1,1,1)
  48.  
  49. ren.ResetCamera()
  50. ren.GetActiveCamera().Azimuth(30)
  51. ren.GetActiveCamera().Elevation(20)
  52. ren.GetActiveCamera().Dolly(2.8/3)
  53. ren.ResetCameraClippingRange()
  54.  
  55. # Render the scene and start interaction.
  56. iren.Initialize()
  57. renWin.Render()
  58. iren.Start()
  59.  
  60. #gw = vtk.vtkXMLUnstructuredGridWriter()
  61. #gw.SetFileName("vertex.vtu")
  62. #gw.SetInputData(vc_grid)
  63. #gw.Write()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement