]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintDistance.cpp
Salome HOME
Issue #2824: Constraints at wrong positions when editing the sketch
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintDistance.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <SketchSolver_ConstraintDistance.h>
22 #include <SketchSolver_Error.h>
23 #include <SketchSolver_Manager.h>
24
25 #include <PlaneGCSSolver_EdgeWrapper.h>
26 #include <PlaneGCSSolver_PointWrapper.h>
27 #include <PlaneGCSSolver_Storage.h>
28 #include <PlaneGCSSolver_Tools.h>
29
30 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
31 #include <SketchPlugin_ConstraintDistanceVertical.h>
32
33 #include <GeomAPI_Dir2d.h>
34 #include <GeomAPI_Lin2d.h>
35 #include <GeomAPI_Pnt2d.h>
36 #include <GeomAPI_XY.h>
37
38 #include <math.h>
39
40
41 static void getPointAndLine(const ConstraintPtr& theConstraint, const StoragePtr& theStorage,
42                             EntityWrapperPtr& thePoint, EntityWrapperPtr& theLine)
43 {
44   for (int i = 0; i < 2; ++i) {
45     AttributePtr anAttr = theConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i));
46     EntityWrapperPtr anEntity = theStorage->entity(anAttr);
47     if (anEntity->type() == ENTITY_POINT)
48       thePoint = anEntity;
49     else if (anEntity->type() == ENTITY_LINE)
50       theLine = anEntity;
51   }
52 }
53
54 static void adjustOddPoint(const EntityWrapperPtr& theDistPoint,
55                            const EntityWrapperPtr& theDistLine,
56                            GCSPointPtr& theOddPoint)
57 {
58   if (!theOddPoint)
59     return;
60
61   std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(theDistLine);
62   std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(theDistPoint);
63
64   std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
65   std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
66
67   double aDot = aPtLineVec->dot(aLineVec);
68   std::shared_ptr<GeomAPI_XY> aProjectedPnt =
69     aLine->location()->xy()->added(aLineVec->multiplied(aDot));
70
71   *(theOddPoint->x) = aProjectedPnt->x();
72   *(theOddPoint->y) = aProjectedPnt->y();
73 }
74
75
76 void SketchSolver_ConstraintDistance::getAttributes(
77     EntityWrapperPtr& theValue,
78     std::vector<EntityWrapperPtr>& theAttributes)
79 {
80   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
81   if (!myErrorMsg.empty() || !theAttributes[0]) {
82     theAttributes.clear();
83     return;
84   }
85
86   if (theAttributes[1]) {
87     if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
88       myType = CONSTRAINT_HORIZONTAL_DISTANCE;
89     else if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
90       myType = CONSTRAINT_VERTICAL_DISTANCE;
91     else
92       myType = CONSTRAINT_PT_PT_DISTANCE;
93   } else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
94     myType = CONSTRAINT_PT_LINE_DISTANCE;
95   else
96     theAttributes.clear();
97
98   myPrevValue = 0.0;
99 }
100
101 void SketchSolver_ConstraintDistance::adjustConstraint()
102 {
103   if (getType() == CONSTRAINT_PT_LINE_DISTANCE) {
104     bool isSigned = myBaseConstraint->boolean(SketchPlugin_ConstraintDistance::SIGNED())->value();
105     if (myIsSigned == isSigned) {
106       // adjust auxiliary point for sign-keeping
107       if (isSigned) {
108         EntityWrapperPtr aDistPoint, aDistLine;
109         getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
110         adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
111       }
112     }
113     else {
114       // Adjust point-line distance by setting/removing additional constraints
115       if (isSigned)
116         addConstraintsToKeepSign();
117       else
118         removeConstraintsKeepingSign();
119     }
120     myIsSigned = isSigned;
121   }
122 }
123
124 void SketchSolver_ConstraintDistance::update()
125 {
126   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
127   myPrevValue = aConstraint->value();
128
129   SketchSolver_Constraint::update();
130 }
131
132 bool SketchSolver_ConstraintDistance::remove()
133 {
134   removeConstraintsKeepingSign();
135   return SketchSolver_Constraint::remove();
136 }
137
138 void SketchSolver_ConstraintDistance::addConstraintsToKeepSign()
139 {
140   std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
141       std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
142
143   ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
144   std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
145
146   // calculate projection of the point on the line and find a sign of a distance
147   EntityWrapperPtr aDistPoint, aDistLine;
148   getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
149
150   std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(aDistLine);
151   std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(aDistPoint);
152
153   std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
154   std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
155   if (aLineVec->cross(aPtLineVec) >= 0.)
156     mySignValue = PI/2.0;
157   else
158     mySignValue = - PI/2.0;
159
160   // create auxiliary point on the line and set auxiliary constraints
161   myOddPoint = GCSPointPtr(new GCS::Point);
162   myOddPoint->x = aStorage->createParameter();
163   myOddPoint->y = aStorage->createParameter();
164   adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
165
166   PointWrapperPtr aPointWr = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aDistPoint);
167   EdgeWrapperPtr anEdgeWr = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(aDistLine);
168   std::shared_ptr<GCS::Line> aGCSLine = std::dynamic_pointer_cast<GCS::Line>(anEdgeWr->entity());
169   // point-on-line
170   GCSConstraintPtr aNewConstraint(new GCS::ConstraintPointOnLine(*myOddPoint, *aGCSLine));
171   aGCSConstraints.push_back(aNewConstraint);
172   // angle which keep orientation
173   aNewConstraint = GCSConstraintPtr(new GCS::ConstraintL2LAngle(
174       aGCSLine->p1, aGCSLine->p2, *myOddPoint, *(aPointWr->point()), &mySignValue));
175   aGCSConstraints.push_back(aNewConstraint);
176
177   aConstraint->setConstraints(aGCSConstraints);
178
179   aStorage->removeConstraint(myBaseConstraint);
180   aStorage->addConstraint(myBaseConstraint, aConstraint);
181 }
182
183 void SketchSolver_ConstraintDistance::removeConstraintsKeepingSign()
184 {
185   if (!myOddPoint)
186     return; // no sign kept => nothing to remove
187
188   std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
189       std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
190
191   ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
192   std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
193
194   aStorage->removeConstraint(myBaseConstraint);
195
196   // remove parameters related to auxiliary point
197   GCS::SET_pD aParams;
198   aParams.insert(myOddPoint->x);
199   aParams.insert(myOddPoint->y);
200   aStorage->removeParameters(aParams);
201
202   // remove constraints keeping sign of point-line distance,
203   // not more than 2 additional constraints is possible
204   if (!aGCSConstraints.empty())
205     aGCSConstraints.pop_back();
206   if (!aGCSConstraints.empty())
207     aGCSConstraints.pop_back();
208   aConstraint->setConstraints(aGCSConstraints);
209   aStorage->addConstraint(myBaseConstraint, aConstraint);
210
211   myIsSigned = false;
212 }