% Cartoon of laminography experiment with 70-degree tilt of sample. Adjust
% as you see fit 

clear
close all
figure('units','pixels','position',[0 0 1920 1080],'ToolBar','none');
set(0,'defaultfigurecolor',[1 1 1]);

vid = VideoWriter('laminographyCartoon.mp4','MPEG-4');
vid.Quality = 100;
vid.FrameRate = 30;
open(vid);

myGold = [0.8 0.64 0];
lp = [-2 -2 1];
pos1 = [0 0 1 1];

% Dimensions in matlab units
incRadLength = 7; % Distance upstream to which incident radiation extends
FZPpos = -1; % Position upstream of FZP relative to focus @ (0,0,0)
samplePos = 0.0; % Position of sample downstream of focus
DetPos = 10; % Position of detector downstream
halfDetWidth = 0.7; % Half width of detector
sampleHt = 2.0; % Height of sample
imax = 5; % Determines outer radius of FZP 
sampleTilt = 70; % Sample tilt w.r.t. vertical axis 

% Set up cones representing x-rays
% Incident x-rays, slightly divergent
t = 1:-0.001:0.8;
[xC1,yC1,zC1] = cylinder(t,800);
% Cone of focussed x-rays and divergent scattered x-rays
t = 0:0.001:1;
[xC2,yC2,zC2] = cylinder(t,800);

% Image with transparent background - for displaying in cartoon
[chip,~,alphaMap] = imread('chipArchitecture.png');
alphaMap = 0.55*(alphaMap); % alpha map of sample image with maximum opacity of 0.55 

% _________________________________________________________________________
%
% Now nested for-loops to rotate chip

for rotAngle = 0:2:358 % Rotate 2 pi
    
    hold off
    subplot('position',pos1);
    newplot
    hold on
    
    % FZP
    FZPth = 0:pi/100:2*pi;
    for jj = imax:-2:1
        hold on
        r1 = 0.125*(jj)^0.5;
        r2 = 0.125*(jj-1)^0.5;
        z_circle1 = r1*cos(FZPth);
        y_circle1 = r1*sin(FZPth);
        x_circle1 = 0.0*y_circle1;
        z_circle2 = r2*cos(FZPth);
        y_circle2 = r2*sin(FZPth);
        x_circle2 = 0.0*y_circle2;
        z_circle = [z_circle1 z_circle2];
        y_circle = [y_circle1 y_circle2];
        x_circle = [x_circle1 x_circle2];
        zone = fill3(x_circle+FZPpos,y_circle,z_circle,'k','LineStyle','none');
    end
    
    
    % Incident, slightly divergent, radiation cone
    maxFZPrad = imax^0.5*0.125;
    incRad = surf(maxFZPrad*xC1,maxFZPrad*yC1,-FZPpos+incRadLength*zC1,...
        'FaceColor',myGold,'LineStyle',...
        'none','FaceLighting','flat','DiffuseStrength',1);
    rotate(incRad,[0 1 0],-90,[0,0,0]);
    % Vary alpha of divergent radiation in x-direction with offset so it
    % remains semitransparent for all elements of surf = divRad
    alpha(incRad,0.6*incRad.XData/halfDetWidth-2);
    
    % Focussed radiation cone after FZP
    focRad = surf(maxFZPrad*xC2,maxFZPrad*yC2,-FZPpos*zC2,...
        'FaceAlpha',0.25,'FaceColor',myGold,'LineStyle',...
        'none','FaceLighting','flat','DiffuseStrength',1);
    rotate(focRad,[0 1 0],-90,[0,0,0]);
    
    % Divergent radiation cone after focus
    maxFZPrad = imax^0.5*0.125;
    divRad = surf((1+halfDetWidth)*xC2,(1+halfDetWidth)*yC2,(DetPos)*zC2,...
        'FaceColor',myGold,'FaceAlpha',0.28,'LineStyle',...
        'none','FaceLighting','flat','DiffuseStrength',1);
    rotate(divRad,[0 1 0],90,[0,0,0]);
    % Vary alpha of divergent radiation in x-direction with offset so it
    % remains semitransparent for all elements of surf = divRad
    alpha(divRad,-0.5*divRad.XData/halfDetWidth-1.6);
    
    % chip
    for layer = 0:3
        XX = samplePos*[1 1; 1 1]+0.25+layer*0.04; % Position of sample in front of focus
        % YY in matlab coords = xx in sample coords
        YY = sampleHt*[1 -1; 1 -1];
        % ZZ in matlab coords = yy in sample coords
        ZZ = sampleHt*[1 1;-1 -1];
        chiplayer = surf(XX,YY,ZZ,chip,'FaceColor','texturemap',... 
            'FaceAlpha','texturemap','AlphaData',alphaMap,'EdgeColor',... 
            'none','AlphaDataMapping','none');
        rotate(chiplayer,[1 0 0 ],90*layer+rotAngle,[0.25 0 0]);
        rotate(chiplayer,[0 1 0],sampleTilt,[0.25 0 0]);
    end
    
    % Rotation axis
    plot3(0.25+2*[-cosd(sampleTilt) cosd(sampleTilt)],[0 0],... 
        2*[sind(sampleTilt) -sind(sampleTilt)],'k',... 
        'LineStyle','-.','LineWidth',1.6);
    % Incident-beam axis
    plot3([-8 8],[0 0],[0 0],'k','LineWidth',0.8);
    plot3([0.25 0.25],[-2.84 2.84],[0 0],'k','LineWidth',0.8);
    plot3([0.25 0.25],[0 0],[-2.3 2.3],'k','LineWidth',0.8);
    
    view(-34,7);
    set(gca,'Projection','perspective');
    light('Position',lp,'Style','infinite');
    axis equal
    axis off
    frame = getframe(gcf);
    writeVideo(vid,frame);
end

close(vid);