[vtkusers] problems with vtKProgrammableSource [SOLVED]
Bryan Cole
bryan.cole at teraview.com
Wed Jan 20 05:55:12 EST 2016
For the benefit of anyone else struggling to get a vtkProgrammableSource to work for ImageData / StructuredPoints, it seems
the secret sauce (and it really is a secret ... there are no examples or documentation on this that I could find) is to set the OutputInformation (and set the WHOLE_EXTENT in particular). My example script is given below. This works in VTK-6.0.0 . More recent versions of VTK have added a SetRequestInformationMethod to the ProgrammableSource which allows you to provide a script to set the OutputInformation as part of the new pipeline execution.
In my solution, I set the OutputInformation once after creating the ProgrammableSource object which is OK for my purposes (small images where I'm always updating the whole extent). I also tried editing the OutputInformation data in the ExecuteMethod; this worked sort of, if I forced the method of execute the first time with a call to .Update(); I don't think this will give the correct behaviour for proper pipeline execution though. I guess you could update OutputInformation using the VTK observer framework to mimic pipeline execution if you need the OutputInformation to change dynamically and your VTK version doesn't have the SetRequestInformationMethod call.
The other important point is that you must specify which output port you want in both the GetOutputPort and GetOutputInformation methods (port 1 for StructurePoints).
HTH,
BC
=====================
import vtk
from vtk.util.numpy_support import numpy_to_vtk
import numpy
x,y,z = numpy.ogrid[-10:10:50j,-10:10:50j,-10:10:50j]
data = (x**2 + y**2 + z**2)/(5**2)
data = data.ravel().astype("f")
src = vtk.vtkProgrammableSource()
###Initialise the OutputInformation on the ProgrammableSource
executive = src.GetExecutive()
outInfo = executive.GetOutputInformation(1)
outInfo.Set(executive.WHOLE_EXTENT(), 0, 49, 0, 49, 0, 49)
def make_grid():
sp = src.GetStructuredPointsOutput()
sp.SetExtent(0,49,0,49,0,49)
sp.SetOrigin(-10,-10,-10)
sp.SetSpacing(0.4,0.4,0.4)
sp.GetPointData().SetScalars(numpy_to_vtk(data))
src.SetExecuteMethod(make_grid)
func = vtk.vtkSphere()
func.SetCenter(0,0,0)
func.SetRadius(5.0)
clip = vtk.vtkImageMarchingCubes()
clip.SetInputConnection(0,src.GetOutputPort(1))
clip.SetValue(0,1.0)
map = vtk.vtkPolyDataMapper()
map.SetInputConnection(clip.GetOutputPort(0))
map.ScalarVisibilityOff()
surfaceActor = vtk.vtkActor()
surfaceActor.SetMapper(map)
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
ren.AddActor(surfaceActor)
iren.Initialize()
renWin.Render()
iren.Start()
===============================
--
Group Leader, Technical Development Group - Teraview Ltd.
Platinum Building, St. John's Innovation Park, Cambridge CB4 0DS, UK.
tel: +44 (0)1223 435386, fax: +44 (0)1223 435382, web: www.teraview.com<http://www.teraview.com/> Registered Number: 04126946, VAT Number: 770 8883 84
...preferred document formats: ODF (ISO/IEC 26300:2006), PDF
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/vtkusers/attachments/20160120/f331dc58/attachment.html>
More information about the vtkusers
mailing list