forked from pyvista/pyvista
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_polydata.py
385 lines (269 loc) · 10.7 KB
/
test_polydata.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
import os
from math import pi
import numpy as np
import pytest
import vtki
from vtki import examples
from vtki.plotting import system_supports_plotting
radius = 0.5
sphere = vtki.Sphere(radius, theta_resolution=10, phi_resolution=10)
sphere_shifted = vtki.Sphere(center=[0.5, 0.5, 0.5],
theta_resolution=10, phi_resolution=10)
dense_sphere = vtki.Sphere(radius, theta_resolution=100, phi_resolution=100)
try:
test_path = os.path.dirname(os.path.abspath(__file__))
test_data_path = os.path.join(test_path, 'test_data')
except:
test_data_path = '/home/alex/afrl/python/source/vtki/tests/test_data'
stl_test_file = os.path.join(test_data_path, 'sphere.stl')
ply_test_file = os.path.join(test_data_path, 'sphere.ply')
vtk_test_file = os.path.join(test_data_path, 'sphere.vtk')
test_files = [stl_test_file,
ply_test_file,
vtk_test_file]
def test_init():
mesh = vtki.PolyData()
assert not mesh.n_points
assert not mesh.n_cells
def test_init_from_pdata():
mesh = vtki.PolyData(sphere, deep=True)
assert mesh.n_points
assert mesh.n_cells
mesh.points[0] += 1
assert not np.allclose(sphere.points[0], mesh.points[0])
# @pytest.mark.parametrize('filename', test_files)
# def test_init_from_file(filename):
# mesh = vtki.PolyData(filename)
# assert mesh.faces.shape == sphere.faces.shape
# assert mesh.points.shape == sphere.points.shape
def test_init_from_arrays():
vertices = np.array([[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0.5, 0.5, -1]])
# mesh faces
faces = np.hstack([[4, 0, 1, 2, 3],
[3, 0, 1, 4],
[3, 1, 2, 4]]).astype(np.int8)
mesh = vtki.PolyData(vertices, faces)
assert mesh.n_points == 5
assert mesh.n_cells == 3
mesh = vtki.PolyData(vertices, faces, deep=True)
vertices[0] += 1
assert not np.allclose(vertices[0], mesh.points[0])
def test_init_from_arrays_triangular():
vertices = np.array([[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0.5, 0.5, -1]])
# mesh faces
faces = np.vstack([[3, 0, 1, 2],
[3, 0, 1, 4],
[3, 1, 2, 4]])
mesh = vtki.PolyData(vertices, faces)
assert mesh.n_points == 5
assert mesh.n_cells == 3
mesh = vtki.PolyData(vertices, faces, deep=True)
assert mesh.n_points == 5
assert mesh.n_cells == 3
def test_init_as_points():
vertices = np.array([[0, 0, 0],
[1, 0, 0],
[1, 1, 0],
[0, 1, 0],
[0.5, 0.5, -1]])
mesh = vtki.PolyData(vertices)
assert mesh.n_points == vertices.shape[0]
assert mesh.n_cells == vertices.shape[0]
def test_invalid_init():
with pytest.raises(ValueError):
mesh = vtki.PolyData(np.array([1]))
with pytest.raises(TypeError):
mesh = vtki.PolyData(np.array([1]), 'woa')
with pytest.raises(TypeError):
mesh = vtki.PolyData('woa', 'woa')
with pytest.raises(TypeError):
mesh = vtki.PolyData('woa', 'woa', 'woa')
def test_invalid_file():
with pytest.raises(Exception):
mesh = vtki.PolyData('file.bad')
with pytest.raises(TypeError):
filename = os.path.join(test_path, 'test_polydata.py')
mesh = vtki.PolyData(filename)
# with pytest.raises(Exception):
# vtki.PolyData(examples.hexbeamfile)
def test_geodesic():
geodesic = sphere.geodesic(0, sphere.n_points - 1)
assert isinstance(geodesic, vtki.PolyData)
def test_geodesic_distance():
distance = sphere.geodesic_distance(0, sphere.n_points - 1)
assert isinstance(distance, float)
def test_ray_trace():
points, ind = sphere.ray_trace([0, 0, 0], [1, 1, 1])
assert np.any(points)
assert np.any(ind)
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires system to support plotting")
def test_ray_trace_plot():
points, ind = sphere.ray_trace([0, 0, 0], [1, 1, 1], plot=True, first_point=True,
off_screen=True)
assert np.any(points)
assert np.any(ind)
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires system to support plotting")
def test_plot_curvature():
cpos = sphere.plot_curvature(off_screen=True)
assert isinstance(cpos, list)
def test_edge_mask():
mask = sphere.edge_mask(10)
def test_boolean_cut_inplace():
sub_mesh = sphere.copy()
sub_mesh.boolean_cut(sphere_shifted, inplace=True)
assert sub_mesh.n_points
assert sub_mesh.n_cells
def test_subtract():
sub_mesh = sphere - sphere_shifted
assert sub_mesh.n_points
assert sub_mesh.n_cells
def test_add():
add_mesh = sphere + sphere_shifted
npoints = sphere.n_points + sphere_shifted.n_points
assert add_mesh.n_points == npoints
nfaces = sphere.n_cells + sphere_shifted.n_cells
assert add_mesh.n_faces == nfaces
def test_boolean_add_inplace():
sub_mesh = sphere.copy()
sub_mesh.boolean_add(sphere_shifted, inplace=True)
assert sub_mesh.n_points
assert sub_mesh.n_cells
def test_boolean_union_inplace():
sub_mesh = sphere.boolean_union(sphere_shifted)
assert sub_mesh.n_points
assert sub_mesh.n_cells
sub_mesh = sphere.copy()
sub_mesh.boolean_union(sphere_shifted, inplace=True)
assert sub_mesh.n_points
assert sub_mesh.n_cells
def test_boolean_difference():
sub_mesh = sphere.copy()
sub_mesh.boolean_difference(sphere_shifted, inplace=True)
assert sub_mesh.n_points
assert sub_mesh.n_cells
sub_mesh = sphere.boolean_difference(sphere_shifted)
assert sub_mesh.n_points
assert sub_mesh.n_cells
@pytest.mark.parametrize('curv_type', ['mean', 'gaussian', 'maximum', 'minimum'])
def test_curvature(curv_type):
curv = sphere.curvature(curv_type)
assert np.any(curv)
assert curv.size == sphere.n_points
def test_invalid_curvature():
with pytest.raises(Exception):
curv = sphere.curvature('not valid')
@pytest.mark.parametrize('binary', [True, False])
@pytest.mark.parametrize('extension', ['stl', 'vtk', 'ply', 'vtp'])
def test_save(extension, binary, tmpdir):
filename = str(tmpdir.mkdir("tmpdir").join('tmp.%s' % extension))
sphere.save(filename, binary)
mesh = vtki.PolyData(filename)
assert mesh.faces.shape == sphere.faces.shape
assert mesh.points.shape == sphere.points.shape
def test_invalid_save():
with pytest.raises(Exception):
sphere.save('file.abc')
def test_tri_filter():
arrow = vtki.Arrow([0, 0, 0], [1, 1, 1])
assert arrow.faces.size % 4
arrow.tri_filter(inplace=True)
assert not(arrow.faces.size % 4)
@pytest.mark.parametrize('subfilter', ['butterfly', 'loop', 'linear'])
def test_subdivision(subfilter):
mesh = sphere.subdivide(1, subfilter)
assert mesh.n_points > sphere.n_points
assert mesh.n_faces > sphere.n_faces
mesh = sphere.copy()
mesh.subdivide(1, subfilter, inplace=True)
assert mesh.n_points > sphere.n_points
assert mesh.n_faces > sphere.n_faces
def test_invalid_subdivision():
with pytest.raises(Exception):
mesh = sphere.subdivide(1, 'not valid')
def test_extract_edges():
edges = sphere.extract_edges(90)
assert not edges.n_points
more_edges = sphere.extract_edges(10)
assert more_edges.n_points
def test_decimate():
mesh = sphere.copy()
mesh.decimate(0.5)
assert mesh.n_points < sphere.n_points
assert mesh.n_faces < sphere.n_faces
mesh = sphere.decimate(0.5, inplace=False)
assert mesh.n_points < sphere.n_points
assert mesh.n_faces < sphere.n_faces
def test_center_of_mass():
assert np.allclose(sphere.center_of_mass(), [0, 0, 0])
def test_compute_normals():
sphere_normals = sphere.copy()
sphere_normals.compute_normals()
point_normals = sphere_normals.point_arrays['Normals']
cell_normals = sphere_normals.cell_arrays['Normals']
assert point_normals.shape[0] == sphere.n_points
assert cell_normals.shape[0] == sphere.n_cells
def test_point_normals():
assert sphere.point_normals.shape[0] == sphere.n_points
def test_cell_normals():
assert sphere.cell_normals.shape[0] == sphere.n_cells
def test_face_normals():
assert sphere.face_normals.shape[0] == sphere.n_faces
def test_clip_plane():
clipped_sphere = sphere.copy()
clipped_sphere.clip_with_plane([0, 0, 0], [0, 0, -1])
faces = clipped_sphere.faces.reshape(-1 , 4)[:, 1:]
assert np.all(clipped_sphere.points[faces, 2] <= 0)
clipped_sphere = sphere.clip_with_plane([0, 0, 0], [0, 0, -1], inplace=False)
faces = clipped_sphere.faces.reshape(-1 , 4)[:, 1:]
assert np.all(clipped_sphere.points[faces, 2] <= 0)
def test_extract_largest():
mesh = sphere + vtki.Sphere(0.1, theta_resolution=5, phi_resolution=5)
largest = mesh.extract_largest()
assert largest.n_faces == sphere.n_faces
mesh.extract_largest(inplace=True)
assert mesh.n_faces == sphere.n_faces
def test_clean():
mesh = sphere + sphere
assert mesh.n_points > sphere.n_points
cleaned = mesh.clean(merge_tol=1E-5, inplace=False)
assert cleaned.n_points == sphere.n_points
mesh.clean()
assert mesh.n_points == sphere.n_points
def test_area():
radius = 0.5
ideal_area = 4*pi*radius**2
assert np.isclose(dense_sphere.area, ideal_area, rtol=1E-3)
def test_volume():
ideal_volume = (4/3.0)*pi*radius**3
assert np.isclose(dense_sphere.volume, ideal_volume, rtol=1E-3)
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires system to support plotting")
def test_plot_boundaries():
sphere.plot_boundaries(off_screen=True)
@pytest.mark.skipif(not system_supports_plotting(), reason="Requires system to support plotting")
def test_plot_normals():
sphere.plot_normals(off_screen=True)
def test_remove_points_any():
remove_mask = np.zeros(sphere.n_points, np.bool)
remove_mask[:3] = True
sphere_mod, ind = sphere.remove_points(remove_mask, inplace=False, mode='any')
assert sphere_mod.n_points + remove_mask.sum() == sphere.n_points
assert np.allclose(sphere_mod.points, sphere.points[ind])
def test_remove_points_all():
sphere_copy = sphere.copy()
sphere_copy.cell_arrays['ind'] = np.arange(sphere_copy.n_faces)
remove = sphere.faces[1:4]
sphere_copy.remove_points(remove, inplace=True, mode='all')
assert sphere_copy.n_points == sphere.n_points
assert sphere_copy.n_faces == sphere.n_faces - 1
def test_remove_points_fail():
arrow = vtki.Arrow([0, 0, 0], [1, 0, 0])
with pytest.raises(Exception):
arrow.remove_points(range(10))