INTERSECTRAYPOLYGON3D Intersection point of a 3D ray and a 3D polygon. INTER = intersectRayPolygon3d(RAY, POLY) Compute coordinates of intersection point between the 3D ray RAY and the 3D polygon POLY. RAY is a 1-by-6 row vector containing origin and direction vector of the ray, POLY is a Np-by-3 array containing coordinates of 3D polygon vertices. INTER is a 1-by-3 row vector containing coordinates of intersection point, or [NaN NaN NaN] if ray and polygon do not intersect. INTERS = intersectRayPolygon3d(RAYS, POLY) If RAYS is a N-by-6 array representing several rays, the result INTERS is a N-by-3 array containing coordinates of intersection of each ray with the polygon. [INTER INSIDE] = intersectRayPolygon3d(RAY, POLY) Also return a N-by-1 boolean array containing TRUE if both the polygon and the corresponding ray contain the intersection point. Example % Compute intersection between a 3D ray and a 3D triangle pts3d = [3 0 0; 0 6 0;0 0 9]; ray1 = [0 0 0 3 6 9]; inter = intersectRayPolygon3d(ray1, pts3d) inter = 1 2 3 % keep only valid intersections with several rays pts3d = [3 0 0; 0 6 0;0 0 9]; rays = [0 0 0 3 6 9;10 0 0 1 2 3;3 6 9 3 6 9]; [inter inside] = intersectRayPolygon3d(rays, pts3d); inter(inside, :) ans = 1 2 3 See also intersectRayPolygon, intersectLinePolygon3d, intersectLineTriangle3d
0001 function [inter, inside]= intersectRayPolygon3d(ray, poly) 0002 %INTERSECTRAYPOLYGON3D Intersection point of a 3D ray and a 3D polygon. 0003 % 0004 % INTER = intersectRayPolygon3d(RAY, POLY) 0005 % Compute coordinates of intersection point between the 3D ray RAY and 0006 % the 3D polygon POLY. RAY is a 1-by-6 row vector containing origin and 0007 % direction vector of the ray, POLY is a Np-by-3 array containing 0008 % coordinates of 3D polygon vertices. 0009 % INTER is a 1-by-3 row vector containing coordinates of intersection 0010 % point, or [NaN NaN NaN] if ray and polygon do not intersect. 0011 % 0012 % INTERS = intersectRayPolygon3d(RAYS, POLY) 0013 % If RAYS is a N-by-6 array representing several rays, the result 0014 % INTERS is a N-by-3 array containing coordinates of intersection of each 0015 % ray with the polygon. 0016 % 0017 % [INTER INSIDE] = intersectRayPolygon3d(RAY, POLY) 0018 % Also return a N-by-1 boolean array containing TRUE if both the polygon 0019 % and the corresponding ray contain the intersection point. 0020 % 0021 % Example 0022 % % Compute intersection between a 3D ray and a 3D triangle 0023 % pts3d = [3 0 0; 0 6 0;0 0 9]; 0024 % ray1 = [0 0 0 3 6 9]; 0025 % inter = intersectRayPolygon3d(ray1, pts3d) 0026 % inter = 0027 % 1 2 3 0028 % 0029 % % keep only valid intersections with several rays 0030 % pts3d = [3 0 0; 0 6 0;0 0 9]; 0031 % rays = [0 0 0 3 6 9;10 0 0 1 2 3;3 6 9 3 6 9]; 0032 % [inter inside] = intersectRayPolygon3d(rays, pts3d); 0033 % inter(inside, :) 0034 % ans = 0035 % 1 2 3 0036 % 0037 % See also 0038 % intersectRayPolygon, intersectLinePolygon3d, intersectLineTriangle3d 0039 % 0040 0041 % ------ 0042 % Author: David Legland 0043 % E-mail: david.legland@inrae.fr 0044 % Created: 2011-05-22, using Matlab 7.9.0.529 (R2009b) 0045 % Copyright 2011-2024 INRA - Cepia Software Platform 0046 0047 % supporting plane of polygon vertices 0048 plane = createPlane(poly(1:3, :)); 0049 0050 % intersection of 3D ray with the plane 0051 inter = intersectLinePlane(ray, plane); 0052 0053 % project all points on reference plane 0054 pts2d = planePosition(projPointOnPlane(poly, plane), plane); 0055 pInt2d = planePosition(projPointOnPlane(inter, plane), plane); 0056 0057 % need to check polygon orientation 0058 inPoly = xor(isPointInPolygon(pInt2d, pts2d), polygonArea(pts2d) < 0); 0059 onRay = line3dPosition(inter, ray) >= 0; 0060 inside = inPoly & onRay; 0061 0062 % intersection points outside the polygon are set to NaN 0063 inter(~inside, :) = NaN;