clear all;

AxisRange = 4;
L1 = 2;

vertSeg1 = [-0.5 -0.5 -0.5;  ...
            -0.5 0.5 -0.5;  ...
            (0.5 + L1) 0.5 -0.5;  ...
            (0.5 + L1) -0.5 -0.5; ...
            -0.5 -0.5 0.5; ...
            -0.5 0.5 0.5;  ...
            (0.5 + L1) 0.5 0.5; ...
            (0.5 + L1) -0.5 0.5];
   
facSeg1 = [1 2 3 4; ...
    2 6 7 3; ...
    4 3 7 8; ...
    1 5 8 4; ...
    1 2 6 5; ...
    5 6 7 8];

vertHand1 = [0.00 0.5 -0.5;  ...
             0.5 0.5 -0.5;  ...
             0.5 1.0 -0.5;  ...
             1.5 1.0 -0.5;  ...
             1.5 0.5 -0.5;  ...
             1.0 0.5 -0.5;  ...
             1.0 -0.5 -0.5;  ...
             1.5 -0.5 -0.5;  ...
             1.5 -1.0 -0.5;  ...
             0.5 -1.0 -0.5;  ...
             0.5 -0.5 -0.5;  ...
             0.00 -0.5 -0.5;  ...
             0.00 0.5 0.5;  ...
             0.5 0.5 0.5;  ...
             0.5 1.0 0.5;  ...
             1.5 1.0 0.5;  ...
             1.5 0.5 0.5;  ...
             1.0 0.5 0.5;  ...
             1.0 -0.5 0.5;  ...
             1.5 -0.5 0.5;  ...
             1.5 -1.0 0.5;  ...
             0.5 -1.0 0.5;  ...
             0.5 -0.5 0.5;  ...
             0.00 -0.5 0.5;  ...
            ];
   
facHand1 = [1 2 11 12; ...
            2 3 4 5; ...  
            2 6 7 11; ...
            11 8 9 10; ...
            13 14 23 24; ...
            14 15 16 17; ...  
            14 18 19 23; ...
            23 20 21 22; ...
            1 12 24 13; ...
            1 13 14 2; ...
            2 14 15 3; ...
            3 15 16 4; ...
            4 16 17 5; ...
            5 17 18 6; ...
            6 18 19 7; ...
            7 19 20 8; ...
            8 20 21 9; ...
            9 21 22 10; ...
            10 22 23 11; ...
            11 23 24 12; ...
            ];

