Salome HOME
6c8527ada14493693e3a070a458139ef0c77da37
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_Storage.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    PlaneGCSSolver_Storage.cpp
4 // Created: 14 Dec 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <PlaneGCSSolver_Storage.h>
8 #include <PlaneGCSSolver_Builder.h>
9 #include <PlaneGCSSolver_Solver.h>
10 #include <PlaneGCSSolver_ConstraintWrapper.h>
11 #include <PlaneGCSSolver_EntityWrapper.h>
12 #include <PlaneGCSSolver_PointWrapper.h>
13 #include <PlaneGCSSolver_ScalarWrapper.h>
14 #include <PlaneGCSSolver_ParameterWrapper.h>
15
16 #include <GeomAPI_Edge.h>
17 #include <GeomAPI_Dir2d.h>
18 #include <GeomAPI_Pnt2d.h>
19 #include <GeomAPI_XY.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <SketchPlugin_Arc.h>
22 #include <SketchPlugin_ConstraintTangent.h>
23
24 #include <cmath>
25
26
27 PlaneGCSSolver_Storage::PlaneGCSSolver_Storage(const GroupID& theGroup)
28   : SketchSolver_Storage(theGroup),
29     myEntityLastID(EID_SKETCH),
30     myConstraintLastID(CID_UNKNOWN)
31 {
32 }
33
34 void PlaneGCSSolver_Storage::addConstraint(
35     ConstraintPtr                   theConstraint,
36     std::list<ConstraintWrapperPtr> theSolverConstraints)
37 {
38   SketchSolver_Storage::addConstraint(theConstraint, theSolverConstraints);
39
40   // update point-point coincidence
41   if (!theSolverConstraints.empty() &&
42       theSolverConstraints.front()->type() == CONSTRAINT_PT_PT_COINCIDENT) {
43     std::list<ConstraintWrapperPtr>::iterator aCIt = theSolverConstraints.begin();
44     for (; aCIt != theSolverConstraints.end(); ++aCIt)
45       update(*aCIt);
46   }
47 }
48
49
50 bool PlaneGCSSolver_Storage::update(ConstraintWrapperPtr theConstraint)
51 {
52   bool isUpdated = false;
53   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
54       std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
55
56   // point-Line distance should be positive
57   if (aConstraint->type() == CONSTRAINT_PT_LINE_DISTANCE && aConstraint->value() < 0.0)
58     aConstraint->setValue(-aConstraint->value());
59
60   // make value of constraint unchangeable
61   ParameterWrapperPtr aValue = aConstraint->valueParameter();
62   if (aValue)
63     isUpdated = update(aValue) || isUpdated;
64
65   // update constrained entities
66   std::list<EntityWrapperPtr> anEntities = theConstraint->entities();
67   std::list<EntityWrapperPtr>::iterator anIt = anEntities.begin();
68   for (; anIt != anEntities.end(); ++anIt)
69     isUpdated = update(*anIt) || isUpdated;
70
71   if (aConstraint->id() == CID_UNKNOWN) {
72     const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
73     // check middle-point constraint conflicts with point-on-line
74     if (aConstraint->type() == CONSTRAINT_MIDDLE_POINT) {
75       std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
76           anIt = myConstraintMap.begin();
77       for (; anIt != myConstraintMap.end(); ++anIt) {
78         EntityWrapperPtr aPoint, aLine;
79         if (anIt->second.empty())
80           continue;
81         ConstraintWrapperPtr aCurrentConstr = anIt->second.front();
82         if (aCurrentConstr->type() != CONSTRAINT_PT_ON_LINE)
83           continue;
84         const std::list<EntityWrapperPtr>& aCurSubs = aCurrentConstr->entities();
85         std::list<EntityWrapperPtr>::const_iterator aSIt1, aSIt2;
86         for (aSIt1 = aSubs.begin(); aSIt1 != aSubs.end(); ++aSIt1) {
87           if ((*aSIt1)->type() == ENTITY_POINT)
88             aPoint = *aSIt1;
89           else if((*aSIt1)->type() == ENTITY_LINE)
90             aLine = *aSIt1;
91           else
92             continue;
93           for (aSIt2 = aCurSubs.begin(); aSIt2 != aCurSubs.end(); ++aSIt2)
94             if ((*aSIt1)->id() == (*aSIt2)->id())
95               break;
96           if (aSIt2 == aCurSubs.end())
97             break;
98         }
99         // point-on-line found, change it to bisector
100         if (aSIt1 == aSubs.end()) {
101           std::list<GCSConstraintPtr> aConstrList = aConstraint->constraints();
102           aConstrList.pop_front();
103           aConstraint->setConstraints(aConstrList);
104           break;
105         }
106       }
107     }
108
109     // Change ID of constraints
110     aConstraint->setId(++myConstraintLastID);
111   }
112
113   return isUpdated;
114 }
115
116 /// \brief Update coordinates of the point or scalar using its base attribute
117 static bool updateValues(EntityWrapperPtr& theEntity)
118 {
119   const double aTol = 1000. * tolerance;
120   bool isUpdated = false;
121   AttributePtr anAttr = theEntity->baseAttribute();
122   const std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
123
124   double aCoord[2];
125
126   std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
127       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anAttr);
128   if (aPoint2D) {
129     aCoord[0] = aPoint2D->x();
130     aCoord[1] = aPoint2D->y();
131   } else {
132     AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anAttr);
133     if (aScalar)
134       aCoord[0] = aScalar->value();
135   }
136
137   std::list<ParameterWrapperPtr>::const_iterator anIt = aParams.begin();
138   for (int i = 0; anIt != aParams.end(); ++anIt, ++i)
139     if (fabs((*anIt)->value() - aCoord[i]) > aTol) {
140       (*anIt)->setValue(aCoord[i]);
141       isUpdated = true;
142     }
143   return isUpdated;
144 }
145
146 bool PlaneGCSSolver_Storage::update(EntityWrapperPtr theEntity)
147 {
148   if (theEntity->type() == ENTITY_SKETCH)
149     return true; // sketch is not necessary for PlaneGCS, so it is always says true
150
151   bool isUpdated = false;
152
153   if (theEntity->baseAttribute()) {
154     isUpdated = updateValues(theEntity);
155     if (isUpdated) {
156       setNeedToResolve(true);
157       if (theEntity->type() == ENTITY_POINT && theEntity->group() != myGroupID)
158         updateCoincident(theEntity);
159     }
160   }
161
162   // update parameters
163   std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
164   std::list<ParameterWrapperPtr>::iterator aPIt = aParams.begin();
165   for (; aPIt != aParams.end(); ++aPIt)
166     isUpdated = update(*aPIt) || isUpdated;
167
168   // update sub-entities
169   std::list<EntityWrapperPtr> aSubEntities = theEntity->subEntities();
170   std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
171   for (; aSIt != aSubEntities.end(); ++aSIt)
172     isUpdated = update(*aSIt) || isUpdated;
173
174   // additional constraints for the arc processing
175   if (theEntity->type() == ENTITY_ARC)
176     processArc(theEntity);
177
178   // Change entity's ID, if necessary
179   if (theEntity->id() == EID_UNKNOWN) {
180     if (theEntity->type() == ENTITY_POINT) {
181       std::shared_ptr<PlaneGCSSolver_PointWrapper> aPoint =
182           std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
183       if (!aPoint) {
184         aPoint = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
185             theEntity->subEntities().front());
186       }
187       aPoint->setId(++myEntityLastID);
188     } else if (theEntity->type() == ENTITY_SCALAR) {
189       std::shared_ptr<PlaneGCSSolver_ScalarWrapper> aScalar =
190           std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(theEntity);
191       aScalar->setId(++myEntityLastID);
192     } else {
193       std::shared_ptr<PlaneGCSSolver_EntityWrapper> aGCSEnt =
194           std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theEntity);
195       aGCSEnt->setId(++myEntityLastID);
196     }
197   }
198   return isUpdated;
199 }
200
201 bool PlaneGCSSolver_Storage::update(ParameterWrapperPtr theParameter)
202 {
203   std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam =
204       std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(theParameter);
205   if (aParam->isProcessed())
206     return false;
207   if (theParameter->group() != myGroupID || theParameter->isParametric())
208     myConst.push_back(aParam->parameter());
209   else
210     myParameters.push_back(aParam->parameter());
211   aParam->setProcessed(true);
212   return true;
213 }
214
215
216 bool PlaneGCSSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
217 {
218   std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aConstraint =
219     std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(theConstraint);
220
221   bool isFullyRemoved = true;
222   // remove point-point coincidence
223   if (aConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT)
224     isFullyRemoved = removeCoincidence(theConstraint) && isFullyRemoved;
225   // remove sub-entities
226   const std::list<EntityWrapperPtr>& aSubs = aConstraint->entities();
227   std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
228   for (; aSIt != aSubs.end(); ++ aSIt)
229     isFullyRemoved = remove(*aSIt) && isFullyRemoved;
230
231   if (aConstraint->valueParameter())
232     isFullyRemoved = remove(aConstraint->valueParameter()) && isFullyRemoved;
233   if (!isFullyRemoved && aConstraint->baseConstraint() &&
234      (!aConstraint->baseConstraint()->data() || !aConstraint->baseConstraint()->data()->isValid()))
235     isFullyRemoved = true;
236   setNeedToResolve(true);
237   myRemovedConstraints.insert(myRemovedConstraints.end(),
238       aConstraint->constraints().begin(), aConstraint->constraints().end());
239
240   if (isFullyRemoved && theConstraint->id() == myConstraintLastID)
241     --myConstraintLastID;
242
243   return isFullyRemoved;
244 }
245
246 bool PlaneGCSSolver_Storage::remove(EntityWrapperPtr theEntity)
247 {
248   // do not remove entity, if it is used by constraints or other entities
249   if ((theEntity->baseFeature() && isUsed(theEntity->baseFeature())) ||
250       (theEntity->baseAttribute() && isUsed(theEntity->baseAttribute())))
251     return false;
252
253   bool isFullyRemoved = SketchSolver_Storage::remove(theEntity);
254   if (isFullyRemoved) {
255     if (theEntity->type() == ENTITY_ARC) {
256       // remove arc additional constraints
257       std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::iterator
258           aFound = myArcConstraintMap.find(theEntity);
259       if (aFound != myArcConstraintMap.end()) {
260         myRemovedConstraints.insert(myRemovedConstraints.end(),
261             aFound->second.begin(), aFound->second.end());
262         myArcConstraintMap.erase(aFound);
263       }
264     }
265     if (theEntity->id() == myEntityLastID)
266       --myEntityLastID;
267   }
268   return isFullyRemoved;
269 }
270
271 bool PlaneGCSSolver_Storage::remove(ParameterWrapperPtr theParameter)
272 {
273   std::shared_ptr<PlaneGCSSolver_ParameterWrapper> aParam =
274       std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(theParameter);
275   if (aParam->isProcessed()) {
276     double* aValPtr = aParam->parameter();
277     GCS::VEC_pD::iterator anIt =  myParameters.begin();
278     for (; anIt != myParameters.end(); ++anIt)
279       if (*anIt == aValPtr)
280         break;
281     if (anIt != myParameters.end()) {
282       myParameters.erase(anIt);
283       setNeedToResolve(true);
284       aParam->setProcessed(false);
285     }
286     else {
287       for (anIt = myConst.begin(); anIt != myConst.end(); ++anIt)
288         if (*anIt == aValPtr)
289           break;
290       if (anIt != myConst.end()) {
291         myConst.erase(anIt);
292         setNeedToResolve(true);
293         aParam->setProcessed(false);
294       }
295     }
296   }
297   return true;
298 }
299
300
301 void PlaneGCSSolver_Storage::addCoincidentPoints(
302     EntityWrapperPtr theMaster, EntityWrapperPtr theSlave)
303 {
304   if (theMaster->type() != ENTITY_POINT || theSlave->type() != ENTITY_POINT)
305     return;
306
307   std::shared_ptr<PlaneGCSSolver_PointWrapper> aMaster =
308       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theMaster);
309   if (!aMaster)
310     aMaster = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
311       std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theMaster)->subEntities().front());
312   std::shared_ptr<PlaneGCSSolver_PointWrapper> aSlave =
313       std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theSlave);
314   if (!aSlave)
315     aSlave = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(
316       std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theSlave)->subEntities().front());
317
318   // Search available coincidence
319   CoincidentPointsMap::iterator aMasterFound = myCoincidentPoints.find(aMaster);
320   CoincidentPointsMap::iterator aSlaveFound = myCoincidentPoints.find(aSlave);
321   if (aMasterFound == myCoincidentPoints.end() &&  aSlaveFound == myCoincidentPoints.end()) {
322     // try to find master and slave points in the lists of slaves of already existent coincidences
323     CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
324     for (; anIt != myCoincidentPoints.end(); ++anIt) {
325       if (anIt->second.find(aMaster) != anIt->second.end())
326         aMasterFound = anIt;
327       else if (anIt->second.find(aSlave) != anIt->second.end())
328         aSlaveFound = anIt;
329
330       if (aMasterFound != myCoincidentPoints.end() &&  aSlaveFound != myCoincidentPoints.end())
331         break;
332     }
333   }
334
335   if (aMasterFound == myCoincidentPoints.end()) {
336     // create new group
337     myCoincidentPoints[aMaster] = std::set<EntityWrapperPtr>();
338     aMasterFound = myCoincidentPoints.find(aMaster);
339   } else if (aMasterFound == aSlaveFound)
340     return; // already coincident
341
342   if (aSlaveFound != myCoincidentPoints.end()) {
343     // A slave has been found, we need to attach all points coincident with it to the new master
344     std::set<EntityWrapperPtr> aNewSlaves = aSlaveFound->second;
345     aNewSlaves.insert(aSlaveFound->first);
346     myCoincidentPoints.erase(aSlaveFound);
347
348     std::set<EntityWrapperPtr>::const_iterator aSlIt = aNewSlaves.begin();
349     for (; aSlIt != aNewSlaves.end(); ++aSlIt)
350       addCoincidentPoints(aMaster, *aSlIt);
351   } else {
352     //std::list<ParameterWrapperPtr> aSlaveParams = aSlave->parameters();
353     //aSlave->setParameters(aMaster->parameters());
354
355     //// Remove slave's parameters
356     //std::list<ParameterWrapperPtr>::iterator aParIt = aSlaveParams.begin();
357     //for (; aParIt != aSlaveParams.end(); ++aParIt)
358     //  remove(*aParIt);
359
360     aMasterFound->second.insert(aSlave);
361   }
362 }
363
364
365 void PlaneGCSSolver_Storage::changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup)
366 {
367   theEntity->setGroup(theGroup);
368   if (theGroup == myGroupID)
369     makeVariable(theEntity);
370   else {
371     if (theEntity->type() == ENTITY_POINT)
372       update(theEntity);
373     makeConstant(theEntity);
374   }
375 }
376
377 void PlaneGCSSolver_Storage::changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup)
378 {
379   // TODO
380 }
381
382 void PlaneGCSSolver_Storage::verifyFixed()
383 {
384   // TODO
385 }
386
387 void PlaneGCSSolver_Storage::processArc(const EntityWrapperPtr& theArc)
388 {
389   // Calculate additional parameters necessary for PlaneGCS
390   const std::list<EntityWrapperPtr>& aSubs = theArc->subEntities();
391   std::list<EntityWrapperPtr>::const_iterator aSubIt = aSubs.begin();
392   while ((*aSubIt)->type() == ENTITY_POINT) // search scalar entities
393     ++aSubIt;
394   double* aStartAngle =
395     std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt++)->scalar();
396   double* aEndAngle =
397     std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt++)->scalar();
398   double* aRadius = std::dynamic_pointer_cast<PlaneGCSSolver_ScalarWrapper>(*aSubIt)->scalar();
399
400   std::shared_ptr<SketchPlugin_Feature> anArcFeature =
401       std::dynamic_pointer_cast<SketchPlugin_Feature>(theArc->baseFeature());
402   std::shared_ptr<GeomDataAPI_Point2D> aCenterAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
403       anArcFeature->attribute(SketchPlugin_Arc::CENTER_ID()));
404   std::shared_ptr<GeomDataAPI_Point2D> aStartAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
405       anArcFeature->attribute(SketchPlugin_Arc::START_ID()));
406   std::shared_ptr<GeomDataAPI_Point2D> aEndAttr = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
407       anArcFeature->attribute(SketchPlugin_Arc::END_ID()));
408   if (!aCenterAttr || !aStartAttr || !aEndAttr)
409     return;
410   std::shared_ptr<GeomAPI_Pnt2d> aCenterPnt = aCenterAttr->pnt();
411   std::shared_ptr<GeomAPI_Pnt2d> aStartPnt  = aStartAttr->pnt();
412   std::shared_ptr<GeomAPI_Pnt2d> aEndPnt    = aEndAttr->pnt();
413
414   *aRadius = aCenterPnt->distance(aStartPnt);
415   if (!anArcFeature->lastResult())
416     return;
417   static std::shared_ptr<GeomAPI_Dir2d> OX(new GeomAPI_Dir2d(1.0, 0.0));
418   std::shared_ptr<GeomAPI_Dir2d> aDir(new GeomAPI_Dir2d(
419       aStartPnt->xy()->decreased(aCenterPnt->xy())));
420   *aStartAngle = OX->angle(aDir);
421   aDir = std::shared_ptr<GeomAPI_Dir2d>(new GeomAPI_Dir2d(
422       aEndPnt->xy()->decreased(aCenterPnt->xy())));
423   *aEndAngle = OX->angle(aDir);
424
425   // no need to constraint a fixed or a copied arc
426   if (theArc->group() == GID_OUTOFGROUP || anArcFeature->isCopy())
427     return;
428   // No need to add constraints if they are already exist
429   std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::const_iterator
430       aFound = myArcConstraintMap.find(theArc);
431   if (aFound != myArcConstraintMap.end())
432     return;
433
434   // Prepare additional constraints to produce the arc
435   std::vector<GCSConstraintPtr> anArcConstraints;
436   std::shared_ptr<PlaneGCSSolver_EntityWrapper> anArcEnt =
437       std::dynamic_pointer_cast<PlaneGCSSolver_EntityWrapper>(theArc);
438   std::shared_ptr<GCS::Arc> anArc = std::dynamic_pointer_cast<GCS::Arc>(anArcEnt->entity());
439   // Distances from center till start and end points are equal to radius
440   GCSConstraintPtr aNew = GCSConstraintPtr(new GCS::ConstraintP2PDistance(
441       anArc->center, anArc->start, anArc->rad));
442 //  aNew->setTag((int)(++myConstraintLastID));
443   anArcConstraints.push_back(aNew);
444   aNew = GCSConstraintPtr(new GCS::ConstraintP2PDistance(
445       anArc->center, anArc->end, anArc->rad));
446 //  aNew->setTag((int)myConstraintLastID);
447   anArcConstraints.push_back(aNew);
448   // Angles of start and end points should be equal to given angles
449   aNew = GCSConstraintPtr(new GCS::ConstraintP2PAngle(
450       anArc->center, anArc->start, anArc->startAngle));
451 //  aNew->setTag((int)myConstraintLastID);
452   anArcConstraints.push_back(aNew);
453   aNew = GCSConstraintPtr(new GCS::ConstraintP2PAngle(
454       anArc->center, anArc->end, anArc->endAngle));
455 //  aNew->setTag((int)myConstraintLastID);
456   anArcConstraints.push_back(aNew);
457
458   myArcConstraintMap[theArc] = anArcConstraints;
459 }
460
461
462 void PlaneGCSSolver_Storage::makeConstant(const EntityWrapperPtr& theEntity)
463 {
464   toggleEntity(theEntity, myParameters, myConst);
465   if (theEntity->type() == ENTITY_POINT)
466     updateCoincident(theEntity);
467 }
468
469 void PlaneGCSSolver_Storage::makeVariable(const EntityWrapperPtr& theEntity)
470 {
471   toggleEntity(theEntity, myConst, myParameters);
472 }
473
474 static void getParametersToMove(const EntityWrapperPtr& theEntity, std::set<double*>& theParamList)
475 {
476   const std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
477   std::list<ParameterWrapperPtr>::const_iterator aPIt = aParams.begin();
478   for (; aPIt != aParams.end(); ++aPIt)
479     theParamList.insert(
480         std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(*aPIt)->parameter());
481
482   const std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
483   std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
484
485   if (theEntity->type() == ENTITY_ARC) {
486     // workaround for the arc processing, because the arc is fixed by a set of constraints,
487     // which will conflict with all parameters fixed:
488     // 1. take center
489     getParametersToMove(*aSIt++, theParamList);
490     // 2. skip start and end points
491     ++aSIt;
492     // 3. take radius, start angle and end angle parameters
493     getParametersToMove(*(++aSIt), theParamList);
494     getParametersToMove(*(++aSIt), theParamList);
495     getParametersToMove(*(++aSIt), theParamList);
496   } else {
497     for (; aSIt != aSubs.end(); ++aSIt)
498       getParametersToMove(*aSIt, theParamList);
499   }
500 }
501
502 void PlaneGCSSolver_Storage::toggleEntity(
503     const EntityWrapperPtr& theEntity, GCS::VEC_pD& theFrom, GCS::VEC_pD& theTo)
504 {
505   std::set<double*> aParamsToMove;
506   getParametersToMove(theEntity, aParamsToMove);
507
508   GCS::VEC_pD::iterator anIt = theFrom.begin();
509   while (anIt != theFrom.end()) {
510     if (aParamsToMove.find(*anIt) == aParamsToMove.end()) {
511       ++anIt;
512       continue;
513     }
514
515     theTo.push_back(*anIt);
516     int aShift = int(anIt - theFrom.begin());
517     theFrom.erase(anIt);
518     anIt = theFrom.begin() + aShift;
519   }
520 }
521
522 void PlaneGCSSolver_Storage::updateCoincident(const EntityWrapperPtr& thePoint)
523 {
524   CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
525   for (; anIt != myCoincidentPoints.end(); ++anIt) {
526     if (anIt->first == thePoint || anIt->second.find(thePoint) != anIt->second.end()) {
527       std::set<EntityWrapperPtr> aCoincident = anIt->second;
528       aCoincident.insert(anIt->first);
529
530       const std::list<ParameterWrapperPtr>& aBaseParams = thePoint->parameters();
531       std::list<ParameterWrapperPtr> aParams;
532       std::list<ParameterWrapperPtr>::const_iterator aBaseIt, anUpdIt;
533
534       std::set<EntityWrapperPtr>::const_iterator aCoincIt = aCoincident.begin();
535       for (; aCoincIt != aCoincident.end(); ++aCoincIt)
536         if (*aCoincIt != thePoint && (*aCoincIt)->group() != GID_OUTOFGROUP) {
537           aParams = (*aCoincIt)->parameters();
538           aBaseIt = aBaseParams.begin();
539           for (anUpdIt = aParams.begin(); anUpdIt != aParams.end(); ++anUpdIt, ++aBaseIt)
540             (*anUpdIt)->setValue((*aBaseIt)->value());
541         }
542
543       break;
544     }
545   }
546 }
547
548
549 bool PlaneGCSSolver_Storage::isRedundant(
550     GCSConstraintPtr theCheckedConstraint,
551     ConstraintWrapperPtr theParentConstraint,
552     std::list<std::set<double*> >& theCoincidentPoints) const
553 {
554   if (theParentConstraint->type() == CONSTRAINT_SYMMETRIC) {
555     if (theCheckedConstraint->getTypeId() == GCS::Perpendicular) {
556       BuilderPtr aBuilder = PlaneGCSSolver_Builder::getInstance();
557       // check the initial point is placed on the mirror line
558       std::list<EntityWrapperPtr> aSubs = theParentConstraint->entities();
559       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(aSubs.front());
560       std::shared_ptr<GeomAPI_Lin2d> aLine = aBuilder->line(aSubs.back());
561       return aLine->distance(aPoint) < tolerance;
562     }
563   }
564   else if (theParentConstraint->type() == CONSTRAINT_PT_PT_COINCIDENT) {
565     // Verify that the coincidence between points is already added
566     GCS::VEC_pD aParams = theCheckedConstraint->params();
567
568     std::list<std::set<double*> >::iterator aCoincIt, aFound1, aFound2;
569     aFound1 = aFound2 = theCoincidentPoints.end();
570     for (aCoincIt = theCoincidentPoints.begin();
571          aCoincIt != theCoincidentPoints.end(); ++aCoincIt) {
572       if (aFound1 == theCoincidentPoints.end() && aCoincIt->find(aParams[0]) != aCoincIt->end())
573         aFound1 = aCoincIt;
574       if (aFound2 == theCoincidentPoints.end() && aCoincIt->find(aParams[1]) != aCoincIt->end())
575         aFound2 = aCoincIt;
576       if (aFound1 != theCoincidentPoints.end() && aFound2 != theCoincidentPoints.end())
577         break;
578     }
579     if (aCoincIt != theCoincidentPoints.end()) { // both point are found
580       if (aFound1 == aFound2)
581         return true;
582       // merge two groups of coincidence
583       aFound1->insert(aFound2->begin(), aFound2->end());
584       theCoincidentPoints.erase(aFound2);
585     } else {
586       if (aFound1 != theCoincidentPoints.end())
587         aFound1->insert(aParams[1]);
588       else if (aFound2 != theCoincidentPoints.end())
589         aFound2->insert(aParams[0]);
590       else {
591         std::set<double*> aNewCoincidence;
592         aNewCoincidence.insert(aParams[0]);
593         aNewCoincidence.insert(aParams[1]);
594         theCoincidentPoints.push_back(aNewCoincidence);
595       }
596     }
597   }
598
599   return false;
600 }
601
602 void PlaneGCSSolver_Storage::initializeSolver(SolverPtr theSolver)
603 {
604   std::shared_ptr<PlaneGCSSolver_Solver> aSolver =
605       std::dynamic_pointer_cast<PlaneGCSSolver_Solver>(theSolver);
606   if (!aSolver)
607     return;
608   aSolver->clear();
609
610   if (myExistArc)
611     processArcs();
612
613   // initialize constraints
614   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
615       aCIt = myConstraintMap.begin();
616   GCS::SET_I aTangentIDs;
617   std::list<std::set<double*> > aCoincidentPoints;
618   for (; aCIt != myConstraintMap.end(); ++aCIt) {
619     std::list<ConstraintWrapperPtr>::const_iterator aCWIt = aCIt->second.begin();
620     for (; aCWIt != aCIt->second.end(); ++ aCWIt) {
621       std::shared_ptr<PlaneGCSSolver_ConstraintWrapper> aGCS =
622           std::dynamic_pointer_cast<PlaneGCSSolver_ConstraintWrapper>(*aCWIt);
623       std::list<GCSConstraintPtr>::const_iterator anIt = aGCS->constraints().begin();
624       for (; anIt != aGCS->constraints().end(); ++anIt)
625         if (!isRedundant(*anIt, aGCS, aCoincidentPoints))
626           aSolver->addConstraint(*anIt, aGCS->type());
627     }
628     // store IDs of tangent constraints to avoid incorrect report of redundant constraints
629     if (aCIt->first && aCIt->first->getKind() == SketchPlugin_ConstraintTangent::ID())
630       for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++ aCWIt)
631         aTangentIDs.insert((int)(*aCWIt)->id());
632   }
633   // additional constraints for arcs
634   std::map<EntityWrapperPtr, std::vector<GCSConstraintPtr> >::const_iterator
635       anArcIt = myArcConstraintMap.begin();
636   for (; anArcIt != myArcConstraintMap.end(); ++anArcIt) {
637     std::vector<GCSConstraintPtr>::const_iterator anIt = anArcIt->second.begin();
638     for (; anIt != anArcIt->second.end(); ++anIt)
639       aSolver->addConstraint(*anIt, CONSTRAINT_UNKNOWN);
640   }
641   // removed waste constraints
642   std::list<GCSConstraintPtr>::const_iterator aRemIt = myRemovedConstraints.begin();
643   for (; aRemIt != myRemovedConstraints.end(); ++aRemIt)
644     aSolver->removeConstraint(*aRemIt);
645   myRemovedConstraints.clear();
646   // set list of tangent constraints
647   aSolver->setTangent(aTangentIDs);
648   // initialize unknowns
649   aSolver->setParameters(myParameters);
650 }
651
652 // indicates attribute containing in the external feature
653 bool isExternalAttribute(const AttributePtr& theAttribute)
654 {
655   if (!theAttribute)
656     return false;
657   std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
658       std::dynamic_pointer_cast<SketchPlugin_Feature>(theAttribute->owner());
659   return aSketchFeature.get() && aSketchFeature->isExternal();
660 }
661
662 void PlaneGCSSolver_Storage::refresh(bool theFixedOnly) const
663 {
664   //blockEvents(true);
665
666   const double aTol = 1000. * tolerance; // tolerance to prevent frequent updates
667
668   std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
669   std::list<ParameterWrapperPtr> aParams;
670   std::list<ParameterWrapperPtr>::const_iterator aParIt;
671   for (; anIt != myAttributeMap.end(); ++anIt) {
672     // the external feature always should keep the up to date values, so,
673     // refresh from the solver is never needed
674     bool isExternal = isExternalAttribute(anIt->first);
675
676     // update parameter wrappers and obtain values of attributes
677     aParams = anIt->second->parameters();
678     double aCoords[3];
679     bool isUpd[3] = {false};
680     int i = 0;
681     for (aParIt = aParams.begin(); i < 3 && aParIt != aParams.end(); ++aParIt, ++i) {
682       if (!theFixedOnly || isExternal ||
683           (*aParIt)->group() == GID_OUTOFGROUP || (*aParIt)->isParametric()) {
684         aCoords[i] = (*aParIt)->value();
685         isUpd[i] = true;
686       }
687     }
688     if (!isUpd[0] && !isUpd[1] && !isUpd[2])
689       continue; // nothing is updated
690
691     std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
692         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
693     if (aPoint2D) {
694       if ((isUpd[0] && fabs(aPoint2D->x() - aCoords[0]) > aTol) ||
695           (isUpd[1] && fabs(aPoint2D->y() - aCoords[1]) > aTol) || isExternal) {
696         // Find points coincident with this one (probably not in GID_OUTOFGROUP)
697         CoincidentPointsMap::const_iterator aCoincIt = myCoincidentPoints.begin();
698         for (; aCoincIt != myCoincidentPoints.end(); ++aCoincIt)
699           if (aCoincIt->first == anIt->second ||
700               aCoincIt->second.find(anIt->second) != aCoincIt->second.end())
701             break;
702         // get coordinates of "master"-point
703         std::shared_ptr<GeomDataAPI_Point2D> aMaster = aCoincIt != myCoincidentPoints.end() ?
704             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aCoincIt->first->baseAttribute()) :
705             aPoint2D;
706         if (!isUpd[0] || isExternal) aCoords[0] = aMaster->x();
707         if (!isUpd[1] || isExternal) aCoords[1] = aMaster->y();
708         if (!isExternal)
709           aPoint2D->setValue(aCoords[0], aCoords[1]);
710         if (aCoincIt != myCoincidentPoints.end()) {
711           if (aMaster && !isExternalAttribute(aMaster))
712             aMaster->setValue(aCoords[0], aCoords[1]);
713           std::set<EntityWrapperPtr>::const_iterator aSlaveIt = aCoincIt->second.begin();
714           for (; aSlaveIt != aCoincIt->second.end(); ++aSlaveIt) {
715             aPoint2D = std::dynamic_pointer_cast<GeomDataAPI_Point2D>((*aSlaveIt)->baseAttribute());
716             if (aPoint2D && !isExternalAttribute(aPoint2D))
717               aPoint2D->setValue(aCoords[0], aCoords[1]);
718           }
719         }
720       }
721       continue;
722     }
723     AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
724     if (aScalar && !isExternal) {
725       if (isUpd[0] && fabs(aScalar->value() - aCoords[0]) > aTol)
726         aScalar->setValue(aCoords[0]);
727       continue;
728     }
729   }
730
731   //blockEvents(false);
732 }
733
734 EntityWrapperPtr PlaneGCSSolver_Storage::calculateMiddlePoint(
735     EntityWrapperPtr theBase, double theCoeff)
736 {
737   std::shared_ptr<PlaneGCSSolver_Builder> aBuilder =
738       std::dynamic_pointer_cast<PlaneGCSSolver_Builder>(PlaneGCSSolver_Builder::getInstance());
739
740   std::shared_ptr<GeomAPI_XY> aMidPoint;
741   if (theBase->type() == ENTITY_LINE) {
742     std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
743     const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
744     std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
745     for (int i = 0; i < 2; ++i, ++anIt)
746       aPoints[i] = aBuilder->point(*anIt);
747     aMidPoint = aPoints[0]->xy()->multiplied(1.0 - theCoeff)->added(
748         aPoints[1]->xy()->multiplied(theCoeff));
749   }
750   else if (theBase->type() == ENTITY_ARC) {
751     double theX, theY;
752     double anArcPoint[3][2];
753     const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
754     std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
755     for (int i = 0; i < 3; ++i, ++anIt) {
756       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(*anIt);
757       anArcPoint[i][0] = aPoint->x();
758       anArcPoint[i][1] = aPoint->y();
759     }
760     // project last point of arc on the arc
761     double x = anArcPoint[1][0] - anArcPoint[0][0];
762     double y = anArcPoint[1][1] - anArcPoint[0][1];
763     double aRad = sqrt(x*x + y*y);
764     x = anArcPoint[2][0] - anArcPoint[0][0];
765     y = anArcPoint[2][1] - anArcPoint[0][1];
766     double aNorm = sqrt(x*x + y*y);
767     if (aNorm >= tolerance) {
768       anArcPoint[2][0] = x * aRad / aNorm;
769       anArcPoint[2][1] = y * aRad / aNorm;
770     }
771     anArcPoint[1][0] -= anArcPoint[0][0];
772     anArcPoint[1][1] -= anArcPoint[0][1];
773     if (theCoeff < tolerance) {
774       theX = anArcPoint[0][0] + anArcPoint[1][0];
775       theY = anArcPoint[0][1] + anArcPoint[1][1];
776     } else if (1 - theCoeff < tolerance) {
777       theX = anArcPoint[0][0] + anArcPoint[2][0];
778       theY = anArcPoint[0][1] + anArcPoint[2][1];
779     } else {
780       std::shared_ptr<GeomAPI_Dir2d>
781         aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
782       std::shared_ptr<GeomAPI_Dir2d>
783         aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
784       double anAngle = aStartDir->angle(aEndDir);
785       if (anAngle < 0)
786         anAngle += 2.0 * PI;
787       anAngle *= theCoeff;
788       double aCos = cos(anAngle);
789       double aSin = sin(anAngle);
790       theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
791       theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
792     }
793     aMidPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(theX, theY));
794   }
795
796   if (!aMidPoint)
797     return EntityWrapperPtr();
798
799   std::list<ParameterWrapperPtr> aParameters;
800   aParameters.push_back(aBuilder->createParameter(myGroupID, aMidPoint->x()));
801   aParameters.push_back(aBuilder->createParameter(myGroupID, aMidPoint->y()));
802   // Create entity (parameters are not filled)
803   GCSPointPtr aPnt(new GCS::Point);
804   aPnt->x =
805     std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(aParameters.front())->parameter();
806   aPnt->y =
807     std::dynamic_pointer_cast<PlaneGCSSolver_ParameterWrapper>(aParameters.back())->parameter();
808
809   EntityWrapperPtr aResult(new PlaneGCSSolver_PointWrapper(AttributePtr(), aPnt));
810   aResult->setGroup(myGroupID);
811   aResult->setParameters(aParameters);
812
813   update(aResult);
814   return aResult;
815 }