warp.MarchingCubes#
- class warp.MarchingCubes(*args, **kwargs)[source]#
A reusable context for marching cubes surface extraction.
This class provides a stateful interface for isosurface extraction. You can initialize it with a specific grid configuration and then call the
surface()method multiple times, which is efficient for processing fields of the same size.For a simpler, stateless operation, use the static method
extract_surface_marching_cubes().- domain_bounds_lower_corner#
The lower bound for the mesh coordinate scaling. See the documentation in
extract_surface_marching_cubes()for more details.- Type:
warp.vec3f | tuple | None
- domain_bounds_upper_corner#
The upper bound for the mesh coordinate scaling. See the documentation in
extract_surface_marching_cubes()for more details.- Type:
warp.vec3f | tuple | None
- verts#
An array of vertex positions of type
warp.vec3ffor the output mesh. This is populated by calling thesurface()method.- Type:
warp.array | None
- indices#
An array of triangle indices of type
warp.int32for the output mesh. This is populated by calling thesurface()method.- Type:
warp.array | None
- device#
The device on which the context was created. This attribute is for backward compatibility and is not used by the class’s methods.
- Type:
- __init__(
- nx,
- ny,
- nz,
- max_verts=0,
- max_tris=0,
- device=None,
- domain_bounds_lower_corner=None,
- domain_bounds_upper_corner=None,
Initialize the marching cubes context with a grid configuration.
- Parameters:
nx (int) – Number of grid nodes in the x-direction.
ny (int) – Number of grid nodes in the y-direction.
nz (int) – Number of grid nodes in the z-direction.
max_verts (int) – (Deprecated) This argument is ignored.
max_tris (int) – (Deprecated) This argument is ignored.
device (Device | str | None) – (Deprecated) The value is assigned to
self.devicefor backward compatibility but is not used by the class’s methods. It may be removed in a future version.domain_bounds_lower_corner – See the documentation in
extract_surface_marching_cubes().domain_bounds_upper_corner – See the documentation in
extract_surface_marching_cubes().
Methods
__init__(nx, ny, nz[, max_verts, max_tris, ...])Initialize the marching cubes context with a grid configuration.
extract_surface_marching_cubes(field[, ...])Extract a triangular mesh from a 3D scalar field.
resize(nx, ny, nz[, max_verts, max_tris])Update the grid dimensions for the context.
surface(field, threshold)Compute a 2D surface mesh of a given isosurface from a 3D scalar field.
- resize(nx, ny, nz, max_verts=0, max_tris=0)[source]#
Update the grid dimensions for the context.
This allows the instance to be reused for scalar fields of a different resolution. The new dimensions take effect on the next call to
surface().
- surface(field, threshold)[source]#
Compute a 2D surface mesh of a given isosurface from a 3D scalar field.
This method is a convenience wrapper that calls the core static method and stores the resulting mesh data in the
vertsandindicesattributes.- Parameters:
- Raises:
ValueError – If the shape of
fielddoes not match the configured grid dimensions of the instance.- Return type:
None
- static extract_surface_marching_cubes(
- field,
- threshold=0.0,
- domain_bounds_lower_corner=None,
- domain_bounds_upper_corner=None,
Extract a triangular mesh from a 3D scalar field.
This function generates an isosurface by processing the entire input
field. The resolution of the output mesh is determined by the shape of thefieldarray and may differ along each dimension.The coordinates of the mesh can be scaled to a specific bounding box using the
domain_bounds_lower_corneranddomain_bounds_upper_cornerparameters. If a bound is not provided (i.e., left asNone), it will be assigned a default value that aligns the mesh with the integer indices of the input grid.For example, setting the bounds to
wp.vec3(0.0, 0.0, 0.0)andwp.vec3(1.0, 1.0, 1.0)will scale the output mesh to fit within the unit cube.- Parameters:
field (array(ndim=3, dtype=float32)) – A 3D array representing the scalar values on a regular grid.
threshold (float) – The field value defining the isosurface to extract.
domain_bounds_lower_corner (vec3f | tuple[float, float, float] | None) – The 3D coordinate that the grid’s corner at index (0,0,0) maps to. Defaults to
(0.0, 0.0, 0.0)ifNone.domain_bounds_upper_corner (vec3f | tuple[float, float, float] | None) – The 3D coordinate that the grid’s corner at index (nx-1, ny-1, nz-1) maps to. Defaults to align with the grid’s maximal indices if
None.
- Returns:
A tuple
(vertices, indices)containing the output mesh data. Theindicesarray is a flat list where each group of three consecutive integers forms a single triangle by referencing vertices in theverticesarray.- Raises:
ValueError – If
fieldis not a 3D array or is empty.TypeError – If the
fielddata type is notwp.float32.
- Return type:
tuple[array(ndim=1, dtype=vec3f), array(ndim=1, dtype=int32)]