Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # This example demonstrates how to visualise 3d voronoi cells
- import vtk
- import pyvoro
- import numpy
- mgp = 8*numpy.random.rand(100,3)
- tessellation = pyvoro.compute_voronoi(mgp,
- [[0,10]]*3,
- 2.0)
- actor_list = []
- for cell in tessellation:
- vc_points = vtk.vtkPoints()
- for point in cell['vertices']:
- vc_points.InsertNextPoint(point)
- vc_faces = vtk.vtkIdList()
- vc_faces.InsertNextId(len(cell['faces']))
- for face in cell['faces']:
- vc_faces.InsertNextId(len(face['vertices']))
- [vc_faces.InsertNextId(i) for i in face['vertices']]
- vc_grid = vtk.vtkUnstructuredGrid()
- vc_grid.InsertNextCell(vtk.VTK_POLYHEDRON, vc_faces)
- vc_grid.SetPoints(vc_points)
- vc_mapper = vtk.vtkDataSetMapper()
- vc_mapper.SetInputData(vc_grid)
- vc_actor = vtk.vtkActor()
- vc_actor.SetMapper(vc_mapper)
- vc_actor.GetProperty().SetDiffuseColor(numpy.random.rand(),numpy.random.rand(),numpy.random.rand())
- vc_actor.GetProperty().SetOpacity(0.5)
- actor_list.append(vc_actor)
- # Create the usual rendering stuff.
- ren = vtk.vtkRenderer()
- renWin = vtk.vtkRenderWindow()
- renWin.AddRenderer(ren)
- renWin.SetSize(600, 600)
- iren = vtk.vtkRenderWindowInteractor()
- iren.SetRenderWindow(renWin)
- ren.SetBackground(.1, .2, .4)
- for actor in actor_list:
- ren.AddActor(actor)
- ren.SetBackground(1,1,1)
- ren.ResetCamera()
- ren.GetActiveCamera().Azimuth(30)
- ren.GetActiveCamera().Elevation(20)
- ren.GetActiveCamera().Dolly(2.8/3)
- ren.ResetCameraClippingRange()
- # Render the scene and start interaction.
- iren.Initialize()
- renWin.Render()
- iren.Start()
- # This part is supposed to dump a single voronoi 3d cell, but paraview refuses to read the file
- #gw = vtk.vtkXMLUnstructuredGridWriter()
- #gw.SetFileName("vertex.vtu")
- #gw.SetInputData(vc_grid)
- #gw.Write()
Add Comment
Please, Sign In to add comment