在轴上获取鼠标的3d坐标

时间:2014-11-11 10:47:01

标签: matlab mouse coordinates axes

我使用link下面的代码在轴上绘制鼠标位置(当鼠标在轴上移动时):

point = get(gca, 'CurrentPoint'); % mouse click position
camPos = get(gca, 'CameraPosition'); % camera position
camTgt = get(gca, 'CameraTarget'); % where the camera is pointing to
camDir = camPos - camTgt; % camera direction
camUpVect = get(gca, 'CameraUpVector'); % camera 'up' vector
zAxis = camDir/norm(camDir);
upAxis = camUpVect/norm(camUpVect);
xAxis = cross(upAxis, zAxis);
yAxis = cross(zAxis, xAxis);

rot = [xAxis; yAxis; zAxis]; % view rotation
rotatedPointFront = rot * point' ;
plot3(rotatedPointFront(1),rotatedPointFront(2),rotatedPointFront(3), 'r.','MarkerSize', 20)

但绘制的位置与鼠标位置不同。 enter image description here

1 个答案:

答案 0 :(得分:0)

终于找到了另一个解决方案: http://www.mathworks.com/matlabcentral/fileexchange/1600-dispcoord1-2-m

hold on;
pos=get(gca,'CurrentPoint');
POS=mean(pos);
POS=round(POS*1000)/1000;
disp(POS);
plot3(POS(1),POS(2),POS(3),'r.', 'MarkerSize', 10);