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