% KDEDEMO3    Demonstrate UIControls with 3D Kernel Density Estimates.
%
%             Example: data=randn(500,3); (optional - defines data)
%                      kdedemo3
%
%             Menus: (a) DATA - A choice of several 3D data sets.
%                    (b) ROT. MOVIE - Create a rotational animation.
%                    (c) CONT. MOVIE - Create a contour based movie.
%                    (d) COLOUR - A choice of several colormaps.
%
%             Pop-ups: (a) PLOT - Choice of Percentage Surface Contour, Slice and Scatter plots.
%                      (b) HSELECT - A choice of several data-based h selection strategies.
%
%             Check-boxes: (a) SHADNG - Turns interpolated shading on/off.
%                          (b) LIGHTING - Turns lighting on/off.
%
%             Push buttons: (a) END - Quit demonstration,
%                           (b) VIEW2 - Default 2D view.
%                           (c) VIEW3 - Default 3D view.
%
%             Sliders: (a) LR - Sets azimuth angle of view,
%                      (b) UD - Sets elevation angle of view.
%                      (c)-(e) Slice sliders - Sets position of the three slices.
%
%             Editable text: (a) H - Click here to enter a value for H.  Values 
%                                of H=0 cause the routine to use an 'optimal' 
%                                value.  If entering >1 value use vector notation, 
%                                eg [2,1,1] or [2,0,1].
%                            (b) CONTOUR PERCENTAGE - default is 75.  Only one value
%                                is supported, i.e. no contour "shells".
% 
%             Christian C. Beardah 1997

echo off

load kddata;
load nmd3;

if exist('data')==0,
  data=randn(8,3);
  import=0;
  else,
    data=data(:,1:3);
    import=1;
end;

if exist('nmesh')==0,
  nmesh=32;
end;

xf=0.5; yf=0.5; zf=0.5;

mindata=min(data);
maxdata=max(data);

xmin=mindata(1);
ymin=mindata(2);
zmin=mindata(3);

xmax=maxdata(1);
ymax=maxdata(2);
zmax=maxdata(3);

xp=xmin+xf*(xmax-xmin);
yp=xmin+yf*(ymax-ymin);
zp=xmin+zf*(zmax-zmin);

% Initialise

cframes=18;
cinc=5;
rframes=24;
angle=360/rframes;

kernel='knorm';
azim=-37.5;
elev=30;
h=[hns(data(:,1),kernel) hns(data(:,2),kernel) hns(data(:,3),kernel)];
pc=75;
col='y';

figure('Name','Tri-variate KDE demo');

[Tx,Ty,Tz,X,Y,Z,f,d]=pccont3(pc,data,kernel,nmesh,h,col);
w=pclevel(pc,d);
const=w(1)
title(['h = ',num2str(h(1)),' , ',num2str(h(2)),' , ',num2str(h(3))]);
view(azim,elev);
axvec=axis;

% Callback strings

ends = 'clear global; clear; load kddata; load nmd3; close;';

smooths = 'if get(smoothh,''value'')==1, shading interp; else, shading faceted; end;';

lights = 'if get(lighth,''value'')==1, light; lighting gouraud; else, lighting none; end; ';

fills = 'fill3(Tx,Ty,Tz,col); hold off;';

dview2s = 'view(2); set(udc,''value'',90); set(lrc,''value'',0); azim=0; elev=90;';

dview3s = 'view(3); set(udc,''value'',30); set(lrc,''value'',-37.5); azim=-37.5; elev=30;';

moviers = 'M = moviein(rframes); for j=1:rframes, view(azim+j*angle,elev); M(:,j) = getframe; end; view(azim,elev);';

playrs = ['movie(M,10,10);',dview3s];

moviecs = 'N = moviein(cframes); for j=1:cframes, w=pclevel(5+j*cinc,d); const=w(1); [Tx,Ty,Tz]=surfcv(X,Y,Z,f,const,col); view(azim,elev); N(:,j) = getframe; end;';

playcs = ['movie(N,-10,10);',dview3s];

sliders = ['xf=0.5; yf=0.5; zf=0.5; mindata=min(data); maxdata=max(data);',...
           'xmin=mindata(1); ymin=mindata(2); zmin=mindata(3);',...
           'xmax=maxdata(1); ymax=maxdata(2); zmax=maxdata(3);',...
           'set(xc,''value'',xf); set(yc,''value'',yf); set(zc,''value'',zf);'];

