From 5ac910b0b8ecc6a5130ddf77effa7f92e2aef475 Mon Sep 17 00:00:00 2001 From: "Josh Warner (Mac)" Date: Fri, 14 Mar 2014 14:15:45 -0500 Subject: [PATCH] FIX: correct other instance of np.linalg.norm, clarify comments --- skimage/measure/_marching_cubes.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/skimage/measure/_marching_cubes.py b/skimage/measure/_marching_cubes.py index b5750f41..8f9dfcbc 100644 --- a/skimage/measure/_marching_cubes.py +++ b/skimage/measure/_marching_cubes.py @@ -68,6 +68,12 @@ def marching_cubes(volume, level, spacing=(1., 1., 1.)): lexicographical order) coordinate in the contour. This is a side-effect of how the input array is traversed, but can be relied upon. + The generated mesh does not guarantee coherent orientation because of how + symmetry is used in the algorithm. If this is required, e.g. due to a + particular visualization package or for generating 3D printing STL files, + the utility ``skimage.measure.correct_mesh_orientation`` is available to + fix this in post-processing. + To quantify the area of an isosurface generated by this algorithm, pass the outputs directly into `skimage.measure.mesh_surface_area`. @@ -240,11 +246,12 @@ def correct_mesh_orientation(volume, verts, faces, spacing=(1., 1., 1.), grad_centroids = np.c_[grad_centroids_x, grad_centroids_y, grad_centroids_z] grad_centroids = (grad_centroids / - np.linalg.norm(grad_centroids, axis=1)[:, np.newaxis]) + (np.sum(grad_centroids ** 2, + axis=1) ** 0.5)[:, np.newaxis]) - # Calculate and normalize cross products for each face + # Find normal vectors for each face via cross product crosses = np.cross(a, b) - crosses = crosses / np.sum(crosses ** 2, axis=1) ** (0.5)[:, np.newaxis] + crosses = crosses / (np.sum(crosses ** 2, axis=1) ** (0.5))[:, np.newaxis] # Take dot product dotproducts = (grad_centroids * crosses).sum(axis=1) @@ -260,7 +267,7 @@ def correct_mesh_orientation(volume, verts, faces, spacing=(1., 1., 1.), raise ValueError("Incorrect input %s in `gradient_direction`, see " "docstring." % (gradient_direction)) - # Swap orientation and return, without modifying original data + # Correct orientation and return, without modifying original data faces_corrected = faces.copy() faces_corrected[indices] = faces_corrected[indices, ::-1]