-
Notifications
You must be signed in to change notification settings - Fork 6
/
vtk_helper.py
207 lines (177 loc) · 6.86 KB
/
vtk_helper.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
# vtk_helper.py
# helper functions for vtk and rendering related actions
import vtk
# vtm reader
#from paraview.vtk.vtkIOXML import vtkXMLMultiBlockDataReader
#from vtkmodules.web.utils import mesh as vtk_mesh
from vtkmodules.vtkCommonDataModel import vtkDataObject
#from vtkmodules.vtkFiltersCore import vtkContourFilter #noqa
from vtkmodules.vtkRenderingAnnotation import vtkAxesActor
from vtkmodules.vtkCommonTransforms import vtkTransform
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import vtkLookupTable
from vtkmodules.vtkRenderingAnnotation import vtkCubeAxesActor, vtkScalarBarActor
from vtkmodules.vtkInteractionWidgets import vtkOrientationMarkerWidget, vtkScalarBarWidget
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkDataSetMapper,
vtkRenderer,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkColorTransferFunction,
)
from vtkmodules.vtkCommonDataModel import (
VTK_HEXAHEDRON,
VTK_LINE,
VTK_POLYGON,
VTK_QUAD,
VTK_TETRA,
VTK_TRIANGLE,
VTK_PYRAMID,
VTK_WEDGE,
VTK_TRIANGLE_STRIP,
VTK_VERTEX,
vtkUnstructuredGrid)
# Required for interactor initialization
from vtkmodules.vtkInteractionStyle import vtkInteractorStyleSwitch # noqa
# Required for rendering initialization, not necessary for
# local rendering, but doesn't hurt to include it
import vtkmodules.vtkRenderingOpenGL2 # noqa
# mesh_mapper
from mesh import *
# Logging function
from logger import log
# default visibility of the actors
state.cube_axes_visibility = True
state.coord_axes_visibility = True
state.color_bar_visibiliy = True
renderer = vtkRenderer()
renderWindow = vtkRenderWindow()
renderWindow.AddRenderer(renderer)
# offscreen rendering, no additional pop-up window
renderWindow.SetOffScreenRendering(1)
renderWindowInteractor = vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.GetInteractorStyle().SetCurrentStyleToTrackballCamera()
def MakeAxesActor():
axes = vtkAxesActor()
axes.SetObjectName("CoordAxes")
axes.SetShaftTypeToCylinder()
# text labels
axes.SetXAxisLabelText('X')
axes.SetYAxisLabelText('Y')
axes.SetZAxisLabelText('Z')
axes.SetTotalLength(1.0, 1.0, 1.0)
axes.SetCylinderRadius(0.5 * axes.GetCylinderRadius())
axes.SetConeRadius(1.025 * axes.GetConeRadius())
axes.SetSphereRadius(1.5 * axes.GetSphereRadius())
return axes
def MakeOrientationMarkerWidget(axes):
coordaxes = vtkOrientationMarkerWidget()
coordaxes.SetOrientationMarker(axes)
# Position lower left in the viewport.
coordaxes.SetViewport(0, 0, 0.2, 0.2)
coordaxes.SetInteractor(renderWindowInteractor)
coordaxes.SetEnabled(True)
coordaxes.InteractiveOn()
return coordaxes
def MakeCubeAxesActor():
cubeaxes = vtkCubeAxesActor()
cubeaxes.SetObjectName("CubeAxes")
# Cube Axes: Boundaries, camera, and styling
# nijso TODO BUG? Which actor is used here???
cubeaxes.SetBounds(mesh_actor.GetBounds())
cubeaxes.SetCamera(renderer.GetActiveCamera())
cubeaxes.SetXLabelFormat("%6.1f")
cubeaxes.SetYLabelFormat("%6.1f")
cubeaxes.SetZLabelFormat("%6.1f")
#cube_axes.SetFlyModeToOuterEdges()
#cube_axes.SetUseTextActor3D(1)
cubeaxes.GetTitleTextProperty(0).SetColor(0.0, 0.0, 0.0)
cubeaxes.GetTitleTextProperty(1).SetColor(0.0, 0.0, 0.0)
cubeaxes.GetTitleTextProperty(2).SetColor(0.0, 0.0, 0.0)
cubeaxes.GetLabelTextProperty(0).SetColor(0.0, 0.0, 0.0)
cubeaxes.GetLabelTextProperty(1).SetColor(0.0, 0.0, 0.0)
cubeaxes.GetLabelTextProperty(2).SetColor(0.0, 0.0, 0.0)
cubeaxes.DrawXGridlinesOn()
cubeaxes.DrawYGridlinesOn()
cubeaxes.DrawZGridlinesOn()
cubeaxes.GetXAxesLinesProperty().SetColor(0.0, 0.0, 0.0)
cubeaxes.GetYAxesLinesProperty().SetColor(0.0, 0.0, 0.0)
cubeaxes.GetZAxesLinesProperty().SetColor(0.0, 0.0, 0.0)
cubeaxes.GetXAxesGridlinesProperty().SetColor(0.0, 0.0, 0.0)
cubeaxes.GetYAxesGridlinesProperty().SetColor(0.0, 0.0, 0.0)
cubeaxes.GetZAxesGridlinesProperty().SetColor(0.0, 0.0, 0.0)
cubeaxes.XAxisMinorTickVisibilityOff()
cubeaxes.YAxisMinorTickVisibilityOff()
cubeaxes.ZAxisMinorTickVisibilityOff()
cubeaxes.SetVisibility(False)
state.cube_axes_visibility = False
return cubeaxes
def get_diverging_lut():
"""
See: [Diverging Color Maps for Scientific Visualization](https://www.kennethmoreland.com/color-maps/)
start point midPoint end point
cool to warm: 0.230, 0.299, 0.754 0.865, 0.865, 0.865 0.706, 0.016, 0.150
purple to orange: 0.436, 0.308, 0.631 0.865, 0.865, 0.865 0.759, 0.334, 0.046
green to purple: 0.085, 0.532, 0.201 0.865, 0.865, 0.865 0.436, 0.308, 0.631
blue to brown: 0.217, 0.525, 0.910 0.865, 0.865, 0.865 0.677, 0.492, 0.093
green to red: 0.085, 0.532, 0.201 0.865, 0.865, 0.865 0.758, 0.214, 0.233
:return:
"""
ctf = vtkColorTransferFunction()
ctf.SetColorSpaceToDiverging()
# Cool to warm.
ctf.AddRGBPoint(0.0, 0.230, 0.299, 0.754)
ctf.AddRGBPoint(0.5, 0.865, 0.865, 0.865)
ctf.AddRGBPoint(1.0, 0.706, 0.016, 0.150)
table_size = 16
lut = vtkLookupTable()
lut.SetNumberOfTableValues(table_size)
lut.Build()
for i in range(0, table_size):
rgba = list(ctf.GetColor(float(i) / table_size))
rgba.append(1)
lut.SetTableValue(i, rgba)
return lut
def get_diverging_lut1():
colors = vtkNamedColors()
# Colour transfer function.
ctf = vtkColorTransferFunction()
ctf.SetColorSpaceToDiverging()
p1 = [0.0] + list(colors.GetColor3d('MidnightBlue'))
p2 = [0.5] + list(colors.GetColor3d('Gainsboro'))
p3 = [1.0] + list(colors.GetColor3d('DarkOrange'))
ctf.AddRGBPoint(*p1)
ctf.AddRGBPoint(*p2)
ctf.AddRGBPoint(*p3)
table_size = 256
lut = vtkLookupTable()
lut.SetNumberOfTableValues(table_size)
lut.Build()
for i in range(0, table_size):
rgba = list(ctf.GetColor(float(i) / table_size))
rgba.append(1)
lut.SetTableValue(i, rgba)
return lut
def MakeScalarBarActor():
# Create a scalar bar
scalarbar = vtkScalarBarActor()
scalarbar.SetObjectName("ScalarAxes")
scalarbar.SetLookupTable(mesh_mapper.GetLookupTable())
log("info", f"scalar_range = {mesh_mapper.GetLookupTable().GetTableRange()[0]}")
#scalar_bar.SetTitle('scalar bar')
scalarbar.UnconstrainedFontSizeOn()
scalarbar.SetBarRatio(0.2)
#scalar_bar.SetNumberOfLabels(5)
scalarbar.SetMaximumWidthInPixels(100)
scalarbar.SetMaximumHeightInPixels(600)
return scalarbar
def MakeScalarBarWidget(scalarbar):
# create the scalar_bar_widget
scalarbarwidget = vtkScalarBarWidget()
scalarbarwidget.SetInteractor(renderWindowInteractor)
scalarbarwidget.SetScalarBarActor(scalarbar)
scalarbarwidget.RepositionableOn()
scalarbarwidget.On()
return scalarbarwidget