Skip to content

idra-lab/cuda_marching_cubes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CUDA Marching Cubes — GPU-Accelerated Iso-Surface Extraction for PyTorch

A fast CUDA implementation of the Marching Cubes algorithm for extracting iso-surfaces from volumetric grids, built as a PyTorch extension. Designed for real-time 3D reconstruction pipelines, with native support for TSDF grids and per-cell observation masks.

Features

  • Full GPU pipeline: parallel counting, allocation, and generation of triangles
  • Mask support: per-cell boolean mask to avoid unwanted triangles
  • PyTorch-native: inputs and outputs are CUDA tensors, no CPU round-trips
  • Compiled JIT with -O3 --use_fast_math for maximum throughput
    • Retrives also vertex normals if compute_normals=True (requires 3D grid access, so slightly slower)

Requirements

  • Python 3.8+
  • PyTorch with CUDA support
  • CUDA toolkit (compatible with your PyTorch build)

Usage

import torch
from mc_mixin import _load_mc_ext

mc_cuda = _load_mc_ext()  # JIT-compiles on first call

# --- Basic: extract iso-surface from a TSDF grid ---
grid = torch.zeros(128, 128, 128, device="cuda", dtype=torch.float32)
# ... fill grid with signed distance values ...
verts, faces = mc_cuda.marching_cubes(grid, thresh=0.0)

# verts: [V, 3] float32 — positions in grid-index space
# faces: [F, 3] int32  — triangle indices



# --- With mask: ignore unobserved voxels ---
mask = torch.ones(128, 128, 128, device="cuda", dtype=torch.bool)
mask[:64, :64, :] = False   # unobserved slice / spicchio

verts, faces, normals = mc_cuda.marching_cubes(grid, thresh=0.0, mask=mask, compute_normals=True)
Description Image
Different Resolutions Support Marching Cubes Resolutions
Masking Support Marching Cubes with Masks
Normals Computation Support Marching Cubes with Normals

API

marching_cubes(grid, thresh, mask=None, compute_normals=False) -> (verts, faces, normals (optional))
Parameter Type Description
grid float32 [X, Y, Z] CUDA tensor Signed distance (or any scalar field) values
thresh float Iso-surface level to extract (0.0 for TSDF)
mask bool [X, Y, Z] CUDA tensor, optional If given, only masked-in cells contribute faces
compute_normals bool, optional If True, also compute vertex normals

Credits

The code is a revised and improved version of CuMCubes to which I added mask and normals support similarly to scipy's implementation.

About

CUDA implementation of Marching Cubes that also accept a mask argument to select active voxels

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors