Skip to content
Snippets Groups Projects

Sphericity

Merged s212246 requested to merge sphericity into main
1 file
+ 13
9
Compare changes
  • Side-by-side
  • Inline
+ 13
9
import numpy as np
import qim3d.processing
from qim3d.utils.logger import log
import trimesh
import qim3d
def volume(obj, **mesh_kwargs) -> float:
"""
Compute the volume of a 3D volume or mesh.
@@ -44,7 +46,8 @@ def volume(obj, **mesh_kwargs) -> float:
log.info("Converting volume to mesh.")
obj = qim3d.processing.create_mesh(obj, **mesh_kwargs)
return obj.volume
return abs(obj.volume)
def area(obj, **mesh_kwargs) -> float:
"""
@@ -88,13 +91,14 @@ def area(obj, **mesh_kwargs) -> float:
return obj.area
def sphericity(obj, **mesh_kwargs) -> float:
"""
Compute the sphericity of a 3D volume or mesh.
Sphericity is a measure of how spherical an object is. It is defined as the ratio
of the surface area of a sphere with the same volume as the object to the object's
actual surface area.
Sphericity is a measure of how spherical an object is. It is defined as the ratio
of the surface area of a sphere with the same volume as the object to the object's
actual surface area.
Args:
obj: Either a np.ndarray volume or a mesh object of type trimesh.Trimesh.
@@ -127,7 +131,7 @@ def sphericity(obj, **mesh_kwargs) -> float:
```
!!! info "Limitations Due to Pixelation"
Sphericity is particularly sensitive to the resolution of the mesh, as it directly impacts the accuracy of surface area and volume calculations.
Sphericity is particularly sensitive to the resolution of the mesh, as it directly impacts the accuracy of surface area and volume calculations.
Indeed, since the mesh is generated from voxel-based 3D volume data, the discrete nature of the voxels leads to pixelation effects that reduce the precision of sphericity measurements.
Higher resolution meshes may mitigate these errors but often at the cost of increased computational demands.
"""
@@ -135,13 +139,13 @@ def sphericity(obj, **mesh_kwargs) -> float:
log.info("Converting volume to mesh.")
obj = qim3d.processing.create_mesh(obj, **mesh_kwargs)
V = volume(obj)
A = area(obj)
volume = qim3d.processing.volume(obj)
area = qim3d.processing.area(obj)
if A == 0:
if area == 0:
log.warning("Surface area is zero, sphericity is undefined.")
return np.nan
sphericity = (np.pi**(1/3) * (6 * V)**(2/3)) / A
sphericity = (np.pi ** (1 / 3) * (6 * volume) ** (2 / 3)) / area
log.info(f"Sphericity: {sphericity}")
return sphericity
Loading