# How to find the distance between a point and a curve or between two curve using extrema in OCC

Hi

I am having two interpolated Geom2d_BSplineCurve curves.Using extrema , how i can find the distance between the two curves.

for my work , I doesn't need of any visualization.Basically I need only the normal distance values between the curves.

It would be grateful if explained with any sample code.

rgds
Senthil

The most reliable way is to create edges from your curves and use BRepExtrema_DistShapeShape. For example (untested):

TopoDS_Edge edge1 = BRepBuilderAPI_MakeEdge2d(curve1);
TopoDS_Edge edge2 = BRepBuilderAPI_MakeEdge2d(curve2);
BRepExtrema_DistShapeShape extrema(edge1, edge2);
if (extrema.IsDone()) minDist = extrema.Value();

hi Rob ,

my code as follow

Handle(TColgp_HArray1OfPnt2d) harray =
new TColgp_HArray1OfPnt2d (1,5); // sizing harray
harray->SetValue(1,gp_Pnt2d (7+ 0,0));
harray->SetValue(2,gp_Pnt2d (7+ 1,2));
harray->SetValue(3,gp_Pnt2d (7+ 2,3));
harray->SetValue(4,gp_Pnt2d (7+ 4,3));
harray->SetValue(5,gp_Pnt2d (7+ 5,5));
Geom2dAPI_Interpolate anInterpolation(harray,Standard_False,0.01);
anInterpolation.Perform();
Handle(Geom2d_BSplineCurve) SPL1 = anInterpolation.Curve();

Handle(TColgp_HArray1OfPnt2d) harray2 =
new TColgp_HArray1OfPnt2d (1,5); // sizing harray
harray2->SetValue(1,gp_Pnt2d (11+ 0,0));
harray2->SetValue(2,gp_Pnt2d (11+ 1,2));
harray2->SetValue(3,gp_Pnt2d (11+ 2,3));
harray2->SetValue(4,gp_Pnt2d (11+ 4,3));
harray2->SetValue(5,gp_Pnt2d (11+ 5,5));
Geom2dAPI_Interpolate anInterpolation2(harray2,Standard_True,0.01);
anInterpolation2.Perform();
Handle(Geom2d_BSplineCurve) SPL2 = anInterpolation2.Curve();

TopoDS_Edge edge1 = BRepBuilderAPI_MakeEdge2d(SPL1);
TopoDS_Edge edge2 = BRepBuilderAPI_MakeEdge2d(SPL2);
BRepExtrema_DistShapeShape extrema(edge1, edge2);
if (extrema.IsDone())
int minDist = extrema.Value();

while executing the code , i am receiving Zero in the minDist .

I am new to OCC , correct me if the code is not addressing to my requirements

rgds
Senthil

hi Rob ,

my code as follow

Handle(TColgp_HArray1OfPnt2d) harray =
new TColgp_HArray1OfPnt2d (1,5); // sizing harray
harray->SetValue(1,gp_Pnt2d (7+ 0,0));
harray->SetValue(2,gp_Pnt2d (7+ 1,2));
harray->SetValue(3,gp_Pnt2d (7+ 2,3));
harray->SetValue(4,gp_Pnt2d (7+ 4,3));
harray->SetValue(5,gp_Pnt2d (7+ 5,5));
Geom2dAPI_Interpolate anInterpolation(harray,Standard_False,0.01);
anInterpolation.Perform();
Handle(Geom2d_BSplineCurve) SPL1 = anInterpolation.Curve();

Handle(TColgp_HArray1OfPnt2d) harray2 =
new TColgp_HArray1OfPnt2d (1,5); // sizing harray
harray2->SetValue(1,gp_Pnt2d (11+ 0,0));
harray2->SetValue(2,gp_Pnt2d (11+ 1,2));
harray2->SetValue(3,gp_Pnt2d (11+ 2,3));
harray2->SetValue(4,gp_Pnt2d (11+ 4,3));
harray2->SetValue(5,gp_Pnt2d (11+ 5,5));
Geom2dAPI_Interpolate anInterpolation2(harray2,Standard_True,0.01);
anInterpolation2.Perform();
Handle(Geom2d_BSplineCurve) SPL2 = anInterpolation2.Curve();

TopoDS_Edge edge1 = BRepBuilderAPI_MakeEdge2d(SPL1);
TopoDS_Edge edge2 = BRepBuilderAPI_MakeEdge2d(SPL2);
BRepExtrema_DistShapeShape extrema(edge1, edge2);
if (extrema.IsDone())
int minDist = extrema.Value();

while executing the code , i am receiving Zero in the minDist .

I am new to OCC , correct me if the code is not addressing to my requirements

rgds
Senthil

The computed distance is about 0.997. You are placing this value in an "int", so it is getting truncated to 0. Try placing the value in a "Standard_Real" or "double".

Hi Rob,

Thanks for your inputs to show the correct way.

is it possible to find the particular points which makes the closest distance?

similiarly how I can find the distance between a point and a Geom2d_BSplineCurve curve.

Need your inputs regarding the same.

rgds
Senthil

Look at the documentation on BRepExtrema_DistShapeShape.
NbSolution() gives the number of point pairs giving the minimum distance.
PointOnShape1 & PointOnShape2 allow you to access the point pairs.
ParOnEdgeS1 & ParOnEdgeS2 allow you to access the parametric locations on the curves.

To find the distance between a point and the curve, make an edge from the curve and make a TopoDS_Vertex from the point. Then use the extrema as before.