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   bool isFixed[2][2];
31   std::shared_ptr<GeomAPI_Pnt2d> aPoints[2][2]; // start and end points of lines
32   const std::list<EntityWrapperPtr>& aConstrLines = aConstraint->entities();
33   std::list<EntityWrapperPtr>::const_iterator aCLIt = aConstrLines.begin();
34   for (int i = 0; aCLIt != aConstrLines.end(); ++i, ++aCLIt) {
35     const std::list<EntityWrapperPtr>& aLinePoints = (*aCLIt)->subEntities();
36     std::list<EntityWrapperPtr>::const_iterator aLPIt = aLinePoints.begin();
37     for (int j = 0; aLPIt != aLinePoints.end(); ++j, ++aLPIt) {
38       isFixed[i][j] = ((*aLPIt)->group() != myGroupID);
39       aPoints[i][j] = aBuilder->point(*aLPIt);
40     }
41   }
42
43   if (isFixed[0][0] && isFixed[0][1] && isFixed[1][0] && isFixed[1][1])
44     return; // both lines are fixed => no need to update them
45
46   std::shared_ptr<GeomAPI_Lin2d> aLine[2] = {
47     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[0][0], aPoints[0][1])),
48     std::shared_ptr<GeomAPI_Lin2d>(new GeomAPI_Lin2d(aPoints[1][0], aPoints[1][1]))
49   };
50   std::shared_ptr<GeomAPI_Pnt2d> anIntersection = aLine[0]->intersect(aLine[1]);
51   if (!anIntersection)
52     return;
53   double aDist[2][2];
54   for (int i = 0; i < 2; i++) {
55     for (int j = 0; j < 2; j++) {
56       aDist[i][j] = anIntersection->distance(aPoints[i][j]);
57       if (fabs(aDist[i][j]) <= tolerance)
58         aDist[i][j] = 0.0;
59     }
60     if (aDist[i][0] > tolerance && aDist[i][1] > tolerance &&
61         aDist[i][0] + aDist[i][1] < aPoints[i][0]->distance(aPoints[i][1]) + 2.0 * tolerance) {
62       // the intersection point is an inner point of the line,
63       // we change the sign of distance till start point to calculate correct coordinates
64       // after rotation
65       aDist[i][0] *= -1.0;
66     }
67   }
68   std::shared_ptr<GeomAPI_Dir2d> aDir[2];
69   for (int i = 0; i < 2; i++)
70     if (aDist[i][1] > fabs(aDist[i][0]))
71       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
72           aPoints[i][1]->xy()->decreased(anIntersection->xy())));
73     else {
74       aDir[i] = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
75           aPoints[i][0]->xy()->decreased(anIntersection->xy())));
76       // main direction is opposite => change signs
77       if (aDist[i][0] < 0.0) {
78         aDist[i][0] *= -1.0;
79         aDist[i][1] *= -1.0;
80       }
81     }
82
83   // Recalculate positions of lines to avoid conflicting constraints
84   // while changing angle value several times
85   double cosA = cos(myAngle * PI / 180.0);
86   double sinA = sin(myAngle * PI / 180.0);
87   if (aDir[0]->cross(aDir[1]) < 0.0)
88     sinA *= -1.0;
89   int aLineToUpd = 1;
90   if (isFixed[1][0] && isFixed[1][1]) {
91     sinA *= -1.0;
92     aLineToUpd = 0;
93   }
94   double x = aDir[1-aLineToUpd]->x() * cosA - aDir[1-aLineToUpd]->y() * sinA;
95   double y = aDir[1-aLineToUpd]->x() * sinA + aDir[1-aLineToUpd]->y() * cosA;
96
97   std::shared_ptr<GeomAPI_Pnt2d> aNewPoints[2];
98   for (int i = 0; i < 2; i++) {
99     aNewPoints[i] = std::shared_ptr<GeomAPI_Pnt2d>(
100         new GeomAPI_Pnt2d(anIntersection->x() + x * aDist[aLineToUpd][i],
101                           anIntersection->y() + y * aDist[aLineToUpd][i]));
102   }
103
104   std::shared_ptr<GeomAPI_XY> aDelta;
105   if (isFixed[aLineToUpd][0] && !isFixed[aLineToUpd][1])
106     aDelta = aPoints[aLineToUpd][0]->xy()->decreased(aNewPoints[0]->xy());
107   else if (!isFixed[aLineToUpd][0] && isFixed[aLineToUpd][1])
108     aDelta = aPoints[aLineToUpd][1]->xy()->decreased(aNewPoints[1]->xy());
109   if (aDelta) {
110     for (int i = 0; i < 2; i++) {
111       aNewPoints[i]->setX(aNewPoints[i]->x() + aDelta->x());
112       aNewPoints[i]->setY(aNewPoints[i]->y() + aDelta->y());
113     }
114   }
115
116   // Update positions of points
117   std::list<EntityWrapperPtr>::const_iterator anUpdLine = aConstrLines.begin();
118   if (aLineToUpd > 0) ++anUpdLine;
119   const std::list<EntityWrapperPtr>& anUpdPoints = (*anUpdLine)->subEntities();
120   std::list<EntityWrapperPtr>::const_iterator aPIt = anUpdPoints.begin();
121   for (int i = 0; aPIt != anUpdPoints.end(); ++aPIt, ++i) {
122     double aCoord[2] = {aNewPoints[i]->x(), aNewPoints[i]->y()};
123     const std::list<ParameterWrapperPtr>& aParams = (*aPIt)->parameters();
124     std::list<ParameterWrapperPtr>::const_iterator aParIt = aParams.begin();
125     for (int j = 0; aParIt != aParams.end(); ++j, ++aParIt)
126       (*aParIt)->setValue(aCoord[j]);
127   }
128
129   aBuilder->adjustConstraint(aConstraint);
130   myStorage->addConstraint(myBaseConstraint, aConstraint);
131 }