forked from pmneila/PyMCubes
-
Notifications
You must be signed in to change notification settings - Fork 16
/
test_smoothing.py
111 lines (77 loc) · 2.73 KB
/
test_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
import pytest
import numpy as np
import marching_cubes as mcubes
def test_sphere():
# Create sphere with radius 25 centered at (50, 50, 50)
x, y, z = np.mgrid[:100, :100, :100]
levelset = np.sqrt((x - 50)**2 + (y - 50)**2 + (z - 50)**2) - 25
# vertices, triangles = mcubes.marching_cubes(levelset, 0)
# mcubes.export_obj(vertices, triangles, 'sphere1.obj')
binary_levelset = levelset > 0
smoothed_levelset = mcubes.smooth(
binary_levelset,
method='constrained',
max_iters=500,
rel_tol=1e-4
)
vertices, _ = mcubes.marching_cubes(smoothed_levelset, 0.0)
# Check all vertices have same distance to (50, 50, 50)
dist = np.sqrt(np.sum((vertices - [50, 50, 50])**2, axis=1))
assert dist.min() > 24.5 and dist.max() < 25.5
assert np.all(np.abs(smoothed_levelset - levelset) < 1)
assert np.all((smoothed_levelset > 0) == binary_levelset)
def test_gaussian_smoothing():
# Create sphere with radius 25 centered at (50, 50, 50)
x, y, z = np.mgrid[:100, :100, :100]
levelset = np.sqrt((x - 50)**2 + (y - 50)**2 + (z - 50)**2) - 25
binary_levelset = levelset > 0
smoothed_levelset = mcubes.smooth(
binary_levelset,
method='gaussian',
sigma=3
)
vertices, _ = mcubes.marching_cubes(smoothed_levelset, 0.0)
# Check all vertices have same distance to (50, 50, 50)
dist = np.sqrt(np.sum((vertices - [50, 50, 50])**2, axis=1))
assert dist.min() > 24 and dist.max() < 25
def test_wrong_ndim():
binary_levelset = np.random.uniform(size=(10)) < 0.5
with pytest.raises(ValueError):
mcubes.smooth(
binary_levelset,
method='constrained',
max_iters=500,
rel_tol=1e-4
)
binary_levelset = np.random.uniform(size=(10, 10, 10, 10)) < 0.5
with pytest.raises(ValueError):
mcubes.smooth(
binary_levelset,
method='constrained',
max_iters=500,
rel_tol=1e-4
)
def test_wrong_method():
with pytest.raises(ValueError):
mcubes.smooth(
np.zeros((10, 10), dtype=np.bool_),
method='wrong',
max_iters=500,
rel_tol=1e-4
)
def test_circle():
x, y = np.mgrid[:100, :100]
levelset = np.sqrt((x - 50)**2 + (y - 50)**2) - 25
binary_levelset = levelset > 0
smoothed_levelset = mcubes.smooth(
binary_levelset,
max_iters=500,
rel_tol=1e-4
)
assert np.all(np.abs(smoothed_levelset - levelset) < 1)
assert np.all((smoothed_levelset > 0) == binary_levelset)
# if __name__ == '__main__':
# # logging.basicConfig(level=logging.DEBUG)
# test_circle()
# test_sphere()
# test_large_sphere()