% uib.m

% writing on thet, phi
% Using fourier on phi(0,2pi) theta(0,pi)
%
%  Test program for 'writing on atoms
%
%
%  L. Kocbach, 14.11.2002
%
%  20x30 points test
%
%  Using the delta function algorithm
%
% uib.m

% number of angle values
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This little thing generates 
% points from letters
% in this case UiB
% The letters must be transformed
% to cover the range wanted
%
%
%

close all,clear all
format compact
figure(1); set(gcf,'color','white');
set(gcf,'pos',[14   249   157    30])

%gtxt=gtext('NOTUR')
set(gca,'xlim',[0 1],'ylim',[0 1])
wtxt=text(0.1,0.1,'U i B '); 
set(wtxt,'fontsize',14)
axis off

[Aimg,Colmap]=capture(1);

UU=min(min(Aimg))
Bimg=(Aimg>UU);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[Px Py]=find(Bimg>0);
points=1+ceil( 3*[ 23+ (max(Px)-Px) Py-30]); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plot(Py-30,20+2*(1+max(Px)-Px),'*')
% points=1+ceil( [ Py-30 20+2*(1+max(Px)-Px)]); 







%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


nnt=60;                   % theta points
dthet=pi/(nnt-1);
thet=0:dthet:pi;
nnp=60;                   % phi points /2 
dphi=2*pi/(2*nnp-1);
phi=0:dphi:2*pi;

thet=(0:179)*pi/180;        
phi=(0:359)*pi/180;   
dphi=pi/(180);
dthet=pi/(180);

[Phi,Theta]=meshgrid(phi,thet);  







%
%  number of functions - counter
%
kk=0;
%
% number of functions in thet and phi
%

    nL=8;

    nL=30;     
   
%
for kl=0:nL
      for km=-kl:1:kl
         kk=kk+1;
         func(kk,1)=kl;
         func(kk,2)=km;
         fprintf(1,'%d  %d %d  functions \n',kk,kl,km);
      end
end

%  Two -dimensional arrays Theta, Phi



% expansion coefficients set to zero

  coefs=func(:,1)*0;
  npoints=max(size(points));
%
%
% filling the expansion coefficients
%
%  
%    /      \       --------    /      \    
%   |        |	    \          |        |	
%   |        |	     \         |        |	
%   |  coefs |  =    /         |Psi(pnt)|  	
%   |        | 	    /          |        | 	
%   |        |      --------   |        |   
%    \      /	    all Psi     \      /	
%   		    all pnt
% 
%
% for each point adding to coeff
%
%  Psi(point)
%
  for kpoints=1:npoints
      for kk=1:max(size(func))
         %kk
         % points(kpoints,2)
         % points(kpoints,1) 
         L= func(kk,1);
         M= func(kk,2);        
         coefs(kk)=coefs(kk)...
            +conj(  ylm(L,M,thet(points(kpoints,1)) ,phi(points(kpoints,2)) )  );         
            fprintf(1,'coefs  Y (%d %d )\n',L,M);    
      end;
  end;
%
%
%
%   Calculating the function
%
%
  FF=0*Theta;
%
  for kk=1:max(size(func))
         %kk
         % points(kpoints,2)
         % points(kpoints,1) 
         L= func(kk,1);
         M= func(kk,2);      
         FF=FF...
          +ylm(L,M,Theta, Phi)*coefs(kk);
         fprintf(1,'adding  Y (%d %d )\n',L,M);          
  end;
  GG=FF.*conj(FF);
% 

HH=GG/max(max(GG));

figure(2);

 Ujda=pcolor(phi,thet, HH);  % shading interp;
 set(Ujda,'EdgeColor','none')
  set(2,'pos',[ 1 1 720 360])  
 colormap hot; colormap(flipud(colormap))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


  set(gca,'units','pixels','pos',[ 0 0 720 360])
  set(1,'pos',[ 1 1 720 360])  
  axis off












%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  
%   Old version CRASHES NOW       [Aimg,Colmap]=capture(1);  
        frm = getframe(2);
       [Aimg,Colmap] = frame2im(frm);  % Colmap will be empty
 

% display the image flat as a test;  

%   imagesc(Aimg,[1 size(Colmap,1)-1]) ;colormap(Colmap)  ; 

figure(3)

  image(Aimg) ;% colormap(Colmap)  ;  % This figure can 
                                    % be discarded 
				    % in final applications
  
% 3-dim show of the flat image on the surface begin
       
  figure (4)   
  set(gcf,'color','white') 


  [x,y,z] = sphere(36);    







   hg.axes3.DataAspectRatio = [1 1 1];
   hg.axes3.PlotBoxAspectRatioMode = 'auto';
   f.ax(3) = axes('Tag','3D','Visible','off', hg.axes3);
   
%mat.dull, 
% 3-dim show of the flat image on the surface end
   mat.dull.AmbientStrength = 0.1;
   mat.dull.DiffuseStrength = 1;
   mat.dull.SpecularColorReflectance = .5; 
   mat.dull.SpecularExponent = 20;
   mat.dull.SpecularStrength = 1;   
   
   % flipud(Aimg);
     Aimg(:,:,1)=flipud(Aimg(:,:,1));
     Aimg(:,:,2)=flipud(Aimg(:,:,2));
     Aimg(:,:,3)=flipud(Aimg(:,:,3));
   
   f.surf = surface(x,y,z, (Aimg),...    % plot the sphere
                  'FaceColor','texturemap', 'CDataMapping','direct',...
                   'FaceLighting','phong','Parent',f.ax(3));
   
  axis square  
%  colormap(Colmap)    
    % f.lite(1) = light('position',[+1 0 1], 'color', [.9 .9 .9]);
    rrr=0.9; eleva=-0.3;
    pphi=pi/180*(135);
     f.lite(1) = light('position',[0.8*rrr*cos(pphi) 0.8*rrr*cos(pphi) eleva+0.4], 'color', [.7 .7 .7]);  
     f.lite(2) = light('position',[rrr*cos(pphi) rrr*cos(pphi) eleva], 'color', [.5 .5 .5]); 
% demonstrating the image












  axis square
% 3-dim show of the flat image on the surface end

% demonstrating the image
  rotate3d
  view(3)
  [az el]=view;
  delay=0.05;
  for azi=az:5:100
     view([azi el]);
     tic; while(toc<delay), az; end; % delay loop
     drawnow;   
  end

 