Problem in converting mouse coordinates

Hi,
I have seen that the conversion of mouse coordinates have some problem especially in 3d. I have converted the mouse coordiantes and displayed in the statusbar. In Any planar mode, the corresponding horizontal and vertical coodinates are displayed correct but the third axis coordinate comes something like + or - 1500. eg when in the top view the x and y coordinates change as expected but the z coordinate is 1500 which is expected to be 0. Similarly when I switch to the axo view the origin of the trihedron shows 150, 150, 150 whereas it is expected to be 0,0,0.
Can anybody help please?

N.Sharjith

fhchina's picture

How you convert mouse coodinates? If you simply use V3d_View::Convert as FAQ pinpoints, it will give incorrect result. But in MFC Samples there is a function ConvertClickToPoint, it works well in my code.

fhchina

Sharjith Naramparambath's picture

Hi fhchina,
Yes I am using the V3d_View::Convert only. Is it incorrect? Which sample do you mean? Please help

Thanks a lot

N. sharjith

Stephane Routelous's picture

Hi,

as fhchina said in a previous post, use the function ConvertClickToPoint ( see at the bottom of this mail )

I wrote this function because of a problem I had converting 2d point in 3d.
The function V3d_View::Convert works fine.
The only problem is that you cannot control the "depth" of your click.
With this function, you have a better result.
In OpenCASCADE, when you are rotating your view, the Eye and Target points are updated according to ( more or less ) the last 3d point you picked ( during a selection ) or to the center of your objects.

So, I created a plane perpendicular to the view direction passing through the target point ( AtPoint ). After that, the converted 3d point ( the "wrong" point ) is projected on this plane, giving you a 2d point ( ConvertedPointOnPlane ). This 2d point is converted in 3d and returned.

With this function, the converted point is always ( quite ) near to the last selected point, so the result is better than the "original" convert function from V3d_View.

I hope it is clear enough.

Stephane
http://www.exotk.org

gp_Pnt ConvertClickToPoint(Standard_Real x, Standard_Real y, Handle(V3d_View) aView)
{

V3d_Coordinate XEye,YEye,ZEye,XAt,YAt,ZAt;
aView->Eye(XEye,YEye,ZEye);
aView->At(XAt,YAt,ZAt);
gp_Pnt EyePoint(XEye,YEye,ZEye);
gp_Pnt AtPoint(XAt,YAt,ZAt);

gp_Vec EyeVector(EyePoint,AtPoint);
gp_Dir EyeDir(EyeVector);

gp_Pln PlaneOfTheView = gp_Pln(AtPoint,EyeDir);
Standard_Real X,Y,Z;
aView->Convert(x,y,X,Y,Z);
gp_Pnt ConvertedPoint(X,Y,Z);
gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project(PlaneOfTheView,ConvertedPoint);

gp_Pnt ResultPoint = ElSLib::Value(ConvertedPointOnPlane.X(),
ConvertedPointOnPlane.Y(),
PlaneOfTheView);
return ResultPoint;
}

Sharjith Naramparambath's picture

Hi,
Thanks a lot Stephane. Now I think all my problem related to this will be solved, including the facility of allowing the user create lines and circles confined to the sketch plane even in axo views.

N.Sharjith

Rahul's picture

ProjLib::Project the following is not valid I tried to use this function.

Selahattin BABADAĞ's picture

Dear Stephane ;

The function you gave works on XY , XZ , YZ Views but in 3d Views , especially when I rotate the view with mouse it gives wrong points ;

I tried all the examples given over internet but none of them gave me the correct result in 3d view.

I really didint understand why? Any body had the same issue? and found a solution???

Mauro C's picture

Hi, what do you mean exacltly?
I suppose, that when you switch to a 3D AXO projection, you get bad results.
Is it so?

If so, I have solved just such question , few months ago; so, I am able to draw interactively with mouse on XY, or XZ or YZ plane, staying in 3d AXO view.
Not sure 100% (because not tested) , on any oriented plane , ie say a plane with a normal of 1,1,1 . But I thnk of yes
Let me know if I have well understood your issue

Selahattin BABADAĞ's picture

Dear Mauro ;

Exactly the issue is like you told ;

if the view is 3d (not plane like xy xz yz) , than the convert mouse coordinates to world coordinates is not true; but in plane views its correct.

Do you have a code for your solution? can you share it?

Selahattin BABADAĞ's picture

Can you share the solution?

Mauro C's picture

Sure,give me just a day,to collect c++ code

Mauro C's picture

Dear Selahattin ,Attached you find fragment of c++ code to draw a circle with mouse , dynamically , in a predefined "workplane". In my example , there are just 3 workplanes : XY,YZ and ZY. 
Maybe it runs for any Worplane ( I haven't check it).
In my application, at starting there 3 workplanes diplayed, and with the mooue , the user clicks on wnated WorPlane.I suggest you to move by step

Byway The key of all, is to "filter" and convert the mouse coordinate in WIndow coordinate , coming from events (onlButtonDown or OnMouseMove) , using the function  
gp_Pnt MyCadView::ConvertToPlane(Standard_Real x, Standard_Real y, Handle(V3d_View) aView,int nPlane)
This function accept :
The coordinate of mouse ( x,y,the handle to V3d_View) and the plane : in my code 0 for XY Plane,1 for YZ plane and 2 for XZ plane ( you can build any plane point origin + normal)
Then "draw" the circle with DrawCircle
In this way , you can draw with mouse any kind of entity 2d or also 3d ( box,cylinder....)
Let me know your opinion
Next step is draw with snaps,to draw with precision ( a bit hard..)

Selahattin BABADAĞ's picture

Dear Mauro ;

Thank you very much for spending time for me , your routine is working on plane views very good , but in perspective view there is no correct result that the variations i tried,

and some of the developers wrote about this that "There is no solution for getting 3D coordinates from perspective view" , only its possible if there is a shape under cursor , you can get that coordinates by

VK = new AIS_ViewController();

Graphic3d_Vec2i V;
V.x() = thePoint.x();
V.y() = thePoint.y();

 
gp_Pnt picked_3d_point = VK->PickPoint(RM_Coordinate,myContext,myView,V,true);

best regards