function matrot2(fn);

%    matrot2: Matrix transformations on the unit square
%    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\

%fn will be used to call the functions below.  They are:
%
%	InitVects:	Initializes all of the vectors and the axes
%	EvalProj:	Gets the matrix from the GUI boxes and evaluates the points
%	PlotBox:	Plots the new box and updates the old one
%	SetAxis:	Sets the axis properties to the user data

%The plotting of this function will be done by directly changing the xdata and
%ydata properties of the plot.  The plan is to tag all of the objects I want 
%to be able to modify and then use the findobj command to retieve them.  
%The handles that will need to be tracked are:
%
%	h_MainFig:	The figure the GUI is in
%	h_XMax:		Max X component of the axis
%	h_XMin:		Min X component of the axis
%	h_YMax:		Max Y component of the axis
%	h_YMin:		Min Y component of the axis
%	h_11data:	the 1,1 element of the trasformation matrix
%	h_12data:	the 1,2 element
%	h_21data:	the 2,1 element
%	h_22data:	the 2,2 element
%	h_axis:		The axis
%	h_Plot:		The unit box or the transformed unit box
%	h_OldPlot:	The old unit box or transformed unit box
%
%
%A note:  The userdata corresponding to h_Plot will have a 2xn matrix
%	  corresponding to the data points to be plotted.  The first
%	  row will be the x data and the second row will be y data.
%________________________________________________________________


%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 Matrix Rotations 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'),'Matrix Transformations')
		h_figs=get(0,'children');
		h_MainFig = findobj(h_figs,'flat',...
				    'name','Matrix Transformations');

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

		if length(h_MainFig)==0
			matrot2('init');
			matrot2(fn);
			return;
		end;
	end;
end;

%________________________________________________________________
%Function: GenMatrix

if strcmp(fn,'GenMatrix')

	h_11data=findobj('Tag','h_11data');
	h_12data=findobj('Tag','h_12data');
	h_21data=findobj('Tag','h_21data');
	h_22data=findobj('Tag','h_22data');

%Get what kind of matrix to generate from h_11data userdata

	MatrixType=get(h_11data,'User');

%Run GMatrx22

	GMatrx22(MatrixType,[h_11data;h_21data;h_12data;h_22data]);

	return;	
end;

%________________________________________________________________

%Function: InitVects

	if strcmp(fn,'InitVects')
		
	%Get handles for h_XMin, h_XMax, h_YMin, h_YMax, h_axis

		h_XMin=findobj('Tag','h_XMin');
		h_XMax=findobj('Tag','h_XMax');
		h_YMin=findobj('Tag','h_YMin');
		h_YMax=findobj('Tag','h_YMax');
		h_axis=gca;
		hold off;

	%Initialize h_Plot		

		plot([0 1 1 0 0],[0 0 1 1 0],...
		    'tag','h_Plot');
		hold on;

	%Initialize h_OldPlot

		plot(0,...
			'tag','h_OldPlot',...
			'xdata',[],...
			'ydata',[],...
			'color',[0 1 1]);

	%Set Initial values in XY Limit boxes

		set(h_XMin,'String','-2');
		set(h_XMax,'String','2');
		set(h_YMin,'String','-3');
		set(h_YMax,'String','3');
		set(h_axis,'xlim',[-2 2],...
			   'ylim',[-3 3]);		
		return;
	end
%________________________________________________________________

%Function: EvalProj

	if strcmp(fn,'EvalProj')

	%Gets handles for h_11data,h_12data,h_21data,h_22data,h_Plot

		h_11data=findobj('Tag','h_11data');
		h_12data=findobj('Tag','h_12data');
		h_21data=findobj('Tag','h_21data');
		h_22data=findobj('Tag','h_22data');
		h_Plot=findobj('Tag','h_Plot');

	%Defines the elements of p as the numbers the users entered.

		p=[eval(get(h_11data,'string')) ...
		eval(get(h_12data,'string')); ...
		eval(get(h_21data,'string')) ...
		eval(get(h_22data,'string'))];

	%Evaluates the new data and stores in the the UserData property
	%of Plot

		xydata=[get(h_Plot,'xdata');get(h_Plot,'ydata')];
		set(h_Plot,'UserData',p*xydata);
		return;
	end
%________________________________________________________________

%Function: DefinePoints

	if strcmp(fn,'DefinePoints')

	%Get handles of h_axis, h_Plot, and h_OldPlot

		h_Plot=findobj('Tag','h_Plot');
		h_axis=findobj('Tag','h_axis');
		h_OldPlot=findobj('Tag','h_OldPlot');
		h_RefreshButton=findobj('Tag','h_RefreshButton');

		set([h_Plot h_OldPlot],'xdata',NaN,...
					     'ydata',NaN);

		set(h_axis,'Button','matrot2(''PutPoint'');');
		set(h_RefreshButton,'string','Click Here to Finish',...
					  'CallBack','matrot2(''ClickFinish'');');
		set(h_Plot,'Button','matrot2(''PutPoint'');');
		return;	
	end;
		
%________________________________________________________________

