function wvsurf(action,myFunction,min_t,max_t,min_x,max_x)
%WVSURF  Graphical interface for plotting a surface u=u(x,t)
%
%        <DESCRIPTION>
%        wvsurf is a graphical interface which graphs the surface
%        u=u(x,t) in xtu-space. 
%
%        <TO RUN>
%        Type wvsurf at the MATLAB prompt with no arguments.
%
%        <USE>
%        After startup, set any of the following controls:
%
%          Time t     - Interval t0 < t < t1 to plot u=u(x,t)
%          Range x    - Interval x0 < x < x1 to plot u=u(x,t)
%          View Point - View xtu-space from and angle PHI rotated up/down from
%                       the xt-plane and angle THETA rotated around
%                       the u axis in the xt-plane.
%          u(x,t)     -  Field in which to define the function u(x,t)
%
%        After setup, press the DRAW button to render the plot.
%
%        Use the OTHER VIEWS menu to pass the current control parameters 
%        to ANIMATE (wvmovie) or view a SLICE PLOT (wvsurf) of the surface. 
 
% Created: 6/95 by R. Knobel
% Last modified: 5/99 by R. Knobel
% Requires: MATLAB 4.2(c) - MATLAB 5.3
% Also links to: wvmovie.m and wvslice.m 

if nargin < 1
   action = 'start';
end

if ~strcmp(action,'start')
   handles = get(gcf,'UserData');
   hTmin = handles(1);
   hTmax = handles(2);
   hXmin  = handles(3);
   hXmax  = handles(4);
   hFunction = handles(5);
   hAxes  = handles(6);
   hPlot  = handles(7);
   hPhi   = handles(8);
   hTheta = handles(9);
end

if strcmp(action,'start'),
   
   bgcolor='black';
   fgcolor='white';
   figure; 
   set(gcf, ...
      'NumberTitle','off', ...
      'Name','Surface Plot', ...
      'BackingStore','on',...
      'Units','normalized',...
      'Color',bgcolor);
   
   %  The Console Panel 
   
   top      =0.95;
   bottom   =0.17;
   left     =0.82;
   btnWid   =0.13;
   btnHt    =0.06;
   spacing  =0.02;
   frmBorder=0.02;
   
   frmPos=[left-frmBorder bottom-frmBorder btnWid+2*frmBorder .82];
   uicontrol( ...
      'Style','frame', ...
      'Units','normalized', ...
      'Position', frmPos, ...
      'ForegroundColor',fgcolor,...
      'BackgroundColor',bgcolor);
   
   %  Time interval
   
   uicontrol( ...
      'Style','text',...
      'Units','normalized',...
      'Position', [.81 .905 .15 .04],...
      'BackgroundColor',bgcolor,...
      'ForegroundColor','red',...
      'String','Time t');
   
   uicontrol( 'Style','text',...
      'Units','normalized',...
      'Position', [.81 .86 .08 .04],...
      'BackgroundColor',bgcolor,...
      'ForegroundColor',fgcolor,...
      'String','Initial');
   if nargin < 3,
      min_t = 0;
   end;
   hTmin=uicontrol( ...
      'Style','edit',...
      'Units','normalized',...
      'Position', [.89 .86 .07 .04],...
      'BackgroundColor','white',...
      'ForegroundColor','black',...
      'String',num2str(min_t));
   
   uicontrol( 'Style','text',...
      'Units','normalized',...
      'Position', [.81 .81 .08 .04],...
      'BackgroundColor',bgcolor,...
      'ForegroundColor',fgcolor,...
      'String','Final');
   if nargin < 4,
      max_t = 5;
   end;
   hTmax=uicontrol( ...
      'Style','edit',...
      'Units','normalized',...
      'Position', [.89 .81 .07 .04],...
      'BackgroundColor','white',...
      'ForegroundColor','black',...
      'String',num2str(max_t));
   
   % X interval
   
   uicontrol( ...
      'Style','text',...
      'Units','normalized',...
      'Position', [.81 .73 .15 .04],...
      'BackgroundColor',bgcolor,...
      'ForegroundColor','red',...
      'String','Range x');
   
   uicontrol( 'Style','text',...
      'Units','normalized',...
      'Position', [.81 .68 .06 .04],...
      'BackgroundColor',bgcolor,...
      'ForegroundColor',fgcolor,...
      'String','Min');
   if nargin < 5,
      min_x = -5;
   end;
   hXmin=uicontrol( ...
      'Style','edit',...
      'Units','normalized',...
      'Position', [.87 .68 .09 .04],...
      'BackgroundColor','white',...
      'ForegroundColor','black',...
      'String',num2str(min_x));
   
   uicontrol( 'Style','text',...
      'Units','normalized',...
      'Position', [.81 .63 .06 .04],...
      'BackgroundColor',bgcolor,...
      'ForegroundColor',fgcolor,...
      'String','Max');
   if nargin < 6,
      max_x = 5;
   end;
   hXmax=uicontrol( ...
      'Style','edit',...
      'Units','normalized',...
      'Position', [.87 .63 .09 .04],...
      'BackgroundColor','white',...
      'ForegroundColor','black',...
      'String',num2str(max_x));
   
   
   % View angles
   
   uicontrol( ...
      'Style','text',...
      'Units','normalized',...
      'Position', [.81 .55 .15 .04],...
      'BackgroundColor',bgcolor,...
      'ForegroundColor','red',...
      'String','View Point');
   
   uicontrol( 'Style','text',...
      'Units','normalized',...
      'Position', [.81 .50 .08 .04],...
      'BackgroundColor',bgcolor,...
      'ForegroundColor',fgcolor,...
      'String','Phi');
   phi = 30;
   hPhi=uicontrol( ...
      'Style','edit',...
      'Units','normalized',...
      'Position', [.89 .50 .07 .04],...
      'BackgroundColor','white',...
      'ForegroundColor','black',...
      'String',num2str(phi));
   
   uicontrol( 'Style','text',...
      'Units','normalized',...
      'Position', [.81 .45 .08 .04],...
      'BackgroundColor',bgcolor,...
      'ForegroundColor',fgcolor,...
      'String','Theta');
   theta = 30;
   hTheta=uicontrol( ...
      'Style','edit',...
      'Units','normalized',...
      'Position', [.89 .45 .07 .04],...
      'BackgroundColor','white',...
      'ForegroundColor','black',...
      'String',num2str(theta));
   
   
   % The Draw button 
   
   uicontrol( ...
      'Style','push', ...
      'Units','normalized', ...
      'Position',[left bottom+btnHt+spacing  btnWid btnHt], ...
      'String','Draw', ...
      'Callback','wvsurf(''restart'')');
   
   % The Close button
   
   uicontrol( ...
      'Style','push', ...
      'Units','normalized',...
      'Position',[left bottom btnWid btnHt], ...
      'String','Close',...
      'Callback','wvsurf(''close'')' );
   
   % Field to define function u(x,t)
   
   if nargin < 2, 
      myFunction = 'exp(-(x-t)^2) + exp( -(x+t)^2)'; 
   end; 
   
   if ~isstr(myFunction), 
      myFunction = string(myFunction);
   end;
   
   uicontrol('Style','text',...
      'Units','normalized',...
      'Position',[.03 .02 .10 .05],...
      'BackgroundColor',bgcolor,...
      'ForegroundColor',fgcolor,...
      'String','u(x,t) = ');
   hFunction=uicontrol(...
      'Style','edit',...
      'Units','normalized',...
      'Position',[.13 .02 .6 .05],...
      'BackgroundColor','white',...
      'ForegroundColor','black',...
      'String',myFunction );
   
   % Menu for calling other 3D visualization tools 
   
   view_menu = uimenu('Label','Other Views');
   
   uimenu(view_menu,...
      'Label','Animate',...
      'CallBack','wvsurf(''GotoAnimate'')');
   uimenu(view_menu,...
      'Label','Slice Plot',...
      'CallBack','wvsurf(''GotoSlice'')');
   
   %  Setup main plot area 
      
   hAxes = axes('Position',[.12 .28 .6 .6],...
      'XLim',[min_x max_x],...
      'YLim',[min_t max_t],...
      'ZLim',[0 1],...
      'Color',bgcolor,...
      'XColor',fgcolor,...
      'YColor',fgcolor,...
      'ZColor',fgcolor,...
      'View',[theta phi] );  
   hold on;
   
   xlabel('x');
   ylabel('t');
   zlabel('u');
   
   [x,t] = meshgrid( [min_x,max_x], [min_t,max_t] );
   u = zeros(size(x));
   colormap(jet);
   hPlot = mesh(x,t,u);
   
   handles = [hTmin hTmax hXmin hXmax hFunction hAxes hPlot hPhi hTheta];
   set(gcf,'UserData',handles);
      
   wvsurf('restart')
   
