% Cartoon of a photon counter 

close all
vid = VideoWriter('photonCounter.mp4','MPEG-4');
vid.Quality = 100;
vid.FrameRate = 60;
open(vid);
figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]);


s2cam = 6.4;
camSize = 3;
sensThick = 0.16;

[x,y,z] = sphere(270);
[a,b,c] = cylinder(1,100);
myBlue = [0.4 0.44 0.73];
dkGrey = [0.1 0.1 0.1];
midGrey = [0.5 0.5 0.5];
myGold = [0.8 0.64 0];         % golden synchrotron radiation!

dtSize= 0.5*sqrt(3);
dtPos = 3;
pr = 0.025; % Photon radius 

lp = [-1 -2 3]; 
noFrames = 1200; 

probThresh = 0.05; 
phPositions = zeros(noFrames,3); 
phPositions(:,2) = -10; 
phPositions(:,3) = -10; 
phExists = zeros(noFrames,1); 
detectedPhotons = 0; 
for tt = 1:noFrames % 600 frames
    hold off
    newplot
    axis off
    axis equal
    xlim([-0.05 3.55]);
    ylim([-1.05 1.05]);
    zlim([-1.05 1.05]);
    light('Position',lp,'Style','infinite');
    % Detector
    [V,F] = platonic_solid(2,dtSize); % Cube,
    V(:,1) = V(:,1)+dtPos;
    patch('Faces',F,'Vertices',V,'FaceColor',midGrey,'FaceAlpha',1, ...
        'EdgeColor','none','FaceLighting','flat','DiffuseStrength',1,'SpecularStrength',0);
    hold on
    [V,F] = platonic_solid(2,dtSize*0.9); % Cube
    V(:,1) = V(:,1)+dtPos-0.1;
    patch('Faces',F,'Vertices',V,'FaceColor',dkGrey,'FaceAlpha',1, ...
        'EdgeColor','none','FaceLighting','flat','DiffuseStrength',1,'SpecularStrength',0);
    probPh = rand;
    if (probPh <= probThresh)
        phExists(tt,1) = 1;
        phPositions(tt,2) = 0.9*(rand-0.5);
        phPositions(tt,3) = 0.9*(rand-0.5);
    end
    for ii = 1:tt
        if (phExists(ii,1) == 1) && (phPositions(ii,1) <= dtPos)
            phPositions(ii,1) = phPositions(ii,1)+0.02;
            surf(phPositions(ii,1)+x*pr,phPositions(ii,2)+y*pr,phPositions(ii,3)+z*pr,'FaceColor',...
                myGold,'LineStyle','none','FaceLighting','gouraud','DiffuseStrength',1);
            if (phPositions(ii,1)>=dtPos-0.45-0.1) && (phPositions(ii,1)<dtPos-0.45-0.1+0.02)
                detectedPhotons = detectedPhotons+1;
            end
        end
        str = num2str(detectedPhotons,'% 5.0f');
        hText = text(2.2, -0.8, -0.8, str, 'FontSize',28);
        
    end
    set(gca, 'Projection','perspective');
    view(-44,20);
    
    
    frame = getframe(gcf);
    writeVideo(vid,frame);
    
    hold off
end
% Output the movie as an mpg file
close(vid);