minimum distance between two curves

Hello together,
how can I use OCC to calculate the minimum distance between two curves, as in the picture?
GeomAPI_ExtremaCurveCurve calculates the distance between Extremas.

Attachments: 
Mikhail Sazonov's picture

GeomAPI_ExtremaCurveCurve first calculates all minimal distances using Extrema_ExtCC, and optionally you can request solutions using the end points.

If the algorithm does not calculate expected solution you are welcome to create a bug.

Aziz GH's picture

Thank you for your answer.

I have tested this function with the following data. The result is shown below.
For the two cases you don't get a distance between the lines, why?

Handle(Geom_TrimmedCurve) line1 = GC_MakeSegment(gp_Pnt(0, 0, 0), gp_Pnt(10, 0, 0));
Handle(Geom_TrimmedCurve) line2 = GC_MakeSegment(gp_Pnt(0, 10, 0), gp_Pnt(10, 10, 0));
Handle(Geom_TrimmedCurve) line3 = GC_MakeSegment(gp_Pnt(10, 10, 0), gp_Pnt(10, 0, 0));
Handle(Geom_TrimmedCurve) line4 = GC_MakeSegment(gp_Pnt(10, 20, 0), gp_Pnt(10, 10, 0));
Handle(Geom_TrimmedCurve) line5 = GC_MakeSegment(gp_Pnt(0, 10, 0), gp_Pnt(20, 0, 0));

GeomAPI_ExtremaCurveCurve extrema1(line1, line2);
GeomAPI_ExtremaCurveCurve extrema2(line1, line3);
GeomAPI_ExtremaCurveCurve extrema3(line1, line4);
GeomAPI_ExtremaCurveCurve extrema4(line1, line5);

qDebug() << "extream1:";
qDebug() << "nr: " << extrema1.NbExtrema();
if (extrema1.NbExtrema() > 0)
qDebug() << "lower distance: " << extrema1.LowerDistance();

qDebug() << "extream2:";
qDebug() << "nr: " << extrema2.NbExtrema();
if(extrema2.NbExtrema() > 0)
qDebug() << "lower distance: " << extrema2.LowerDistance();

qDebug() << "extream3:";
qDebug() << "nr: " << extrema3.NbExtrema();
if(extrema3.NbExtrema() > 0)
qDebug() << "lower distance: " << extrema3.LowerDistance();

qDebug() << "extream4:";
qDebug() << "nr: " << extrema4.NbExtrema();
if (extrema4.NbExtrema() > 0)
qDebug() << "lower distance: " << extrema4.LowerDistance();

output:

extream1:
nr: 1
lower distance: 10
extream2:
nr: 1
lower distance: 0
extream3:
nr: 0
extream4:
nr: 0

Mikhail Sazonov's picture

NBExtrema returns only solutions where the distance vector is perpendicular to both curves.

Probably you need to request extended solution using the method TotalLowerDistance.

Aziz GH's picture

Thank you, TotalLowerDistance is exactly what I need.
You really helped me a lot.

Mauro Mariotti's picture

Thanks.
I used TotalLowerDistance but I found two trivial bugs:
1) square of RealLast(), with gave "inf" as result in my debugger, here
Standard_Real aTotalDist2 = myTotalDist * myTotalDist;
(line 390 in GeomAPI_ExtremaCurveCurve.cxx, version 7.7.0)
2) missing update of aTotalDist2 here
if(aTotalDist2 > d11) {
myTotalDist = sqrt (d11);
and in the three following "if" blocks (lines 392,403,414,425).

I attach my correction.
I can insert the bug if you tell me how to save two Geom_Curve's in a brep file. For shapes I use BRepTools::Write.

Regards.
Mauro