-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_gap.m
54 lines (53 loc) · 1.5 KB
/
fix_gap.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function [xyz,idx,npts] = fix_gap(xyz)
%FIX_GAP Checks distance between 2-D or 3-D points and ensures the
% greatest distance is between the first and last points.
%
% XYZ = FIX_GAP(XYZ) given a two (2) or three (3) columns
% matrix with coordinate point data, XYZ, returns the matrix,
% XYZ, with the points reordered so the greatest distance
% between points is between the first and last points.
%
% [XYZ,IDX] = FIX_GAP(XYZ) The index, IDX, is returned such
% that the returned XYZ = the input XYZ(IDX,:).
%
% [XYZ,IDX,NPTS] = FIX_GAP(XYZ) The number of points, NPTS,
% may also be returned.
%
% NOTES: None.
%
% 31-May-2022 * Mack Gardner-Morse
%
%#######################################################################
%
% Check for Input
%
if (nargin<1)
error(' *** ERROR in FIX_GAP: Must have input points coordinates!');
end
%
[npts,ncol] = size(xyz);
%
if npts<2
error(' *** ERROR in FIX_GAP: Data must have at least two points!');
end
%
if ncol~=2&&ncol~=3
error(' *** ERROR in FIX_GAP: Data must have two or three columns!');
end
%
% Find Distances and Maximum Distance
%
d = diff([xyz; xyz(1,:)]);
d = sum(d.*d,2); % Distances squared
%
[~,idmx] = max(d); % Location of maximum distance
%
% Check Maximum Distance Location and Reorder Points if Necessary
%
idx = (1:npts)';
if idmx~=npts
idx = [idmx+1:npts 1:idmx]';
xyz = xyz(idx,:);
end
%
return