Issue with VTK quadratic Python Program

Shalini Ravishankar shalini.ravishankar at gmail.com
Wed Feb 25 11:20:19 EST 2015


Hello Everyone,

I am new to VTK. I m trying to create a quadratic function with interactive slider.  But the output is not as expected. Can someone tell em what I am doing wrong here ?



# First, we need to import vtk package in order to access VTK classes/functions. 
import vtk 

# create a data source...an implicit function. 
# F(x,y,z) = a0*x^2 + a1*y^2 + a2*z^2 + a3*x*y + a4*y*z + a5*x*z + a6*x + a7*y + a8*z + a9
# F(x,y,z) = 1*x^2 + 0.5*y^2 + 0.2*z^2 + 0*x*y + 0*y*z + 0.1*x*z + 0.2*x + 0*y + 0*z + 0

quadric = vtk.vtkQuadric() 
quadric.SetCoefficients(1, 0.5, 0.2, 0, 0, 0.1, 0.2, 0, 0, 0) 


def vtkSliderCallback2(obj, event):
    sliderRepres = obj.GetRepresentation()
    pos = sliderRepres.GetValue()
    print "Position ",pos
    isosurface.SetValue(0, pos)

# create a filter...a sampling function, which samples an implicit function over the x-y-z range 
# although this object is not called "filter" it takes an input and do something to/with it 
# and produce an output. 
sample = vtk.vtkSampleFunction() 
sample.SetSampleDimensions(100, 100, 100) 
sample.SetImplicitFunction(quadric) 



outline = vtk.vtkOutlineFilter() 
outline.SetInput( sample.GetOutput() ) 
outlineMapper = vtk.vtkPolyDataMapper() 
outlineMapper.SetInput( outline.GetOutput() )
outlineActor = vtk.vtkActor() 
outlineActor.SetMapper( outlineMapper ) 
outlineActor.GetProperty().SetColor(0.0,0.0,1.0)

# create another filter...computing a contour of an input data. 
isosurface = vtk.vtkContourFilter() 
isosurface.SetInputConnection(sample.GetOutputPort()) 
isosurface.GenerateValues(15, 0.0, 4.2) 

# create a mapper, which mapps data to visualizable data structure. 
contMapper = vtk.vtkPolyDataMapper() 
contMapper.SetInputConnection(isosurface.GetOutputPort()) 
contMapper.SetScalarRange(0.0, 1.2) 

# create an actor, which can be displayed. 
contActor = vtk.vtkActor() 
contActor.SetMapper(contMapper) 



# create a window with a renderer. 
ren = vtk.vtkRenderer() 
renWin = vtk.vtkRenderWindow() 
renWin.AddRenderer(ren) 
iren = vtk.vtkRenderWindowInteractor() 
iren.SetRenderWindow(renWin) 
ren.SetBackground(0.95, 0.95, 1.0)
ren.AddActor(contActor) 
renWin.SetSize( 500, 500 ) 


SliderRepres = vtk.vtkSliderRepresentation2D()
min = 0 #ImageViewer.GetSliceMin()
max = 256 #ImageViewer.GetSliceMax()
SliderRepres.SetMinimumValue(min)
SliderRepres.SetMaximumValue(max)
SliderRepres.SetValue((min + max) / 2)
SliderRepres.SetTitleText("Slider")
SliderRepres.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
SliderRepres.GetPoint1Coordinate().SetValue(0.2, 0.9)
SliderRepres.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
SliderRepres.GetPoint2Coordinate().SetValue(0.8, 0.9)

SliderRepres.SetSliderLength(0.02)
SliderRepres.SetSliderWidth(0.03)
SliderRepres.SetEndCapLength(0.01)
SliderRepres.SetEndCapWidth(0.03)
SliderRepres.SetTubeWidth(0.005)
SliderRepres.SetLabelFormat("%3.0lf")
SliderRepres.SetTitleHeight(0.02)
SliderRepres.SetLabelHeight(0.02)

SliderWidget = vtk.vtkSliderWidget()
SliderWidget.SetInteractor(iren)
SliderWidget.SetRepresentation(SliderRepres)
SliderWidget.KeyPressActivationOff()
SliderWidget.SetAnimationModeToAnimate()
SliderWidget.SetEnabled(True)
SliderWidget.AddObserver("EndInteractionEvent", vtkSliderCallback2)

# this causes the pipeline to "execute"
renWin.Render() 

# initialize and start the interactor 
iren.Initialize() 
iren.Start() 




Thanks,
Shalini



More information about the Python-list mailing list