How to visualize scipy.interpolate.rbf object in Slicer 3D view

Hello,

I am working on spline interpolation with scipy.interpolate.Rbf. Is there any possibility to visualize the results from scipy.interpolate.rbf directly in Slicer 3D view?

My approach so far: convert NumPy array into VTK array and then into PolyData.

Thank you in advance!
Gabriella

Going to vtk via numpy is the right way. Depending on what you are trying to do, a vtkImageData to vtkMRMLScalarVolume might also work.

What do you use the RBF for? For creating/interpolating curves, transforms, images, models, …?

There are single-line utility functions to use numpy arrays directly in MRML nodes. Some functions allow you to access MRML node content as a readable/writable numpy array, so that if you update the numpy array then the displayed data update live.

There are also many modeling and interpolation tools in VTK that are much more sophisticated and faster than similar functions in scipy.

Hello Andras and sorry for my late reply.

I have been experimenting, and the natural solution for my case is the one you are suggesting, which is using a vtkKochanekSpline() for parametric interpolation of 3d data points. Here is the code I wrote:

#Load the samples model
points_file = '/home/gabriella/PycharmProjects/Exstension/SlicerFirstExtension/IsoContourRes/points20.vtk'
samples_reader = vtk.vtkPolyDataReader()
samples_reader.SetFileName(points_file)
samples_reader.Update()
mask= samples_reader.GetOutputPort()
points = samples_reader.GetOutput().GetPoints()

xSpline = vtk.vtkKochanekSpline()
ySpline = vtk.vtkKochanekSpline()
zSpline = vtk.vtkKochanekSpline()
#
spline = vtk.vtkParametricSpline()
spline.SetXSpline(xSpline)
spline.SetYSpline(ySpline)
spline.SetZSpline(zSpline)
spline.SetPoints(points)
#
functionSource = vtk.vtkParametricFunctionSource()
functionSource.SetParametricFunction(spline)
functionSource.SetUResolution(50 * numberOfPoints)
functionSource.SetVResolution(50 * numberOfPoints)
functionSource.SetWResolution(50 * numberOfPoints)
functionSource.Update()

#Setup actor and mapper
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(functionSource.GetOutputPort())

actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetColor(colors.GetColor3d("DarkSlateGrey"))
actor.GetProperty().SetLineWidth(3.0)

#  Glyph the points
# Create a polydata to store everything in
polyData = vtk.vtkPolyData()
polyData.SetPoints(points)


sphereSource = vtk.vtkSphereSource()
sphereSource.SetRadius(1)
sphereSource.Update()
sphere = sphereSource.GetOutputPort()

# glyph = vtk.vtkGlyph3D()
# glyph.SetInputConnection(mask)
# glyph.SetSourceConnection(sphere)
# glyph.ScalingOff()
# glyph.Update()

pointMapper = vtk.vtkGlyph3DMapper()
pointMapper.SetInputData(polyData)
pointMapper.SetSourceConnection(sphere)

pointActor = vtk.vtkActor()
pointActor.SetMapper(pointMapper)
pointActor.GetProperty().SetColor(colors.GetColor3d("Peacock"))

# Create the rendering window, renderer, and interactive renderer
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(renderer)
renderWindow.SetWindowName("KochanekSpline")
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)

# Add the actors to the renderer, set the background and size
renderer.AddActor(actor)
renderer.AddActor(pointActor)
renderer.SetBackground(colors.GetColor3d("Silver"))
renderWindow.SetSize(150, 150)

renderer.ResetCamera()
renderer.GetActiveCamera().Zoom(1.5)

# Interact with the data
renderWindowInteractor.Initialize()
renderWindow.Render()
renderWindowInteractor.Start()

But, I am not sure how to implement the same result in Slicer 3D view. Any suggestions? :pray:

Probably the simplest is to use markups curves. Something like this:

points_file = '/home/gabriella/PycharmProjects/Exstension/SlicerFirstExtension/IsoContourRes/points20.vtk'
samples_reader = vtk.vtkPolyDataReader()
samples_reader.SetFileName(points_file)
samples_reader.Update()

curveNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsCurveNode")
curveNode.SetCurveTypeToKochanekSpline()
curveNode.CreateDefaultDisplayNodes()
curveNode.SetControlPointPositionsWorld(samples_reader.GetOutput().GetPoints())
1 Like