MATLAB: How to get the new position coordinates of the object after I rotate it using the HGTRANSFORM function

datamakehgtformMATLABnewposition;rotatetranslate

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

Best Answer

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');