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) Source: https://fr.mathworks.com/matlabcentral/fileexchange/46-iscoplanar-m
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 % Source: 0017 % https://fr.mathworks.com/matlabcentral/fileexchange/46-iscoplanar-m 0018 0019 % ------ 0020 % Author: Brett Shoelson, David Legland 0021 % E-mail: brett.shoelson@joslin.harvard.edu, david.legland@inrae.fr 0022 % Created: 2001-10-06 0023 % Copyright 2001-2024 0024 0025 if nargin == 0 0026 error('Requires at least one input argument.'); 0027 0028 elseif nargin == 1 0029 if size(x,2) == 3 0030 % Matrix of all x,y,z is input 0031 pts = x; 0032 tol = 0; 0033 else 0034 error('Invalid input.') 0035 end 0036 0037 elseif nargin == 2 0038 if size(x,2) == 3 0039 % Matrix of all x,y,z is input 0040 pts = x; 0041 tol = y; 0042 else 0043 error('Invalid input.') 0044 end 0045 elseif nargin == 3 0046 % Compile a matrix of all x,y,z 0047 pts = [x y z]; 0048 tol = 0; 0049 else 0050 pts = [x y z]; 0051 end 0052 0053 if size(x, 1) < 4 0054 error('Requires at least four points to compute coplanarity'); 0055 end 0056 0057 % replace first point at the origin and compute SVD of the matrix 0058 sv = svd(bsxfun(@minus, pts(2:end,:), pts(1,:))); 0059 copl = sv(3) <= tol * sv(1); 0060 0061 % % Alterantive version that computes the rank of the matrix 0062 % rnk = rank(bsxfun(@minus, pts(2:end,:), pts(1,:)), tol); 0063 % copl = rnk <= size(pts, 2) - 1; 0064 0065 % % Old version: 0066 % %Compare all 4-tuples of point combinations; {P1:P4} are coplanar iff 0067 % %det([x1 y1 z1 1;x2 y2 z2 1;x3 y3 z3 1;x4 y4 z4 1])==0 0068 % tmp = nchoosek(1:size(pts,1),4); 0069 % for ii = 1:size(tmp,1) 0070 % copl = abs(det([pts(tmp(ii, :), :) ones(4,1)])) <= tolerance; 0071 % if ~copl 0072 % break 0073 % end 0074 % end