sparse_voxels#

k3d.factory.sparse_voxels(sparse_voxels: List | ndarray | Tuple, space_size: List | ndarray | Tuple, color_map: List[List[float]] | Dict[str, Any] | ndarray | None = None, wireframe: bool = False, outlines: bool = True, outlines_color: int = 0, opacity: float = 1.0, bounds: List | ndarray | Tuple | None = None, name: str | None = None, group: str | None = None, custom_data: Dict[str, Any] | None = None, compression_level: int = 0, **kwargs: Any) SparseVoxels[source]#

See also

Examples#

Basic#

import k3d
import numpy as np

sparse_voxels = np.array([[1, 0, 0, 1],
                          [0, 1, 0, 1],
                          [0, 1, 1, 1],
                          [1, 1, 1, 1],
                          [1, 1, 0, 2],
                          [0, 0, 1, 2],
                          [1, 0, 1, 2]]).astype(np.uint16)

plt_sparse_voxels = k3d.sparse_voxels(sparse_voxels,
                                      space_size=[2, 2, 2],
                                      color_map=[0xfdc192, 0xa15525],
                                      outlines_color=0xffffff)

plot = k3d.plot()
plot += plt_sparse_voxels
plot.display()

Voxels to sparse voxels#

import numpy as np

voxels = np.array([[[0, 1],
                    [1, 2]],
                   [[2, 2],
                    [1, 1]]])

sparse_data = []

for val in np.unique(voxels):
        if val != 0:
            z, y, x = np.where(voxels==val)
            sparse_data.append(np.dstack((x, y, z, np.full(x.shape, val))).reshape(-1,4).astype(np.uint16))

sparse_voxels = np.vstack(sparse_data)

"""
array([[1 0 0 1]
       [0 1 0 1]
       [0 1 1 1]
       [1 1 1 1]
       [1 1 0 2]
       [0 0 1 2]
       [1 0 1 2]])
"""