-
Notifications
You must be signed in to change notification settings - Fork 27
/
reorder_corners.m
223 lines (183 loc) · 5.04 KB
/
reorder_corners.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
function [pp,xx,corner_count_x]=reorder_corners(p,dx)
count = size(p,2);
if(count == 0)
pp = [];
xx = [];
return
end
if(size(p,1)~=2)
error('p must be 2xN');
end
p(3,:) = 1;
%Origin corner
ptemp = p;
[~,i] = min(ptemp(1,:));
pref = ptemp(:,i);
ptemp(:,i) = [];
[p1,i] = nearest_point(pref,ptemp);
ptemp(:,i) = [];
[line1,pline1,ptemp]=extract_line(pref,p1,ptemp);
[p2,i] = nearest_point(pref,ptemp);
ptemp(:,i) = [];
[line2,pline2,ptemp]=extract_line(pref,p2,ptemp);
[p3,i] = nearest_point(pref,ptemp);
ptemp(:,i) = [];
[line3,pline3,ptemp]=extract_line(pref,p3,ptemp);
[~,i] = max(abs([line1(1),line2(1),line3(1)]));
if(i==1)
pline = pline1;
elseif(i==2)
pline = pline2;
else
pline = pline3;
end
%Find topmost along line
[~,i] = min(pline(2,:));
pref = pline(:,i);
pline(:,i) = []; %Remove origin from line for next step
i = point_idx(pref,p); %recover index
pp = pref;
xx = [0;0;0];
p(:,i) = [];
%Find first horizontal line
ptemp = p;
i = point_idx(pline,ptemp);
ptemp(:,i) = [];
[~,i] = min(ptemp(2,:));
p1 = ptemp(:,i);
ptemp(:,i) = [];
[line,pline] = extract_line(pref,p1,ptemp);
%Add line
corner_count_x = size(pline,2);
dist2 = sum(bsxfun(@minus,pline,pref).^2);
[~,i]=sort(dist2);
pline = pline(:,i);
xi=1;
yi=0;
for i=2:size(pline,2)
[~,i] = nearest_point(pline(:,i),p);
pp = [pp, p(:,i)];
xx = [xx, dx*[xi;yi;0]];
xi = xi+1;
p(:,i) = [];
end
%Add other lines
yi=1;
while(~isempty(p))
%Get initial guess for next line
ptemp = p;
[p1,i] = nearest_point(pref,ptemp);
ptemp(:,i) = [];
line = adjust_line(line,p1);
[p2,i] = nearest_in_line(line,ptemp);
ptemp(:,i) = [];
%Get line
[line,pline] = extract_line2(p1,p2,ptemp,corner_count_x);
%Check corner count
if(corner_count_x ~= size(pline,2))
error('Found lines of different length!');
end
%Add all points on the line
[~,i]=sort(pline(1,:));
pline = pline(:,i);
xi=0;
for i=1:size(pline,2)
[~,i] = nearest_point(pline(:,i),p);
pp = [pp, p(:,i)];
xx = [xx, dx*[xi;yi;0]];
xi = xi+1;
p(:,i) = [];
end
pref = pline(:,1); %save for next line
yi=yi+1;
end
pp = pp(1:2,:);
end
function idx=point_idx(p,pset)
pcount = size(p,2);
idx = zeros(1,pcount);
for i=1:pcount
e = pset(1,:) == p(1,i) & pset(2,:) == p(2,i);
idx_i = find(e);
if(isempty(idx_i))
warning('point_not_found','Point not found.');
elseif(length(idx_i)>1)
warning('many_points','Many points found.');
else
idx(i) = idx_i;
end
end
end
function [pnearest,idx]=nearest_point(pref,p)
dif = bsxfun(@minus,p,pref);
dist = sum(dif.^2,1);
[~,idx] = min(dist);
pnearest = p(:,idx);
end
function line=points2line(pline)
%This works for two points
%line = cross(p1,p2);
%line = line/norm(line(1:2));
%This works for many points
[~,~,V]=svd(pline');
line = V(:,end) / norm(V(1:2,end));
end
function [p,idx,dist] = nearest_in_line(line,pp)
%Distance to line
fits_temp = abs(sum( bsxfun(@times, pp, line) ));
[~,idx] = min(fits_temp);
p = pp(:,idx);
dist = fits_temp(idx);
end
function [line,pline,pp]=extract_line(pref,p2,p)
%Start line with two points
pp = p;
pline = [pref,p2];
%dist_ref = sqrt(sum((pref-p2).^2));
%fit_threshold = dist_ref/10;
line = points2line(pline);
line_found=false;
%Add points to line
while(~line_found && size(pp,2)>0)
%Calculate line with new point
[~,i] = nearest_in_line(line,pp);
pline2 = [pline,pp(:,i)];
nline = points2line(pline2);
%Calculate threshold
dist_ref = sqrt(sum(bsxfun(@minus,pref,pline2(:,2:end)).^2));
fit_threshold = min(dist_ref)/10;
%Check that all points fit
fits_prev = abs(sum( bsxfun(@times, pline2, nline) )) < fit_threshold;
if(~all(fits_prev))
line_found = true;
else
line = nline;
pline = pline2;
pp(:,i) = [];
end
end
end
%This version extracts a line with the given point count
function [line,pline,pp]=extract_line2(pref,p2,p,min_pcount)
if(size(p,2)+2 < min_pcount)
error('Insufficient points to construct line.');
end
%Start line with two points
pp = p;
pline = [pref,p2];
%dist_ref = sqrt(sum((pref-p2).^2));
%fit_threshold = dist_ref/10;
line = points2line(pline);
%Add points to line
while(size(pline,2)<min_pcount)
%Calculate line with new point
[~,i] = nearest_in_line(line,pp);
pline = [pline,pp(:,i)];
line = points2line(pline);
pp(:,i) = [];
end
end
function line=adjust_line(line0,pref)
res = line0(1:2)'*pref(1:2);
line = [line0(1:2); -res];
end