-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsplitContour.m
More file actions
72 lines (61 loc) · 1.76 KB
/
splitContour.m
File metadata and controls
72 lines (61 loc) · 1.76 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
function indexpairs = splitContour(edgedistance, threshold, debug)
% function indexpairs = splitcontour(edgedistance)
%
% Takes a vector of distance products for each point on contour line. If
% contour touches an edge, want to discard all edge points after the first
% (or last), and start a new path if that contour departs from the edge
% again.
%
% Note that this function uses the "edgedistance" vector which is a product
% of the nearest distances from the given point to *each edge*! If you have
% a high order polygon that's pretty small, interior points could also fail
% this test. Make the threshold value smaller.
%
% ARR 2020.03.07
if ~exist('threshold','var') || isempty(threshold)
threshold = 1e-12;
end
if ~exist('debug','var') || isempty(debug)
debug = false;
end
startpts = [];
stoppts = [];
Npts = length(edgedistance);
if edgedistance(1)>threshold
startpts=[1];
end
for i=2:Npts
if edgedistance(i-1)<=threshold && edgedistance(i)>threshold
startpts = [startpts i-1];
end
end
if isempty(startpts)
%this shouldn't actually happen...
error('Found all edge path? Empty startpoint list? Weird.');
startpts = [1];
end
if edgedistance(end)>threshold
stoppts = [Npts];
end
for i=1:Npts-1
if edgedistance(end-i)>threshold && edgedistance(end-i+1)<=threshold
stoppts = [stoppts Npts-i+1];
end
end
if isempty(stoppts)
%this shouldn't actually happen either...
error('Found all edge path? Empty stoppoint list? Weird.');
stoppts = [Npts];
end
stoppts = flip(stoppts);
if length(startpts) ~= length(stoppts)
error('Unpaired start/stop points.');
indexpairs = [];
else
indexpairs = [startpts; stoppts];
end
% If debug flag is true, just return the full string.
if debug
indexpairs = [1; Npts];
end
end