Skip to content

Commit

Permalink
type check for threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedh committed Nov 8, 2024
1 parent 3dbde01 commit fc40523
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ requires = ["setuptools >= 61.0", "wheel"]
[project]
name = "trimesh"
requires-python = ">=3.8"
version = "4.5.2"
version = "4.5.3"
authors = [{name = "Michael Dawson-Haggerty", email = "mikedh@kerfed.com"}]
license = {file = "LICENSE.md"}
description = "Import, export, process, analyze and view triangular meshes."
Expand Down
21 changes: 17 additions & 4 deletions tests/test_voxel.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,29 @@ def test_marching(self):
g.log.warning("no skimage, skipping marching cubes test")
return

march = g.trimesh.voxel.ops.matrix_to_marching_cubes

# make sure offset is correct
matrix = g.np.ones((3, 3, 3), dtype=bool)
mesh = g.trimesh.voxel.ops.matrix_to_marching_cubes(matrix=matrix)
mesh = march(matrix=matrix)
assert mesh.is_watertight

mesh = g.trimesh.voxel.ops.matrix_to_marching_cubes(matrix=matrix).apply_scale(
3.0
)
mesh = march(matrix=matrix).apply_scale(3.0)
assert mesh.is_watertight

# try an array full of a small number
matrix = g.np.full((3, 3, 3), 0.01, dtype=g.np.float64)
# set some to zero
matrix[:2, :2, :2] = 0.0

a = march(matrix)
assert a.is_watertight

# but above the threshold it should be not empty
b = march(matrix, threshold=-0.001)
assert b.is_watertight
assert b.volume > a.volume

def test_marching_points(self):
"""
Try marching cubes on points
Expand Down
6 changes: 4 additions & 2 deletions trimesh/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from .iteration import chain

# use our wrapped types for wider version compatibility
from .typed import Union
from .typed import Dict, List, Optional, Set, Union

# create a default logger
log = logging.getLogger("trimesh")
Expand Down Expand Up @@ -2359,7 +2359,9 @@ def is_ccw(points, return_all=False):
return ccw, area, centroid


def unique_name(start, contains, counts=None):
def unique_name(
start: str, contains: Union[Dict, Set, List], counts: Optional[Dict] = None
):
"""
Deterministically generate a unique name not
contained in a dict, set or other grouping with
Expand Down
13 changes: 8 additions & 5 deletions trimesh/voxel/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .. import util
from ..constants import log
from ..typed import ArrayLike, Floating, Number, Optional


def fill_orthographic(dense):
Expand Down Expand Up @@ -95,7 +96,9 @@ def fill_base(sparse_indices):
fill_voxelization = fill_base


def matrix_to_marching_cubes(matrix, pitch=1.0, threshold=None):
def matrix_to_marching_cubes(
matrix: ArrayLike, pitch: Floating = 1.0, threshold: Optional[Number] = None
):
"""
Convert an (n, m, p) matrix into a mesh, using marching_cubes.
Expand All @@ -106,7 +109,8 @@ def matrix_to_marching_cubes(matrix, pitch=1.0, threshold=None):
pitch : float or length-3 tuple of floats, optional
Voxel spacing in each dimension
threshold : float or None, optional
If specified, converts the input 'matrix' into a Boolean matrix by setting values above to 'threshold' to True and those below or equal to False
If specified, converts the input into a boolean
matrix by considering values above `threshold` as True
Returns
Expand All @@ -119,9 +123,8 @@ def matrix_to_marching_cubes(matrix, pitch=1.0, threshold=None):

from ..base import Trimesh

if isinstance(threshold, float) or isinstance(threshold, int) :
matrix_as_array = np.asarray(matrix)
matrix = np.where(matrix_as_array > threshold, True, False)
if threshold is not None:
matrix = np.asanyarray(matrix) > threshold
else:
matrix = np.asanyarray(matrix, dtype=bool)

Expand Down

0 comments on commit fc40523

Please sign in to comment.