Find if a triangle is inside a 3d Geometry


Here is the plot: I have a quite complex 3D triangulated geometry and a 2D triangulated geometry.
The 2D geometry can be moved. I’d like to render the triangle of 2D geometry if completly inside the 3D geometry with a given color, if it is completly ouside with another color.
Is anyone has an idea or a pointer on how to archieve this?
The geometry are quite complex, using a bounding box is unfortunately not enough to be used.

This should be able to help you.

The guide is for Babylon - but the idea is quite literally universal. Just raycast from each vertex of the triangle and check the amount of intersections with the mesh - if it’s odd, the point is inside, if it’s even, the point is outside.

Ok, thanks for this link. It’s indeed a nice solution.
Do you know once I checked if a triangle is inside or ouside the mesh, if I can color with two colors?
Sould I create 2 geometries, one where all triangles are inside the mesh, one with all triangles ouside the mesh?

For performant raycasting, I would recommend to use this plugin: Three-mesh-bvh: A plugin for fast geometry raycasting and spatial queries!
With .firstHitOnly = true I get the normal of intersected face and find the dot-product of it with raycaster’s ray. If the result is greater then 0, the point is inside.
Example of the using: Sand Nefertiti (three-mesh-bvh)

1 Like

note that that solution only works for closed water-tight (basically 3d-printable) meshes. for the normal meshes there is always non-0 probability of it failing.


Note that it’s also not enough to just check the three triangle vertices since the triangle could be positioned such that they are inside the mesh but the center of the triangle is outside of it if the geometry is not convex. If the triangle does not intersect the geometry and the center of the triangle is within the geometry it should be able to be considered completely contained.

Also - as @makc3d has already been mentioned - the mesh would need to be watertight.


Theoretically, the requirements are more strict. For example, the odd-even algorithm does not work when an edge is shared by three faces. Here is en example with internal wall:


the odd-even algorithm does not work when an edge is shared by three faces.

Yeah the word “watertight” is not so intuitive but it seems to often be used as short hand for “two-manifold” geometry - which disallows three faces sharing an edge. Here’s an article from Autodesk on the topic.


Indeed it is so. “Watertight” is quite misleading as the CG meaning is different from the linguistic meaning of the word.