v9.1.1
Menu

Marching Cubes SDF to Mesh

mediumPROSurface RepresentationsCVCV: 3D Reconstruction

Implement a method to extract a mesh surface from a signed distance field (SDF) using the Marching Cubes algorithm. This technique is crucial in 3D reconstruction for converting implicit surface representations into explicit mesh models.

The SDF is a scalar field where each point's value represents its distance to the surface, with d(x)d(x) being negative if xx is inside the surface, positive if outside, and zero if on the surface. The Marching Cubes algorithm works by discretizing the 3D space into cubes and checking the sign of the SDF values at each cube's vertices to determine the surface intersection.

Here are the key steps:

  1. Discretize the 3D space into cubes.
  2. Evaluate the SDF at each cube's vertices.
  3. Determine the surface intersection based on the SDF values.
d(x)={<0,if x is inside>0,if x is outside=0,if x is on the surfaced(x) = \begin{cases} < 0, & \text{if }x\text{ is inside} \\ > 0, & \text{if }x\text{ is outside} \\ = 0, & \text{if }x\text{ is on the surface} \end{cases}

Return a tuple (vertices, faces): vertices are the 3D points where the surface crosses the grid (in grid-index coordinates), and faces are triangles given as triples of indices into vertices.

This technique is widely used in computer vision and 3D modeling applications.

Example

Input
3D SDF grid
Output
Mesh vertices and faces
Reasoning
  • Input: sdf is a 3D grid of signed distances, dijkd_{ijk} sampled at integer grid points (i,j,k)(i, j, k). Take a tiny 2×2×22 \times 2 \times 2 grid (a single cube) with d000=−0.2d_{000} = -0.2 and the other seven corners at 0.60.6: one corner is inside the surface, the rest outside.
  • Where the surface crosses: only a cube edge whose two end values have opposite signs can contain a zero. Here that is the three edges leaving corner (0,0,0)(0,0,0); the other nine join two positive corners.
  • Locating the crossing: treat dd as linear along the edge and solve for the fraction tt where it hits zero: t=0−dlodhi−dlo=0.20.8=0.25t = \frac{0 - d_{\text{lo}}}{d_{\text{hi}} - d_{\text{lo}}} = \frac{0.2}{0.8} = 0.25.
  • Output (mesh vertices and faces): vertices (0.25,0,0)(0.25, 0, 0), (0,0.25,0)(0, 0.25, 0), (0,0,0.25)(0, 0, 0.25) in grid-index coordinates, and one face, the triangle [0,1,2][0, 1, 2], cutting off the inside corner.
  • Notice that a value closer to zero pulls the vertex toward that corner; at d000=−0.6d_{000} = -0.6 every vertex would sit at the edge midpoint, t=0.5t = 0.5.

Constraints

  • Input SDF: 3D numpy array with shape (x, y, z) and dtype float32, representing the signed distance field
  • Valid SDF values: in the range [-inf, inf], where negative values indicate points inside the surface, positive values indicate points outside, and zero indicates points on the surface
  • Output mesh: list of vertices as 3D float32 numpy arrays and list of faces as integer numpy arrays, representing the extracted mesh surface
  • Grid size: x, y, z dimensions of the input SDF grid are in the range [1, 100]
  • Output precision: float32 for vertex coordinates and uint32 for face indices

Learn this in

More from CV: 3D Reconstruction

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.