Combined rotation and translation on a cube

8 次查看(过去 30 天)
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');

采纳的回答

Arnaud Miege
Arnaud Miege 2011-4-7

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

更多回答(2 个)

Arnaud Miege
Arnaud Miege 2011-4-7
Here's why it doesn't work:
>> size(combined)
ans =
4 4
>> size([-50,-50,0])
ans =
1 3
You are trying to multiply a 1x3 vector by a 4x4 matrix, hence the error message about matrix dimensions. Either make your matrix 3x3 or your vector 1x4.
HTH,
Arnaud
  2 个评论
Andrew
Andrew 2011-4-7
Thanks for your response. if i try to make the vector 1 x 4 i still get an error. This is what i mentioned earlier about the patch problem. Although the program will draw my axes, it will not display the cube. Any thoughts?
Arnaud Miege
Arnaud Miege 2011-4-7
I'm not very familiar with geometry, but shouldn't your rotation, etc... matrices be 3x3 instead of 4x4, which would mean that CV and CF should 8x3 and 6x3. Did you have a look at the documentation for the patch command:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/patch.html

请先登录,再进行评论。


alejandro perez
alejandro perez 2020-2-2
In the description of this video is the code of a cube which rotates, moves and grows up.

类别

Help CenterFile Exchange 中查找有关 Polygons 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by