]> SALOME platform Git repositories - modules/shaper.git/blobdiff - src/GeomAPI/GeomAPI_BSpline2d.cpp
Salome HOME
updated copyright message
[modules/shaper.git] / src / GeomAPI / GeomAPI_BSpline2d.cpp
index f702a278a0bf1248ceebfb56ddba1ab74a31e312..987ccd88b6053076f115978abc48e2b36b70f503 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-2020  CEA/DEN, EDF R&D
+// Copyright (C) 2019-2023  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -48,8 +48,15 @@ static Handle_Geom2d_BSplineCurve* newBSpline2d(
     return newBSpline2d(thePoles, theWeights, theDegree, thePeriodic);
 
   int anAuxPole = 0;
-  if (thePeriodic && thePoles.front()->distance(thePoles.back()) < Precision::Confusion())
-    anAuxPole = -1;
+  if (thePeriodic && thePoles.front()->distance(thePoles.back()) < Precision::Confusion()) {
+    // additionally check the number of poles is greater than needed for th periodic B-spline
+    int aNbPoles = 0;
+    std::list<int>::const_iterator it = theMults.begin();
+    for (++it; it != theMults.end(); ++it)
+      aNbPoles += *it;
+    if ((int)thePoles.size() > aNbPoles)
+      anAuxPole = -1;
+  }
 
   // collect arrays of poles, weights, knots and multiplicities
   TColgp_Array1OfPnt2d aPoles(1, (int)thePoles.size() + anAuxPole);
@@ -179,8 +186,24 @@ const bool GeomAPI_BSpline2d::parameter(const std::shared_ptr<GeomAPI_Pnt2d> the
                                         const double theTolerance,
                                         double& theParameter) const
 {
-  return GeomLib_Tool::Parameter(MY_BSPLINE, thePoint->impl<gp_Pnt2d>(),
-                                 theTolerance, theParameter) == Standard_True;
+  const gp_Pnt2d& aPoint = thePoint->impl<gp_Pnt2d>();
+  bool isOk = GeomLib_Tool::Parameter(MY_BSPLINE, aPoint,
+                                      theTolerance, theParameter) == Standard_True;
+  if (!isOk) {
+    // Sometimes OCCT's Extrema algorithm cannot find the parameter on B-spline curve
+    // (usually, if the point is near the curve extremity).
+    // Workaround: compute distance to each boundary point
+    isOk = true;
+    double aDistPS = aPoint.Distance(MY_BSPLINE->Poles().First());
+    double aDistPE = aPoint.Distance(MY_BSPLINE->Poles().Last());
+    if (aDistPS < aDistPE && aDistPS < theTolerance)
+      theParameter = MY_BSPLINE->Knots().First();
+    else if (aDistPE < aDistPS && aDistPE < theTolerance)
+      theParameter = MY_BSPLINE->Knots().Last();
+    else
+      isOk = false;
+  }
+  return isOk;
 }
 
 void GeomAPI_BSpline2d::D0(const double theU, std::shared_ptr<GeomAPI_Pnt2d>& thePoint)