-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathtest_render.py
239 lines (192 loc) · 6.03 KB
/
test_render.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
from pyeit.eit.render import (
pt_in_triang,
map_image,
model_inverse_uv,
get_bounds,
scale_uv_list,
)
from pyeit.mesh.external import load_mesh
import numpy as np
from pathlib import Path
from numpy.testing import assert_almost_equal
import matplotlib.pyplot as plt
from pyeit.visual.plot import create_mesh_plot
parent_dir = str(Path(__file__).parent)
def test_pt_in_triang():
p0, p1, p2 = ([0, 0], [1, 0], [1, 1])
p_out = [0.25, 0.75]
p_in = [0.75, 0.25]
should_be_false = pt_in_triang(p_out, p0, p1, p2)
should_be_true = pt_in_triang(p_in, p0, p1, p2)
assert not should_be_false
assert should_be_true
def test_pt_in_triang_neg():
p0, p1, p2 = ([-1, -1], [1, -1], [0, 1])
p_out_1 = [-0.5, -1.5]
p_out_2 = [0.5, -1.5]
p_in = [-0.5, -0.75]
out_1 = pt_in_triang(p_out_1, p0, p1, p2)
out_2 = pt_in_triang(p_out_2, p0, p1, p2)
in_1 = pt_in_triang(p_in, p0, p1, p2)
assert not out_1
assert not out_2
assert in_1
def test_get_bounds():
array = [[1, 1], [2, 3], [4, 5], [5, 7], [3, 2]]
bounds = (1, 5, 1, 7)
result = get_bounds(array)
assert result == bounds
def test_get_bounds_neg():
array = [[-5, -4], [5, -5], [3, 3], [-3, 4]]
bounds = (-5, 5, -5, 4)
result = get_bounds(array)
assert result == bounds
def test_model_inverse_uv():
mesh = load_mesh(parent_dir + "/data/circle.STL")
image = model_inverse_uv(
{"node": mesh.node[:, :2], "element": mesh.element},
(100, 100),
preserve_aspect_ratio=False,
)
image = image.T[
:, ::-1
] # Flip back because this test was created before we corrected the orientation
circle_image = np.load(parent_dir + "/data/circle_image.npy")
assert np.all(image == circle_image)
def test_model_inverse_uv_neg():
mesh = load_mesh(parent_dir + "/data/circle.STL")
mesh.node -= 5
image = model_inverse_uv(
{"node": mesh.node[:, :2], "element": mesh.element},
(100, 100),
preserve_aspect_ratio=False,
)
image = image.T[
:, ::-1
] # Flip back because this test was created before we corrected the orientation
circle_image = np.load(parent_dir + "/data/circle_image.npy")
assert np.all(image == circle_image)
def test_render():
mesh = load_mesh(parent_dir + "/data/L_shape.STL")
image = model_inverse_uv(
{"node": mesh.node[:, :2], "element": mesh.element}, (100, 100)
)
mapped = map_image(image, mesh.perm)
correct_image = np.load(parent_dir + "/data/L_image.npy")
correct_mapped = np.load(parent_dir + "/data/L_mapped.npy")
# fig, axs = plt.subplots(1, 3)
# create_mesh_plot(axs[0], mesh)
# axs[1].imshow(image)
# axs[2].imshow(mapped)
# fig.tight_layout()
# plt.show()
np.testing.assert_array_equal(image, correct_image)
np.testing.assert_array_equal(mapped, correct_mapped)
def test_map_image():
circle_image = np.load(parent_dir + "/data/circle_image.npy")
values = np.array(
[
5.0,
5.0,
10.0,
5.0,
5.0,
5.0,
5.0,
5.0,
5.0,
5.0,
5.0,
5.0,
5.0,
5.0,
5.0,
5.0,
5.0,
5.0,
10,
5.0,
5.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
10.0,
]
)
image = map_image(circle_image, values)
mapped_image = np.load(parent_dir + "/data/circle_image_mapped.npy")
equal = True
for i in range(image.shape[0]):
for j in range(image.shape[1]):
if image[i][j] == mapped_image[i][j] or (
np.isnan(image[i][j]) and np.isnan((mapped_image[i][j]))
):
pass
else:
equal = False
break
assert equal
def test_scale_uv_list():
uv_list = np.array([[1, 1], [2, 1], [2, 4], [1, 4]])
scaled_0 = scale_uv_list(
uv_list,
resolution=[10, 10],
preserve_aspect_ratio=True,
bounds=np.array([[0, 0], [2, 4]]),
)
scaled_1 = scale_uv_list(
uv_list,
resolution=[10, 10],
preserve_aspect_ratio=False,
bounds=np.array([[0, 0], [2, 4]]),
)
scaled_2 = scale_uv_list(
uv_list, resolution=[10, 10], preserve_aspect_ratio=True, bounds=None
)
correct_scaled_0 = np.array([[2.5, 2.5], [5.0, 2.5], [5.0, 10.0], [2.5, 10.0]])
correct_scaled_1 = np.array([[5.0, 2.5], [10.0, 2.5], [10.0, 10.0], [5.0, 10.0]])
correct_scaled_2 = np.array(
[[0.0, 0.0], [3.33333333, 0.0], [3.33333333, 10.0], [0.0, 10.0]]
)
assert np.array_equal(scaled_0, correct_scaled_0)
assert np.array_equal(scaled_1, correct_scaled_1)
assert_almost_equal(scaled_2, correct_scaled_2)
def test_scale_uv_list_neg():
uv_list = np.array([[-1, -1], [0, -1], [0, 2], [-1, 2]])
scaled_0 = scale_uv_list(
uv_list,
resolution=[10, 10],
preserve_aspect_ratio=True,
bounds=np.array([[-2, -2], [0, 2]]),
)
scaled_1 = scale_uv_list(
uv_list,
resolution=[10, 10],
preserve_aspect_ratio=False,
bounds=np.array([[-2, -2], [0, 2]]),
)
scaled_2 = scale_uv_list(
uv_list, resolution=[10, 10], preserve_aspect_ratio=True, bounds=None
)
correct_scaled_0 = np.array([[2.5, 2.5], [5.0, 2.5], [5.0, 10.0], [2.5, 10.0]])
correct_scaled_1 = np.array([[5.0, 2.5], [10.0, 2.5], [10.0, 10.0], [5.0, 10.0]])
correct_scaled_2 = np.array(
[[0.0, 0.0], [3.33333333, 0.0], [3.33333333, 10.0], [0.0, 10.0]]
)
assert np.array_equal(scaled_0, correct_scaled_0)
assert np.array_equal(scaled_1, correct_scaled_1)
assert_almost_equal(scaled_2, correct_scaled_2)