marching_cubes#

k3d.factory.marching_cubes(scalar_field: List | ndarray | Tuple, level: float, color: int = 255, attribute: List | ndarray | Tuple = None, color_map: List[List[float]] | Dict[str, Any] | ndarray | None = None, color_range: List[float] = None, opacity_function: List[float] = None, wireframe: bool = False, flat_shading: bool = True, roughness: float = 0.4, metalness: float = 0.0, shininess: float = None, opacity: float = 1.0, spacings_x: List | ndarray | Tuple = None, spacings_y: List | ndarray | Tuple = None, spacings_z: List | ndarray | Tuple = None, name: str | None = None, group: str | None = None, custom_data: Dict[str, Any] | None = None, compression_level: int = 0, visible: bool = True, click_callback: Callable | None = None, hover_callback: Callable | None = None, **kwargs: Any) → MarchingCubes[source]#

Create a MarchingCubes drawable, an isosurface of a scalar field.

Parameters:
  • scalar_field (array_like) – 3D array of the scalar field, indexed as [z, y, x]. The surface faces outwards where the field is negative inside it.

  • level (float) – Value at the computed isosurface.

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

  • attribute (array_like, optional) – 3D array of float sampled on the same grid as scalar_field, from which the surface colour is read at each vertex. A flat, per-vertex array is not accepted: it cannot be sampled at a position and is ignored, with a warning in the browser console. Default is None.

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

  • color_range (list, optional) – A pair [min_value, max_value], which determines the levels of color attribute mapped to 0 and 1 in the color map respectively. Default is None.

  • opacity_function (list, optional) – A list of float tuples (attribute value, opacity), sorted by attribute value. The first tuple should have value 0.0, the last 1.0; opacity is in the range 0.0 to 1.0. Default is None.

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

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

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

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

  • shininess (float, optional) – Removed in 3.0.0; passing it raises. Use roughness and metalness. Default is None.

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

  • spacings_x (array_like, optional) – Distances between consecutive samples along x: one shorter than that axis of scalar_field. Any other length is ignored and the axis falls back to even spacing. Default is None.

  • spacings_y (array_like, optional) – Distances between consecutive samples along y, one shorter than that axis. Default is None.

  • spacings_z (array_like, optional) – Distances between consecutive samples along z, one shorter than that axis. 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.

  • click_callback (callable, optional) – Called with the picking parameters when the object is clicked, while the plot is in mode=’callback’. Default is None.

  • hover_callback (callable, optional) – Called with the picking parameters when the cursor is over the object, while the plot is in mode=’callback’. Default is None.

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

Returns:

The created MarchingCubes object.

Return type:

MarchingCubes

Examples#

Sinus cube#

# Example from https://graphics.stanford.edu/~mdfisher/MarchingCubes.html

import k3d
import numpy as np
from numpy import sin

t = np.linspace(-5, 5, 100, dtype=np.float32)
x, y, z = np.meshgrid(t, t, t, indexing='ij')

scalars = sin(x*y + x*z + y*z) + sin(x*y) + sin(y*z) + sin(x*z) - 1

plt_marching = k3d.marching_cubes(scalars, level=0.0,
                                  color=0x0e2763,
                                  opacity=0.25,
                                  xmin=0, xmax=1,
                                  ymin=0, ymax=1,
                                  zmin=0, zmax=1,
                                  compression_level=9,
                                  flat_shading=False)

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

Levels#

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)

plot = k3d.plot()

for i in range(3):
    level = 0 + i * 1.5

    plt_heart = k3d.marching_cubes(eq_heart, level=level,
                                   color=0xe31b23,
                                   xmin=-1.5, xmax=1.5,
                                   ymin=-1.5, ymax=1.5,
                                   zmin=-1.5, zmax=1.5,
                                   translation=[i * 3.5, 0, 0])
    plt_torus = k3d.marching_cubes(eq_torus, level=level,
                                   color=0x5aabac,
                                   xmin=-1.5, xmax=1.5,
                                   ymin=-1.5, ymax=1.5,
                                   zmin=-1.5, zmax=1.5,
                                   translation=[i * 3.5, 0, -3.5])

    plot += plt_heart
    plot += plt_torus

plot.display()