]> SALOME platform Git repositories - modules/shaper.git/commitdiff
Salome HOME
Fix the wrong calculation of B-spline value during sketch solving (issue #17347)
authorArtem Zhidkov <Artem.Zhidkov@opencascade.com>
Thu, 19 Mar 2020 11:32:02 +0000 (14:32 +0300)
committerArtem Zhidkov <Artem.Zhidkov@opencascade.com>
Thu, 19 Mar 2020 13:06:25 +0000 (16:06 +0300)
src/GeomAPI/GeomAPI_BSpline2d.cpp
src/SketchSolver/PlaneGCSSolver/PlaneGCSSolver_GeoExtensions.cpp
src/SketchSolver/PlaneGCSSolver/PlaneGCSSolver_Tools.cpp
src/SketchSolver/PlaneGCSSolver/PlaneGCSSolver_Tools.h
src/SketchSolver/SketchSolver_ConstraintCoincidence.cpp

index f702a278a0bf1248ceebfb56ddba1ab74a31e312..5835d9707d4a06a278429c48375e45382ab0f91f 100644 (file)
@@ -179,8 +179,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)
index 480dbd58d07333c444db93468748032d4b79a8b0..f69cd48b54ebe71c270fb2df7885fe7582747948 100644 (file)
@@ -38,7 +38,32 @@ DeriVector2 BSplineImpl::Value(double u, double du, double* derivparam)
   std::shared_ptr<GeomAPI_XY> aDeriv;
   myCurve->D1(u, aValue, aDeriv);
 
-  return DeriVector2(aValue->x(), aValue->y(), aDeriv->x() * du, aDeriv->y() * du);
+  // calculate the derivative on solver's parameter
+  std::shared_ptr<GeomAPI_Pnt2d> aValueDeriv(new GeomAPI_Pnt2d(0.0, 0.0));
+  bool hasParam = false;
+  std::list<GeomPnt2dPtr> aPolesDeriv;
+  for (GCS::VEC_P::iterator anIt = poles.begin(); anIt != poles.end(); ++anIt) {
+    double x = 0.0, y = 0.0;
+    if (anIt->x == derivparam) {
+      x = 1.0;
+      hasParam = true;
+    }
+    else if (anIt->y == derivparam) {
+      y = 1.0;
+      hasParam = true;
+    }
+         aPolesDeriv.push_back(GeomPnt2dPtr(new GeomAPI_Pnt2d(x, y)));
+  }
+  if (hasParam) {
+    // use non-periodic curve, because the most of B-spline coefficients are 0,
+    // thus, it is not necessary to keep original knots and multiplicities to get correct value
+    std::shared_ptr<GeomAPI_BSpline2d> aCurveDeriv(
+        new GeomAPI_BSpline2d(degree, aPolesDeriv, myCachedWeights));
+    aCurveDeriv->D0(u, aValueDeriv);
+  }
+
+  return DeriVector2(aValue->x(), aValue->y(),
+                     aValueDeriv->x() + aDeriv->x() * du, aValueDeriv->y() + aDeriv->y() * du);
 }
 
 DeriVector2 BSplineImpl::CalculateNormal(Point &p, double* derivparam)
@@ -47,20 +72,8 @@ DeriVector2 BSplineImpl::CalculateNormal(Point &p, double* derivparam)
     rebuildCache();
 
   double u = 0.0;
-  if (!myCurve->parameter(GeomPnt2dPtr(new GeomAPI_Pnt2d(*p.x, *p.y)), 1e100, u)) {
-    // 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
-    double aDistPS = PlaneGCSSolver_Tools::distance(p, poles.front());
-    double aDistPE = PlaneGCSSolver_Tools::distance(p, poles.back());
-    static const double THE_TOLERANCE = 1.e-6;
-    if (aDistPS < aDistPE && aDistPS < THE_TOLERANCE)
-      u = *knots.front();
-    else if (aDistPE < aDistPS && aDistPE < THE_TOLERANCE)
-      u = *knots.back();
-    else
-      return DeriVector2();
-  }
+  if (!myCurve->parameter(GeomPnt2dPtr(new GeomAPI_Pnt2d(*p.x, *p.y)), 1e100, u))
+    return DeriVector2();
 
   std::shared_ptr<GeomAPI_Pnt2d> aValue;
   std::shared_ptr<GeomAPI_XY> aDeriv;
index e29d23018fba251e44f77bb27672d4e8e695456d..5cc2e86c15a36e9cd3540c4d5eba95e937ba8e48 100644 (file)
@@ -65,6 +65,7 @@
 #include <SketchPlugin_MultiTranslation.h>
 #include <SketchPlugin_Point.h>
 
+#include <GeomAPI_BSpline2d.h>
 #include <GeomAPI_Circ2d.h>
 #include <GeomAPI_Dir2d.h>
 #include <GeomAPI_Ellipse2d.h>