elseif strcmp(action,'restart'),
   
   f = get(hFunction,'String');
   l = length(f);
   for k = fliplr(find((f=='^') | (f=='*') | (f=='/')))
      if ~strcmp(f(k-1),'.')
         f = [f(1:k-1) '.' f(k:l)];
         l = l+1;
      end
   end
   vector_f = f;
   
   min_x = str2num(get(hXmin,'String'));
   max_x = str2num(get(hXmax,'String'));
   min_t = str2num(get(hTmin,'String'));
   max_t = str2num(get(hTmax,'String'));
   theta = str2num(get(hTheta,'String'));
   phi   = str2num(get(hPhi,'String'));
   
   [x,t] = meshgrid(linspace(min_x,max_x,100),linspace(min_t,max_t,100));
   
   u     = eval(vector_f);
   min_u = min(u(:));
   max_u = max(u(:));  
   
   set(hAxes,...
      'XLim',[min_x max_x],...
      'YLim',[min_t max_t],...
      'ZLim',[min_u max_u],...
      'View', [theta phi] );  
   
   set(hPlot,'XData',x,'YData',t,'ZData',u,'CData',u)
   
elseif strcmp(action(1:4),'Goto')
   
   myFunction = get(hFunction,'String');
   min_x      = str2num(get(hXmin,'String'));
   max_x      = str2num(get(hXmax,'String'));
   min_t      = str2num(get(hTmin,'String'));
   max_t      = str2num(get(hTmax,'String'));
   
   wvsurf('close')
   
   if strcmp(action,'GotoAnimate'),
      wvmovie('start',myFunction,min_t,max_t,min_x,max_x),
   elseif strcmp(action,'GotoSlice'),
      wvslice('start',myFunction,min_t,max_t,min_x,max_x), 
   end;
   
elseif strcmp(action,'close'),
   
   close
   
end