Salome HOME
Update coincidence constraints
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMultiRotation.cpp
index 0ceaee69066ce7e0f2c335518a61398c8b8fbafa..d93983f721985c2cb98e7f9d57a0bedb1d8f1f4a 100644 (file)
@@ -30,8 +30,8 @@ static double squareDistance(
 
 void SketchSolver_ConstraintMultiRotation::getAttributes(
     Slvs_hEntity& theCenter, double& theAngle,
-    std::vector<std::vector<Slvs_hEntity> >& thePoints,
-    std::vector<std::vector<Slvs_hEntity> >& theCircular)
+    std::vector< std::vector<Slvs_hEntity> >& thePoints,
+    std::vector< std::vector<Slvs_hEntity> >& theEntities)
 {
   DataPtr aData = myBaseConstraint->data();
   theAngle = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
@@ -65,20 +65,22 @@ void SketchSolver_ConstraintMultiRotation::getAttributes(
   // Also all circles and arc collected too, because they will be constrained by equal radii.
   FeaturePtr aFeature;
   ResultConstructionPtr aRC;
-  std::vector<Slvs_hEntity> aPoints[2]; // lists of points of features
-  std::vector<Slvs_hEntity> aCircs;     // list of circular objects
+  static const size_t MAX_POINTS = 3;
+  std::vector<Slvs_hEntity> aPoints[MAX_POINTS]; // lists of points of features
+  std::vector<Slvs_hEntity> anEntities;
   std::list<ObjectPtr> anObjectList = aRefList->list();
   std::list<ObjectPtr>::iterator anObjectIter = anObjectList.begin();
   while (anObjectIter != anObjectList.end()) {
-    aPoints[0].clear();
-    aPoints[1].clear();
-    aCircs.clear();
+    for (size_t i = 0; i < MAX_POINTS; ++i)
+      aPoints[i].clear();
+    anEntities.clear();
 
     for (size_t i = 0; i <= myNumberOfCopies && anObjectIter != anObjectList.end(); i++, anObjectIter++) {
       aFeature = ModelAPI_Feature::feature(*anObjectIter);
       if (!aFeature)
         continue;
       anEntityID = changeEntity(aFeature, aType);
+      anEntities.push_back(anEntityID);
       Slvs_Entity anEntity = myStorage->getEntity(anEntityID);
       switch (aType) {
       case SLVS_E_POINT_IN_2D:
@@ -91,12 +93,11 @@ void SketchSolver_ConstraintMultiRotation::getAttributes(
         break;
       case SLVS_E_CIRCLE:
         aPoints[0].push_back(anEntity.point[0]); // center of circle
-        aCircs.push_back(anEntityID);
         break;
       case SLVS_E_ARC_OF_CIRCLE:
-        aPoints[0].push_back(anEntity.point[1]); // start point of arc
-        aPoints[1].push_back(anEntity.point[2]); // end point of arc
-        aCircs.push_back(anEntityID);
+        aPoints[0].push_back(anEntity.point[0]); // center of arc
+        aPoints[1].push_back(anEntity.point[1]); // start point of arc
+        aPoints[2].push_back(anEntity.point[2]); // end point of arc
         break;
       default:
         myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
@@ -104,12 +105,11 @@ void SketchSolver_ConstraintMultiRotation::getAttributes(
       }
     }
 
-    if (!aPoints[0].empty())
-      thePoints.push_back(aPoints[0]);
-    if (!aPoints[1].empty())
-      thePoints.push_back(aPoints[1]);
-    if (!aCircs.empty())
-      theCircular.push_back(aCircs);
+    for (size_t i = 0; i < MAX_POINTS; ++i)
+      if (!aPoints[i].empty())
+        thePoints.push_back(aPoints[i]);
+    if (!anEntities.empty())
+      theEntities.push_back(anEntities);
   }
 }
 
@@ -123,67 +123,13 @@ void SketchSolver_ConstraintMultiRotation::process()
   if (!mySlvsConstraints.empty()) // some data is changed, update constraint
     update(myBaseConstraint);
 
-  Slvs_hEntity aCenter;
-  std::vector<std::vector<Slvs_hEntity> > aPointsAndCopies;
-  std::vector<std::vector<Slvs_hEntity> > aCircsAndCopies;
-  getAttributes(aCenter, myAngle, aPointsAndCopies, aCircsAndCopies);
+  std::vector<std::vector<Slvs_hEntity> > anEntitiesAndCopies;
+  getAttributes(myRotationCenter, myAngle, myPointsAndCopies, anEntitiesAndCopies);
   if (!myErrorMsg.empty())
     return;
 
-  myAuxLines.clear();
-
-  // Create lines between neighbor rotated points and make angle between them equal to anAngle.
-  // Also these lines should have equal lengths.
-  Slvs_Constraint aConstraint;
-  myRotationCenter = aCenter;
-  Slvs_Entity aPrevLine;
-  std::vector<std::vector<Slvs_hEntity> >::iterator aCopyIter = aPointsAndCopies.begin();
-  for (; aCopyIter != aPointsAndCopies.end(); aCopyIter++) {
-    size_t aSize = aCopyIter->size();
-    if (aSize <= 1) continue;
-
-    aPrevLine = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, myGroup->getId(),
-          myGroup->getWorkplaneId(), aCenter, (*aCopyIter)[0]);
-    aPrevLine.h = myStorage->addEntity(aPrevLine);
-    std::vector<Slvs_hEntity> anEqualLines(1, aPrevLine.h);
-    for (size_t i = 1; i < aSize; i++) {
-      Slvs_Entity aLine = Slvs_MakeLineSegment(SLVS_E_UNKNOWN, myGroup->getId(),
-          myGroup->getWorkplaneId(), aCenter, (*aCopyIter)[i]);
-      aLine.h = myStorage->addEntity(aLine);
-      // Equal length constraint
-      aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
-          SLVS_C_EQUAL_LENGTH_LINES, myGroup->getWorkplaneId(), 0.0,
-          SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aPrevLine.h, aLine.h);
-      aConstraint.h = myStorage->addConstraint(aConstraint);
-      mySlvsConstraints.push_back(aConstraint.h);
-      // Angle constraint
-      aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
-          SLVS_C_ANGLE, myGroup->getWorkplaneId(), fabs(myAngle), SLVS_E_UNKNOWN, SLVS_E_UNKNOWN,
-          aPrevLine.h, aLine.h);
-      if (myAngle < 0.0) // clockwise rotation
-        aConstraint.other = true;
-      aConstraint.h = myStorage->addConstraint(aConstraint);
-      mySlvsConstraints.push_back(aConstraint.h);
-
-      aPrevLine = aLine;
-      anEqualLines.push_back(aPrevLine.h);
-    }
-
-    myAuxLines.push_back(anEqualLines);
-  }
-  // Equal radii constraints
-  for (aCopyIter = aCircsAndCopies.begin(); aCopyIter != aCircsAndCopies.end(); aCopyIter++) {
-    size_t aSize = aCopyIter->size();
-    for (size_t i = 0; i < aSize - 1; i++) {
-      aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
-          SLVS_C_EQUAL_RADIUS, myGroup->getWorkplaneId(), 0.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN,
-          (*aCopyIter)[i], (*aCopyIter)[i+1]);
-      aConstraint.h = myStorage->addConstraint(aConstraint);
-      mySlvsConstraints.push_back(aConstraint.h);
-    }
-  }
-
   // Set the rotation center unchanged during constraint recalculation