%Function: PutPoint

	if strcmp(fn,'PutPoint')

	%Get handles of h_axis, h_Plot, and h_OldPlot

		h_Plot=findobj('Tag','h_Plot');
		h_axis=findobj('Tag','h_axis');

		CurPoint=get(h_axis,'CurrentPoint');
		xdata=get(h_Plot,'xdata');
		ydata=get(h_Plot,'ydata');
		set(h_Plot,'xdata',[xdata CurPoint(1,1)],...
			     'ydata',[ydata CurPoint(1,2)]);

		return;
	end;

%________________________________________________________________

%Function: ClickFinish

	if strcmp(fn,'ClickFinish')

	%Going to need h_axis, h_Plot, and h_Refresh

		h_Plot=findobj('Tag','h_Plot');
		h_axis=findobj('Tag','h_axis');
		h_RefreshButton=findobj('Tag','h_RefreshButton');

		set(h_RefreshButton,'string','Click to enter points',...
					  'CallBack','matrot2(''DefinePoints'');');
		xdata=get(h_Plot,'xdata');
		ydata=get(h_Plot,'ydata');
		set(h_Plot,'xdata',[xdata xdata(2)],...
			     'ydata',[ydata ydata(2)]);
		set(h_axis,'button','');
		return;
	end;
%________________________________________________________________

%Function: PlotBox

	if strcmp(fn,'PlotBox')
		
	%Get handles for h_Plot and h_OldPlot
	
		h_Plot=findobj('Tag','h_Plot');
		h_OldPlot=findobj('Tag','h_OldPlot');

	%Updates OldPlot's xdata and ydata properties
		set(h_OldPlot,...
			'xdata',get(h_Plot,'xdata'),...
			'ydata',get(h_Plot,'ydata'));

	%Updates Plot's xdata and ydata properties

		xydata=get(h_Plot,'UserData');
		set(h_Plot,...
			'xdata',xydata(1,:),...
			'ydata',xydata(2,:));
		matrot2('SetAxis');
		return;
	end;
%________________________________________________________________

%Function: SetAxis

	if strcmp(fn,'SetAxis')
	
	%Gets handles h_XMin,h_XMax,h_Ymin,h_YMax

		h_axis=findobj('Tag','h_axis');
		h_XMin=findobj('Tag','h_XMin');
		h_XMax=findobj('Tag','h_XMax');
		h_YMin=findobj('Tag','h_YMin');
		h_YMax=findobj('Tag','h_YMax');

	%Sets the xlim and ylim properties for axis 

		set(h_axis,'xlim',...
			[eval(get(h_XMin,'string')) eval(get(h_XMax,'string'))]);
		set(h_axis,'ylim',...
			[eval(get(h_YMin,'string')) eval(get(h_YMax,'string'))]);
		return
	end;
%________________________________________________________________

%Fucntion: TrackPoint

	if strcmp(fn,'TrackPoint')

	%Grab the axis and tracker handles

		h_axis=findobj('tag','h_axis');
		h_Tracker=findobj('tag','h_Tracker');

	%Get the current point and update track readout.

		CurPoint=get(h_axis,'CurrentPoint');
		set(h_Tracker,'string',[num2str(CurPoint(1,1)) ', ' num2str(CurPoint(1,2))]);

		return
	end


%________________________________________________________________

%Function: init

	if strcmp(fn,'init')

	%Before doing anything, kill any copy of this already running

		h_figs=get(0,'Children');
		for index=h_figs'
				if strcmp(get(h_figs(index),'name'),'Matrix Transformations')
					delete(h_figs(index));
				end
		end

		h_MainFig=figure('NumberTitle','off',...
			'Name','Matrix Transformations',...
			'MenuBar','none');

%UIMenu Entries for matrix generation

		h_RootMatrix=uimenu(h_MainFig,...
				'label','Generate');

		uimenu(h_RootMatrix,...
			'label','Rotation Matrix',...
			'CallBack',['h1=findobj(''tag'',''h_11data'');'...
				    'set(h1,''user'',''Rotation'');'...
				    'matrot2(''GenMatrix'');']);

		uimenu(h_RootMatrix,...
			'label','Projection Matrix',...
			'CallBack',['h1=findobj(''tag'',''h_11data'');'...
				    'set(h1,''user'',''Projection'');'...
				    'matrot2(''GenMatrix'');']);

		uimenu(h_RootMatrix,...
			'label','Reflection Matrix',...
			'CallBack',['h1=findobj(''tag'',''h_11data'');'...
				    'set(h1,''user'',''Reflection'');'...
				    'matrot2(''GenMatrix'');']);

		uimenu(h_RootMatrix,...
			'label','Scalar Matrix',...
			'CallBack',['h1=findobj(''tag'',''h_11data'');'...
				    'set(h1,''user'',''Scalar'');'...
				    'matrot2(''GenMatrix'');']);

%h_axis (Tagged)

		axes('Position',[.1 .1 .4 .8],'Tag','h_axis');

