coordinate in 3D


I want to convert the position of mouse to the coordinate in 3D.

I used the following.

view->Convert(mx, my, x, y, z);

But, there may be much deviation.

Could you tell me the way to convert it correctly?

Stephane Routelous's picture


I've done a function a long time ago to do what you want.


Standard_Boolean exotkUtils_View::Convert2dPntTo3dPnt(const Handle_V3d_View& aView, const Standard_Integer aX2d, const Standard_Integer aY2d, gp_Pnt& a3dPoint)
if (aView.IsNull())
return Standard_False;

// get the eye and the target points
V3d_Coordinate theXEye, theYEye, theZEye, theXAt, theYAt, theZAt;
aView->Eye(theXEye, theYEye, theZEye);
aView->At(theXAt, theYAt, theZAt);
gp_Pnt theEyePoint(theXEye, theYEye, theZEye);
gp_Pnt theAtPoint(theXAt, theYAt, theZAt);

// create the direction
gp_Vec theEyeVector(theEyePoint, theAtPoint);
gp_Dir theEyeDir(theEyeVector);

// make a plane perpendicular to this direction
gp_Pln thePlaneOfTheView = gp_Pln(theAtPoint, theEyeDir);

// convert the 2d point into 3d
Standard_Real theX, theY, theZ;
aView->Convert(aX2d, aY2d, theX, theY, theZ);
gp_Pnt theConvertedPoint(theX, theY, theZ);

// project the converted point to the plane
gp_Pnt2d theConvertedPointOnPlane = ProjLib::Project(thePlaneOfTheView, theConvertedPoint);

// get the 3d point of this 2d point
gp_Pnt theResultPoint = ElSLib::Value(theConvertedPointOnPlane.X(), theConvertedPointOnPlane.Y(), thePlaneOfTheView);
a3dPoint = theResultPoint;
return Standard_True;


mir7942's picture

Thank you.

what is the followings?

ProjLib::Project(thePlaneOfTheView, theConvertedPoint);

ElSLib::Value(theConvertedPointOnPlane.X(), theConvertedPointOnPlane.Y(), thePlaneOfTheView);

Stephane Routelous's picture

ProjLib::Project(thePlaneOfTheView, theConvertedPoint);
Project the converted point on thePlaneOfTheView

ElSLib::Value(theConvertedPointOnPlane.X(), theConvertedPointOnPlane.Y(), thePlaneOfTheView);
Convert the 2d point in the space of thePlaneOfTheView into 3d space coordinates


Rahul's picture

ProjLib::Project is not valid , can I have the new code

DELORME's picture


When I use this routine to display the current 3D position corresponding to the mouse cursor while rotating, it messes the rotation(buggy).
I use routines V3d_View::StartRotation() and V3d_View::Rotation().
I took a look at the source code and realized that calling V3d_View::Eye() modifies some global variables defined as static in V3d_View.cxx (in particular the variable MyViewReferencePoint).
So use V3d_View::Eye() with care.
I modified stephane's code to avoid calling V3d_View::Eye(). To build the view plane, I rather use the projection orientation :

gp_Pnt to_3d_point (const Handle_V3d_View& view, Standard_Integer y, Standard_Integer y)
assert (!view.IsNull ());//view_not_null

// Make a plane perpendicular to the projection orientation.
Standard_Real x_ori, y_ori, z_ori;
view->Proj (x_ori, y_ori, z_ori);
gp_Dir proj_orientation (x_ori, y_ori, z_ori);
gp_Pln view_plane = gp_Pln (gp_Pnt (0, 0, 0), proj_orientation);
// Convert the 2d point into a 3d point.
Standard_Real xp, yp, zp;
view->Convert (x, y, xp, yp, zp);
gp_Pnt converted_pnt (xp, yp, zp);
// Project the converted point in the plane.
gp_Pnt2d projected_pnt = ProjLib::Project (view_plane, converted_pnt);
// Get a 3d point from this 2d point.
return ElSLib::Value (projected_pnt.X (), projected_pnt.Y (), view_plane);

fhchina's picture

Hi, Dear Stephane , Could you tell me how can I confine the mouse movement in one plane(such as in a Face of TopoDS_Shape) in a 3D view? And unfortunately your website is unaccessible for me. Is all the infomation of this site is available in

farges's picture

i use the method given by Stephane but there is still a deviation.
Have you got one?

Selahattin BABADAĞ's picture

why why why i get wrong results when i to convert mouse coordinates to world coordinates when the view is 3d , but when the view is plane it gives true coordinates?