function plot_fwselect(celldata, idx, flag)
% PLOT_FWSELECT - plot selected freeway cell.
%
% Call: plot_fwselect(celldata, idx)
%
% Parameters:
% celldata - array of freeway cell structures;
% idx - index to 'celldata' array;
% flag - specifies if the cell is selected or unselected;
%
% Returns: none.
%
% Last modified: 11/11/2006.
%
% Alex Kurzhanskiy <akurzhan@eecs.berkeley.edu>
%
K = 2;
N = size(celldata, 2); % number of cells
if (N < 1) | (idx < 1) | (idx > N)
return;
end
ih = ishold;
if celldata(idx).PMstart <= celldata(idx).PMend % direction: from LEFT to RIGHT
dr = 1;
else % direction: from RIGHT to LEFT
dr = -1;
end
if flag
C1 = 'y';
C2 = 0.5 * [1 1 1];
else
C1 = 'w';
C2 = 'k';
end
xylim = axis;
Xml = [celldata(idx).PMstart celldata(idx).PMstart celldata(idx).PMend celldata(idx).PMend celldata(idx).PMstart];
Yml = [(-dr)*ceil(celldata(idx).lanes) 0 0 (-dr)*ceil(celldata(idx).lanes) (-dr)*ceil(celldata(idx).lanes)];
Yfr = [xylim(3) xylim(4) xylim(4) xylim(3) xylim(3)];
fill(Xml, Yfr, C1);
hold on;
plot(Xml, Yfr, C1);
fill(Xml, Yml, C2);
plot(Xml, Yml, C1);
Yr = [Yml(1) Yml(1) (Yml(1)-K*dr) (Yml(1)-K*dr) Yml(1)];
wdt = min(get_cell_lengths(celldata))/8;
if ~isempty(celldata(idx).ORname) % plot on-ramp
w = wdt * celldata(idx).ORlanes;
x = celldata(idx).PMstart;
if dr > 0
Xor = [(x+wdt) (x+wdt+w) (x+w) x (x+wdt)];
else
Xor = [(x-wdt) (x-wdt-w) (x-w) x (x-wdt)];
end
fill(Xor, Yr, 'b');
plot(Xor, Yr, 'b');
end
if ~isempty(celldata(idx).FRname) % plot off-ramp
w = wdt * celldata(idx).FRlanes;
x = celldata(idx).PMend;
if dr > 0
Xfr = [(x-wdt) (x-wdt-w) (x-w) x (x-wdt)];
else
Xfr = [(x+wdt) (x+wdt+w) (x+w) x (x+wdt)];
end
fill(Xfr, Yr, 'g');
plot(Xfr, Yr, 'g');
end
plot([xylim(1:2) xylim(2) xylim(1) xylim(1)], ...
[xylim(3) xylim(3) xylim(4) xylim(4) xylim(3)], 'k');
axis(xylim);
xlabel('Post Mile');
h = gca;
set(h, 'YTick', []);
if ih == 0
hold off;
end
return;