ANGLESORT3D Sort 3D coplanar points according to their angles in plane. PTS2 = angleSort3d(PTS); Considers all points are located on the same plane, and sort them according to the angle on plane. PTS is a [Nx2] array. Note that the result depends on the plane orientation: points can be in reverse order compared to expected. The reference plane is computed based on the first three points. PTS2 = angleSort3d(PTS, PTS0); Computes angles between each point of PTS and PT0. By default, uses centroid of points. PTS2 = angleSort3d(PTS, PTS0, PTS1); Specifies the point which will be used as a start. [PTS2, I] = angleSort3d(...); Also return in I the indices of PTS, such that PTS2 = PTS(I, :); See also: points3d, angles3d, angleSort ------ Author: David Legland e-mail: david.legland@grignon.inra.fr Created: 2005-11-24 Copyright 2005 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas).
0001 function varargout = angleSort3d(pts, varargin) 0002 %ANGLESORT3D Sort 3D coplanar points according to their angles in plane. 0003 % 0004 % PTS2 = angleSort3d(PTS); 0005 % Considers all points are located on the same plane, and sort them 0006 % according to the angle on plane. PTS is a [Nx2] array. Note that the 0007 % result depends on the plane orientation: points can be in reverse order 0008 % compared to expected. The reference plane is computed based on the 0009 % first three points. 0010 % 0011 % PTS2 = angleSort3d(PTS, PTS0); 0012 % Computes angles between each point of PTS and PT0. By default, uses 0013 % centroid of points. 0014 % 0015 % PTS2 = angleSort3d(PTS, PTS0, PTS1); 0016 % Specifies the point which will be used as a start. 0017 % 0018 % [PTS2, I] = angleSort3d(...); 0019 % Also return in I the indices of PTS, such that PTS2 = PTS(I, :); 0020 % 0021 % See also: 0022 % points3d, angles3d, angleSort 0023 % 0024 % ------ 0025 % Author: David Legland 0026 % e-mail: david.legland@grignon.inra.fr 0027 % Created: 2005-11-24 0028 % Copyright 2005 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas). 0029 0030 0031 % HISTORY : 0032 % 04/01/2007: remove unused variables 0033 0034 % default values 0035 pt0 = mean(pts, 1); 0036 pt1 = pts(1,:); 0037 0038 if length(varargin)==1 0039 pt0 = varargin{1}; 0040 elseif length(varargin)==2 0041 pt0 = varargin{1}; 0042 pt1 = varargin{2}; 0043 end 0044 0045 % create support plane 0046 plane = createPlane(pts(1:3, :)); 0047 0048 % project points onto the plane 0049 pts2d = planePosition(pts, plane); 0050 pt0 = planePosition(pt0, plane); 0051 pt1 = planePosition(pt1, plane); 0052 0053 % compute origin angle 0054 theta0 = atan2(pt1(2)-pt0(2), pt1(1)-pt0(1)); 0055 theta0 = mod(theta0 + 2*pi, 2*pi); 0056 0057 % translate to reference point 0058 n = size(pts, 1); 0059 pts2d = pts2d - repmat(pt0, [n 1]); 0060 0061 % compute angles 0062 angle = atan2(pts2d(:,2), pts2d(:,1)); 0063 angle = mod(angle - theta0 + 4*pi, 2*pi); 0064 0065 % sort points according to angles 0066 [angle, I] = sort(angle); %#ok<ASGLU> 0067 0068 0069 % format output 0070 if nargout<2 0071 varargout{1} = pts(I, :); 0072 elseif nargout==2 0073 varargout{1} = pts(I, :); 0074 varargout{2} = I; 0075 end 0076