How to check whether a point is inside a mesh #136
-
I wonder if there is a way in Geogram to query whether a point is inside a mesh or not. I noticed that under third_party/PoissonRecon there is an OctNode class, which has an "isInside" member function. But I am not sure whether this is meant for this task. And if so, there is also no documentation/comments nor an example for how to use it. Any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 1 reply
-
Hi, it depends whether you need performance or not. Now if you need performance (if you have many points to test), you may use an Axis-Aligned Bbox tree to accelerate ray-triangle intersections. |
Beta Was this translation helpful? Give feedback.
-
There is an important question: do you need the result to be exact or not ? I mean, is it acceptable to have some kind of "z fighting" near the boundary ? If you do not need exactness, then you can simply If you need exact intersections, then it is a completely different story: you can no longer use A key component is this function that tells whether there is an intersection between a ray and a triangle (and whether this intersection is degenerate). You can reuse it as is (it is a template, I use it with exact points, but it will also work with If you have millions / billions points (but a rather small number of triangles in your surface, like 100K), there is another option:
If your points are organized on a grid, then it is also possible to "rasterize" the mesh in the grid, which is much much faster ! |
Beta Was this translation helpful? Give feedback.
-
Thanks! Great suggestions! I would say that rather than "exactness", "robustness" is more important in this case, e.g., if I know that errors within an epsilon near the boundary can happen, that is fine; but a few flipped triangles should not cause an issue. I should have mentioned earlier that I am working with brain images and surface meshes, and many times they come with non-manifold vertices/triangles, and many times they can be hard to fix despite the mesh repair routines in Geogram. To give a concrete example for an application, consider the signed Euclidean distance transform on an image grid. (This is not the only place I need this isInside function but it tells about the importance of robustness I think.) Until now, I have been using a combination of approaches for checking "isInside" (that includes a form a rasterization of the mesh into voxels). I have found that the approach in libigl's fast winding number is quite fast and robust to degeneracies on the mesh too. But this is the only place I am using libigl, so it would make my software lighter if I can find a Geogram replacement. I think, overall, the tetrahedralization idea can be more suitable for my needs but there I hit a license issue with tetgen. I was not able to find mg-tetra, which is mentioned in your code though, maybe it has a more permissible license(?) Can you share a link or clarify how to compile Geogram with mg-tetra? |
Beta Was this translation helpful? Give feedback.
-
The "voxelization" is really the way to go, that is, outer loop is "for each triangle" instead of "for each voxel"). It is much much faster. Why not re-implementing "fast winding numbers" ? I think it is not too difficult, and the method is fully explained here. I think it is not too much code. Another interesting list of references here About the tetrahedralization approach (but re-implementing fast winding number is probably better)
To compute To test whether a point p is inside a tet (p1,p2,p3,p4), compute the four signed volumes of (p,p2,p3,p4), (p1, p, p3, p4), (p1, p2, p, p4), (p1, p2, p3, p) and compare their signs. If they are all the same, then p is inside (p1,p2,p3,p4).
|
Beta Was this translation helpful? Give feedback.
-
Oh I did not remember that I had this code (I mean AABB for computing distance to surface + tet for inside/outside test) ready in Graphite. |
Beta Was this translation helpful? Give feedback.
-
Great! I think the function in Graphite is indeed doing all that is needed for me. If more efficiency is needed, one can look into the voxelization based idea with a fast winding number implementation too (unfortunately, I don't have the time to implement it myself now, even though that would be fun!) Thanks again for your comments. |
Beta Was this translation helpful? Give feedback.
Oh I did not remember that I had this code (I mean AABB for computing distance to surface + tet for inside/outside test) ready in Graphite.
See here
(it is not optimized, but it may be a starting point)