voxels#

k3d.factory.voxels(voxels, 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 Voxels drawable for 3D volumetric data.

By default, the voxels are a grid inscribed in the -0.5 < x, y, z < 0.5 cube regardless of the passed voxel array shape, like aspect ratio.

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

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

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

Parameters
  • voxels (array_like) – 3D array of int from 0 to 255. 0 means empty voxel; 1 and above refer to consecutive color_map.

  • 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

Voxels Drawable.

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()