Skip to content
Snippets Groups Projects
Commit 072fd216 authored by Alessia Saccardo's avatar Alessia Saccardo
Browse files

Add pygel3d version of trimesh code

parent 29bcad95
No related branches found
No related tags found
1 merge request!156Mesh pygel3d
...@@ -3,6 +3,7 @@ import qim3d.processing ...@@ -3,6 +3,7 @@ import qim3d.processing
from qim3d.utils._logger import log from qim3d.utils._logger import log
import trimesh import trimesh
import qim3d import qim3d
from pygel3d import hmesh
def volume(obj: np.ndarray|trimesh.Trimesh, def volume(obj: np.ndarray|trimesh.Trimesh,
...@@ -89,15 +90,14 @@ def area(obj: np.ndarray|trimesh.Trimesh, ...@@ -89,15 +90,14 @@ def area(obj: np.ndarray|trimesh.Trimesh,
synthetic_blob = qim3d.generate.noise_object(noise_scale = 0.015) synthetic_blob = qim3d.generate.noise_object(noise_scale = 0.015)
# Compute the surface area of the blob # Compute the surface area of the blob
volume = qim3d.features.area(synthetic_blob, level=0.5) area = qim3d.features.area(synthetic_blob, level=0.5)
volume = qim3d.features.area(synthetic_blob, level=0.5) area = qim3d.features.area(synthetic_blob, level=0.5)
print('Area:', volume) print('Area:', area)
``` ```
""" """
if isinstance(obj, np.ndarray): if isinstance(obj, np.ndarray):
log.info("Converting volume to mesh.") log.info("Converting volume to mesh.")
obj = qim3d.mesh.from_volume(obj, **mesh_kwargs) obj = qim3d.mesh.from_volume(obj, **mesh_kwargs)
obj = qim3d.mesh.from_volume(obj, **mesh_kwargs)
return obj.area return obj.area
...@@ -153,7 +153,6 @@ def sphericity(obj: np.ndarray|trimesh.Trimesh, ...@@ -153,7 +153,6 @@ def sphericity(obj: np.ndarray|trimesh.Trimesh,
if isinstance(obj, np.ndarray): if isinstance(obj, np.ndarray):
log.info("Converting volume to mesh.") log.info("Converting volume to mesh.")
obj = qim3d.mesh.from_volume(obj, **mesh_kwargs) obj = qim3d.mesh.from_volume(obj, **mesh_kwargs)
obj = qim3d.mesh.from_volume(obj, **mesh_kwargs)
volume = qim3d.features.volume(obj) volume = qim3d.features.volume(obj)
area = qim3d.features.area(obj) area = qim3d.features.area(obj)
...@@ -167,3 +166,63 @@ def sphericity(obj: np.ndarray|trimesh.Trimesh, ...@@ -167,3 +166,63 @@ def sphericity(obj: np.ndarray|trimesh.Trimesh,
sphericity = (np.pi ** (1 / 3) * (6 * volume) ** (2 / 3)) / area sphericity = (np.pi ** (1 / 3) * (6 * volume) ** (2 / 3)) / area
log.info(f"Sphericity: {sphericity}") log.info(f"Sphericity: {sphericity}")
return sphericity return sphericity
def volume_pygel3d(obj: np.ndarray|hmesh.Manifold) -> float:
"""
Compute the volume of a 3D mesh using the Pygel3D library.
Args:
obj: Either a np.ndarray volume or a mesh object of type hmesh.Manifold.
Returns:
volume (float): The volume of the object.
"""
if isinstance(obj, np.ndarray):
log.info("Converting volume to mesh.")
obj = qim3d.mesh.from_volume_pygel3d(obj)
return hmesh.volume(obj)
def area_pygel3d(obj: np.ndarray|hmesh.Manifold) -> float:
"""
Compute the surface area of a 3D mesh using the Pygel3D library.
Args:
obj: Either a np.ndarray volume or a mesh object of type hmesh.Manifold.
Returns:
area (float): The surface area of the object.
"""
if isinstance(obj, np.ndarray):
log.info("Converting volume to mesh.")
obj = qim3d.mesh.from_volume_pygel3d(obj)
return hmesh.area(obj)
def sphericity_pygel3d(obj: np.ndarray|hmesh.Manifold) -> float:
"""
Compute the sphericity of a 3D mesh using the Pygel3D library.
Args:
obj: Either a np.ndarray volume or a mesh object of type hmesh.Manifold.
Returns:
sphericity (float): The sphericity of the object.
"""
if isinstance(obj, np.ndarray):
log.info("Converting volume to mesh.")
obj = qim3d.mesh.from_volume_pygel3d(obj)
volume = volume_pygel3d(obj)
area = area_pygel3d(obj)
if area == 0:
log.warning("Surface area is zero, sphericity is undefined.")
return np.nan
sphericity = (np.pi ** (1 / 3) * (6 * volume) ** (2 / 3)) / area
log.info(f"Sphericity: {sphericity}")
return sphericity
\ No newline at end of file
from ._common_mesh_methods import from_volume from ._common_mesh_methods import from_volume, from_volume_pygel3d
import numpy as np import numpy as np
from skimage import measure, filters from skimage import measure, filters
import trimesh import trimesh
from pygel3d import hmesh
from typing import Tuple, Any from typing import Tuple, Any
from qim3d.utils._logger import log from qim3d.utils._logger import log
...@@ -76,3 +77,15 @@ def from_volume( ...@@ -76,3 +77,15 @@ def from_volume(
trimesh.repair.fix_inversion(mesh, multibody=True) trimesh.repair.fix_inversion(mesh, multibody=True)
return mesh return mesh
def from_volume_pygel3d(
volume: np.ndarray,
**Kwargs
) -> hmesh.Manifold:
if volume.ndim != 3:
raise ValueError("The input volume must be a 3D numpy array.")
m = hmesh.volumetric_isocontour(volume)
return m
\ No newline at end of file
test_mesh.ipynb 0 → 100644
NaN GiB (NaN%)
View file @ 072fd216
File suppressed by a .gitattributes entry or the file's encoding is unsupported.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment