ISCOPLANAR Tests input points for coplanarity in 3-space. COPL = isCoplanar(PTS) Tests the coplanarity of the input points in array PTS. Input array must be 4-by-3, each row containing coordinate of one point. COPL = isCoplanar(PTS, TOLERANCE) Specifies the tolerance value used for checking coplanarity. Default is zero. Example: iscoplanar([1 2 -2; -3 1 -14; -1 2 -6; 1 -2 -8], eps)
0001 function copl = isCoplanar(x,y,z,tol) 0002 %ISCOPLANAR Tests input points for coplanarity in 3-space. 0003 % 0004 % COPL = isCoplanar(PTS) 0005 % Tests the coplanarity of the input points in array PTS. Input array must 0006 % be 4-by-3, each row containing coordinate of one point. 0007 % 0008 % COPL = isCoplanar(PTS, TOLERANCE) 0009 % Specifies the tolerance value used for checking coplanarity. Default is 0010 % zero. 0011 % 0012 % 0013 % Example: 0014 % iscoplanar([1 2 -2; -3 1 -14; -1 2 -6; 1 -2 -8], eps) 0015 0016 % 0017 % Adapted from a function originally written by Brett Shoelson, Ph.D. 0018 % brett.shoelson@joslin.harvard.edu 0019 % https://fr.mathworks.com/matlabcentral/fileexchange/46-iscoplanar-m 0020 % 0021 0022 if nargin == 0 0023 error('Requires at least one input argument.'); 0024 0025 elseif nargin == 1 0026 if size(x,2) == 3 0027 % Matrix of all x,y,z is input 0028 pts = x; 0029 tol = 0; 0030 else 0031 error('Invalid input.') 0032 end 0033 0034 elseif nargin == 2 0035 if size(x,2) == 3 0036 % Matrix of all x,y,z is input 0037 pts = x; 0038 tol = y; 0039 else 0040 error('Invalid input.') 0041 end 0042 elseif nargin == 3 0043 % Compile a matrix of all x,y,z 0044 pts = [x y z]; 0045 tol = 0; 0046 else 0047 pts = [x y z]; 0048 end 0049 0050 if size(x, 1) < 4 0051 error('Requires at least four points to compute coplanarity'); 0052 end 0053 0054 % replace first point at the origin and compute SVD of the matrix 0055 sv = svd(bsxfun(@minus, pts(2:end,:), pts(1,:))); 0056 copl = sv(3) <= tol * sv(1); 0057 0058 % % Alterantive version that computes the rank of the matrix 0059 % rnk = rank(bsxfun(@minus, pts(2:end,:), pts(1,:)), tol); 0060 % copl = rnk <= size(pts, 2) - 1; 0061 0062 % % Old version: 0063 % %Compare all 4-tuples of point combinations; {P1:P4} are coplanar iff 0064 % %det([x1 y1 z1 1;x2 y2 z2 1;x3 y3 z3 1;x4 y4 z4 1])==0 0065 % tmp = nchoosek(1:size(pts,1),4); 0066 % for ii = 1:size(tmp,1) 0067 % copl = abs(det([pts(tmp(ii, :), :) ones(4,1)])) <= tolerance; 0068 % if ~copl 0069 % break 0070 % end 0071 % end