%% Gather Label Data and Write Associated Signal Frames
% Gather label data for a video signal and a lidar point cloud sequence
% signal from a <docid:driving_ref#mw_42782d95-09f4-4844-91ee-44ddd8357f59
% |groundTruthMultisignal|> object. Write the signal frames associated with
% that label data to disk and visualize the frames.

% Copyright 2019 The MathWorks, Inc.
%%
% Add the point cloud sequence folder path to the MATLAB(R) search
% path. The video is already on the MATLAB search path.

pcSeqDir = fullfile(toolboxdir('driving'),'drivingdata', ...
    'lidarSequence');
addpath(pcSeqDir);
%% 
% Load a |groundTruthMultisignal| object that contains label data for the
% video and the lidar point cloud sequence.

data = load('MultisignalGTruth.mat');
gTruth = data.gTruth;
%% 
% Specify the signals from which to gather label data.

signalNames = ["video_01_city_c2s_fcw_10s" "lidarSequence"];
%% 
% The video contains rectangle labels, whereas the lidar point cloud
% sequence contains cuboid labels. Gather the rectangle labels from the
% video and the cuboid labels from the lidar point cloud sequence.

labelTypes = [labelType.Rectangle labelType.Cuboid];
[labelData,timestamps] = gatherLabelData(gTruth,signalNames,labelTypes);
%% 
% Display the first eight rows of label data from the two signals. Both
% signals contain data for the |Car| label. In the video, the |Car| label
% is drawn as a rectangle bounding box. In the lidar point cloud sequence,
% the |Car| label is drawn as a cuboid bounding box.

videoLabelSample = head(labelData{1})
lidarLabelSample = head(labelData{2})
%% 
% Write signal frames associated with the gathered label data to temporary
% folder locations, with one folder per signal. Use the timestamps returned
% by the |gatherLabelData| function to indicate which signal frames to
% write.

outputFolder = fullfile(tempdir,["videoFrames" "lidarFrames"]);
fileNames = writeFrames(gTruth,signalNames,outputFolder,timestamps);
%% 
% Load the written video signal frames by using an <docid:matlab_ref#butueui-1 
% |imageDatastore|> object. Load the associated rectangle label data by using 
% a <docid:vision_ref#mw_ea7fe152-be5c-4868-acb6-7c5d7fd40b93 |boxLabelDatastore|> 
% object.

imds = imageDatastore(fileNames{1});
blds = boxLabelDatastore(labelData{1});
%% 
% Load the written lidar signal frames by using a <docid:matlab_ref#bvako8w
% |fileDatastore|> object. Load the associated cuboid label data by using a
% |boxLabelDatastore| object.

fds = fileDatastore(fileNames{2},'ReadFcn',@pcread);
clds = boxLabelDatastore(labelData{2});
%% 
% Visualize the written video frames by using a <docid:vision_ref#bse_sae
% |vision.VideoPlayer|> object. Visualize the written lidar frames by using
% a <docid:vision_ref#busm5az-7 |pcplayer|> object.

videoPlayer = vision.VideoPlayer;

ptCloud = preview(fds);
ptCloudPlayer = pcplayer(ptCloud.XLimits,ptCloud.YLimits,ptCloud.ZLimits);

while hasdata(imds)
    % Read video and lidar frames.
    I = read(imds);
    ptCloud = read(fds);

    % Visualize video and lidar frames.
    videoPlayer(I);
    view(ptCloudPlayer,ptCloud);
end
%% 
% Remove the path to the point cloud sequence folder.

rmpath(pcSeqDir);