@@ -362,6 +363,37 @@ std::shared_ptr<GeomAPI_Ellipse2d> PlaneGCSSolver_Tools::ellipse(EntityWrapperPt
       new GeomAPI_Ellipse2d(aCenter, anAxis, anEllipse->getRadMaj(), *anEllipse->radmin));
 }
 
+std::shared_ptr<GeomAPI_BSpline2d> PlaneGCSSolver_Tools::bspline(EntityWrapperPtr theEntity)
+{
+  if (theEntity->type() != ENTITY_BSPLINE)
+    return std::shared_ptr<GeomAPI_BSpline2d>();
+
+  std::shared_ptr<PlaneGCSSolver_EdgeWrapper> anEntity =
+      std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theEntity);
+  std::shared_ptr<GCS::BSpline> aSpline =
+      std::dynamic_pointer_cast<GCS::BSpline>(anEntity->entity());
+
+  std::list<GeomPnt2dPtr> aPoles;
+  for (GCS::VEC_P::iterator anIt = aSpline->poles.begin(); anIt != aSpline->poles.end(); ++anIt)
+    aPoles.push_back(GeomPnt2dPtr(new GeomAPI_Pnt2d(*anIt->x, *anIt->y)));
+
+  std::list<double> aWeights;
+  for (GCS::VEC_pD::iterator anIt = aSpline->weights.begin();
+       anIt != aSpline->weights.end(); ++anIt)
+    aWeights.push_back(**anIt);
+
+  std::list<double> aKnots;
+  for (GCS::VEC_pD::iterator anIt = aSpline->knots.begin(); anIt != aSpline->knots.end(); ++anIt)
+    aKnots.push_back(**anIt);
+
+  std::list<int> aMultiplicities;
+  aMultiplicities.assign(aSpline->mult.begin(), aSpline->mult.end());
+
+  return std::shared_ptr<GeomAPI_BSpline2d>(
+         new GeomAPI_BSpline2d(aSpline->degree, aPoles, aWeights,
+                               aKnots, aMultiplicities, aSpline->periodic));
+}
+
 void PlaneGCSSolver_Tools::recalculateArcParameters(EntityWrapperPtr theArc)
 {
   std::shared_ptr<PlaneGCSSolver_EdgeWrapper> anEdge =
index 2604d1541c706c082520e96cc9c84d63f18b88df..aba6c18140b925d0177053a844755e78f980e55e 100644 (file)
@@ -24,6 +24,7 @@
 #include <SketchSolver_ConstraintMovement.h>
 #include <SketchPlugin_Constraint.h>
 
+class GeomAPI_BSpline2d;
 class GeomAPI_Circ2d;
 class GeomAPI_Ellipse2d;
 class GeomAPI_Lin2d;
@@ -88,6 +89,9 @@ namespace PlaneGCSSolver_Tools
   /// \brief Convert entity to ellipse
   /// \return empty pointer if the entity is not an ellipse
   std::shared_ptr<GeomAPI_Ellipse2d> ellipse(EntityWrapperPtr theEntity);
+  /// \brief Convert entity to Bs-pline
+  /// \return empty pointer if the entity is not an ellipse
+  std::shared_ptr<GeomAPI_BSpline2d> bspline(EntityWrapperPtr theEntity);
 
   /// \brief Convert entity to line
   /// \return empty pointer if the entity is not a line
index c00dcfce8d1ccd609a33d9083c1e9d74fad0d69a..cecd7a61624533df9cdb0ab44c6c49e0c9770b50 100644 (file)
@@ -24,6 +24,9 @@
 #include <PlaneGCSSolver_Tools.h>
 #include <PlaneGCSSolver_UpdateCoincidence.h>
 
+#include <GeomAPI_BSpline2d.h>
+#include <GeomAPI_Pnt2d.h>
+
 #include <GeomDataAPI_Point2D.h>
 
 #include <ModelAPI_AttributeInteger.h>
@@ -270,6 +273,11 @@ void SketchSolver_ConstraintCoincidence::getAttributes(
       std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
           std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
       myAuxValue.reset(new PlaneGCSSolver_ScalarWrapper(aStorage->createParameter()));
+      // calculate the parameter of the point on B-spline nearest to the constrained point.
+      GeomPnt2dPtr aPoint = PlaneGCSSolver_Tools::point(theAttributes[0]);
+      std::shared_ptr<GeomAPI_BSpline2d> aSpline = PlaneGCSSolver_Tools::bspline(theAttributes[2]);
+      if (aPoint && aSpline)
+        aSpline->parameter(aPoint, 1.e100, *myAuxValue->scalar());
     }
     else {
       // obtain extremity points of the coincident feature for further checking of multi-coincidence