Skip to content

Meshes

qim3d.mesh

qim3d.mesh.from_volume

from_volume(volume, mesh_precision=1.0, **kwargs)

Convert a 3D numpy array to a mesh object using the volumetric_isocontour function from Pygel3D.

Parameters:

Name Type Description Default
volume ndarray

A 3D numpy array representing a volume.

required
mesh_precision float

Scaling factor for adjusting the resolution of the mesh. Default is 1.0 (no scaling).

1.0
**kwargs any

Additional arguments to pass to the Pygel3D volumetric_isocontour function.

{}

Raises:

Type Description
ValueError

If the input volume is not a 3D numpy array or if the input volume is empty.

Returns:

Type Description
Manifold

hmesh.Manifold: A Pygel3D mesh object representing the input volume.

Example

Convert a 3D numpy array to a Pygel3D mesh object:

import qim3d

# Generate a 3D blob
synthetic_blob = qim3d.generate.volume()

# Convert the 3D numpy array to a Pygel3D mesh object
mesh = qim3d.mesh.from_volume(synthetic_blob, mesh_precision=0.5)

# Visualize the generated mesh
qim3d.viz.mesh(mesh)
pygel3d_visualization

Source code in qim3d/mesh/_common_mesh_methods.py
def from_volume(
    volume: np.ndarray, mesh_precision: float = 1.0, **kwargs: any
) -> hmesh.Manifold:
    """
    Convert a 3D numpy array to a mesh object using the [volumetric_isocontour](https://www2.compute.dtu.dk/projects/GEL/PyGEL/pygel3d/hmesh.html#volumetric_isocontour)
    function from Pygel3D.

    Args:
        volume (np.ndarray): A 3D numpy array representing a volume.
        mesh_precision (float, optional): Scaling factor for adjusting the resolution of the mesh.
                                          Default is 1.0 (no scaling).
        **kwargs: Additional arguments to pass to the Pygel3D volumetric_isocontour function.

    Raises:
        ValueError: If the input volume is not a 3D numpy array or if the input volume is empty.

    Returns:
        hmesh.Manifold: A Pygel3D mesh object representing the input volume.

    Example:
        Convert a 3D numpy array to a Pygel3D mesh object:
        ```python
        import qim3d

        # Generate a 3D blob
        synthetic_blob = qim3d.generate.volume()

        # Convert the 3D numpy array to a Pygel3D mesh object
        mesh = qim3d.mesh.from_volume(synthetic_blob, mesh_precision=0.5)

        # Visualize the generated mesh
        qim3d.viz.mesh(mesh)
        ```
        ![pygel3d_visualization](../../assets/screenshots/viz-pygel_mesh.png)


    """

    if volume.ndim != 3:
        msg = 'The input volume must be a 3D numpy array.'
        raise ValueError(msg)

    if volume.size == 0:
        msg = 'The input volume must not be empty.'
        raise ValueError(msg)

    if not (0 < mesh_precision <= 1):
        msg = 'The mesh precision must be between 0 and 1.'
        raise ValueError(msg)

    # Apply scaling to adjust mesh resolution
    volume = scipy.ndimage.zoom(volume, zoom=mesh_precision, order=0)

    mesh = hmesh.volumetric_isocontour(volume, **kwargs)

    log.info(
        f'Mesh generated with {len(mesh.vertices())} vertices and {len(mesh.faces())} faces.'
    )

    return mesh