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, shininess: float = 50.0, 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, **kwargs: Any) MarchingCubes[source]#

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