]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_ConstraintDistance.cpp
Salome HOME
c3828b79405be5ae32b083a47f5a975fe0b2722b
[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
100 void SketchSolver_ConstraintDistance::adjustConstraint()
101 {
102   if (getType() == CONSTRAINT_PT_LINE_DISTANCE) {
103     bool isSigned = myBaseConstraint->boolean(SketchPlugin_ConstraintDistance::SIGNED())->value();
104     if (myIsSigned == isSigned) {
105       // adjust auxiliary point for sign-keeping
106       if (isSigned) {
107         EntityWrapperPtr aDistPoint, aDistLine;
108         getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
109         adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
110       }
111     }
112     else {
113       // Adjust point-line distance by setting/removing additional constraints
114       if (isSigned)
115         addConstraintsToKeepSign();
116       else
117         removeConstraintsKeepingSign();
118     }
119     myIsSigned = isSigned;
120   }
121 }
122
123 void SketchSolver_ConstraintDistance::update()
124 {
125   ConstraintWrapperPtr aConstraint = myStorage->constraint(myBaseConstraint);
126   myPrevValue = aConstraint->value();
127
128   SketchSolver_Constraint::update();
129 }
130
131 bool SketchSolver_ConstraintDistance::remove()
132 {
133   removeConstraintsKeepingSign();
134   return SketchSolver_Constraint::remove();
135 }
136
137 void SketchSolver_ConstraintDistance::addConstraintsToKeepSign()
138 {
139   std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
140       std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
141
142   ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
143   std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
144
145   // calculate projection of the point on the line and find a sign of a distance
146   EntityWrapperPtr aDistPoint, aDistLine;
147   getPointAndLine(myBaseConstraint, myStorage, aDistPoint, aDistLine);
148
149   std::shared_ptr<GeomAPI_Lin2d> aLine = PlaneGCSSolver_Tools::line(aDistLine);
150   std::shared_ptr<GeomAPI_Pnt2d> aPoint = PlaneGCSSolver_Tools::point(aDistPoint);
151
152   std::shared_ptr<GeomAPI_XY> aLineVec = aLine->direction()->xy();
153   std::shared_ptr<GeomAPI_XY> aPtLineVec = aPoint->xy()->decreased(aLine->location()->xy());
154   if (aLineVec->cross(aPtLineVec) >= 0.)
155     mySignValue = PI/2.0;
156   else
157     mySignValue = - PI/2.0;
158
159   // create auxiliary point on the line and set auxiliary constraints
160   myOddPoint = GCSPointPtr(new GCS::Point);
161   myOddPoint->x = aStorage->createParameter();
162   myOddPoint->y = aStorage->createParameter();
163   adjustOddPoint(aDistPoint, aDistLine, myOddPoint);
164
165   PointWrapperPtr aPointWr = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(aDistPoint);
166   EdgeWrapperPtr anEdgeWr = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(aDistLine);
167   std::shared_ptr<GCS::Line> aGCSLine = std::dynamic_pointer_cast<GCS::Line>(anEdgeWr->entity());
168   // point-on-line
169   GCSConstraintPtr aNewConstraint(new GCS::ConstraintPointOnLine(*myOddPoint, *aGCSLine));
170   aGCSConstraints.push_back(aNewConstraint);
171   // angle which keep orientation
172   aNewConstraint = GCSConstraintPtr(new GCS::ConstraintL2LAngle(
173       aGCSLine->p1, aGCSLine->p2, *myOddPoint, *(aPointWr->point()), &mySignValue));
174   aGCSConstraints.push_back(aNewConstraint);
175
176   aConstraint->setConstraints(aGCSConstraints);
177
178   aStorage->removeConstraint(myBaseConstraint);
179   aStorage->addConstraint(myBaseConstraint, aConstraint);
180 }
181
182 void SketchSolver_ConstraintDistance::removeConstraintsKeepingSign()
183 {
184   if (!myOddPoint)
185     return; // no sign kept => nothing to remove
186
187   std::shared_ptr<PlaneGCSSolver_Storage> aStorage =
188       std::dynamic_pointer_cast<PlaneGCSSolver_Storage>(myStorage);
189
190   ConstraintWrapperPtr aConstraint = aStorage->constraint(myBaseConstraint);
191   std::list<GCSConstraintPtr> aGCSConstraints = aConstraint->constraints();
192
193   aStorage->removeConstraint(myBaseConstraint);
194
195   // remove parameters related to auxiliary point
196   GCS::SET_pD aParams;
197   aParams.insert(myOddPoint->x);
198   aParams.insert(myOddPoint->y);
199   aStorage->removeParameters(aParams);
200
201   // remove constraints keeping sign of point-line distance,
202   // not more than 2 additional constraints is possible
203   if (!aGCSConstraints.empty())
204     aGCSConstraints.pop_back();
205   if (!aGCSConstraints.empty())
206     aGCSConstraints.pop_back();
207   aConstraint->setConstraints(aGCSConstraints);
208   aStorage->addConstraint(myBaseConstraint, aConstraint);
209
210   myIsSigned = false;
211 }