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, roughness: float = 0.4, metalness: float = 0.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, visible: bool = True, **kwargs: Any) → SparseVoxels[source]#

Create a SparseVoxels drawable from a list of voxel coordinates and ids.

Parameters:
  • sparse_voxels (array_like) – 2D array of coords in format [[x,y,z,v],[x,y,z,v]]. v = 0 means empty voxel, 1 and above refer to consecutive color_map entries.

  • space_size (array_like) – Width, height and length of the voxel space, in voxels.

  • color_map (list, optional) – A list of float quadruplets (attribute value, R, G, B), sorted by attribute value. The first quadruplet should have value 0.0, the last 1.0; R, G, B are RGB color components in the range 0.0 to 1.0. Default is None.

  • wireframe (bool, optional) – Whether mesh should display as wireframe. Default is False.

  • outlines (bool, optional) – Whether mesh should display with outlines. Default is True.

  • outlines_color (int, optional) – Packed RGB color of the resulting outlines (0xff0000 is red, 0xff is blue) Default is 0.

  • opacity (float, optional) – Opacity of voxels. Default is 1.0.

  • roughness (float, optional) – Roughness of the material. Default is 0.4.

  • metalness (float, optional) – Metalness of the material. Default is 0.0.

  • bounds (array_like, optional) – Bounding box [xmin, xmax, ymin, ymax, zmin, zmax] the object is scaled into; derived from the data when omitted. Default is None.

  • name (str, optional) – A name of the object. Default is None.

  • group (str, optional) – A name of a group. Default is None.

  • custom_data (dict, optional) – An object with custom data attached to object. Default is None.

  • compression_level (int, optional) – Level of compression [-1, 9]. Default is 0.

  • visible (bool, optional) – Whether the object is drawn. Default is True.

  • **kwargs – Additional keyword arguments passed to process_transform_arguments.

Returns:

The created SparseVoxels object.

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