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

Add auto_scaling parameter for scaling the mesh inside the unit sphere #39

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions mesh_to_sdf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def mesh_to_voxels(mesh, voxel_resolution=64, surface_point_method='scan', sign_
return surface_point_cloud.get_voxels(voxel_resolution, sign_method=='depth', normal_sample_count, pad, check_result, return_gradients)

# Sample some uniform points and some normally distributed around the surface as proposed in the DeepSDF paper
def sample_sdf_near_surface(mesh, number_of_points = 500000, surface_point_method='scan', sign_method='normal', scan_count=100, scan_resolution=400, sample_point_count=10000000, normal_sample_count=11, min_size=0, return_gradients=False):
mesh = scale_to_unit_sphere(mesh)
def sample_sdf_near_surface(mesh, number_of_points = 500000, surface_point_method='scan', sign_method='normal', scan_count=100, scan_resolution=400, sample_point_count=10000000, normal_sample_count=11, min_size=0, return_gradients=False, auto_scaling=True, scale_ratio=1):
mesh = scale_to_unit_sphere(mesh, auto_scaling, scale_ratio)

if surface_point_method == 'sample' and sign_method == 'depth':
print("Incompatible methods for sampling points and determining sign, using sign_method='normal' instead.")
Expand Down
9 changes: 6 additions & 3 deletions mesh_to_sdf/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
import trimesh
import numpy as np

def scale_to_unit_sphere(mesh):
def scale_to_unit_sphere(mesh, auto_scaling, scale_ratio):
if isinstance(mesh, trimesh.Scene):
mesh = mesh.dump().sum()

vertices = mesh.vertices - mesh.bounding_box.centroid
distances = np.linalg.norm(vertices, axis=1)
vertices /= np.max(distances)
if auto_scaling:
distances = np.linalg.norm(vertices, axis=1)
vertices /= np.max(distances)
else:
vertices /= scale_ratio # ratio between the original mesh and the scaled mesh inside the unit sphere

return trimesh.Trimesh(vertices=vertices, faces=mesh.faces)

Expand Down