%by Ankur Kumar
%rotate a cube through 3-D space in cartesian and reflect it in PC
%Rotation in Cartesian about blue line <--> Translation in PC

clf
clear all

nframes = 40;
fname='C:\Documents and Settings\labuser\Desktop\HiD\RotCube.avi';

mov=avifile(fname);
mov.fps = 15;
mov.compression='cinepak';%compression is 'none' for unix
mov.quality=100;

figure(1)

%3 dimensions
ndim = 3;
index = ndim-1;


%vertices coords matrix
v = [];
%finding vertices of n-D cube, which are the elements of a n-D truth table, using repmat
for i = 0:index
    temp = [zeros(2^i,1);ones(2^i,1)];
    col = repmat(temp,2^(index-i),1);
    v = [v,col]; 
end

subplot(2,1,1)
hold on
%the axes for parallel coords
axisx = [1;ndim];
axisy = [0;0];
label = [];
for i = 1:ndim
    axisx = [axisx,[i;i]];
    axisy = [axisy,[0;1]];
    label = [label, {sprintf('x%d',i)}];    %used for labelling the axes
end
line(axisx, axisy, 'color', 'b');           %draw the axes
set(gca, 'xtick',[1:1:ndim]);
set(gca, 'xticklabel', label);
for i = 1:2^ndim
    plot(v(i,:),'r');
end

hold off

subplot(2,1,2)
set(gca,'color',[1 1 1])
box on
axis vis3d
view(22,23)
xlabel('x1'); ylabel('x2'); zlabel('x3');
%six faces connection with vertecies
f = [1 2 6 5;2 4 8 6;3 7 5 1;4 8 6 2;5 6 8 7;1 2 4 3];
patch('Vertices',v,'Faces',f,'FaceColor','w','EdgeColor','r');

%unitvector <x,y,z>


x = sqrt(3)/3
y = sqrt(3)/3
z = sqrt(3)/3

%theta
%rotation 3D matrix -- rotation around a direction vector by an angle theta
for theta = linspace(-2*pi,2*pi,nframes) %100 iterations
    
    clf
    R = exp([0 -z*theta y*theta; z*theta 0 -x*theta; -y*theta x*theta 0;]);
    m = max(R);
    m = max(m);
    c = v*(R./m);
    %graph the cube in cartesian
    subplot(2,1,2)
    set(gca,'color',[1 1 1])
    box on
    axis vis3d
    view(22,23)
    xlabel('x1'); ylabel('x2'); zlabel('x3');
    line([0 x]',[0 y]',[0 z]','color','b')
    %six faces connection with vertecies
    patch('Vertices',c,'Faces',f,'FaceColor','none','EdgeColor','r');
    %graph the cube in parallel coords
    subplot(2,1,1)
    hold on
    for i = 1:2^ndim
        plot(c(i,:),'r');
    end
    axisx = [1,3; 1,1; 2,2; 3,3]';
    axisy = [0,0; 0,1; 0,1; 0,1]';
    line(axisx, axisy, 'color', 'k');
    set(gca, 'xtick',[1 2 3])
    set(gca, 'xticklabel', {'x1', 'x2','x3'})
    hold off
    drawnow
    F=getframe(gcf);
    mov=addframe(mov,F);
end
mov = close(mov);
