32
32
33
33
def create_vtk_pipeline (file_to_load ):
34
34
colors = vtkNamedColors ()
35
- sphere_source = vtkSphereSource ()
36
- sphere_source .SetRadius (10.0 )
37
- sphere_source .Update ()
38
- input_bounds = sphere_source .GetOutput ().GetBounds ()
39
35
40
36
fp = None
41
37
if file_to_load :
@@ -46,59 +42,49 @@ def create_vtk_pipeline(file_to_load):
46
42
return
47
43
48
44
# Setup a visualization pipeline.
49
- plane = vtkPlane ()
50
- clipper = vtkClipPolyData ()
51
- clipper .SetClipFunction (plane )
52
- clipper .InsideOutOn ()
53
- if file_to_load :
54
- reader = vtkXMLPolyDataReader ()
55
- reader .SetFileName (fp )
56
- reader .Update ()
45
+ source = (
46
+ vtkXMLPolyDataReader (file_name = fp )
47
+ if file_to_load
48
+ else vtkSphereSource (radius = 10.0 )
49
+ )
50
+ source .Update ()
51
+
52
+ input_bounds = source .output .bounds
53
+ plane = vtkPlane (
54
+ origin = (
55
+ 0.5 * (input_bounds [0 ] + input_bounds [1 ]),
56
+ 0.5 * (input_bounds [2 ] + input_bounds [3 ]),
57
+ 0.5 * (input_bounds [4 ] + input_bounds [5 ]),
58
+ )
59
+ )
57
60
58
- input_bounds = reader .GetOutput ().GetBounds ()
59
- clipper .SetInputConnection (reader .GetOutputPort ())
60
- else :
61
- clipper .SetInputConnection (sphere_source .GetOutputPort ())
61
+ clipper = vtkClipPolyData (
62
+ clip_function = plane , inside_out = 1 , input_connection = source .output_port
63
+ )
62
64
63
65
# Create a mapper and actor.
64
- mapper = vtkPolyDataMapper ()
65
- mapper .SetInputConnection (clipper .GetOutputPort ())
66
- actor = vtkActor ()
67
- actor .SetMapper (mapper )
68
-
69
- back_faces = vtkProperty ()
70
- back_faces .SetDiffuseColor (colors .GetColor3d ("Gold" ))
71
-
72
- actor .SetBackfaceProperty (back_faces )
66
+ mapper = vtkPolyDataMapper (input_connection = clipper .output_port )
67
+ back_faces = vtkProperty (diffuse_color = colors .GetColor3d ("Gold" ))
68
+ actor = vtkActor (mapper = mapper , backface_property = back_faces )
73
69
74
70
# A renderer and render window
75
- renderer = vtkRenderer ()
71
+ renderer = vtkRenderer (background = colors .GetColor3d ("SlateGray" ))
72
+ renderer .AddActor (actor )
76
73
ren_win = vtkRenderWindow ()
77
74
ren_win .AddRenderer (renderer )
78
- ren_win .SetWindowName ("ImplicitPlaneWidget2" )
79
-
80
- renderer .AddActor (actor )
81
- renderer .SetBackground (colors .GetColor3d ("SlateGray" ))
82
75
83
76
# An interactor
84
- iren = vtkRenderWindowInteractor ()
77
+ iren = vtkRenderWindowInteractor (render_window = ren_win )
85
78
iren .GetInteractorStyle ().SetCurrentStyleToTrackballCamera ()
86
- iren .SetRenderWindow (ren_win )
87
79
88
- rep = vtkImplicitPlaneRepresentation ()
89
- rep .SetPlaceFactor (1.25 ) # This must be set prior to placing the widget
90
- rep .PlaceWidget (input_bounds )
91
- plane .SetOrigin (
92
- 0.5 * (input_bounds [0 ] + input_bounds [1 ]),
93
- 0.5 * (input_bounds [2 ] + input_bounds [3 ]),
94
- 0.5 * (input_bounds [4 ] + input_bounds [5 ]),
80
+ rep = vtkImplicitPlaneRepresentation (
81
+ place_factor = 1.25 ,
95
82
)
96
- rep .SetNormal (plane .GetNormal ())
97
- rep .SetOrigin (plane .GetOrigin ())
83
+ rep .PlaceWidget (input_bounds )
84
+ rep .normal = plane .normal
85
+ rep .origin = plane .origin
98
86
99
- plane_widget = vtkImplicitPlaneWidget2 ()
100
- plane_widget .SetInteractor (iren )
101
- plane_widget .SetRepresentation (rep )
87
+ plane_widget = vtkImplicitPlaneWidget2 (interactor = iren , representation = rep )
102
88
103
89
renderer .ResetCamera (input_bounds )
104
90
ren_win .Render ()
@@ -142,8 +128,8 @@ def _on_widget_update(self, plane_widget, **_):
142
128
return
143
129
144
130
# update cutting plane
145
- self .plane .SetNormal ( plane_widget .get ("normal" ) )
146
- self .plane .SetOrigin ( plane_widget .get ("origin" ) )
131
+ self .plane .normal = plane_widget .get ("normal" )
132
+ self .plane .origin = plane_widget .get ("origin" )
147
133
148
134
# prevent requesting geometry too often
149
135
self .html_view .render_throttle ()
@@ -152,30 +138,31 @@ def toggle_listeners(self):
152
138
if self .state .wasm_listeners is not None and len (self .state .wasm_listeners ):
153
139
self .state .wasm_listeners = {}
154
140
else :
155
- widget_id = self .html_view .get_wasm_id (self .widget )
156
- rep_id = self .html_view .get_wasm_id (self .widget .representation )
157
141
self .state .wasm_listeners = {
158
- widget_id : {
142
+ self . widget_id : {
159
143
"InteractionEvent" : {
160
144
"plane_widget" : {
161
- rep_id : {
162
- "Normal" : "normal" ,
163
- "Origin" : "origin" ,
164
- }
145
+ "normal" : (
146
+ self .widget_id ,
147
+ "WidgetRepresentation" ,
148
+ "Normal" ,
149
+ ),
150
+ "origin" : (
151
+ self .widget_id ,
152
+ "WidgetRepresentation" ,
153
+ "Origin" ,
154
+ ),
165
155
}
166
156
}
167
157
}
168
158
}
169
159
170
160
def one_time_update (self ):
171
- rep_id = self .html_view .get_wasm_id (self .widget .representation )
172
161
self .html_view .eval (
173
162
{
174
163
"plane_widget" : {
175
- rep_id : {
176
- "Normal" : "normal" ,
177
- "Origin" : "origin" ,
178
- }
164
+ "origin" : (self .widget_id , "WidgetRepresentation" , "Origin" ),
165
+ "normal" : (self .widget_id , "WidgetRepresentation" , "Normal" ),
179
166
}
180
167
}
181
168
)
@@ -201,7 +188,7 @@ def _ui(self):
201
188
throttle_rate = 20 ,
202
189
listeners = ("wasm_listeners" , {}),
203
190
)
204
- self .html_view .register_widget (self .widget )
191
+ self .widget_id = self . html_view .register_widget (self .widget )
205
192
206
193
return layout
207
194
0 commit comments