function ReturnVal=trans32(fn,arg);

%    trans32: Transformation Pictures 3x2
%    Copyright (C) 1996  Brad North

%    This program is free software; you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation; either version 2 of the License, or
%    (at your option) any later version.

%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.

%    You should have received a copy of the GNU General Public License
%    along with this program; if not, write to the Free Software
%    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

%    Problems, Comments, and Suggestions can be sent to 
%            northbh@nextwork.rose-hulman.edu

%If there are no input strings, initialize the gui

if nargin == 0
	fn = 'init';
end;


%Activates the Matrix Rotations figure and makes sure it has
%been properly initialized

if ~strcmp(fn,'init')
	h_MainFig = gcf;

%If the current figure is not named Transformation Pictures then find 
%the figure that is and put its handle in h_MainFig. This will 
%be found by checking the children of root.

	if ~strcmp(get(h_MainFig,'name'),'Transformation Pictures 3x2')
		h_figs=get(0,'children');
		h_MainFig = findobj(h_figs,'flat',...
				    'name','Transformation Pictures 3x2');

	%If the figure does not exist, initialize it and then
	%run the function that was originally intended

		if length(h_MainFig)==0
			trans32('init');
			trans32(fn);
			return;
		end;
	end;
end;
%_______________________________________________________________________________

%Function: EvalMatrix
%
%(3)	h_data11:	element (1,1)
%(4)	h_data12:	element (1,2)
%(5)	h_data21:	element (2,1)
%(6)	h_data22:	element (2,2)
%(8)	h_data31:	element (3,1)
%(9)	h_data32:	element (3,2) 

if strcmp(fn,'EvalMatrix')

	h_data=get(h_MainFig,'UserData');
	h_data11=h_data(3);
	h_data12=h_data(4);
	h_data21=h_data(5);
	h_data22=h_data(6);
	h_data31=h_data(15);
	h_data32=h_data(16);

	a(1,1)=eval(get(h_data11,'string'));
	a(1,2)=eval(get(h_data12,'string'));
	a(2,1)=eval(get(h_data21,'string'));
	a(2,2)=eval(get(h_data22,'string'));
	a(3,1)=eval(get(h_data31,'string'));
	a(3,2)=eval(get(h_data32,'string'));
	
	ReturnVal=a;
	return;
end;
%_______________________________________________________________________________

%Function: Null

if strcmp('Null',fn)

	h_data=get(h_MainFig,'UserData');	
	h_axis=h_data(1);
	h_axis2=h_data(2);
	h_Nx=h_data(13);
	h_Ny=h_data(14);

	xlim=get(h_axis,'xlim');
	ylim=get(h_axis,'ylim');

	axes(h_axis);
	A=trans32('EvalMatrix');
	Na=null(A);

	if size(Na) == [2 2]

		delete(get(h_axis,'children'));
		set(h_axis,'color',[1 0 0]);
		set(h_Nx,'string','1	0');
		set(h_Ny,'string','0	1');

	elseif length(Na) == 0

		set(h_axis,'color',[0 0 0]);
		delete(get(h_axis,'children'));
		plot(0,0,'ro','linewidth',1.5);
		set(h_axis,'xlim',xlim,...
			   'ylim',ylim);
		set(h_Nx,'string','0');
		set(h_Ny,'string','0');

	else

		scalar=min(abs(Na));
		if scalar ~= 0
			Na(1)=Na(1)/scalar;
			Na(2)=Na(2)/scalar;
		end;
	
		set(h_axis,'color',[0 0 0]);
		delete(get(h_axis,'children'));
		plot([-3*Na(1) 3*Na(1)],[-3*Na(2) 3*Na(2)],'r');
		set(h_axis,'xlim',xlim,...
			   'ylim',ylim);
		set(h_Nx,'string',num2str(Na(1)));
		set(h_Ny,'string',num2str(Na(2)));

	end;
	
	title('N(A)','FontName','Monotype Corsiva','FontSize',16);
	trans32('Range',A);
	return;


end;				
%_______________________________________________________________________________

%Function: Range