calcs = ['[Tx,Ty,Tz,X,Y,Z,f,d]=pccont3(pc,data,kernel,nmesh,h,col); w=pclevel(pc,d); const=w(1); axvec=axis;'];

views = [lights,'view(azim,elev); xlabel(''x''); ylabel(''y''); zlabel(''z'');',...
      '[hr,hc]=size(h); if hr==1, title([''h = '',num2str(h(1)),'' , '',num2str(h(2)),'' , '',num2str(h(3))]);',...
      'else, title([''h = '',num2str(sqrt(h(1,1))),'' , '',num2str(sqrt(h(2,2))),'' , '',num2str(sqrt(h(3,3)))]); end;'];

plots = ['v=get(ploth,''value''); if v==2, xp=xmin+xf*(xmax-xmin); yp=ymin+yf*(ymax-ymin);',...
         'zp=zmin+zf*(zmax-zmin); slice(X,Y,Z,f,xp,yp,zp); axis(axvec);',...
         'elseif v==1,',fills,'axis(axvec); set(gca,''box'',''on'');',...
         'elseif v==4, [in,vin]=hull3int2(data); in(:,4)=d(vin); in(:,5)=vin; in(find(in(:,4)>=const),:),',...
         'v=1; set(ploth,''value'',v); else, plot3(data(:,1),data(:,2),data(:,3),''wx''); axis(axvec); end;',views];

hselects = ['v=get(hselecth,''value''); if v==1, h=[hns(data(:,1),kernel) hns(data(:,2),kernel) hns(data(:,3),kernel)];',...
           'elseif v==2 & kernel==''knorm'', h=[hdpi(data(:,1),kernel,1) hdpi(data(:,2),kernel,1) hdpi(data(:,3),kernel,1)];',...
           'elseif v==3 & kernel==''knorm'', h=[hdpi(data(:,1),kernel,2) hdpi(data(:,2),kernel,2) hdpi(data(:,3),kernel,2)];',...
           'elseif v==4 & kernel==''knorm'', h=[hdpi(data(:,1),kernel,3) hdpi(data(:,2),kernel,3) hdpi(data(:,3),kernel,3)];',...
           'elseif v==5 & kernel==''knorm'', h=[hste(data(:,1),kernel) hste(data(:,2),kernel) hste(data(:,3),kernel)];',...
           'else, h=[hns(data(:,1),kernel) hns(data(:,2),kernel) hns(data(:,3),kernel)];',...  
           'set(hselecth,''value'',1); end; set(hvalh,''string'',[''['',num2str(h(1)),'','',num2str(h(2)),'','',num2str(h(3)),'']'']);',calcs,views];

hvals = ['h=str2num(get(hvalh,''string''));',calcs,views];

pcvals = ['pc=str2num(get(pcvalh,''string'')); pc=pc(1); w=pclevel(pc,d); const=w(1); [Tx,Ty,Tz]=surfcv(X,Y,Z,f,const,col);',views];

meshs = ['nmesh=str2num(get(meshh,''string''));',calcs,views];

% Frames

frame1h = uicontrol('style','frame','units','normal','pos',[.84 .85 .15 .14]);

frame2h = uicontrol('style','frame','units','normal','pos',[.02 .01 .22 .07]);

% Check boxes

smoothh = uicontrol('style','checkbox','units','normal','pos',[.02 .87 .135 .05],...
  'string','Shading','call',smooths);
lighth = uicontrol('style','checkbox','units','normal','pos',[.02 .8 .135 .05],...
  'string','Lighting','call',lights);

set(smoothh,'value',0);
set(lighth,'value',0);

% Pop-up menus

ploth = uicontrol('style','popupmenu','units','normal','pos',[.015 .91 .24 .08],...
  'string','Percent Contour|Slice|Scatter|In sphere','call',plots);   
hselecth = uicontrol('style','popupmenu','units','normal','pos',[.8 .76 .19 .08],...
  'string','Normal Scale|DPI 1|DPI 2|DPI 3|STE2','call',hselects);

% Menus

datamenus = ['clear M N; set(ploth,''value'',1),',...
             'set(wvglassh,''checked'',''off''),',...
             'set(sglassh,''checked'',''off''),',...
             'set(lmglassh,''checked'',''off''),',...
             'set(wprglassh,''checked'',''off''),',...
             'set(cglassh,''checked'',''off''),',...
             'set(lavrionh,''checked'',''off''),',...
             'set(keah,''checked'',''off''),',...
             'set(nmdah,''checked'',''off''),',...
             'set(nmdbh,''checked'',''off''),',...
             'set(nmdch,''checked'',''off''),',...
             'set(smallranh,''checked'',''off'');'];

