% Object Builder -- calls MakeSurface, Plot2D, ResetSpline
% function MakeSurface(fx,fy,fz,size,edgeflag,styleflag,alt,azm,res)
% function [X,Y] = Plot2D(f1,f2,res)
% function [X,Y] = ResetSpline;

clear all;
close;

% Default values

% Axis size
size = 2;

% No edges
edgeflag = 0 ;

% Create body by rotation (rather than by scaling)
styleflag = 0;

% View angles
alt = 30 ;
azm = 120;

% Resolution
res = 30;

% Function strings
% sf1 and sf2 define the shape of rotation
% pf1 and pf2 define the path of rotation
sf1 = 'sin(t)';
sf2 = 'cos(t)';
pf1 = 'sin(t)';
pf2 = 'cos(t)';

%== Shape window ==========================
sfig=figure(1);
clf;

%== Window setup ===========================
set(sfig,'Position',[10,100,235,450],...
'Name','Shape of Rotation',...
'NumberTitle','off',...
'Resize', 'off', ...
'Color','white');

%== Drawing square ==============================
set(0, 'DefaultAxesCreateFcn',...
'[sX,sY] = Plot2D(sf1,sf2,res);');

%== Shape funtion string 1 input ===========================
[sX,sY] = Plot2D(sf1,sf2,res);
ss1 = uicontrol(gcf,'Style','edit', ...
'String', sf1 ,...
'HorizontalAlignment','Center', ...
'Position',[10,410,215,30], ...
'BackgroundColor','white', ...
'FontSize', 14, ...
'CallBack', ...
['sf1 = get(ss1,''String'');' ...
'[sX,sY] = Plot2D(sf1,sf2,res);' ...
] );

%== Shape funtion string 2 input ===========================
ss2 = uicontrol(gcf,'Style','edit', ...
'String', sf2 ,...
'HorizontalAlignment','Center', ...
'Position',[10,370,215,30], ...
'BackgroundColor','white', ...
'FontSize', 14, ...
'CallBack', ...
['sf2 = get(ss2,''String'');' ...
'[sX,sY] = Plot2D(sf1,sf2,res);' ...
] );

%== Spline button ===========================
ssp = uicontrol(gcf,'Style','Pushbutton', ...
'String','Reset Spline',...
'Position',[30,25,70,20],...
'BackgroundColor',[0.8,0.8,0.8], ...
'CallBack', ...
'[sX,sY] = ResetSpline;' );

%== Close button ===========================
sc = uicontrol(gcf,'Style','Pushbutton', ...
'String','Close',...
'Position',[135,25,70,20],...
'BackgroundColor',[0.8,0.8,0.8], ...
'CallBack', ...
'close' );

%== Path window ==========================
pfig=figure(2);
clf

%== Window setup ===========================
set(pfig,'Position',[250,100,235,450],...
'Name','Path of Rotation',...
'NumberTitle','off',...
'Resize', 'off', ...
'Color','white');

%== Drawing square ==============================
set(0, 'DefaultAxesCreateFcn',...
'[pX,pY] = Plot2D(pf1,pf2,res);');

%== Path funtion string 1 input ===========================
[pX,pY] = Plot2D(pf1,pf2,res);
ps1 = uicontrol(gcf,'Style','edit', ...
'String', pf1 ,...
'HorizontalAlignment','Center', ...
'Position',[10,410,215,30], ...
'BackgroundColor','white', ...
'FontSize', 14, ...
'CallBack', ...
['pf1 = get(ps1,''String'');' ...
'[pX,pY] = Plot2D(pf1,pf2,res);' ...
] );

%== Path funtion string 2 input ===========================
ps2 = uicontrol(gcf,'Style','edit', ...
'String', pf2 ,...
'HorizontalAlignment','Center', ...
'Position',[10,370,215,30], ...
'BackgroundColor','white', ...
'FontSize', 14, ...
'CallBack', ...
['pf2 = get(ps2,''String'');' ...
'[pX,pY] = Plot2D(pf1,pf2,res);' ...
] );

%== Spline button ===========================
psp = uicontrol(gcf,'Style','Pushbutton', ...
'String','Reset Spline',...
'Position',[30,25,70,20],...
'BackgroundColor',[0.8,0.8,0.8], ...
'CallBack', ...
'[pX,pY] = ResetSpline;' );

%== Close button ===========================
pc = uicontrol(gcf,'Style','Pushbutton', ...
'String','Close',...
'Position',[135,25,70,20],...
'BackgroundColor',[0.8,0.8,0.8], ...
'CallBack', ...
'close' );

%== The main window ==========================
fig=figure(3);
clf

%== Window setup ===========================
set(fig,'Position',[490,100,320,450],...
'Name','Parametric Surface Maker',...
'NumberTitle','off',...
'Resize', 'off', ...
'Color','white');

%== Drawing box ==============================
set(0, 'DefaultAxesCreateFcn', ...
'MakeSurface(sX,sY,pX,pY,size,edgeflag,styleflag,alt,azm,res)')
ah = axes( 'Position', [0.1 0.15, 0.8 0.6],...
'Box', 'on', ...
'Color','white',...
'XColor',[0,0,0],'YColor',[0,0,0]);
axis square

%=== Header text ==========================
t1 = uicontrol(gcf,'Style','text', ...
'String',...
'Parametric Surface Maker ',...
'HorizontalAlignment','Center',...
'Position',[120,460,290,30],...
'BackgroundColor','white', ...
'FontSize', 18 ...
);

