In [ ]:
import k3d
import numpy as np

dim = 256
data = np.zeros((dim, dim, dim))
In [ ]:
N = 100000
paths = [np.cumsum(np.random.randn(N,3).astype(np.float32), axis=0) for _ in range(3)]
In [ ]:
for i in range(len(paths)):
    path = paths[i]
    minimum = np.min(path, axis=0)
    maximum = np.max(path, axis=0)
    
    paths[i] = (path - minimum) / np.max(maximum-minimum)
In [ ]:
plot = k3d.plot()
plot.display()
In [ ]:
lines = []
for i, path in enumerate(paths):
    lines.append(k3d.line(100.0 * path, width=0.001, color=k3d.nice_colors[i]))
    plot += lines[i]
In [ ]:
for i, path in enumerate(paths):
    indices = np.fix((dim-1) * path).astype(np.uint16)
    data[(indices[:,2], indices[:,1], indices[:,0])] = i + 1

dense_data = data.astype(np.uint8)
dense_voxels = k3d.voxels(dense_data, bounds=[0, 100, 0, 100, 0, 100], compression_level=1)
plot += dense_voxels
In [ ]:
for i, path in enumerate(paths):
    plot -= lines[i]
In [ ]:
sparse_data = []

for val in np.unique(dense_data):
    if val != 0:
        x, y, z = np.where(dense_data==val)
        sparse_data.append(np.dstack((x, y, z, np.full(x.shape, val))).reshape(-1,4).astype(np.uint16))
    
sparse_data = np.vstack(sparse_data)
In [ ]:
plot -= dense_voxels
dense_data.nbytes / (1024 ** 2), sparse_data.nbytes / (1024 ** 2)
In [ ]:
sparse_voxels = k3d.sparse_voxels(sparse_data, [dim, dim, dim], bounds=[0, 100, 0, 100, 0, 100], compression_level=1)
plot += sparse_voxels

Edit object (add/remove some voxels)

In [ ]:
sparse_voxels.fetch_data('sparse_voxels')
In [ ]:
sparse_voxels.sparse_voxels.shape
In [ ]:
sparse_voxels.sparse_voxels
In [ ]: