%% Find Candidate Ego Lane Boundaries
% Find candidate ego lane boundaries from an array of lane boundaries.
%
% Create an array of cubic lane boundaries.

lbs = [cubicLaneBoundary([-0.0001, 0.0, 0.003,  1.6]), ...
       cubicLaneBoundary([-0.0001, 0.0, 0.003,  4.6]), ...
       cubicLaneBoundary([-0.0001, 0.0, 0.003, -1.6]), ...
       cubicLaneBoundary([-0.0001, 0.0, 0.003, -4.6])];

%
%   For each lane boundary, compute the y-axis location at which the x-coordinate is 0.

xWorld = 0; % meters
yWorld = computeBoundaryModel(lbs,0);

% Use the computed locations to find the ego lane boundaries that best meet the criteria.

leftEgoBoundaryIndex = find(yWorld == min(yWorld(yWorld>0)));            
rightEgoBoundaryIndex = find(yWorld == max(yWorld(yWorld<=0)));
leftEgoBoundary = lbs(leftEgoBoundaryIndex);
rightEgoBoundary = lbs(rightEgoBoundaryIndex);

% Plot the boundaries using a bird's-eye plot and lane boundary plotter.

bep = birdsEyePlot('XLimits',[0 30],'YLimits',[-5 5]);
lbPlotter = laneBoundaryPlotter(bep,'DisplayName','Left-lane boundary','Color','r');
rbPlotter = laneBoundaryPlotter(bep,'DisplayName','Right-lane boundary','Color','g');
plotLaneBoundary(lbPlotter,leftEgoBoundary)
plotLaneBoundary(rbPlotter,rightEgoBoundary)

% Copyright 2012 The MathWorks, Inc.