Get 3d Coordinate Of Mouse In Axis

2019-09-06 21:03发布

问题:

I used below code from link to plot mouse position in axes(when mouse moving in axes) :

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)

but plotted position differs from mouse location.

回答1:

Finally Found Another Solution : 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);