%function plotSonar
function [m,n,z,ztrack] = plotSonar(noplot)
	
global mission

	if isfield(mission{1},'trackspacing1'),
		trackspacing1 = mission{1}.trackspacing1;
		trackspacing2 = mission{1}.trackspacing2;
	else
		helpdlg('Sonar Cross-Section display unavailable for this Mission','Cross Section Help');
		return
	end

	sonarrange = selectSonarRange;
	nLegs = getLegs('get',0);

	% make a structure of relevant data for each leg
	% trackleg, sonar coverage
	sonar{1}.stype = 'sidescan';
	sonar{1}.range = sonarrange;
	sonar{1}.numlegs = nLegs;
	sonar{1}.leg{1}.relrange = [0 sonarrange*2]; 	% m, relative range
	sonar{1}.leg{1}.trackpos = sonarrange; 		% relative first trackline position
	sonar{1}.leg{1}.maxoverlaps = 1; 		% max num of sonar overlay for this leg
	
	% initial values for first leg
	trackpos = [sonarrange]; % trackline positions
	x = [0]; 		 % beginning of x-axis
	m = [x sonarrange*2]; 	 % sonar ranges for each trackline
	ay = [0];
	
	n = []; 	% used for plotting - m expanded into one column
	z = []; 	% sonar coverage indicator as a function of n. 0 = no sonar, 1 = 1 sonar, 2 = 2 sonars (overlap)
	ztrack = []; 	% index for the trackleg that the current point originally belonged to.
	zlast = 0; 	% last sonar coverage indicator - increment this is more than 1 overlap
	maxnumoverlaps = zlast;
	
	for i=2:nLegs,
		b = trackpos(length(trackpos)); % get the last value of a
		
		if (mod(i,2) == 0) 		% alternate narrow-wide-narrow-wide, start with narrow
		    c = b + trackspacing1;
		else
	 	    c = b + trackspacing2;
		end
		
		trackpos = [trackpos c];
		ay = [ay 0];
		
		m = [m; (c - sonarrange),(c + sonarrange)];
		sonar{1}.leg{i}.relrange = [(c - sonarrange),(c + sonarrange)];
		sonar{1}.leg{i}.trackpos = c;
	end

	for j=1:length(m), 
		
	    if (j == 1),
		n = [n,m(j,1),m(j,1),m(j,2),m(j,2)];
		z = [z,0,1,1,0];
		ztrack = [ztrack,j,j,j,j];
	    else
		[n,z,ztrack] = insertPoint(m(j,1),'begin',n,z,ztrack,j);
		[n,z,ztrack] = insertPoint(m(j,2),'end',n,z,ztrack,j);
	    end
	end
	
	maxnumoverlaps = max(z);

	% get number of overlaps at each trackline position
	numoverlaps = zeros(1,length(m));
	tracks = zeros(1,length(m)*3);
	lineindices = zeros(1,length(m)*3);

	for ii=1:length(m),
	    indextrack = find(n<trackpos(ii));
	    indextrack = indextrack(length(indextrack));
	    numoverlaps(ii) = z(indextrack);
	    tracks = [tracks,trackpos(ii),trackpos(ii),trackpos(ii)];
	    lineindices = [lineindices,0,numoverlaps(ii),0];
	end

	if (nargin == 0)

		figure;
		title('Cross-Section of Sonar Coverage Showing Trackline Positions')	
	
		set(gca,'Box','on')
		xlabel('Relative Trackline and Sonar Spacing (meters)')
		ylabel('Sonar Coverage/100 (%)')

		mno = (trackpos(length(trackpos)) + sonarrange*0.5);
		x = [x, mno]; % end of x-axis

		xround1 = roundn(x(1),2);
		xround2 = roundn(x(2)+sonarrange,2);
		axis([xround1 xround2 0 maxnumoverlaps]); % xmin xmax ymin ymax

		hold on;

		colormap(winter);
		area(n,z); % plot(x,y)
		plot(trackpos,ay,'yd','MarkerFaceColor','y');
		plot(tracks,lineindices,'y:');

		xaxisticks = [m(1,1),trackpos,m(length(m),2)];
		set(gca,'xtick',xaxisticks);

		grid on;
		hold off;
	
	%plotColors(maxnumoverlaps,m,trackpos,ay)
	end
	
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [n,z,ztrack] = insertPoint(currpoint,position,n,z,ztrack,mindex)