datamenuh = uimenu(gcf,'label','Data');

wvglassh = uimenu(datamenuh,'label','Win. Vessel (102)','callback',...
  ['data=wvglass(:,1:3);',hselects,datamenus,...
   'set(wvglassh,''checked'',''on'')']);
sglassh = uimenu(datamenuh,'label','Southampton (271)','callback',...
  ['data=sglass(:,1:3);',hselects,datamenus,...
   'set(sglassh,''checked'',''on'')']);
lmglassh = uimenu(datamenuh,'label','Leic./Man. (105)','callback',...
  ['data=lmglass(:,1:3);',hselects,datamenus,...
   'set(lmglassh,''checked'',''on'')']);
wprglassh = uimenu(datamenuh,'label','Win. post-Roman (250)','callback',...
  ['data=wprglass(:,1:3);',hselects,datamenus,...
   'set(wprglassh,''checked'',''on'')']);
cglassh = uimenu(datamenuh,'label','Coppergate (234)','callback',...
  ['data=cglass(:,1:3);',hselects,datamenus,...
   'set(cglassh,''checked'',''on'')']);
lavrionh = uimenu(datamenuh,'label','LI: Lavrion (59)','callback',...
  ['data=leadiso;',hselects,datamenus,...
   'set(lavrionh,''checked'',''on'')']);
keah = uimenu(datamenuh,'label','LI: Kea (62)','callback',...
  ['data=kea;',hselects,datamenus,...
   'set(keah,''checked'',''on'')']);
nmdah = uimenu(datamenuh,'label','NMD A (60)','callback',...
  ['data=snmd3(60,wa,sigmaa,mua);',hselects,datamenus,...
   'set(nmdah,''checked'',''on'')']);
nmdbh = uimenu(datamenuh,'label','NMD B (60)','callback',...
  ['data=snmd3(60,wb,sigmab,mub);',hselects,datamenus,...
   'set(nmdbh,''checked'',''on'')']);
nmdch = uimenu(datamenuh,'label','NMD C (60)','callback',...
  ['data=snmd3(60,wlav,sigmalav,mulav);',hselects,datamenus,...
   'set(nmdch,''checked'',''on'')']);
smallranh = uimenu(datamenuh,'label','Small random (8)','callback',...
  ['data=randn(8,3);',hselects,datamenus,...
   'set(smallranh,''checked'',''on'')']);

if import==0,
  set(smallranh,'checked','on');
end;

colmenus = ['set(yellowh,''checked'',''off''),',...
            'set(redh,''checked'',''off''),',...
            'set(whiteh,''checked'',''off''),',...
            'set(blueh,''checked'',''off''),',...
            'set(magentah,''checked'',''off''),',...
            'set(cyanh,''checked'',''off''),',...
            'set(greenh,''checked'',''off'');'];

colmenuh = uimenu(gcf,'label','Cont. col.');

yellowh = uimenu(colmenuh,'label','Yellow','callback',...
  ['col=''y'';', colmenus, 'set(yellowh,''checked'',''on'')']);
redh = uimenu(colmenuh,'label','Red','callback',...
  ['col=''r'';', colmenus, 'set(redh,''checked'',''on'')']);
whiteh = uimenu(colmenuh,'label','White','callback',...
  ['col=''w'';', colmenus, 'set(whiteh,''checked'',''on'')']);
blueh = uimenu(colmenuh,'label','Blue','callback',...
  ['col=''b'';', colmenus, 'set(blueh,''checked'',''on'')']);
magentah = uimenu(colmenuh,'label','Magenta','callback',...
  ['col=''m'';', colmenus, 'set(magentah,''checked'',''on'')']);
cyanh = uimenu(colmenuh,'label','Cyan','callback',...
  ['col=''c'';', colmenus, 'set(cyanh,''checked'',''on'')']);
greenh = uimenu(colmenuh,'label','Green','callback',...
  ['col=''g'';', colmenus, 'set(greenh,''checked'',''on'')']);

set(yellowh,'checked','on');

colormen;

movrmenuh = uimenu(gcf,'label','Rot. Movie');

