]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintAngle.cpp
Salome HOME
First phase of SketchSolver refactoring
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintAngle.cpp
1 #include <SketchSolver_ConstraintAngle.h>
2 #include <SketchSolver_Manager.h>
3
4 #include <GeomAPI_Dir2d.h>
5 #include <GeomAPI_Lin2d.h>
6 #include <GeomAPI_Pnt2d.h>
7 #include <GeomAPI_XY.h>
8
9 #include <cmath>
10
11 void SketchSolver_ConstraintAngle::getAttributes(
12     double& theValue, std::vector<EntityWrapperPtr>& theAttributes)
13 {
14   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
15
16   myAngle = theValue;
17 }
18
19
20 void SketchSolver_ConstraintAngle::adjustConstraint()
21 {
22   static const double aTol = 1000. * tolerance;
23   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
24
25   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
26   if (fabs(myAngle - aConstraint->value()) < aTol)
27     return;
28   myAngle = aConstraint->value();
29
30 ////  Slvs_Constraint aConstraint = myStorage->getConstraint(mySlvsConstraints.front());
31
32   bool isFixed[2][2];
33   std::shared_ptr<GeomAPI_Pnt2d> aPoints[2][2]; // start and end points of lines
34   const std::list<EntityWrapperPtr>& aConstrLines = aConstraint->entities();
35   std::list<EntityWrapperPtr>::const_iterator aCLIt = aConstrLines.begin();
36   for (int i = 0; aCLIt != aConstrLines.end(); ++i, ++aCLIt) {
37     const std::list<EntityWrapperPtr>& aLinePoints = (*aCLIt)->subEntities();
38     std::list<EntityWrapperPtr>::const_iterator aLPIt = aLinePoints.begin();
39     for (int j = 0; aLPIt != aLinePoints.end(); ++j, ++aLPIt) {
40       isFixed[i][j] = ((*aLPIt)->group() != myGroupID);
41       aPoints[i][j] = aBuilder->point(*aLPIt);
42     }
43   }
44
45   if (isFixed[0][0] && isFixed[0][1] && isFixed[1][0] && isFixed[1][1])
46     return; // both lines are fixed => no need to update them
47
48   std::shared_ptr<GeomAPI_Lin2d> aLine[2] = {
49     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[0][0], aPoints[0][1])),
50     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[1][0], aPoints[1][1]))
51   };
52   std::shared_ptr<GeomAPI_Pnt2d> anIntersection = aLine[0]->intersect(aLine[1]);
53   if (!anIntersection)
54     return;
55   double aDist[2][2];
56   for (int i = 0; i < 2; i++) {
57     for (int j = 0; j < 2; j++) {
58       aDist[i][j] = anIntersection->distance(aPoints[i][j]);
59       if (fabs(aDist[i][j]) <= tolerance)
60         aDist[i][j] = 0.0;
61     }
62     if (aDist[i][0] > tolerance && aDist[i][1] > tolerance &&
63         aDist[i][0] + aDist[i][1] < aPoints[i][0]->distance(aPoints[i][1]) + 2.0 * tolerance) {
64       // the intersection point is an inner point of the line,
65       // we change the sign of distance till start point to calculate correct coordinates
66       // after rotation
67       aDist[i][0] *= -1.0;
68     }
69   }
70   std::shared_ptr<GeomAPI_Dir2d> aDir[2];
71   for (int i = 0; i < 2; i++)
72     if (aDist[i][1] > fabs(aDist[i][0]))
73       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
74           aPoints[i][1]->xy()->decreased(anIntersection->xy())));
75     else {
76       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
77           aPoints[i][0]->xy()->decreased(anIntersection->xy())));
78       // main direction is opposite => change signs
79       if (aDist[i][0] < 0.0) {
80         aDist[i][0] *= -1.0;
81         aDist[i][1] *= -1.0;
82       }
83     }
84
85 ////  aConstraint.other = false;
86 ////  for (int i = 0; i < 2; i++)
87 ////    if (aLine[i]->direction()->dot(aDir[i]) < 0.0)
88 ////      aConstraint.other = !aConstraint.other;
89 ////  myStorage->updateConstraint(aConstraint);
90 ////
91 ////  bool isChanged = fabs(myAngle - aConstraint.valA) > aTol;
92 ////  // myAngle should be updated even if the angle of constraint is changed too little
93 ////  myAngle = aConstraint.valA;
94 ////  if (!isChanged)
95 ////    return; // the angle was not changed, no need to recalculate positions of lines
96
97   // Recalculate positions of lines to avoid conflicting constraints
98   // while changing angle value several times
99   double cosA = cos(myAngle * PI / 180.0);
100   double sinA = sin(myAngle * PI / 180.0);
101   if (aDir[0]->cross(aDir[1]) < 0.0)
102     sinA *= -1.0;
103   int aLineToUpd = 1;
104   if (isFixed[1][0] && isFixed[1][1]) {
105     sinA *= -1.0;
106     aLineToUpd = 0;
107   }
108   double x = aDir[1-aLineToUpd]->x() * cosA - aDir[1-aLineToUpd]->y() * sinA;
109   double y = aDir[1-aLineToUpd]->x() * sinA + aDir[1-aLineToUpd]->y() * cosA;
110
111   std::shared_ptr<GeomAPI_Pnt2d> aNewPoints[2];
112   for (int i = 0; i < 2; i++) {
113     aNewPoints[i] = std::shared_ptr<GeomAPI_Pnt2d>(
114         new GeomAPI_Pnt2d(anIntersection->x() + x * aDist[aLineToUpd][i],
115                           anIntersection->y() + y * aDist[aLineToUpd][i]));
116   }
117
118   std::shared_ptr<GeomAPI_XY> aDelta;
119   if (isFixed[aLineToUpd][0] && !isFixed[aLineToUpd][1])
120     aDelta = aPoints[aLineToUpd][0]->xy()->decreased(aNewPoints[0]->xy());
121   else if (!isFixed[aLineToUpd][0] && isFixed[aLineToUpd][1])
122     aDelta = aPoints[aLineToUpd][1]->xy()->decreased(aNewPoints[1]->xy());
123   if (aDelta) {
124     for (int i = 0; i < 2; i++) {
125       aNewPoints[i]->setX(aNewPoints[i]->x() + aDelta->x());
126       aNewPoints[i]->setY(aNewPoints[i]->y() + aDelta->y());
127     }
128   }
129
130   // Update positions of points
131   std::list<EntityWrapperPtr>::const_iterator anUpdLine = aConstrLines.begin();
132   if (aLineToUpd > 0) ++anUpdLine;
133   const std::list<EntityWrapperPtr>& anUpdPoints = (*anUpdLine)->subEntities();
134   std::list<EntityWrapperPtr>::const_iterator aPIt = anUpdPoints.begin();
135   for (int i = 0; aPIt != anUpdPoints.end(); ++aPIt, ++i) {
136     double aCoord[2] = {aNewPoints[i]->x(), aNewPoints[i]->y()};
137     const std::list<ParameterWrapperPtr>& aParams = (*aPIt)->parameters();
138     std::list<ParameterWrapperPtr>::const_iterator aParIt = aParams.begin();
139     for (int j = 0; aParIt != aParams.end(); ++j, ++aParIt)
140       (*aParIt)->setValue(aCoord[j]);
141   }
142
143   aBuilder->adjustConstraint(aConstraint);
144   myStorage->addConstraint(myBaseConstraint, aConstraint);
145 }