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