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!