% original object
subplot(6,4,1);
patch('Faces',facSeg1,'Vertices',vertSeg1,'FaceColor','r');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(30,30);
title('original');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along y-axis (x-z plane)
subplot(6,4,2);
patch('Faces',facSeg1,'Vertices',vertSeg1,'FaceColor','r');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(0,0);
title('x-z plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along x-axis (y-z plane)
subplot(6,4,5);
patch('Faces',facSeg1,'Vertices',vertSeg1,'FaceColor','r');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(90,0);
title('y-z plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along z-axis (x-y plane)
subplot(6,4,6);
patch('Faces',facSeg1,'Vertices',vertSeg1,'FaceColor','r');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(0,90);
title('x-y plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% transformed object
VerticesHomSeg1 = zeros(8,4);
for i = 1:8
    VerticesHomSeg1(i,:) = [vertSeg1(i,:) 1];
end

phiXSeg1 = 0;
TxMatrixXSeg1 = [1       0             0        0;...
                 0 cos(phiXSeg1) -sin(phiXSeg1) 0; ...
                 0 sin(phiXSeg1) cos(phiXSeg1)  0; ...
                 0       0             0        1];
        
phiYSeg1 = pi/6;
TxMatrixYSeg1 = [cos(phiYSeg1)  0 -sin(phiYSeg1) 0;...
                 0              1      0         0; ...
                 sin(phiYSeg1)  0  cos(phiYSeg1) 0; ...
                 0              0      0         1];
        
phiZSeg1 = 0;
TxMatrixZSeg1 = [cos(phiZSeg1) -sin(phiZSeg1)  0  0;...
                 sin(phiZSeg1) cos(phiZSeg1)   0  0; ...
                 0                 0           1  0; ...
                 0                 0           0  1];
        
TxMatrixShiftScaleHand1 = [1        0        0        L1;...
                           0        1        0        0; ...
                           0        0        1        0; ...
                           0        0        0        1];
             
phiXHand1 = pi/4;
TxMatrixXHand1 = [1       0             0        0;...
                 0 cos(phiXHand1) -sin(phiXHand1) 0; ...
                 0 sin(phiXHand1) cos(phiXHand1)  0; ...
                 0       0             0        1];
        
phiYHand1 = 0;
TxMatrixYHand1 = [cos(phiYHand1)  0 -sin(phiYHand1) 0;...
                 0              1      0         0; ...
                 sin(phiYHand1)  0  cos(phiYHand1) 0; ...
                 0              0      0         1];
        
phiZHand1 = 0;
TxMatrixZHand1 = [cos(phiZHand1) -sin(phiZHand1)  0  0;...
                 sin(phiZHand1) cos(phiZHand1)   0  0; ...
                 0                 0           1  0; ...
                 0                 0           0  1];


TxMatrixRotSeg1 = TxMatrixZSeg1 * TxMatrixYSeg1 * TxMatrixXSeg1;             
TxVerticesHomSeg1 = TxMatrixRotSeg1 * VerticesHomSeg1';
TxVerticesHomSeg1 = TxVerticesHomSeg1';

TxVerticesSeg1 = zeros(8,3);
for i = 1:8
    TxVerticesSeg1(i,:) = TxVerticesHomSeg1(i,1:3);
end

% Plotting transformed Seg1
subplot(6,4,3);
patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b');  
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(30,30);
title('transformed');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along y-axis (x-z plane)
subplot(6,4,4);
patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b');  
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(0,0);
title('x-z plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along x-axis (y-z plane)
subplot(6,4,7);
patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b');  
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(90,0);
title('y-z plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along z-axis (x-y plane)
subplot(6,4,8);
patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b'); 
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(0,90);
title('x-y plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);


% original object --- Hand1
subplot(6,4,9);
patch('Faces',facHand1,'Vertices',vertHand1,'FaceColor','r');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(30,30);
title('original');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along y-axis (x-z plane)
subplot(6,4,10);
patch('Faces',facHand1,'Vertices',vertHand1,'FaceColor','r');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(0,0);
title('x-z plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along x-axis (y-z plane)
subplot(6,4,13);
patch('Faces',facHand1,'Vertices',vertHand1,'FaceColor','r');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(90,0);
title('y-z plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along z-axis (x-y plane)
subplot(6,4,14);
patch('Faces',facHand1,'Vertices',vertHand1,'FaceColor','r');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(0,90);
title('x-y plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% transforming object
VerticesHomHand1 = zeros(24,4);
for i = 1:24
    VerticesHomHand1(i,:) = [vertHand1(i,:) 1];
end


TxMatrixRotSeg1 = TxMatrixZSeg1 * TxMatrixYSeg1 * TxMatrixXSeg1;  
TxMatrixRotHand1 = TxMatrixXHand1 * TxMatrixYHand1 * TxMatrixZHand1;
TxVerticesHomHand1 = TxMatrixRotSeg1 * TxMatrixShiftScaleHand1 * TxMatrixRotHand1 * VerticesHomHand1';
TxVerticesHomHand1 = TxVerticesHomHand1';

TxVerticesHand1 = zeros(24,3);
for i = 1:24
    TxVerticesHand1(i,:) = TxVerticesHomHand1(i,1:3);
end

% plotting transformed hand
subplot(6,4,11);
patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(30,30);
title('transformed');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along y-axis (x-z plane)
subplot(6,4,12);
patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(0,0);
title('x-z plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along x-axis (y-z plane)
subplot(6,4,15);
patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(90,0);
title('y-z plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along z-axis (x-y plane)
subplot(6,4,16);
patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(0,90);
title('x-y plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% plotting transformed arm and hand ---------------------------------------
subplot(6,4,19);
patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b');
patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(30,30);
title('transformed');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along y-axis (x-z plane)
subplot(6,4,20);
patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b');
patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g');  
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(0,0);
title('x-z plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along x-axis (y-z plane)
subplot(6,4,23);
patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b');
patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g'); 
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(90,0);
title('y-z plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% view along z-axis (x-y plane)
subplot(6,4,24);
patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b');
patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g');  
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(0,90);
title('x-y plane');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

% plot in the enlarged view
subplot(6,4,[17 18 21 22]);
patch('Faces',facSeg1,'Vertices',TxVerticesSeg1,'FaceColor','b');
patch('Faces',facHand1,'Vertices',TxVerticesHand1,'FaceColor','g');  % draw the red cube
axis([-AxisRange AxisRange -AxisRange AxisRange -AxisRange AxisRange]);
grid();
material shiny;
alpha('color');
alphamap('rampdown');
view(30,30);
title('transformed');
set(gca,'xticklabel',[]);set(gca,'yticklabel',[]); set(gca,'zticklabel',[]);

