# Finding Transformation Matrix of a viewer3d object

10 views (last 30 days)
Reza on 20 Feb 2024
Commented: Reza on 21 Feb 2024
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?
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.
Reza on 20 Feb 2024
Yes, that could be the case. I was thinking of calculating the position of my 3D points to the 2D coordinates and see which ones resides inside the polygon. But it could also be the other way around (maybe more complicated though)

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.
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.
Reza on 21 Feb 2024
I'm not going to show the points to the user. The user only sees the nice viewer3D rendered image, then selects some parts of it for edits. That's why it was important to figure out the mapping precisely.
Everything needs to look professional, the app should have the feeling of a final product.
Actually I was surprized myself how I could create a professional looking app using matlab!

Catalytic on 20 Feb 2024
Edited: Catalytic on 20 Feb 2024
Maybe you could use estimateCameraParameters using a list of hand-picked points.
Reza on 20 Feb 2024
Thanks for your answer, that could certainly be helpful while I try to figure out how this system is working.
I'm wonderring how that could be eventually, since I'm developing an app which would be used by the end user. I want the user to be able to view their 3D object, and select a set of points to remove or modify. So it would be difficult to ask them to handpick some points. Or maybe I could do that automatically?
I was hoping there would be a way to extract usable camera properties for this task.

### Categories

Find more on Graphics Performance in Help Center and File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!