transform a patch using a transform object
3 views (last 30 days)
Show older comments
Hi,
I created a square face at the origin using the "patch" function. Then I defined the square as a child of a transform object. I started to move the transform object to several locations and changed its orientation. The square object followed well the transform object. The problem is that when I asked to get the current vertices of the square (after it was moved), I got the original vertices around the origin. I need the current vertices in order to calculate the current normal-vector of that square-face.
Thanks
0 Comments
Answers (1)
Walter Roberson
on 23 Apr 2016
Let p be the patch handle. Let hgtrans be the hgtransform object. Then,
M = get(hgtrans, 'Matrix');
xd = get(p, 'XData');
xd = xd(:);
yd = get(p, 'YData');
try
zd = get(p, 'ZData');
if isempty(zd); zd = zeros(size(xd)); end
catch
zd = zeros(size(xd));
end
XYZ1 = [xd, yd(:), zd(:), ones(size(xd))];
XYZ1_trans = XYZ1 * M';
x_trans = XYZ1_trans(:,1);
y_trans = XYZ1_trans(:,2);
z_trans = XYZ1_trans(:,3);
p does not need to be a patch, but it does need to have XData and YData; if it does not have ZData then the code will compensate. For example, image objects do not have ZData but can validly be translated to a non-zero Z coordinate. Some convenient graphics objects such as rectangle() objects do not have XData or YData; the code here does not account for those.
3 Comments
Walter Roberson
on 24 Apr 2016
No, there is no direct way. You need to multiply out all of the transform matrices.
For clarity: when you get the XData, YData, ZData of a graphics object, those are always the values that you passed to the graphics routines, not the transformed values.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!