Finding Transformation Matrix of a viewer3d object
5 views (last 30 days)
Show older comments
I need to map 3D vertices in a viewer3d object to 2d figure coordinates. It's actually because I want the user of my app to be able to select a set of points by drawing a polygon, as I have mentioned here.
The viewer3d has some camera properties: cameraPosition, cameraTarget, cameraUpvector and cameraZoom.
But appearently I cannot use these parameters to construct a transformation matrix, since the camera zoom seems to be relative; it's 1.0 at the beginning of the display and is not a function of the window size, object dimension etc.
Is there any way I could extract the camera information needed?
4 Comments
Matt J
on 20 Feb 2024
Edited: Matt J
on 20 Feb 2024
It's actually because I want the user of my app to be able to select a set of points by drawing a polygon
If so, a mapping from 3D to 2D is not what you would need. You need the other way around. A polygon is 2D, so the input to your mapping would be a 2D selection and the output would be 3D locations on the object.
Accepted Answer
Matt J
on 20 Feb 2024
Edited: Matt J
on 21 Feb 2024
I think the 3x4 camera projection matrix P would be,
h=gca();
camz=h.CameraTarget-h.CameraPosition;
camy=-h.CameraUpVector;
camx=cross(camy,camz);
P = normalize([camx;camy;camz],2,'n',2) * [eye(3), -h.CameraPosition(:)];
This P should map things from 3D data units to 2D coordinates on the plane shown in Graphics Camera Terminology. I will call this 2D coordinate space "GCT".
Using this, you should be able to find the coordinates of the "corners" of the image rectangle enclosing the 3D axes in GCT coordinates:
C=table2array(combinations(xlim,ylim,zlim))'; %3D corners
fn=@(q) q(1:2,:)./q(3,:); %resolve homogeneous projection
cmap= fn( P*[C;ones(1,8)] ); %3D corners mapped to GCT
upperLeft=min(cmap,[],2); %upper left corner of plot box in GCT coords
lowerRight=max(cmap,[],2); %ower right corner of plot box in GCT coords
Finally, getframe() produces an image whose corners, in pixel coordinates will correspond 1-1 with upperLeft and lowerRight, so you can use that to map your drawpolygon() vertices from image pixel coordinates to 2D GCT coordinates.
So, doing all of that will map your viewer3D coordinates and your polygon vertex coordinates to a common 2D coordinate system.
4 Comments
Matt J
on 21 Feb 2024
Edited: Matt J
on 21 Feb 2024
Do I need to provide full details or add another answer for others to see?
What you have is fine, I think.
I'm a bit surprised that it matters, though. Even if the up vector is not normal to the view direction, I would still expect that the transformed polygon would enclose the transformed points, even if the dimensions are distorted, which is the only thing your app requires.
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!