formrh = uimenu(movrmenuh,'label','Create','callback',moviers);
playrh = uimenu(movrmenuh,'label','Play','callback',playrs);
clearrh = uimenu(movrmenuh,'label','Clear','callback',['clear M']);

movcmenuh = uimenu(gcf,'label','Cont. Movie');

formch = uimenu(movcmenuh,'label','Create','callback',moviecs);
playch = uimenu(movcmenuh,'label','Play','callback',playcs);
clearch = uimenu(movcmenuh,'label','Clear','callback',['clear N']);

% Sliders

lrc = uicontrol('style','slider','units','normal','pos',[.385 .02 .25 .04],...
     'min',-90,'max',90,'value',azim,...
     'call',['azim = ceil(get(lrc,''value'')); set(lrc,''value'',azim);',views;]);
lrmin = uicontrol('style','text','units','normal','pos',[.34 .02 .04 .04],...
     'string',num2str(get(lrc,'min')));
lrmax = uicontrol('style','text','units','normal','pos',[.64 .02 .04 .04],...
     'string',num2str(get(lrc,'max')));

udc = uicontrol('style','slider','units','normal','pos',[.945 .36 .04 .25],...
     'min',0,'max',90,'value',elev,...
     'call',['elev = ceil(get(udc,''value'')); set(udc,''value'',elev);',views;]);
udmin = uicontrol('style','text','units','normal','pos',[.945 .32 .04 .035],...
     'string',num2str(get(udc,'min')));
udmax = uicontrol('style','text','units','normal','pos',[.945 .615 .04 .035],...
     'string',num2str(get(udc,'max')));

% Can these three sliders only be made available when slice plot is on:

xc = uicontrol('style','slider','units','normal','pos',[.69 .1 .25 .04],...
     'min',0,'max',1,'value',xf,...
     'call',['xf = get(xc,''value''); set(xc,''value'',xf);',plots;]);
xmint = uicontrol('style','text','units','normal','pos',[.645 .1 .04 .04],...
     'string',num2str(get(xc,'min')));
xmaxt = uicontrol('style','text','units','normal','pos',[.945 .1 .04 .04],...
     'string',num2str(get(xc,'max')));

yc = uicontrol('style','slider','units','normal','pos',[.065 .1 .25 .04],...
     'min',-1,'max',0,'value',-yf,...
     'call',['yf = abs(get(yc,''value'')); set(yc,''value'',-yf);',plots;]);
ymint = uicontrol('style','text','units','normal','pos',[.02 .1 .04 .04],...
     'string','1');
ymaxt = uicontrol('style','text','units','normal','pos',[.32 .1 .04 .04],...
     'string','0');

zc = uicontrol('style','slider','units','normal','pos',[.02 .36 .04 .25],...
     'min',0,'max',1,'value',zf,...
     'call',['zf = get(zc,''value''); set(zc,''value'',zf);',plots;]);
zmint = uicontrol('style','text','units','normal','pos',[.02 .32 .04 .035],...
     'string',num2str(get(zc,'min')));
zmaxt = uicontrol('style','text','units','normal','pos',[.02 .615 .04 .035],...
     'string',num2str(get(zc,'max')));

% Push buttons

endh = uicontrol('style','push','units','normal','pos',[.91 .02 .08 .06],...
        'string','End','call',ends);
dview2h = uicontrol('style','push','units','normal','pos',[.71 .02 .08 .06],...
        'string','View2','call',dview2s);
dview3h = uicontrol('style','push','units','normal','pos',[.81 .02 .08 .06],...
        'string','View3','call',dview3s);

% Editable text

hvalh = uicontrol('style','edit','units','normal','pos',[.885 .93 .09 .04],...
        'string','0.00','min',0,'call',hvals);
hvalt = uicontrol('style','text','units','normal','pos',[.86 .93 .02 .04],...
        'string','h:');

set(hvalh,'string',['[',num2str(h(1)),',',num2str(h(2)),',',num2str(h(3)),']']);

pcvalh = uicontrol('style','edit','units','normal','pos',[.185 .025 .04 .04],...
        'string','75','min',0,'call',pcvals);
pcvalt = uicontrol('style','text','units','normal','pos',[.035 .025 .15 .04],...
        'string','Contour %:');

meshh = uicontrol('style','edit','units','normal','pos',[.92 .87 .05 .04],...
        'string',num2str(nmesh),'min',8,'call',meshs);
mesht = uicontrol('style','text','units','normal','pos',[.86 .87 .06 .04],...
        'string','mesh:');