-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsplitContourPlus.m
More file actions
121 lines (100 loc) · 2.87 KB
/
splitContourPlus.m
File metadata and controls
121 lines (100 loc) · 2.87 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
function indexpairs = splitContourPlus(hullpoints, tric, threshold, debug)
% function indexpairs = splitContourPlus(edgedistance, points, hull, threshold, debug)
%
% ARR 2021.03.24
if ~exist('threshold','var') || isempty(threshold)
threshold = 1e-8;
end
if ~exist('debug','var') || isempty(debug)
debug = false;
end
hullcycle = [hullpoints; hullpoints(1)];
contourlength = tric(2,n);
pts = tric(:,n+1:n+contourlength-1);
tracker = zeros(length(pts),length(hulln));
% Calculate the distance from a particular point to each
% edge as defined by the convex hull.
for hull=1:length(hullpoints)
A = hullcycle(hull);
B = hullcycle(hull+1);
AB = (B-A);
ABhat = AB/norm(AB);
for pt=1:length(pts)
AC = [pts(1,pt)-A(1), pts(2,pt)-A(2)];
D = abs(ABhat(1)*AC(2)-ABhat(2)*AC(1));
tracker(pt,hull) = D;
end
end
% Select the shortest of those distances, and use that to tell
% which points are "running along an edge".
closest = min(tracker,[],2);
% find which points are actually closest to the edge.
[~,locb] = ismember(closest,tracker);
nelements = length(locb);
% find the column that minimum belongs to (this is the edge number).
edgenum = (locb-[1:nelements]')/nelements+1;
% Now look for points that are within some threshold distance to the same
% edge.
for i=1:nelements
end
subpaths = splitContour(tracker,[],[]);
[~,pathpairs] = size(subpaths);
% split the contours when they hit an edge.
for splitindex=1:pathpairs
temppath = pts(:,subpaths(1,splitindex):subpaths(2,splitindex));
[reverse, votes] = checkDirection(x, y, Vm, temppath, level, 0.2,verbose,[]);
if reverse
temppath = fliplr(temppath);
end
if debug
if abs(mean(votes))<0.5
debugcell{i}{j} = votes;
end
end
if isempty(contours{j})
contours{j}={temppath};
else
contours{j}=[contours{j}(:)' {temppath}];
end
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