elf.mesh.mesh

 1from typing import Optional, Tuple
 2
 3import bioimage_cpp as bic
 4import numpy as np
 5from skimage.measure import marching_cubes as marching_cubes_impl
 6
 7
 8def marching_cubes(
 9    obj: np.ndarray,
10    smoothing_iterations: int = 0,
11    resolution: Optional[Tuple[float, float, float]] = None,
12) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
13    """Compute mesh via marching cubes.
14
15    This is a wrapper around the skimage marching cubes implementation that provides
16    additional mesh smoothing.
17
18    Args:
19        obj: Volume containing the object to be meshed.
20        smoothing_iterations: Number of mesh smoothing iterations.
21        resolution: Resolution of the data.
22
23    Returns:
24        The vertices of the mesh.
25        The faces of the mesh.
26        The normals of the mesh.
27    """
28    resolution = (1.0, 1.0, 1.0) if resolution is None else resolution
29    if len(resolution) != 3:
30        raise ValueError(f"Invalid resolution argument: {resolution}")
31    resolution = tuple(resolution)
32
33    verts, faces, normals, _ = marching_cubes_impl(obj, spacing=resolution)
34    if smoothing_iterations > 0:
35        verts, normals = smooth_mesh(verts, normals, faces, smoothing_iterations)
36
37    return verts, faces, normals
38
39
40def smooth_mesh(
41    verts: np.ndarray, normals: np.ndarray, faces: np.ndarray, iterations: int
42) -> Tuple[np.ndarray, np.ndarray]:
43    """Smooth mesh surface via laplacian smoothing.
44
45    Args:
46        verts: The mesh vertices.
47        normals: The mesh normals.
48        faces: The mesh faces.
49        iterations: The number of smoothing iterations.
50
51    Returns:
52        The vertices after smoothing.
53        The normals after smoothing.
54    """
55    return bic.utils.smooth_mesh(verts, normals, faces, iterations)
def marching_cubes( obj: numpy.ndarray, smoothing_iterations: int = 0, resolution: Tuple[float, float, float] | None = None) -> Tuple[numpy.ndarray, numpy.ndarray, numpy.ndarray]:
 9def marching_cubes(
10    obj: np.ndarray,
11    smoothing_iterations: int = 0,
12    resolution: Optional[Tuple[float, float, float]] = None,
13) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:
14    """Compute mesh via marching cubes.
15
16    This is a wrapper around the skimage marching cubes implementation that provides
17    additional mesh smoothing.
18
19    Args:
20        obj: Volume containing the object to be meshed.
21        smoothing_iterations: Number of mesh smoothing iterations.
22        resolution: Resolution of the data.
23
24    Returns:
25        The vertices of the mesh.
26        The faces of the mesh.
27        The normals of the mesh.
28    """
29    resolution = (1.0, 1.0, 1.0) if resolution is None else resolution
30    if len(resolution) != 3:
31        raise ValueError(f"Invalid resolution argument: {resolution}")
32    resolution = tuple(resolution)
33
34    verts, faces, normals, _ = marching_cubes_impl(obj, spacing=resolution)
35    if smoothing_iterations > 0:
36        verts, normals = smooth_mesh(verts, normals, faces, smoothing_iterations)
37
38    return verts, faces, normals

Compute mesh via marching cubes.

This is a wrapper around the skimage marching cubes implementation that provides additional mesh smoothing.

Arguments:
  • obj: Volume containing the object to be meshed.
  • smoothing_iterations: Number of mesh smoothing iterations.
  • resolution: Resolution of the data.
Returns:

The vertices of the mesh. The faces of the mesh. The normals of the mesh.

def smooth_mesh( verts: numpy.ndarray, normals: numpy.ndarray, faces: numpy.ndarray, iterations: int) -> Tuple[numpy.ndarray, numpy.ndarray]:
41def smooth_mesh(
42    verts: np.ndarray, normals: np.ndarray, faces: np.ndarray, iterations: int
43) -> Tuple[np.ndarray, np.ndarray]:
44    """Smooth mesh surface via laplacian smoothing.
45
46    Args:
47        verts: The mesh vertices.
48        normals: The mesh normals.
49        faces: The mesh faces.
50        iterations: The number of smoothing iterations.
51
52    Returns:
53        The vertices after smoothing.
54        The normals after smoothing.
55    """
56    return bic.utils.smooth_mesh(verts, normals, faces, iterations)

Smooth mesh surface via laplacian smoothing.

Arguments:
  • verts: The mesh vertices.
  • normals: The mesh normals.
  • faces: The mesh faces.
  • iterations: The number of smoothing iterations.
Returns:

The vertices after smoothing. The normals after smoothing.