mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
2.98k stars 577 forks source link

Barycentric Coordinates Warning #1889

Open Eric-Vin opened 1 year ago

Eric-Vin commented 1 year ago

Trimesh currently throws warnings when computing barycentric coordinates for certain meshes, like the ones below:

trimesh/triangles.py:518: RuntimeWarning: divide by zero encountered in divide
    inverse_denominator = 1.0 / (dot00 * dot11 - dot01 * dot01)
trimesh/triangles.py:525: RuntimeWarning: invalid value encountered in subtract
    barycentric[:, 0] = 1 - barycentric[:, 1] - barycentric[:, 2]

Is this something to be concerned about or can it be safely ignored?

mikedh commented 1 year ago

Is it in meshes with degenerate triangles? If I recall correctly that warning just inserts some NaN values into the result (which sounds somewhat reasonable for a degenerate although I guess any value in the range 0.0 - 1.0 would also be correct and possibly more useful).

Eric-Vin commented 1 year ago

Hmm it doesn't look like it has any degenerate faces, since calling the remove_degenerate_faces method returns all True values (I definitely could be misunderstanding that though). I did some more digging and it looks like the warnings are being raised when using the trimesh.sample.volume_mesh method.

Here's a program and mesh to reproduce it if that helps:

import trimesh

with open(("debug_mesh.stl"), "rb") as mesh_file:
    mesh = trimesh.load(mesh_file, file_type="stl")

assert all(mesh.remove_degenerate_faces())

trimesh.sample.volume_mesh(mesh, 100)

debug_mesh.zip

Eric-Vin commented 1 year ago

@mikedh I just wanted to follow up regarding this. Appreciate your help!