marian42 / mesh_to_sdf

Calculate signed distance fields for arbitrary meshes
https://pypi.org/project/mesh-to-sdf/
MIT License
991 stars 107 forks source link

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

Open maurock opened 1 year ago

maurock commented 1 year ago

Problem statement

When the method sample_sdf_near_surface(..) is called, the mesh is first scaled to fit into a unit sphere. The scaling ratio is not fixed, as it depends on the object geometry - which means that different objects are scaled differently. While in the virtual domain this is not a problem, as we can extract geometry information from the simulator, a geometry-dependent scaling ratio is a problem in the real world, where we cannot extract geometry information from the object easily. Therefore, it is essential to have a way to keep the scaling ratio fixed for all the objects in our dataset. Moreover, having control over the scaling ratio could be beneficial in the virtual domain as well.

Changes

This PR adds two parameters to the methods sample_sdf_near_surface(..) and scale_to_unit_sphere(..) :

Example

# sample from mesh_1 and mesh_2, keeping the ratio between the original and resulting mesh fixed at 1.3
samples_1 = mesh_to_sdf.sample_sdf_near_surface(mesh_1, auto_scaling=False, scale_ratio = 1.3)[0]
samples_2 = mesh_to_sdf.sample_sdf_near_surface(mesh_2, auto_scaling=False, scale_ratio = 1.3)[0]

# plot the samples
fig = plotly.graph_objects.Figure(
            [   go.Scatter3d(
                     x=samples_1[:, 0], y=samples_1[:, 1],  z=samples_1[:, 2], 
                     mode='markers', marker=dict(size=1, color='darkblue') ),
                 go.Scatter3d(
                     x=samples_2[:, 0], y=samples_2[:, 1], z=samples_2[:, 2], 
                     mode='markers', marker=dict(size=1, color='orange')  )
            ]
        )
fig.show()

image

Both objects have been scaled by the same ratio, therefore we can use the DeepSDF method in downstream tasks in the real world.

I hope this makes sense, I am happy answer any questions you may have about this PR.