How can I project points from "World" coordinates to monitor pixel locations in MATLAB?

1 view (last 30 days)
I am trying to use the transform matrix returned by the VIEW or VIEWMTX functions to tranform point coordinates (as returned by the 'currentPoint' property) into pixel locations on the computer monitor. The two points (front and back) returned by 'CurrentPoint' do not project onto the same location.
In other words, after the projection, these two points do not have the same screen coordinates as I have computed them when they necessarily should.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The ability to transform a point's coordinates from the "World" coordinate system to the computer monitor's coordinate system is not available as a MATLAB function.
The VIEW and VIEWMTX functions return what is called a "view transform" matrix. This matrix is not sufficient to transform a point's coordinates in the "world" coordinate system to its coordinates in the computer screen's coordiunate system. The reason for this is as follows:
The "view transform" is only one part of the transform process from "world coordinates" to "monitor/screen coordinates".
To transform from world coordinates to pixels, one should use the product of the
following transform matrices:
viewport transform matrix
projection transform matrix
view transform matrix
normalizing transform matrix
model transform matrices
In the case at hand, the normalizing transform is missing from the equation. The normalizing matrix N looks like this:
xl=get(gca,'xlim');
yl=get(gca,'ylim');
zl=get(gca,'zlim');
N=[1/(xl(2)-xl(1)) 0 0 0; ...
0 1/(yl(2)-yl(1)) 0 0; ...
0 0 1/(zl(2)-zl(1)) 0; ...
0 0 0 1];
Using this normalizing transform (which is based on the axis' limits), one can then compute the following:
A=view;
A = A*N;
This way, A will be a matrix that will get the front and back points to transform to the same place on the screen.
This is not the "correct" place because the remaining matrices have not been factored in. Please note that these matrices are not generated by MATLAB functions and are not directly accessible from the axes data. They are listed here for information purposes.
The viewport transform is simply the size of the window. It can account for the case where the window is and nonsquare rectangle.
The projection transform is simply a uniform scale factor in this case, but it can be quite complex in a perspective view.
The model transforms are the matrix properties of HGTRANSFORM objects.

More Answers (0)

Products


Release

R2006a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!