if strcmp(fn,'Range')

	h_data=get(h_MainFig,'UserData');
	h_axis=h_data(2);
	h_Rx=h_data(7);
	h_Ry=h_data(8);
	h_Rz=h_data(9);
	h_Rx2=h_data(10);
	h_Ry2=h_data(11);
	h_Rz2=h_data(12);
	axes(h_axis);
	delete(get(h_axis,'children'));
	
	a=arg;
	Ra=orth(a);

	xlim=get(h_axis,'xlim');
	ylim=get(h_axis,'ylim');
	zlim=get(h_axis,'zlim');
	
	if size(Ra) == [3 2]

	
		colormap cool
		set(h_axis,'xlim',xlim,...
			   'ylim',ylim);
		set(h_Rx,'string',num2str(Ra(1,1)));
		set(h_Ry,'string',num2str(Ra(2,1)));
		set(h_Rz,'string',num2str(Ra(3,1)));
		set(h_Rx2,'string',num2str(Ra(1,2)));
		set(h_Ry2,'string',num2str(Ra(2,2)));
		set(h_Rz2,'string',num2str(Ra(3,2)));

		[x,y]=meshgrid(linspace(-1.5,1.5,20),linspace(-1.5,1.5,20));
		ZeroGrid=zeros(20,20);

		if (Ra(1,1)==0) & (Ra(2,1)==0)

			mesh(ZeroGrid,x,y);

		elseif (Ra(2,1)==0) & (Ra(2,2)==0)

			mesh(x,ZeroGrid,y);

		elseif (Ra(3,1)==0) & (Ra(3,2)==0)

			mesh(x,y,ZeroGrid);

		else

			PlaneA=[Ra(1,1) Ra(2,1);Ra(2,1) Ra(2,2)];
			PlaneB=[Ra(3,1);Ra(3,2)];
			Slopes=PlaneA\PlaneB;
			
			z=Slopes(1)*x+Slopes(2)*y;
			mesh(x,y,z);
		end;

	elseif length(Ra) == 0

		set(h_axis,'color',[0 0 0]);
		plot3(0,0,0,'co','linewidth',1.5);
		set(h_axis,'xlim',xlim,...
			   'ylim',ylim,...
			   'zlim',zlim);
		set(h_Rx,'string','0');
		set(h_Ry,'string','0');
		set(h_Rz,'string','0');
		set(h_Rx2,'string','');
		set(h_Ry2,'string','');
		set(h_Rz2,'string','');
		

	else
	
		scalar=min(abs(Ra));

		plot3([-3*Ra(1) 3*Ra(1)],[-3*Ra(2) 3*Ra(2)],[-3*Ra(3) 3*Ra(3)],'c');
		set(h_axis,'xlim',xlim,...
			   'ylim',ylim,...
			   'zlim',zlim);
		set(h_Rx,'string',num2str(Ra(1)));
		set(h_Ry,'string',num2str(Ra(2)));
		set(h_Rz,'string',num2str(Ra(3)));
		set(h_Rx2,'string','');
		set(h_Ry2,'string','');
		set(h_Rz2,'string','');

	end;

	title('R(A)','FontName','Monotype Corsiva','FontSize',16);
	return;

end;

	
%_______________________________________________________________________________


%Function: init

if strcmp('init',fn)



	h_fig=	figure('Tag','vla',...
		'Name','Transformation Pictures 3x2',...
		'NumberTitle','off');

	h_axis=	axes('position',[.07 .45 .4 .45]);

	h_axis2=axes('position',[.57 .45 .4 .45]);

	set(h_axis,...
		'XLim',[-1.5 1.5],'YLim',[-1.5 1.5]);

	set(h_axis2,...
		'XLim',[-1.5 1.5],'YLim',[-1.5 1.5],...
		'ZLim',[-1.5 1.5]);

	view(3);

