-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_library_smoothing.py
executable file
·387 lines (318 loc) · 12.4 KB
/
test_library_smoothing.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
386
"""
Collection of tests for the diffusion_smoothing library.
Author: Virgile Fritsch, 2010
"""
import numpy as np
import library_smoothing as smooth
# -----------------------------------------------------------
# --------- Test compute_areas_and_cotangentes --------------
# -----------------------------------------------------------
def test1_1():
"""
This test function builds a triangle with the points
- (0, 0, 0)
- (0, 1, 0)
- (1, 0, 0)
and then has its area and cotangentes values computed by
the smoothing library.
Results should be :
- area = 0.5
- cotan(A) = 0, cotan(B) = 1, cotan(C) = 1
"""
vertices = np.array([[0.,0.,0.], [0., 1., 0.], [1., 0., 0.]])
polygons = np.array([[0, 1, 2]])
areas, cotangentes = smooth.compute_areas_and_cotangentes(polygons, vertices)
assert (areas.shape == (1,) and areas[0] == 0.5)
assert (cotangentes.shape == (3,1) and \
np.all(np.equal(cotangentes, np.array([[0.], [1.], [1.]]))))
def test1_2():
"""
This test function builds two triangles, respectively with the points
- (0, 0, 0), (0, 1, 0), (1, 0, 0)
- (0, 1, 0), (1, 0, 0), (0, 0, 1)
and then has their areas and cotangentes values computed by
the smoothing library.
Results should be :
- areas = (0.5, sqrt(3)/2)
- cotan(A) = (0, 1/sqrt(3)),
cotan(B) = (1, 1/sqrt(3)),
cotan(C) = (1, 1/sqrt(3))
"""
vertices = np.array([[0.,0.,0.], [0., 1., 0.], [1., 0., 0.], [0., 0., 1.]])
polygons = np.array([[0, 1, 2], [1, 2, 3]])
areas, cotangentes = smooth.compute_areas_and_cotangentes(polygons, vertices)
assert (areas.shape == (2,))
assert (np.all(np.equal(areas, np.array([0.5, np.sqrt(3)/2]))))
assert (cotangentes.shape == (3,2))
assert (np.all(np.equal(cotangentes, np.array([[0., 1/np.sqrt(3)],
[1., 1/np.sqrt(3)],
[1., 1/np.sqrt(3)]]))))
# -----------------------------------------------------------
# --------- Test compute_weights_matrix ---------------------
# -----------------------------------------------------------
def test2_1():
"""
This functions builds a planar mesh and checks that the weights of the
middle vertex are correct. Here is the mesh (with the non-zero weights):
________________
|\ | /|
| \ 0.5 / |
| \ | / |
|_0.5__\|/__0.5_|
| /|\ |
| / | \ |
| / 0.5 \ |
|/______|______\|
"""
# create vertices
x = np.array([0., 1., 2.])
x = np.repeat(x, 3)
y = np.array([0., 1., 2.])
y = np.tile(y ,3)
z = np.zeros(9)
vertices = np.vstack((x, y, z)).T
# create polygons
polygons = np.array([[0, 3, 4], [0, 4, 1], [1, 4, 2], [2, 4, 5],
[3, 6, 4], [4, 6, 7], [4, 7, 8], [4, 8, 5]])
# get edges
nb_edges = 3 * polygons.shape[0]
edges = np.zeros((nb_edges, 2))
# get the polygons edges as tuples
permutator = np.array([(0,0,1),(1,0,0),(0,1,0)], dtype=int)
edges[:,0] = np.ravel(polygons)
edges[:,1] = np.ravel(np.dot(polygons, permutator))
ind = np.lexsort((edges[:,1], edges[:,0]))
edges = edges[ind]
weights_matrix = smooth.compute_weights_matrix(polygons, vertices, edges)
assert (weights_matrix[4,0] == 0. and weights_matrix[4,2] == 0. and
weights_matrix[4,6] == 0. and weights_matrix[4,8] == 0.)
assert (weights_matrix[4,1] == 0.5 and weights_matrix[4,3] == 0.5 and
weights_matrix[4,5] == 0.5 and weights_matrix[4,7] == 0.5)
def test2_2():
"""
This functions builds a planar mesh and checks that the weights of the
middle vertex are correct. Here is the mesh:
\\
|\ \
| \ \
| \ \_________
| -0.22 | /|
| \ 0.88 / |
| \ | / |
|_0.66_\|/__0.44|
| /|\ |
| / | \ |
| / 0.44 \ |
|/______|______\|
"""
# create vertices
x = np.array([0., 1., 2.])
x = np.repeat(x, 3)
y = np.array([0., 1., 2.])
y = np.tile(y ,3)
y[2] = 3.
z = np.zeros(9)
vertices = np.vstack((x, y, z)).T
# create polygons
polygons = np.array([[0, 3, 4], [0, 4, 1], [1, 4, 2], [2, 4, 5],
[3, 6, 4], [4, 6, 7], [4, 7, 8], [4, 8, 5]])
# get edges
nb_edges = 3 * polygons.shape[0]
edges = np.zeros((nb_edges, 2))
# get the polygons edges as tuples
permutator = np.array([(0,0,1),(1,0,0),(0,1,0)], dtype=int)
edges[:,0] = np.ravel(polygons)
edges[:,1] = np.ravel(np.dot(polygons, permutator))
ind = np.lexsort((edges[:,1], edges[:,0]))
edges = edges[ind]
weights_matrix = smooth.compute_weights_matrix(polygons, vertices, edges)
assert (weights_matrix[4,0] == 0. and
weights_matrix[4,2] == -0.22222222222222221 and
weights_matrix[4,6] == 0. and weights_matrix[4,8] == 0.)
assert (weights_matrix[4,1] == 0.66666666666666663 and
weights_matrix[4,3] == 4./9. and
weights_matrix[4,5] == 0.88888888888888884 and
weights_matrix[4,7] == 4./9.)
# -----------------------------------------------------------
# --------- Test compute_weights_matrix_biased --------------
# -----------------------------------------------------------
# /!\ Warning: This function is for debug purpose only /!\
def test3_1():
"""
This functions builds a planar mesh and checks that the weights of the
middle vertex are correct. Here is the mesh (with the non-zero weights):
________________
|\ | /|
| \ 0.5 / |
| \ | / |
|_0.5__\|/__0.5_|
| /|\ |
| / | \ |
| / 0.5 \ |
|/______|______\|
"""
# create vertices
x = np.array([0., 1., 2.])
x = np.repeat(x, 3)
y = np.array([0., 1., 2.])
y = np.tile(y ,3)
z = np.zeros(9)
vertices = np.vstack((x, y, z)).T
# create polygons
polygons = np.array([[0, 3, 4], [0, 4, 1], [1, 4, 2], [2, 4, 5],
[3, 6, 4], [4, 6, 7], [4, 7, 8], [4, 8, 5]])
# get edges
nb_edges = 3 * polygons.shape[0]
edges = np.zeros((nb_edges, 2))
# get the polygons edges as tuples
permutator = np.array([(0,0,1),(1,0,0),(0,1,0)], dtype=int)
edges[:,0] = np.ravel(polygons)
edges[:,1] = np.ravel(np.dot(polygons, permutator))
ind = np.lexsort((edges[:,1], edges[:,0]))
edges = edges[ind]
weights_matrix = smooth.compute_weights_matrix_biased(polygons,
vertices, edges)
assert (weights_matrix[4,0] == 0. and weights_matrix[4,2] == 0. and
weights_matrix[4,6] == 0. and weights_matrix[4,8] == 0.)
assert (weights_matrix[4,1] == 0.5 and weights_matrix[4,3] == 0.5 and
weights_matrix[4,5] == 0.5 and weights_matrix[4,7] == 0.5)
def test3_2():
"""
This functions builds a planar mesh and checks that the weights of the
middle vertex are correct. Here is the mesh:
\\
|\ \
| \ \
| \ \_________
| 0 | /|
| \ 0.88 / |
| \ | / |
|_0.66_\|/_0.44_|
| /|\ |
| / | \ |
| / 0.44 \ |
|/______|______\|
"""
# create vertices
x = np.array([0., 1., 2.])
x = np.repeat(x, 3)
y = np.array([0., 1., 2.])
y = np.tile(y ,3)
y[2] = 3.
z = np.zeros(9)
vertices = np.vstack((x, y, z)).T
# create polygons
polygons = np.array([[0, 3, 4], [0, 4, 1], [1, 4, 2], [2, 4, 5],
[3, 6, 4], [4, 6, 7], [4, 7, 8], [4, 8, 5]])
# get edges
nb_edges = 3 * polygons.shape[0]
edges = np.zeros((nb_edges, 2))
# get the polygons edges as tuples
permutator = np.array([(0,0,1),(1,0,0),(0,1,0)], dtype=int)
edges[:,0] = np.ravel(polygons)
edges[:,1] = np.ravel(np.dot(polygons, permutator))
ind = np.lexsort((edges[:,1], edges[:,0]))
edges = edges[ind]
weights_matrix = smooth.compute_weights_matrix_biased(polygons,
vertices, edges)
assert (weights_matrix[4,0] == 0. and
weights_matrix[4,2] == 0. and
weights_matrix[4,6] == 0. and weights_matrix[4,8] == 0.)
assert (weights_matrix[4,1] == 0.66666666666666663 and
weights_matrix[4,3] == 4./9. and
weights_matrix[4,5] == 0.88888888888888884 and
weights_matrix[4,7] == 4./9.)
# -----------------------------------------------------------
# --------- Test define_LB_operator -------------------------
# -----------------------------------------------------------
def test4_1():
"""
This functions builds a planar mesh and checks that the weights of the
middle vertex is correct. Here is the mesh (with the non-zero weights):
________________
|\ | /|
| \ 0.5 / |
| \ | / |
|_0.5__\|/__0.5_| sum(wi) = 2
| /|\ |
| / | \ |
| / 0.5 \ |
|/______|______\|
It also checks that the Laplace-Beltrami operator is correctly defined
"""
# create vertices
x = np.array([0., 1., 2.])
x = np.repeat(x, 3)
y = np.array([0., 1., 2.])
y = np.tile(y ,3)
z = np.zeros(9)
vertices = np.vstack((x, y, z)).T
# create polygons
polygons = np.array([[0, 3, 4], [0, 4, 1], [1, 4, 2], [2, 4, 5],
[3, 6, 4], [4, 6, 7], [4, 7, 8], [4, 8, 5]])
# get edges
nb_edges = 3 * polygons.shape[0]
edges = np.zeros((nb_edges, 2))
# get the polygons edges as tuples
permutator = np.array([(0,0,1),(1,0,0),(0,1,0)], dtype=int)
edges[:,0] = np.ravel(polygons)
edges[:,1] = np.ravel(np.dot(polygons, permutator))
ind = np.lexsort((edges[:,1], edges[:,0]))
edges = edges[ind]
weights_matrix = smooth.compute_weights_matrix(polygons, vertices, edges)
LB_operator = smooth.define_LB_operator(weights_matrix)
assert (LB_operator[4,0] == 0. and LB_operator[4,2] == 0. and
LB_operator[4,6] == 0. and LB_operator[4,8] == 0.)
assert (LB_operator[4,1] == 0.5 and LB_operator[4,3] == 0.5 and
LB_operator[4,5] == 0.5 and LB_operator[4,7] == 0.5)
assert (LB_operator[4,4] == -2.)
# -----------------------------------------------------------
# --------- Test compute_smoothing_parameters ---------------
# -----------------------------------------------------------
def test5_1():
"""
This functions builds a planar mesh and compute the associated
weights matrix:
________________
|\ | /|
| \ 0.5 / |
| \ | / |
|_0.5__\|/__0.5_|
| /|\ |
| / | \ |
| / 0.5 \ |
|/______|______\|
Then, the smoothing parameters are computed according to the given FWHM.
The function checks that the computed parameters are correct.
"""
# create vertices
x = np.array([0., 1., 2.])
x = np.repeat(x, 3)
y = np.array([0., 1., 2.])
y = np.tile(y ,3)
z = np.zeros(9)
vertices = np.vstack((x, y, z)).T
# create polygons
polygons = np.array([[0, 3, 4], [0, 4, 1], [1, 4, 2], [2, 4, 5],
[3, 6, 4], [4, 6, 7], [4, 7, 8], [4, 8, 5]])
# get edges
nb_edges = 3 * polygons.shape[0]
edges = np.zeros((nb_edges, 2))
# get the polygons edges as tuples
permutator = np.array([(0,0,1),(1,0,0),(0,1,0)], dtype=int)
edges[:,0] = np.ravel(polygons)
edges[:,1] = np.ravel(np.dot(polygons, permutator))
ind = np.lexsort((edges[:,1], edges[:,0]))
edges = edges[ind]
weights_matrix = smooth.compute_weights_matrix(polygons, vertices, edges)
dt_max = 1/np.amax(np.ravel(weights_matrix.sum(1)))
dt_max /= 2
for FWHM in np.arange(1, 20):
N, dt = smooth.compute_smoothing_parameters(weights_matrix, FWHM)
assert (FWHM == 4 * np.sqrt(np.log(2) * N * dt))
N, dt = smooth.compute_smoothing_parameters(weights_matrix,
FWHM, dt_max)
assert (FWHM == 4 * np.sqrt(np.log(2) * N * dt))
if __name__ == "__main__":
import nose
nose.run(argv=['', __file__])