-
Notifications
You must be signed in to change notification settings - Fork 4
/
scanner.m
79 lines (73 loc) · 2.04 KB
/
scanner.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
function [ R,Alpha,Ls ] = scanner( theta,rho )
%Function to extract lines out of scanner measurements.
%Constants
%The line splitting dist in split.
d_thresh_split = 50;
%The lines merging dist in merge
d_thresh_merge = 100;
%The number of points needede to be considered a valid line.
num_point_thresh = 12;
%Initialise
L = {[rho',theta']};
i = 1;
R = [];
Alpha = [];
%Split
while i <= size(L,2)
temp = L{i};
if(size(temp,1)<7)%3
L = {L{1:i-1},L{i+1:end}};
continue
end
rho = temp(:,1);
theta = temp(:,2);
[r,alpha] = fitline_polar(rho',theta');
[X,Y] = pol2cart(theta,rho);
diff = abs(X(2:end-1)*cos(alpha) + Y(2:end-1)*sin(alpha) - r);
[D,I] = max(diff);
I = I+2;
if D > d_thresh_split
L{i} = [rho(1:I-1),theta(1:I-1)];
L{end+1} = [rho(I:end),theta(I:end)];
else
i = i+1;
R = [R;r];
Alpha = [Alpha;alpha];
end
end
[X,Y] = pol2cart(Alpha,R);
%Merge
for i =1:size(X,1)-1
j = i+1;
while j <= size(X,1)
dx = X(i) - X(j);
dy = Y(i) - Y(j);
if(sqrt((dx^2) + (dy^2)) < d_thresh_merge)
X = [X(1:j-1);X(j+1:end)];
Y = [Y(1:j-1);Y(j+1:end)];
temp = L{i};
temp = [temp;L{j}];
L{i} = temp;
L = {L{1:j-1},L{j+1:end}};
else
j = j+1;
end
end
end
%Fit
R = [];
Alpha = [];
Ls = [];
for i = 1:size(L,2)
temp = L{i};
if(size(temp,1) > num_point_thresh)
rho = temp(:,1);
theta = temp(:,2);
[X,Y] = pol2cart([theta(1),theta(end)],[rho(1),rho(end)]);
Ls = [Ls; [X,Y]];
[r,alpha] = fitline_polar(rho',theta');
R = [R;r];
Alpha = [Alpha;alpha];
end
end
end