-
Notifications
You must be signed in to change notification settings - Fork 0
/
isogm.py
385 lines (312 loc) · 12 KB
/
isogm.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
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 10 09:56:01 2020
@author: Jhon Corro
@author: Cristhyan De Marchena
"""
import vtk
# Min and max value from data
max = 0
min = 0
# planes
xplane = vtk.vtkPlane()
yplane = vtk.vtkPlane()
zplane = vtk.vtkPlane()
# SLIDE BAR COLORS
red_r = 224/255
red_g = 69/255
red_b = 85/255
green_r = 70/255
green_g = 224/255
green_b = 105/255
white = 242/255
def get_program_parameters():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('data_file', nargs='?', default=None, help='data file')
parser.add_argument('grad_file', nargs='?', default=None, help='grad map file')
parser.add_argument('isoval_file', nargs='?', default=None, help='iso value')
parser.add_argument('--cmap', dest='cmap_file', default=None, help='Colormap file')
parser.add_argument('--clip', dest='clip', nargs=3, type=int, default=None)
args = parser.parse_args()
return args.data_file, args.grad_file, args.isoval_file, args.cmap_file, args.clip
def read_file(file_name):
import os
if(file_name):
path, extension = os.path.splitext(file_name)
extension = extension.lower()
if extension == ".vti":
reader = vtk.vtkXMLImageDataReader()
reader.SetFileName(file_name)
reader.Update()
else:
# the file provided doesn't match the accepted extenstions
reader = None
else:
reader = None
return reader
# list of singleton values.
def read_isovalues(file_name):
values = list()
with open(file_name) as file:
line = file.readline()
while line:
if not line.startswith("#"):
values.append(int(line))
line = file.readline()
return values
# list of <value> <r> <g> <b>, separated by and space.
def read_cmap(file_name):
values = list()
with open(file_name) as file:
line = file.readline()
while line:
if not line.startswith("#"):
arr = line.split(" ")
value, r, g, b = arr
rgb = [int(r)/255, int(g)/255, int(b)/255]
values.append([int(value), rgb])
line = file.readline()
return values
def generate_ctf(cmap):
ctf = vtk.vtkColorTransferFunction()
if(cmap):
[ctf.AddRGBPoint(value, rgb[0], rgb[1], rgb[2]) for(value, rgb) in cmap]
else:
ctf.AddRGBPoint(min, 31/255, 162/255, 255/255)
ctf.AddRGBPoint(max/4, 163/255, 99/255, 235/255)
ctf.AddRGBPoint(max/2, 255/255, 102/255, 102/255)
ctf.AddRGBPoint(max *3/4, 235/255, 183/255, 113/255)
ctf.AddRGBPoint(max, 255/255, 251/255, 19/255)
return ctf
def generate_plane_origins(clip):
# origins of the planes
origins = vtk.vtkPoints()
origins.SetNumberOfPoints(3)
if(clip):
if(clip[0]):
origins.InsertPoint(0, [clip[0], 0, 0]) # x
else:
origins.InsertPoint(0, [0, 0, 0]) # x
if(clip[1]):
origins.InsertPoint(1, [0, clip[1], 0]) # y
else:
origins.InsertPoint(1, [0, 0, 0]) # y
if(clip[2]):
origins.InsertPoint(2, [0, 0, clip[2]]) # z
else:
origins.InsertPoint(2, [0, 0, 0]) # z
return origins
def generate_plane_normals():
normals = vtk.vtkDoubleArray()
normals.SetNumberOfComponents(3)
normals.SetNumberOfTuples(3)
normals.SetTuple(0, [1, 0, 0])
normals.SetTuple(1, [0, 1, 0])
normals.SetTuple(2, [0, 0, 1])
return normals
def generate_actors(data, gradient_magnitude, iso_values, cmap, clip):
# contour
iso = vtk.vtkContourFilter()
iso.SetInputConnection(data.GetOutputPort())
if(iso_values):
[iso.SetValue(index, value) for(index, value) in enumerate(iso_values)]
else:
iso.SetValue(0, max/4)
#probe
probe = vtk.vtkProbeFilter()
probe.SetInputConnection(iso.GetOutputPort())
probe.SetSourceConnection(gradient_magnitude.GetOutputPort())
# generate vtkPlanes stuff.
origins = generate_plane_origins(clip)
normals = generate_plane_normals()
# the list of planes
planes = vtk.vtkPlanes()
planes.SetPoints(origins)
planes.SetNormals(normals)
planes.GetPlane(0, xplane)
planes.GetPlane(1, yplane)
planes.GetPlane(2, zplane)
xclipper = vtk.vtkClipPolyData()
xclipper.SetInputConnection(probe.GetOutputPort())
xclipper.SetClipFunction(xplane)
yclipper = vtk.vtkClipPolyData()
yclipper.SetInputConnection(xclipper.GetOutputPort())
yclipper.SetClipFunction(yplane)
zclipper = vtk.vtkClipPolyData()
zclipper.SetInputConnection(yclipper.GetOutputPort())
zclipper.SetClipFunction(zplane)
ctf = generate_ctf(cmap)
clipMapper = vtk.vtkDataSetMapper()
clipMapper.SetLookupTable(ctf)
clipMapper.SetInputConnection(zclipper.GetOutputPort())
clipMapper.SetScalarRange(0, 255)
# Generate iso surface actor from iso surface mapper.
isoActor = vtk.vtkActor()
isoActor.SetMapper(clipMapper)
return [isoActor]
def set_slide_bar_colors(bar):
bar.GetSliderProperty().SetColor(red_r, red_g, red_b)
bar.GetTitleProperty().SetColor(white, white, white)
bar.GetLabelProperty().SetColor(red_r, red_g, red_b)
bar.GetSelectedProperty().SetColor(green_r, green_g, green_b)
bar.GetTubeProperty().SetColor(white, white, white)
bar.GetCapProperty().SetColor(red_r, red_g, red_b)
return bar
def generate_x_axis_slide_bar(max, value):
# Create Slidebar
slide_bar = vtk.vtkSliderRepresentation2D()
# Set range and title.
slide_bar.SetMinimumValue(0)
slide_bar.SetMaximumValue(max)
if(value):
slide_bar.SetValue(value)
else:
slide_bar.SetValue(0)
slide_bar.SetTitleText("X clip")
# Set colors.
slide_bar = set_slide_bar_colors(slide_bar)
# Set coordinates.
slide_bar.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
slide_bar.GetPoint1Coordinate().SetValue(0.02, 0.5)
slide_bar.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
slide_bar.GetPoint2Coordinate().SetValue(0.22 , 0.5)
return slide_bar
def x_axis_custom_callback(obj, event):
value = int (obj.GetRepresentation().GetValue())
global xplane
xplane.SetOrigin(value, 0, 0)
def generate_y_axis_slide_bar(max, value):
# Create Slidebar
slide_bar = vtk.vtkSliderRepresentation2D()
# Set range and title.
slide_bar.SetMinimumValue(0)
slide_bar.SetMaximumValue(max)
if(value):
slide_bar.SetValue(value)
else:
slide_bar.SetValue(0)
slide_bar.SetTitleText("Y clip")
# Set colors.
slide_bar = set_slide_bar_colors(slide_bar)
# Set coordinates.
slide_bar.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
slide_bar.GetPoint1Coordinate().SetValue(0.02, 0.3)
slide_bar.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
slide_bar.GetPoint2Coordinate().SetValue(0.22 , 0.3)
return slide_bar
def y_axis_custom_callback(obj, event):
value = int (obj.GetRepresentation().GetValue())
global yplane
yplane.SetOrigin(0, value, 0)
def generate_z_axis_slide_bar(max, value):
# Create Slidebar
slide_bar = vtk.vtkSliderRepresentation2D()
# Set range and title.
slide_bar.SetMinimumValue(0)
slide_bar.SetMaximumValue(max)
if(value):
slide_bar.SetValue(value)
else:
slide_bar.SetValue(0)
slide_bar.SetTitleText("Z clip")
# Set colors.
slide_bar = set_slide_bar_colors(slide_bar)
# Set coordinates.
slide_bar.GetPoint1Coordinate().SetCoordinateSystemToNormalizedDisplay()
slide_bar.GetPoint1Coordinate().SetValue(0.02, 0.1)
slide_bar.GetPoint2Coordinate().SetCoordinateSystemToNormalizedDisplay()
slide_bar.GetPoint2Coordinate().SetValue(0.22 , 0.1)
return slide_bar
def z_axis_custom_callback(obj, event):
value = int (obj.GetRepresentation().GetValue())
global zplane
zplane.SetOrigin(0, 0, value)
def generate_gui(actors, cmap, clip):
actorBounds = actors[0].GetBounds()
maxX = int(actorBounds[1] + 1)
maxY = int(actorBounds[3] + 1)
maxZ = int(actorBounds[5] + 1)
# Create renderer stuff
renderer = vtk.vtkRenderer()
renderer_window = vtk.vtkRenderWindow()
renderer_window.AddRenderer(renderer)
renderer_window_interactor = vtk.vtkRenderWindowInteractor()
renderer_window_interactor.SetRenderWindow(renderer_window)
# Add x-axis slide bar
x_axis_slide_bar = generate_x_axis_slide_bar(maxX, clip[0]) if clip else generate_x_axis_slide_bar(maxX, 0)
x_axis_slider_widget = vtk.vtkSliderWidget()
x_axis_slider_widget.SetInteractor(renderer_window_interactor)
x_axis_slider_widget.SetRepresentation(x_axis_slide_bar)
x_axis_slider_widget.AddObserver("InteractionEvent", x_axis_custom_callback)
x_axis_slider_widget.EnabledOn()
# Add y-axis slide bar
y_axis_slide_bar = generate_y_axis_slide_bar(maxY, clip[1]) if clip else generate_y_axis_slide_bar(maxY, 0)
y_axis_slider_widget = vtk.vtkSliderWidget()
y_axis_slider_widget.SetInteractor(renderer_window_interactor)
y_axis_slider_widget.SetRepresentation(y_axis_slide_bar)
y_axis_slider_widget.AddObserver("InteractionEvent", y_axis_custom_callback)
y_axis_slider_widget.EnabledOn()
# Add z-axis slide bar
z_axis_slide_bar = generate_z_axis_slide_bar(maxZ, clip[2]) if clip else generate_z_axis_slide_bar(maxZ, 0)
z_axis_slider_widget = vtk.vtkSliderWidget()
z_axis_slider_widget.SetInteractor(renderer_window_interactor)
z_axis_slider_widget.SetRepresentation(z_axis_slide_bar)
z_axis_slider_widget.AddObserver("InteractionEvent", z_axis_custom_callback)
z_axis_slider_widget.EnabledOn()
ctf = generate_ctf(cmap)
scalar_bar = vtk.vtkScalarBarActor()
scalar_bar.SetOrientationToHorizontal()
scalar_bar.SetTextPositionToPrecedeScalarBar()
scalar_bar.UnconstrainedFontSizeOff()
# Pops CTF from the actors' list
scalar_bar.SetLookupTable(ctf)
scalar_bar.SetNumberOfLabels(len(cmap) if cmap else 5)
scalar_bar.SetLabelFormat("%-6.0f")
scalar_bar.SetPosition(0.24, 0.02)
scalar_bar.SetHeight(0.1)
scalar_bar.SetWidth(0.7)
# Add the actors and camera to the renderer, set background and size
for index, actor in enumerate(actors):
renderer.AddActor(actor)
renderer.AddActor2D(scalar_bar)
renderer.ResetCamera()
renderer.GetActiveCamera().Roll(200)
renderer.GetActiveCamera().Elevation(90)
renderer.GetActiveCamera().Azimuth(0)
renderer.SetBackground(0.1, 0.1, 0.1)
renderer.ResetCameraClippingRange()
renderer_window.SetSize(renderer_window.GetScreenSize());
cam1 = renderer.GetActiveCamera()
cam1.Zoom(0.8)
# Smoother camera controls
renderer_window_interactor.GetInteractorStyle().SetCurrentStyleToTrackballCamera();
renderer_window_interactor.Initialize()
renderer_window.Render()
renderer_window.SetWindowName('IsoGM')
renderer_window.Render()
renderer_window_interactor.Start()
def update_max_min_from_data(data):
_min, _max = data.GetOutput().GetScalarRange()
global min
global max
min = _min
max = _max
def main():
# Get file paths from cli params.
data_file, grad_file, isoval_file, cmap_file, clip = get_program_parameters()
# Read data file.
data = read_file(data_file)
gradient_magnitude = read_file(grad_file)
iso_values = read_isovalues(isoval_file)
cmap = read_cmap(cmap_file) if cmap_file else None
# update min and max
update_max_min_from_data(gradient_magnitude)
if(data):
actors = generate_actors(data, gradient_magnitude, iso_values, cmap, clip)
# Generate GUI
generate_gui(actors, cmap, clip)
else:
print('The data file was not found or the file provided does not match neither the .vti and .vtp extension.')
if __name__ == '__main__':
main()