def test_marching_cubes(dtype, value, smooth): """Test creating surfaces via marching cubes.""" data = np.zeros((50, 50, 50), dtype=dtype) data[20:30, 20:30, 20:30] = value level = [value] out = _marching_cubes(data, level, smooth=smooth) assert len(out) == 1 verts, triangles = out[0] # verts and faces are rather large so use checksum rtol = 1e-2 if smooth else 1e-9 assert_allclose(verts.sum(axis=0), [14700, 14700, 14700], rtol=rtol) assert_allclose(triangles.sum(axis=0), [363402, 360865, 350588]) # problematic values with pytest.raises(TypeError, match='1D array-like'): _marching_cubes(data, ['foo']) with pytest.raises(TypeError, match='1D array-like'): _marching_cubes(data, [[1]]) with pytest.raises(TypeError, match='1D array-like'): _marching_cubes(data, [1.]) with pytest.raises(ValueError, match='must be between 0'): _marching_cubes(data, [1], smooth=1.) with pytest.raises(ValueError, match='3D data'): _marching_cubes(data[0], [1])
def test_marching_cubes(dtype, value, smooth): """Test creating surfaces via marching cubes.""" pytest.importorskip('pyvista') data = np.zeros((50, 50, 50), dtype=dtype) data[20:30, 20:30, 20:30] = value level = [value] out = _marching_cubes(data, level, smooth=smooth) assert len(out) == 1 verts, triangles = out[0] # verts and faces are rather large so use checksum rtol = 1e-2 if smooth else 1e-9 assert_allclose(verts.sum(axis=0), [14700, 14700, 14700], rtol=rtol) assert_allclose(triangles.sum(axis=0), [363402, 360865, 350588]) # test fill holes data[24:27, 24:27, 24:27] = 0 verts, triangles = _marching_cubes(data, level, smooth=smooth, fill_hole_size=2)[0] # check that no surfaces in the middle assert np.linalg.norm(verts - np.array([25, 25, 25]), axis=1).min() > 4 # problematic values with pytest.raises(TypeError, match='1D array-like'): _marching_cubes(data, ['foo']) with pytest.raises(TypeError, match='1D array-like'): _marching_cubes(data, [[1]]) with pytest.raises(TypeError, match='1D array-like'): _marching_cubes(data, [1.]) with pytest.raises(ValueError, match='must be between 0'): _marching_cubes(data, [1], smooth=1.) with pytest.raises(ValueError, match='3D data'): _marching_cubes(data[0], [1])