MATLAB Answers

1

Screen (2D) projection of 3D plot

Asked by Oleg Komarov on 13 Oct 2015
Latest activity Commented on by Oleg Komarov on 13 Oct 2015
How can I get the screen projection (2D) of the following 3D plot:
h = ezplot3('t', 'sin(t)', '20*cos(t)', [0 10*pi]);
The following solution as in the example in viewmtx() does NOT achieve the expected result (although it does for e.g. h = ezplot3('cos(t)', 'sin(t)', 'sin(5*t)', [-pi pi])):
% Original data
data = get(h,{'XData','YData','Zdata'});
data = [cat(1,data{:})', ones(numel(data{1}),1)];
% Projection matrix
[az,el] = view();
A = viewmtx(az,el);
% Projected data
data_transformed = A*data';
plot(data_transformed(1,:), data_transformed(2,:))

  0 Comments

Sign in to comment.

1 Answer

Answer by Mike Garrity
on 13 Oct 2015
 Accepted Answer

I talked about the theory behind this in this recent post on the MATLAB Graphics blog.
The basic idea is that you want to take the data from your object like this:
h = ezplot3('t', 'sin(t)', '20*cos(t)', [0 10*pi]);
pts = [h.XData; h.YData; h.ZData; ones(size(h.XData))];
and then multiply it by the view transform. Unfortunately accurately reproducing the view transform for an arbitrary 3D MATLAB plot can be a bit tricky. It is pretty close to this:
mat = viewmtx(-37.5,30) * makehgtform('scale',1./[20 1 20])
The first half of that is the azimuth and elevation. The second half is the DataAspectRatio.
Once you have that matrix, you can transform your points like this:
pt2 = mat * pts;
x = pt2(1,:) ./ pt2(4,:);
y = pt2(2,:) ./ pt2(4,:);
If we plot those, we'll see that I'm close, but slightly off:
plot(x,y)
axis equal
I think I've neglected to account for the PlotBoxAspectRatio, but I'm not 100% sure of that.
Hope that helps.

  1 Comment

Haha that was sync thinking. I just posted on your blog :)

Sign in to comment.