% frame1 (Main Background)
	
		uicontrol(h_MainFig,...
		'units','normalize',...
		'BackgroundColor',[1 1 1],...
		'ForegroundColor',[1 1 1],...
		'Position',[.6,0,.4,1],...
		'style','frame');


% xtext1 (x-min)
	
		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','text',...
		'Position',[.68 .23 .1 .05],...
		'string','x-min',...
		'BackGroundColor',[1 1 1]);

% xtext2 (x-max)
		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','text',...
		'Position',[.68 .13 .1 .05],...
		'string','x-max',...
		'BackGroundColor',[1 1 1]);
	

%h_XMax (Tagged) Edit
	
		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','edit',...
		'Position',[.62 .13 .05 .05],...
		'foregroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'tag','h_XMax',...
		'CallBack','matrot2(''SetAxis'')');	

%h_XMin (Tagged) Edit
	
		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','edit',...
		'Position',[.62 .23 .05 .05],...
		'foregroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'tag','h_XMin',...
		'CallBack','matrot2(''SetAxis'')');
	
% Framex1 (Around xtext1)

		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.61 .22 .07 .07],...
		'style','frame');	

% Framex2 (Around xtext2)

		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.61 .12 .07 .07],...
		'style','frame');	

% ytext1 (y-min)

		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','text',...
		'Position',[.87 .23 .1 .05],...
		'string','y-min',...
		'BackGroundColor',[1 1 1]);

% ytext2 (y-max)

		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','text',...
		'Position',[.87 .13 .1 .05],...
		'string','y-max',...
		'BackGroundColor',[1 1 1]);

%h_YMax (Tagged) Edit
	
		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','edit',...
		'Position',[.8 .13 .05 .05],...
		'foregroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Tag','h_YMax',...
		'CallBack','matrot2(''SetAxis'')');

%h_YMin (Tagged)

		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','edit',...
		'Position',[.8 .23 .05 .05],...
		'foregroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Tag','h_YMin',...
		'CallBack','matrot2(''SetAxis'')');

% FrameY (Around ytext1)
	
		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.79 .22 .07 .07],...
		'style','frame');		

% FrameY2 (Around ytext2)
	
		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.79 .12 .07 .07],...
		'style','frame');	

% text3 

		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.61 .32 .38 .05],...
		'String','Adjust Axes',...
		'Style','text');

%frame4

		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.6 0 .4 .4],...
		'style','frame');	

%Tracker(Tagged)

		uicontrol(h_MainFig,...
		'units','normal',...
		'Fore',[0 0 0],...
		'Back',[1 1 1],...
		'style','text',...
		'tag','h_Tracker',...
		'pos',[.8 .41 .18 .05]);

%Text for Tracker

		uicontrol(h_MainFig,...
		'units','normal',...
		'fore',[0 0 0],...
		'back',[1 1 1],...
		'style','text',...
		'string','Current Point:',...
		'pos',[.62 .41 .16 .05]);
	
%h_11data (Tagged) Edit

		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','edit',...
		'Position',[.62 .6 .16 .05],...
		'foregroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Tag','h_11data',...
		'string','0');
	
% Frame11

		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.61 .59 .18 .07],...
		'style','frame');	

%h_12data (Tagged) Edit

		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','edit',...
		'Position',[.82 .6 .16 .05],...
		'foregroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Tag','h_12data',...
		'string','0');
	
% Frame12

		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.81 .59 .18 .07],...
		'style','frame');	

%h_21data (Tagged) Edit

		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','edit',...
		'Position',[.62 .5 .16 .05],...
		'foregroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Tag','h_21data',...
		'string','0');
	
% Frame21

		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.61 .49 .18 .07],...
		'style','frame');		

%h_22data (Tagged)

		uicontrol(h_MainFig,...
		'units','normalize',...
		'style','edit',...
		'Position',[.82 .5 .16 .05],...
		'foregroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Tag','h_22data',...
		'string','0');
	
% Frame22

		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.81 .49 .18 .07],...
		'style','frame');	

%h_MatrixText

		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.61 .73 .38 .05],...
		'String','Enter the Matrix Elements.',...
		'Style','text',...
		'Tag','h_MatrixText');

% frame5
	
		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.6 .4 .4 .42],...
		'style','frame');		

% PlotButton
	
		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForegroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.64 .84 .32 .07],...
		'string','Apply Matrix',...
		'CallBack','matrot2(''EvalProj'');matrot2(''PlotBox'')'); 


% ResetButton

		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForeGroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.64 .92 .32 .07],...
		'string','Reset',...
		'CallBack','matrot2(''InitVects'')');

% RefreshButton
	
		uicontrol(h_MainFig,...
		'units','normalize',...
		'ForeGroundColor',[0 0 0],...
		'BackGroundColor',[1 1 1],...
		'Position',[.64 .025 .32 .07],...
		'string','Click to define points',...
		'CallBack','matrot2(''DefinePoints'');',...
		'Tag','h_RefreshButton');


		set(h_MainFig,'WindowButtonMotionFcn',...
				  'matrot2(''TrackPoint'');');

%Init and Plot Unit Vectors

		matrot2('InitVects');
	end
end
