_mc
Marching cubes implementation of volumetric data.
This file is a modification of the original _marching_cubes_lewiner.pyx file from the scikit-image project, retrieved from git revision:
129af33b9c118dd87efd4a39ce623e70f8188ce8
As such the following copyright notice must be included here, and we should include the acknowledgement in the LICENSE.txt of this project.
Copyright (C) 2019, the scikit-image team All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. Neither the name of skimage nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE), ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
marching_cubes(volume, level=None, spacing=(1.0, 1.0, 1.0), gradient_direction='descent', step_size=1, allow_degenerate=True, use_classic=False)
Lewiner marching cubes algorithm to find surfaces in 3d volumetric data.
In contrast to marching_cubes_classic()
, this algorithm is faster,
resolves ambiguities, and guarantees topologically correct results.
Therefore, this algorithm generally a better choice, unless there
is a specific need for the classic algorithm.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
volume |
(M, N, P) array Input data volume to find isosurfaces. Will internally be converted to float32 if necessary. |
required | |
level |
float, optional
Contour value to search for isosurfaces in |
None
|
|
spacing |
length-3 tuple of floats, optional
Voxel spacing in spatial dimensions corresponding to numpy array
indexing dimensions (M, N, P) as in |
(1.0, 1.0, 1.0)
|
|
gradient_direction |
string, optional Controls if the mesh was generated from an isosurface with gradient descent toward objects of interest (the default), or the opposite, considering the left-hand rule. The two options are: * descent : Object was greater than exterior * ascent : Exterior was greater than object |
'descent'
|
|
step_size |
int, optional Step size in voxels. Default 1. Larger steps yield faster but coarser results. The result will always be topologically correct though. |
1
|
|
allow_degenerate |
bool, optional Whether to allow degenerate (i.e. zero-area) triangles in the end-result. Default True. If False, degenerate triangles are removed, at the cost of making the algorithm slower. |
True
|
|
use_classic |
bool, optional
If given and True, the classic marching cubes by Lorensen (1987)
is used. This option is included for reference purposes. Note
that this algorithm has ambiguities and is not guaranteed to
produce a topologically correct result. The results with using
this option are not generally the same as the
|
False
|
Returns
verts : (V, 3) array
Spatial coordinates for V unique mesh vertices. Coordinate order
matches input volume
(M, N, P).
faces : (F, 3) array
Define triangular faces via referencing vertex indices from verts
.
This algorithm specifically outputs triangles, so each face has
exactly three indices.
normals : (V, 3) array
The normal direction at each vertex, as calculated from the
data.
values : (V, ) array
Gives a measure for the maximum value of the data in the local region
near each vertex. This can be used by visualization tools to apply
a colormap to the mesh.
Notes
The algorithm [1] is an improved version of Chernyaev's Marching Cubes 33 algorithm. It is an efficient algorithm that relies on heavy use of lookup tables to handle the many different cases, keeping the algorithm relatively easy. This implementation is written in Cython, ported from Lewiner's C++ implementation.
To quantify the area of an isosurface generated by this algorithm, pass
verts and faces to skimage.measure.mesh_surface_area
.
Regarding visualization of algorithm output, to contour a volume
named myvolume
about the level 0.0, using the mayavi
package::
from mayavi import mlab # doctest: +SKIP verts, faces, normals, values = marching_cubes_lewiner(myvolume, 0.0) # doctest: +SKIP mlab.triangular_mesh([vert[0] for vert in verts], ... [vert[1] for vert in verts], ... [vert[2] for vert in verts], ... faces) # doctest: +SKIP mlab.show() # doctest: +SKIP
Similarly using the visvis
package::
import visvis as vv # doctest: +SKIP verts, faces, normals, values = marching_cubes_lewiner(myvolume, 0.0) # doctest: +SKIP vv.mesh(np.fliplr(verts), faces, normals, values) # doctest: +SKIP vv.use().Run() # doctest: +SKIP
References
[1] Thomas Lewiner, Helio Lopes, Antonio Wilson Vieira and Geovan Tavares. Efficient implementation of Marching Cubes' cases with topological guarantees. Journal of Graphics Tools 8(2) pp. 1-15 (Dec 2003). https://dx.doi.org/10.1080/10867651.2003.10487582
See Also
skimage.measure.marching_cubes_classic skimage.measure.mesh_surface_area
Source code in chmpy/mc/_mc.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|