function make_bmp

% function make_bmp
%
% This function is used in conjunction with FD3.  It creates "bumps" on the
% flow diagram.  Bumps has a slightly different meaning than normal in this
% context.  FD3 has two different bump modes.  When FD3STATUS(1) equals one,
% FD3 makes a visible bump on the most recently updated stream.  When
% FD3STATUS(1) equals zero, FD3 makes an *invisible* bump on the most recently
% updated stream.  When FD3 makes stream labels, it adds them to the last
% stream segment.  When streams intersect, it is necessary to insert x/ydata
% into both streams at the intersection point so that one stream will not
% cover the label of the other stream.  When visible bumps are used,
% this data is inserted in the form of a visible bump.  When invisible bumps
% are created, the inserted data lies along the same lines as the old streams
% and therefore does not change their appearance.  When two stream intersect,
% at least one of the two streams always has an invisible bump.
%
% ACP

% Last update:  July 11, 1993
% Author:       Adrian C. Penisson

global FD3STREAMS FD3STATUS FD3SCALE FD3FIGURE

% First, change the pointer from 'arrow' to 'watch' since the bump making
% routine can sometimes take a little while.

set(FD3FIGURE,'Pointer','Watch')

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Get rid of all previously created bumps. %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=2:size(FD3STREAMS,2)

   xdata=get(FD3STREAMS(1,i),'Xdata');
   ydata=get(FD3STREAMS(1,i),'Ydata');

   keep=[];

   j=2;
   while j<length(xdata)
      if isnan(xdata(j+1))
         if isnan(xdata(j+2))
	    % It's an invisible bump. Through out this and next three points.
            keep=[keep 0 0 0 0];
            j=j+4;
         else
	    % It's a visible bump.  Through out this and next 19 points.
            keep=[keep zeros(1,20)];
            j=j+20;
         end
      else
	 % Keep this point.  No bump here.
         keep=[keep 1];
         j=j+1;
      end
   end

   xdata=xdata(find([1 keep 1]));
   ydata=ydata(find([1 keep 1]));

   set(FD3STREAMS(1,i),'Xdata',xdata,'Ydata',ydata)

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Find out where the streams intersect %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=2:size(FD3STREAMS,2)-1

   % Get xdata and ydata for stream i.

   xdata1=get(FD3STREAMS(1,i),'Xdata');
   ydata1=get(FD3STREAMS(1,i),'Ydata');

   for j=i+1:size(FD3STREAMS,2)

      % Get xdata and ydata for stream j.

      xdata2=get(FD3STREAMS(1,j),'Xdata');
      ydata2=get(FD3STREAMS(1,j),'Ydata');

      for k=1:length(xdata1)-1

         for l=1:length(xdata2)-1

	    if ~((xdata1(k)==xdata1(k+1) & xdata2(l)==xdata2(l+1) & ...
	       xdata1(k)==xdata2(l)) | (ydata1(k)==ydata1(k+1) & ...
	       ydata2(l)==ydata2(l+1) & ydata1(k)==ydata2(l)))

	       if xdata1(k)==xdata1(k+1) & xdata1(k)<max(xdata2([l l+1])) &...
		  xdata1(k)>min(xdata2([l l+1]))

	          m2=(ydata2(l+1)-ydata2(l))/(xdata2(l+1)-xdata2(l));
	          b2=ydata2(l)-m2*xdata2(l);
		  xint=xdata1(k);
		  yint=m2*xint+b2;

		  if yint<max(ydata1([k k+1])) & yint>min(ydata1([k k+1]))

		     bumps=[bumps [i k xint yint 0 0]'];
		     bumps=[bumps [j l xint yint m2 FD3STATUS(1)]'];

		  end 

	       elseif xdata2(l)==xdata2(l+1) & ...
		  xdata2(l)<max(xdata1([k k+1])) & ...
		  xdata2(l)>min(xdata1([k k+1]))

		  m1=(ydata1(k+1)-ydata1(k))/(xdata1(k+1)-xdata1(k));
	          b1=ydata1(k)-m1*xdata1(k);
		  m2=inf;
		  xint=xdata2(l);
		  yint=m1*xint+b1;

		  if yint<max(ydata2([l l+1])) & yint>min(ydata2([l l+1]))

		     bumps=[bumps [i k xint yint 0 0]'];
		     bumps=[bumps [j l xint yint m2 FD3STATUS(1)]'];

		  end 

	       elseif ~((ydata1(k)==ydata1(k+1) & ydata2(l)==ydata2(l+1))|...
		  xdata1(k)==xdata1(k+1)|xdata2(l)==xdata2(l+1))

		  m1=(ydata1(k+1)-ydata1(k))/(xdata1(k+1)-xdata1(k));
	          b1=ydata1(k)-m1*xdata1(k);
	          m2=(ydata2(l+1)-ydata2(l))/(xdata2(l+1)-xdata2(l));
	          b2=ydata2(l)-m2*xdata2(l);
		  xint=(b2-b1)/(m1-m2);
		  yint=xint*m1+b1;

		  if xint<max(xdata1([k k+1])) & ...
		     xint>min(xdata1([k k+1])) & ...
		     xint<max(xdata2([l l+1])) & ...
		     xint>min(xdata2([l l+1]))

		     bumps=[bumps [i k xint yint 0 0]'];
		     bumps=[bumps [j l xint yint m2 FD3STATUS(1)]'];

	          end

	       end

	    end

	 end

      end

   end

end

%%%%%%%%%%%%%%%%%%
% Make new bumps %
%%%%%%%%%%%%%%%%%%

for i=2:size(FD3STREAMS,2)

   xdata=get(FD3STREAMS(1,i),'Xdata');
   ydata=get(FD3STREAMS(1,i),'Ydata');

   adjust=0;

   j=0;
   while j<(length(xdata)-1) & length(bumps)>0 
      j=j+1;

      xtemp=[];
      ytemp=[];

      bumpsij=bumps(:,find(all([bumps(1,:)==i;bumps(2,:)==j-adjust])));

      if any(bumpsij)

         if xdata(j)~=xdata(j+1)

	    [junk,sortx]=sort(bumpsij(3,:));
	    bumpsij=bumpsij(:,sortx);

	    for k=1:size(bumpsij,2)

	       if bumpsij(6,k)

 		  arcpts=arc([1 sqrt(-1)]*bumpsij(3:4,k),FD3SCALE/4,15,...
		     atan(bumpsij(5,k))+pi,atan(bumpsij(5,k)));

		  x1=bumpsij(3,k)-FD3SCALE/4*cos(atan(bumpsij(5,k)));
		  y1=bumpsij(4,k)-FD3SCALE/4*sin(atan(bumpsij(5,k)));

		  x2=bumpsij(3,k)+FD3SCALE/4*cos(atan(bumpsij(5,k)));
		  y2=bumpsij(4,k)+FD3SCALE/4*sin(atan(bumpsij(5,k)));

		  xtemp=[xtemp x1 NaN real(arcpts) NaN x2];
		  ytemp=[ytemp y1 NaN imag(arcpts) NaN y2];

	       else

		  xtemp=[xtemp bumpsij(3,k) NaN NaN bumpsij(3,k)];
		  ytemp=[ytemp bumpsij(4,k) NaN NaN bumpsij(4,k)];

	       end

	    end

	    if xdata(j+1)<xdata(j)
	       xtemp=fliplr(xtemp);
	       ytemp=fliplr(ytemp);
	    end

         else

	    [junk,sorty]=sort(bumpsij(4,:));
	    bumpsij=bumpsij(:,sorty);

	    for k=1:size(bumpsij,2)

	       if bumpsij(6,k)

		  arcpts=arc([1 sqrt(-1)]*bumpsij(3:4,k),FD3SCALE/4,15,...
		     -pi/2,pi/2);

		  x1=bumpsij(3,k);
		  y1=bumpsij(4,k)-FD3SCALE/4;

		  x2=x1;
		  y2=bumpsij(4,k)+FD3SCALE/4;

		  xtemp=[xtemp x1 NaN real(arcpts) NaN x2];
		  ytemp=[ytemp y1 NaN imag(arcpts) NaN y2];

	       else

		  xtemp=[xtemp bumpsij(3,k) NaN NaN bumpsij(3,k)];
		  ytemp=[ytemp bumpsij(4,k) NaN NaN bumpsij(4,k)];

	       end

	    end

	    if ydata(j+1)<ydata(j)
	       xtemp=fliplr(xtemp);
	       ytemp=fliplr(ytemp);
	    end

         end

         xdata=[xdata(1:j) xtemp xdata(j+1:length(xdata))];
         ydata=[ydata(1:j) ytemp ydata(j+1:length(ydata))];

         j=j+length(xtemp);
	 adjust=adjust+length(xtemp);

	 end

      end

      set(FD3STREAMS(1,i),'Xdata',xdata,'Ydata',ydata)

      adj_lbl(i)

end

set(FD3FIGURE,'Pointer','Arrow')

