forked from andregouws/mrMeshPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmp_VTKProcessing.py
executable file
·149 lines (101 loc) · 4.88 KB
/
mp_VTKProcessing.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
## TODO header
import vtk
from numpy import pi
debug = True
def VTK_smoothing(the_smoother, the_mapper, iterations, relaxation_factor):
# Standard way to perform mesh smoothing via relaxation in VTK.
# I'm pretty sure this is a direct replication of what mrMesh was
# doing before.
if debug: print 'starting smoothing'
the_smoother.SetNumberOfIterations(iterations)
the_smoother.SetRelaxationFactor(relaxation_factor)
the_smoother.Modified()
the_smoother.Update()
if debug: print 'finished smoothing -setting up mapper'
the_mapper.SetInputConnection(the_smoother.GetOutputPort())
the_mapper.SetScalarModeToUsePointData()
the_mapper.SetColorModeToDefault()
the_mapper.Modified()
if debug: print 'finished mapper -setting up actor'
newActor = vtk.vtkActor()
newActor.SetMapper(the_mapper)
if debug: print 'finished actor'
return newActor
def VTK_updateMesh(currVTKInstance, colorData, mainWindowUI):
# user has send some new scalar values to be rendered on the mesh BUT COLORS ARE
# ALREADY CALCULATED IN MATLAB
if debug: print(colorData)
currVTKInstance.curr_polydata.GetPointData().SetScalars(colorData)
currVTKInstance.curr_polydata.Modified()
currVTKInstance.curr_smoother.Update()
currVTKInstance.curr_mapper.SetColorModeToDefault()
currVTKInstance.curr_mapper.Modified()
# in case of error when drawing ROIs we can revert the color map
# turns out that later processes access the inherited renderwindowinteractor (?)
# so lets put all the above in the scope of that
currVTKInstance._Iren.ScalarsCopyForRevert = vtk.vtkUnsignedCharArray()
currVTKInstance._Iren.ScalarsCopyForRevert.DeepCopy(colorData)
newActor = vtk.vtkActor()
newActor.SetMapper(currVTKInstance.curr_mapper)
return newActor
if debug: print('colorData processed in VTK_updateMeshDirect')
''' OBSOLETE if we use the direct color import - i.e. all colour handling done in matlab
def VTK_updateMesh(currVTKInstance, newLUT, phaseData, cohData, cohThr, mainWindowUI):
# user has send some new scalar values to be rendered on the mesh
## TODO - other data types - assuming phase for now
# rescale phase data into range: 0-1024 to match lookup table
rescaledPhaseData = phaseData/(2.0*pi)*1024.0
curvature = currVTKInstance.curr_curvature
scalars = vtk.vtkFloatArray()
for i in range(len(rescaledPhaseData)):
if cohData[i] >= float(cohThr): # apply threshold
scalars.InsertNextValue(rescaledPhaseData[i])
else:
if curvature[i] < 0:
scalars.InsertNextValue(1024+50)
else:
scalars.InsertNextValue(1024+150)
currVTKInstance.curr_polydata.GetPointData().SetScalars(scalars)
currVTKInstance.curr_polydata.Modified()
currVTKInstance.curr_smoother.Update()
currVTKInstance.curr_mapper.SetLookupTable(newLUT)
currVTKInstance.curr_mapper.SetScalarRange(0,1224)
currVTKInstance.curr_mapper.Modified()
newActor = vtk.vtkActor()
newActor.SetMapper(currVTKInstance.curr_mapper)
return newActor
if debug: print('here')
def VTK_buildLookupTable(r_vec, g_vec, b_vec):
# we need to build custom colour lookup table that can show colour
# data for vertices above threshold and grayscale anatomy at
# vertices that do nor reach threshold.
# I build a lookup table that is made up of two parts: the lower
# end of the table is a RGB colour map (entries 1-1024)- when we get
# the scalar values from vista for the overlay data (e.g. phase) we
# rescale the incoming data into the range 1-1024 so that the scalar
# values map explicitly onto a colour table value.
# The 'upper' part of the table (1025-1224) has grayscale values. In
# theory we could just have 2 extra entries in the table for light
# or dark gray, but interpolation in vtk can cause some odd effects
# and color blending so we pad and extra 200 values onto the table
#create an arbitrary LUT with 1224 entries, we'll overwrite this
cLUT = vtk.vtkLookupTable()
cLUT.SetHueRange(0,1)
cLUT.SetValueRange(1,1)
cLUT.SetSaturationRange(1,1)
cLUT.SetNumberOfColors(1224)
cLUT.Build() #build it
# now overwrite it with the incoming RGB data for entries 1-1024
for i in range(1024):
cLUT.SetTableValue(i,(r_vec[i],g_vec[i],b_vec[i],1))
# and overwrite 1025-1124 with dark gray
for i in range(1024,1124):
cLUT.SetTableValue(i,(0.3,0.3,0.3,1))
# and the overwrite 1125-1224 with light gray
for i in range(1124,1224):
cLUT.SetTableValue(i,(0.7,0.7,0.7,1))
## TODO - allow modulation of light/dark gray levels?
# rebuild the table with these new values
cLUT.Modified()
return cLUT
'''