+  Slvs_Constraint aConstraint;
   if (!myStorage->isPointFixed(myRotationCenter, aConstraint.h, true)) {
     aConstraint = Slvs_MakeConstraint(
         SLVS_E_UNKNOWN, myGroup->getId(), SLVS_C_WHERE_DRAGGED, myGroup->getWorkplaneId(), 0.0,
@@ -192,6 +138,39 @@ void SketchSolver_ConstraintMultiRotation::process()
     mySlvsConstraints.push_back(aConstraint.h);
   }
 
+  // Keep all objects unchanged (only initial object may be changed by user)
+  myCircsAndCopies.clear();
+  std::vector<std::vector<Slvs_hEntity> >::const_iterator anEntIt = anEntitiesAndCopies.begin();
+  std::vector<Slvs_hEntity>::const_iterator aCpIt;
+  for (; anEntIt != anEntitiesAndCopies.end(); ++anEntIt) {
+    std::vector<Slvs_hEntity> aCircs;
+    aCpIt = anEntIt->begin();
+    // Obtain initial points
+    Slvs_Entity anInitial = myStorage->getEntity(*aCpIt);
+    if (anInitial.type == SLVS_E_POINT_IN_2D || anInitial.type == SLVS_E_POINT_IN_3D)
+      myInitialPoints.insert(anInitial.h);
+    else {
+      for (int i = 0; i < 4 && anInitial.point[i] != SLVS_E_UNKNOWN; i++)
+        myInitialPoints.insert(anInitial.point[i]);
+    }
+
+    // Fix the copies
+    for (++aCpIt; aCpIt != anEntIt->end(); ++aCpIt) {
+      const Slvs_Entity& anEntity = myStorage->getEntity(*aCpIt);
+      std::vector<Slvs_hConstraint> aNewConstr;
+      if (anEntity.type == SLVS_E_CIRCLE) {
+        aCircs.push_back(anEntity.distance);
+        // for circles we fix only center
+        aNewConstr = myStorage->fixEntity(anEntity.point[0]);
+      } else
+        aNewConstr = myStorage->fixEntity(*aCpIt);
+      mySlvsConstraints.insert(mySlvsConstraints.end(), aNewConstr.begin(), aNewConstr.end());
+    }
+
+    if (!aCircs.empty())
+      myCircsAndCopies.push_back(aCircs);
+  }
+
   adjustConstraint();
 }
 
