Marching Cubes Algorithm
Marching cubes is one of the most widely used algorithms for isosurface extraction, producing a triangular mesh approximation of the isosurface 1.
In crater.rs
, the algorithm is implemented with GPU acceleration using the Burn tensor computation library, making it highly efficient even for large grids.
Overview
The marching cubes algorithm converts implicit surfaces (represented by scalar fields) into explicit triangular meshes. This is essential for visualization, 3D printing, and further geometric processing.
Why Marching Cubes?
- Universal: Works with any scalar field function
- Robust: Handles complex topologies automatically
- Efficient: GPU-accelerated implementation
- Widely Supported: Standard mesh format output (STL, VTK)
Core Algorithm Steps
- Grid Construction: Create a voxel grid with vertices within a bounding box
- Field Evaluation: Evaluate the scalar field at each grid vertex: for all vertices
- Voxel Processing: For each voxel in the grid:
- Determine which of the 8 corners are inside or outside the isosurface
- Compute the 8-bit cube index (0-255) based on corner classifications. This can be done with a tensor convolution over reshaped to with a kernel of shape and stride 1.
- If cube index indicates the isosurface intersects the voxel:
- Lookup triangle configuration in edge table
- For each edge intersected by the isosurface:
- Interpolate vertices to find the exact intersection point
- Construct triangles using the interpolated intersection points
- Mesh Assembly: Collect all triangles into a single mesh
Mathematical Foundation
Cube Classification
Each voxel has 8 corners, labeled 0-7. For each corner , we evaluate:
- If : corner is inside (bit = 1)
- If : corner is outside (bit = 0)
The cube index is computed as:
This gives 256 possible configurations (0-255).
Edge Interpolation
When the isosurface crosses an edge between vertices and with field values and , the intersection point is:
where the interpolation parameter is:
This assumes linear interpolation along the edge.