-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvisualisationTool.py
339 lines (237 loc) · 11.8 KB
/
visualisationTool.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
# by Facundo Sosa-Rey, 2021. MIT license
from random import random as rand
from trackingFunctions import fiberPloting
import mayavi.mlab as mlab
from tvtk.api import tvtk
from mayavi.sources.api import BuiltinSurface,ParametricSurface
import vtk
from vtk.util.numpy_support import numpy_to_vtk,vtk_to_numpy
import cameraConfig
import numpy as np
from mayavi.modules.image_plane_widget import ImagePlaneWidget
from mayavi.modules.surface import Surface
from fibers import fiberObj
def makeLegend(rangeOutline,plotRejectedFibers=False,greyValue=0.8):
#####################################
# pseudo Legend object
#####################################
legendPlaneObj = BuiltinSurface(source='plane')
legendPlane = mlab.pipeline.surface(legendPlaneObj)
legendLabels=fiberObj.getLegendLabels(plotRejectedFibers)
legendLabels=list(legendLabels)
legendLabels.sort()
heightOneEntry=10
totalHeight=heightOneEntry*(1+len(legendLabels))
totalWidth=int(3.5*max([len(iLabel) for iLabel in legendLabels]))
zOffset=40
legendExtent=[
rangeOutline[0],
rangeOutline[0]+totalWidth,
rangeOutline[2],rangeOutline[2]+.5, #y thickness
rangeOutline[5]+zOffset,
rangeOutline[5]+zOffset+totalHeight]
mlab.outline(extent=legendExtent,color=(0.,0.,0.))
imSlice = np.ones((totalWidth,totalHeight))*greyValue
gridLegend = vtk.vtkImageData()
gridLegend.SetDimensions((imSlice.shape[1],1,imSlice.shape[0]))
vtkarr = numpy_to_vtk(imSlice.ravel())
vtkarr.SetName('legendBackGround')
gridLegend.GetPointData().AddArray(vtkarr)
gridLegend.GetPointData().SetActiveScalars('legendBackGround')
vtexLegend = vtk.vtkTexture()
vtexLegend.SetInputDataObject(gridLegend)
# white colormap
lookUpTableLegend=vtk.vtkLookupTable()
lookUpTableLegend.SetHueRange(0.,0.)
lookUpTableLegend.SetNumberOfColors(256)
lookUpTableLegend.SetSaturationRange(0.,0.)
lookUpTableLegend.SetValueRange(0.,1.)
lookUpTableLegend.Build()
vtexLegend.SetLookupTable(lookUpTableLegend)
vtexLegend.Update()
# reposition plane
legendPlaneObj.data_source.trait_set(
origin=(legendExtent[0],legendExtent[2],legendExtent[4]),
point1=(legendExtent[1],legendExtent[2],legendExtent[4]),
point2=(legendExtent[0],legendExtent[2],legendExtent[5])
)
legendPlane.actor.actor.texture = vtexLegend
offset=5
textHandle=mlab.text3d(legendExtent[1]-30,legendExtent[3],legendExtent[5]-offset,
"Legend",color=(0.,0.,0.),orient_to_camera=False,scale=3,orientation=(90.,180.,0.))
for iLabel in legendLabels:
offset+=10
textHandle=mlab.text3d(legendExtent[1]-20,legendExtent[3],legendExtent[5]-offset,
iLabel,color=(0.,0.,0.),orient_to_camera=False,scale=3,orientation=(90.,180.,0.))
mlab.plot3d([legendExtent[1]-5,legendExtent[1]-15],[legendExtent[3],legendExtent[3]],[legendExtent[5]-offset+2,legendExtent[5]-offset+2],
tube_radius=0.6,color=fiberObj.getColor(iLabel))
def plotPororityMask(V_porosity,rangeOutline):
src = mlab.pipeline.scalar_field(V_porosity)
src.spacing = [1, 1, 1]
if rangeOutline is None:
src.origin = [0.,0.,0.]
else:
src.origin = [rangeOutline[0],rangeOutline[2],rangeOutline[4]]
src.update_image_data = True
#smooting
# blur = mlab.pipeline.user_defined(src, filter='ImageGaussianSmooth')
# Extract views of the outside surface.
voi = mlab.pipeline.extract_grid(src)
# Add surface module to the cylinder source
outer = mlab.pipeline.iso_surface(voi, contours=[250, ],
color=(0.1, 0.1, 0.1),opacity=0.25)
def addPlaneWidgets(V_widget,engine,widgetLUT=None,axOnly=None,rangeOutline=None):
src1 = mlab.pipeline.scalar_field(V_widget)
src1.spacing = [1, 1, 1]
if rangeOutline is None:
src1.origin = [0.,0.,0.]
else:
src1.origin = [rangeOutline[0],rangeOutline[2],rangeOutline[4]]
src1.update_image_data = True
ipw_z = ImagePlaneWidget()
engine.add_module(ipw_z)
if axOnly is None:
ipw_z.ipw.plane_orientation = 'z_axes'
else:
ipw_z.ipw.plane_orientation = axOnly
ipw_z.module_manager.scalar_lut_manager.show_scalar_bar = False
if widgetLUT is not None:
ipw_z.module_manager.scalar_lut_manager.lut_mode = widgetLUT
# ipw.module_manager.scalar_lut_manager.lut_mode = 'black-white'
# ipw.module_manager.scalar_lut_manager.lut_mode = 'binary'
# ipw.module_manager.scalar_lut_manager.lut.table = lutNumpyArray
if axOnly is None:
ipw_x = ImagePlaneWidget()
engine.add_module(ipw_x)
ipw_x.ipw.plane_orientation = 'x_axes'
ipw_y = ImagePlaneWidget()
engine.add_module(ipw_y)
ipw_y.ipw.plane_orientation = 'y_axes'
##################################################################################
# Visualise fibre tracks
##################################################################################
def makeVisualisation(fiberStruct, V_porosity, V_widget, rangeOutline, params,widgetLUT='jet',makeLegendHandle=True):
# (0.2,0.2,0.2)
fig = mlab.figure(size=(1200,900),bgcolor=(0.9,0.9,0.9),fgcolor=(1.,1.,1.))
engine = mlab.get_engine()
if params["plotOnlyStitchedFibers"]:
params["plotRejectedFibers"]=False
if fiberStruct:
numFibersTracked=len(fiberObj.classAttributes["listFiberIDs_tracked"])
else:
numFibersTracked=0
for fibID,fib in fiberStruct.items():
if params["plotRejectedFibers"]: # plot ALL fibers
fiberPloting(fib,fibID,len(fiberStruct),numFibersTracked,engine,params)
elif params["plotOnlyStitchedFibers"]:
doNotPlotSet={"basic","too short","too steep"}
if not {fib.colorLabel}.intersection(doNotPlotSet):
fiberPloting(fib,fibID,len(fiberStruct),numFibersTracked,engine,params)
elif params["plotOnlyTrackedFibers"]:
if fibID in fib.classAttributes["listFiberIDs_tracked"]:
fiberPloting(fib,fibID,len(fiberStruct),numFibersTracked,engine,params)
else:
if not fib.rejected:
fiberPloting(fib,fibID,len(fiberStruct),numFibersTracked,engine,params)
mlab.outline(extent=rangeOutline,color=(.3,.3,.3))
axes = mlab.axes(color=(0., 0., 0.), nb_labels=11)
axes.title_text_property.color = (0., 0., 0.)
axes.title_text_property.font_family = 'times'
axes.label_text_property.color = (0., 0., 0.)
axes.label_text_property.font_family = 'times'
axes.axes.font_factor=0.65
if makeLegendHandle:
makeLegend(rangeOutline,params["plotRejectedFibers"])
if params["plotPorosityMask"]:
print("Plotting porosity mask")
plotPororityMask(V_porosity,rangeOutline)
scene = engine.scenes[0]
topToBottom=False
# bluish-greyscale colormap
lookUpTable=vtk.vtkLookupTable()
lookUpTable.SetHueRange(0.5,0.6)
lookUpTable.SetNumberOfColors(256)
lookUpTable.SetSaturationRange(0.,0.5)
lookUpTable.SetValueRange(0.,1.)
lookUpTable.SetAlpha(1.)
lookUpTable.Build()
lutNumpyArray=vtk_to_numpy(lookUpTable.GetTable())
if params["staticCam"]:
cameraConfig.shiftCamera(params["cameraConfigKey"],scene,0.)
if params["planeWidgets"]:
print("adding plane widgets")
addPlaneWidgets(V_widget,engine,widgetLUT,rangeOutline=rangeOutline)
if params["panningPlane"]:
delayVal=100
else:
delayVal=50000
@mlab.animate(delay=delayVal)
def anim():
if params["panningPlane"]:
# plot plane with CT slice
planeObj = BuiltinSurface(source='plane')
plane = mlab.pipeline.surface(planeObj)
while True:
if topToBottom:
imVec=range(V_widget.shape[2]-1,0,-1)
else:
imVec=range(V_widget.shape[2])
for im in imVec:
if im == 0 and params["drawEllipsoids"] and params["panningPlane"]:
for iFib in range(len(fiberStruct)):
if not params["plotRejectedFibers"]:
#fiberStruct[iFib]["wireObj"].scene.disable_render = False
fiberStruct[iFib].wireObj.visible=False
# fiberStruct[iFib]["surfaceObj"].scene.disable_render = False
fiberStruct[iFib].surfaceObj.visible=False
elif params["plotRejectedFibers"]:
#fiberStruct[iFib]["wireObj"].scene.disable_render = False
fiberStruct[iFib].wireObj.visible=False
# fiberStruct[iFib]["surfaceObj"].scene.disable_render = False
fiberStruct[iFib].surfaceObj.visible=False
print('Updating scene...')
if params["panningPlane"]:
imSlice = np.transpose(V_widget[:,:,im])
grid = vtk.vtkImageData()
grid.SetDimensions((imSlice.shape[1],imSlice.shape[0],1))
vtkarr = numpy_to_vtk(imSlice.ravel())
vtkarr.SetName('imSlice')
grid.GetPointData().AddArray(vtkarr)
grid.GetPointData().SetActiveScalars('imSlice')
vtex = vtk.vtkTexture()
vtex.SetInputDataObject(grid)
vtex.SetLookupTable(lookUpTable)
vtex.Update()
# reposition plane
planeObj.data_source.trait_set(
origin=(rangeOutline[0], rangeOutline[2], float(im)),
point1=(rangeOutline[0]+imSlice.shape[1], rangeOutline[2], float(im)),
point2=(rangeOutline[0], rangeOutline[2]+imSlice.shape[0], float(im))
)
plane.actor.actor.texture = vtex
if im%10==0:
mlab.text3d(float(imSlice.shape[0])+5,float(imSlice.shape[1])+5,float(im),
"{}".format(im),color=(0.,0.,0.))
if params["drawEllipsoids"]:
for iFib in range(len(fiberStruct)):
if not params["plotRejectedFibers"]:
if fiberStruct[iFib].startPnt[2]<(float(im)+5) and\
fiberStruct[iFib].endPnt[2]>(float(im)-5):
fiberStruct[iFib].wireObj.visible=True
fiberStruct[iFib].surfaceObj.visible=True
else:
fiberStruct[iFib].wireObj.visible=False
fiberStruct[iFib].surfaceObj.visible=False
elif params["plotRejectedFibers"]:
if fiberStruct[iFib].startPnt[2]<(float(im)+5) and\
fiberStruct[iFib].endPnt[2]>(float(im)-5):
fiberStruct[iFib].wireObj.visible=True
fiberStruct[iFib].surfaceObj.visible=True
else:
fiberStruct[iFib].wireObj.visible=False
fiberStruct[iFib].surfaceObj.visible=False
if not params["staticCam"]:
cameraConfig.shiftCamera(params["cameraConfigKey"],scene,im)
yield
anim()
mlab.show(stop=False)