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

precision issue in forming cuboids with close vertices #2301

Open
mahmouddinar opened this issue Oct 15, 2024 · 2 comments
Open

precision issue in forming cuboids with close vertices #2301

mahmouddinar opened this issue Oct 15, 2024 · 2 comments

Comments

@mahmouddinar
Copy link

Thank you very much for sharing this amazing library.
I am trying to create very tiny cuboids within the bounding box of a mesh and compute their overlapping volumes with the mesh. When the coordinates of the vertices are too close, the cube is not formed: cuboid.extents returns [0,0,0] and the exception is thrown 'Not all meshes are volumes!'
The cuboids are created using:

cuboid = trimesh.Trimesh(vertices=vertices, faces=faces)
The following is an example of the coordinates used to form two cuboids, preceded by the bounding box extents, the first being 0 leading to the exception, and another with non zero extents:

cuboid[0,0,0]: [0. 0. 0.]
[[ 8.53873789e-06 -1.73472348e-18 1.73472348e-18]
[ 8.53873789e-06 -1.73472348e-18 7.38000008e-10]
[ 8.53873789e-06 7.64906759e-10 1.73472348e-18]
[ 8.53873789e-06 7.64906759e-10 7.38000008e-10]
[ 8.53967351e-06 -1.73472348e-18 1.73472348e-18]
[ 8.53967351e-06 -1.73472348e-18 7.38000008e-10]
[ 8.53967351e-06 7.64906759e-10 1.73472348e-18]
[ 8.53967351e-06 7.64906759e-10 7.38000008e-10]]
cuboid[0,0,1]: [0.00000000e+00 0.00000000e+00 7.38000007e-09]
[[ 8.53873789e-06 -1.73472348e-18 7.38000008e-10]
[ 8.53873789e-06 -1.73472348e-18 8.11800008e-09]
[ 8.53873789e-06 7.64906759e-10 7.38000008e-10]
[ 8.53873789e-06 7.64906759e-10 8.11800008e-09]
[ 8.53967351e-06 -1.73472348e-18 7.38000008e-10]
[ 8.53967351e-06 -1.73472348e-18 8.11800008e-09]
[ 8.53967351e-06 7.64906759e-10 7.38000008e-10]
[ 8.53967351e-06 7.64906759e-10 8.11800008e-09]]

when I scale up my mesh by one order of magnitude, for the same example, I get no errors:

cuboid[0,0,0]: [9.35615767e-09 7.64906762e-09 7.38000007e-09]
[[ 8.53873789e-05 1.38777878e-17 -1.38777878e-17]
[ 8.53873789e-05 1.38777878e-17 7.38000006e-09]
[ 8.53873789e-05 7.64906763e-09 -1.38777878e-17]
[ 8.53873789e-05 7.64906763e-09 7.38000006e-09]
[ 8.53967351e-05 1.38777878e-17 -1.38777878e-17]
[ 8.53967351e-05 1.38777878e-17 7.38000006e-09]
[ 8.53967351e-05 7.64906763e-09 -1.38777878e-17]
[ 8.53967351e-05 7.64906763e-09 7.38000006e-09]]
cuboid[0,0,1]: [9.35615767e-09 7.64906762e-09 7.38000007e-08]
[[8.53873789e-05 1.38777878e-17 7.38000006e-09]
[8.53873789e-05 1.38777878e-17 8.11800008e-08]
[8.53873789e-05 7.64906763e-09 7.38000006e-09]
[8.53873789e-05 7.64906763e-09 8.11800008e-08]
[8.53967351e-05 1.38777878e-17 7.38000006e-09]
[8.53967351e-05 1.38777878e-17 8.11800008e-08]
[8.53967351e-05 7.64906763e-09 7.38000006e-09]
[8.53967351e-05 7.64906763e-09 8.11800008e-08]]

This example at least implies vertices shouldn't be less than 1e-9 along any dimension from one another. Is there a way to control/know the precision of the vertices that allow/disallow forming a mesh?

Thanks again.

@mikedh
Copy link
Owner

mikedh commented Oct 15, 2024

Glad it's helpful! I think you should just need to turn off the default merge-vertex-on-creation behavior as your scale is smaller than trimesh.tol.merge (or set that value to something smaller if you'd like it to merge just at the different scale):

cuboid = trimesh.Trimesh(vertices=vertices, faces=faces, process=False) 

@mahmouddinar
Copy link
Author

I appreciate your quick feedback. Though I don't understand exactly what setting the process=False does, it avoids the previous problem which is great. But I tried setting the tolerance as you suggested to a value based on the mesh I am processing, something like:

trimesh.tol.merge = min(bb_x,bb_y,bb_z)/resolution

But should this be with process=False, or process=True, or no process flag at all?
I am creating these cuboids one by one by defining the 8 vertices and 6 faces. Does merge-vertex-on-creation even apply, or does it apply when checking the intersection of each cuboid with the mesh? I noticed that for the same mesh, when I scale it down, there are fewer intersecting cuboids which shouldn't be happening since the size of the cuboids are based on the size of the bounding box of the mesh; the proportion should remain the same.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants