Salome HOME
Set the orientation of the Distance constraint
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintDistance.cpp
index 901053c6d7a90d70d09cefaad4169ce0efea0f39..4aa15648da14c82c36c8e290a0d591224c0b5f60 100644 (file)
 #include <SketchSolver_Error.h>
 #include <SketchSolver_Manager.h>
 
+#include <PlaneGCSSolver_EdgeWrapper.h>
+#include <PlaneGCSSolver_PointWrapper.h>
+#include <PlaneGCSSolver_Storage.h>
+#include <PlaneGCSSolver_Tools.h>
+
 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
 #include <SketchPlugin_ConstraintDistanceVertical.h>
 
@@ -60,53 +65,18 @@ void SketchSolver_ConstraintDistance::getAttributes(
 
 void SketchSolver_ConstraintDistance::adjustConstraint()
 {
-  ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
+  if (getType() == CONSTRAINT_PT_LINE_DISTANCE) {
+    bool isSigned = myBaseConstraint->boolean(SketchPlugin_ConstraintDistance::SIGNED())->value();
+    if (myIsSigned == isSigned)
+      return; // distance type is not changed => nothing to adjust
 
-  // Adjust point-point distance if the points are equal
-  if (getType() == CONSTRAINT_PT_PT_DISTANCE) {
-////    AttributePtr aPt1 = myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_A());
-////    AttributePtr aPt2 = myBaseConstraint->attribute(SketchPlugin_Constraint::ENTITY_B());
-////
-////    BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
-////    std::shared_ptr<GeomAPI_Pnt2d> aPoint1 = aBuilder->point(myStorage->entity(aPt1));
-////    EntityWrapperPtr anEntity2 = myStorage->entity(aPt2);
-////    std::shared_ptr<GeomAPI_Pnt2d> aPoint2 = aBuilder->point(anEntity2);
-////
-////////    if (aPoint1->distance(aPoint2) < tolerance) {
-////////      // Change X coordinate of second point to eliminate coincidence
-////////      ParameterWrapperPtr aX = aSubs.back()->parameters().front();
-////////      aX->setValue(aX->value() + 1.0);
-////////      myStorage->update(aX);
-////////    }
-    return;
-  }
-
-  // Adjust point-line distance
-  if (fabs(myPrevValue) == fabs(aConstraint->value())) {
-    // sign of distance is not changed
-////    aConstraint->setValue(myPrevValue);
-////    myStorage->addConstraint(myBaseConstraint, aConstraint);
-    return;
+    // Adjust point-line distance by setting/removing additional constraints
+    if (isSigned)
+      addConstraintsToKeepSign();
+    else
+      removeConstraintsKeepingSign();
+    myIsSigned = isSigned;
   }
-
-////  // Adjust the sign of constraint value
-////  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
-////
-////  std::shared_ptr<GeomAPI_Lin2d> aLine;
-////  std::shared_ptr<GeomAPI_Pnt2d> aPoint;
-////  for (int i = 0; i < 2; ++i) {
-////    AttributePtr anAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i));
-////    EntityWrapperPtr anEntity = myStorage->entity(anAttr);
-////    if (anEntity->type() == ENTITY_POINT)
-////      aPoint = aBuilder->point(anEntity);
-////    else if (anEntity->type() == ENTITY_LINE)
-////      aLine = aBuilder->line(anEntity);
-////  }
-////
-////  std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
-////  std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
-////  if (aLineVec->cross(aPtLineVec) * aConstraint->value() < 0.0)
-////    aConstraint->setValue(aConstraint->value() * (-1.0));
 }
 
 void SketchSolver_ConstraintDistance::update()
@@ -116,3 +86,80 @@ void SketchSolver_ConstraintDistance::update()
 
   SketchSolver_Constraint::update();
 }
+
+void SketchSolver_ConstraintDistance::addConstraintsToKeepSign()
+{
+  std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
+      std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
+
+  ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
+  std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
+
+  // calculate projection of the point on the line and find a sign of a distance
+  EntityWrapperPtr aDistPoint, aDistLine;
+  for (int i = 0; i < 2; ++i) {
+    AttributePtr anAttr = myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i));
+    EntityWrapperPtr anEntity = myStorage->entity(anAttr);
+    if (anEntity->type() == ENTITY_POINT)
+      aDistPoint = anEntity;
+    else if (anEntity->type() == ENTITY_LINE)
+      aDistLine = anEntity;
+  }
+  std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(aDistLine);
+  std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(aDistPoint);
+
+  std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
+  std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
+  if (aLineVec->cross(aPtLineVec) >= 0.)
+    mySignValue = PI/2.0;
+  else
+    mySignValue = - PI/2.0;
+  double aDot = aPtLineVec->dot(aLineVec);
+  std::shared_ptr<GeomAPI_XY> aProjectedPnt =
+      aLine->location()->xy()->added(aLineVec->multiplied(aDot));
+
+  // create auxiliary point on the line and set auxiliary constraints
+  myOddPoint = GCSPointPtr(new GCS::Point);
+  myOddPoint->x = aStorage->createParameter();
+  myOddPoint->y = aStorage->createParameter();
+  *(myOddPoint->x) = aProjectedPnt->x();
+  *(myOddPoint->y) = aProjectedPnt->y();
+
+  PointWrapperPtr aPointWr = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aDistPoint);
+  EdgeWrapperPtr anEdgeWr = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(aDistLine);
+  std::shared_ptr<GCS::Line> aGCSLine = std::dynamic_pointer_cast<GCS::Line>(anEdgeWr->entity());
+  // point-on-line
+  GCSConstraintPtr aNewConstraint(new GCS::ConstraintPointOnLine(*myOddPoint, *aGCSLine));
+  aGCSConstraints.push_back(aNewConstraint);
+  // angle which keep orientation
+  aNewConstraint = GCSConstraintPtr(new GCS::ConstraintL2LAngle(
+      aGCSLine->p1, aGCSLine->p2, *myOddPoint, *(aPointWr->point()), &mySignValue));
+  aGCSConstraints.push_back(aNewConstraint);
+
+  aConstraint->setConstraints(aGCSConstraints);
+
+  aStorage->removeConstraint(myBaseConstraint);
+  aStorage->addConstraint(myBaseConstraint, aConstraint);
+}
+
+void SketchSolver_ConstraintDistance::removeConstraintsKeepingSign()
+{
+  std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
+      std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
+
+  ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
+  std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
+
+  aStorage->removeConstraint(myBaseConstraint);
+
+  // remove parameters related to auxiliary point
+  GCS::SET_pD aParams;
+  aParams.insert(myOddPoint->x);
+  aParams.insert(myOddPoint->y);
+  aStorage->removeParameters(aParams);
+
+  aGCSConstraints.pop_back();
+  aGCSConstraints.pop_back();
+  aConstraint->setConstraints(aGCSConstraints);
+  aStorage->addConstraint(myBaseConstraint, aConstraint);
+}