sparse_voxels#

k3d.factory.sparse_voxels(sparse_voxels, space_size, color_map=None, wireframe=False, outlines=True, outlines_color=0, opacity=1.0, bounds=None, name=None, group=None, custom_data=None, compression_level=0, **kwargs)[source]#

Create a SparseVoxels drawable for 3D volumetric data.

Different grid size, shape and rotation can be obtained using kwargs

  • sparse_voxels(..., bounds=[0, 300, 0, 400, 0, 500])

  • sparse_voxels(..., scaling=[scale_x, scale_y, scale_z])

Parameters
  • sparse_voxels (array_like) – 2D array of cordinates [x, y, z, v], x, y, z >= 0 and 0<= v <= 255. v = 0 means empty voxel; v >= 1 refer to consecutive color_map.

  • space_size (array_like) – Width, Height and Length of space.

  • color_map (int, optional) – List of Hex color, by default None. The color defined at index i is for voxel value (i+1).

  • wireframe (bool, optional) – Display voxels as wireframe, by default False.

  • outlines (bool, optional) – Display voxels outlines, by default True.

  • outlines_color (int, optional) – Hex color of voxels outlines, by default 0.

  • opacity (float, optional) – Opacity of voxels, by default 1.0.

  • name (str, optional) – Object name, by default None.

  • group (str, optional) – Name of a group, by default None.

  • custom_data (dict) – A object with custom data attached to object.

  • compression_level (int, optional) – Level of data compression [-1, 9], by default 0.

  • **kwargs – For other keyword-only arguments, see process_transform_arguments.

Returns

SparseVoxels Drawable.

Return type

SparseVoxels

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]])
"""