# Show UnstructuredGrid as isosurface?

Hi, I am new in VTK, and I have a vtk UnstructuredGridFile and I read it, but it show me with polygons and I need to deploy it as isosurface. This is my code
I can’t upload an image (I am a new user here). I need help pls

``````from vtk import (vtkUnstructuredGridReader, vtkDataSetMapper, vtkActor,
vtkRenderer, vtkRenderWindow, vtkRenderWindowInteractor, vtkContourFilter)

from vtk import *

file_name = 'prueba_leo.vtk'
scalar_range = output.GetScalarRange()

c2p = vtkCellDataToPointData()
c2p.SetInputData(output)

contours = vtkContourGrid()
contours.SetInputData(c2p.GetOutput())
contours.SetValue(0, 9)

#create the mappers
#import pdb; pdb.set_trace()
#mapper = vtkDataSetMapper()
#mapper.SetInputConnection(output_port)
#mapper.SetScalarRange(scalar_range)
gridmapper = vtkDataSetMapper()
gridmapper.SetInputData(output)
#gridmapper.GetInput().GetCellData().SetActiveScalars("A")
gridmapper.SetScalarVisibility(1)
gridmapper.SetScalarRange(scalar_range)

#creamos el otro mapper
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(contours.GetOutputPort())

#create The Actor
actor = vtkActor()
actor.SetMapper(mapper)

gridactor = vtkActor()
gridactor.SetMapper(gridmapper)
gridactor.GetProperty().EdgeVisibilityOn()

# Create the Renderer
renderer = vtkRenderer()
renderer.SetBackground(1, 1, 1) # Set background to white

# Create the RendererWindow
renderer_window = vtkRenderWindow()

# Create the RendererWindowInteractor and display the vtk_file
interactor = vtkRenderWindowInteractor()
interactor.SetRenderWindow(renderer_window)
interactor.Initialize()
interactor.Start()
``````

here are the files files

If you are new to VTK, then I would highly recommend using a high-level framework built on top of VTK to make solving this problem more approachable.

Such high-level frameworks include but are not limited to: ParaView, vedo, Mayavi, and PyVista.

If graphical user interfaces are your thing, then definitely try ParaView first

Here is an example with PyVista to solve the problem of generating isosurfaces:

``````import pyvista as pv

mesh
``````

PyVista makes it easy to see what data arrays are present:

A simple plot

``````plotter = pv.Plotter(notebook=0)
plotter.add_mesh(mesh, scalars='Resistivity/Ohmm', cmap='jet', clim=[5,700],)
plotter.view_xy()
plotter.show()
``````

Since your data are a 2D surface, taking contours will generate isolines rather than isosrufaces.

Generate isolines with a contour filter:

``````# Must migrate cell data to point data for contouring to work
iso = mesh.cell_data_to_point_data().contour(10, scalars='Resistivity/Ohmm') # <- creates 10 isosurfaces

plotter = pv.Plotter(notebook=0)
plotter.add_mesh(mesh, scalars='Resistivity/Ohmm', cmap='jet', clim=[5,700],)
plotter.view_xy()
plotter.show()
``````

Hope this helps!

God Bless you, thanks! I’m gonna study Pyvista, but I’m new in this kind of job (it’s my first and Im so excited) and I want to learn how to do this in vtk, can you help me if you want of course… Thanks, you saved my position!

Blessings

Power to you for wanting to learn VTK proper! I, unfortunately, do not have the bandwidth to walk through a full example in pure VTK to do this. But the VTK examples is a great place to get started: https://kitware.github.io/vtk-examples/site/

1 Like

Hi @lasc95 ,

for someone new to VTK this is very good !

There are only two minor issues with your script.

First:

``````c2p = vtkCellDataToPointData()
c2p.SetInputData(output)

contours = vtkContourGrid()
contours.SetInputData(c2p.GetOutput())
# c2p.GetOutput() is empty ! To validate : call print(c2p.GetOutput().GetNumberOfPoints())
``````

