@@ -22,6 +22,69 @@ def test_marching_cubes_smoothing(self):
2222 self .assertGreater (len (faces ), 0 )
2323 self .assertGreater (len (normals ), 0 )
2424
25+ def test_marching_cubes_resolution (self ):
26+ from elf .mesh import marching_cubes
27+ shape = (32 ,) * 3
28+ rng = np .random .default_rng (0 )
29+ seg = (rng .random (shape ) > 0.8 ).astype ("uint32" )
30+
31+ verts_unit , _ , _ = marching_cubes (seg , resolution = (1.0 , 1.0 , 1.0 ))
32+ resolution = (2.0 , 1.0 , 0.5 )
33+ verts_scaled , _ , _ = marching_cubes (seg , resolution = resolution )
34+
35+ self .assertEqual (verts_unit .shape , verts_scaled .shape )
36+ expected = verts_unit * np .array (resolution )
37+ self .assertTrue (np .allclose (verts_scaled , expected ))
38+
39+ def test_smooth_mesh_tetrahedron (self ):
40+ from elf .mesh .mesh import smooth_mesh
41+
42+ verts = np .array (
43+ [[0.0 , 0.0 , 0.0 ], [1.0 , 0.0 , 0.0 ], [0.0 , 1.0 , 0.0 ], [0.0 , 0.0 , 1.0 ]],
44+ dtype = np .float64 ,
45+ )
46+ normals = verts + 0.5
47+ faces = np .array (
48+ [[0 , 1 , 2 ], [0 , 1 , 3 ], [0 , 2 , 3 ], [1 , 2 , 3 ]], dtype = np .int64
49+ )
50+
51+ out_verts , out_normals = smooth_mesh (verts , normals , faces , iterations = 1 )
52+
53+ self .assertEqual (out_verts .shape , verts .shape )
54+ self .assertEqual (out_normals .shape , normals .shape )
55+ self .assertEqual (out_verts .dtype , verts .dtype )
56+ self .assertEqual (out_normals .dtype , normals .dtype )
57+
58+ # Every vertex of a tetrahedron is connected to every other vertex,
59+ # so one Jacobi-Laplacian iteration collapses each vertex onto the
60+ # mean of all four vertices (the centroid).
61+ centroid_verts = verts .mean (axis = 0 )
62+ centroid_normals = normals .mean (axis = 0 )
63+ for i in range (len (verts )):
64+ self .assertTrue (np .allclose (out_verts [i ], centroid_verts ))
65+ self .assertTrue (np .allclose (out_normals [i ], centroid_normals ))
66+
67+ def test_smooth_mesh_no_op (self ):
68+ from elf .mesh .mesh import smooth_mesh
69+
70+ verts = np .array (
71+ [[0.0 , 0.0 , 0.0 ], [1.0 , 0.0 , 0.0 ], [0.0 , 1.0 , 0.0 ], [0.0 , 0.0 , 1.0 ]],
72+ dtype = np .float64 ,
73+ )
74+ normals = verts + 0.5
75+ faces = np .array (
76+ [[0 , 1 , 2 ], [0 , 1 , 3 ], [0 , 2 , 3 ], [1 , 2 , 3 ]], dtype = np .int64
77+ )
78+
79+ out_verts , out_normals = smooth_mesh (verts , normals , faces , iterations = 0 )
80+
81+ self .assertEqual (out_verts .shape , verts .shape )
82+ self .assertEqual (out_normals .shape , normals .shape )
83+ self .assertEqual (out_verts .dtype , verts .dtype )
84+ self .assertEqual (out_normals .dtype , normals .dtype )
85+ self .assertTrue (np .array_equal (out_verts , verts ))
86+ self .assertTrue (np .array_equal (out_normals , normals ))
87+
2588
2689if __name__ == '__main__' :
2790 unittest .main ()
0 commit comments