Finding Transformation Matrix of a viewer3d object
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Matt J
2024-2-20
编辑:Matt J
2024-2-20
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.
采纳的回答
Matt J
2024-2-20
编辑:Matt J
2024-2-21
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 个评论
Matt J
2024-2-21
编辑:Matt J
2024-2-21
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Support Package for USB Webcams 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!