function volvec(action);
%VOLVEC Demonstrates Volume Visualization in MATLAB.
%   This window lets you see a variety of volume
%   visualizations in MATLAB by simply pushing 
%   the buttons on the right side of the window. 
%   The commands that generate the plots are 
%   shown in the MiniCommand Window.                   
%
%   If you like, you can even directly edit the  
%   commands in the MiniCommand Window.          
%   Type control-return to execute code in the   
%   MiniCommand Window.    
%
%   See also ISOSURFACE, STREAMTUBE, STREAMRIBBON, 
%            STREAMLINE, CONEPLOT.

%   Ned Gulley, 6-21-93
%   Copyright 1984-2000 The MathWorks, Inc. 
%   $Revision: 1.6 $  $Date: 2000/08/29 18:56:02 $

if nargin<1,
    action='initialize';
end;

if strcmp(action,'initialize'),
    figNumber=figure( ...
        'Name','Examples of Volume and Vector Visualization in MATLAB', ...
        'NumberTitle','off', ...
        'Visible','off');
    % Give the figure a nice black background
    colordef(figNumber,'black')
    axes( ...
        'Units','normalized', ...
        'Position',[0.10 0.45 0.60 0.45]);
  %  clf;
    %===================================
    % Set up the MiniCommand Window
    top=0.35;
    left=0.05;
    right=0.75;
    bottom=0.05;
    labelHt=0.05;
    spacing=0.005;
    promptStr=str2mat(' ',' % Press the buttons at the right to see', ...
        ' % examples of some Volume and Vector Visualizations in MATLAB');
    % First, the MiniCommand Window frame
    frmBorder=0.02;
    frmPos=[left-frmBorder bottom-frmBorder ...
        (right-left)+2*frmBorder (top-bottom)+2*frmBorder];
    uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
        'BackgroundColor',[0.50 0.50 0.50]);
    % Then the text label
    labelPos=[left top-labelHt (right-left) labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',[0.50 0.50 0.50], ...
        'ForegroundColor',[1 1 1], ...
        'String','MiniCommand Window');
    % Then the editable text field
    mcwPos=[left bottom (right-left) top-bottom-labelHt-spacing];
    mcwHndl=uicontrol( ...
        'Style','edit', ...
        'HorizontalAlignment','left', ...
        'Units','normalized', ...
        'Max',10, ...
        'BackgroundColor',[1 1 1], ...
        'Position',mcwPos, ...
        'Callback','volvec(''eval'')', ...
        'String',promptStr);
    % Save this handle for future use
    set(gcf,'UserData',mcwHndl);

    %====================================
    % Information for all buttons
    labelColor=[0.8 0.8 0.8];
    top=0.95;
    left=0.80;
    btnWid=0.15;
    btnHt=0.08;
    % Spacing between the button and the next command's label
    spacing=0.03;

    %====================================
    % The CONSOLE frame
    frmBorder=0.02;
    yPos=0.05-frmBorder;
    frmPos=[left-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
    uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
        'BackgroundColor',[0.50 0.50 0.50]);
    
    %====================================
    % The Isosurface button
    btnNumber=1;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Isosurface';
    callbackStr='volvec(''grafbutton'')';
    cmdStr=str2mat(...
        ' % Isosurface of MRI data', ...
		' load mri; D = squeeze(D);', ...
		' [x y z D] = subvolume(D, [nan nan nan nan nan 4]);', ...
		' p = patch(isosurface(x,y,z,D, 5), ''FaceColor'', ''red'', ''EdgeColor'', ''none'');', ...
		' p2 = patch(isocaps(x,y,z,D, 5), ''FaceColor'', ''interp'', ''EdgeColor'', ''none'');', ...
		' isonormals(x,y,z,D,p);', ...
		' view(3); axis tight;  daspect([1 1 .4])', ...
		' colormap(gray(100))', ...
		' camva(6); box on' , ...
		' camlight(40, 40); camlight(-20,-10); lighting gouraud' ...
    );
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',cmdStr);
   
    %====================================
    % The Coneplot button
    btnNumber=2;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Coneplot';
    callbackStr='volvec(''grafbutton'')';
    cmdStr=str2mat( ...
        ' % Coneplot of wind data', ...
        ' load wind', ...
        ' [cx cy cz] = meshgrid(linspace(71,134,10),linspace(18,59,10),3:4:15);', ...
        ' daspect([1 1 1])', ...
        ' h=coneplot(x,y,z,u,v,w,cx,cy,cz,y,3);', ...
        ' set(h,''EdgeColor'', ''none'');', ...
		' colormap(hsv);',...
        ' axis tight; box on;  camproj perspective; ', ...
        ' camva(24); campos([175 10 85]); camtarget([105 40 0])', ...
        ' camlight left; lighting gouraud');
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',cmdStr);

    %====================================
    % The Streamline button
    btnNumber=3;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Streamline';
    callbackStr='volvec(''grafbutton'')';
    cmdStr=str2mat( ...
        ' % Streamlines of wind data', ...
        ' load wind', ...
        ' [sx sy sz] = meshgrid(80, 20:10:50, 0:5:15);', ...
        ' h=streamline(x,y,z,u,v,w,sx,sy,sz);', ...
        ' set(h, ''Color'', ''cyan'');', ...
        ' daspect([1 1 1])', ...
	    ' axis tight; box on;  camproj perspective; ', ...
        ' camva(24); campos([175 10 85]); camtarget([105 40 0])', ...
        ' camlight left; lighting gouraud');
	   
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',cmdStr);

	%====================================
    % The Streamtube button
    btnNumber=4;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Streamtube';
    callbackStr='volvec(''grafbutton'')';
    cmdStr=str2mat( ...
		' % Streamtubes of wind data', ...
		' % Useful for visualizing divergence of a vector field', ...
        ' load wind', ...
       	' [sx sy sz] = meshgrid(80, [20 30 40], [5 10]);', ...
	    ' daspect([1,1,1]);',...
	    ' h=streamtube(x,y,z,u,v,w,sx,sy,sz);', ...
		' set(h,''facecolor'',''cyan'',''edgecolor'',''none'');',...         
	    ' axis tight; box on;  camproj perspective; ', ...
		' axis([70 138 17 60 2.5 16]);',...
        ' camva(24); campos([175 10 85]); camtarget([105 40 0])', ...
        ' camlight left; lighting gouraud');
				
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',cmdStr);

	    
	%====================================
    % The Streamribbon button
    btnNumber=5;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Streamribbon';
    callbackStr='volvec(''grafbutton'')';
	cmdStr=str2mat( ...
		' % Streamribbons of wind data', ...
        ' % Useful for visualizing curl of a vector field', ...
        ' load wind', ...
    	' [sx sy sz] = meshgrid(80, [20 30 40], [5 10]);', ...
		' daspect([1,1,1]);',...
    	' h=streamribbon(x,y,z,u,v,w,sx,sy,sz);', ...
		' set(h,''facecolor'',''cyan'',''edgecolor'',''none'')',...   
	    ' axis tight; box on;  camproj perspective; ', ...
	    ' axis([70 138 17 60 2.5 16]);',...
        ' camva(24); campos([175 10 85]); camtarget([105 40 0])', ...
        ' camlight left; lighting gouraud');
	
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',cmdStr);
	
	
    %====================================
    % The Multi-technique button
    btnNumber=6;
    yPos=top-(btnNumber-1)*(btnHt+spacing);
    labelStr='Multiple';
    callbackStr='volvec(''grafbutton'')';
    cmdStr=str2mat( ...
        ' % Isosurface, isocaps, coneplot, and streamlines of wind data', ...
        ' load wind', ...
        ' spd = sqrt(u.*u + v.*v + w.*w);', ...
        ' p = patch(isosurface(x,y,z,spd, 40));', ...
        ' isonormals(x,y,z,spd, p)', ...
        ' set(p, ''FaceColor'', ''red'', ''EdgeColor'', ''none'');', ...
        ' p2 = patch(isocaps(x,y,z,spd, 40));', ...
        ' set(p2, ''FaceColor'', ''interp'', ''EdgeColor'', ''none'')',...
        ' daspect([1 1 1]);', ...
        ' [f verts] = reducepatch(isosurface(x,y,z,spd, 30), .2); ', ...
        ' h=coneplot(x,y,z,u,v,w,verts(:,1),verts(:,2),verts(:,3),2);', ...
        ' set(h, ''FaceColor'', ''cyan'', ''EdgeColor'', ''none'');', ...
        ' [sx sy sz] = meshgrid(80, 20:10:50, 0:5:15);', ...
        ' h2=streamline(x,y,z,u,v,w,sx,sy,sz);', ...
        ' set(h2, ''Color'', [.4 1 .4]);', ...
		' colormap(jet)', ...
        ' axis tight; box on', ...
        ' camproj perspective; camva(24); ',...
	    ' campos([165 -20 65]); camtarget([100 40 -5])', ...
        ' camlight left; lighting gouraud' );
    
    % Generic button information
    btnPos=[left yPos-btnHt btnWid btnHt];
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelStr, ...
        'Callback',callbackStr, ...
        'UserData',cmdStr);

    %====================================
    % The INFO button
    uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'Position',[left bottom+btnHt+spacing btnWid btnHt], ...
        'String','Info', ...
        'Callback','volvec(''info'')');

    %====================================
    % The CLOSE button
    uicontrol( ...
        'Style','push', ...
        'Units','normalized', ...
        'Position',[left bottom btnWid btnHt], ...
        'String','Close', ...
        'Callback','close(gcf)');

    % Now uncover the figure
    set(figNumber,'Visible','on');

elseif strcmp(action,'grafbutton'),
    cla reset;
    cmdStr=get(gco,'UserData');
    mcwHndl=get(gcf,'UserData');
    set(mcwHndl,'String',cmdStr);
    evalmcw(mcwHndl);

elseif strcmp(action,'eval'),
    cla reset;
    mcwHndl=get(gcf,'UserData');
    evalmcw(mcwHndl);

elseif strcmp(action,'info'),
    helpwin(mfilename);  
	
end;    % if strcmp(action, ...
