VTK Quadratic - vtkContourFilter - vtkSliderRepresentation2D in Python not working properly
Shalini Ravishankar
shalini.ravishankar at gmail.com
Wed Feb 18 22:55:58 EST 2015
Hello Everyone,
I am new to VTK. I am trying to extract isosurfaces(Contour) from a quadratic function with a Slider to change the iso surfaces.
#!/usr/bin/env python
# First, we need to import vtk package in order to access VTK classes/functions.
import vtk
# create a data source...an implicit function.
quadric = vtk.vtkQuadric()
quadric.SetCoefficients(.5, 1, .2, 0, .1, 0, 0, .2, 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()
This is my code. It gives me output for the quadratic fucntion but when I change the contour values using slider I couldn't see the changes. Can Someone tell me What I am doing wrong here??
More information about the Python-list
mailing list