@@ -252,9 +231,28 @@ bool SketchSolver_ConstraintMultiRotation::remove(ConstraintPtr theConstraint)
       myFeatureMap[aFIter->first] = anEntity;
   }
 
+  // Clear list of rotated points
+  myPointsAndCopies.clear();
+  myInitialPoints.clear();
+
   return true;
 }
 
+void SketchSolver_ConstraintMultiRotation::addFeature(FeaturePtr theFeature)
+{
+  SketchSolver_Constraint::addFeature(theFeature);
+
+  std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt = myFeatureMap.find(theFeature);
+  if (aFeatIt == myFeatureMap.end())
+    return;
+
+  // store list of points of the feature
+  const Slvs_Entity& theEntity = myStorage->getEntity(aFeatIt->second);
+  for (int i = 0; i < 4; i++)
+    if (theEntity.point[i] != SLVS_E_UNKNOWN)
+      myPointsJustUpdated.insert(theEntity.point[i]);
+}
+
 void SketchSolver_ConstraintMultiRotation::adjustConstraint()
 {
   if (abs(myAngle) < tolerance) {
@@ -262,63 +260,25 @@ void SketchSolver_ConstraintMultiRotation::adjustConstraint()
     return;
   }
 
-  // Check the lengths of auxiliary lines are zero.
-  // If they become zero, remove corresponding Angle constraints.
-  // It they become non-zero (but were zero recently), add Angle constraint.
-  std::vector<Slvs_hConstraint>::iterator aConstr = mySlvsConstraints.begin();
-  std::map<Slvs_hEntity, Slvs_hEntity> anEqualLines;
-  bool isFirstRemoved = false;
-  for (; aConstr != mySlvsConstraints.end();
-       isFirstRemoved ? aConstr = mySlvsConstraints.begin() : ++aConstr) {
-    isFirstRemoved = false;
-    Slvs_Constraint aConstraint = myStorage->getConstraint(*aConstr);
-    if (aConstraint.type == SLVS_C_ANGLE || aConstraint.type == SLVS_C_EQUAL_LENGTH_LINES) {
-      Slvs_Entity aLine = myStorage->getEntity(aConstraint.entityA);
-      // Line length became zero => remove constraint
-      if (squareDistance(myStorage, aLine.point[0], aLine.point[1]) < tolerance * tolerance) {
-        myStorage->removeConstraint(aConstraint.h);
-        isFirstRemoved = aConstr == mySlvsConstraints.begin();
-        std::vector<Slvs_hConstraint>::iterator aTmpIter = aConstr;
-        if (!isFirstRemoved)
-          --aConstr;
-        mySlvsConstraints.erase(aTmpIter);
+  std::list<Slvs_Constraint> aCoincident = myStorage->getConstraintsByType(SLVS_C_POINTS_COINCIDENT);
+  std::list<Slvs_Constraint>::const_iterator aCoIt;
+
+  // Check overconstrained on rotation center (if it is coincident with other fixed point)
+  Slvs_hConstraint aFixedCenter;
+  if (myStorage->isPointFixed(myRotationCenter, aFixedCenter, false)) {
+    Slvs_hConstraint aFixed;
+    for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt)
+      if ((aCoIt->ptA == myRotationCenter && myStorage->isPointFixed(aCoIt->ptB, aFixed, true)) ||
+          (aCoIt->ptB == myRotationCenter && myStorage->isPointFixed(aCoIt->ptA, aFixed, true))) {
+        // Un-fix the center
+        myStorage->removeConstraint(aFixedCenter);
+        std::vector<Slvs_hConstraint>::iterator aSCIt = mySlvsConstraints.begin();
+        for (; aSCIt != mySlvsConstraints.end(); ++aSCIt)
+          if (*aSCIt == aFixedCenter) {
+            mySlvsConstraints.erase(aSCIt);
+            break;
+          }
       }
-      // Store the lines into the map
-      anEqualLines[aConstraint.entityB] = aConstraint.entityA;
-    }
-  }
-  // Create Angle and Equal constraints for non-degenerated lines
-  AuxLinesList::iterator anIt = myAuxLines.begin();
-  for (; anIt != myAuxLines.end(); ++anIt) {
-    if (anEqualLines.find(anIt->back()) != anEqualLines.end())
-      continue;
-
-    std::vector<Slvs_hEntity>::iterator anEqLinesIt = anIt->begin();
-    Slvs_hEntity aPrevLine = (*anEqLinesIt);
-    // Check the length of the line
-    Slvs_Entity aLine = myStorage->getEntity(aPrevLine);
-    if (squareDistance(myStorage, aLine.point[0], aLine.point[1]) < tolerance * tolerance)
-      continue;
-
-    for (++anEqLinesIt; anEqLinesIt != anIt->end(); ++anEqLinesIt) {
-      Slvs_hEntity aLine = (*anEqLinesIt);
-      // Equal length constraint
-      Slvs_Constraint aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
-          SLVS_C_EQUAL_LENGTH_LINES, myGroup->getWorkplaneId(), 0.0,
-          SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, aPrevLine, aLine);
-      aConstraint.h = myStorage->addConstraint(aConstraint);
-      mySlvsConstraints.push_back(aConstraint.h);
-      // Angle constraint
-      aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
-          SLVS_C_ANGLE, myGroup->getWorkplaneId(), fabs(myAngle), SLVS_E_UNKNOWN, SLVS_E_UNKNOWN,
-          aPrevLine, aLine);
-      if (myAngle < 0.0) // clockwise rotation
-        aConstraint.other = true;
-      aConstraint.h = myStorage->addConstraint(aConstraint);
-      mySlvsConstraints.push_back(aConstraint.h);
-
-      aPrevLine = aLine;
-    }
   }
 
   // Obtain coordinates of rotation center
