Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
495 changes: 495 additions & 0 deletions PARALLEL.md

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ a `NumPy` array:
Note that using a function to represent the volumetric data is **much** slower
than using a `NumPy` array.

## Parallel processing

Both `mcubes.marching_cubes` and `mcubes.marching_cubes_func` accept a
`num_threads` argument. The default, `0`, uses one thread per CPU core and `1`
runs the algorithm on the calling thread only. The extracted mesh is the same
for every value, so the argument only affects speed:

```Python
>>> vertices, triangles = mcubes.marching_cubes(u, 0, num_threads=4)
```

The volume is swept in several passes (classify the cubes, prefix sum their
vertex counts, place the vertices, and finally emit the triangles), and each
pass runs in parallel. Vertices are owned by exactly one cube, which is what
makes the passes independent of each other. See [PARALLEL.md](PARALLEL.md) for
the algorithm, the implementation notes and the benchmarks.

## Smoothing binary arrays

![Overview](images/smoothing_overview.png "Overview of mcubes.smooth")
Expand Down
32 changes: 25 additions & 7 deletions mcubes/src/_mcubes.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,43 @@ cimport numpy as np
np.import_array()

cdef extern from "pywrapper.h":
cdef object c_marching_cubes "marching_cubes"(np.ndarray, double) except +
cdef object c_marching_cubes_func "marching_cubes_func"(tuple, tuple, int, int, int, object, double) except +
cdef object c_marching_cubes "marching_cubes"(np.ndarray, double, int) except +
cdef object c_marching_cubes_func "marching_cubes_func"(tuple, tuple, int, int, int, object, double, int) except +

def marching_cubes(np.ndarray volume, float isovalue):

verts, faces = c_marching_cubes(volume, isovalue)
def marching_cubes(np.ndarray volume, float isovalue, int num_threads=0):
"""Extract the isosurface of a 3D volume.

``num_threads`` selects how many threads the algorithm may use. The default
of 0 uses one thread per CPU core, and 1 keeps it single-threaded. The
result does not depend on this value.
"""

if num_threads < 0:
raise ValueError("num_threads cannot be negative")

verts, faces = c_marching_cubes(volume, isovalue, num_threads)
verts.shape = (-1, 3)
faces.shape = (-1, 3)
return verts, faces

def marching_cubes_func(tuple lower, tuple upper, int numx, int numy, int numz, object f, double isovalue):
def marching_cubes_func(tuple lower, tuple upper, int numx, int numy, int numz, object f, double isovalue, int num_threads=0):
"""Extract the isosurface of a volume given as a function ``f(x, y, z)``.

``num_threads`` selects how many threads the algorithm may use. The default
of 0 uses one thread per CPU core, and 1 keeps it single-threaded. The
result does not depend on this value.
"""

if any(l_i >= u_i for l_i, u_i in zip(lower, upper)):
raise ValueError("lower coordinates cannot be larger than upper coordinates")

if numx < 2 or numy < 2 or numz < 2:
raise ValueError("numx, numy, numz cannot be smaller than 2")

verts, faces = c_marching_cubes_func(lower, upper, numx, numy, numz, f, isovalue)
if num_threads < 0:
raise ValueError("num_threads cannot be negative")

verts, faces = c_marching_cubes_func(lower, upper, numx, numy, numz, f, isovalue, num_threads)
verts.shape = (-1, 3)
faces.shape = (-1, 3)
return verts, faces
657 changes: 323 additions & 334 deletions mcubes/src/marchingcubes.cpp

Large diffs are not rendered by default.

Loading