Salome HOME
Task 2.12. New entities: ellipses and arcs of ellipses (issue #3003)
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_Solver.cpp
index 75919af5f55a50f941f15f9e7d1ad41bf7253261..416b338b05d4dadbedc3a8da7686886c34ece178 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017  CEA/DEN, EDF R&D
+// Copyright (C) 2014-2019  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
 //
 // You should have received a copy of the GNU Lesser General Public
 // License along with this library; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
-// See http://www.salome-platform.org/ or
-// email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 
 #include <PlaneGCSSolver_Solver.h>
 #include <Events_LongOp.h>
 
+// Multiplier to correlate IDs of SketchPlugin constraint and primitive PlaneGCS constraints
+static const int THE_CONSTRAINT_MULT = 100;
+
 
 PlaneGCSSolver_Solver::PlaneGCSSolver_Solver()
   : myEquationSystem(new GCS::System),
@@ -48,26 +50,46 @@ void PlaneGCSSolver_Solver::clear()
   removeFictiveConstraint();
 }
 
-void PlaneGCSSolver_Solver::addConstraint(GCSConstraintPtr theConstraint)
+void PlaneGCSSolver_Solver::addConstraint(const ConstraintID& theMultiConstraintID,
+                                          const std::list<GCSConstraintPtr>& theConstraints)
 {
-  myEquationSystem->addConstraint(theConstraint.get());
-  myConstraints[theConstraint->getTag()].insert(theConstraint);
-  if (theConstraint->getTag() >= 0)
+  int anID = theMultiConstraintID > CID_UNKNOWN ?
+             theMultiConstraintID * THE_CONSTRAINT_MULT :
+             theMultiConstraintID;
+
+  for (std::list<GCSConstraintPtr>::const_iterator anIt = theConstraints.begin();
+       anIt != theConstraints.end(); ++anIt) {
+    GCSConstraintPtr aConstraint = *anIt;
+    aConstraint->setTag(anID);
+    myEquationSystem->addConstraint(aConstraint.get());
+    myConstraints[theMultiConstraintID].insert(aConstraint);
+
+    if (anID > CID_UNKNOWN)
+      ++anID;
+  }
+
+  if (theMultiConstraintID >= CID_UNKNOWN)
     myDOF = -1;
   myInitilized = false;
 }
 
-void PlaneGCSSolver_Solver::removeConstraint(ConstraintID theID)
+void PlaneGCSSolver_Solver::removeConstraint(const ConstraintID& theID)
 {
-  myConstraints.erase(theID);
+  ConstraintMap::iterator aFound = myConstraints.find(theID);
+  if (aFound != myConstraints.end()) {
+    for (std::set<GCSConstraintPtr>::iterator anIt = aFound->second.begin();
+         anIt != aFound->second.end(); ++anIt)
+      myEquationSystem->clearByTag((*anIt)->getTag());
+
+    myConstraints.erase(aFound);
+  }
+
   if (myConstraints.empty()) {
     myEquationSystem->clear();
     myDOF = (int)myParameters.size();
-  } else {
-    myEquationSystem->clearByTag(theID);
-    if (theID >= 0)
-      myDOF = -1;
-  }
+  } else if (theID >= CID_UNKNOWN)
+    myDOF = -1;
+
   myInitilized = false;
 }
 
@@ -131,7 +153,7 @@ PlaneGCSSolver_Solver::SolveStatus PlaneGCSSolver_Solver::solve()
   }
 
   if (myParameters.empty())
-    return STATUS_INCONSISTENT;
+    return myConstraints.empty() ? STATUS_OK : STATUS_INCONSISTENT;
 
   GCS::SolveStatus aResult = GCS::Success;
   Events_LongOp::start(this);
@@ -139,11 +161,20 @@ PlaneGCSSolver_Solver::SolveStatus PlaneGCSSolver_Solver::solve()
     aResult = (GCS::SolveStatus)myEquationSystem->solve();
   } else {
     addFictiveConstraintIfNecessary();
-
-    if (myDiagnoseBeforeSolve)
-      diagnose();
+    diagnose();
     aResult = (GCS::SolveStatus)myEquationSystem->solve(myParameters);
   }
+
+  if (aResult == GCS::Failed) {
+    // DogLeg solver failed without conflicting constraints, try to use Levenberg-Marquardt solver
+    diagnose(GCS::LevenbergMarquardt);
+    aResult = (GCS::SolveStatus)myEquationSystem->solve(myParameters, true,
+                                                        GCS::LevenbergMarquardt);
+    if (aResult == GCS::Failed) {
+      diagnose(GCS::BFGS);
+      aResult = (GCS::SolveStatus)myEquationSystem->solve(myParameters, true, GCS::BFGS);
+    }
+  }
   Events_LongOp::end(this);
 
   // collect information about conflicting constraints every time,
@@ -152,30 +183,6 @@ PlaneGCSSolver_Solver::SolveStatus PlaneGCSSolver_Solver::solve()
   collectConflicting();
   if (!myConflictingIDs.empty())
     aResult = GCS::Failed;
