# Find if a triangle is inside a 3d Geometry

**URL:** <https://discourse.threejs.org/t/find-if-a-triangle-is-inside-a-3d-geometry/50914>\
**Category:** Questions\
**Tags:** textures, geometry\
**Created:** [April 27, 2023, 8:39am UTC](https://discourse.threejs.org/t/find-if-a-triangle-is-inside-a-3d-geometry/50914 "2023-04-27T08:39:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![winnip\_fr](https://avatars.discourse-cdn.com/v4/letter/w/c67d28/32.png) [@winnip\_fr](https://discourse.threejs.org/u/winnip_fr)\
**Post date:** [April 27, 2023, 8:39am UTC](https://discourse.threejs.org/t/find-if-a-triangle-is-inside-a-3d-geometry/50914/1 "2023-04-27T08:39:32Z")

</div>

Hi,

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.  
Thanks

---

<div class="post-metadata">

**Author:** ![mjurczyk](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mjurczyk/32/10995_2.png) [@mjurczyk](https://discourse.threejs.org/u/mjurczyk)\
**Post date:** [April 27, 2023, 8:43am UTC](https://discourse.threejs.org/t/find-if-a-triangle-is-inside-a-3d-geometry/50914/2 "2023-04-27T08:43:22Z")

</div>

[This](https://doc.babylonjs.com/toolsAndResources/utilities/IsInside) 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.

---

<div class="post-metadata">

**Author:** ![winnip\_fr](https://avatars.discourse-cdn.com/v4/letter/w/c67d28/32.png) [@winnip\_fr](https://discourse.threejs.org/u/winnip_fr)\
**Post date:** [April 27, 2023, 8:56am UTC](https://discourse.threejs.org/t/find-if-a-triangle-is-inside-a-3d-geometry/50914/3 "2023-04-27T08:56:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![prisoner849](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/prisoner849/32/535_2.png) [@prisoner849](https://discourse.threejs.org/u/prisoner849)\
**Post date:** [April 27, 2023, 9:08am UTC](https://discourse.threejs.org/t/find-if-a-triangle-is-inside-a-3d-geometry/50914/4 "2023-04-27T09:08:42Z")

</div>

For performant raycasting, I would recommend to use this plugin: [Three-mesh-bvh: A plugin for fast geometry raycasting and spatial queries!](https://discourse.threejs.org/t/three-mesh-bvh-a-plugin-for-fast-geometry-raycasting-and-spatial-queries/26394)  
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)](https://discourse.threejs.org/t/sand-nefertiti-three-mesh-bvh/29098)

---

<div class="post-metadata">

**Author:** ![makc3d](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/makc3d/32/10452_2.png) [@makc3d](https://discourse.threejs.org/u/makc3d)\
**Post date:** [April 27, 2023, 9:09am UTC](https://discourse.threejs.org/t/find-if-a-triangle-is-inside-a-3d-geometry/50914/5 "2023-04-27T09:09:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gkjohnson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/gkjohnson/32/2298_2.png) [@gkjohnson](https://discourse.threejs.org/u/gkjohnson)\
**Post date:** [April 27, 2023, 9:54am UTC](https://discourse.threejs.org/t/find-if-a-triangle-is-inside-a-3d-geometry/50914/6 "2023-04-27T09:54:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [April 27, 2023, 10:14am UTC](https://discourse.threejs.org/t/find-if-a-triangle-is-inside-a-3d-geometry/50914/7 "2023-04-27T10:14:10Z")

</div>

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:

![image](https://canada1.discourse-cdn.com/flex035/uploads/threejs/original/3X/e/b/eb116ed707cda57a179c78324d202f93f256342b.png)

---

<div class="post-metadata">

**Author:** ![gkjohnson](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/gkjohnson/32/2298_2.png) [@gkjohnson](https://discourse.threejs.org/u/gkjohnson)\
**Post date:** [April 27, 2023, 10:53am UTC](https://discourse.threejs.org/t/find-if-a-triangle-is-inside-a-3d-geometry/50914/8 "2023-04-27T10:53:01Z")

</div>

> 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](https://help.autodesk.com/view/MAYALT/2015/ENU/?guid=Polygons_overview_Twomanifold_vs) an article from Autodesk on the topic.

---

<div class="post-metadata">

**Author:** ![PavelBoytchev](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/pavelboytchev/32/38639_2.png) [@PavelBoytchev](https://discourse.threejs.org/u/PavelBoytchev)\
**Post date:** [April 27, 2023, 11:02am UTC](https://discourse.threejs.org/t/find-if-a-triangle-is-inside-a-3d-geometry/50914/9 "2023-04-27T11:02:32Z")

</div>

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