ISPOINTONLINE3D Test if a 3D point belongs to a 3D line. B = isPointOnLine3d(POINT, LINE) with POINT being [xp yp zp], and LINE being [x0 y0 z0 dx dy dz]. Returns 1 if point lies on the line, 0 otherwise. If POINT is an N-by-3 array of points, B is a N-by-1 array of booleans. If LINE is a N-by-6 array of lines, B is a N-by-1 array of booleans. B = isPointOnLine3d(POINT, LINE, TOL) Specifies the tolerance used for testing location on 3D line. See also: lines3d, distancePointLine3d, linePosition3d, isPointOnLine
0001 function b = isPointOnLine3d(point, line, varargin) 0002 %ISPOINTONLINE3D Test if a 3D point belongs to a 3D line. 0003 % 0004 % B = isPointOnLine3d(POINT, LINE) 0005 % with POINT being [xp yp zp], and LINE being [x0 y0 z0 dx dy dz]. 0006 % Returns 1 if point lies on the line, 0 otherwise. 0007 % 0008 % If POINT is an N-by-3 array of points, B is a N-by-1 array of booleans. 0009 % 0010 % If LINE is a N-by-6 array of lines, B is a N-by-1 array of booleans. 0011 % 0012 % B = isPointOnLine3d(POINT, LINE, TOL) 0013 % Specifies the tolerance used for testing location on 3D line. 0014 % 0015 % See also: 0016 % lines3d, distancePointLine3d, linePosition3d, isPointOnLine 0017 % 0018 0019 % --------- 0020 % author : David Legland 0021 % e-mail: david.legland@inra.fr 0022 % INRA - TPV URPOI - BIA IMASTE 0023 % created the 31/10/2003. 0024 % 0025 0026 % HISTORY 0027 % 17/12/2013 create from isPointOnLine 0028 0029 % extract computation tolerance 0030 tol = 1e-14; 0031 if ~isempty(varargin) 0032 tol = varargin{1}; 0033 end 0034 0035 % size of inputs 0036 np = size(point,1); 0037 nl = size(line, 1); 0038 0039 if np == 1 || nl == 1 || np == nl 0040 % test if lines are colinear, using norm of the cross product 0041 b = bsxfun(@rdivide, vectorNorm3d( ... 0042 crossProduct3d(bsxfun(@minus, line(:,1:3), point), line(:,4:6))), ... 0043 vectorNorm3d(line(:,4:6))) < tol; 0044 else 0045 % same test, but after reshaping arrays to manage difference of 0046 % dimensionality 0047 point = reshape(point, [np 1 3]); 0048 line = reshape(line, [1 nl 6]); 0049 b = bsxfun(@rdivide, vectorNorm3d( ... 0050 cross(bsxfun(@minus, line(:,:,1:3), point), line(ones(1,np),:,4:6), 3)), ... 0051 vectorNorm3d(line(:,:,4:6))) < tol; 0052 end