In [1]:
import k3d
import numpy as np

dim = 128
data = np.zeros((dim, dim, dim))

In [2]:
N = 100000
paths = [np.cumsum(np.random.randn(N,3).astype(np.float32), axis=0) for _ in range(3)]

In [3]:
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 [4]:
plot = k3d.plot()
plot.display()



In [5]:
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 [6]:
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 [7]:
for i, path in enumerate(paths):
    plot -= lines[i]

In [8]:
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 [9]:
plot -= dense_voxels
dense_data.nbytes / (1024 ** 2), sparse_data.nbytes / (1024 ** 2)


Out[9]:
(2.0, 0.24796295166015625)

In [10]:
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 [ ]: