@@ -232,8 +232,8 @@ namespace
232232 class CurveCurveDistanceObjective : public math_MultipleVarFunctionWithGradient
233233 {
234234 public:
235- CurveCurveDistanceObjective (const Handle (Geom_Curve)& c1, const Handle(Geom_Curve)& c2)
236- : m_c1(c1), m_c2(c2)
235+ CurveCurveDistanceObjective (const Handle (Geom_Curve)& c1, const Handle(Geom_Curve)& c2, double scale )
236+ : m_c1(c1), m_c2(c2), m_invScaleSqr( 1 . / sqr(std::max(scale, std::numeric_limits< double >::epsilon())))
237237 {}
238238
239239 virtual Standard_Integer NbVariables () const override
@@ -310,18 +310,18 @@ namespace
310310 m_c2->D1 (v, p2, d2);
311311
312312 gp_Vec diff = p1.XYZ () - p2.XYZ ();
313- F = diff.SquareMagnitude ();
314- G (1 ) = 2 . * diff.Dot (d1) * d_getUParam (X.Value (1 ));
315- G (2 ) = -2 . * diff.Dot (d2) * d_getVParam (X.Value (2 ));
313+ F = diff.SquareMagnitude () * m_invScaleSqr ;
314+ G (1 ) = 2 . * diff.Dot (d1) * d_getUParam (X.Value (1 )) * m_invScaleSqr ;
315+ G (2 ) = -2 . * diff.Dot (d2) * d_getVParam (X.Value (2 )) * m_invScaleSqr ;
316316
317317 return true ;
318318 }
319319
320320 private:
321321 const Handle (Geom_Curve) m_c1, m_c2;
322+ const double m_invScaleSqr;
322323 };
323324
324-
325325 void CheckGradient (math_MultipleVarFunctionWithGradient& func, const math_Vector& X, double step)
326326 {
327327 int nvars = func.NbVariables ();
@@ -373,6 +373,7 @@ namespace occ_gordon_internal
373373
374374std::vector<CurveIntersectionResult> IntersectBSplines (const Handle (Geom_BSplineCurve) curve1, const Handle(Geom_BSplineCurve) curve2, double tolerance)
375375{
376+ const double optimizerScale = (BSplineAlgorithms::scale (curve1) + BSplineAlgorithms::scale (curve2)) / 2 .;
376377 auto hulls = getRangesOfIntersection (curve1, curve2, tolerance);
377378
378379 std::list<BoundingBox> curve1_ints, curve2_ints;
@@ -425,7 +426,7 @@ std::vector<CurveIntersectionResult> IntersectBSplines(const Handle(Geom_BSpline
425426 auto c1 = BSplineAlgorithms::trimCurve (curve1, boxes.b1 .range .min ,boxes.b1 .range .max );
426427 auto c2 = BSplineAlgorithms::trimCurve (curve2, boxes.b2 .range .min ,boxes.b2 .range .max );
427428
428- CurveCurveDistanceObjective obj (c1, c2);
429+ CurveCurveDistanceObjective obj (c1, c2, optimizerScale );
429430
430431 // The objective is designed such that x=[0, 0] is in the middle of the parameter space of both curves
431432 math_Vector guess (1 , 2 );
0 commit comments