% Ray-box intersection algorithm of Smit (1998) formatted for arrayfun to
% allow hardware acceleration:
% Smits, B. (1998). Efficiency issues for ray tracing. Journal of Graphics
% Tools, 3(2):1–14.
% Call with gpuarray and arrayfun to execute on the GPU: this
% may give two orders of magnitude speed up over vectorized
% cpu based implementation
% Ray box intersection is typically used to locate (axis aligned) spatial
% bins (e.g. octree bins / regular grid) to optimise ray-tracing (i.e. by
% reducing the number of potential triangles
% INPUT (scalar):
% orx, ory, orz: xyz componants of the ray origin
% Dx, Dy, Dz: xyz components of the ray directional (unit) vectors
% minx, miny, minz: xyz componants of the box minima
% maxx, maxy, maxz: xyz componants of the box maxima
% OUTPUT (scalar:
% tmin: minimum distance from from the ray-box intersection to the
% origin or nan if no intersection is located
% flag: 1 if intersection / 0 if no intersection
% Usage example:
% Step 1: convert mx3 direction vectors, D = [Dx Dy Dz] to gpuarray object
% >> gD = gpuArray(D);
% Step 2: call rayBoxGPU using arrayfun with scalar input formatting
% where min, max are the nx3 vertex lists of the box min-max corner points
% and where or is the xyz coordinates of the origin
% >> [tmin, flag] = arrayfun(@rayBoxGPU, min(:,1)', min(:,2)', min(:,3)', ...
% max(:,1)', max(:,2)', max(:,3)', ...
% or(:,1), or(:,2), or(:,3), ...
% Step 3: recover data
% distmin = gather(tmin);
% flagBox = gather(flag);
% Output is one mxn array containing a the distance from the ray-box
% intersection to the origin or nan if no intersection is located (distmin)
% and one mxn logical containing flags for ray-box intersections.
% Per-ray flags can be obtained from the output tmin using the following
% >> flagB = true(size(D,1),1);
% >> flagB(sum(isnan(tmin),2) == size(min,1)) = false;
% This may save transfer time off the GPU
% Dependencies: requires Parallel Computing Toolbox
% Based upon the implementation by Jesus P. Mena-Chalco
% Test data (testDataBox.mat) is provided with the package
Thomas (2021). Hardware accelerated ray-box intersection (https://www.mathworks.com/matlabcentral/fileexchange/49671-hardware-accelerated-ray-box-intersection), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!