voxels#

k3d.factory.voxels(voxels: 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) → Voxels[source]#

Create a Voxels drawable from a dense array of voxel ids.

Parameters:
  • voxels (array_like) – 3D array of int in range (0, 255), indexed as [z, y, x]. 0 means empty voxel, 1 and above refer to consecutive color_map entries.

  • 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 Voxels object.

Return type:

Voxels

Examples#

Basic#

import k3d
import numpy as np

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

plt_voxels = k3d.voxels(voxels,
                        color_map=[0xfdc192, 0xa15525],
                        outlines_color=0xffffff)

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

Shapes#

import k3d
import numpy as np

t = np.linspace(-1.5, 1.5, 50, dtype=np.float32)
x, y, z = np.meshgrid(t, t, t, indexing='ij')
R = 1
r = 0.5

eq_heart = (x**2 + (9/4 * y**2) + z**2 - 1)**3 - (x**2 * z**3) - (9/200 * y**2 * z**3)
eq_torus = (x**2 + y**2 + z**2 + R**2 - r**2)**2 - 4 * R**2 * (x**2 + y**2)

voxels_heart = np.zeros_like(eq_heart).astype(np.uint8)
voxels_torus = np.zeros_like(eq_torus).astype(np.uint8)
voxels_heart[eq_heart < 0] = 1
voxels_torus[eq_torus > 0] = 1

plt_voxels_heart = k3d.voxels(voxels_heart,
                              color_map=[0xbc4749],
                              outlines=False,
                              bounds=[-1.5, 1.5,-1.5, 1.5,-1.5, 1.5])

plt_voxels_torus = k3d.voxels(voxels_torus,
                              color_map=[0x3b60e4],
                              outlines=False,
                              wireframe=True,
                              bounds=[-1.5, 1.5,-1.5, 1.5,-1.5, 1.5],
                              translation=[0, 0, -3.5])

plot = k3d.plot()
plot += plt_voxels_heart
plot += plt_voxels_torus
plot.display()