Since you are using `SetInputData`/`GetOutput` API you need to call `c2p.Update()` before calling `c2p.GetOutput()`

Also:

``````contours.SetValue(0, 9)
``````

This means that you want a contour line at value 9. The selected array (first in your dataset) however has values only in the range:

``````  array = c2p.GetOutput().GetPointData().GetArray(0)
vrange = array.GetValueRange()
print(array.GetName())
print(vrange)

Resistivity(log10)
(1.3729533657979573, 3.5309165269118665)
``````

so you need to either pick a value in this range or automatically generate few in the range. For the latter you can do:

``````contours.GenerateValues(10,vrange,vrange)
``````

Now the fitler should work as you expect. To make things more visible I would also suggest to place the contour line actor after the grid.
Here is an edited version of your script:

``````from vtk import (vtkUnstructuredGridReader, vtkDataSetMapper, vtkActor,
vtkRenderer, vtkRenderWindow, vtkRenderWindowInteractor, vtkContourFilter)

from vtk import *

file_name = 'prueba_leo.vtk'
scalar_range = output.GetScalarRange()

c2p = vtkCellDataToPointData()
c2p.SetInputData(output)
## Generate output !
c2p.Update()

contours = vtkContourGrid()
contours.SetInputData(c2p.GetOutput())
## Pick values in range:
#contours.SetValue(0, 2)
# or
array = c2p.GetOutput().GetPointData().GetArray(0)
vrange = array.GetValueRange()
contours.GenerateValues(10,vrange,vrange)
contours.Update()

#create the mappers
#import pdb; pdb.set_trace()
#mapper = vtkDataSetMapper()
#mapper.SetInputConnection(output_port)
#mapper.SetScalarRange(scalar_range)
gridmapper = vtkDataSetMapper()
gridmapper.SetInputData(output)
#gridmapper.GetInput().GetCellData().SetActiveScalars("A")
gridmapper.SetScalarVisibility(1)
gridmapper.SetScalarRange(scalar_range)

#creamos el otro mapper
mapper = vtkPolyDataMapper()
mapper.SetInputConnection(contours.GetOutputPort())

#create The Actor
actor = vtkActor()
actor.SetMapper(mapper)

gridactor = vtkActor()
gridactor.SetMapper(gridmapper)
gridactor.GetProperty().EdgeVisibilityOn()

# Create the Renderer
renderer = vtkRenderer()
# draw first the grid and then the lines.
renderer.SetBackground(1, 1, 1) # Set background to white

# Create the RendererWindow
renderer_window = vtkRenderWindow()
{"ft_lang_label":"__label__en","ft_lang_prob":0.58802384,"math_prob":0.71230257,"size":2807,"snap":"2022-40-2023-06","text_gpt3_token_len":704,"char_repetition_ratio":0.1323582,"word_repetition_ratio":0.0,"special_character_ratio":0.21517634,"punctuation_ratio":0.17673378,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9554926,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-07T16:43:52Z\",\"WARC-Record-ID\":\"<urn:uuid:61fee113-d292-4364-ad3d-7fe48c9b7258>\",\"Content-Length\":\"34662\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:67e37802-550d-4cb9-be52-a15f94fb7f6a>\",\"WARC-Concurrent-To\":\"<urn:uuid:2aaa14dd-6543-4d5a-83ec-9a2e5321fc77>\",\"WARC-IP-Address\":\"50.58.123.179\",\"WARC-Target-URI\":\"https://discourse.vtk.org/t/show-unstructuredgrid-as-isosurface/9306\",\"WARC-Payload-Digest\":\"sha1:W2ORD3WZO6YDGXWBCQF7RPXSHF4D6YYX\",\"WARC-Block-Digest\":\"sha1:FBHOJF2SRAFW3HKC5CPVCEE7OGB4WSZB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030338213.55_warc_CC-MAIN-20221007143842-20221007173842-00565.warc.gz\"}"}