Return the position of a 3D point projected on a 3D line. T = linePosition3d(POINT, LINE) Computes position of point POINT on the line LINE, relative to origin point and direction vector of the line. LINE has the form [x0 y0 z0 dx dy dy], POINT has the form [x y z], and is assumed to belong to line. The result T is the value such that POINT = LINE(1:3) + T * LINE(4:6). If POINT does not belong to LINE, the position of its orthogonal projection is computed instead. T = linePosition3d(POINT, LINES) If LINES is an array of NL lines, return NL positions, corresponding to each line. T = linePosition3d(POINTS, LINE) If POINTS is an array of NP points, return NP positions, corresponding to each point. See also: lines3d, createLine3d, distancePointLine3d, projPointOnLine3d --------- author : David Legland INRA - TPV URPOI - BIA IMASTE created the 17/02/2005.
0001 function pos = linePosition3d(point, line) 0002 % Return the position of a 3D point projected on a 3D line. 0003 % 0004 % T = linePosition3d(POINT, LINE) 0005 % Computes position of point POINT on the line LINE, relative to origin 0006 % point and direction vector of the line. 0007 % LINE has the form [x0 y0 z0 dx dy dy], 0008 % POINT has the form [x y z], and is assumed to belong to line. 0009 % The result T is the value such that POINT = LINE(1:3) + T * LINE(4:6). 0010 % If POINT does not belong to LINE, the position of its orthogonal 0011 % projection is computed instead. 0012 % 0013 % T = linePosition3d(POINT, LINES) 0014 % If LINES is an array of NL lines, return NL positions, corresponding to 0015 % each line. 0016 % 0017 % T = linePosition3d(POINTS, LINE) 0018 % If POINTS is an array of NP points, return NP positions, corresponding 0019 % to each point. 0020 % 0021 % See also: 0022 % lines3d, createLine3d, distancePointLine3d, projPointOnLine3d 0023 % 0024 % --------- 0025 % author : David Legland 0026 % INRA - TPV URPOI - BIA IMASTE 0027 % created the 17/02/2005. 0028 % 0029 0030 % HISTORY 0031 % 05/01/2007 update doc 0032 % 28/10/2010 change to bsxfun calculation for arbitrary input sizes 0033 % (Thanks to Sven Holcombe) 0034 0035 % size of input arguments 0036 np = size(point, 1); 0037 nl = size(line, 1); 0038 0039 if np == 1 || nl == 1 || np == nl 0040 % standard case where result is either scalar or vector 0041 0042 % vector from line origin to point 0043 dp = bsxfun(@minus, point, line(:,1:3)); 0044 0045 % direction vector of the line 0046 vl = line(:, 4:6); 0047 0048 % precompute and check validity of denominator 0049 denom = sum(vl.^2, 2); 0050 invalidLine = denom < eps; 0051 denom(invalidLine) = 1; 0052 0053 % compute position using dot product normalized with norm of line vector. 0054 pos = bsxfun(@rdivide, sum(bsxfun(@times, dp, vl), 2), denom); 0055 0056 % position on a degenerated line is set to 0 0057 pos(invalidLine) = 0; 0058 0059 else 0060 % reshape input 0061 point = reshape(point, [np 1 3]); 0062 line = reshape(line, [1 nl 6]); 0063 0064 % vector from line origin to point 0065 dp = bsxfun(@minus, point, line(:,:,1:3)); 0066 0067 % direction vector of the line 0068 vl = line(:, :, 4:6); 0069 0070 % precompute and check validity of denominator 0071 denom = sum(vl.^2, 3); 0072 invalidLine = denom < eps; 0073 denom(invalidLine) = 1; 0074 0075 % compute position using dot product normalized with norm of line vector. 0076 pos = bsxfun(@rdivide, sum(bsxfun(@times, dp, vl), 3), denom); 0077 0078 % position on a degenerated line is set to 0 0079 pos(invalidLine) = 0; 0080 end