How can I get the new position coordinates of my object after I rotate it using the HGTRANSFORM function?
3 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2009-6-27
编辑: MathWorks Support Team
2019-4-24
I created a surface using the example in the documentation for the 'hgtransform' function. I then rotate it through some angle with respect to an arbitrary axis. I would like to obtain the position of the points after doing this. However, the 'xdata','ydata' and 'zdata' property of the handle does not change.
ax = axes('XLim',[-1.5 1.5],'YLim',[-1.5 1.5],'ZLim',[-1.5 1.5]);
view(3); grid on; axis equal
[x y z] = cylinder([.2 0]);
h = surface(x,y,z,'FaceColor','red');
xlabel('x'); ylabel('y'); zlabel('z');
t = hgtransform('Parent',ax);
set(h,'Parent',t)
set(gcf,'Renderer','opengl')
drawnow
x_temp = get(h,'xdata');
y_temp = get(h,'ydata');
z_temp =get(h,'zdata');
Rz = eye(4);
Sxy = Rz;
r = pi;
Rz = makehgtform('xrotate',r);
set(t,'Matrix',Rz*Sxy)
drawnow
采纳的回答
MathWorks Support Team
2019-6-6
编辑:MathWorks Support Team
2019-4-24
The new position coordinates can be formed by applying the rotation matrices to the initial coordinates. An example of how you can determine the new positions follows:
First, the cone is graphed and transformed using the 'hgtransform' function, and the values of the 'xdata', 'ydata', and 'zdata' properties are queried.
ax = axes('XLim',[-1.5 1.5],'YLim',[-1.5 1.5],'ZLim',[-1.5 1.5]);
view(3); grid on; axis equal
[x y z] = cylinder([.2 0]);
h = surface(x,y,z,'FaceColor','red');
xlabel('x'); ylabel('y'); zlabel('z');
t = hgtransform('Parent',ax);
set(h,'Parent',t)
set(gcf,'Renderer','opengl')
drawnow
x_temp = get(h,'xdata');
y_temp = get(h,'ydata');
z_temp =get(h,'zdata');
Rz = eye(4);
Sxy = Rz;
r = pi;
Rz = makehgtform('xrotate',r);
% Sxy = makehgtform('scale',r/4);
set(t,'Matrix',Rz*Sxy)
drawnow
% end
Then, the transform matrix is used along with the 'maketform' and 'tformfwd' functions of the Image Processing Toolbox to perform the transformation on the coordinates.
T = maketform('affine',Rz)
[xx(1,:), yy(1,:), zz(1,:)] = tformfwd(T, x_temp(1,:),y_temp(1,:),z_temp(1,:));
[xx(2,:), yy(2,:), zz(2,:)] = tformfwd(T, x_temp(2,:),y_temp(2,:),z_temp(2,:));
If you do not have the Image Processing Toolbox, this operation can be performed with a for-loop and standard matrix multiplication.
%calculate new coordinates by multiplying by the rotation matrix.
%
% Rz * old_matrix = new_matrix
% Where old_matrix =[x_pos;y_pos;z_pos;1]
%
for i = 1:21
new_first_row(i,:) = (Rz* [x_temp(1,i);y_temp(1,i);z_temp(1,i);1])';
end
for i = 1:21
new_second_row(i,:) = (Rz* [x_temp(2,i);y_temp(2,i);z_temp(2,i);1])';
end
xx = new_first_row(:,1)';
xx(2,:) = new_second_row(:,1)';
yy = new_first_row(:,2)';
yy(2,:) = new_second_row(:,2)';
zz = new_first_row(:,3)';
zz(2,:) = new_second_row(:,3)';
You can now use the coordinates returned by the transformation to create the surface. Comparing this figure to the original figure, you can see that the transformation is as expected.
figure; ax = axes('XLim',[-1.5 1.5],'YLim',[-1.5 1.5],...
'ZLim',[-1.5 1.5]);
view(3); grid on; axis equal
[x y z] = cylinder([.2 0]);
h(1) = surface(xx,yy,zz,'FaceColor','red');
xlabel('x'); ylabel('y'); zlabel('z');
0 个评论
更多回答(1 个)
David Verrelli
2015-8-20
编辑:David Verrelli
2015-8-21
Two short follow-up points:
(1) It doesn't seem to be necessary to split the manual transformation into two separate steps
[xxxx, yyyy, zzzz] = tformfwd(T, x_temp,y_temp,z_temp);
seems to work just fine.
(2) maketform is now [R2014b] apparently deprecated: "maketform is not recommended. Use fitgeotrans, affine2d, affine3d, or projective2d instead." The updated code would be
TAffine = affine3d(Rz)
[xxx, yyy, zzz] = transformPointsInverse(TAffine, x_temp,y_temp,z_temp);
N.B. I have amended this code from transformPointsForward, which didn't work for me in my testing on another shape
This information is useful for computing/evaluating the extents or bounds of a transformed shape or volume. —DIV
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Object Containers 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!