% position: if 'begin', increment, if 'end', decrement subsequent z-indices
	
	for i=length(n):-1:1,
		if (currpoint > n(i)),
		indextostartat = i + 1;
		break;
		end
	end
	
	nummove = length(n) - indextostartat + 1; % shift matrix over by nummove
	nlength = length(n); 
	nlast = []; zlast = []; ztracklast = [];
	
	for iii=nummove-1:-1:0,
		nlast = [nlast, n(nlength-iii)];
		zlast = [zlast, z(nlength-iii)];
		ztracklast = [ztracklast, ztrack(nlength-iii)];
	end
	
	% increase all the z's by one for the new overlap = znew if begin, decrease by one if end
	znew = [];
	if (strcmp(position,'begin')),
		znew = zlast + 1;
	else
		znew = zlast - 1;
	end
	
	z(indextostartat) = z(indextostartat-1);
	
	if (strcmp(position,'begin')),
		z(indextostartat+1) = z(indextostartat) + 1;
	else
		z(indextostartat+1) = z(indextostartat) - 1;
	end
		
	z = [z(1:indextostartat+1),znew];

	n(indextostartat) = currpoint;
	n(indextostartat+1) = currpoint;
	n = [n(1:indextostartat+1),nlast];
	ztrack(indextostartat) = mindex;
	ztrack(indextostartat+1) = mindex;
	ztrack = [ztrack(1:indextostartat+1),ztracklast];


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function plotColors(maxnumoverlaps,m,a,ay)

	figure;
	grid on;
	hold on;

		xlabel('Trackline Positions (meters)')
		ylabel('Sonar Overlap Display')
		
		plot(a,ay,'yd','MarkerFaceColor','y');
			
		xaxisticks = [m(1,1),a,m(length(m),2)];
		set(gca,'xtick',xaxisticks);


	% draw each patch
	vert = []; fac = [];
	verttot = [];
	
	cmap = colormap(winter);
	cmapindex = 1;
	for iii=1:maxnumoverlaps,
		tcolor(iii,:) = cmap(cmapindex,:);
		cmapindex = cmapindex + 1;
	end
	
	% only specific set of working colors in each range
	% i have m: use [i,j] = find(m == number)
	% first one : blue, m = 1, sec: grn, m = 2,... alternate patches up/down
	
	rangeflag = zeros(1,length(m));
	taken = zeros(1,maxnumoverlaps);
	
	% calculate rangeflag for each m range
	[rangeflag] = calcIndexPerRange(m,maxnumoverlaps,rangeflag);

	for ii=1:length(m),
	
		fac = [1 2 3 4];
	
		top = rangeflag(ii);
		bottom = rangeflag(ii) - 1;
		vert = [m(ii,1),bottom;m(ii,1),top;m(ii,2),top;m(ii,2),bottom];
		verttot = [verttot; vert];
	
		tcolor = cmap(cmapindex,:);
		%patch('Faces',fac,'Vertices',vert,'FaceVertexCData',tcolor(1:3),'EdgeColor',tcolor(1:3),'FaceColor',tcolor(1:3));
		patch('Faces',fac,'Vertices',vert,'FaceVertexCData',tcolor(1:3),'FaceColor',tcolor(1:3));
		cmapindex = cmapindex + 10;
		if (cmapindex >= length(cmap)), cmapindex = 1; end
	end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [rangeflag] = calcIndexPerRange(m,maxnumoverlaps,rangeflag)
	
	for i=1:length(m),
	
		taken = zeros(1,maxnumoverlaps); % reset before each new range   
		
		% initialize rangeflag and taken array
		if (i <= maxnumoverlaps),
			if (i == 1),
				rangeflag(i) = 1;
			else 
				for j=1:i-1,
					if (m(i,1) < m(i-j,2)),
						[taken] = takeflag(taken,rangeflag(i-j),maxnumoverlaps);
					end
				end
				rangeflag(i) = getrangeflag(taken,maxnumoverlaps);
			end
		else
			for j=1:maxnumoverlaps,
				if (m(i,1) < m(i-j,2)),
					[taken] = takeflag(taken,rangeflag(i-j),maxnumoverlaps);
				end
			end
			rangeflag(i) = getrangeflag(taken,maxnumoverlaps);
		end

	end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [taken] = takeflag(taken,rflag,maxnumoverlaps)

% figures out which indices already have a range in them
% input a point you want to add to the taken array

for i=1:maxnumoverlaps,
	if (rflag == i),
		taken(i) = 1;
		return;
	end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [flagindex] = getrangeflag(taken,maxnumoverlaps)

for i=1:maxnumoverlaps,
	if (taken(i) ~= 1),
		flagindex = i;
		return;
	end
end
