-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.py
1109 lines (931 loc) · 35.1 KB
/
common.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import io
import json
import zipfile
from trame_client.widgets.core import AbstractElement
from trame_vtk.modules import common
from trame_vtk import reference_id
try:
import zlib # noqa
ZIP_COMPRESSION = zipfile.ZIP_DEFLATED
except ImportError:
ZIP_COMPRESSION = zipfile.ZIP_STORED
def activate_module_for(helper, server, vtk_or_paraview_obj):
if helper is not None:
return helper
if vtk_or_paraview_obj.IsA("vtkSMRemoteObject"):
from trame_vtk.modules import paraview
server.enable_module(paraview)
return paraview.get_helper(server)
else:
from trame_vtk.modules import vtk
server.enable_module(vtk)
return vtk.get_helper(server)
class HtmlElement(AbstractElement):
def __init__(self, _elem_name, children=None, **kwargs):
super().__init__(_elem_name, children, **kwargs)
self._helper = None
if self.server:
self.server.enable_module(common)
@property
def module(self):
return self._helper
class VtkPiecewiseEditor(HtmlElement):
def __init__(self, children=None, **kwargs):
"""
VtkPiecewiseEditor will create a UI to view and edit opacity function
"""
super().__init__("vtk-piecewise-editor", children, **kwargs)
self._attr_names += [
"value",
("max_zoom", "maxZoom"),
"padding",
("content_rectangle_style", "contentRectangleStyle"),
("result_opacity_line_style", "resultOpacityLineStyle"),
("inside_zoom_style", "insideZoomStyle"),
("gaussian_opacity_style", "gaussianOpacityStyle"),
("active_gaussian_opacity_style", "activeGaussianOpacityStyle"),
("active_linear_opacity_style", "activeLinearOpacityStyle"),
("linear_opacity_style", "linearOpacityStyle"),
("linear_opacity_control_style", "linearOpacityControlStyle"),
("active_linear_opacity_control_style", "activeLinearOpacityControlStyle"),
("zoom_control_style", "zoomControlStyle"),
]
self._event_names += [
"opacities",
"input",
]
class VtkAlgorithm(HtmlElement):
def __init__(self, children=None, **kwargs):
"""
VtkAlgorithm will create a vtk.js object from its vtkClass and state
:param vtk_class: Name of vtk.js class to instantiate from `here <https://github.com/Kitware/vue-vtk-js/blob/master/src/AvailableClasses.js>`_
:type vtk_class: str
:param state: JS object listing all the value to override
:type state: str/obj
:param port: Port to bind into parent VtkAlgorithm or VtkRepresentation
:type port: int
"""
super().__init__("vtk-algorithm", children, **kwargs)
self._attr_names += [
("vtk_class", "vtkClass"),
"state",
"port",
]
class VtkCellData(HtmlElement):
def __init__(self, children=None, **kwargs):
super().__init__("vtk-cell-data", children, **kwargs)
class VtkDataArray(HtmlElement):
def __init__(self, **kwargs):
"""
VtkDataArray
:param name:
:type name: str
:param registration:
:type registration: str
:param type:
:type type: str
:param values:
:type values: Array/TypedArray
:param number_of_components:
:type number_of_components: int
"""
super().__init__("vtk-data-array", **kwargs)
self._attr_names += [
"name",
"registration",
"type",
"values",
"number_of_components",
]
class VtkFieldData(HtmlElement):
def __init__(self, children=None, **kwargs):
super().__init__("vtk-field-data", children, **kwargs)
class VtkGeometryRepresentation(HtmlElement):
def __init__(self, children=None, **kwargs):
"""
VtkGeometryRepresentation
:param id: Identifier used in picking
:type id: str
:param color_map_preset: Name of a vtk.js color preset
:type color_map_preset: str
:param color_data_range:
:type color_data_range: [min, max]
:param actor: Properties to edit on actor
:type values: {}
:param mapper: Properties to edit on mapper
:type mapper: {}
:param property: Properties to edit on property
:type property: {}
"""
super().__init__("vtk-geometry-representation", children, **kwargs)
self._attr_names += [
"id",
"color_map_preset",
"color_data_range",
"actor",
"mapper",
"property",
]
class VtkGlyphRepresentation(HtmlElement):
def __init__(self, children=None, **kwargs):
"""
VtkGlyphRepresentation
:param color_map_preset: Name of a vtk.js color preset
:type color_map_preset: str
:param color_data_range:
:type color_data_range: [min, max]
:param actor: Properties to edit on actor
:type values: {}
:param mapper: Properties to edit on mapper
:type mapper: {}
:param property: Properties to edit on property
:type property: {}
"""
super().__init__("vtk-glyph-representation", children, **kwargs)
self._attr_names += [
"color_map_preset",
"color_data_range",
"actor",
"mapper",
"property",
]
class VtkMesh(HtmlElement):
def __init__(
self,
name,
dataset=None,
field_to_keep=None,
point_arrays=None,
cell_arrays=None,
**kwargs,
):
super().__init__("vtk-mesh", **kwargs)
self.__name = name
self.__dataset = dataset
self.__field_to_keep = field_to_keep
self.__point_arrays = point_arrays
self.__cell_arrays = cell_arrays
self._attr_names += ["port", "state"]
self._helper = None
if dataset:
self._helper = activate_module_for(self._helper, self.server, dataset)
self._attributes["state"] = f':state="{name}"'
self.update()
def set_dataset(self, dataset):
"""
Change this mesh's internal dataset and update shared state"""
self.__dataset = dataset
self._helper = activate_module_for(self._helper, self.server, dataset)
self.update()
def update(self, **kwargs):
"""
Propagate changes in internal data to shared state
"""
if self.__dataset:
self.server.state[self.__name] = self._helper.mesh(
self.__dataset,
field_to_keep=kwargs.get("field_to_keep", self.__field_to_keep),
point_arrays=kwargs.get("point_arrays", self.__point_arrays),
cell_arrays=kwargs.get("cell_arrays", self.__cell_arrays),
)
class VtkPointData(HtmlElement):
def __init__(self, children=None, **kwargs):
super().__init__("vtk-point-data", children, **kwargs)
class VtkPolyData(HtmlElement):
def __init__(self, name, children=None, dataset=None, **kwargs):
"""
VtkPolyData
:param port: Port to bind into parent VtkAlgorithm or VtkRepresentation
:type port: int
:param points: Points (xyz array)
:type points: Array
:param verts: CellArray to use for verts
:type verts: Array
:param lines: CellArray to use for lines
:type lines: Array
:param polys: CellArray to use for polys
:type polys: Array
:param strips: CellArray to use for strips
:type strips: Array
:param connectivity: One of 'manual', 'points', 'triangles', 'strips'.
When 'manual' is used we expect the user to provide cells.
For the other options, the JS code will generate the cells automatically.
:type connectivity: str
"""
super().__init__("vtk-polydata", children, **kwargs)
self.__name = name
self.__dataset = dataset
self._attr_names += [
"port",
"points",
"verts",
"lines",
"polys",
"strips",
"connectivity",
]
self._helper = None
if dataset:
self._helper = activate_module_for(self._helper, self.server, dataset)
self._attributes["bind"] = f'v-bind="{name}.mesh"'
self.update()
def set_dataset(self, dataset):
"""
Change this polydata's internal dataset and update shared state
"""
self.__dataset = dataset
self._helper = activate_module_for(self._helper, self.server, dataset)
self.update()
def update(self):
"""
Propagate changes in internal data to shared state
"""
if self.__dataset:
self.server.state[self.__name] = self._helper.mesh(self.__dataset)
class VtkReader(HtmlElement):
def __init__(self, **kwargs):
"""
VtkReader
:param port: Port to bind into parent VtkAlgorithm or VtkRepresentation
:type port: int
:param parse_as_array_buffer: base64 strings
:type parse_as_array_buffer: str
:param parse_as_text: text content to parse
:type parse_as_text: str
:param render_on_update: Once ready trigger a render
:type render_on_update: bool
:param reset_camera_on_update: Once ready trigger a resetCamera
:type reset_camera_on_update: bool
:param url: url to download data from
:type url: str
:param vtk_class: Class name to use as reader
:type vtk_class: str
"""
super().__init__("vtk-reader", **kwargs)
self._attr_names += [
("parse_as_array_buffer", "parseAsArrayBuffer"),
("parse_as_text", "parseAsText"),
"port",
("render_on_update", "renderOnUpdate"),
("reset_camera_on_update", "resetCameraOnUpdate"),
"url",
("vtk_class", "vtkClass"),
]
class VtkRemoteLocalView(HtmlElement):
"""
The VtkRemoteLocalView component is a blend of VtkLocalView and VtkRemoteView where the user can choose dynamically which mode they want to be in. When instantiating a VtkRemoteLocalView several variables and triggers will be created for you to more easily control your view.
>>> rl_view = vtk.VtkRemoteLocalView(
... view=..., # Instance of the view (required)
... # - VTK: vtkRenderWindow
... # - Paraview: viewProxy
... # Just VtkRemoteLocalView params
... namespace=..., # Prefix for variables and triggers. See below. (required)
... mode="local", # Decide between local or remote. See below.
... disable_auto_switch=True # Skip automatic remote rendering switch while local rendering is pending
...
... # VtkRemoteView params
... **remote_view_params,
...
... # VtkLocalView params
... **local_view_params,
... )
"""
_next_id = 0
def __init__(self, view, enable_rendering=True, widgets=[], **kwargs):
super().__init__("vtk-remote-local-view", **kwargs)
self._helper = None
self._helper = activate_module_for(self._helper, self.server, view)
self._helper.has_capabilities("web", "rendering")
__ns = kwargs.get("namespace", "view")
if __ns == "view":
VtkRemoteLocalView._next_id += 1
if VtkRemoteLocalView._next_id > 1:
__ns = f"view{VtkRemoteLocalView._next_id}"
self.__mode_key = f"{__ns}Mode"
self.__scene_id = f"{__ns}Scene"
self.__view_key_id = f"{__ns}Id"
self.__ref = kwargs.get("ref", __ns)
self.__rendering = enable_rendering
self.__namespace = __ns
self._widgets = widgets
# !!! HACK !!!
# Allow user to configure view mode by providing (..., local/remote) and or "local/remote"
__mode_expression = kwargs.get("mode", self.__mode_key)
__mode_start = "remote"
if isinstance(__mode_expression, (tuple, list)):
if len(__mode_expression) == 2:
__mode_expression, __mode_start = __mode_expression
else:
__mode_expression = __mode_expression[0]
elif __mode_expression in ["local", "remote"]:
__mode_start = __mode_expression
__mode_expression = self.__mode_key
self._attributes["mode"] = f':mode="{__mode_expression}"'
# !!! HACK !!!
self.server.state[self.__view_key_id] = self._helper.id(view)
self.__view = view
self.__wrapped_view = self._helper.view(view, name=__ns, mode=__mode_start)
# Provide mandatory attributes
self._attributes["ref"] = f'ref="{self.__ref}"'
self._attributes["refprefix"] = f'refPrefix="{self.__ref}"'
self._attributes["namespace"] = f'namespace="{__ns}"'
# vue2/3 compatibility
self._attributes["view_id"] = f':viewId="{self.__view_key_id}"'
self._attributes["view_state"] = f':viewState="{self.__scene_id}"'
self._attr_names += [
# "mode", # <--- Managed by hand above
"context_name",
("enable_picking", "enablePicking"),
("interactive_quality", "interactiveQuality"),
("interactive_ratio", "interactiveRatio"),
("still_ratio", "stillRatio"),
("still_quality", "stillQuality"),
("interactor_events", "interactorEvents"),
"interactor_settings",
("box_selection", "boxSelection"),
("disable_auto_switch", "disableAutoSwitch"),
("picking_modes", "pickingModes"),
]
self._event_names += [
"click",
"hover",
"select",
("on_local_image_capture", "onLocalImageCapture"),
("on_remote_image_capture", "onRemoteImageCapture"),
"resize",
("reset_camera", "resetCamera"),
("view_state_change", "viewStateChange"),
("before_scene_loaded", "beforeSceneLoaded"),
("after_scene_loaded", "afterSceneLoaded"),
("on_ready", "onReady"),
("box_selection_change", "BoxSelection"),
"StartAnimation",
"Animation",
"EndAnimation",
"MouseEnter",
"MouseLeave",
"StartMouseMove",
"MouseMove",
"EndMouseMove",
"LeftButtonPress",
"LeftButtonRelease",
"MiddleButtonPress",
"MiddleButtonRelease",
"RightButtonPress",
"RightButtonRelease",
"KeyPress",
"KeyDown",
"KeyUp",
"StartMouseWheel",
"MouseWheel",
"EndMouseWheel",
"StartPinch",
"Pinch",
"EndPinch",
"StartPan",
"Pan",
"EndPan",
"StartRotate",
"Rotate",
"EndRotate",
"Button3D",
"Move3D",
"StartPointerLock",
"EndPointerLock",
"StartInteraction",
"Interaction",
"EndInteraction",
]
self._server.controller.on_server_ready.add(self.update_geometry)
def push_remote_camera_on_end_interaction(self):
# vtkCommand.EndInteractionEvent 45
# from vtkmodules.vtkCommonCore import vtkCommand
# print("vtkCommand.EndInteractionEvent", vtkCommand.EndInteractionEvent)
self.__view.GetInteractor().AddObserver(45, self._push_camera)
def update_geometry(
self, reset_camera=False, widgets=None, orientation_axis=0, **kwargs
):
"""
Force update to geometry
"""
if widgets is None:
widgets = self._widgets
if self.server.protocol:
delta_state = self._helper.scene(
self.__view,
new_state=False,
widgets=widgets,
orientation_axis=orientation_axis,
)
self.server.protocol.publish("trame.vtk.delta", delta_state)
full_state = self._helper.scene(
self.__view,
new_state=True,
widgets=widgets,
orientation_axis=orientation_axis,
)
self.server.state[self.__scene_id] = full_state
def export_geometry(self, widgets=None, orientation_axis=0, format="zip", **kwargs):
"""Export standalone scene for OfflineViewer
:param format: Can be either be "zip" or "json".
"""
encoded_data = None
if widgets is None:
widgets = self._widgets
if self.server.protocol:
encoded_data = self._helper.export(
self.__view,
widgets=widgets,
orientation_axis=orientation_axis,
)
if encoded_data:
json_out = json.dumps(encoded_data)
if format == "json":
return json_out.encode(encoding="UTF-8", errors="strict")
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "a") as zfile:
zfile.writestr("index.json", json_out, compress_type=ZIP_COMPRESSION)
return zip_buffer.getvalue()
def update_image(self, reset_camera=False):
"""
Force update to image
"""
self._helper.push_image(self.__view, reset_camera)
def set_local_rendering(self, local=True, **kwargs):
self.server.state[self.__mode_key] = "local" if local else "remote"
def set_remote_rendering(self, remote=True, **kwargs):
self.server.state[self.__mode_key] = "remote" if remote else "local"
def update(self, reset_camera=False, widgets=None, orientation_axis=0, **kwargs):
# need to do both to keep things in sync
if self.__rendering:
self.update_image(reset_camera)
self.update_geometry(
reset_camera=reset_camera,
widgets=widgets,
orientation_axis=orientation_axis,
)
if reset_camera:
self.server.js_call(
self.__ref,
"setCamera",
self._helper.camera(self.__view),
)
def _push_camera(self, *args, **kwargs):
self.push_camera()
def push_camera(self, camera=None, center_of_rotation=None, **kwargs):
if camera is None:
if hasattr(self.__view, "GetRenderers"): # VTK
camera = self.__view.GetRenderers().GetFirstRenderer().GetActiveCamera()
elif hasattr(self.__view, "GetActiveCamera"): # ParaView
camera = self.__view.GetActiveCamera()
if camera is None:
return
camera_params = dict(
position=camera.GetPosition(),
focalPoint=camera.GetFocalPoint(),
viewUp=camera.GetViewUp(),
parallelProjection=camera.GetParallelProjection(),
parallelScale=camera.GetParallelScale(),
viewAngle=camera.GetViewAngle(),
)
if center_of_rotation is not None:
camera_params["centerOfRotation"] = center_of_rotation
self.server.js_call(
self.__ref,
"setCamera",
camera_params,
)
def replace_view(self, new_view, **kwargs):
self.server.state[self.__view_key_id] = self._helper.id(new_view)
_mode = self.server.state[self.__mode_key]
self.__view = new_view
self.__wrapped_view = self._helper.view(
new_view, name=self.__namespace, mode=_mode, force_replace=True
)
self.update()
self.resize()
def reset_camera(self, **kwargs):
self.server.js_call(ref=self.__ref, method="resetCamera")
def resize(self, **kwargs):
self.server.js_call(ref=self.__ref, method="resize")
@property
def view(self):
"""
Get linked vtkRenderWindow instance
"""
return self.__wrapped_view
def capture_image(self, format="image/png", opts={}):
self.server.js_call(
self.__ref,
"captureImage",
format,
opts,
)
def get_widgets(self):
return self._widgets
def set_widgets(self, value):
self._widgets = value
self.update_geometry()
def release_resources(self):
self.__wrapped_view.release_resources()
self.__view = None
def get_scene_object_id(self, vtk_obj):
return reference_id(vtk_obj)
class VtkRemoteView(HtmlElement):
"""
The VtkRemoteView component relies on the server for rendering by sending images to the client by binding your vtkRenderWindow to it. This component gives you control over the image size and quality to reduce latency while interacting.
>>> remote_view = vtk.vtkRemoteView(
... view=..., # Instance of the view (required)
... # - VTK: vtkRenderWindow
... # - Paraview: viewProxy
... ref=..., # Identifier for this component
... interactive_quality=60, # [0, 100] 0 for fastest render, 100 for best quality
... interactive_ratio=..., # [0.1, 1] Image size scale factor while interacting
... interactor_events=( # Enable vtk.js interactor events for method binding
... "events",
... ['EndAnimation'],
... ),
... EndAnimation=end, # Bind method to the enabled event
...
... box_selection=True, # toggle selection box rendering
... box_selection_change=fn # Bind method to get rect selection
... )
"""
_next_id = 0
def __init__(self, view, ref=None, **kwargs):
super().__init__("vtk-remote-view", **kwargs)
self._is_animating = False
self._img_quality = None
self._helper = activate_module_for(None, self.server, view)
self._helper.has_capabilities("web", "rendering")
if ref is None:
VtkRemoteView._next_id += 1
ref = f"trame__remote_view_{VtkRemoteView._next_id}"
self.__view = view
self.__ref = ref
self.__view_key_id = f"{ref}Id"
self.server.state[self.__view_key_id] = self._helper.id(view)
self._attributes["ref"] = f'ref="{ref}"'
self._attributes["view_id"] = f':viewId="{self.__view_key_id}"'
self._attr_names += [
("enable_picking", "enablePicking"),
("interactive_quality", "interactiveQuality"),
("interactive_ratio", "interactiveRatio"),
("still_ratio", "stillRatio"),
("still_quality", "stillQuality"),
("interactor_events", "interactorEvents"),
("box_selection", "boxSelection"),
"visible",
("picking_modes", "pickingModes"),
]
self._event_names += [
("on_image_capture", "onImageCapture"),
("box_selection_change", "BoxSelection"),
"click",
"hover",
"select",
"StartAnimation",
"Animation",
"EndAnimation",
"MouseEnter",
"MouseLeave",
"StartMouseMove",
"MouseMove",
"EndMouseMove",
"LeftButtonPress",
"LeftButtonRelease",
"MiddleButtonPress",
"MiddleButtonRelease",
"RightButtonPress",
"RightButtonRelease",
"KeyPress",
"KeyDown",
"KeyUp",
"StartMouseWheel",
"MouseWheel",
"EndMouseWheel",
"StartPinch",
"Pinch",
"EndPinch",
"StartPan",
"Pan",
"EndPan",
"StartRotate",
"Rotate",
"EndRotate",
"Button3D",
"Move3D",
"StartPointerLock",
"EndPointerLock",
"StartInteraction",
"Interaction",
"EndInteraction",
]
def update(self, **kwargs):
"""
Force image to be pushed to client
"""
self._helper.push_image(self.__view)
def start_animation(self, fps=30, quality=100, ratio=1):
if self._is_animating:
return
self._is_animating = True
self._img_quality = self._helper.get_current_image_quality(self.__view)
self._helper.start_animation(self.__view, fps, quality, ratio)
def stop_animation(self):
if not self._is_animating:
return
self._is_animating = False
self._helper.set_image_quality(self.__view, **self._img_quality)
self._helper.stop_animation(self.__view)
self.update()
def reset_camera(self, **kwargs):
self.server.js_call(ref=self.__ref, method="resetCamera")
def replace_view(self, new_view, **kwargs):
self.__view = new_view
self.server.state[self.__view_key_id] = self._helper.id(new_view)
self.update()
self.resize()
def resize(self, **kwargs):
self.server.js_call(ref=self.__ref, method="resize")
def capture_image(self, format="image/png", opts={}):
self.server.js_call(
self.__ref,
"captureImage",
format,
opts,
)
def release_resources(self):
self.__view = None
@property
def ref_name(self):
return self.__ref
class VtkShareDataset(HtmlElement):
def __init__(self, children=None, **kwargs):
super().__init__("vtk-share-dataset", children, **kwargs)
self._attr_names += ["port", "name"]
class VtkLocalView(HtmlElement):
"""
The VtkLocalView component relies on the server for defining the vtkRenderWindow but then only the geometry is exchanged with the client.
The server does not need a GPU as no rendering is happening on the server.
The vtkRenderWindow is only used to retrieve the scene data and parameters (coloring by, representations, ...).
By relying on the same vtkRenderWindow, you can easily switch from a ``VtkRemoteView`` to a ``VtkLocalView`` or vice-versa.
This component gives you controls on how you want to map mouse interaction with the camera.
The default setting mimic default VTK interactor style so you will rarely have to override to the ``interactor_settings``.
The widgets argument allow to properly capture behavior for those assuming they can be handled by the client side.
>>> local_view = vtk.VtkLocalView(
... view=..., # Instance of the view (required)
... widgets=[], # List of vtkWidgets in view
... # - VTK: vtkRenderWindow
... # - Paraview: viewProxy
... ref=..., # Identifier for this component
... context_name=..., # Namespace for geometry cache
... interactor_settings=..., # Options for camera controls. See below.
... interactor_events=( # Enable vtk.js interactor events for method binding
... "events",
... ['EndAnimation'],
... ),
... EndAnimation=end, # Bind method to the enabled event
...
... box_selection=True, # toggle selection box rendering
... box_selection_change=fn # Bind method to get rect selection
... )
"""
_next_id = 0
def __init__(self, view, ref=None, widgets=[], **kwargs):
super().__init__("vtk-local-view", **kwargs)
self._helper = activate_module_for(None, self.server, view)
self._helper.has_capabilities("web")
if ref is None:
VtkLocalView._next_id += 1
ref = f"trame__local_view_{VtkLocalView._next_id}"
self.__scene_id = f"scene_{ref}"
self.__view = view
self.__ref = ref
self._attributes["ref"] = f'ref="{ref}"'
self._attributes["view_state"] = f':viewState="{self.__scene_id}"'
self._widgets = widgets
self._attr_names += [
("interactor_events", "interactorEvents"),
"interactor_settings",
("context_name", "contextName"),
("box_selection", "boxSelection"),
("picking_modes", "pickingModes"),
]
self._event_names += [
("on_image_capture", "onImageCapture"),
"resize",
("reset_camera", "resetCamera"),
("view_state_change", "viewStateChange"),
("before_scene_loaded", "beforeSceneLoaded"),
("after_scene_loaded", "afterSceneLoaded"),
("on_ready", "onReady"),
("box_selection_change", "BoxSelection"),
"select",
"hover",
"click",
"StartAnimation",
"Animation",
"EndAnimation",
"MouseEnter",
"MouseLeave",
"StartMouseMove",
"MouseMove",
"EndMouseMove",
"LeftButtonPress",
"LeftButtonRelease",
"MiddleButtonPress",
"MiddleButtonRelease",
"RightButtonPress",
"RightButtonRelease",
"KeyPress",
"KeyDown",
"KeyUp",
"StartMouseWheel",
"MouseWheel",
"EndMouseWheel",
"StartPinch",
"Pinch",
"EndPinch",
"StartPan",
"Pan",
"EndPan",
"StartRotate",
"Rotate",
"EndRotate",
"Button3D",
"Move3D",
"StartPointerLock",
"EndPointerLock",
"StartInteraction",
"Interaction",
"EndInteraction",
]
self.update()
self._server.controller.on_server_ready.add(self.update)
def get_widgets(self):
return self._widgets
def set_widgets(self, value):
self._widgets = value
self.update()
def update(self, widgets=None, orientation_axis=0, **kwargs):
"""
Force geometry to be pushed
"""
if widgets is None:
widgets = self._widgets
if self.server.protocol:
delta_state = self._helper.scene(
self.__view,
new_state=False,
widgets=widgets,
orientation_axis=orientation_axis,
)
self.server.protocol.publish("trame.vtk.delta", delta_state)
full_state = self._helper.scene(
self.__view,
new_state=True,
widgets=widgets,
orientation_axis=orientation_axis,
)
self.server.state[self.__scene_id] = full_state
def export(self, widgets=None, orientation_axis=0, format="zip", **kwargs):
"""Export standalone scene for OfflineViewer
:param format: Can be either be "zip" or "json".
"""
encoded_data = None
if widgets is None:
widgets = self._widgets
if self.server.protocol:
encoded_data = self._helper.export(
self.__view,
widgets=widgets,
orientation_axis=orientation_axis,
)
if encoded_data:
json_out = json.dumps(encoded_data)
if format == "json":
return json_out.encode(encoding="UTF-8", errors="strict")
zip_buffer = io.BytesIO()
with zipfile.ZipFile(zip_buffer, "a") as zfile:
zfile.writestr("index.json", json_out, compress_type=ZIP_COMPRESSION)
return zip_buffer.getvalue()
def reset_camera(self, **kwargs):
"""
Move camera to center actors within the frame
"""
self.server.js_call(ref=self.__ref, method="resetCamera")
def replace_view(self, new_view, **kwargs):
self.__view = new_view
self.server.js_call(
self.__ref, "setSynchronizedViewId", self._helper.id(new_view)
)
self.update()
def resize(self, **kwargs):
self.server.js_call(ref=self.__ref, method="resize")
def push_camera(self, camera=None, center_of_rotation=None, **kwargs):
if camera is None:
camera = self.__view.GetRenderers().GetFirstRenderer().GetActiveCamera()
camera_params = dict(
position=camera.GetPosition(),
focalPoint=camera.GetFocalPoint(),
viewUp=camera.GetViewUp(),
parallelProjection=camera.GetParallelProjection(),
parallelScale=camera.GetParallelScale(),
viewAngle=camera.GetViewAngle(),
)