NVIDIAGameWorks / kaolin

A PyTorch Library for Accelerating 3D Deep Learning Research
Apache License 2.0
4.48k stars 559 forks source link

point_to_mesh_distance returns strange "jumps" in distance #807

Open lorjul opened 3 months ago

lorjul commented 3 months ago

Hello! I have troubles to get the point_to_mesh_distance function to work. Here is a MWE:

import matplotlib.pyplot as plt
import torch
from kaolin.io import obj
from kaolin.ops.mesh import index_vertices_by_faces
from kaolin.metrics.trianglemesh import point_to_mesh_distance

mesh = obj.import_mesh("/path/to/model.obj", with_normals=True, triangulate=True)

resolution = 64
# hardcoded to match the model bounds
x = torch.linspace(-10, 10, resolution)
y = torch.linspace(-10, 10, resolution)
z = torch.linspace(-10, 10, resolution)
grid_points = torch.stack(torch.meshgrid(x, y, z, indexing="ij"), dim=-1).view(-1, 3)

vertices = mesh.vertices.cuda()
faces = mesh.faces.cuda()
grid_points = grid_points.cuda()

face_vertices = index_vertices_by_faces(vertices[None], faces)
distance, _index, _dist_type = point_to_mesh_distance(grid_points[None], face_vertices)

plt.imshow(distance.reshape(resolution, resolution, resolution).cpu()[:, :, 31])
plt.savefig("df-kaolin.png")

However, the resulting distance field looks way off (I'm especially irritated by the edge in the top part of the image):

mwe

Calculating the distance field using trimesh returns the following, which makes much more sense:

image

What am I doing wrong?

shumash commented 3 months ago

Would you be able to share the mesh file that is causing this issue?

lorjul commented 3 months ago

Here is a link to the used mesh: https://uniaugsburg-my.sharepoint.com/:u:/g/personal/julian_lorenz_uni-a_de/ESAXVkFPQ-xNlEipcrXJjcoBLfyl_CXqIgHLdwnsWAreiQ?e=7H5Ixm

The used mesh was converted to the OBJ file format. The original mesh from Kristen Brown can be found here

(edit: updated the link)

Caenorst commented 3 months ago

Hi @lorjul I can't have access to the first link, just to make sure I'm using the same model can you share it with us? Please contact me at cfujitsang@nvidia.com

lorjul commented 2 months ago

My bad, sorry, the link was set to expire. Here is an updated link which should work: https://uniaugsburg-my.sharepoint.com/:u:/g/personal/julian_lorenz_uni-a_de/ESAXVkFPQ-xNlEipcrXJjcoBLfyl_CXqIgHLdwnsWAreiQ?e=7H5Ixm