-  else if (aResult == GCS::Failed) {
-    // DogLeg solver failed without conflicting constraints, try to use Levenberg-Marquardt solver
-    // if there are point-line distance constraints
-    ConstraintMap::iterator aCIt = myConstraints.begin();
-    for (; aCIt != myConstraints.end(); ++aCIt) {
-      if (aCIt->second.size() <= 1)
-        continue;
-      std::set<GCSConstraintPtr>::const_iterator anIt = aCIt->second.begin();
-      for (; anIt != aCIt->second.end(); ++anIt)
-        if ((*anIt)->getTypeId() == GCS::P2LDistance)
-          break;
-      if (anIt != aCIt->second.end())
-        break;
-    }
-
-    if (aCIt != myConstraints.end()) {
-      aResult = (GCS::SolveStatus)myEquationSystem->solve(
-          myParameters, true, GCS::LevenbergMarquardt);
-      myConfCollected = false;
-      collectConflicting();
-      if (!myConflictingIDs.empty())
-        aResult = GCS::Failed;
-    }
-  }
 
   SolveStatus aStatus;
   if (aResult == GCS::Failed)
@@ -204,14 +211,20 @@ bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) con
   return myConflictingIDs.find((int)theConstraint) != myConflictingIDs.end();
 }
 
-void PlaneGCSSolver_Solver::collectConflicting()
+void PlaneGCSSolver_Solver::collectConflicting(bool withRedundant)
 {
   GCS::VEC_I aConflict;
   myEquationSystem->getConflicting(aConflict);
-  myConflictingIDs.insert(aConflict.begin(), aConflict.end());
-
-  myEquationSystem->getRedundant(aConflict);
-  myConflictingIDs.insert(aConflict.begin(), aConflict.end());
+  // convert PlaneGCS constraint IDs to SketchPlugin's ID
+  for (GCS::VEC_I::const_iterator anIt = aConflict.begin(); anIt != aConflict.end(); ++anIt)
+    myConflictingIDs.insert((*anIt) / THE_CONSTRAINT_MULT);
+
+  if (withRedundant) {
+    myEquationSystem->getRedundant(aConflict);
+    // convert PlaneGCS constraint IDs to SketchPlugin's ID
+    for (GCS::VEC_I::const_iterator anIt = aConflict.begin(); anIt != aConflict.end(); ++anIt)
+      myConflictingIDs.insert((*anIt) / THE_CONSTRAINT_MULT);
+  }
 
   myConfCollected = true;
 }
@@ -223,39 +236,47 @@ int PlaneGCSSolver_Solver::dof()
   return myDOF;
 }
 
-void PlaneGCSSolver_Solver::diagnose()
+void PlaneGCSSolver_Solver::diagnose(const GCS::Algorithm& theAlgo)
 {
   myEquationSystem->declareUnknowns(myParameters);
-  myDOF = myEquationSystem->diagnose();
+  myDOF = myEquationSystem->diagnose(theAlgo);
   myDiagnoseBeforeSolve = false;
 }
 
 void PlaneGCSSolver_Solver::addFictiveConstraintIfNecessary()
 {
-  if (!myConstraints.empty() &&
-      myConstraints.find(CID_MOVEMENT) == myConstraints.end())
-    return;
+  bool hasOnlyMovement = true;
+  for (ConstraintMap::iterator anIt = myConstraints.begin();
+       anIt != myConstraints.end() && hasOnlyMovement; ++anIt)
+    hasOnlyMovement = anIt->first == CID_MOVEMENT;
+  if (!hasOnlyMovement)
+    return; // regular constraints are available too
 
   if (myFictiveConstraint)
     return; // no need several fictive constraints
 
+  int aDOF = myDOF;
   double* aParam = createParameter();
   double* aFictiveParameter = new double(0.0);
 
   myFictiveConstraint = new GCS::ConstraintEqual(aFictiveParameter, aParam);
   myFictiveConstraint->setTag(CID_FICTIVE);
   myEquationSystem->addConstraint(myFictiveConstraint);
+  // DoF should not be changed when adding fictive constraint
+  myDOF = aDOF;
 }
 
 void PlaneGCSSolver_Solver::removeFictiveConstraint()
 {
   if (myFictiveConstraint) {
-    myEquationSystem->removeConstraint(myFictiveConstraint);
+    myEquationSystem->clearByTag(myFictiveConstraint->getTag());
     myParameters.pop_back();
 
     GCS::VEC_pD aParams = myFictiveConstraint->params();
-    for (GCS::VEC_pD::iterator anIt = aParams.begin(); anIt != aParams.end(); ++ anIt)
-      delete *anIt;
+    for (GCS::VEC_pD::iterator anIt = aParams.begin(); anIt != aParams.end(); ++anIt) {
+      double* aPar = *anIt;
+      delete aPar;
+    }
     delete myFictiveConstraint;
     myFictiveConstraint = 0;
   }