trimesh icon indicating copy to clipboard operation
trimesh copied to clipboard

bug: remove_infinite_values() can't work for faces

Open kampelmuehler opened this issue 3 years ago • 3 comments

remove_infinite_values() is using np.isfinite(), which will only work for float types, but the faces array is np.int64 which can not represent np.nan or np.inf.

https://github.com/mikedh/trimesh/blob/main/trimesh/base.py#L1246

Since np.full(1, fill_value=np.inf, dtype=np.int64) and np.full(1, fill_value=np.nan, dtype=np.int64) gives an array with np.iinfo(np.int64).min (minimum value for int64) I would suggest checking for np.iinfo(np.int64).min or even just negative values within faces .

I'm happy to open a PR once I got your opinion on this @mikedh.

kampelmuehler avatar Jan 25 '22 10:01 kampelmuehler

Gotcha, so isn't the np.isfinite(faces) a no-op? It appears to just always return an all-True mask, which doesn't alter faces.

We should probably just remove that line, and if we want to check for invalid face indices do it in mesh.remove_degenerate_faces or maybe add a remove_invalid_faces?

mikedh avatar Jan 27 '22 20:01 mikedh

I agree with getting rid of this line.

I'd vote for adding mesh.remove_invalid_faces since mesh.remove_degenerate_faces is operating on a geometric rather than a data type related basis. Also it is already used throughout the library, and we could encounter unexpected behavior.

In the docstring for mesh.remove_degenerate_faces it says "If not specified, it will remove any face with a zero normal." - I can't see anything along those lines happening at a glance. What's meant by that?

If you agree with adding mesh.remove_invalid_faces and potentially updating the above docstring, let me know and I'll make a PR @mikedh

kampelmuehler avatar Jan 31 '22 08:01 kampelmuehler

@mikedh ping

kampelmuehler avatar May 04 '22 05:05 kampelmuehler