Vector/Matrix                                                                                        Home : www.sharetechnote.com

 

 

 

Rotation in 3D along 3 Axis - Example using Homeogenous Matrix

 

This is an example of translating a 3D object (a Cube) using a Homogenous Matrix in the form as shown below. The transformation matrix is applied (multiplied) to each vertex of the cube. The three matrix (Rotating around each axis) is multiplied to all the vertices in sequence. The sequence of matrix multiplication is as shown below.

 

 

The result of this example is as shown below.

 

 

Following is the MatLab code for this example. For now, don't pay too much about the code itself, just focus on number marked in red and try to undertand the mathematical meaning intuitively. You can just copy the code here into your Matlab and change the numbers in red part until you develop the intuitive understanding.

Note : When copy and paste this code, '...' may cause some error. In that case, erase '...' and retry '...' in your matlab editor.

     

    clear all;

     

    vert = [-0.5 -0.5 -0.5;  ...

            -0.5 0.5 -0.5;  ...

             0.5 0.5 -0.5;  ...

             0.5 -0.5 -0.5; ...

            -0.5 -0.5 0.5; ...

             -0.5 0.5 0.5;  ...

              0.5 0.5 0.5; ...

              0.5 -0.5 0.5];

       

    fac = [1 2 3 4; ...

        2 6 7 3; ...

        4 3 7 8; ...

        1 5 8 4; ...

        1 2 6 5; ...

        5 6 7 8];

     

    % original object

    subplot(2,4,1);

    patch('Faces',fac,'Vertices',vert,'FaceColor','r');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(30,30);

    title('original');

     

    % view along y-axis (x-z plane)

    subplot(2,4,2);

    patch('Faces',fac,'Vertices',vert,'FaceColor','r');

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(0,0);

    title('x-z plane');

     

    % view along x-axis (y-z plane)

    subplot(2,4,5);

    patch('Faces',fac,'Vertices',vert,'FaceColor','r');

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(90,0);

    title('y-z plane');

     

    % view along z-axis (x-y plane)

    subplot(2,4,6);

    patch('Faces',fac,'Vertices',vert,'FaceColor','r');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(0,90);

    title('x-y plane');

     

    % transforming object

    VerticesHom = zeros(8,4);

    for i = 1:8

        VerticesHom(i,:) = [vert(i,:) 1];

    end

     

    phiX = pi/3;

    TxMatrixX = [1    0       0        0;...

                0 cos(phiX) -sin(phiX) 0; ...

                0 sin(phiX) cos(phiX)  0; ...

                0     0       0        1];

            

    phiY = pi/4;

    TxMatrixY = [cos(phiY)  0 -sin(phiY) 0;...

                 0          1      0     0; ...

                 sin(phiY)  0  cos(phiY) 0; ...

                 0          0       0    1];

            

    phiZ = pi/8;

    TxMatrixZ = [cos(phiZ) -sin(phiZ)  0  0;...

                 sin(phiZ) cos(phiZ)   0  0; ...

                 0         0           1  0; ...

                 0         0           0  1];

            

    TxVerticesHom = TxMatrixZ * TxMatrixY * TxMatrixX * VerticesHom';

    TxVerticesHom = TxVerticesHom';

     

    TxVertices = zeros(8,3);

    for i = 1:8

        TxVertices(i,:) = TxVerticesHom(i,1:3);

    end

     

    % transformed object

    subplot(2,4,3);

    patch('Faces',fac,'Vertices',TxVertices,'FaceColor','b');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(30,30);

    title('transformed');

     

    % view along y-axis (x-z plane)

    subplot(2,4,4);

    patch('Faces',fac,'Vertices',TxVertices,'FaceColor','b');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(0,0);

    title('x-z plane');

     

    % view along x-axis (y-z plane)

    subplot(2,4,7);

    patch('Faces',fac,'Vertices',TxVertices,'FaceColor','b');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(90,0);

    title('y-z plane');

     

    % view along z-axis (x-y plane)

    subplot(2,4,8);

    patch('Faces',fac,'Vertices',TxVertices,'FaceColor','b');  

    axis([-2 2 -2 2 -2 2]);

    grid();

    material shiny;

    alpha('color');

    alphamap('rampdown');

    view(0,90);

    title('x-y plane');