@@ -330,66 +290,67 @@ void SketchSolver_ConstraintMultiRotation::adjustConstraint()
   double cosA = cos(myAngle * PI / 180.0);
   double sinA = sin(myAngle * PI / 180.0);
 
+  double aVec[2]; // coordinates of vector defining a direction from rotation center to a point
+
   // Update positions of all points to satisfy angles
-  std::list<Slvs_Constraint> aConstrAngle = myStorage->getConstraintsByType(SLVS_C_ANGLE);
-  std::list<Slvs_Constraint>::iterator anAngIt = aConstrAngle.begin();
-  std::vector<Slvs_hConstraint>::iterator aCIt;
-  Slvs_hConstraint aFixed; // temporary variable
-  double aVec[2]; // coordinates of vector defining a line
-  Slvs_Param aTarget[2]; // parameter to be changed
-  for (; anAngIt != aConstrAngle.end(); anAngIt++) {
-    for (aCIt = mySlvsConstraints.begin(); aCIt != mySlvsConstraints.end(); aCIt++)
-      if (anAngIt->h == *aCIt)
-        break;
-    if (aCIt == mySlvsConstraints.end())
-      continue;
-    Slvs_Entity aLineA = myStorage->getEntity(anAngIt->entityA);
-    Slvs_Entity aLineB = myStorage->getEntity(anAngIt->entityB);
-    if (myStorage->isPointFixed(aLineB.point[1], aFixed))
-      continue;
-    Slvs_Entity aPointA = myStorage->getEntity(aLineA.point[1]);
-    Slvs_Entity aPointB = myStorage->getEntity(aLineB.point[1]);
-    for (int i = 0; i < 2; i++) {
-      aVec[i] = myStorage->getParameter(aPointA.param[i]).val - aCenterXY[i];
-      aTarget[i] = myStorage->getParameter(aPointB.param[i]);
+  std::vector< std::vector<Slvs_hEntity> >::const_iterator aPointsIter = myPointsAndCopies.begin();
+  std::vector<Slvs_hEntity>::const_iterator aCopyIter;
+  for (; aPointsIter != myPointsAndCopies.end(); ++aPointsIter) {
+    aCopyIter = aPointsIter->begin();
+    const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
+    for (int i = 0; i < 2; i++)
+      aVec[i] = myStorage->getParameter(anInitial.param[i]).val - aCenterXY[i];
+
+    // if the point is coincident with another one which is temporary fixed (moved by user),
+    // we will update its position correspondingly
+    Slvs_hConstraint aFixed;
+    for (aCoIt = aCoincident.begin(); aCoIt != aCoincident.end(); ++aCoIt) {
+      if ((aCoIt->ptA == anInitial.h && myInitialPoints.find(aCoIt->ptB) != myInitialPoints.end()) ||
+          (aCoIt->ptB == anInitial.h && myInitialPoints.find(aCoIt->ptA) != myInitialPoints.end())) {
+        Slvs_hEntity anOtherId = aCoIt->ptA == anInitial.h ? aCoIt->ptB : aCoIt->ptA;
+        if (!myStorage->isTemporary(aFixed) &&
+            myPointsJustUpdated.find(anOtherId) == myPointsJustUpdated.end())
+          continue; // nothing to change
+
+        const Slvs_Entity& anOtherPnt = myStorage->getEntity(anOtherId);
+        for (int i = 0; i < 2; i++) {
+          Slvs_Param anInitParam = myStorage->getParameter(anInitial.param[i]);
+          const Slvs_Param& anOtherParam = myStorage->getParameter(anOtherPnt.param[i]);
+          anInitParam.val = anOtherParam.val;
+          myStorage->updateParameter(anInitParam);
+          aVec[i] = anOtherParam.val - aCenterXY[i];
+        }
+      }
     }
-    aTarget[0].val = aCenterXY[0] + aVec[0] * cosA - aVec[1] * sinA;
-    aTarget[1].val = aCenterXY[1] + aVec[0] * sinA + aVec[1] * cosA;
-    myStorage->updateParameter(aTarget[0]);
-    myStorage->updateParameter(aTarget[1]);
 
-    anAngIt->valA = myAngle;
-    myStorage->updateConstraint(*anAngIt);
+    // update copied points
+    aCopyIter = aPointsIter->begin();
+    for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
+      // rotate direction
+      double aTemp = aVec[0] * cosA - aVec[1] * sinA;
+      aVec[1] = aVec[0] * sinA + aVec[1] * cosA;
+      aVec[0] = aTemp;
+
+      const Slvs_Entity& aTarget = myStorage->getEntity(*aCopyIter);
+      for (int i = 0; i < 2; i++) {
+        Slvs_Param aParam = myStorage->getParameter(aTarget.param[i]);
+        aParam.val = aCenterXY[i] + aVec[i];
+        myStorage->updateParameter(aParam);
+      }
+    }
   }
 
-  // update positions of centers of arcs for correct radius calculation
-  std::list<Slvs_Constraint> aRadii = myStorage->getConstraintsByType(SLVS_C_EQUAL_RADIUS);
-  std::map<FeaturePtr, Slvs_hEntity>::iterator aFeatIt;
-  for (anAngIt = aRadii.begin(); anAngIt != aRadii.end(); anAngIt++) {
-    int aNbFound = 0; // number of arcs used in translation
-    for (aFeatIt = myFeatureMap.begin(); aFeatIt != myFeatureMap.end(); aFeatIt++)
-      if (aFeatIt->second == anAngIt->entityA || aFeatIt->second == anAngIt->entityB) {
-        if (aFeatIt->first->getKind() == SketchPlugin_Arc::ID())
-          aNbFound++;
-        else
-          break;
-      }
-    if (aNbFound != 2)
-      continue;
-    // two arcs were found, update their centers
-    Slvs_Entity anArcA = myStorage->getEntity(anAngIt->entityA);
-    Slvs_Entity anArcB = myStorage->getEntity(anAngIt->entityB);
-    if (myStorage->isPointFixed(anArcB.point[0], aFixed))
-      continue;
-    Slvs_Entity aCenterA = myStorage->getEntity(anArcA.point[0]);
-    Slvs_Entity aCenterB = myStorage->getEntity(anArcB.point[0]);
-    for (int i = 0; i < 2; i++) {
-      aVec[i] = myStorage->getParameter(aCenterA.param[i]).val - aCenterXY[i];
-      aTarget[i] = myStorage->getParameter(aCenterB.param[i]);
+  for (aPointsIter = myCircsAndCopies.begin(); aPointsIter != myCircsAndCopies.end(); ++aPointsIter) {
+    aCopyIter = aPointsIter->begin();
+    const Slvs_Entity& anInitial = myStorage->getEntity(*aCopyIter);
+    const Slvs_Param& anInitRad = myStorage->getParameter(anInitial.param[0]);
+    for (++aCopyIter; aCopyIter != aPointsIter->end(); ++aCopyIter) {
+      const Slvs_Entity& aCopy = myStorage->getEntity(*aCopyIter);
+      Slvs_Param aCopyRad = myStorage->getParameter(aCopy.param[0]);
+      aCopyRad.val = anInitRad.val;
+      myStorage->updateParameter(aCopyRad);
     }
-    aTarget[0].val = aCenterXY[0] + aVec[0] * cosA - aVec[1] * sinA;
-    aTarget[1].val = aCenterXY[1] + aVec[0] * sinA + aVec[1] * cosA;
-    myStorage->updateParameter(aTarget[0]);
-    myStorage->updateParameter(aTarget[1]);
   }
+
+  myPointsJustUpdated.clear();
 }