]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintDistance.cpp
Salome HOME
Merge remote-tracking branch 'remotes/origin/Filters_Development_2'
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintDistance.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include <SketchSolver_ConstraintDistance.h>
21 #include <SketchSolver_Error.h>
22 #include <SketchSolver_Manager.h>
23
24 #include <PlaneGCSSolver_EdgeWrapper.h>
25 #include <PlaneGCSSolver_PointWrapper.h>
26 #include <PlaneGCSSolver_Storage.h>
27 #include <PlaneGCSSolver_Tools.h>
28
29 #include <SketchPlugin_ConstraintDistanceHorizontal.h>
30 #include <SketchPlugin_ConstraintDistanceVertical.h>
31
32 #include <GeomAPI_Dir2d.h>
33 #include <GeomAPI_Lin2d.h>
34 #include <GeomAPI_Pnt2d.h>
35 #include <GeomAPI_XY.h>
36
37 #include <math.h>
38
39
40 static void getPointAndLine(const ConstraintPtr& theConstraint, const StoragePtr& theStorage,
41                             EntityWrapperPtr& thePoint, EntityWrapperPtr& theLine)
42 {
43   for (int i = 0; i < 2; ++i) {
44     AttributePtr anAttr = theConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(i));
45     EntityWrapperPtr anEntity = theStorage->entity(anAttr);
46     if (anEntity->type() == ENTITY_POINT)
47       thePoint = anEntity;
48     else if (anEntity->type() == ENTITY_LINE)
49       theLine = anEntity;
50   }
51 }
52
53 static void adjustOddPoint(const EntityWrapperPtr& theDistPoint,
54                            const EntityWrapperPtr& theDistLine,
55                            GCSPointPtr& theOddPoint)
56 {
57   if (!theOddPoint)
58     return;
59
60   std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(theDistLine);
61   std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(theDistPoint);
62
63   std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
64   std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
65
66   double aDot = aPtLineVec->dot(aLineVec);
67   std::shared_ptr<GeomAPI_XY> aProjectedPnt =
68     aLine->location()->xy()->added(aLineVec->multiplied(aDot));
69
70   *(theOddPoint->x) = aProjectedPnt->x();
71   *(theOddPoint->y) = aProjectedPnt->y();
72 }
73
74
75 void SketchSolver_ConstraintDistance::getAttributes(
76     EntityWrapperPtr& theValue,
77     std::vector<EntityWrapperPtr>& theAttributes)
78 {
79   SketchSolver_Constraint::getAttributes(theValue, theAttributes);
80   if (!myErrorMsg.empty() || !theAttributes[0]) {
81     theAttributes.clear();
82     return;
83   }
84
85   if (theAttributes[1]) {
86     if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceHorizontal::ID())
87       myType = CONSTRAINT_HORIZONTAL_DISTANCE;
88     else if (myBaseConstraint->getKind() == SketchPlugin_ConstraintDistanceVertical::ID())
89       myType = CONSTRAINT_VERTICAL_DISTANCE;
90     else
91       myType = CONSTRAINT_PT_PT_DISTANCE;
92   } else if (theAttributes[2] && theAttributes[2]->type() == ENTITY_LINE)
93     myType = CONSTRAINT_PT_LINE_DISTANCE;
94   else
95     theAttributes.clear();
96
97   myPrevValue = 0.0;
98
99   myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateFeature::GROUP());
100 }
101
102 void SketchSolver_ConstraintDistance::adjustConstraint()
103 {
104   if (getType() == CONSTRAINT_PT_LINE_DISTANCE) {
105     bool isSigned = myBaseConstraint->boolean(SketchPlugin_ConstraintDistance::SIGNED())->value();
106     if (myIsSigned == isSigned) {
107       // adjust auxiliary point for sign-keeping
108       if (isSigned) {
109         EntityWrapperPtr aDistPoint, aDistLine;
110         getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
111         adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
112       }
113     }
114     else {
115       // Adjust point-line distance by setting/removing additional constraints
116       if (isSigned)
117         addConstraintsToKeepSign();
118       else
119         removeConstraintsKeepingSign();
120     }
121     myIsSigned = isSigned;
122   }
123 }
124
125 void SketchSolver_ConstraintDistance::update()
126 {
127   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
128   myPrevValue = aConstraint->value();
129
130   SketchSolver_Constraint::update();
131 }
132
133 bool SketchSolver_ConstraintDistance::remove()
134 {
135   removeConstraintsKeepingSign();
136   return SketchSolver_Constraint::remove();
137 }
138
139 void SketchSolver_ConstraintDistance::addConstraintsToKeepSign()
140 {
141   std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
142       std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
143
144   ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
145   std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
146
147   // calculate projection of the point on the line and find a sign of a distance
148   EntityWrapperPtr aDistPoint, aDistLine;
149   getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
150
151   std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(aDistLine);
152   std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(aDistPoint);
153
154   std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
155   std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
156   if (aLineVec->cross(aPtLineVec) >= 0.)
157     mySignValue = PI/2.0;
158   else
159     mySignValue = - PI/2.0;
160
161   // create auxiliary point on the line and set auxiliary constraints
162   myOddPoint = GCSPointPtr(new GCS::Point);
163   myOddPoint->x = aStorage->createParameter();
164   myOddPoint->y = aStorage->createParameter();
165   adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
166
167   PointWrapperPtr aPointWr = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aDistPoint);
168   EdgeWrapperPtr anEdgeWr = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(aDistLine);
169   std::shared_ptr<GCS::Line> aGCSLine = std::dynamic_pointer_cast<GCS::Line>(anEdgeWr->entity());
170   // point-on-line
171   GCSConstraintPtr aNewConstraint(new GCS::ConstraintPointOnLine(*myOddPoint, *aGCSLine));
172   aGCSConstraints.push_back(aNewConstraint);
173   // angle which keep orientation
174   aNewConstraint = GCSConstraintPtr(new GCS::ConstraintL2LAngle(
175       aGCSLine->p1, aGCSLine->p2, *myOddPoint, *(aPointWr->point()), &mySignValue));
176   aGCSConstraints.push_back(aNewConstraint);
177
178   aConstraint->setConstraints(aGCSConstraints);
179
180   aStorage->removeConstraint(myBaseConstraint);
181   aStorage->addConstraint(myBaseConstraint, aConstraint);
182 }
183
184 void SketchSolver_ConstraintDistance::removeConstraintsKeepingSign()
185 {
186   if (!myOddPoint)
187     return; // no sign kept => nothing to remove
188
189   std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
190       std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
191
192   ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
193   std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
194
195   aStorage->removeConstraint(myBaseConstraint);
196
197   // remove parameters related to auxiliary point
198   GCS::SET_pD aParams;
199   aParams.insert(myOddPoint->x);
200   aParams.insert(myOddPoint->y);
201   aStorage->removeParameters(aParams);
202
203   // remove constraints keeping sign of point-line distance,
204   // not more than 2 additional constraints is possible
205   if (!aGCSConstraints.empty())
206     aGCSConstraints.pop_back();
207   if (!aGCSConstraints.empty())
208     aGCSConstraints.pop_back();
209   aConstraint->setConstraints(aGCSConstraints);
210   aStorage->addConstraint(myBaseConstraint, aConstraint);
211
212   myIsSigned = false;
213 }
214
215 void SketchSolver_ConstraintDistance::notify(const FeaturePtr& theFeature,
216                                              PlaneGCSSolver_Update*)
217 {
218   if (getType() == CONSTRAINT_PT_LINE_DISTANCE && myIsSigned &&
219       theFeature->getKind() == SketchPlugin_Sketch::ID()) {
220     // the sketch plane was updated, recalculate auxiliary constraints
221     removeConstraintsKeepingSign();
222     addConstraintsToKeepSign();
223     myIsSigned = true; // reset it, due to changing by removeConstraintsKeepingSign()
224   }
225 }