MATLAB: Combined rotation and translation on a cube

3dcombinedcubematriciesmatrixrotationtranslation

Hi All
I am trying to draw a simple cube in matlab and then translate this to the origin, rotate and then move back again. I want to achieve this by combining my translation and rotation matricies and apply this to each of my verticies.
I have tried various matrix manipulations and i keep getting errors telling me that the matrix dimensions dont agree or that there is an error in the patch command.
Below is the code i have so far. Im sure im within striking distance but i just cant see what the issue is. Any help will be greatly appreciated. Many Thanks
Andrew
close all;
z=1;
rotation=[cos(z),-sin(z),0,0;sin(z),cos(z),0,0;0,0,1,0;0,0,0,1];
move2origin=[-150,0,0,0;0,-150,0,0;0,0,1,0;0,0,0,1];
moveback=[150,0,0,0;0,150,0,0;0,0,1,0;0,0,0,1];
combined=move2origin*rotation*moveback;
CV = zeros(8,4); % vertex matrix
CF = zeros(6,4); % face matrix
CV(1,:) = [-50,-50,0]*combined; %vertex 1
CV(2,:) = [50,-50,0]*combined; %vertex 2
CV(3,:) = [50,50,0]*combined; %vertex 3
CV(4,:) = [-50,50,0]*combined; %vertex 4
CV(5,:) = [-50,-50,100]*combined; %vertex 5
CV(6,:) = [50,-50,100]*combined; %vertex 6
CV(7,:) = [50,50,100]*combined; %vertex 7
CV(8,:) = [-50,50,100]*combined; %vertex 8
CF(1,:) = [1,2,6,5];
CF(2,:) = [2,3,7,6];
CF(3,:) = [3,4,8,7];
CF(4,:) = [4,1,5,8];
CF(5,:) = [5,6,7,8];
CF(6,:) = [1,2,3,4];
view(3); axis([-200 200 -200 200 -200 200]);
patch('Vertices', CV, 'Faces', CF,'FaceVertexCData', hsv(6), 'FaceColor', 'flat');

Best Answer

The first step is drawing the cube. The following is from Multifaceted Patches in the doc:

vertex_matrix = [0 0 0
1 0 0
1 1 0
0 1 0
0 0 1
1 0 1
1 1 1
0 1 1];
faces_matrix = [1 2 6 5
2 3 7 6
3 4 8 7
4 1 5 8
1 2 3 4
5 6 7 8];
patch('Vertices',vertex_matrix,'Faces',faces_matrix,...
'FaceVertexCData',hsv(8),'FaceColor','interp')
view(3); axis square

The next step is doing the translation and rotation of the cube.

For the translation, say by 1 unit along the x-axis, I would do something like this:

translation_matrix = repmat([1 0 0],size(vertex_matrix,1),1);
vertex_matrix = vertex_matrix + translation_matrix;
patch('Vertices',vertex_matrix,'Faces',faces_matrix,...
'FaceVertexCData',hsv(8),'FaceColor','interp')
view(3); axis square

Not sure about the rotation, I'll leave it up to you to figure out how to manipulate the vertex and faces matrices.

HTH,

Arnaud