Resampling and ploting multiple slices from a vtkPolydata

Question:

I have a vtkPolydata with 3D coordinates of the vertex of the grid as points and Temperature at each point stored as point field data. I want to plot the Temperature distribution at different elevations. I am using Python for the operation. I have created a pipeline, and I am able to get the required slice from the vtkPolydata. However, when I rendered it, the plot looked like a scatter plot as shown below:
Plot generated based on the below script

I want to plot it without any white space in between even when zoomed in. Please help me with this. I think I have to interpolate or create contours from the data, but I have no idea how to do it even after searching for a couple of days.

The code I used to generate the plot is given below.

The vtkPolydata I used to generate the above plot can be downloaded from here: vtkPolydata

import vtk
from vtk.util import numpy_support
import numpy as np

# Load vtkPolydata function
def loadvtp(fname):
    reader = vtk.vtkXMLPolyDataReader()
    reader.SetFileName(fname)
    reader.Update()
    data=reader.GetOutput()
    return data

# Load vtkPolydata
polydata=loadvtp("TestPolydata.vtp")

# Set the elevation at which data need to be plotted
ele=12

# %% Clip polydata based on the elevation
# Create plane in +Z to cut the polydata below elevation
plane = vtk.vtkPlane()
plane.SetOrigin(0, 0, ele)
plane.SetNormal(0, 0, 1)

# create polydata clipper
clipper = vtk.vtkClipPolyData()
clipper.SetInputData(polydata)
clipper.SetClipFunction(plane)
clipper.Update()

out_data=clipper.GetOutput()

# Create plane in -Z to cut the polydata above elevation
plane = vtk.vtkPlane()
plane.SetOrigin(0, 0, ele+0.5)
plane.SetNormal(0, 0, -1)
clipper = vtk.vtkClipPolyData()
clipper.SetInputData(out_data)
clipper.SetClipFunction(plane)
clipper.Update()

# Cliped polydata
sampled_data=clipper.GetOutput()

# Get the "Temperature" Array from the polydata
point_scalars = sampled_data.GetPointData().GetArray("Temperature")

# Create a lookup table to map the point field data to colors
lut = vtk.vtkLookupTable()
lut.SetNumberOfTableValues(2)
lut.SetTableValue(0, 1, 0, 0, 1) # red
lut.SetTableValue(1, 0, 1, 0, 1) # green
lut.Build()

# Create a mapper and actor to display the cliped plane
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(clipper.GetOutputPort())
mapper.SetScalarModeToUsePointFieldData()
mapper.ScalarVisibilityOn()
mapper.SetColorModeToMapScalars()
mapper.SelectColorArray(0)
mapper.SetLookupTable(lut)
mapper.SetScalarRange(point_scalars.GetRange())

actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Create the RenderWindow, Renderer
colors = vtk.vtkNamedColors()

ren1 = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren1)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)

ren1.AddActor(actor)

ren1.SetBackground(colors.GetColor3d('Gainsboro'))
renWin.SetSize(650, 650)
renWin.SetWindowName('Test')

style = vtk.vtkInteractorStyleTrackballCamera()
iren.SetInteractorStyle(style)

ren1.TwoSidedLightingOff()

ren1.ResetCamera()

renWin.Render()
iren.Start()
Asked By: Jish

||

Answers:

Your dataset has no polygons in it, I would create a vtkImageData object and slice that.

Quick test in vedo gives:

from vedo import *
mtest = load("TestPolydata.vtp")
scalars = mtest.pointdata["Temperature"]
vol = Volume(scalars.reshape([251,251,101]))
vslice = vol.zslice(50).cmap("rainbow").add_scalarbar()
show(vol, vslice, N=2, axes=1)

enter image description here

Answered By: mmusy

Finally, I got the workaround. We can use the vtkPointGaussianMapper. Refer vtk solved example for implementation.

Answered By: Jish
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.