<p>If you want to know all triangles that are hit by the ray, you have to check all of them. However, that doesn't mean that you have to check each of them individually. Usually, you apply some sort of <a href="https://en.wikipedia.org/wiki/Space_partitioning" rel="nofollow noreferrer">space partitioning</a> to your scene and group related objects in a <a href="https://en.wikipedia.org/wiki/Bounding_volume_hierarchy" rel="nofollow noreferrer">bounding volume hierarchy</a>. Then it gets a lot more efficient because you can drop multiple triangles with a single check.</p>
<p>For example, consider a scene with a rectangular area. You might subdivide this area into a 10x10 grid. Now check which parts of the grid are hit by the ray. All objects located in grid cells that aren't hit by the ray can be discarded and you only need to check the remaining objects. Now let's say that one of the hit objects is a human. Before you test the individual triangles, you narrow down the affected area in multiple substeps by checking against its <a href="https://en.wikipedia.org/wiki/Bounding_volume_hierarchy" rel="nofollow noreferrer">bounding volume hierarchy</a>. The traversal of the hierarchy might look like this: upper body hit -&gt; left arm hit -&gt; hand hit -&gt; thumb hit. So with 4 checks, you can discard all triangles of the humans' mesh that do not belong to the thumb.</p>
<p>In case you only want to find the first triangle that is hit by the ray, you can also sort all hit cells and objects by distance before performing detailed tests. Then you start your checks at the closest object and continue until you get the first hit.</p>

CC BY-SA 4.0
2020-11-10T08:33:40.933Z
May I ask if these methods also applies to secondary rays?
Spade 000
CC BY-SA 4.0
2020-11-10T09:39:55.353Z
@Spade000 yes, why shouldn't they? But you have to treat them like a new ray and retest all objects.
wychmaster
CC BY-SA 4.0
2020-11-04T08:58:57.007Z
2020-11-04T08:58:57.007Z
wychmaster
<p>I've taken a class about ray-plane intersection on this: <a href="https://www.khanacademy.org/computing/pixar/rendering/rendering-2/v/rendering-5" rel="nofollow noreferrer">https://www.khanacademy.org/computing/pixar/rendering/rendering-2/v/rendering-5</a></p>
<p>Suppose we shoot a ray at the scene and this scene contains millions of triangles. How can we know if the ray intersected one of the triangle? Do we just run a check on this millions of triangle if it intersects with the ray? That would be inefficient!!</p>

<raytracing>
How do we know if a ray intersected a triangle in one in a million
Spade 000
# How do we know if a ray intersected a triangle in one in a million

(Asked by: username_0 on 2020-11-04)

I've taken a class about ray-plane intersection on this: <https://www.khanacademy.org/computing/pixar/rendering/rendering-2/v/rendering-5>

Suppose we shoot a ray at the scene and this scene contains millions of triangles. How can we know if the ray intersected one of the triangle? Do we just run a check on this millions of triangle if it intersects with the ray? That would be inefficient!!

**Tags:** raytracing


---

## 1 answers

### Answer by username_1 on 2020-11-04. Score: 3 | Accepted answer

If you want to know all triangles that are hit by the ray, you have to check all of them. However, that doesn't mean that you have to check each of them individually. Usually, you apply some sort of [space partitioning](https://en.wikipedia.org/wiki/Space_partitioning) to your scene and group related objects in a [bounding volume hierarchy](https://en.wikipedia.org/wiki/Bounding_volume_hierarchy). Then it gets a lot more efficient because you can drop multiple triangles with a single check.

For example, consider a scene with a rectangular area. You might subdivide this area into a 10x10 grid. Now check which parts of the grid are hit by the ray. All objects located in grid cells that aren't hit by the ray can be discarded and you only need to check the remaining objects. Now let's say that one of the hit objects is a human. Before you test the individual triangles, you narrow down the affected area in multiple substeps by checking against its [bounding volume hierarchy](https://en.wikipedia.org/wiki/Bounding_volume_hierarchy). The traversal of the hierarchy might look like this: upper body hit -> left arm hit -> hand hit -> thumb hit. So with 4 checks, you can discard all triangles of the humans' mesh that do not belong to the thumb.

In case you only want to find the first triangle that is hit by the ray, you can also sort all hit cells and objects by distance before performing detailed tests. Then you start your checks at the closest object and continue until you get the first hit.

> **Comments:**
> **username_0**: May I ask if these methods also applies to secondary rays?  
> **username_1**: @username_0 yes, why shouldn't they? But you have to treat them like a new ray and retest all objects.  

---

