Salome HOME
Fix compilation error on Linux. Part V.
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintDistance.cpp
1 #include <SketchSolver_ConstraintDistance.h>
2 #include <SketchSolver_Error.h>
3 #include <SketchSolver_Manager.h>
4
5 #include <GeomAPI_Dir2d.h>
6 #include <GeomAPI_Lin2d.h>
7 #include <GeomAPI_Pnt2d.h>
8 #include <GeomAPI_XY.h>
9
10 #include <math.h>
11
12
13 void SketchSolver_ConstraintDistance::getAttributes(
14     double& theValue,
15     std::vector<EntityWrapperPtr>& theAttributes)
16 {
17   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
18   if (!myErrorMsg.empty() || !theAttributes[0]) {
19     theAttributes.clear();
20     return;
21   }
22
23   if (theAttributes[1])
24     myType = CONSTRAINT_PT_PT_DISTANCE;
25   else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
26     myType = CONSTRAINT_PT_LINE_DISTANCE;
27   else
28     theAttributes.clear();
29
30   myPrevValue = 0.0;
31 }
32
33 void SketchSolver_ConstraintDistance::adjustConstraint()
34 {
35   // Adjust point-line distance
36   if (getType() != CONSTRAINT_PT_LINE_DISTANCE)
37     return;
38
39   // Check the sign of distance is changed
40   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
41   if (fabs(myPrevValue) == fabs(aConstraint->value())) {
42     aConstraint->setValue(myPrevValue);
43     myStorage->addConstraint(myBaseConstraint, aConstraint);
44     return;
45   }
46
47   // Get constraint parameters and check the sign of constraint value
48   BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
49   std::shared_ptr<GeomAPI_Pnt2d> aPoint;
50   std::shared_ptr<GeomAPI_Lin2d> aLine;
51   std::list<EntityWrapperPtr> aSubs = aConstraint->entities();
52   std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
53   for (; aSIt != aSubs.end(); ++aSIt) {
54     if ((*aSIt)->type() == ENTITY_POINT)
55       aPoint = aBuilder->point(*aSIt);
56     else if ((*aSIt)->type() == ENTITY_LINE)
57       aLine = aBuilder->line(*aSIt);
58   }
59
60   std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
61   std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
62   if (aPtLineVec->cross(aLineVec) * aConstraint->value() < 0.0 || myIsNegative) {
63     aConstraint->setValue(aConstraint->value() * (-1.0));
64     myStorage->addConstraint(myBaseConstraint, aConstraint);
65     myIsNegative = true;
66   }
67 }
68
69 void SketchSolver_ConstraintDistance::update()
70 {
71   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint).front();
72   myPrevValue = aConstraint->value();
73
74   SketchSolver_Constraint::update();
75 }