How to converts the position of a point in global coordinate system to position in a local coordinate system

Hi Bros, I got an axis (gp_Ax1), it's the axis of symmetry from a cylinder; and I also have a point.

I want to get the shortest distance from point to axis.

I have two ideas:
(1) create a line (gp_Lin) with the axis (gp_Ax1), and then find a tool to get the shortest distance.
However, I didn't find a tool to calculate the the shortest distance between line and point. (I find the
tool:GeomAPI_ProjectPointOnCurve, it can get the extrema between point and curve).

(2) get the local coordinate system from the cylinder, and converts the position of the point in global coordinate system to position in the local coordinate system, then can get the shortest distance easy.
However, I don't know how to complete it.

So I try to get help from you.

Mikhail Sazonov's picture

The class gp_Lin has the method Distance that does the job. See manual

Sathiya nathan's picture

gp_Pnt axisPnt1; // Cylinder axis top point, in local coordinate
gp_Pnt axisPnt2; // Cylinder axis bottom point, in local coordinate

// Convert local to global scene coordinate
const TopLoc_Location &loc = aisCylinder->InteractiveContext ()->Location (aisCylinder);
axisPnt1 = axisPnt1.Transformed (loc.Transformation ());
axisPnt2 = axisPnt2.Transformed (loc.Transformation ());

Use these two points and create a Line.
Then find shortest distance between line to point(mouse point) .
Hope it would work.