function [lfts,rgts,bankAngle,headingAngle] = lateralShift(tileLength,roadWidth, roadCenters, rollAngle,InHeading)
%LATERALSHIFT shifts road centers to center of the road.

%   Copyright 2019-2022 The MathWorks, Inc.

% upsample x,y by clothoid interpolation
    [x,y] = driving.scenario.clothoid(roadCenters(:,1),roadCenters(:,2));
    nwaypoints = size(roadCenters,1);
    oversample = (size(x,1)-1)/(nwaypoints-1);

    % upsample z by shape-preserving cubic spline based upon
    % distance traveled along (x,y)
    iover = 1+(0:oversample:oversample*(nwaypoints-1))';
    xyDist = cumsum(sqrt(sum(diff([x(1) y(1); x y]).^2,2)));
    z = interp1(xyDist(iover),roadCenters(:,3),xyDist,'pchip');

    % return total distance traveled along the way
    splinewaypoints = [[x(1) y(1) z(1)]; [x y z]];
    cumdist = cumsum(sqrt(sum(diff(splinewaypoints,1,1).^2,2)));

    % divide total length into tiles of approx length TileLength
    %ntiles = ceil(cumdist(end)/obj.TileLength);
    ntiles = ceil(cumdist(end)/tileLength);
    splinedist = linspace(cumdist(1),cumdist(end),1+ntiles);
    ctrs = interp1(cumdist, splinewaypoints(2:end,:),splinedist, 'spline');
    dctrs = interp1(cumdist, splinewaypoints(2:end,:), splinedist+1e-3, 'spline');
    % convert roll angle to clockwise radian angle
    bankAngle = deg2rad(-rollAngle);

    % smoothly connect bank angles based upon cumulative xy distance.
    if ~isscalar(bankAngle)
        splinebankangle = interp1(xyDist(iover),bankAngle,xyDist,'pchip');
        bankAngle = interp1(cumdist, splinebankangle,splinedist', 'spline');
    else
        bankAngle = repmat(bankAngle,size(ctrs,1),1);
    end

    if  ~isscalar(InHeading)
        InHeading = matlabshared.tracking.internal.scenario.clothoidG2fitCourse(roadCenters);
        splineheadingangle = interp1(xyDist(iover),InHeading,xyDist,'pchip');
        headingAngle = interp1(cumdist, splineheadingangle,splinedist', 'spline');
    else
        headingAngle = repmat(InHeading,size(ctrs,1),1);

    end

    % compute "left" vector displacement with banking information
    dplane = [ctrs(:,2)-dctrs(:,2), dctrs(:,1)-ctrs(:,1)];
    dplane = dplane./sqrt(sum(dplane.^2,2));
    dplane = [dplane(:,1).*cos(bankAngle) dplane(:,2).*cos(bankAngle) sin(bankAngle)];

    % add it to the centers for the left and right road borders
    lfts = ctrs+dplane.*roadWidth;
    rgts = ctrs-dplane.*roadWidth;

    % set z value to exact zero if the original value is zero
    if all(roadCenters(:,3) == 0)
        lfts(:,3) = zeros(numel(lfts(:,1)),1);
        rgts(:,3) = zeros(numel(rgts(:,1)),1);
    end
end
