function pathStructOut=coveragePattern(polygons,theta,unitwidth,sidelap,unitlength,overlap,referenceHeight)
%coveragePattern computes a struct array of all possible coverage
%alternatives of each polygon. 'Normal','Reverse','CCWNormal','CCWReverse'
%as well as the start and end points of each coverage alternative.
%#codegen

%   Copyright 2022 The MathWorks, Inc.

pathStruct=struct('Normal',[],'Reverse',[],'CCWNormal',[],'CCWReverse',[],'Start',zeros(4,3),'Exit',zeros(4,3));
coder.varsize('pathStruct.Normal','pathStruct.Reverse','pathStruct.CCWNormal','pathStruct.CCWReverse');
pathStructOut = repmat(pathStruct, 1,numel(polygons));
for i=1:1:numel(polygons)
    polygon=polygons{i};
    %rotate to frame where sweep lines are horizontal.
    rotatedPolygon=uav.internal.coveragePlanner.rotatePoints(polygon,-theta(i));
    %Step 1 Compute the sweep lines (forward-clockwise sweep)
    sweepLines = uav.internal.coveragePlanner.horizontalSweepLines(rotatedPolygon,unitwidth,sidelap);
    [intersectionPoints,~]=uav.internal.coveragePlanner.coverageIntersections(rotatedPolygon,sweepLines);
    %Normal path.
    path = uav.internal.coveragePlanner.coveragePath(intersectionPoints,unitlength,overlap);
    %Rotate path back to actual frame and append the referenceHeight.
    pathStruct.Normal=[uav.internal.coveragePlanner.rotatePoints(path,theta(i)),repmat(referenceHeight,size(path,1),1)];
    %Set start and end.
    pathStruct.Start(1,1:3)=pathStruct.Normal(1,:);
    pathStruct.Exit(1,1:3)=pathStruct.Normal(end,:);
    %Reverse
    pathStruct.Reverse = uav.internal.coveragePlanner.reverseSweep(pathStruct.Normal);
    pathStruct.Start(2,1:3)=pathStruct.Reverse(1,:);
    pathStruct.Exit(2,1:3)=pathStruct.Reverse(end,:);

    %Counter clockwise normal.
    pathStruct.CCWNormal = uav.internal.coveragePlanner.ccwNormalSweep(pathStruct.Normal);
    pathStruct.Start(3,1:3)=pathStruct.CCWNormal(1,:);
    pathStruct.Exit(3,1:3)=pathStruct.CCWNormal(end,:);
    %Counter clockwise reverse
    pathStruct.CCWReverse = uav.internal.coveragePlanner.ccwReverseSweep(pathStruct.Normal);
    pathStruct.Start(4,1:3)=pathStruct.CCWReverse(1,:);
    pathStruct.Exit(4,1:3)=pathStruct.CCWReverse(end,:);
    pathStructOut(i)=pathStruct;
end
end
