function axList = exampleHelperSetupDisplay(egoVehicle,sensorList)
%exampleHelperSetupDisplay Configures window for display of maps

% Copyright 2020 The MathWorks, Inc.

    % Move figures to correct regions of screen
    fDSD = findobj('Type', 'Figure', 'Tag', 'VIODemoDisplay');
    dsdPanel = findobj(fDSD,'Type','uipanel');
    dsdPanel.Position = [0 0 .35 1];
    movegui(fDSD, 'north');

    % Create uipanels and axes for all displayed maps
    numSensor = numel(sensorList);
    axList = cell(numSensor+2,1);

    % Create hard-coded regions for reconstructed map and sensor config view
    x0 = dsdPanel.Position(3);
    hCenterPanel = uipanel(fDSD,'Position',[x0 0 .3 1]);
    x1 = sum(hCenterPanel.Position([1 3]));
    hRightPanel  = uipanel(fDSD,'Position',[x1 0 1-x1 1]);
    
    hBotCenterPanel = uipanel(hCenterPanel,'Position',[0 0 1 .25]);
    hMidCenterPanel = uipanel(hCenterPanel,'Position',[0 .25 1 .5]);
    hTopCenterPanel = uipanel(hCenterPanel,'Position',[0 .75 1 .25]);
    hTopLeftCenterPanel = uipanel(hTopCenterPanel,'Position',[0 0 .5 1]);
    hTopRightCenterPanel = uipanel(hTopCenterPanel,'Position',[.5 0 .5 1]);
    
    axList{1} = axes(hRightPanel);
    hold on;
    constructVehicle(axList{1},egoVehicle,sensorList);
    axList{2} = axes(hMidCenterPanel);
    
    % Display a birds-eye plot, vehicle plotter and sensor plotter
    v = egoVehicle.Mesh.Vertices;
    bep = birdsEyePlot('Parent',axList{2},'Xlim',[min(v(:,1)),max(v(:,1))]+[-1 1],'YLim',[min(v(:,2)),max(v(:,2))]+[-.5 .5]);
    mPlotter  = meshPlotter(bep);
    caPlotters = cell(numel(sensorList),1);
    legend('off');

    % Display ego vehicle mesh
    plotMesh(mPlotter,{egoVehicle.Mesh.Vertices},{egoVehicle.Mesh.Faces},'Color',egoVehicle.PlotColor);
    for i = 1:numel(sensorList)
        s = sensorList{i};
        caPlotter{i} = coverageAreaPlotter(bep);
        FOV = diff(s.AzimuthLimits);
        plotCoverageArea(caPlotter{i},s.SensorLocation,s.MaxRange,s.Yaw+s.AzimuthLimits(1)+FOV/2,FOV);
    end
    axis equal
    
    axList{3} = axes(hTopLeftCenterPanel);
    constructVehicle(axList{3},egoVehicle,sensorList(1));
    hold on
    axList{4} = axes(hTopRightCenterPanel);
    constructVehicle(axList{4},egoVehicle,sensorList(2));
    hold on
    axList{5} = axes(hBotCenterPanel);
    constructVehicle(axList{5},egoVehicle,sensorList(3));
    hold on
end

function hG = constructVehicle(ax, egoVehicle, sensorList)
    % Create hggroup/transform which will have its pose updated
    hG = hggroup('Parent',ax,'Tag','Vehicle');
    hTEgo = hgtransform('Parent',hG);
    
    % Attach car mesh to hggroup
    patch(hTEgo,'Faces',egoVehicle.Mesh.Faces,'Vertices',egoVehicle.Mesh.Vertices,'FaceColor',egoVehicle.PlotColor);
    
    % Create patch object for any sensors applied to this object
    for i = 1:numel(sensorList)
        s = sensorList{i};
        hT = hgtransform('Parent',hTEgo);
        angles = linspace(s.AzimuthLimits(1)/180*pi, s.AzimuthLimits(2)/180*pi, 20);
        v = [0 0; cos(angles(:)) sin(angles(:))]*s.MaxRange;
        f = [1:size(v,1) 1];
        patch(hT,'Faces',f,'Vertices',v,'FaceColor',[.1 .1 .5],'FaceAlpha',.25);
        hT.Matrix(1:3,:) = [eul2rotm([s.Yaw/180*pi 0 0]) [s.SensorLocation(:); s.Height]];
    end
end