# How can I get the new position coordinates of my object after I rotate it using the HGTRANSFORM function?

32 views (last 30 days)
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 on 6 Jun 2019
Edited: MathWorks Support Team on 6 Jun 2019
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');

David Verrelli on 20 Aug 2015
Edited: David Verrelli on 21 Aug 2015
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