-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvtk_visualizer.py
More file actions
1815 lines (1606 loc) · 71.5 KB
/
vtk_visualizer.py
File metadata and controls
1815 lines (1606 loc) · 71.5 KB
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
"""
This file is part of VisualPIC.
The module contains the classes for 3D visualization with VTK.
Copyright 2016-2020, Angel Ferran Pousa.
License: GNU GPL-3.0.
"""
import os
import sys
from pkg_resources import resource_filename
import warnings
import numpy as np
from scipy.ndimage import zoom
try:
import vtk
vtk_installed = True
except:
vtk_installed = False
try:
import pyvista as pv
pyvista_installed = True
except:
pyvista_installed = False
try:
from PyQt5 import QtWidgets
qt_installed = True
except:
qt_installed = False
from visualpic.helper_functions import get_common_timesteps
from visualpic.visualization.volume_appearance import (VolumeStyleHandler,
Colormap, Opacity)
if qt_installed and vtk_installed:
from visualpic.ui.basic_render_window import BasicRenderWindow
class VTKVisualizer():
"""Main class controlling the 3D visualization"""
def __init__(self, show_axes=True, show_cube_axes=True,
show_bounding_box=True, show_colorbars=True, show_logo=True,
background='default gradient', scale_x=1, scale_y=1,
scale_z=1, forced_norm_factor=None,
use_qt=True, use_multi_volume=False,
window_size=[600, 400]):
"""
Initialize the 3D visualizer.
Parameters
----------
show_axes : bool
Determines whether to show a 3D axis in the render.
show_cube_axes : bool
Determines whether to show a bounding box with the volume axes.
show_bounding_box : bool
Determines whether to show a bounding box around the 3D volume.
show_colorbars : bool
Determines whether to show the field colorbars.
show_logo : bool
Determines whether to show the VisualPIC logo in the render.
background : str or list
Background of the render. Possible string values are
'default gradient', 'white' and 'black'. A list of 3 floats
containing the RGB components (range 0 to 1) of any color can
also be provided (e.g. background=[1, 1, 0.5]). Alternatively,
a list of two colors can also be given
(e.g. background=['black', [1, 1, 0.5]]). In this case, the
background will be a linear gradient between the two specified
colors.
scale_x : float
Scaling factor of the horizontal direction. A value < 1 will lead
to a shrinking of the x-axis, while a value > 1 will magnify it.
scale_y : float
Scaling factor of the vertical direction. A value < 1 will lead
to a shrinking of the y-axis, while a value > 1 will magnify it.
scale_z : float
Scaling factor of the longitudinal direction. A value < 1 will lead
to a shrinking of the z-axis, while a value > 1 will magnify it.
forced_norm_factor : float
Normalization factor between the data units and the vtk units. By
default, visualpic applies a scaling factor to the visualized data
(its value depends on the data units) to normalize its spatial
dimesions in order to make sure that it has a reasonable size in
spatial units. If for some reason the automatic scaling leads to
issues, a custom scaling can be forced by specifying this
parameter.
use_qt : bool
Whether to use Qt for the windows opened by the visualizer.
use_multi_volume : bool
Whether to use vtkMultiVolume or vtkVolume for the volume
rendering.
window_size : sequence[int], optional
Window size in pixels. Defaults to ``[600, 400]``
"""
self._check_dependencies()
use_qt = self._check_qt(use_qt)
self.vis_config = {'background': background,
'show_logo': show_logo,
'show_axes': show_axes,
'show_cube_axes': show_cube_axes,
'show_bounding_box': show_bounding_box,
'show_colorbars': show_colorbars,
'axes_scale': [scale_z, scale_y, scale_x],
'use_qt': use_qt,
'use_multi_volume': use_multi_volume}
self._unit_norm_factors = {'m': 1e5,
'um': 0.1,
'c/\\omega_p': 1}
self.forced_norm_factor = forced_norm_factor
self.camera_props = {'zoom': 1, 'focus_shift': None}
self.volume_field_list = []
self.scatter_species_list = []
self.colorbar_list = []
self.colorbar_widgets = []
self._colorbar_visibility = []
self.current_time_step = -1
self.available_time_steps = None
self._window_size = window_size
self._initialize_base_vtk_elements()
self.set_background(background)
def add_field(self, field, cmap='viridis', opacity='auto',
gradient_opacity='uniform opaque', vmax=None, vmin=None,
xtrim=None, ytrim=None, ztrim=None, resolution=None,
max_resolution_3d=[100, 100]):
"""
Add a field to the 3D visualization.
Parameters
----------
field : Field
The field to be displayed.
cmap : str
Colormap to be used. Possible values are the same as those availabe
in matplotlib.
opacity : str or Opacity
The opacity scheme to be used. Possible values are 'auto',
'linear positive', 'linear negative', 'v shape', 'inverse v shape',
'uniform opaque', 'uniform translucid', the path to an .h5 file
containing an opacity or any instace of Opacity.
gradient_opacity : str or Opacity
The gradient opacity to be used. Possible values are 'auto',
'linear positive', 'linear negative', 'v shape', 'inverse v shape',
'uniform opaque', 'uniform translucid', the path to an .h5 file
containing an opacity or any instace of Opacity.
vmin, vmax : float
Define the minimum and the maximum of the range of field values
that the colormap and opacity cover.
xtrim, ytrim, ztrim : list
Allow to downselect the field volume to be displayed by trimming
off the parts out of the range defined by these parameters. The
provided value should be a list of two values containing the
minimum and maximum of the spatial range to be displayed along the
desired axis. These values should be between -1 and 1, which
correspond to the minimum and the maximum of the original data. For
example xtrim=[-1, 1] wont have any effect, as it preserves all
data. On the contrary, xtrim=[-0.5, 0.5] would lead to only half of
the field around the x axis.
resolution : list
This allows rendering the field with a different 3D resolution than
that of the original data. A list of 3 integers should be provided
contaning the resolution along z (longitudinal), x and y
(transverse).
max_resolution_3d : list
Maximum longitudinal and transverse resolution (eg. [1000, 500])
that the 3d field generated from thetaMode cylindrical data should
have. This allows for faster reconstruction of the 3d field and
less memory usage.
"""
if field.get_geometry() in ['cylindrical', 'thetaMode', '3dcartesian']:
# check if this field has already been added to a volume
name_suffix = None
fld_repeated_idx = 0
for vol_field in self.volume_field_list:
if vol_field.field == field:
fld_repeated_idx += 1
name_suffix = str(fld_repeated_idx)
# add to volume list
volume_field = VolumetricField(
field, cmap, opacity, gradient_opacity, vmax, vmin, xtrim,
ytrim, ztrim, resolution, max_resolution_3d, name_suffix)
self.volume_field_list.append(volume_field)
self.colorbar_list.append(volume_field.get_colorbar(5))
self.available_time_steps = self.get_possible_timesteps()
else:
fld_geom = field.get_geometry()
raise ValueError(
"Field geometry '{}' not supported.".format(fld_geom))
def add_species(self, species, color='w', cmap='viridis', vmax=None,
vmin=None, xtrim=None, ytrim=None, ztrim=None, size=1,
color_according_to=None, scale_with_charge=False):
"""
Add a particle species to the 3D visualization.
Parameters
----------
species : Species
The particle species to be displayed.
color : str or list
The color of the particles. Can be a string with the name of any
matplotlib color or a list with 3 RGV values (range 0 to 1). This
parameter is ignore if color_according_to is not None. In this
case a colormap is instead used.
cmap : str
Colormap to apply to the particles. Only used if color_according_to
is not None. Possible values are the same as those availabe
in matplotlib.
vmin, vmax : float
Define the minimum and the maximum of the range of field values
that the colormap covers.
xtrim, ytrim, ztrim : list
Allow to downselect the particles to be displayed by trimming
off the those out of the range defined by these parameters. The
provided value should be a list of two values containing the
minimum and maximum of the spatial range to be displayed along the
desired axis. These values should be between -1 and 1, which
correspond to the minimum and the maximum of the simulation box.
For example xtrim=[-1, 1] will cut any particle out of the
simulation box. xtrim=[0, 1] would lead to showing only the
particles along the positive x within the simulation box.
size : float
Size of the particles.
color_according_to : str
Name of a particle component according to which the particles
should be colored. The colors follow the specified colormap.
scale_with_charge : bool
If true, the size of the particles will be proportional to their
charge, where those with the maximum charge will have the size
specified by the size parameter.
"""
sp_comps = species.get_list_of_available_components()
if ('x' in sp_comps) and ('y' in sp_comps) and ('z' in sp_comps):
# check if this species has already been added to a ScatterSpecies
name_suffix = None
sp_repeated_idx = 0
for sc_species in self.scatter_species_list:
if sc_species.species == species:
sp_repeated_idx += 1
name_suffix = str(sp_repeated_idx)
scatter_species = ScatterSpecies(
species, color, cmap, vmax, vmin, xtrim, ytrim, ztrim, size,
color_according_to, scale_with_charge, self._unit_norm_factors,
self.forced_norm_factor, name_suffix)
self.scatter_species_list.append(scatter_species)
self.renderer.AddActor(scatter_species.get_actor())
self.available_time_steps = self.get_possible_timesteps()
self.colorbar_list.append(scatter_species.get_colorbar(5))
else:
raise ValueError(
'Particle species cannot be added because it is not 3D.')
def render_to_file(self, timestep, file_path, resolution=None,
scale=1, ts_is_index=True):
"""
Render the fields in the visualizer at a specific time step and save
image to a file.
Parameters
----------
timestep : int
Time step of the fields to be rendered. Can be the index of a
time step in self.available_time_steps or directly the numerical
value of a time step. This is indicated by the 'ts_is_index'
parameter.
file_path : str
Path to the file to which the render should be saved.
resolution : list
List containing the horizontal and vertical resolution of the
rendered image.
scale : int
Scales the output resolution by this factor.
ts_is_index : bool
Indicates whether the value provided in 'timestep' is the index of
the time step (True) or the numerical value of the time step
(False).
"""
self.window.SetOffScreenRendering(1)
if resolution is not None:
self.window.SetSize(*resolution)
# Only make render if any data has been added for visualization
if len(self.volume_field_list + self.scatter_species_list) > 0:
self._make_timestep_render(timestep, ts_is_index)
self.window.Render()
w2if = vtk.vtkWindowToImageFilter()
w2if.SetInput(self.window)
w2if.SetScale(scale)
w2if.Update()
writer = vtk.vtkPNGWriter()
writer.SetFileName(file_path)
writer.SetInputConnection(w2if.GetOutputPort())
writer.Write()
def show(self, timestep=0, ts_is_index=True, window_size=None):
"""
Render and show the fields in the visualizer at a specific time step.
Parameters
----------
timestep : int
Time step of the fields to be rendered. Can be the index of a
time step in self.available_time_steps or directly the numerical
value of a time step. This is indicated by the 'ts_is_index'
parameter.
ts_is_index : bool
Indicates whether the value provided in 'timestep' is the index of
the time step (True) or the numerical value of the time step
(False).
window_size : list
List containing the horizontal and vertical size of the
render window. If given, it overrides the window size of the
`VTKVisualizer`.
"""
# Only make render if any data has been added for visualization
if len(self.volume_field_list + self.scatter_species_list) > 0:
self._make_timestep_render(timestep, ts_is_index)
self.window.SetOffScreenRendering(0)
if window_size is None:
window_size = self._window_size
if self.vis_config['use_qt']:
app = QtWidgets.QApplication(sys.argv)
self.qt_window = BasicRenderWindow(self, window_size=window_size)
app.exec_()
else:
self.window.SetSize(*window_size)
self.window.Render()
self.interactor.Start()
def show_axes(self, value):
"""
Show (for value=True) or hide (for value=False) the 3D axes in the
render.
"""
self.vis_config['show_axes'] = value
self.vtk_orientation_marker.SetEnabled(value)
def show_cube_axes(self, value):
"""
Show (for value=True) or hide (for value=False) the cube axes around
the 3D volume.
"""
self.vis_config['show_cube_axes'] = value
self.vtk_cube_axes.SetVisibility(self.vis_config['show_cube_axes'])
def show_bounding_box(self, value):
"""
Show (for value=True) or hide (for value=False) the bounding box
around the 3D volume.
"""
self.vis_config['show_bounding_box'] = value
self.vtk_cube_axes_edges.SetVisibility(
self.vis_config['show_bounding_box'])
def show_colorbars(self, value):
"""
Show (for value=True) or hide (for value=False) the field colorbars.
"""
self.vis_config['show_colorbars'] = value
if len(self.colorbar_widgets) > 0:
for cw in self.colorbar_widgets:
if value:
cw.On()
else:
cw.Off()
def show_logo(self, value):
"""
Show (for value=True) or hide (for value=False) the VisualPIC logo in
the render.
"""
self.vis_config['show_logo'] = value
self.visualpic_logo.SetVisibility(value)
def set_background(self, background):
"""
Set the render background.
Parameters
----------
background : str or list
Possible string values are 'default gradient', 'white' and 'black'.
A list of 3 floats containing the RGB components (range 0 to 1)
of any color can also be provided (e.g. background=[1, 1, 0.5]).
Alternatively, a list of two colors can also be given
(e.g. background=['black', [1, 1, 0.5]]). In this case, the
background will be a linear gradient between the two specified
colors.
"""
if isinstance(background, list) and (len(background) == 2):
self._set_background_colors(*background)
elif background == 'default gradient':
self._set_background_colors('black', [0.12, 0.3, 0.475])
else:
self._set_background_colors(background)
self.vis_config['background'] = background
def get_background(self):
"""Return the current render background."""
return self.vis_config['background']
def get_list_of_fields(self):
"""Returns a list with the names of all available fields."""
fld_list = []
for vol_field in self.volume_field_list:
fld_list.append(vol_field.get_name())
return fld_list
def get_list_of_species(self):
"""Returns a list with the names of all available species."""
sp_list = []
for species in self.scatter_species_list:
sp_list.append(species.get_name())
return sp_list
def set_camera_angles(self, azimuth, elevation):
"""
Set the azimuth and elevation angles of the camera. This values are
additive, meaning that every time this method is called the angles are
increased or decreased (negative values) by the specified value.
Parameters
----------
azimuth : float
The azimuth angle in degrees.
elevation : float
The elevation angle in degrees.
"""
self.camera.Azimuth(azimuth)
self.camera.Elevation(elevation)
def set_camera_zoom(self, zoom):
"""
Set the camera zoom.
Parameters
----------
zoom : float
The zoom value of the camera. A value greater than 1 is a zoom-in,
a value less than 1 is a zoom-out.
"""
self.camera_props['zoom'] = zoom
def set_camera_shift(self, shift):
"""
Shift the focal point of the camera in three directions.
Parameters
----------
shift : list
The three components of the shift vector.
"""
self.camera_props['focus_shift'] = shift
def get_possible_timesteps(self):
"""
Returns a numpy array with all the time steps commonly available
to all fields in the visualizer.
"""
data_list = []
for volume in self.volume_field_list:
data_list.append(volume.field)
for species in self.scatter_species_list:
data_list.append(species.species)
return get_common_timesteps(data_list)
def set_color_window(self, value):
"""
Set the color window of the mapper (controls the contrast). For more
information see
https://vtk.org/doc/nightly/html/classvtkGPUVolumeRayCastMapper.html
or
https://vtk.org/doc/nightly/html/classvtkSmartVolumeMapper.html
Parameters
----------
value : float
Default window value is 1.0. The value can also be <0, leading to a
'negative' effect in the color.
"""
self.vtk_volume_mapper.SetFinalColorWindow(value)
def set_color_level(self, value):
"""
Set the color level of the mapper (controls the brightness). For more
information see
https://vtk.org/doc/nightly/html/classvtkGPUVolumeRayCastMapper.html
or
https://vtk.org/doc/nightly/html/classvtkSmartVolumeMapper.html
Parameters
----------
value : float
Default level value is 0.5. The final color window will be centered
at the final color level, and together represent a linear
remapping of color values.
"""
self.vtk_volume_mapper.SetFinalColorLevel(value)
def set_contrast(self, value):
"""
Set the contrast of the render without having to manually change the
color window. For full control, using 'set_color_window' is advised.
Parameters
----------
value : float
A float between -1 (minimum contrast) and 1 (maximum contrast).
The default contrast is 0.
"""
if value < -1:
value = -1
elif value > 1:
value = 1
color_window = 10 ** (-value)
brightness = self.get_brightness()
self.set_color_window(color_window)
self.set_brightness(brightness)
def set_brightness(self, value):
"""
Set the brightness of the render without having to manually change the
color level. For full control, using 'set_color_level' is advised.
Parameters
----------
value : float
A float between -1 (minimum brightness) and 1 (maximum brightness).
The default brightness is 0.
"""
if value < -1:
value = -1
elif value > 1:
value = 1
w = self.get_color_window()
color_level = 0.5 - (0.5 + w/2)*value
self.set_color_level(color_level)
def get_color_window(self):
"""Return the color window value"""
return self.vtk_volume_mapper.GetFinalColorWindow()
def get_color_level(self):
"""Return the color level value"""
return self.vtk_volume_mapper.GetFinalColorLevel()
def get_contrast(self):
"""Return the contrast value"""
w = self.vtk_volume_mapper.GetFinalColorWindow()
return -np.log10(np.abs(w))
def get_brightness(self):
"""Return the brightness value"""
color_level = self.vtk_volume_mapper.GetFinalColorLevel()
color_window = self.vtk_volume_mapper.GetFinalColorWindow()
return (1 - 2*color_level) / (1 + np.abs(color_window))
def draw_colorbars(self):
cbar_visibility = [True] * len(self.colorbar_list)
for species in self.scatter_species_list:
if species.get_color_according_to() is None:
cbar_idx = self.colorbar_list.index(species.cbar)
cbar_visibility[cbar_idx] = False
if cbar_visibility != self._colorbar_visibility:
self._colorbar_visibility = cbar_visibility
self.colorbar_widgets.clear()
for cbar, cbar_vis in zip(self.colorbar_list, cbar_visibility):
if cbar_vis:
self._add_colorbar_widget(cbar)
self._position_colorbars()
def _initialize_base_vtk_elements(self):
if self.vis_config['use_multi_volume']:
try:
# vtkMultiVolume class available only in vtk >= 8.2.0
self.vtk_volume = vtk.vtkMultiVolume()
except:
self.vtk_volume = vtk.vtkVolume()
self.vis_config['use_multi_volume'] = False
else:
self.vtk_volume = vtk.vtkVolume()
self.vtk_volume_mapper = vtk.vtkGPUVolumeRayCastMapper()
self.vtk_volume_mapper.UseJitteringOn()
self.vtk_volume.SetMapper(self.vtk_volume_mapper)
self.renderer = vtk.vtkRenderer()
self.renderer.AddVolume(self.vtk_volume)
self.window = vtk.vtkRenderWindow()
self.window.SetSize(*self._window_size)
self.window.AddRenderer(self.renderer)
self.window.SetOffScreenRendering(1)
if self.vis_config['use_qt']:
self.interactor = vtk.vtkGenericRenderWindowInteractor()
else:
self.interactor = vtk.vtkRenderWindowInteractor()
self.interactor.SetRenderWindow(self.window)
self.interactor.GetInteractorStyle().SetCurrentStyleToTrackballCamera()
self.camera = self.renderer.GetActiveCamera()
self._add_axes_widget()
self._add_cube_axes()
self._add_bounding_box()
self._add_visualpic_logo()
def _add_axes_widget(self):
self.vtk_axes = vtk.vtkAxesActor()
self.vtk_axes.SetXAxisLabelText("Z")
self.vtk_axes.SetZAxisLabelText("X")
self.vtk_axes.GetZAxisShaftProperty().SetColor(0.129, 0.467, 0.694)
self.vtk_axes.GetZAxisTipProperty().SetColor(0.129, 0.467, 0.694)
self.vtk_axes.GetXAxisShaftProperty().SetColor(0.835, 0.165, 0.176)
self.vtk_axes.GetXAxisTipProperty().SetColor(0.835, 0.165, 0.176)
self.vtk_axes.GetYAxisShaftProperty().SetColor(0.188, 0.627, 0.224)
self.vtk_axes.GetYAxisTipProperty().SetColor(0.188, 0.627, 0.224)
self.vtk_orientation_marker = vtk.vtkOrientationMarkerWidget()
self.vtk_orientation_marker.SetOutlineColor(1, 1, 1)
self.vtk_orientation_marker.SetOrientationMarker(self.vtk_axes)
self.vtk_orientation_marker.SetInteractor(self.interactor)
self.vtk_orientation_marker.SetViewport(0, 0, 0.2, 0.2)
self.show_axes(self.vis_config['show_axes'])
self.vtk_orientation_marker.InteractiveOff()
def _add_cube_axes(self):
self.vtk_cube_axes = vtk.vtkCubeAxesActor()
self.vtk_cube_axes.SetCamera(self.camera)
self.vtk_cube_axes.SetVisibility(self.vis_config['show_cube_axes'])
self.renderer.AddActor(self.vtk_cube_axes)
def _add_bounding_box(self):
self.vtk_cube_axes_edges = vtk.vtkCubeAxesActor()
self.vtk_cube_axes_edges.SetCamera(self.camera)
self.vtk_cube_axes_edges.SetFlyModeToStaticEdges()
self.vtk_cube_axes_edges.StickyAxesOff()
self.vtk_cube_axes_edges.SetVisibility(
self.vis_config['show_bounding_box'])
self.vtk_cube_axes_edges.XAxisLabelVisibilityOff()
self.vtk_cube_axes_edges.XAxisTickVisibilityOff()
self.vtk_cube_axes_edges.YAxisLabelVisibilityOff()
self.vtk_cube_axes_edges.YAxisTickVisibilityOff()
self.vtk_cube_axes_edges.ZAxisLabelVisibilityOff()
self.vtk_cube_axes_edges.ZAxisTickVisibilityOff()
self.renderer.AddActor(self.vtk_cube_axes_edges)
def _add_visualpic_logo(self):
self.vtk_image_data = vtk.vtkImageData()
self.logo_path = resource_filename(
'visualpic.ui.icons', 'vp_logo_horiz_transp.png')
self.vtk_png_reader = vtk.vtkPNGReader()
self.vtk_png_reader.SetFileName(self.logo_path)
self.vtk_png_reader.Update()
self.vtk_image_data = self.vtk_png_reader.GetOutput()
self.visualpic_logo = vtk.vtkLogoRepresentation()
self.visualpic_logo.SetImage(self.vtk_image_data)
self.visualpic_logo.SetPosition(0.79, 0.01)
self.visualpic_logo.SetPosition2(.2, .1)
self.visualpic_logo.GetImageProperty().SetOpacity(1)
self.visualpic_logo.GetImageProperty().SetDisplayLocationToBackground()
self.renderer.AddViewProp(self.visualpic_logo)
self.visualpic_logo.SetRenderer(self.renderer)
self.show_logo(self.vis_config['show_logo'])
def _make_timestep_render(self, timestep, ts_is_index=True):
if ts_is_index:
self.current_time_step = self.available_time_steps[timestep]
else:
if timestep not in self.available_time_steps:
raise ValueError(
'Time step {} is not available.'.format(timestep))
self.current_time_step = timestep
self._render_volumes(self.current_time_step)
self._render_species(self.current_time_step)
self.draw_colorbars()
self._scale_actors()
self._setup_cube_axes_and_bbox()
self._setup_camera()
def _render_volumes(self, timestep):
if self.vis_config['use_multi_volume']:
self._load_data_into_multi_volume(self.current_time_step)
else:
self._load_data_into_single_volume(self.current_time_step)
def _load_data_into_single_volume(self, timestep):
vtk_volume_prop = self._get_single_volume_properties(timestep)
vtk_data_import = self._import_single_volume_data(timestep)
# Setup mapper
self.vtk_volume_mapper.SetInputConnection(
vtk_data_import.GetOutputPort())
self.vtk_volume_mapper.Update()
# Add to volume
self.vtk_volume.SetProperty(vtk_volume_prop)
def _get_single_volume_properties(self, timestep):
vtk_volume_prop = vtk.vtkVolumeProperty()
vtk_volume_prop.IndependentComponentsOn()
vtk_volume_prop.SetInterpolationTypeToLinear()
for i, vol_field in enumerate(self.volume_field_list):
vtk_volume_prop.SetColor(i, vol_field.get_vtk_colormap())
vtk_volume_prop.SetScalarOpacity(
i, vol_field.get_vtk_opacity(timestep))
vtk_volume_prop.SetGradientOpacity(
vol_field.get_vtk_gradient_opacity(timestep))
vtk_volume_prop.ShadeOff(i)
return vtk_volume_prop
def _import_single_volume_data(self, timestep):
# Get data
volume_data_list = list()
for vol_field in self.volume_field_list:
volume_data_list.append(vol_field.get_data(timestep))
self._data_all_volumes = np.concatenate(
[aux[..., np.newaxis] for aux in volume_data_list], axis=3)
ax_data = self.volume_field_list[0].get_axes_data(timestep)
ax_origin = ax_data[0]
ax_spacing = ax_data[1]
ax_units = ax_data[3]
# Normalize volume spacing
ax_spacing, ax_origin = self._normalize_volume_spacing(
ax_spacing, ax_origin, ax_units)
# Put data in VTK format
vtk_data_import = self._create_vtk_image_import(
self._data_all_volumes, ax_origin, ax_spacing,
num_comps=len(self.volume_field_list))
return vtk_data_import
def _load_data_into_multi_volume(self, timestep):
# Workaround to fix wrong volume boundaries when a 'vtkMultiVolume' has
# only a single volume. The fix replaces the 'vtkMultiVolume' for a
# 'vtkVolume' and then calls '_load_data_into_single_volume'.
if len(self.volume_field_list) == 1:
if isinstance(self.vtk_volume, vtk.vtkMultiVolume):
self.renderer.RemoveVolume(self.vtk_volume)
self.vtk_volume = vtk.vtkVolume()
self.renderer.AddVolume(self.vtk_volume)
self.vtk_volume.SetMapper(self.vtk_volume_mapper)
return self._load_data_into_single_volume(timestep)
# If the 'vtkMultiVolume' was replaced by a 'vtkVolume' but now the
# number of volumes is >1, go back to having a 'vtkMultiVolume'.
if not isinstance(self.vtk_volume, vtk.vtkMultiVolume):
self.renderer.RemoveVolume(self.vtk_volume)
self.vtk_volume = vtk.vtkMultiVolume()
self.renderer.AddVolume(self.vtk_volume)
self.vtk_volume.SetMapper(self.vtk_volume_mapper)
# End of workaround
# Workaround for avoiding segmentation fault using vtkMultiVolume.
# A new mapper has to be created instead of updated when switching
# time steps.
cw = self.get_color_window()
cl = self.get_color_level()
self.vtk_volume_mapper = vtk.vtkGPUVolumeRayCastMapper()
self.vtk_volume_mapper.UseJitteringOn()
self.vtk_volume.SetMapper(self.vtk_volume_mapper)
self.set_color_window(cw)
self.set_color_level(cl)
# End of workaround.
vtk_vols, imports = self._create_volumes(timestep)
for i, (vol, imp) in enumerate(zip(vtk_vols, imports)):
self.vtk_volume_mapper.SetInputConnection(i, imp.GetOutputPort())
self.vtk_volume.SetVolume(vol, i)
def _create_volumes(self, timestep):
vol_list = list()
imports_list = list()
for vol_field in self.volume_field_list:
vtk_vol = vtk.vtkVolume()
vtk_volume_prop = vtk.vtkVolumeProperty()
vtk_volume_prop.SetInterpolationTypeToLinear()
vtk_volume_prop.SetColor(vol_field.get_vtk_colormap())
vtk_volume_prop.SetScalarOpacity(
vol_field.get_vtk_opacity(timestep))
vtk_volume_prop.SetGradientOpacity(
vol_field.get_vtk_gradient_opacity(timestep))
vtk_volume_prop.ShadeOff()
vtk_vol.SetProperty(vtk_volume_prop)
vol_data = vol_field.get_data(timestep)
vol_list.append(vtk_vol)
# Normalize volume spacing
ax_data = vol_field.get_axes_data(timestep)
ax_origin = ax_data[0]
ax_spacing = ax_data[1]
ax_units = ax_data[3]
ax_spacing, ax_origin = self._normalize_volume_spacing(
ax_spacing, ax_origin, ax_units)
# Put data in VTK format
imports_list.append(
self._create_vtk_image_import(vol_data, ax_origin, ax_spacing))
return vol_list, imports_list
def _normalize_volume_spacing(self, ax_spacing, ax_origin, ax_units):
if self.forced_norm_factor is not None:
norm_factor = self.forced_norm_factor
else:
norm_factor = self._unit_norm_factors[ax_units[0]]
ax_origin *= norm_factor
ax_spacing *= norm_factor
return ax_spacing, ax_origin
def _create_vtk_image_import(self, volume_data, ax_origin, ax_spacing,
num_comps=1):
vtk_data_import = vtk.vtkImageImport()
vtk_data_import.SetImportVoidPointer(volume_data)
vtk_data_import.SetDataScalarTypeToFloat()
vtk_data_import.SetNumberOfScalarComponents(num_comps)
vtk_data_import.SetDataExtent(0, volume_data.shape[2]-1,
0, volume_data.shape[1]-1,
0, volume_data.shape[0]-1)
vtk_data_import.SetWholeExtent(0, volume_data.shape[2]-1,
0, volume_data.shape[1]-1,
0, volume_data.shape[0]-1)
vtk_data_import.SetDataSpacing(ax_spacing[0],
ax_spacing[2],
ax_spacing[1])
# data origin is also changed by the normalization
vtk_data_import.SetDataOrigin(ax_origin[0], ax_origin[2], ax_origin[1])
vtk_data_import.Update()
return vtk_data_import
def _render_species(self, timestep):
for species in self.scatter_species_list:
species.update_data(timestep)
def _scale_actors(self):
scale = self.vis_config['axes_scale']
self.vtk_volume.SetScale(*scale)
for species in self.scatter_species_list:
species.get_actor().SetScale(*scale)
def _setup_cube_axes_and_bbox(self):
# Determine axes range of all volumes and species
z_range_all = []
x_range_all = []
y_range_all = []
ax_units_all = []
for element in self.volume_field_list + self.scatter_species_list:
el_has_range = False
if isinstance(element, VolumetricField):
ax_data = element.get_axes_data(self.current_time_step)
ax_range = ax_data[2]
z_range = ax_range[0]
x_range = ax_range[1]
y_range = ax_range[2]
ax_units = ax_data[3]
el_has_range = True
elif (isinstance(element, ScatterSpecies) and
not element.is_empty(self.current_time_step)):
z_range, y_range, x_range, ax_units = element.get_data_range(
self.current_time_step)
el_has_range = True
if el_has_range:
if len(z_range_all) == 0:
z_range_all = z_range
x_range_all = x_range
y_range_all = y_range
ax_units_all = ax_units
else:
z_range_all = [np.min((z_range_all[0], z_range[0])),
np.max((z_range_all[1], z_range[1]))]
x_range_all = [np.min((x_range_all[0], x_range[0])),
np.max((x_range_all[1], x_range[1]))]
y_range_all = [np.min((y_range_all[0], y_range[0])),
np.max((y_range_all[1], y_range[1]))]
# Determine bounds in vtk coordinates
bounds = None
if len(self.volume_field_list) > 0:
bounds = np.array(self.vtk_volume.GetBounds())
for species in self.scatter_species_list:
if not species.is_empty(self.current_time_step):
sp_bounds = np.array(species.get_actor().GetBounds())
if bounds is None:
bounds = sp_bounds
else:
bounds[[0, 2, 4]] = np.where(
sp_bounds[[0, 2, 4]] < bounds[[0, 2, 4]],
sp_bounds[[0, 2, 4]], bounds[[0, 2, 4]])
bounds[[1, 3, 5]] = np.where(
sp_bounds[[1, 3, 5]] > bounds[[1, 3, 5]],
sp_bounds[[1, 3, 5]], bounds[[1, 3, 5]])
# If there are no bounds (i.e. no data is displayed) hide axes and bbox
if bounds is None:
self.show_cube_axes(False)
self.show_bounding_box(False)
else:
bounds = list(bounds)
self.vtk_cube_axes_edges.SetBounds(bounds)
self.vtk_cube_axes.SetBounds(bounds)
self.vtk_cube_axes.SetXTitle('z')
self.vtk_cube_axes.SetYTitle('y')
self.vtk_cube_axes.SetZTitle('x')
self.vtk_cube_axes.SetXAxisRange(z_range_all[0], z_range_all[1])
self.vtk_cube_axes.SetYAxisRange(y_range_all[0], y_range_all[1])
self.vtk_cube_axes.SetZAxisRange(x_range_all[0], x_range_all[1])
self.vtk_cube_axes.SetXUnits(ax_units_all[0])
self.vtk_cube_axes.SetYUnits(ax_units_all[2])
self.vtk_cube_axes.SetZUnits(ax_units_all[1])
def _setup_camera(self):
self.renderer.ResetCamera()
self.camera.Zoom(self.camera_props['zoom'])
if self.camera_props['focus_shift'] is not None:
focus = np.array(self.camera.GetFocalPoint()) \
+ self.camera_props['focus_shift']
self.camera.SetFocalPoint(focus[0], focus[1], focus[2])
def _set_background_colors(self, color, color_2=None):
"""
Set the background color of the 3D visualizer.
Parameters
----------
color : str or list
Background color of the render. Possible values are 'black',
'white' or a list of 3 floats with the RGB values.
color_2 : str or list
If specified, a linear gradient backround is set where 'color_2'
is the second color of the gradient. Possible values are 'black',
'white' or a list of 3 floats with the RGB values.
"""
if isinstance(color, str):
if color == 'white':
self.renderer.SetBackground(1, 1, 1)
elif color == 'black':
self.renderer.SetBackground(0, 0, 0)
else:
self.renderer.SetBackground(*color)
if color_2 is not None:
self.renderer.GradientBackgroundOn()
if isinstance(color_2, str):
if color_2 == 'white':
self.renderer.SetBackground2(1, 1, 1)
elif color_2 == 'black':
self.renderer.SetBackground2(0, 0, 0)
else: