File Exchange

## Triangle/Ray Intersection

version 1.7 (309 KB) by Jaroslaw Tuszynski

### Jaroslaw Tuszynski (view profile)

Fast vectorized triangle/ray intersection algorithm

Updated 18 May 2018

Ray/triangle intersection using the algorithm proposed by Möller and
Trumbore (1997), implemented as highly vectorized MATLAB code.
The algorithm can work with one and two sided surfaces, as well as, with
infinite lines, rays (lines bounded on one side) and segments (lines bounded on
both sides).
Input (all arrays in in Nx3, where N is number of vertices or rays):
orig : ray's origin
dir : ray's direction
vert0, vert1, vert2: vertices of the triangle
Output:
Intersect - boolean array of length N
t - distance from the ray origin to the intersection point in |dir|
u,v - barycentric coordinates of the intersection point units
xcoor - carthesian coordinates of the intersection point
In addition PointInsideVolume is 3D equivalent to 2D inpolygon function and can test if
array of points is inside or outside any volume defined by the surface grid.

Philip Kunz

Fabricio Castro

### Fabricio Castro (view profile)

Jaroslaw Tuszynski

### Jaroslaw Tuszynski (view profile)

Reply to jack' green: I think you should be able to find much simpler codes to do 2D case. Or just write one to do line/line intersection for ray intersection with each line segment and see if the intersection point is between each segment end points.

jack' green

### jack' green (view profile)

Hi, Jaroslaw. Thanks for provding such a cool tool. It works well on my computer. Just wonder is it possible to apply it to a purely 2D (in-plane) case where the triangles all become segments?

Yanjun Han

### Yanjun Han (view profile)

many faces / many rays intersection, the return value 'flag'? Should it be a matrix but actual it is not.

Yanjun Han

### Yanjun Han (view profile)

Hello, Can anyone help me! Now I have a series of path points, and I want project them on a specified mesh surface along the specified direction, for example dir=(0,0,1). For every path point, I can iterate over all the triangular meshes to get the corresponding projection points，but this is too time-consuming. Can I use a faster method？
I want to get the projection point of all the points in the fixed direction on the triangular mesh surface. How can I use the 'many faces / many rays intersection' ?

Ander Biguri

### Ander Biguri (view profile)

Does the many rays-many faces case only work with Nfaces==Nrays? If so, is there any specific reason for implementing that specific case?

Yumin Sun

### Yumin Sun (view profile)

Actually for multiple faces and multiple rays, one should remember you need to repmat origin, if all origins are same.

Leonardo Colavitti

Igor

### Igor (view profile)

To do your program better I suggests
1) Add description about use option FullReturn. It shold be '1' but not 'true' for example.
2) Add in initialize default output string
xcoor=zeros(size(orig));
If xcoor not initialized than if no intersections error appear with params count.

Jamie Heather

### Jamie Heather (view profile)

Nice vectorised implementation with handy options for choosing between ray/line/segments and handling numerical precision issues - thanks!

Jaroslaw Tuszynski

### Jaroslaw Tuszynski (view profile)

Reply to Murat: You might have to look into using "border" parameter. If it does not work can you construct a scenario that demonstrates the issue and email it to me

Murat

### Murat (view profile)

Hi, the code is working well but it sometimes cannot handle with large incident angles. When the incident angle is around 90, it ignores triangles. Could you correct it?

Ehsan golk

Jeffrey

### Jeffrey (view profile)

I'm having trouble with the many rays, many faces option. Lets say I have 11 faces and 5 rays. What resizing of the inputs needs to be done? Do I need to repmat all arrays to be of size 55x3?

Based on what I can tell it can handle:
- one ray, many faces
- many rays, one face
- many rays, many faces (but # of faces = # of rays)

But I think I am just missing something here.

Jaroslaw Tuszynski

### Jaroslaw Tuszynski (view profile)

Reply to Nick: In many rays and many triangle case you still test for intersection of prearranged pairs and the return value informs you which ray/triangle pair intersected

Nick

### Nick (view profile)

Very useful but when considering many rays and many triangles is there a way to find which ray has intersected which triangle?

Nick

Amilcar Torres

Ben

### Ben (view profile)

Jaroslaw Tuszynski

### Jaroslaw Tuszynski (view profile)

Re to Anton Semechko: This is a low level function which can be called with one ray and many faces, many faces and one ray or many faces and rays. It is quite simple to apply repmat function to the inputs.

Anton Semechko

### Anton Semechko (view profile)

why does the number of rays have to equal the number of faces? that's so inconvenient

 18 May 2018 1.7 small corrections to the interface 25 Sep 2017 1.6.0.0 Minimal changes suggested by Igor in Comments and Ratings 15 Jun 2016 1.5.0.0 correct description: no symbolic toolbox is needed 3 Oct 2014 1.5.0.0 Major rewrite of the function with additional options and output variables. Also provided PointInsideVolume function 7 Jun 2013 1.4.0.0 documentation improvements and typo correction 20 Mar 2013 1.2.0.0 correct treatment of 3x3 arrays as suggested by Andreas Weber 7 Oct 2011 1.1.0.0 Improvements to border handling
##### MATLAB Release Compatibility
Created with R2017b
Compatible with any release
##### Platform Compatibility
Windows macOS Linux
##### Acknowledgements

Inspired by: Ray/Triangle Intersection

Inspired: in_polyhedron, RayShapeArticle_FEX.zip

### Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.