%% Use Scan Matching to Transform Scans
% Use the matchScans function to find the relative transformation between two lidar scans. Then, transform the second scan into the coordinate frame of the first scan.
% Load a pair of lidar scans as a pair of lidarScan objects. They are two scans of the same scene with a change in relative pose.

load tb3_scanPair.mat
plot(s1)
hold on
plot(s2)
hold off

% The relative pose is estimated from an odometry sensor and provided as a variable, initGuess,  as [x y theta].

disp(initGuess)

%    -0.7000    0.1500   -0.3254
% Use scan matching to find the relative pose between the two laser scans and specify the intial guess for the pose.

pose = matchScans(s2,s1,"InitialPose",initGuess);

% pose = 1×3
%    -0.7213    0.1160   -0.2854
%
% Transform the second scan to the coordinate frame of the first scan. Plot the two scans to see that they now overlap.

s2Transformed = transformScan(s2,pose);
plot(s1)
hold on
plot(s2Transformed)
hold off

%
% Copyright 2019 The MathWorks, Inc.