Home

Awesome

CuMCubes

CuMCubes is an CUDA implementation of the marching cubes algorithm to extract iso-surfaces from volumetric data. The volumetric data can be given as a three-dimensional torch.Tensor or as a Python function f(x, y, z).

Requirements

The enviroment of my developer machine:

Installation

You can use pip install cumcubes directly, or use:

python setup.py install

Or use:

pip install .

Or use:

pip install https://github.com/lzhnb/CuMCubes

Example

# toy examples from the PyMCubes
python examples/sphere.py
python examples/function.py
# we extract the sdf of bunny via `mesh_to_sdf`
python examples/bunny_sdf.py

The following example creates a torch.Tensor volume with spherical iso-surfaces and extracts one of them (i.e., a sphere) with mcubes.marching_cubes. The result is exported to sphere.dae:

>>> import torch
>>> import cumcubes

>>> X, Y, Z = torch.meshgrid(torch.linspace(0, 99, 200), torch.linspace(0, 99, 200), torch.linspace(0, 99, 200), indexing="ij")
>>> DENSITY_GRID = ((X - 50)**2 + (Y - 50)**2 + (Z - 50)**2 - 25**2).cuda()
>>> with cumcubes.Timer("cuda marching cube: {:.6f}s"):
>>>     vertices_cu, faces_cu = cumcubes.marching_cubes(DENSITY_GRID, 0, verbose=True) # verbose to print the number of vertices and faces
# cuda marching cube: 0.004981s

""" Compare with the CPU implementation """
>>> density_grid_cpu = DENSITY_GRID.cpu().numpy()
>>> with cumcubes.Timer("cpu marching cube: {:.6f}s"):
>>>     vertices_c, faces_c = mcubes.marching_cubes(density_grid_cpu, 0)
# cpu marching cube: 0.337798s

<img src="asserts/bunny_cumcubes.png" height="342"/> <img src="asserts/bunny_mcubes.png" height="342"/> The left image is the result of cumcubes.marching_cubes, the right image is the result of mcubes.marching_cubes. (The Stanford Bunny Example)

NOTE: Mallocating memory on GPU will consume some time.

TODO

Acknowledgement

Please feel free to discuss :)