%== Plot button ===========================
pt = uicontrol(gcf,'Style','Pushbutton', ...
'String','Plot',...
'Position',[30,25,70,20],...
'BackgroundColor',[0.8,0.8,0.8], ...
'CallBack', ...
'MakeSurface(sX,sY,pX,pY,size,edgeflag,styleflag,alt,azm,res);' );

%== Close button ===========================
c = uicontrol(gcf,'Style','Pushbutton', ...
'String','Close',...
'Position',[220,25,70,20],...
'BackgroundColor',[0.8,0.8,0.8], ...
'CallBack', ...
'close' );

%== Axis size slider ===========================
s1 = uicontrol(gcf,'Style','text', ...
'String','Axis size',...
'Position',[5,420,75,20],...
'BackgroundColor',[0.8,0.8,0.8], ...
'FontSize', 12 );
s2 = uicontrol(gcf,'Style','text',...
'String',num2str(size,1),...
'FontSize', 12, ...
'BackgroundColor',[0.8,0.8,0.8], ...
'Position',[5,400,75,20]);
s3 = uicontrol(gcf,...
'Style','slider',...
'Min' ,0.1 ,'Max', 10, ...
'Position',[5,380,75,20], ...
'Value', size,...
'CallBack', ...
['size = (get(s3,''Value''));' ...
'set(s2, ''String'',num2str(size));' ...
'MakeSurface(sX,sY,pX,pY,size,edgeflag,styleflag,alt,azm,res);' ...
] );

%== Altitude slider ===========================
a1 = uicontrol(gcf,'Style','text', ...
'String','Altitude',...
'Position',[83,420,75,20],...
'BackgroundColor',[0.8,0.8,0.8], ...
'FontSize', 12 );
a2 = uicontrol(gcf,'Style','text',...
'String',num2str(alt,2),...
'FontSize', 12, ...
'BackgroundColor',[0.8,0.8,0.8], ...
'Position',[83,400,75,20]);
a3 = uicontrol(gcf,...
'Style','slider',...
'Min' ,0 ,'Max', 90, ...
'Position',[83,380,75,20], ...
'Value', alt,...
'CallBack', ...
['alt = (get(a3,''Value''));' ...
'set(a2, ''String'',num2str(alt));' ...
'MakeSurface(sX,sY,pX,pY,size,edgeflag,styleflag,alt,azm,res);' ...
] );

%== Azimuth slider ===========================
az1 = uicontrol(gcf,'Style','text', ...
'String','Azimuth',...
'Position',[161,420,75,20],...
'BackgroundColor',[0.8,0.8,0.8], ...
'FontSize', 12 );
az2 = uicontrol(gcf,'Style','text',...
'String',num2str(azm,2),...
'FontSize', 12, ...
'BackgroundColor',[0.8,0.8,0.8], ...
'Position',[161,400,75,20]);
az3 = uicontrol(gcf,...
'Style','slider',...
'Min' ,0 ,'Max', 180, ...
'Position',[161,380,75,20], ...
'Value', azm,...
'CallBack', ...
['azm = (get(az3,''Value''));' ...
'set(az2, ''String'',num2str(azm));' ...
'MakeSurface(sX,sY,pX,pY,size,edgeflag,styleflag,alt,azm,res);' ...
] );

%== Resolution slider ===========================
r1 = uicontrol(gcf,'Style','text', ...
'String','Resolution',...
'Position',[239,420,75,20],...
'BackgroundColor',[0.8,0.8,0.8], ...
'FontSize', 12 );
r2 = uicontrol(gcf,'Style','text',...
'String',num2str(res,2),...
'FontSize', 12, ...
'BackgroundColor',[0.8,0.8,0.8], ...
'Position',[239,400,75,20]);
r3 = uicontrol(gcf,...
'Style','slider',...
'Min' ,5 ,'Max', 100, ...
'Position',[239,380,75,20], ...
'Value', res,...
'CallBack', ...
['res = (get(r3,''Value''));' ...
'set(r2, ''String'',num2str(res));' ...
'[sX,sY] = Plot2D(sf1,sf2,res);' ...
'[pX,pY] = Plot2D(pf1,pf2,res);' ...
'MakeSurface(sX,sY,pX,pY,size,edgeflag,styleflag,alt,azm,res);' ...
] );

%== Edge Menu ===========================
m1 = uimenu (gcf,'Label', 'Shading') ;
uimenu (m1, 'Label', 'Light Surface', ...
'CallBack', 'edgeflag=0; MakeSurface(sX,sY,pX,pY,size,edgeflag,styleflag,alt,azm,res);')
uimenu (m1, 'Label', 'Show Edges', ...
'CallBack', 'edgeflag=1; MakeSurface(sX,sY,pX,pY,size,edgeflag,styleflag,alt,azm,res);')

%== Style Menu ===========================
m2 = uimenu (gcf,'Label', 'Creation Style') ;
uimenu (m2, 'Label', 'Rotate Body', ...
'CallBack', 'styleflag=0; MakeSurface(sX,sY,pX,pY,size,edgeflag,styleflag,alt,azm,res);')
uimenu (m2, 'Label', 'Scale Body', ...
'CallBack', 'styleflag=1; MakeSurface(sX,sY,pX,pY,size,edgeflag,styleflag,alt,azm,res);')