PROJPOINTONLINE3D Project a 3D point orthogonally onto a 3D line. PT2 = projPointOnLine3d(PT, LINE). Computes the (orthogonal) projection of 3D point PT onto the 3D line LINE. Function works also for multiple points and lines. In this case, it returns multiple points. Point PT1 is a N-by-3 array, and LINE is a N-by-6 array. Result PT2 is a N-by-3 array, containing coordinates of orthogonal projections of PT1 onto lines LINE. See also: projPointOnLine, distancePointLine3d --------- author : David Legland INRA - TPV URPOI - BIA IMASTE created the 2012-08-23.
0001 function point = projPointOnLine3d(point, line) 0002 %PROJPOINTONLINE3D Project a 3D point orthogonally onto a 3D line. 0003 % 0004 % PT2 = projPointOnLine3d(PT, LINE). 0005 % Computes the (orthogonal) projection of 3D point PT onto the 3D line 0006 % LINE. 0007 % 0008 % Function works also for multiple points and lines. In this case, it 0009 % returns multiple points. 0010 % Point PT1 is a N-by-3 array, and LINE is a N-by-6 array. 0011 % Result PT2 is a N-by-3 array, containing coordinates of orthogonal 0012 % projections of PT1 onto lines LINE. 0013 % 0014 % 0015 % See also: 0016 % projPointOnLine, distancePointLine3d 0017 % 0018 % --------- 0019 % author : David Legland 0020 % INRA - TPV URPOI - BIA IMASTE 0021 % created the 2012-08-23. 0022 % 0023 0024 % HISTORY 0025 0026 % direction vector of the line 0027 vx = line(:, 4); 0028 vy = line(:, 5); 0029 vz = line(:, 6); 0030 0031 % difference of point with line origin 0032 dx = point(:,1) - line(:,1); 0033 dy = point(:,2) - line(:,2); 0034 dz = point(:,3) - line(:,3); 0035 0036 % Position of projection on line, using dot product 0037 delta = vx .* vx + vy .* vy + vz .* vz; 0038 tp = (dx .* vx + dy .* vy + dz .* vz) ./ delta; 0039 0040 % convert position on line to cartesian coordinates 0041 point = [line(:,1) + tp .* vx, line(:,2) + tp .* vy, line(:,3) + tp .* vz];