Salome HOME
Copyright update 2022
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintMovement.cpp
1 // Copyright (C) 2017-2022  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_ConstraintMovement.h>
21 #include <SketchSolver_Error.h>
22 #include <SketchSolver_Manager.h>
23
24 #include <PlaneGCSSolver_EdgeWrapper.h>
25 #include <PlaneGCSSolver_PointArrayWrapper.h>
26 #include <PlaneGCSSolver_PointWrapper.h>
27
28 #include <SketchPlugin_Arc.h>
29 #include <SketchPlugin_Circle.h>
30 #include <SketchPlugin_Ellipse.h>
31 #include <SketchPlugin_EllipticArc.h>
32 #include <SketchPlugin_Line.h>
33 #include <SketchPlugin_Point.h>
34
35 #include <GeomDataAPI_Point2D.h>
36
37 #include <GeomAPI_Pnt2d.h>
38
39 #include <cmath>
40
41 static GCS::Point createGCSPoint(double* x, double* y)
42 {
43   GCS::Point aPoint;
44   aPoint.x = x;
45   aPoint.y = y;
46   return aPoint;
47 }
48
49
50 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(FeaturePtr theFeature)
51   : SketchSolver_ConstraintFixed(ConstraintPtr()),
52     myMovedFeature(theFeature),
53     mySimpleMove(true)
54 {
55 }
56
57 SketchSolver_ConstraintMovement::SketchSolver_ConstraintMovement(AttributePtr theAttribute,
58                                                                  const int thePointIndex)
59   : SketchSolver_ConstraintFixed(ConstraintPtr()),
60     myDraggedAttribute(theAttribute),
61     myDraggedPointIndex(thePointIndex),
62     mySimpleMove(true)
63 {
64   myMovedFeature = ModelAPI_Feature::feature(theAttribute->owner());
65 }
66
67 void SketchSolver_ConstraintMovement::blockEvents(bool isBlocked)
68 {
69   if (myMovedFeature)
70     myMovedFeature->data()->blockSendAttributeUpdated(isBlocked);
71 }
72
73 void SketchSolver_ConstraintMovement::process()
74 {
75   cleanErrorMsg();
76   if (!myMovedFeature || !myStorage) {
77     // Not enough parameters are initialized
78     return;
79   }
80
81   mySolverConstraint = initMovement();
82   if (!myErrorMsg.empty() || !mySolverConstraint) {
83     // Nothing to move, clear the feature to avoid changing its group
84     // after removing the Movement constraint.
85     myMovedFeature = FeaturePtr();
86     return;
87   }
88   myStorage->addMovementConstraint(mySolverConstraint);
89 }
90
91
92 static bool isSimpleMove(FeaturePtr theMovedFeature, AttributePtr theDraggedPoint)
93 {
94   bool isSimple = true;
95 #ifdef CHANGE_RADIUS_WHILE_MOVE
96   if (theMovedFeature->getKind() == SketchPlugin_Circle::ID() ||
97       theMovedFeature->getKind() == SketchPlugin_Ellipse::ID())
98     isSimple = (theDraggedPoint.get() != 0);
99   else if (theMovedFeature->getKind() == SketchPlugin_Arc::ID() ||
100            theMovedFeature->getKind() == SketchPlugin_EllipticArc::ID()) {
101     isSimple = (theDraggedPoint.get() != 0 &&
102                (theDraggedPoint->id() == SketchPlugin_Arc::CENTER_ID() ||
103                 theDraggedPoint->id() == SketchPlugin_EllipticArc::CENTER_ID()));
104   }
105 #endif
106   return isSimple;
107 }
108
109 ConstraintWrapperPtr SketchSolver_ConstraintMovement::initMovement()
110 {
111   ConstraintWrapperPtr aConstraint;
112
113   // if the feature is copy, do not move it
114   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
115       std::dynamic_pointer_cast<SketchPlugin_Feature>(myMovedFeature);
116   if (!aSketchFeature || aSketchFeature->isCopy()) {
117     myStorage->setNeedToResolve(true);
118     return aConstraint;
119   }
120
121   EntityWrapperPtr anEntity = myDraggedAttribute ? myStorage->entity(myDraggedAttribute)
122                                                  : myStorage->entity(myMovedFeature);
123   if (!anEntity) {
124     myStorage->update(myMovedFeature, true);
125     anEntity = myDraggedAttribute ? myStorage->entity(myDraggedAttribute)
126                                   : myStorage->entity(myMovedFeature);
127     if (!anEntity)
128       return aConstraint;
129   }
130
131   mySimpleMove = isSimpleMove(myMovedFeature, myDraggedAttribute);
132
133   if (mySimpleMove) {
134     if (anEntity->type() == ENTITY_POINT_ARRAY) {
135       anEntity = std::dynamic_pointer_cast<PlaneGCSSolver_PointArrayWrapper>(anEntity)
136                  ->value(myDraggedPointIndex);
137     }
138     aConstraint = fixFeature(anEntity);
139   }
140   else {
141     if (myDraggedAttribute) // start or end point of arc has been moved
142       aConstraint = fixArcExtremity(anEntity);
143     else if (anEntity->type() == ENTITY_CIRCLE || anEntity->type() == ENTITY_ARC) {
144       // arc or circle has been moved
145       aConstraint = fixPointOnCircle(anEntity);
146     }
147     else if (anEntity->type() == ENTITY_ELLIPSE || anEntity->type() == ENTITY_ELLIPTIC_ARC) {
148       // ellipse or elliptic arc has been moved
149       aConstraint = fixPointOnEllipse(anEntity);
150     }
151   }
152
153   return aConstraint;
154 }
155
156 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixArcExtremity(
157     const EntityWrapperPtr& theArcExtremity)
158 {
159   static const int nbParams = 4;
160   myFixedValues.reserve(nbParams); // moved point and center of arc
161
162   EdgeWrapperPtr aCircularEntity = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(
163       myStorage->entity(myMovedFeature));
164   std::shared_ptr<GCS::Arc> anArc =
165       std::dynamic_pointer_cast<GCS::Arc>(aCircularEntity->entity());
166   std::shared_ptr<GCS::ArcOfEllipse> anEllArc =
167       std::dynamic_pointer_cast<GCS::ArcOfEllipse>(aCircularEntity->entity());
168
169   PointWrapperPtr aPoint =
170       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theArcExtremity);
171
172   double* aParams[nbParams] = { aPoint->point()->x, aPoint->point()->y, 0, 0 };
173   if (anArc) {
174     aParams[2] = anArc->center.x;
175     aParams[3] = anArc->center.y;
176   }
177   else if (anEllArc) {
178     aParams[2] = anEllArc->center.x;
179     aParams[3] = anEllArc->center.y;
180   }
181
182   std::list<GCSConstraintPtr> aConstraints;
183   for (int i = 0; i < nbParams; ++i) {
184     myFixedValues.push_back(*aParams[i]);
185     GCSConstraintPtr aNewConstraint(new GCS::ConstraintEqual(&myFixedValues[i], aParams[i]));
186     aNewConstraint->rescale(0.01);
187     aConstraints.push_back(aNewConstraint);
188   }
189
190   return ConstraintWrapperPtr(
191       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
192 }
193
194 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnCircle(
195     const EntityWrapperPtr& theCircular)
196 {
197   static const double scale = 0.01;
198   static const int nbParams = 4;
199   myFixedValues.reserve(nbParams); // moved point and center of arc/circle
200
201   EdgeWrapperPtr aCircularEntity =
202       std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theCircular);
203   std::shared_ptr<GCS::Circle> aCircular =
204       std::dynamic_pointer_cast<GCS::Circle>(aCircularEntity->entity());
205
206   // initialize fixed values
207   myFixedValues.push_back(*aCircular->center.x + *aCircular->rad);
208   myFixedValues.push_back(*aCircular->center.y);
209   myFixedValues.push_back(*aCircular->center.x);
210   myFixedValues.push_back(*aCircular->center.y);
211
212   // create a moved point
213   GCS::Point aPointOnCircle = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
214
215   std::list<GCSConstraintPtr> aConstraints;
216   // point-on-circle
217   GCSConstraintPtr aNewConstraint(
218       new GCS::ConstraintP2PDistance(aPointOnCircle, aCircular->center, aCircular->rad));
219   aNewConstraint->rescale(scale);
220   aConstraints.push_back(aNewConstraint);
221   // fixed center (x)
222   aNewConstraint = GCSConstraintPtr(
223       new GCS::ConstraintEqual(&myFixedValues[2], aCircular->center.x));
224   aNewConstraint->rescale(scale);
225   aConstraints.push_back(aNewConstraint);
226   // fixed center (y)
227   aNewConstraint = GCSConstraintPtr(
228       new GCS::ConstraintEqual(&myFixedValues[3], aCircular->center.y));
229   aNewConstraint->rescale(scale);
230   aConstraints.push_back(aNewConstraint);
231
232   return ConstraintWrapperPtr(
233       new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
234 }
235
236 ConstraintWrapperPtr SketchSolver_ConstraintMovement::fixPointOnEllipse(
237     const EntityWrapperPtr& theConic)
238 {
239   static const double scale = 0.01;
240   static const int nbParams = 6;
241   myFixedValues.reserve(nbParams); // moved point; center and focus of ellipse
242
243   EdgeWrapperPtr anEdge = std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theConic);
244   std::shared_ptr<GCS::Ellipse> aConic = std::dynamic_pointer_cast<GCS::Ellipse>(anEdge->entity());
245
246   // major axis direction
247   double dx = *aConic->focus1.x - *aConic->center.x;
248   double dy = *aConic->focus1.y - *aConic->center.y;
249   double norm = sqrt(dx * dx + dy* dy);
250   if (norm < tolerance) {
251     dx = 1.0;
252     dy = 0.0;
253   }
254   else {
255     dx /= norm;
256     dy /= norm;
257   }
258
259   double aMajorRad = aConic->getRadMaj();
260
261   // initialize fixed values
262   myFixedValues.push_back(*aConic->center.x + dx * aMajorRad);
263   myFixedValues.push_back(*aConic->center.y + dy * aMajorRad);
264   myFixedValues.push_back(*aConic->center.x);
265   myFixedValues.push_back(*aConic->center.y);
266   myFixedValues.push_back(*aConic->focus1.x);
267   myFixedValues.push_back(*aConic->focus1.y);
268
269   // create a moved point
270   GCS::Point aPointOnEllipse = createGCSPoint(&myFixedValues[0], &myFixedValues[1]);
271
272   std::list<GCSConstraintPtr> aConstraints;
273   // point-on-circle
274   GCSConstraintPtr aNewConstraint(
275     new GCS::ConstraintPointOnEllipse(aPointOnEllipse, *aConic));
276   aNewConstraint->rescale(scale);
277   aConstraints.push_back(aNewConstraint);
278   // fixed center (x)
279   aNewConstraint = GCSConstraintPtr(
280     new GCS::ConstraintEqual(&myFixedValues[2], aConic->center.x));
281   aNewConstraint->rescale(scale);
282   aConstraints.push_back(aNewConstraint);
283   // fixed center (y)
284   aNewConstraint = GCSConstraintPtr(
285     new GCS::ConstraintEqual(&myFixedValues[3], aConic->center.y));
286   aNewConstraint->rescale(scale);
287   aConstraints.push_back(aNewConstraint);
288   // focus on the major axis
289   GCS::Point aStartPoint = createGCSPoint(&myFixedValues[2], &myFixedValues[3]);
290   GCS::Point aEndPoint   = createGCSPoint(&myFixedValues[4], &myFixedValues[5]);
291   aNewConstraint = GCSConstraintPtr(
292     new GCS::ConstraintPointOnLine(aConic->focus1, aStartPoint, aEndPoint));
293   aNewConstraint->rescale(scale);
294   aConstraints.push_back(aNewConstraint);
295
296   return ConstraintWrapperPtr(
297     new PlaneGCSSolver_ConstraintWrapper(aConstraints, getType()));
298 }
299
300 void SketchSolver_ConstraintMovement::startPoint(
301     const std::shared_ptr<GeomAPI_Pnt2d>& theStartPoint)
302 {
303   myStartPoint = theStartPoint;
304   if (!mySimpleMove) {
305     myFixedValues[0] = myStartPoint->x();
306     myFixedValues[1] = myStartPoint->y();
307   }
308 }
309
310 void SketchSolver_ConstraintMovement::moveTo(
311     const std::shared_ptr<GeomAPI_Pnt2d>& theDestinationPoint)
312 {
313   if (!myMovedFeature)
314     return; // nothing to move
315
316   double aDelta[2] = { theDestinationPoint->x() - myStartPoint->x(),
317                        theDestinationPoint->y() - myStartPoint->y() };
318
319 #ifdef CHANGE_RADIUS_WHILE_MOVE
320   int aMaxSize = mySimpleMove ? (int)myFixedValues.size() : 2;
321 #else
322   int aMaxSize =
323       myMovedFeature->getKind() == SketchPlugin_Line::ID() && !myDraggedAttribute ? 4 : 2;
324 #endif
325   for (int i = 0; i < aMaxSize; ++i)
326     myFixedValues[i] += aDelta[i % 2];
327 }