marching_cubes#

k3d.factory.marching_cubes(scalar_field, level, color=255, attribute=[], color_map=None, color_range=[], opacity_function=[], wireframe=False, flat_shading=True, opacity=1.0, spacings_x=[], spacings_y=[], spacings_z=[], name=None, group=None, custom_data=None, compression_level=0, **kwargs)[source]#

Create a MarchingCubes drawable.

Plot an isosurface of a scalar field obtained through Marching cubes algorithm. The default domain of the scalar field is -0.5 < x, y, z < 0.5.

If the domain should be different, the bounding box needs to be transformed using kwargs

  • marching_cubes(..., bounds=[-1, 1, -1, 1, -1, 1])

  • marching_cubes(..., xmin=-10, xmax=10, ymin=-4, ymax=4, zmin=0, zmax=20)

  • marching_cubes(..., scaling=[width, height, length])

Parameters
  • scalar_field (array_like) – 3D scalar field of values.

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

  • color (int, optional) – Hex color of the isosurface, by default _default_color.

  • attribute (list, optional) – List of values used to apply color_map, by default [].

  • color_map (list, optional) – List of float quadruplets (attribute value, R, G, B) sorted by attribute value, by default None. 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.

  • color_range (list, optional) – [min_value, max_value] pair determining the levels of color attribute mapped to 0 and 1 in the colormap, by default [].

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

  • wireframe (bool, optional) – Display the mesh as wireframe, by default False.

  • flat_shading (bool, optional) – Display the mesh with flat shading, by default True.

  • opacity (float, optional) – Opacity of the mesh, by default 1.0.

  • spacings_x (list, optional) – Spacings in x axis, by default []. Should match scalar_field shape.

  • spacings_y (list, optional) – Spacings in y axis, by default []. Should match scalar_field shape.

  • spacings_z (list, optional) – Spacings in z axis, by default []. Should match scalar_field shape.

  • 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

MarchingCubes Drawable.

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()
xx
yy
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
zz
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
yy
0.10.1
0.20.2
0.30.3
0.40.4
0.50.5
0.60.6
0.70.7
0.80.8
0.90.9
xx
K3D panel
Full screen
cameraAutoFit
gridAutoFit
gridVisible
fpsMeter
Mode
View
Camera
Trackball
FOV
voxelColor
lighting
color
flat_shading
level
opacity
visible
wireframe
Js version:

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()
xx
yy
zz
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
yy
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
0.5-0.5
0.50.5
yy
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
yy
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
0.5-0.5
0.50.5
yy
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
yy
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
0.5-0.5
0.50.5
yy
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
yy
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
0.5-0.5
0.50.5
yy
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
yy
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
0.5-0.5
0.50.5
yy
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
yy
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
0.5-0.5
0.50.5
yy
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
yy
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
0.5-0.5
0.50.5
yy
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
yy
4-4
3-3
2-2
1-1
00
zz
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
0.5-0.5
0.50.5
yy
0.5-0.5
0.50.5
1.51.5
2.52.5
3.53.5
4.54.5
5.55.5
6.56.5
7.57.5
xx
K3D panel
Full screen
cameraAutoFit
gridAutoFit
gridVisible
fpsMeter
Mode
View
Camera
Trackball
FOV
voxelColor
lighting
color
flat_shading
level
opacity
visible
wireframe
color
flat_shading
level
opacity
visible
wireframe
color
flat_shading
level
opacity
visible
wireframe
color
flat_shading
level
opacity
visible
wireframe
color
flat_shading
level
opacity
visible
wireframe
color
flat_shading
level
opacity
visible
wireframe
Js version: