Hardware accelerated ray-triangle intersection

version (35.6 KB) by Thomas
GPU portable implementation of the ray-triangle intersection method of Moller and Trumbore (1997)


Updated 13 Feb 2015

View License

% Ray-triangle intersection algorithm of Muller and Trumbore (1997)
% formatted for arrayfun to allow hardware acceleration
% Call with gpuarray and arrayfun to execute on the GPU: thjs
% may give two orders of magnitude speed up over vectorized
% cpu based implementation
% P0x, P0y, P0z / P1x, P1y, P1z / P2x, P2y, P2z: xyz components of the
% triangle objects
% orx, ory, orz: xyz componants of the ray origin
% Dx, Dy, Dz: xyz components of the ray directional (unit) vectors
% distOut: distance from from the ray-tri intersection to the origin or nan
% if no intersection is located
% flag: logical where true indicates intersection and false indicates
% no intersection
% Usage example:
% Step 1: convert mx3 direction vectors, D = [Dx Dy Dz] to gpuarray object
% >> gD = gpuArray(D);
% Step 2: call rayTriGPU using arrayfun with scalar input formatting
% where P0, P1, P2 are the nx3 vertex lists of the triangle corner points
% and where or is the xyz coordinates of the origin
% >> [dist, flag] = arrayfun(@rayTriGPU, P0(:,1)', P0(:,2)', P0(:,3)', ...
% P1(:,1)', P1(:,2)', P1(:,3)', ...
% P2(:,1)', P2(:,2)', P2(:,3)', ...
% or(:,1), or(:,2), or(:,3), ...
% gD(:,1),gD(:,2),gD(:,3));
% Step 3: recover data
% distances = gather(dist);
% Output is an mxn array containing a the distance from the ray-tri
% intersection to the origin or nan if no intersection is located
% Implentation based upon that of Paul Peeling (originally from Jesus P.
% Mena-Chalco of FEX), MathWorks (which returns a flag but not the
% intersection distance).

% Per ray flags can be obtained from the output dist using the following
% method:
% >> flagT = true(size(D,1),1);
% >> flagT(sum(isnan(dist),2) == size(P0,1)) = false;
% This may save transfer time off the GPU

% Dependencies: requires Parallel Computing Toolbox
% Test data (testDataTri.mat) is provided with the package

% references
% Fast, Minimum Storage Ray/Triangle Intersection, Möller & Trumbore.
% Journal of Graphics Tools, 1997.

Cite As

Thomas (2022). Hardware accelerated ray-triangle intersection (https://www.mathworks.com/matlabcentral/fileexchange/49670-hardware-accelerated-ray-triangle-intersection), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Ray/Triangle Intersection, RayShapeArticle_FEX.zip

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!