Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding thresholding option to matrix_to_marching_cubes() #2320

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions trimesh/voxel/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,19 @@ def fill_base(sparse_indices):
fill_voxelization = fill_base


def matrix_to_marching_cubes(matrix, pitch=1.0):
def matrix_to_marching_cubes(matrix, pitch=1.0, threshold=None):
"""
Convert an (n, m, p) matrix into a mesh, using marching_cubes.

Parameters
-----------
matrix : (n, m, p) bool
Occupancy array
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


Returns
----------
Expand All @@ -114,7 +119,11 @@ def matrix_to_marching_cubes(matrix, pitch=1.0):

from ..base import Trimesh

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

rev_matrix = np.logical_not(matrix) # Takes set about 0.
# Add in padding so marching cubes can function properly with
Expand Down
Loading