4

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.

Alvaro Fuentes
  • 16,937
  • 4
  • 56
  • 68

3 Answers3

4

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)

Alvaro Fuentes
  • 16,937
  • 4
  • 56
  • 68
lib
  • 2,918
  • 3
  • 27
  • 53
  • It seems to point to the right direction, could you provide me with an example usage of the `vtkSphereSource` and how to save it to a file? – Alvaro Fuentes Apr 22 '15 at 13:35
  • I added a short example – lib Apr 22 '15 at 14:38
  • when I open the corresponding file in Paraview nothing happens. I tried to apply a Glyph filter but it did not have any effect. What do you do to "visualise" the sphere in Paraview ? – MarcoMag Dec 15 '18 at 16:40
3

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.

dpielstick
  • 31
  • 3
  • I noticed you are a new contributor. Small modifications like this are better done in the original answer (maybe you don't have enough rep yet...). Anyways it was a good point. Upvoted your answer ;) – Alvaro Fuentes Jun 05 '19 at 13:12
1

I think the necessary steps would be:

Combine the data see example http://www.vtk.org/Wiki/VTK/Examples/Cxx/Filtering/CombinePolyData

and write the result to a VTP File see example http://www.vtk.org/Wiki/VTK/Examples/Cxx/IO/WriteVTP

JohnnyQ
  • 1,591
  • 1
  • 16
  • 25
  • The idea is interesting but what I really need is a way to save a sphere to a file, maybe after a could combine and save as you proposed. – Alvaro Fuentes Apr 22 '15 at 12:47