forked from meyiao/LaserSLAM
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FastMatch.m
83 lines (60 loc) · 1.89 KB
/
FastMatch.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
% Fast scan matching, note this may get stuck in local minimas
function [pose, bestHits] = FastMatch(gridmap, scan, pose, searchResolution)
% Grid map information
metricMap = gridmap.metricMap;
ipixel = 1 / gridmap.pixelSize;
minX = gridmap.topLeftCorner(1);
minY = gridmap.topLeftCorner(2);
nCols = size(metricMap, 2);
nRows = size(metricMap, 1);
% Go down the hill
maxIter = 50;
maxDepth = 3;
iter = 0;
depth = 0;
pixelScan = scan * ipixel;
bestPose = pose;
bestScore = Inf;
t = searchResolution(1);
r = searchResolution(3);
while iter < maxIter
noChange = true;
% Rotation
for theta = pose(3) + [-r, 0, r]
ct = cos(theta);
st = sin(theta);
S = pixelScan * [ct, st; -st, ct];
% Translation
for tx = pose(1) + [-t, 0, t]
Sx = round(S(:,1)+(tx-minX)*ipixel) + 1;
for ty = pose(2) + [-t, 0, t]
Sy = round(S(:,2)+(ty-minY)*ipixel) + 1;
isIn = Sx>1 & Sy>1 & Sx<nCols & Sy<nRows;
ix = Sx(isIn);
iy = Sy(isIn);
% metric socre
idx = iy + (ix-1)*nRows;
hits = metricMap(idx);
score = sum(hits);
% update
if score < bestScore
noChange = false;
bestPose = [tx; ty; theta];
bestScore = score;
bestHits = hits;
end
end
end
end
% No better match was found, increase resolution
if noChange
r = r / 2;
t = t / 2;
depth = depth + 1;
if depth > maxDepth
break;
end
end
pose = bestPose;
iter = iter + 1;
end