-
Notifications
You must be signed in to change notification settings - Fork 0
/
quadratic.py
118 lines (92 loc) · 3.77 KB
/
quadratic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 12 14:53:20 2015
@author: Shalini
"""
# Import vtk package to access VTK classes/functions.
import vtk
def vtkSliderCallback2(obj, event):
sliderRepres = obj.GetRepresentation()
pos = sliderRepres.GetValue()
print "Position ",pos
isosurface.SetValue(0, pos)
#==============================================================================
# 2. 100^3 Samlping dataset of the Quadric function
#==============================================================================
# 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)
#==============================================================================
# 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 process it
# and produce an output.
#==============================================================================
sample = vtk.vtkSampleFunction()
sample.SetSampleDimensions(100, 100, 100)
sample.SetImplicitFunction(quadric)
min = 0.05 #Slider minimum value
max = 1.5 #Slider maximum value
# computing a contour of an input data.
isosurface = vtk.vtkContourFilter()
isosurface.SetInputConnection(sample.GetOutputPort())
isosurface.SetValue(0,(min + max) / 2)
isosurfaceMapper = vtk.vtkPolyDataMapper()
isosurfaceMapper.SetInput( isosurface.GetOutput() )
isosurfaceMapper.SetColorModeToMapScalars()
isosurfaceActor = vtk.vtkActor()
isosurfaceActor.SetMapper( isosurfaceMapper )
#Create the outline
outline = vtk.vtkOutlineFilter()
outline.SetInput( sample.GetOutput() )
outlineMapper = vtk.vtkPolyDataMapper()
outlineMapper.SetInput( outline.GetOutput() )
outlineActor = vtk.vtkActor()
outlineActor.SetMapper( outlineMapper )
outlineActor.GetProperty().SetColor(1,1,1)
# renderer and render window
ren = vtk.vtkRenderer()
ren.SetBackground(0, 0, 0)
renWin = vtk.vtkRenderWindow()
renWin.SetSize( 500, 500 )
renWin.AddRenderer( ren )
# render window interactor
iren = vtk.vtkRenderWindowInteractor()
iren.SetSize(1500,1500)
iren.SetRenderWindow( renWin )
# add the actors
ren.AddActor( outlineActor )
ren.AddActor( isosurfaceActor )
#==============================================================================
# Interactive Slider representation
#==============================================================================
SliderRepres = vtk.vtkSliderRepresentation2D()
SliderRepres.SetMinimumValue(min)
SliderRepres.SetMaximumValue(max)
SliderRepres.SetValue((min + max) / 2)
SliderRepres.SetTitleText("Interactive Slider")
SliderRepres.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
SliderRepres.GetPoint1Coordinate().SetValue(0.3, 0.05)
SliderRepres.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
SliderRepres.GetPoint2Coordinate().SetValue(0.7, 0.05)
SliderRepres.SetSliderLength(0.02)
SliderRepres.SetSliderWidth(0.03)
SliderRepres.SetEndCapLength(0.01)
SliderRepres.SetEndCapWidth(0.03)
SliderRepres.SetTubeWidth(0.005)
SliderRepres.SetTitleHeight(0.02)
SliderRepres.SetLabelHeight(0.02)
SliderRepres.GetSelectedProperty().SetColor(0,1,0)
SliderWidget = vtk.vtkSliderWidget()
SliderWidget.SetInteractor(iren)
SliderWidget.SetRepresentation(SliderRepres)
SliderWidget.KeyPressActivationOff()
SliderWidget.SetAnimationModeToAnimate()
SliderWidget.SetEnabled(True)
SliderWidget.AddObserver("EndInteractionEvent", vtkSliderCallback2)
# Execute the Pipeline
renWin.Render()
# initialize and start the interactor
iren.Initialize()
iren.Start()