forked from mathLab/PyGeM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ffd.py
More file actions
420 lines (351 loc) · 17.7 KB
/
Copy pathtest_ffd.py
File metadata and controls
420 lines (351 loc) · 17.7 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import filecmp
import os
from unittest import TestCase
import numpy as np
from pygem import FFD
class TestFFD(TestCase):
def test_class_members_default_n_control_points(self):
params = FFD()
assert np.array_equal(params.n_control_points, [2, 2, 2])
def test_class_members_default_conversion_unit(self):
params = FFD()
assert params.conversion_unit == 1.
def test_class_members_default_box_length(self):
params = FFD()
assert np.array_equal(params.box_length, np.ones(3))
def test_class_members_default_box_origin(self):
params = FFD()
assert np.array_equal(params.box_origin, np.zeros(3))
def test_class_members_default_rot_angle(self):
params = FFD()
assert np.array_equal(params.rot_angle, np.zeros(3))
def test_class_members_default_array_mu_x(self):
params = FFD()
np.testing.assert_array_almost_equal(params.array_mu_x,
np.zeros((2, 2, 2)))
def test_class_members_default_array_mu_y(self):
params = FFD()
np.testing.assert_array_almost_equal(params.array_mu_y,
np.zeros((2, 2, 2)))
def test_class_members_default_array_mu_z(self):
params = FFD()
np.testing.assert_array_almost_equal(params.array_mu_z,
np.zeros((2, 2, 2)))
def test_class_members_default_rotation_matrix(self):
params = FFD()
np.testing.assert_array_almost_equal(params.rotation_matrix, np.eye(3))
def test_class_members_default_position_vertices(self):
params = FFD()
expected_matrix = np.array([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.],
[0., 0., 1.]])
np.testing.assert_array_almost_equal(params.position_vertices,
expected_matrix)
def test_class_members_generic_n_control_points(self):
params = FFD([2, 3, 5])
assert np.array_equal(params.n_control_points, [2, 3, 5])
def test_class_members_generic_array_mu_x(self):
params = FFD([2, 3, 5])
np.testing.assert_array_almost_equal(params.array_mu_x,
np.zeros((2, 3, 5)))
def test_class_members_generic_array_mu_y(self):
params = FFD([2, 3, 5])
np.testing.assert_array_almost_equal(params.array_mu_y,
np.zeros((2, 3, 5)))
def test_class_members_generic_array_mu_z(self):
params = FFD([2, 3, 5])
np.testing.assert_array_almost_equal(params.array_mu_z,
np.zeros((2, 3, 5)))
def test_reflect_n_control_points_1(self):
params = FFD([2, 3, 5])
params.reflect(axis=0)
assert np.array_equal(params.n_control_points, [3, 3, 5])
def test_reflect_n_control_points_2(self):
params = FFD([2, 3, 5])
params.reflect(axis=1)
assert np.array_equal(params.n_control_points, [2, 5, 5])
def test_reflect_n_control_points_3(self):
params = FFD([2, 3, 5])
params.reflect(axis=2)
assert np.array_equal(params.n_control_points, [2, 3, 9])
def test_reflect_box_length_1(self):
params = FFD([2, 3, 5])
params.reflect(axis=0)
assert params.box_length[0] == 2
def test_reflect_box_length_2(self):
params = FFD([2, 3, 5])
params.reflect(axis=1)
assert params.box_length[1] == 2
def test_reflect_box_length_3(self):
params = FFD([2, 3, 5])
params.reflect(axis=2)
assert params.box_length[2] == 2
def test_reflect_wrong_axis(self):
params = FFD([2, 3, 5])
with self.assertRaises(ValueError):
params.reflect(axis=4)
def test_reflect_wrong_symmetry_plane_1(self):
params = FFD([3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
params.array_mu_x = np.array(
[0.2, 0., 0., 0., 0.5, 0., 0., 0., 1., 0., 0.3, 0.]).reshape((3, 2,
2))
with self.assertRaises(RuntimeError):
params.reflect(axis=0)
def test_reflect_wrong_symmetry_plane_2(self):
params = FFD([3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
params.array_mu_y = np.array(
[0.2, 0., 0., 0., 0.5, 0., 0., 0., 1., 0., 0.3, 0.]).reshape((3, 2,
2))
with self.assertRaises(RuntimeError):
params.reflect(axis=1)
def test_reflect_wrong_symmetry_plane_3(self):
params = FFD([3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
params.array_mu_z = np.array(
[0.2, 0., 0., 0., 0.5, 0., 0., 0., 1., 0., 0.3, 0.1]).reshape((3, 2,
2))
with self.assertRaises(RuntimeError):
params.reflect(axis=2)
def test_reflect_axis_0(self):
params = FFD([3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
params.array_mu_x = np.array(
[0.2, 0., 0., 0., 0.5, 0., 0., .2, 0., 0., 0., 0.]).reshape((3, 2,
2))
params.reflect(axis=0)
array_mu_x_exact = np.array([0.2, 0., 0., 0., 0.5, 0., 0., 0.2, 0.,
0., 0., 0., -0.5, -0., -0., -0.2, -0.2, -0., -0., -0.]).reshape((5, 2,
2))
np.testing.assert_array_almost_equal(params.array_mu_x,
array_mu_x_exact)
def test_reflect_axis_1(self):
params = FFD([3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
params.array_mu_y = np.array(
[0.2, 0., 0., 0., 0.5, 0., 0., 0., 0., 0., 0., 0.]).reshape((3, 2,
2))
params.reflect(axis=1)
array_mu_y_exact = np.array([0.2, 0., 0., 0., -0.2, -0., 0.5, 0., 0., 0.,
-0.5, -0., 0., 0., 0., 0., 0., 0.]).reshape((3, 3, 2))
np.testing.assert_array_almost_equal(params.array_mu_y,
array_mu_y_exact)
def test_reflect_axis_2(self):
params = FFD([3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
params.array_mu_z = np.array(
[0.2, 0., 0., 0., 0.5, 0., 0., 0., 0., 0., 0., 0.]).reshape((3, 2,
2))
params.reflect(axis=2)
array_mu_z_exact = np.array([0.2, 0., -0.2, 0., 0., 0., 0.5, 0., -0.5,
0., 0., -0., 0., 0., -0., 0., 0., -0.]).reshape((3, 2, 3))
np.testing.assert_array_almost_equal(params.array_mu_z,
array_mu_z_exact)
def test_read_parameters_conversion_unit(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
assert params.conversion_unit == 1.
def test_read_parameters_n_control_points(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
assert np.array_equal(params.n_control_points, [3, 2, 2])
def test_read_parameters_box_length_x(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
assert np.array_equal(params.box_length, [45.0, 90.0, 90.0])
def test_read_parameters_box_origin(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
box_origin_exact = np.array([-20.0, -55.0, -45.0])
np.testing.assert_array_almost_equal(params.box_origin,
box_origin_exact)
def test_read_parameters_rot_angle_x(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
assert np.array_equal(params.rot_angle, [20.3, 11.0, 0.])
def test_read_parameters_array_mu_x(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
array_mu_x_exact = np.array(
[0.2, 0., 0., 0., 0.5, 0., 0., 0., 1., 0., 0., 0.]).reshape((3, 2,
2))
np.testing.assert_array_almost_equal(params.array_mu_x,
array_mu_x_exact)
def test_read_parameters_array_mu_y(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
array_mu_y_exact = np.array(
[0., 0., 0.5555555555, 0., 0., 0., 0., 0., -1., 0., 0.,
0.]).reshape((3, 2, 2))
np.testing.assert_array_almost_equal(params.array_mu_y,
array_mu_y_exact)
def test_read_parameters_array_mu_z(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
array_mu_z_exact = np.array(
[0., -0.2, 0., -0.45622985, 0., 0., 0., 0., -1.22, 0., -1.,
0.]).reshape((3, 2, 2))
np.testing.assert_array_almost_equal(params.array_mu_z,
array_mu_z_exact)
def test_read_parameters_rotation_matrix(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
rotation_matrix_exact = np.array(
[[0.98162718, 0., 0.190809], [0.06619844, 0.93788893, -0.34056147],
[-0.17895765, 0.34693565, 0.92065727]])
np.testing.assert_array_almost_equal(params.rotation_matrix,
rotation_matrix_exact)
def test_read_parameters_position_vertex_0_origin(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
np.testing.assert_array_almost_equal(params.position_vertices[0],
params.box_origin)
def test_read_parameters_position_vertex_0(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
position_vertices = np.array(
[[-20.0, -55.0, -45.0], [24.17322326, -52.02107006, -53.05309404],
[-20., 29.41000412,
-13.77579136], [-2.82719042, -85.65053198, 37.85915459]])
np.testing.assert_array_almost_equal(params.position_vertices,
position_vertices)
def test_read_parameters_failing_filename_type(self):
params = FFD(n_control_points=[3, 2, 2])
with self.assertRaises(TypeError):
params.read_parameters(3)
def test_read_parameters_filename_default_existance(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters()
outfilename = 'parameters.prm'
assert os.path.isfile(outfilename)
os.remove(outfilename)
def test_read_parameters_filename_default(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters()
outfilename = 'parameters.prm'
outfilename_expected = 'tests/test_datasets/parameters_default.prm'
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
os.remove(outfilename)
def test_write_parameters_failing_filename_type(self):
params = FFD(n_control_points=[3, 2, 2])
with self.assertRaises(TypeError):
params.write_parameters(5)
def test_write_parameters_filename_default_existance(self):
params = FFD(n_control_points=[3, 2, 2])
params.write_parameters()
outfilename = 'parameters.prm'
assert os.path.isfile(outfilename)
os.remove(outfilename)
def test_write_parameters_filename_default(self):
params = FFD(n_control_points=[3, 2, 2])
params.write_parameters()
outfilename = 'parameters.prm'
outfilename_expected = 'tests/test_datasets/parameters_default.prm'
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
os.remove(outfilename)
def test_write_parameters(self):
params = FFD(n_control_points=[3, 2, 2])
params.read_parameters('tests/test_datasets/parameters_sphere.prm')
outfilename = 'tests/test_datasets/parameters_sphere_out.prm'
outfilename_expected = 'tests/test_datasets/parameters_sphere_out_true.prm'
params.write_parameters(outfilename)
self.assertTrue(filecmp.cmp(outfilename, outfilename_expected))
os.remove(outfilename)
"""
def test_save_points(self):
params = FFD()
params.read_parameters(
filename='tests/test_datasets/parameters_test_ffd_sphere.prm')
outfilename = 'tests/test_datasets/box_test_sphere_out.vtk'
outfilename_expected = 'tests/test_datasets/box_test_sphere.vtk'
params.save_points(outfilename, False)
with open(outfilename, 'r') as out, open(outfilename_expected, 'r') as exp:
self.assertTrue(out.readlines()[1:] == exp.readlines()[1:])
os.remove(outfilename)
def test_save_points_deformed(self):
params = FFD()
params.read_parameters(
filename='tests/test_datasets/parameters_test_ffd_sphere.prm')
outfilename = 'tests/test_datasets/box_test_sphere_deformed_out.vtk'
outfilename_expected = 'tests/test_datasets/box_test_sphere_deformed.vtk'
params.save_points(outfilename, True)
with open(outfilename, 'r') as out, open(outfilename_expected, 'r') as exp:
self.assertTrue(out.readlines()[1:] == exp.readlines()[1:])
os.remove(outfilename)
"""
def test_print(self):
params = FFD(n_control_points=[3, 2, 2])
print(params)
# def test_build_bounding_box_1(self):
# origin = np.array([0., 0., 0.])
# tops = np.array([1., 1., 1.])
# cube = BRepPrimAPI_MakeBox(*tops).Shape()
# params = FFD()
# params.build_bounding_box(cube)
#
# np.testing.assert_array_almost_equal(params.box_length, tops, decimal=5)
#
# def test_build_bounding_box_2(self):
# origin = np.array([0., 0., 0.])
# tops = np.array([1., 1., 1.])
# cube = BRepPrimAPI_MakeBox(*tops).Shape()
# params = FFD()
# params.build_bounding_box(cube)
#
# expected_matrix = np.array([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.],
# [0., 0., 1.]])
# np.testing.assert_almost_equal(
# params.position_vertices, expected_matrix, decimal=5)
def test_set_position_of_vertices(self):
expected_matrix = np.array([[0., 0., 0.], [1., 0., 0.], [0., 1., 0.],
[0., 0., 1.]])
tops = np.array([1., 1., 1.])
params = FFD()
params.box_origin = expected_matrix[0]
params.box_length = tops - expected_matrix[0]
np.testing.assert_almost_equal(
params.position_vertices, expected_matrix, decimal=5)
def test_set_modification_parameters_to_zero(self):
params = FFD([5, 5, 5])
params.reset_weights()
np.testing.assert_almost_equal(
params.array_mu_x, np.zeros(shape=(5, 5, 5)))
np.testing.assert_almost_equal(
params.array_mu_y, np.zeros(shape=(5, 5, 5)))
np.testing.assert_almost_equal(
params.array_mu_z, np.zeros(shape=(5, 5, 5)))
def test_ffd_sphere_mod(self):
ffd = FFD()
ffd.read_parameters(
filename='tests/test_datasets/parameters_test_ffd_sphere.prm')
mesh_points = np.load('tests/test_datasets/meshpoints_sphere_orig.npy')
mesh_points_ref = np.load(
'tests/test_datasets/meshpoints_sphere_mod.npy')
mesh_points_test = ffd(mesh_points)
np.testing.assert_array_almost_equal(mesh_points_test, mesh_points_ref)
def test_fit_to_points_default_padding(self):
params = FFD()
points = np.array([[0., 0., 0.], [10., 10., 10.]])
params.fit_to_points(points)
expected_length = np.array([11., 11., 11.])
expected_origin = np.array([-0.5, -0.5, -0.5])
np.testing.assert_array_almost_equal(params.box_length, expected_length)
np.testing.assert_array_almost_equal(params.box_origin, expected_origin)
def test_fit_to_points_custom_padding(self):
params = FFD()
points = np.array([[-5., 2., 0.], [5., 6., 10.]])
# Test with 10% padding
params.fit_to_points(points, padding_factor=0.1)
expected_length = np.array([12., 4.8, 12.])
expected_origin = np.array([-6., 1.6, -1.])
np.testing.assert_array_almost_equal(params.box_length, expected_length)
np.testing.assert_array_almost_equal(params.box_origin, expected_origin)
def test_fit_to_points_zero_padding(self):
params = FFD()
points = np.array([[1., 1., 1.], [3., 4., 5.]])
# Test with exactly bounding the points (0% padding)
params.fit_to_points(points, padding_factor=0.0)
expected_length = np.array([2., 3., 4.])
expected_origin = np.array([1., 1., 1.])
np.testing.assert_array_almost_equal(params.box_length, expected_length)
np.testing.assert_array_almost_equal(params.box_origin, expected_origin)