-
Notifications
You must be signed in to change notification settings - Fork 5
/
test_vtk_surface_model.py
357 lines (260 loc) · 11.5 KB
/
test_vtk_surface_model.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
# -*- coding: utf-8 -*-
import os
import sys
import cv2
import numpy as np
import pytest
import vtk
from sksurgeryimage.utilities.utilities import are_similar
from vtk.util import colors
from sksurgeryvtk.models.vtk_surface_model import VTKSurfaceModel
@pytest.fixture(scope="function")
def valid_vtk_model():
input_file = 'tests/data/models/Prostate.vtk'
model = VTKSurfaceModel(input_file, colors.red)
return model
def test_valid_vtk_results_in_vtkpolydatareader(valid_vtk_model):
assert isinstance(valid_vtk_model.reader, vtk.vtkPolyDataReader)
def test_valid_stl_results_in_vtkstlreader():
input_file = 'tests/data/models/Fiducial.stl'
model = VTKSurfaceModel(input_file, colors.red)
assert isinstance(model.reader, vtk.vtkSTLReader)
def test_valid_ply_results_in_vtkplyreader():
input_file = 'tests/data/models/Tumor.ply'
model = VTKSurfaceModel(input_file, colors.red)
assert isinstance(model.reader, vtk.vtkPLYReader)
def test_invalid_because_model_file_format():
input_file = 'tox.ini'
with pytest.raises(ValueError):
VTKSurfaceModel(input_file, colors.red)
def test_its_valid_for_null_filename():
model = VTKSurfaceModel(None, colors.red)
assert model.source is not None
def test_invalid_because_filename_invalid():
with pytest.raises(TypeError):
VTKSurfaceModel(9, colors.red)
def test_invalid_because_color_red_not_float():
with pytest.raises(TypeError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (1, 0.0, 0.0))
def test_invalid_because_color_green_not_float():
with pytest.raises(TypeError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (0.0, 1, 0.0))
def test_invalid_because_color_blue_not_float():
with pytest.raises(TypeError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (0.0, 0.0, 1))
def test_invalid_because_color_red_too_low():
with pytest.raises(ValueError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (-1.0, 0.0, 0.0))
def test_invalid_because_color_green_too_low():
with pytest.raises(ValueError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (0.0, -1.0, 0.0))
def test_invalid_because_color_blue_too_low():
with pytest.raises(ValueError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (0.0, 0.0, -1.0))
def test_invalid_because_color_red_too_high():
with pytest.raises(ValueError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.1, 0.0, 0.0))
def test_invalid_because_color_green_too_high():
with pytest.raises(ValueError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.0, 1.1, 0.0))
def test_invalid_because_color_blue_too_high():
with pytest.raises(ValueError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.0, 0.0, 1.1))
def test_invalid_because_opacity_not_float():
with pytest.raises(TypeError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.0, 0.0, 1.0), opacity=1)
def test_invalid_because_opacity_too_low():
with pytest.raises(ValueError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.0, 0.0, 1.0), opacity=-1.0)
def test_invalid_because_opacity_too_high():
with pytest.raises(ValueError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.0, 0.0, 1.0), opacity=1.1)
def test_invalid_because_visibility_not_bool():
with pytest.raises(TypeError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.0, 0.0, 1.0), visibility=1.0)
def test_invalid_because_pickable_not_bool():
with pytest.raises(TypeError):
VTKSurfaceModel('tests/data/models/Prostate.vtk', (1.0, 0.0, 1.0),
pickable=1.0)
def test_invalid_because_name_is_none():
with pytest.raises(TypeError):
model = VTKSurfaceModel(None, (1.0, 0.0, 1.0))
model.set_name(None)
def test_invalid_because_name_is_empty():
with pytest.raises(ValueError):
model = VTKSurfaceModel(None, (1.0, 0.0, 1.0))
model.set_name("")
def test_ensure_setter_and_getter_set_something():
model = VTKSurfaceModel(None, (1.0, 0.0, 1.0))
assert model.get_name() == ""
model.set_name("banana")
assert model.get_name() == "banana"
def test_set_get_user_transform_do_set_something():
model = VTKSurfaceModel(None, (1.0, 0.0, 1.0))
vtk_matrix = vtk.vtkMatrix4x4()
vtk_matrix.Identity()
vtk_matrix.SetElement(0, 0, 2) # i.e. not identity
model.set_user_matrix(vtk_matrix)
result = model.get_user_matrix()
assert result is not None
assert result == vtk_matrix
def test_set_model_transform_is_used():
model = VTKSurfaceModel(None, (1.0, 0.0, 1.0))
vtk_matrix = vtk.vtkMatrix4x4()
vtk_matrix.Identity()
vtk_matrix.SetElement(0, 0, 2) # i.e. not identity
model.set_model_transform(vtk_matrix)
result = model.get_model_transform()
assert result is not None
assert result.GetElement(0, 0) == vtk_matrix.GetElement(0, 0)
def test_extract_points_and_normals_as_numpy_array():
input_file = 'tests/data/models/Prostate.vtk'
model = VTKSurfaceModel(input_file, colors.red)
number_of_points = model.get_number_of_points()
points = model.get_points_as_numpy()
assert isinstance(points, np.ndarray)
assert points.shape[0] == number_of_points
assert points.shape[1] == 3
normals = model.get_normals_as_numpy()
assert isinstance(normals, np.ndarray)
assert normals.shape[0] == number_of_points
assert normals.shape[1] == 3
def test_flat_shaded_on_coloured_background(setup_vtk_overlay_window):
# input_file = 'tests/data/models/liver.ply' # Don't use this one. It renders Grey, regardless of what colour you create it at.
input_file = 'tests/data/liver/liver_sub.ply'
model = VTKSurfaceModel(input_file, colors.white)
widget, _, app = setup_vtk_overlay_window
widget.add_vtk_actor(model.actor)
model.set_no_shading(True)
widget.get_background_renderer().SetBackground(0, 0, 1)
widget.show()
model.set_no_shading(False)
widget.get_background_renderer().SetBackground(0, 1, 0)
widget.show()
# app.exec()
def test_valid_set_texture_with_png_format(vtk_overlay_with_gradient_image):
input_file = 'tests/data/models/liver.ply'
model = VTKSurfaceModel(input_file, colors.red)
texture_file = 'tests/data/images/image0232.png'
model.set_texture(texture_file)
image, widget, _, app = vtk_overlay_with_gradient_image
widget.add_vtk_actor(model.actor)
widget.show()
# return model
with pytest.raises(ValueError):
model.set_texture('')
# Save the scene to a file for parity check.
# See test_set_texture_regression() below.
# This line should be run again if the code is (purposefully) changed.
# screenshot_filename = 'tests/data/images/set_texture_test.png'
# widget.save_scene_to_file(screenshot_filename)
# app.exec()
def test_valid_set_texture_with_jpeg_format(vtk_overlay_with_gradient_image):
input_file = 'tests/data/models/liver.ply'
model = VTKSurfaceModel(input_file, colors.red)
model.set_texture('tests/data/images/image0232.jpeg')
image, widget, _, app = vtk_overlay_with_gradient_image
widget.add_vtk_actor(model.actor)
widget.show()
# # return model
with pytest.raises(ValueError):
model.set_texture('')
# app.exec()
def test_valid_set_texture_with_jpg_format(vtk_overlay_with_gradient_image):
input_file = 'tests/data/models/liver.ply'
model = VTKSurfaceModel(input_file, colors.red)
model.set_texture('tests/data/images/image0232.jpg')
image, widget, _, app = vtk_overlay_with_gradient_image
widget.add_vtk_actor(model.actor)
widget.show()
# return model
with pytest.raises(ValueError):
model.set_texture('')
# app.exec()
def test_invalid_set_texture_because_texture_file_format():
input_file = 'tests/data/models/liver.ply'
model = VTKSurfaceModel(input_file, colors.red)
texture_file = 'tox.ini'
with pytest.raises(ValueError):
model.set_texture(texture_file)
def test_invalid_set_texture_because_texture_filename_empty():
input_file = 'tests/data/models/liver.ply'
model = VTKSurfaceModel(input_file, colors.red)
with pytest.raises(ValueError):
model.set_texture('')
def test_valid_unset_texture_when_called_with_none(vtk_overlay_with_gradient_image):
input_file = 'tests/data/models/liver.ply'
model = VTKSurfaceModel(input_file, colors.red)
model.set_texture('tests/data/images/image0232.jpg')
image, widget, _, app = vtk_overlay_with_gradient_image
widget.add_vtk_actor(model.actor)
widget.show()
model.set_texture(None)
# return model
with pytest.raises(ValueError):
model.set_texture('')
# app.exec()
def test_set_texture_regression(vtk_overlay_with_gradient_image):
"""
Test texture regression
For local test, remember to uncomment `_pyside_qt_app.exec()` at the end of this module
"""
in_github_ci = os.environ.get('CI')
print("Github CI: " + str(in_github_ci))
input_file = 'tests/data/models/liver.ply'
model = VTKSurfaceModel(input_file, colors.red)
model.set_texture('tests/data/images/image0232.png')
image, widget, _vtk_std_err, _pyside_qt_app = vtk_overlay_with_gradient_image
widget.add_vtk_actor(model.actor)
widget.resize(400, 400)
widget.AddObserver("ExitEvent", lambda o, e, a=_pyside_qt_app: a.quit())
widget.show()
widget.Initialize()
widget.Start()
# Read the saved scene and compare it with the current scene.
screenshot_filename = 'tests/data/images/set_texture_test.png'
screenshot = cv2.imread(screenshot_filename)
# OpenCV uses BGR while VTK uses RGB.
screenshot = cv2.cvtColor(screenshot, cv2.COLOR_BGR2RGB)
current_scene = widget.convert_scene_to_numpy_array()
tmp_dir = 'tests/output'
if not os.path.isdir(tmp_dir):
os.makedirs(tmp_dir)
cv2.imwrite(os.path.join(tmp_dir, 'screenshot.png'), screenshot)
cv2.imwrite(os.path.join(tmp_dir, 'current_scene.png'), current_scene)
assert are_similar(screenshot, current_scene, threshold=0.995,
metric=cv2.TM_CCOEFF_NORMED, mean_threshold=0.005)
# You don't really want this in a unit test, otherwise you can't exit.
# If you want to do interactive testing, please uncomment the following line
# _pyside_qt_app.exec()
widget.close()
def test_get_set_visibility():
input_file = 'tests/data/models/liver.ply'
model = VTKSurfaceModel(input_file, colors.red)
assert (isinstance(model.get_visibility(), bool))
assert (model.get_visibility())
model.set_visibility(False)
assert (not model.get_visibility())
model.set_visibility(True)
assert (model.get_visibility())
def test_get_set_outline():
"""Setting and getting the outline rendering status"""
input_file = 'tests/data/models/liver.ply'
model = VTKSurfaceModel(input_file, colors.red, visibility=True,
opacity=1.0, pickable=True,
outline=True)
assert (isinstance(model.get_outline(), bool))
assert (model.get_outline())
model.set_outline(False)
assert (not model.get_outline())
model.set_outline(True)
assert (model.get_outline())
def test_get_outline_actor():
"""Calling get_outline rendering without a camera"""
input_file = 'tests/data/models/liver.ply'
model = VTKSurfaceModel(input_file, colors.red, visibility=True,
opacity=1.0, pickable=True,
outline=True)
model.get_outline_actor(active_camera=None)
model.set_outline(False)
assert model.get_outline_actor(active_camera=None) is None