%Now the gui

		uicontrol(h_fig,...
		'position',[0 0 1 .35],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','frame');

		uicontrol(h_fig,...
		'position',[.5 .35 .003 .65],...
		'BackgroundColor',[1 1 1],...
		'ForeGroundColor',[1 1 1],...
		'units','normalize',...
		'style','frame');
	
		uicontrol(h_fig,...
		'position',[.5 0 .003 .35],...
		'BackgroundColor',[0 0 0],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','frame');

		uicontrol(h_fig,...
		'position',[.1 .27 .23 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','text',...
		'string','Enter matrix A');
	
		uicontrol(h_fig,...
		'position',[.35 .27 .13 .07],...
		'units','normalize',...
		'string','Plot',...
		'CallBack','trans32(''Null'');');

	h_data11=uicontrol(h_fig,...
		'position',[.05 .2 .17 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundcolor',[0 0 0],...
		'units','normalize',...
		'style','edit',...
		'string','0');

		uicontrol(h_fig,...
		'position',[.04 .19 .19 .07],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','frame');
	
	h_data12=uicontrol(h_fig,...
		'position',[.25 .2 .17 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundcolor',[0 0 0],...
		'units','normalize',...
		'style','edit',...
		'string','0');

		uicontrol(h_fig,...
		'position',[.24 .19 .19 .07],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','frame');


	h_data21=uicontrol(h_fig,...
		'position',[.05 .12 .17 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundcolor',[0 0 0],...
		'units','normalize',...
		'style','edit',...
		'string','0');

		uicontrol(h_fig,...
		'position',[.04 .11 .19 .07],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','frame');
	
	h_data22=uicontrol(h_fig,...
		'position',[.25 .12 .17 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundcolor',[0 0 0],...
		'units','normalize',...
		'style','edit',...
		'string','0');

		uicontrol(h_fig,...
		'position',[.24 .11 .19 .07],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','frame');

	h_data31=uicontrol(h_fig,...
		'position',[.05 .04 .17 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundcolor',[0 0 0],...
		'units','normalize',...
		'style','edit',...
		'string','0');

		uicontrol(h_fig,...
		'position',[.04 .03 .19 .07],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','frame');


	h_data32=uicontrol(h_fig,...
		'position',[.25 .04 .17 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundcolor',[0 0 0],...
		'units','normalize',...
		'style','edit',...
		'string','0');

		uicontrol(h_fig,...
		'position',[.24 .03 .19 .07],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','frame');




%*************************************************

		uicontrol(h_fig,...
		'position',[.77 .27 .2 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','text',...
		'string','Range');

	h_Rx=	uicontrol(h_fig,...
		'position',[.76 .19 .1 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','text');

	h_Ry=	uicontrol(h_fig,...
		'position',[.76 .13 .1 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','text');

	h_Rz=	uicontrol(h_fig,...
		'position',[.76 .07 .1 .05],...
		'back',[1 1 1],...
		'units','normalize',...
		'style','text');

	h_Rx2=	uicontrol(h_fig,...
		'position',[.87 .19 .1 .05],...
		'Back',[1 1 1],...
		'units','normalize',...
		'style','text');
	
	h_Ry2=	uicontrol(h_fig,...
		'position',[.87 .13 .1 .05],...
		'Back',[1 1 1],...
		'units','normalize',...
		'style','text');

	h_Rz2=	uicontrol(h_fig,...
		'position',[.87 .07 .1 .05],...
		'Back',[1 1 1],...
		'units','normalize',...
		'style','text');

		uicontrol(h_fig,...
		'position',[.52 .27 .26 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','text',...
		'string','Nullspace');

	h_Nx=	uicontrol(h_fig,...
		'position',[.6 .15 .1 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','text');

	h_Ny=	uicontrol(h_fig,...
		'position',[.6 .10 .1 .05],...
		'BackGroundColor',[1 1 1],...
		'ForeGroundColor',[0 0 0],...
		'units','normalize',...
		'style','text');






%(1)	h_axis:		Left hand axis
%(2)	h_axis2:	Right hand axis
%(3)	h_data11:	element (1,1)
%(4)	h_data12:	element (1,2)
%(5)	h_data21:	element (2,1)
%(6)	h_data22:	element (2,2)
%(7)	h_Nx:		X component of nullspace
%(8)	h_Ny:		Y component of nullspace
%(9)	h_Rx:		X component of range
%(10)	h_Ry:		Y component of range
%	h_MainFig:	Main figure

	h_data(1)   =   h_axis;
	h_data(2)   =	h_axis2;
	h_data(3)   =	h_data11;
	h_data(4)   =	h_data12;
	h_data(5)   =	h_data21;
	h_data(6)   =	h_data22;
	h_data(7)   =	h_Rx;
	h_data(8)   =	h_Ry;
	h_data(9)   =	h_Rz;
	h_data(10)  =	h_Rx2;
	h_data(11)  =	h_Ry2;
	h_data(12)  =	h_Rz2;
	h_data(13)  =	h_Nx;
	h_data(14)  =	h_Ny;
	h_data(15)  =	h_data31;
	h_data(16)  =	h_data32;

	set(h_fig,'UserData',h_data);
end;

