Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a vtkSphere to a VTK file?

I'm trying to save multiples spheres to a file for the later visualization with ParaView.

What I have is a text file with the information about position and shape (radius) of each sphere. With Python and VTK I'm constructing a file to visualize the data in ParaView. Right now I'm able to save the center and radii of the spheres as a vtkUnstructuredGrid and later in ParaView I'm adding a sphere glyph for the correct visualization. This approach is working but is very tedious to manually add the glyph each time I have to visualize the data. So I'm trying to figure out how to save the spheres as a whole into a vtk file.

I was playing around with vtkSphere but can't find a way to save the sphere to a vtk file.

Now the question again is: How to save a vtkSphere to a VTK file?

I'm open to suggestions for alternative solutions to my problem.

like image 257
Alvaro Fuentes Avatar asked Oct 18 '25 12:10

Alvaro Fuentes


2 Answers

If you want to save from Paraview, you can simply save the data after applying the glyph.

If you want to generate the spheres directly, use vtkSphereSource (instead of vtkSphere which creates an implicit function, for example to be used with a clip filter or a glyph filter), then connect its output to a vtkwriter. Here is the code for creating and writing a single sphere:

sphere = vtk.vtkSphereSource()
sphere.SetRadius(4)
sphere.SetCenter(0,0,1)
writer = vtk.vtkPolyDataWriter()
writer.SetInputData(sphere.GetOutput())
writer.SetFileName('mysphere.vtk')
writer.Update()

You can also create the glyphs directly with the vtk code, see http://www.vtk.org/Wiki/VTK/Examples/Cxx/Visualization/ProgrammableGlyphFilter (in this case, you use vtkSphere as source for the glyph)

like image 162
lib Avatar answered Oct 21 '25 01:10

lib


Lib's answer is correct, but was missing one line of code to make it work. The vtkSphereSource needs to be updated after the changes are made for the file to work in Paraview.

sphere = vtk.vtkSphereSource()
sphere.SetRadius(4)
sphere.SetCenter(0,0,1)
sphere.Update()
writer = vtk.vtkPolyDataWriter()
writer.SetInputData(sphere.GetOutput())
writer.SetFileName('mysphere.vtk')
writer.Update()

This generates the file as expected.

like image 44
dpielstick Avatar answered Oct 21 '25 01:10

dpielstick