Salome HOME
Fix compilation error on Linux. Part V.
[modules/shaper.git] / src / SketchSolver / SolveSpaceSolver / SolveSpaceSolver_Storage.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SolveSpaceSolver_Storage.cpp
4 // Created: 18 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <SolveSpaceSolver_Storage.h>
8 #include <SolveSpaceSolver_ConstraintWrapper.h>
9 #include <SolveSpaceSolver_EntityWrapper.h>
10 #include <SolveSpaceSolver_ParameterWrapper.h>
11 #include <SolveSpaceSolver_Builder.h>
12
13 #include <GeomAPI_Dir2d.h>
14 #include <GeomAPI_Pnt2d.h>
15 #include <GeomAPI_XY.h>
16 #include <math.h>
17 #include <assert.h>
18
19 #include <GeomDataAPI_Point.h>
20 #include <GeomDataAPI_Point2D.h>
21 #include <ModelAPI_AttributeDouble.h>
22
23 /** \brief Search the entity/parameter with specified ID in the list of elements
24  *  \param[in] theEntityID unique ID of the element
25  *  \param[in] theEntities list of elements
26  *  \return position of the found element or -1 if the element is not found
27  */
28 template<typename T>
29 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
30
31 /// \brief Compare two parameters to be different
32 static bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2);
33 /// \brief Compare two entities to be different
34 static bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2);
35 /// \brief Compare two constraints to be different
36 static bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2);
37
38
39 SolveSpaceSolver_Storage::SolveSpaceSolver_Storage(const GroupID& theGroup)
40   : SketchSolver_Storage(theGroup),
41     myWorkplaneID(SLVS_E_UNKNOWN),
42     myParamMaxID(SLVS_E_UNKNOWN),
43     myEntityMaxID(SLVS_E_UNKNOWN),
44     myConstrMaxID(SLVS_C_UNKNOWN),
45     myFixed(SLVS_E_UNKNOWN),
46     myDuplicatedConstraint(false)
47 {
48 }
49
50 bool SolveSpaceSolver_Storage::update(ConstraintWrapperPtr& theConstraint)
51 {
52   bool isUpdated = false;
53   std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
54       std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
55   Slvs_Constraint aSlvsConstr = getConstraint((Slvs_hConstraint)aConstraint->id());
56   if (aSlvsConstr.h == SLVS_C_UNKNOWN)
57     aSlvsConstr = aConstraint->constraint();
58
59   // update value of constraint if exist
60   if (fabs(aSlvsConstr.valA - theConstraint->value()) > tolerance) {
61     aSlvsConstr.valA = theConstraint->value();
62     isUpdated = true;
63   }
64
65   // update constrained entities
66   Slvs_hEntity* aPnts[2] = {&aSlvsConstr.ptA, &aSlvsConstr.ptB};
67   Slvs_hEntity* anEnts[4] = {&aSlvsConstr.entityA, &aSlvsConstr.entityB,
68                              &aSlvsConstr.entityC, &aSlvsConstr.entityD};
69   int aPtInd = 0;
70   int aEntInd = 0;
71
72   std::list<EntityWrapperPtr> anEntities = theConstraint->entities();
73   std::list<EntityWrapperPtr>::iterator anIt = anEntities.begin();
74   for (; anIt != anEntities.end(); ++anIt) {
75     isUpdated = update(*anIt) || isUpdated;
76
77     Slvs_hEntity anID = (Slvs_hEntity)(*anIt)->id();
78     if ((*anIt)->type() == ENTITY_POINT) {
79       if (*(aPnts[aPtInd]) != anID) {
80         *(aPnts[aPtInd]) = anID;
81         isUpdated = true;
82       }
83       ++aPtInd;
84     } else {
85       if (*(anEnts[aEntInd]) != anID) {
86         *(anEnts[aEntInd]) = anID;
87         isUpdated = true;
88       }
89       ++aEntInd;
90     }
91   }
92
93   // update constraint itself (do not update constraints Multi)
94   if (aSlvsConstr.type != SLVS_C_MULTI_ROTATION && aSlvsConstr.type != SLVS_C_MULTI_TRANSLATION) {
95     if (aSlvsConstr.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
96       aSlvsConstr.wrkpl = myWorkplaneID;
97     if (aSlvsConstr.group == SLVS_G_UNKNOWN)
98       aSlvsConstr.group = (Slvs_hGroup)myGroupID;
99     Slvs_hConstraint aConstrID = updateConstraint(aSlvsConstr);
100     if (aSlvsConstr.h == SLVS_C_UNKNOWN) {
101       aConstraint->changeConstraint() = getConstraint(aConstrID);
102       isUpdated = true;
103     }
104   }
105   return isUpdated;
106 }
107
108 bool SolveSpaceSolver_Storage::update(EntityWrapperPtr& theEntity)
109 {
110   bool isUpdated = false;
111   std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity = 
112       std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
113   Slvs_Entity aSlvsEnt = getEntity((Slvs_hEntity)anEntity->id());
114   if (aSlvsEnt.h == SLVS_E_UNKNOWN)
115     aSlvsEnt = anEntity->entity();
116
117   std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
118   std::list<ParameterWrapperPtr>::iterator aPIt;
119   // if the entity is an attribute, need to update its coordinates
120   if (anEntity->baseAttribute()) {
121     BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
122     EntityWrapperPtr anUpdAttr = aBuilder->createAttribute(anEntity->baseAttribute(), GID_UNKNOWN);
123     if (anUpdAttr) {
124       std::list<ParameterWrapperPtr> anUpdParams = anUpdAttr->parameters();
125       std::list<ParameterWrapperPtr>::iterator anUpdIt = anUpdParams.begin();
126       for (aPIt = aParams.begin(); aPIt != aParams.end() && anUpdIt != anUpdParams.end();
127           ++aPIt, ++anUpdIt) {
128         (*aPIt)->update(*anUpdIt);
129       }
130     }
131   }
132
133   // update parameters
134   int anInd = 0;
135   for (aPIt = aParams.begin(); aPIt != aParams.end(); ++aPIt, ++anInd) {
136     assert(anInd < 4);
137     isUpdated = update(*aPIt) || isUpdated;
138     if (aSlvsEnt.param[anInd] != (Slvs_hEntity)(*aPIt)->id()) {
139       isUpdated = true;
140       aSlvsEnt.param[anInd] = (Slvs_hEntity)(*aPIt)->id();
141     }
142   }
143
144   // update sub-entities
145   std::list<EntityWrapperPtr> aSubEntities = theEntity->subEntities();
146   std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
147   for (anInd = 0; aSIt != aSubEntities.end(); ++aSIt, ++anInd) {
148     assert(anInd < 4);
149     isUpdated = update(*aSIt) || isUpdated;
150
151     Slvs_hEntity anID = Slvs_hEntity((*aSIt)->id());
152     if ((*aSIt)->type() == ENTITY_NORMAL)
153       aSlvsEnt.normal = anID;
154     else if ((*aSIt)->type() == ENTITY_SCALAR)
155       aSlvsEnt.distance = anID;
156     else if (aSlvsEnt.point[anInd] != anID) {
157       aSlvsEnt.point[anInd] = anID;
158       isUpdated = true;
159     }
160   }
161
162   // update entity itself
163   if (aSlvsEnt.wrkpl == SLVS_E_UNKNOWN && myWorkplaneID != SLVS_E_UNKNOWN)
164     aSlvsEnt.wrkpl = myWorkplaneID;
165   if (aSlvsEnt.group == SLVS_G_UNKNOWN)
166     aSlvsEnt.group = (Slvs_hGroup)myGroupID;
167   Slvs_hEntity anEntID = updateEntity(aSlvsEnt);
168   if (aSlvsEnt.h == SLVS_E_UNKNOWN) {
169     anEntity->changeEntity() = getEntity(anEntID);
170     isUpdated = true;
171
172     if (anEntity->type() == ENTITY_SKETCH)
173       storeWorkplane(anEntity);
174   }
175   return isUpdated;
176 }
177
178 bool SolveSpaceSolver_Storage::update(ParameterWrapperPtr& theParameter)
179 {
180   std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aParameter = 
181       std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(theParameter);
182   const Slvs_Param& aParam = getParameter((Slvs_hParam)aParameter->id());
183   if (aParam.h != SLVS_E_UNKNOWN && fabs(aParam.val - aParameter->value()) < tolerance)
184     return false;
185   Slvs_Param aParamToUpd = aParameter->parameter();
186   if (aParamToUpd.group == SLVS_G_UNKNOWN)
187     aParamToUpd.group = aParameter->isParametric() ? (Slvs_hGroup)GID_OUTOFGROUP : (Slvs_hGroup)myGroupID;
188 ////  else if (aParameter->isParametric() && aParamToUpd.group != (Slvs_hGroup)GID_OUTOFGROUP)
189 ////    aParameter->setGroup(GID_OUTOFGROUP);
190   Slvs_hParam anID = updateParameter(aParamToUpd);
191   if (aParam.h == SLVS_E_UNKNOWN) // new parameter
192     aParameter->changeParameter() = getParameter(anID);
193   return true;
194 }
195
196 void SolveSpaceSolver_Storage::storeWorkplane(EntityWrapperPtr theSketch)
197 {
198   myWorkplaneID = (Slvs_hEntity)theSketch->id();
199
200   // Update sub-entities of the sketch
201   std::list<EntityWrapperPtr> aSubEntities = theSketch->subEntities();
202   std::list<EntityWrapperPtr>::iterator aSIt = aSubEntities.begin();
203   for (; aSIt != aSubEntities.end(); ++aSIt) {
204     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSub =
205         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*aSIt);
206     aSub->changeEntity().wrkpl = myWorkplaneID;
207   }
208
209   // Update all stored entities
210   std::vector<Slvs_Entity>::iterator anIt = myEntities.begin();
211   for (; anIt != myEntities.end(); ++anIt)
212     anIt->wrkpl = myWorkplaneID;
213 }
214
215 void SolveSpaceSolver_Storage::changeGroup(EntityWrapperPtr theEntity, const GroupID& theGroup)
216 {
217   std::list<ParameterWrapperPtr> aParams = theEntity->parameters();
218   std::list<ParameterWrapperPtr>::iterator aPIt = aParams.begin();
219   for (; aPIt != aParams.end(); ++aPIt)
220     changeGroup(*aPIt, theGroup);
221
222   std::list<EntityWrapperPtr> aSubs = theEntity->subEntities();
223   std::list<EntityWrapperPtr>::iterator aSIt = aSubs.begin();
224   for (; aSIt != aSubs.end(); ++aSIt)
225     changeGroup(*aSIt, theGroup);
226
227   if (theEntity->group() != theGroup) {
228     theEntity->setGroup(theGroup);
229     int aPos = Search((Slvs_hEntity)theEntity->id(), myEntities);
230     if (aPos >= 0 && aPos < (int)myEntities.size()) {
231       myEntities[aPos].group = (Slvs_hGroup)theGroup;
232       setNeedToResolve(true);
233     }
234   }
235 }
236
237 void SolveSpaceSolver_Storage::changeGroup(ParameterWrapperPtr theParam, const GroupID& theGroup)
238 {
239   GroupID aGroup = theGroup;
240   if (theParam->isParametric())
241     aGroup = GID_OUTOFGROUP;
242   if (theParam->group() == aGroup)
243     return;
244
245   theParam->setGroup(aGroup);
246   int aPos = Search((Slvs_hParam)theParam->id(), myParameters);
247   if (aPos >= 0 && aPos < (int)myParameters.size()) {
248     myParameters[aPos].group = (Slvs_hGroup)aGroup;
249     setNeedToResolve(true);
250   }
251 }
252
253 void SolveSpaceSolver_Storage::addCoincidentPoints(
254     EntityWrapperPtr theMaster, EntityWrapperPtr theSlave)
255 {
256   if (theMaster->type() != ENTITY_POINT || theSlave->type() != ENTITY_POINT)
257     return;
258
259   // Search available coincidence
260   CoincidentPointsMap::iterator aMasterFound = myCoincidentPoints.find(theMaster);
261   CoincidentPointsMap::iterator aSlaveFound = myCoincidentPoints.find(theSlave);
262   if (aMasterFound == myCoincidentPoints.end() &&  aSlaveFound == myCoincidentPoints.end()) {
263     // try to find master and slave points in the lists of slaves of already existent coincidences
264     CoincidentPointsMap::iterator anIt = myCoincidentPoints.begin();
265     for (; anIt != myCoincidentPoints.end(); ++anIt) {
266       if (anIt->second.find(theMaster) != anIt->second.end())
267         aMasterFound = anIt;
268       else if (anIt->second.find(theSlave) != anIt->second.end())
269         aSlaveFound = anIt;
270
271       if (aMasterFound != myCoincidentPoints.end() &&  aSlaveFound != myCoincidentPoints.end())
272         break;
273     }
274   }
275
276   if (aMasterFound == myCoincidentPoints.end()) {
277     // create new group
278     myCoincidentPoints[theMaster] = std::set<EntityWrapperPtr>();
279     aMasterFound = myCoincidentPoints.find(theMaster);
280   } else if (aMasterFound == aSlaveFound)
281     return; // already coincident
282
283   if (aSlaveFound != myCoincidentPoints.end()) {
284     // A slave has been found, we need to attach all points coincident with it to the new master
285     std::set<EntityWrapperPtr> aNewSlaves = aSlaveFound->second;
286     aNewSlaves.insert(aSlaveFound->first);
287     myCoincidentPoints.erase(aSlaveFound);
288
289     std::set<EntityWrapperPtr>::const_iterator aSlIt = aNewSlaves.begin();
290     for (; aSlIt != aNewSlaves.end(); ++aSlIt)
291       addCoincidentPoints(theMaster, *aSlIt);
292   } else {
293     // Update the slave if it was used in constraints and features
294     replaceInFeatures(theSlave, theMaster);
295     replaceInConstraints(theSlave, theMaster);
296
297     // Remove slave entity
298     removeEntity((Slvs_hEntity)theSlave->id());
299
300     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointMaster = 
301         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theMaster);
302     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aPointSlave = 
303         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theSlave);
304     aPointSlave->changeEntity() = aPointMaster->entity();
305     aPointSlave->setParameters(aPointMaster->parameters());
306
307     aMasterFound->second.insert(theSlave);
308   }
309 }
310
311 void SolveSpaceSolver_Storage::replaceInFeatures(
312     EntityWrapperPtr theSource, EntityWrapperPtr theDest)
313 {
314   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator anIt = myFeatureMap.begin();
315   for (; anIt != myFeatureMap.end(); ++anIt) {
316     bool isUpdated = false;
317     std::list<EntityWrapperPtr> aSubs = anIt->second->subEntities();
318     std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
319     for (; aSubIt != aSubs.end(); ++aSubIt)
320       if ((*aSubIt)->id() == theSource->id()) {
321         (*aSubIt)->update(theDest);
322         isUpdated = true;
323       }
324
325     if (!isUpdated)
326       continue;
327
328     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aWrapper =
329         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(anIt->second);
330     // update SolveSpace entity
331     Slvs_Entity anEnt = aWrapper->entity();
332     for (int i = 0; i < 4; ++i)
333       if (anEnt.point[i] == (Slvs_hEntity)theSource->id())
334         anEnt.point[i] = (Slvs_hEntity)theDest->id();
335     anEnt.h = updateEntity(anEnt);
336     aWrapper->changeEntity() = anEnt;
337
338     // update sub-entities
339     aWrapper->setSubEntities(aSubs);
340   }
341 }
342
343 void SolveSpaceSolver_Storage::replaceInConstraints(
344     EntityWrapperPtr theSource, EntityWrapperPtr theDest)
345 {
346   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
347       anIt = myConstraintMap.begin();
348   std::list<ConstraintWrapperPtr>::const_iterator aCIt;
349   for (; anIt != myConstraintMap.end(); ++anIt)
350     for (aCIt = anIt->second.begin(); aCIt != anIt->second.end(); ++aCIt) {
351       // Do not process coincidence between points
352       // (these constraints are stored to keep the structure of constraints).
353       if ((*aCIt)->type() == CONSTRAINT_PT_PT_COINCIDENT)
354         continue;
355
356       bool isUpdated = false;
357       std::list<EntityWrapperPtr> aSubs = (*aCIt)->entities();
358       std::list<EntityWrapperPtr>::iterator aSubIt = aSubs.begin();
359       for (; aSubIt != aSubs.end(); ++aSubIt)
360         if ((*aSubIt)->id() == theSource->id()) {
361           (*aSubIt)->update(theDest);
362           isUpdated = true;
363         }
364
365       if (!isUpdated)
366         continue;
367
368       std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aWrapper =
369           std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(*aCIt);
370       // change constraint entities
371       Slvs_Constraint aConstr = aWrapper->constraint();
372       if (aConstr.ptA == (Slvs_hEntity)theSource->id())
373         aConstr.ptA = (Slvs_hEntity)theDest->id();
374       if (aConstr.ptB == (Slvs_hEntity)theSource->id())
375         aConstr.ptB = (Slvs_hEntity)theDest->id();
376
377       // check the constraint is duplicated
378       std::vector<Slvs_Constraint>::const_iterator aSlvsCIt = myConstraints.begin();
379       for (; aSlvsCIt != myConstraints.end(); ++aSlvsCIt)
380         if (aConstr.h != aSlvsCIt->h &&
381             aConstr.type == aSlvsCIt->type &&
382             aConstr.ptA == aSlvsCIt->ptA && aConstr.ptB == aSlvsCIt->ptB &&
383             aConstr.entityA == aSlvsCIt->entityA && aConstr.entityB == aSlvsCIt->entityB &&
384             aConstr.entityC == aSlvsCIt->entityC && aConstr.entityD == aSlvsCIt->entityD) {
385           removeConstraint(aConstr.h);
386           aConstr = *aSlvsCIt;
387           break;
388         }
389
390       if (aSlvsCIt != myConstraints.end()) {
391         // constraint is duplicated, search its wrapper to add the mapping
392         std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
393             anIt2 = myConstraintMap.begin();
394         std::list<ConstraintWrapperPtr>::const_iterator aCIt2;
395         for (; anIt2 != myConstraintMap.end(); ++anIt2)
396           for (aCIt2 = anIt2->second.begin(); aCIt2 != anIt2->second.end(); ++aCIt2)
397             if ((Slvs_hConstraint)(*aCIt2)->id() == aConstr.h) {
398               addSameConstraints(*aCIt2, aWrapper);
399               break;
400             }
401       } else 
402         aConstr.h = updateConstraint(aConstr);
403       aWrapper->changeConstraint() = aConstr;
404
405       // update sub-entities
406       aWrapper->setEntities(aSubs);
407     }
408 }
409
410 void SolveSpaceSolver_Storage::addSameConstraints(ConstraintWrapperPtr theConstraint1,
411                                                   ConstraintWrapperPtr theConstraint2)
412 {
413   SameConstraintMap::iterator anIt = myEqualConstraints.begin();
414   for (; anIt != myEqualConstraints.end(); ++anIt) {
415     if (anIt->find(theConstraint1) != anIt->end()) {
416       anIt->insert(theConstraint2);
417       return;
418     }
419     else if (anIt->find(theConstraint2) != anIt->end()) {
420       anIt->insert(theConstraint1);
421       return;
422     }
423   }
424   // group not found => create new one
425   std::set<ConstraintWrapperPtr> aNewGroup;
426   aNewGroup.insert(theConstraint1);
427   aNewGroup.insert(theConstraint2);
428   myEqualConstraints.push_back(aNewGroup);
429 }
430
431
432 EntityWrapperPtr SolveSpaceSolver_Storage::calculateMiddlePoint(
433     EntityWrapperPtr theBase, double theCoeff)
434 {
435   BuilderPtr aBuilder = SolveSpaceSolver_Builder::getInstance();
436
437   std::shared_ptr<GeomAPI_XY> aMidPoint;
438   if (theBase->type() == ENTITY_LINE) {
439     std::shared_ptr<GeomAPI_Pnt2d> aPoints[2];
440     const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
441     std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
442     for (int i = 0; i < 2; ++i, ++anIt)
443       aPoints[i] = aBuilder->point(*anIt);
444     aMidPoint = aPoints[0]->xy()->multiplied(1.0 - theCoeff)->added(
445         aPoints[1]->xy()->multiplied(theCoeff));
446   }
447   else if (theBase->type() == ENTITY_ARC) {
448     double theX, theY;
449     double anArcPoint[3][2];
450     const std::list<EntityWrapperPtr>& aSubs = theBase->subEntities();
451     std::list<EntityWrapperPtr>::const_iterator anIt = aSubs.begin();
452     for (int i = 0; i < 3; ++i, ++anIt) {
453       std::shared_ptr<GeomAPI_Pnt2d> aPoint = aBuilder->point(*anIt);
454       anArcPoint[i][0] = aPoint->x();
455       anArcPoint[i][1] = aPoint->y();
456     }
457     // project last point of arc on the arc
458     double x = anArcPoint[1][0] - anArcPoint[0][0];
459     double y = anArcPoint[1][1] - anArcPoint[0][1];
460     double aRad = sqrt(x*x + y*y);
461     x = anArcPoint[2][0] - anArcPoint[0][0];
462     y = anArcPoint[2][1] - anArcPoint[0][1];
463     double aNorm = sqrt(x*x + y*y);
464     if (aNorm >= tolerance) {
465       anArcPoint[2][0] = x * aRad / aNorm;
466       anArcPoint[2][1] = y * aRad / aNorm;
467     }
468     anArcPoint[1][0] -= anArcPoint[0][0];
469     anArcPoint[1][1] -= anArcPoint[0][1];
470     if (theCoeff < tolerance) {
471       theX = anArcPoint[0][0] + anArcPoint[1][0];
472       theY = anArcPoint[0][1] + anArcPoint[1][1];
473     } else if (1 - theCoeff < tolerance) {
474       theX = anArcPoint[0][0] + anArcPoint[2][0];
475       theY = anArcPoint[0][1] + anArcPoint[2][1];
476     } else {
477       std::shared_ptr<GeomAPI_Dir2d> aStartDir(new GeomAPI_Dir2d(anArcPoint[1][0], anArcPoint[1][1]));
478       std::shared_ptr<GeomAPI_Dir2d> aEndDir(new GeomAPI_Dir2d(anArcPoint[2][0], anArcPoint[2][1]));
479       double anAngle = aStartDir->angle(aEndDir);
480       if (anAngle < 0)
481         anAngle += 2.0 * PI;
482       anAngle *= theCoeff;
483       double aCos = cos(anAngle);
484       double aSin = sin(anAngle);
485       theX = anArcPoint[0][0] + anArcPoint[1][0] * aCos - anArcPoint[1][1] * aSin;
486       theY = anArcPoint[0][1] + anArcPoint[1][0] * aSin + anArcPoint[1][1] * aCos;
487     }
488     aMidPoint = std::shared_ptr<GeomAPI_XY>(new GeomAPI_XY(theX, theY));
489   }
490
491   if (!aMidPoint)
492     return EntityWrapperPtr();
493
494   std::list<ParameterWrapperPtr> aParameters;
495   Slvs_Param aParam1 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->x());
496   aParam1.h = addParameter(aParam1);
497   aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam1)));
498   Slvs_Param aParam2 = Slvs_MakeParam(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID, aMidPoint->y());
499   aParam2.h = addParameter(aParam2);
500   aParameters.push_back(ParameterWrapperPtr(new SolveSpaceSolver_ParameterWrapper(aParam2)));
501   // Create entity (parameters are not filled)
502   Slvs_Entity anEntity = Slvs_MakePoint2d(SLVS_E_UNKNOWN, (Slvs_hGroup)myGroupID,
503       (Slvs_hEntity)myWorkplaneID, aParam1.h, aParam2.h);
504   anEntity.h = addEntity(anEntity);
505
506   EntityWrapperPtr aResult(new SolveSpaceSolver_EntityWrapper(AttributePtr(), anEntity));
507   aResult->setParameters(aParameters);
508   return aResult;
509 }
510
511
512
513
514
515
516 Slvs_hParam SolveSpaceSolver_Storage::addParameter(const Slvs_Param& theParam)
517 {
518   if (theParam.h > 0 && theParam.h <= myParamMaxID) {
519     // parameter is already used, rewrite it
520     return updateParameter(theParam);
521   }
522
523   Slvs_Param aParam = theParam;
524   if (aParam.h > myParamMaxID)
525     myParamMaxID = aParam.h;
526   else
527     aParam.h = ++myParamMaxID;
528   myParameters.push_back(aParam);
529   myNeedToResolve = true;
530   return aParam.h;
531 }
532
533 Slvs_hParam SolveSpaceSolver_Storage::updateParameter(const Slvs_Param& theParam)
534 {
535   if (theParam.h > 0 && theParam.h <= myParamMaxID) {
536     // parameter already used, rewrite it
537     int aPos = Search(theParam.h, myParameters);
538     if (aPos >= 0 && aPos < (int)myParameters.size()) {
539       if (IsNotEqual(myParameters[aPos], theParam))
540         myUpdatedParameters.insert(theParam.h);
541       myParameters[aPos] = theParam;
542       return theParam.h;
543     }
544   }
545
546   // Parameter is not found, add new one
547   Slvs_Param aParam = theParam;
548   aParam.h = 0;
549   return addParameter(aParam);
550 }
551
552 bool SolveSpaceSolver_Storage::removeParameter(const Slvs_hParam& theParamID)
553 {
554   int aPos = Search(theParamID, myParameters);
555   if (aPos >= 0 && aPos < (int)myParameters.size()) {
556     // Firstly, search the parameter is not used elsewhere
557     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
558     for (; anEntIter != myEntities.end(); anEntIter++) {
559       for (int i = 0; i < 4; i++)
560         if (anEntIter->param[i] == theParamID)
561           return false;
562     }
563     // Remove parameter
564     myParameters.erase(myParameters.begin() + aPos);
565     myParamMaxID = myParameters.empty() ? SLVS_E_UNKNOWN : myParameters.back().h;
566     myNeedToResolve = true;
567 ////    myRemovedParameters.insert(theParamID);
568     return true;
569   }
570 ////  else if (myRemovedParameters.find(theParamID) != myRemovedParameters.end())
571 ////    return true;
572   return true;
573 }
574
575 const Slvs_Param& SolveSpaceSolver_Storage::getParameter(const Slvs_hParam& theParamID) const
576 {
577   int aPos = Search(theParamID, myParameters);
578   if (aPos >= 0 && aPos < (int)myParameters.size())
579     return myParameters[aPos];
580
581   // Parameter is not found, return empty object
582   static Slvs_Param aDummy;
583   aDummy.h = 0;
584   return aDummy;
585 }
586
587
588 Slvs_hEntity SolveSpaceSolver_Storage::addEntity(const Slvs_Entity& theEntity)
589 {
590   if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
591     // Entity is already used, rewrite it
592     return updateEntity(theEntity);
593   }
594
595   Slvs_Entity aEntity = theEntity;
596   if (aEntity.h > myEntityMaxID)
597     myEntityMaxID = aEntity.h;
598   else
599     aEntity.h = ++myEntityMaxID;
600   myEntities.push_back(aEntity);
601   myNeedToResolve = true;
602   return aEntity.h;
603 }
604
605 Slvs_hEntity SolveSpaceSolver_Storage::updateEntity(const Slvs_Entity& theEntity)
606 {
607   if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
608     // Entity already used, rewrite it
609     int aPos = Search(theEntity.h, myEntities);
610     if (aPos >= 0 && aPos < (int)myEntities.size()) {
611       myNeedToResolve = myNeedToResolve || IsNotEqual(myEntities[aPos], theEntity);
612       myEntities[aPos] = theEntity;
613       return theEntity.h;
614     }
615   }
616
617   // Entity is not found, add new one
618   Slvs_Entity aEntity = theEntity;
619   aEntity.h = 0;
620   return addEntity(aEntity);
621 }
622
623 bool SolveSpaceSolver_Storage::removeEntity(const Slvs_hEntity& theEntityID)
624 {
625   bool aResult = true;
626   int aPos = Search(theEntityID, myEntities);
627   if (aPos >= 0 && aPos < (int)myEntities.size()) {
628     // Firstly, check the entity and its attributes is not used elsewhere
629     std::set<Slvs_hEntity> anEntAndSubs;
630     anEntAndSubs.insert(theEntityID);
631     for (int i = 0; i < 4; i++)
632       if (myEntities[aPos].point[i] != SLVS_E_UNKNOWN)
633         anEntAndSubs.insert(myEntities[aPos].point[i]);
634
635     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
636     for (; anEntIter != myEntities.end(); anEntIter++) {
637       if (anEntAndSubs.find(anEntIter->h) != anEntAndSubs.end())
638         continue;
639       for (int i = 0; i < 4; i++)
640         if (anEntAndSubs.find(anEntIter->point[i]) != anEntAndSubs.end())
641           return false;
642       if (anEntAndSubs.find(anEntIter->distance) != anEntAndSubs.end())
643         return false;
644     }
645     std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
646     for (; aConstrIter != myConstraints.end(); aConstrIter++) {
647       Slvs_hEntity anEntIDs[6] = {aConstrIter->ptA, aConstrIter->ptB,
648           aConstrIter->entityA, aConstrIter->entityB,
649           aConstrIter->entityC, aConstrIter->entityD};
650       for (int i = 0; i < 6; i++)
651         if (anEntAndSubs.find(anEntIDs[i]) != anEntAndSubs.end())
652           return false;
653     }
654     // The entity is not used, remove it and its parameters
655     Slvs_Entity anEntity = myEntities[aPos];
656     myEntities.erase(myEntities.begin() + aPos);
657     myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
658     if (anEntity.distance != SLVS_E_UNKNOWN)
659       aResult = aResult && removeParameter(anEntity.distance);
660     for (int i = 0; i < 4; i++)
661       if (anEntity.param[i] != SLVS_E_UNKNOWN)
662         aResult = removeParameter(anEntity.param[i]) && aResult;
663     for (int i = 0; i < 4; i++)
664       if (anEntity.point[i] != SLVS_E_UNKNOWN)
665         aResult = removeEntity(anEntity.point[i]) && aResult;
666     myNeedToResolve = true;
667 ////    myRemovedEntities.insert(theEntityID);
668 ////    if (anEntity.type == SLVS_E_POINT_IN_2D || anEntity.type == SLVS_E_POINT_IN_3D)
669 ////      removeCoincidentPoint(theEntityID);
670   }
671 ////  else if (myRemovedEntities.find(theEntityID) != myRemovedEntities.end())
672 ////    return true;
673   return aResult;
674 }
675
676 void SolveSpaceSolver_Storage::removeUnusedEntities()
677 {
678   std::set<Slvs_hEntity> anUnusedEntities;
679   std::vector<Slvs_Entity>::const_iterator aEIt = myEntities.begin();
680   for (; aEIt != myEntities.end(); ++aEIt) {
681     if (aEIt->h == aEIt->wrkpl) {
682       // don't remove workplane
683       anUnusedEntities.erase(aEIt->point[0]);
684       anUnusedEntities.erase(aEIt->normal);
685       continue;
686     }
687     anUnusedEntities.insert(aEIt->h);
688   }
689
690   std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
691   for (; aCIt != myConstraints.end(); ++aCIt) {
692     Slvs_hEntity aSubs[6] = {
693         aCIt->entityA, aCIt->entityB,
694         aCIt->entityC, aCIt->entityD,
695         aCIt->ptA,     aCIt->ptB};
696     for (int i = 0; i < 6; i++) {
697       if (aSubs[i] != SLVS_E_UNKNOWN) {
698         anUnusedEntities.erase(aSubs[i]);
699         int aPos = Search(aSubs[i], myEntities);
700         if (aPos >= 0 && aPos < (int)myEntities.size()) {
701           for (int j = 0; j < 4; j++)
702             if (myEntities[aPos].point[j] != SLVS_E_UNKNOWN)
703               anUnusedEntities.erase(myEntities[aPos].point[j]);
704           if (myEntities[aPos].distance != SLVS_E_UNKNOWN)
705             anUnusedEntities.erase(myEntities[aPos].distance);
706         }
707       }
708     }
709   }
710
711   std::set<Slvs_hEntity>::const_iterator anEntIt = anUnusedEntities.begin();
712   while (anEntIt != anUnusedEntities.end()) {
713     int aPos = Search(*anEntIt, myEntities);
714     if (aPos < 0 && aPos >= (int)myEntities.size())
715       continue;
716     Slvs_Entity anEntity = myEntities[aPos];
717     // Remove entity if and only if all its parameters unused
718     bool isUsed = false;
719     if (anEntity.distance != SLVS_E_UNKNOWN && 
720       anUnusedEntities.find(anEntity.distance) == anUnusedEntities.end())
721       isUsed = true;
722     for (int i = 0; i < 4 && !isUsed; i++)
723       if (anEntity.point[i] != SLVS_E_UNKNOWN &&
724           anUnusedEntities.find(anEntity.point[i]) == anUnusedEntities.end())
725         isUsed = true;
726     if (isUsed) {
727       anUnusedEntities.erase(anEntity.distance);
728       for (int i = 0; i < 4; i++)
729         if (anEntity.point[i] != SLVS_E_UNKNOWN)
730           anUnusedEntities.erase(anEntity.point[i]);
731       std::set<Slvs_hEntity>::iterator aRemoveIt = anEntIt++;
732       anUnusedEntities.erase(aRemoveIt);
733       continue;
734     }
735     ++anEntIt;
736   }
737
738   for (anEntIt = anUnusedEntities.begin(); anEntIt != anUnusedEntities.end(); ++anEntIt) {
739     int aPos = Search(*anEntIt, myEntities);
740     if (aPos >= 0 && aPos < (int)myEntities.size()) {
741       // Remove entity and its parameters
742       Slvs_Entity anEntity = myEntities[aPos];
743       myEntities.erase(myEntities.begin() + aPos);
744       myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
745       if (anEntity.distance != SLVS_E_UNKNOWN)
746         removeParameter(anEntity.distance);
747       for (int i = 0; i < 4; i++)
748         if (anEntity.param[i] != SLVS_E_UNKNOWN)
749           removeParameter(anEntity.param[i]);
750       for (int i = 0; i < 4; i++)
751         if (anEntity.point[i] != SLVS_E_UNKNOWN)
752           removeEntity(anEntity.point[i]);
753 ////      myRemovedEntities.insert(*anEntIt);
754 ////      if (anEntity.type == SLVS_E_POINT_IN_2D || anEntity.type == SLVS_E_POINT_IN_3D)
755 ////        removeCoincidentPoint(*anEntIt);
756     }
757   }
758
759   if (!anUnusedEntities.empty())
760     myNeedToResolve = true;
761 }
762
763 bool SolveSpaceSolver_Storage::isUsedByConstraints(const Slvs_hEntity& theEntityID) const
764 {
765   std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
766   for (; aCIt != myConstraints.end(); ++aCIt) {
767     Slvs_hEntity aSubs[6] = {
768         aCIt->entityA, aCIt->entityB,
769         aCIt->entityC, aCIt->entityD,
770         aCIt->ptA,     aCIt->ptB};
771     for (int i = 0; i < 6; i++)
772       if (aSubs[i] != SLVS_E_UNKNOWN && aSubs[i] == theEntityID)
773         return true;
774   }
775   return false;
776 }
777
778 const Slvs_Entity& SolveSpaceSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
779 {
780   int aPos = Search(theEntityID, myEntities);
781   if (aPos >= 0 && aPos < (int)myEntities.size())
782     return myEntities[aPos];
783
784   // Entity is not found, return empty object
785   static Slvs_Entity aDummy;
786   aDummy.h = SLVS_E_UNKNOWN;
787   return aDummy;
788 }
789
790 Slvs_hEntity SolveSpaceSolver_Storage::copyEntity(const Slvs_hEntity& theCopied)
791 {
792   int aPos = Search(theCopied, myEntities);
793   if (aPos < 0 || aPos >= (int)myEntities.size())
794     return SLVS_E_UNKNOWN;
795
796   Slvs_Entity aCopy = myEntities[aPos];
797   aCopy.h = SLVS_E_UNKNOWN;
798   int i = 0;
799   while (aCopy.point[i] != SLVS_E_UNKNOWN) {
800     aCopy.point[i] = copyEntity(aCopy.point[i]);
801     i++;
802   }
803   if (aCopy.param[0] != SLVS_E_UNKNOWN) {
804     aPos = Search(aCopy.param[0], myParameters);
805     i = 0;
806     while (aCopy.param[i] != SLVS_E_UNKNOWN) {
807       Slvs_Param aNewParam = myParameters[aPos];
808       aNewParam.h = SLVS_E_UNKNOWN;
809       aCopy.param[i] = addParameter(aNewParam);
810       i++;
811       aPos++;
812     }
813   }
814   return addEntity(aCopy);
815 }
816
817 void SolveSpaceSolver_Storage::copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo)
818 {
819   int aPosFrom = Search(theFrom, myEntities);
820   int aPosTo = Search(theTo, myEntities);
821   if (aPosFrom < 0 || aPosFrom >= (int)myEntities.size() || 
822       aPosTo < 0 || aPosTo >= (int)myEntities.size())
823     return;
824
825   Slvs_Entity aEntFrom = myEntities[aPosFrom];
826   Slvs_Entity aEntTo = myEntities[aPosTo];
827   int i = 0;
828   while (aEntFrom.point[i] != SLVS_E_UNKNOWN) {
829     copyEntity(aEntFrom.point[i], aEntTo.point[i]);
830     i++;
831   }
832   if (aEntFrom.param[0] != SLVS_E_UNKNOWN) {
833     aPosFrom = Search(aEntFrom.param[0], myParameters);
834     aPosTo = Search(aEntTo.param[0], myParameters);
835     i = 0;
836     while (aEntFrom.param[i] != SLVS_E_UNKNOWN) {
837       myParameters[aPosTo++].val = myParameters[aPosFrom++].val;
838       i++;
839     }
840   }
841 }
842
843
844 bool SolveSpaceSolver_Storage::isPointFixed(
845     const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate) const
846 {
847   // Search the set of coincident points
848   std::set<Slvs_hEntity> aCoincident;
849   aCoincident.insert(thePointID);
850 ////  std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
851 ////  for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
852 ////    if (aCPIter->find(thePointID) != aCPIter->end()) {
853 ////      aCoincident = *aCPIter;
854 ////      break;
855 ////    }
856
857   // Check whether one of coincident points is out-of-group
858   std::set<Slvs_hEntity>::const_iterator aCoincIt = aCoincident.begin();
859   for (; aCoincIt != aCoincident.end(); ++aCoincIt) {
860     Slvs_Entity aPoint = getEntity(*aCoincIt);
861     if (aPoint.group == SLVS_G_OUTOFGROUP)
862       return true;
863   }
864
865   // Search the Rigid constraint
866   theFixed = SLVS_C_UNKNOWN;
867   std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
868   for (; aConstrIter != myConstraints.end(); aConstrIter++)
869     if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
870         aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
871       theFixed = aConstrIter->h;
872       if (aConstrIter->ptA == thePointID)
873         return true;
874     }
875   if (theFixed != SLVS_C_UNKNOWN)
876     return true;
877
878   if (theAccurate) {
879     // Try to find the fixed entity which uses such point or its coincidence
880     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
881     for (; anEntIter != myEntities.end(); anEntIter++) {
882       for (int i = 0; i < 4; i++) {
883         Slvs_hEntity aPt = anEntIter->point[i];
884         if (aPt != SLVS_E_UNKNOWN &&
885             (aPt == thePointID || aCoincident.find(aPt) != aCoincident.end())) {
886           if (isEntityFixed(anEntIter->h, true))
887             return true;
888         }
889       }
890     }
891   }
892   return SLVS_E_UNKNOWN;
893 }
894
895 bool SolveSpaceSolver_Storage::isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate) const
896 {
897   int aPos = Search(theEntityID, myEntities);
898   if (aPos < 0 || aPos >= (int)myEntities.size())
899     return false;
900
901   // Firstly, find how many points are under Rigid constraint
902   int aNbFixed = 0;
903   for (int i = 0; i < 4; i++) {
904     Slvs_hEntity aPoint = myEntities[aPos].point[i];
905     if (aPoint == SLVS_E_UNKNOWN)
906       continue;
907
908     std::set<Slvs_hEntity> aCoincident;
909     aCoincident.insert(aPoint);
910 ////    std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
911 ////    for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
912 ////      if (aCPIter->find(aPoint) != aCPIter->end()) {
913 ////        aCoincident = *aCPIter;
914 ////        break;
915 ////      }
916
917     // Search the Rigid constraint
918     std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
919     for (; aConstrIter != myConstraints.end(); aConstrIter++)
920       if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
921           aCoincident.find(aConstrIter->ptA) != aCoincident.end())
922         aNbFixed++;
923   }
924
925   std::list<Slvs_Constraint> aList;
926   std::list<Slvs_Constraint>::iterator anIt;
927   Slvs_hConstraint aTempID; // used in isPointFixed() method
928
929   if (myEntities[aPos].type == SLVS_E_LINE_SEGMENT) {
930     if (aNbFixed == 2)
931       return true;
932     else if (aNbFixed == 0 || !theAccurate)
933       return false;
934     // Additional check (the line may be fixed if it is used by different constraints):
935     // 1. The line is used in Equal constraint, another entity is fixed and there is a fixed point on line
936     aList = getConstraintsByType(SLVS_C_PT_ON_LINE);
937     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
938       if (anIt->entityA == theEntityID && isPointFixed(anIt->ptA, aTempID))
939         break;
940     if (anIt != aList.end()) {
941       aList = getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
942       aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
943       for (anIt = aList.begin(); anIt != aList.end(); anIt++)
944         if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
945           Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
946           if (isEntityFixed(anOther, false))
947             return true;
948         }
949     }
950     // 2. The line is used in Parallel/Perpendicular/Vertical/Horizontal and Length constraints
951     aList = getConstraintsByType(SLVS_C_PARALLEL);
952     aList.splice(aList.end(), getConstraintsByType(SLVS_C_PERPENDICULAR));
953     aList.splice(aList.end(), getConstraintsByType(SLVS_C_VERTICAL));
954     aList.splice(aList.end(), getConstraintsByType(SLVS_C_HORIZONTAL));
955     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
956       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
957         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
958         if (isEntityFixed(anOther, false))
959           break;
960       }
961     if (anIt != aList.end()) {
962       aList = getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
963       for (anIt = aList.begin(); anIt != aList.end(); anIt++)
964         if ((anIt->ptA == myEntities[aPos].point[0] && anIt->ptB == myEntities[aPos].point[1]) ||
965             (anIt->ptA == myEntities[aPos].point[1] && anIt->ptB == myEntities[aPos].point[0]))
966           return true;
967     }
968     // 3. Another verifiers ...
969   } else if (myEntities[aPos].type == SLVS_E_CIRCLE) {
970     if (aNbFixed == 0)
971       return false;
972     // Search for Diameter constraint
973     aList = getConstraintsByType(SLVS_C_DIAMETER);
974     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
975       if (anIt->entityA == theEntityID)
976         return true;
977     if (!theAccurate)
978       return false;
979     // Additional check (the circle may be fixed if it is used by different constraints):
980     // 1. The circle is used in Equal constraint and another entity is fixed
981     aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
982     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
983       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
984         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
985         if (isEntityFixed(anOther, false))
986           return true;
987       }
988     // 2. Another verifiers ...
989   } else if (myEntities[aPos].type == SLVS_E_ARC_OF_CIRCLE) {
990     if (aNbFixed > 2)
991       return true;
992     else if (aNbFixed <= 1)
993       return false;
994     // Search for Diameter constraint
995     aList = getConstraintsByType(SLVS_C_DIAMETER);
996     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
997       if (anIt->entityA == theEntityID)
998         return true;
999     if (!theAccurate)
1000       return false;
1001     // Additional check (the arc may be fixed if it is used by different constraints):
1002     // 1. The arc is used in Equal constraint and another entity is fixed
1003     aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
1004     aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
1005     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
1006       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
1007         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
1008         if (isEntityFixed(anOther, false))
1009           return true;
1010       }
1011     // 2. Another verifiers ...
1012   }
1013   return false;
1014 }
1015
1016
1017 Slvs_hConstraint SolveSpaceSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
1018 {
1019   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
1020     // Constraint is already used, rewrite it
1021     return updateConstraint(theConstraint);
1022   }
1023
1024   Slvs_Constraint aConstraint = theConstraint;
1025
1026   // Find a constraint with same type uses same arguments to show user overconstraint situation
1027   std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
1028   for (; aCIt != myConstraints.end(); aCIt++) {
1029     if (aConstraint.type != aCIt->type)
1030       continue;
1031     if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
1032         aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
1033         aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD)
1034       myDuplicatedConstraint = true;
1035   }
1036
1037   if (aConstraint.h > myConstrMaxID)
1038     myConstrMaxID = aConstraint.h;
1039   else
1040     aConstraint.h = ++myConstrMaxID;
1041   myConstraints.push_back(aConstraint);
1042   myNeedToResolve = true;
1043 ////  if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
1044 ////    addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
1045   return aConstraint.h;
1046 }
1047
1048 Slvs_hConstraint SolveSpaceSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
1049 {
1050   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
1051     // Constraint already used, rewrite it
1052     int aPos = Search(theConstraint.h, myConstraints);
1053     if (aPos >= 0 && aPos < (int)myConstraints.size()) {
1054       myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
1055       myConstraints[aPos] = theConstraint;
1056 ////      if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
1057 ////        addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
1058       return theConstraint.h;
1059     }
1060   }
1061
1062   // Constraint is not found, add new one
1063   Slvs_Constraint aConstraint = theConstraint;
1064   aConstraint.h = 0;
1065   return addConstraint(aConstraint);
1066 }
1067
1068 bool SolveSpaceSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
1069 {
1070   bool aResult = true;
1071   int aPos = Search(theConstraintID, myConstraints);
1072   if (aPos >= 0 && aPos < (int)myConstraints.size()) {
1073     Slvs_Constraint aConstraint = myConstraints[aPos];
1074     myConstraints.erase(myConstraints.begin() + aPos);
1075     myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
1076     myNeedToResolve = true;
1077 ////    myRemovedConstraints.insert(theConstraintID);
1078 ////    if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
1079 ////      removeCoincidence(aConstraint);
1080
1081     // Remove all entities
1082     Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
1083         aConstraint.entityA, aConstraint.entityB,
1084         aConstraint.entityC, aConstraint.entityD};
1085     for (int i = 0; i < 6; i++)
1086       if (anEntities[i] != SLVS_E_UNKNOWN)
1087         aResult = removeEntity(anEntities[i]) && aResult;
1088     // remove temporary fixed point, if available
1089     if (myFixed == theConstraintID)
1090       myFixed = SLVS_E_UNKNOWN;
1091     if (myDuplicatedConstraint) {
1092       // Check the duplicated constraints are still available
1093       myDuplicatedConstraint = false;
1094       std::vector<Slvs_Constraint>::const_iterator anIt1 = myConstraints.begin();
1095       std::vector<Slvs_Constraint>::const_iterator anIt2 = myConstraints.begin();
1096       for (; anIt1 != myConstraints.end() && !myDuplicatedConstraint; anIt1++)
1097         for (anIt2 = anIt1+1; anIt2 != myConstraints.end() && !myDuplicatedConstraint; anIt2++) {
1098           if (anIt1->type != anIt2->type)
1099             continue;
1100           if (anIt1->ptA == anIt2->ptA && anIt1->ptB == anIt2->ptB &&
1101               anIt1->entityA == anIt2->entityA && anIt1->entityB == anIt2->entityB &&
1102               anIt1->entityC == anIt2->entityC && anIt1->entityD == anIt2->entityD)
1103             myDuplicatedConstraint = true;
1104         }
1105     }
1106   }
1107 ////  else if (myRemovedConstraints.find(theConstraintID) != myRemovedConstraints.end())
1108 ////    return true;
1109   return aResult;
1110 }
1111
1112 const Slvs_Constraint& SolveSpaceSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
1113 {
1114   int aPos = Search(theConstraintID, myConstraints);
1115   if (aPos >= 0 && aPos < (int)myConstraints.size())
1116     return myConstraints[aPos];
1117
1118   // Constraint is not found, return empty object
1119   static Slvs_Constraint aDummy;
1120   aDummy.h = 0;
1121   return aDummy;
1122 }
1123
1124 std::list<Slvs_Constraint> SolveSpaceSolver_Storage::getConstraintsByType(int theConstraintType) const
1125 {
1126   std::list<Slvs_Constraint> aResult;
1127   std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
1128   for (; aCIter != myConstraints.end(); aCIter++)
1129     if (aCIter->type == theConstraintType)
1130       aResult.push_back(*aCIter);
1131   return aResult;
1132 }
1133
1134
1135 void SolveSpaceSolver_Storage::addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID)
1136 {
1137   if (myFixed != SLVS_E_UNKNOWN)
1138     return; // the point is already fixed
1139   int aPos = Search(theConstraintID, myConstraints);
1140   if (aPos >= 0 && aPos < (int)myConstraints.size())
1141     myFixed = theConstraintID;
1142 }
1143
1144 void SolveSpaceSolver_Storage::addTemporaryConstraint(const Slvs_hConstraint& theConstraintID)
1145 {
1146   myTemporaryConstraints.insert(theConstraintID);
1147 }
1148
1149 void SolveSpaceSolver_Storage::removeAllTemporary()
1150 {
1151   myTemporaryConstraints.clear();
1152 }
1153
1154 size_t SolveSpaceSolver_Storage::removeTemporary(size_t theNbConstraints)
1155 {
1156   if (myTemporaryConstraints.empty())
1157     return 0;
1158   // Search the point-on-line or a non-rigid constraint
1159   std::set<Slvs_hConstraint>::iterator aCIt = myTemporaryConstraints.begin();
1160   for (; aCIt != myTemporaryConstraints.end(); aCIt++) {
1161     int aPos = Search(*aCIt, myConstraints);
1162     if (aPos >= (int)myConstraints.size() || myConstraints[aPos].type != SLVS_C_WHERE_DRAGGED)
1163       break;
1164     std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
1165     for (; anIt != myConstraints.end(); anIt++)
1166       if (anIt->type == SLVS_C_PT_ON_LINE && anIt->ptA == myConstraints[aPos].ptA)
1167         break;
1168     if (anIt != myConstraints.end())
1169       break;
1170   }
1171   if (aCIt == myTemporaryConstraints.end())
1172     aCIt = myTemporaryConstraints.begin();
1173   bool aNewFixed = false;
1174
1175   size_t aNbRemain = theNbConstraints;
1176   while (aNbRemain > 0 && aCIt != myTemporaryConstraints.end()) {
1177     aNewFixed = aNewFixed || (*aCIt == myFixed);
1178     --aNbRemain;
1179
1180     std::set<Slvs_hConstraint>::iterator aRemoveIt = aCIt++;
1181     removeConstraint(*aRemoveIt);
1182     myTemporaryConstraints.erase(aRemoveIt);
1183     if (aCIt == myTemporaryConstraints.end())
1184       aCIt = myTemporaryConstraints.begin();
1185   }
1186
1187   if (aNewFixed) {
1188     for (aCIt = myTemporaryConstraints.begin(); aCIt != myTemporaryConstraints.end(); aCIt++) {
1189       int aPos = Search(*aCIt, myConstraints);
1190       if (myConstraints[aPos].type == SLVS_C_WHERE_DRAGGED) {
1191         myFixed = *aCIt;
1192         break;
1193       }
1194     }
1195   }
1196   return myTemporaryConstraints.size();
1197 }
1198
1199 bool SolveSpaceSolver_Storage::isTemporary(const Slvs_hConstraint& theConstraintID) const
1200 {
1201   return myTemporaryConstraints.find(theConstraintID) != myTemporaryConstraints.end();
1202 }
1203
1204
1205 ////void SolveSpaceSolver_Storage::getRemoved(
1206 ////    std::set<Slvs_hParam>& theParameters,
1207 ////    std::set<Slvs_hEntity>& theEntities,
1208 ////    std::set<Slvs_hConstraint>& theConstraints)
1209 ////{
1210 ////  theParameters = myRemovedParameters;
1211 ////  theEntities = myRemovedEntities;
1212 ////  theConstraints = myRemovedConstraints;
1213 ////
1214 ////  myRemovedParameters.clear();
1215 ////  myRemovedEntities.clear();
1216 ////  myRemovedConstraints.clear();
1217 ////}
1218
1219 void SolveSpaceSolver_Storage::initializeSolver(SolverPtr theSolver)
1220 {
1221   std::shared_ptr<SolveSpaceSolver_Solver> aSolver =
1222       std::dynamic_pointer_cast<SolveSpaceSolver_Solver>(theSolver);
1223   if (!aSolver)
1224     return;
1225
1226   if (myConstraints.empty()) {
1227     // Adjust all arc to place their points correctly
1228     std::vector<Slvs_Entity>::const_iterator anEntIt = myEntities.begin();
1229     for (; anEntIt != myEntities.end(); ++anEntIt)
1230       if (anEntIt->type == SLVS_E_ARC_OF_CIRCLE)
1231         adjustArc(*anEntIt);
1232   }
1233
1234   aSolver->setParameters(myParameters.data(), (int)myParameters.size());
1235   aSolver->setEntities(myEntities.data(), (int)myEntities.size());
1236
1237   // Copy constraints excluding the fixed one
1238   std::vector<Slvs_Constraint> aConstraints = myConstraints;
1239   if (myFixed != SLVS_E_UNKNOWN) {
1240     Slvs_hEntity aFixedPoint = SLVS_E_UNKNOWN;
1241     std::vector<Slvs_Constraint>::iterator anIt = aConstraints.begin();
1242     for (; anIt != aConstraints.end(); anIt++)
1243       if (anIt->h == myFixed) {
1244         aFixedPoint = anIt->ptA;
1245         aConstraints.erase(anIt);
1246         break;
1247       }
1248     // set dragged parameters
1249     int aPos = Search(aFixedPoint, myEntities);
1250     aSolver->setDraggedParameters(myEntities[aPos].param);
1251   }
1252   aSolver->setConstraints(aConstraints.data(), (int)aConstraints.size());
1253 }
1254
1255 ////void SolveSpaceSolver_Storage::addCoincidentPoints(
1256 ////    const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
1257 ////{
1258 ////  std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
1259 ////  std::vector< std::set<Slvs_hEntity> >::iterator aFoundIter = myCoincidentPoints.end(); // already found coincidence
1260 ////  bool isFound = false;
1261 ////  for (; aCIter != myCoincidentPoints.end(); aCIter++) {
1262 ////    bool isFirstFound = aCIter->find(thePoint1) != aCIter->end();
1263 ////    bool isSecondFound = aCIter->find(thePoint2) != aCIter->end();
1264 ////    isFound = isFound || isFirstFound || isSecondFound;
1265 ////    if (isFirstFound && isSecondFound)
1266 ////      break; // already coincident
1267 ////    else if (isFirstFound || isSecondFound) {
1268 ////      if (aFoundIter != myCoincidentPoints.end()) {
1269 ////        // merge two sets
1270 ////        aFoundIter->insert(aCIter->begin(), aCIter->end());
1271 ////        myCoincidentPoints.erase(aCIter);
1272 ////        break;
1273 ////      } else
1274 ////        aFoundIter = aCIter;
1275 ////      aCIter->insert(thePoint1);
1276 ////      aCIter->insert(thePoint2);
1277 ////    }
1278 ////  }
1279 ////  // coincident points not found
1280 ////  if (!isFound) {
1281 ////    std::set<Slvs_hEntity> aNewSet;
1282 ////    aNewSet.insert(thePoint1);
1283 ////    aNewSet.insert(thePoint2);
1284 ////    myCoincidentPoints.push_back(aNewSet);
1285 ////  }
1286 ////}
1287 ////
1288 ////void SolveSpaceSolver_Storage::removeCoincidentPoint(const Slvs_hEntity& thePoint)
1289 ////{
1290 ////  std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
1291 ////  for (; aCIter != myCoincidentPoints.end(); aCIter++)
1292 ////    if (aCIter->find(thePoint) != aCIter->end()) {
1293 ////      aCIter->erase(thePoint);
1294 ////      if (aCIter->size() <= 1)
1295 ////        myCoincidentPoints.erase(aCIter);
1296 ////      break;
1297 ////    }
1298 ////}
1299 ////
1300 ////void SolveSpaceSolver_Storage::removeCoincidence(const Slvs_Constraint& theCoincidence)
1301 ////{
1302 ////  // Find set of coincident points
1303 ////  std::vector< std::set<Slvs_hEntity> >::iterator aCIt = myCoincidentPoints.begin();
1304 ////  for (; aCIt != myCoincidentPoints.end(); ++aCIt)
1305 ////    if (aCIt->find(theCoincidence.ptA) != aCIt->end() ||
1306 ////        aCIt->find(theCoincidence.ptB) != aCIt->end())
1307 ////      break;
1308 ////  if (aCIt == myCoincidentPoints.end())
1309 ////    return;
1310 ////
1311 ////  // Leave only the points which are still coincident
1312 ////  std::set<Slvs_hEntity> aRemainCoincidence;
1313 ////  std::vector<Slvs_Constraint>::const_iterator aConstrIt = myConstraints.begin();
1314 ////  for (; aConstrIt != myConstraints.end(); ++aConstrIt) {
1315 ////    if (aConstrIt->type != SLVS_C_POINTS_COINCIDENT)
1316 ////      continue;
1317 ////    if (aCIt->find(aConstrIt->ptA) != aCIt->end() ||
1318 ////        aCIt->find(aConstrIt->ptB) != aCIt->end()) {
1319 ////      aRemainCoincidence.insert(aConstrIt->ptA);
1320 ////      aRemainCoincidence.insert(aConstrIt->ptB);
1321 ////    }
1322 ////  }
1323 ////  if (aRemainCoincidence.size() <= 1)
1324 ////    myCoincidentPoints.erase(aCIt);
1325 ////  else
1326 ////    aCIt->swap(aRemainCoincidence);
1327 ////}
1328 ////
1329 ////bool SolveSpaceSolver_Storage::isCoincident(
1330 ////    const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
1331 ////{
1332 ////  std::vector< std::set<Slvs_hEntity> >::const_iterator aCIter = myCoincidentPoints.begin();
1333 ////  for (; aCIter != myCoincidentPoints.end(); aCIter++)
1334 ////    if (aCIter->find(thePoint1) != aCIter->end() && aCIter->find(thePoint2) != aCIter->end())
1335 ////      return true;
1336 ////  return false;
1337 ////}
1338
1339 bool SolveSpaceSolver_Storage::isEqual(
1340     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
1341 {
1342 ////  if (isCoincident(thePoint1, thePoint2))
1343 ////    return true;
1344
1345   // Precise checking of coincidence: verify that points have equal coordinates
1346   int aEnt1Pos = Search(thePoint1, myEntities);
1347   int aEnt2Pos = Search(thePoint2, myEntities);
1348   if (aEnt1Pos >= 0 && aEnt1Pos < (int)myEntities.size() &&
1349       aEnt2Pos >= 0 && aEnt2Pos < (int)myEntities.size()) {
1350     double aDist[2];
1351     int aParamPos;
1352     for (int i = 0; i < 2; i++) {
1353       aParamPos = Search(myEntities[aEnt1Pos].param[i], myParameters);
1354       aDist[i] = myParameters[aParamPos].val;
1355       aParamPos = Search(myEntities[aEnt2Pos].param[i], myParameters);
1356       aDist[i] -= myParameters[aParamPos].val;
1357     }
1358     if (aDist[0] * aDist[0] + aDist[1] * aDist[1] < tolerance * tolerance)
1359       return true;
1360   }
1361   return false;
1362 }
1363
1364
1365 std::vector<Slvs_hConstraint> SolveSpaceSolver_Storage::fixEntity(const Slvs_hEntity& theEntity)
1366 {
1367   std::vector<Slvs_hConstraint> aNewConstraints;
1368
1369   int aPos = Search(theEntity, myEntities);
1370   if (aPos >= 0 && aPos < (int)myEntities.size()) {
1371     switch (myEntities[aPos].type) {
1372     case SLVS_E_POINT_IN_2D:
1373     case SLVS_E_POINT_IN_3D:
1374       fixPoint(myEntities[aPos], aNewConstraints);
1375       break;
1376     case SLVS_E_LINE_SEGMENT:
1377       fixLine(myEntities[aPos], aNewConstraints);
1378       break;
1379     case SLVS_E_CIRCLE:
1380       fixCircle(myEntities[aPos], aNewConstraints);
1381       break;
1382     case SLVS_E_ARC_OF_CIRCLE:
1383       fixArc(myEntities[aPos], aNewConstraints);
1384       break;
1385     default:
1386       break;
1387     }
1388   }
1389
1390   return aNewConstraints;
1391 }
1392
1393 void SolveSpaceSolver_Storage::fixPoint(const Slvs_Entity& thePoint,
1394     std::vector<Slvs_hConstraint>& theCreated)
1395 {
1396   Slvs_Constraint aConstraint;
1397   Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
1398   bool isFixed = isPointFixed(thePoint.h, aConstrID, true);
1399   bool isForceUpdate = (isFixed && isTemporary(aConstrID));
1400   if (!isForceUpdate) { // create new constraint
1401     if (isFixed) return;
1402     aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, thePoint.group, SLVS_C_WHERE_DRAGGED, thePoint.wrkpl,
1403         0.0, thePoint.h, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
1404     aConstraint.h = addConstraint(aConstraint);
1405     theCreated.push_back(aConstraint.h);
1406   } else { // update already existent constraint
1407     if (!isFixed || aConstrID == SLVS_E_UNKNOWN)
1408       return;
1409     int aPos = Search(aConstrID, myConstraints);
1410     if (aPos >= 0 && aPos < (int)myConstraints.size())
1411       myConstraints[aPos].ptA = thePoint.h;
1412   }
1413 }
1414
1415 void SolveSpaceSolver_Storage::fixLine(const Slvs_Entity& theLine,
1416     std::vector<Slvs_hConstraint>& theCreated)
1417 {
1418   Slvs_Entity aPoint[2] = {
1419       getEntity(theLine.point[0]),
1420       getEntity(theLine.point[1])
1421   };
1422
1423   Slvs_Constraint anEqual;
1424   if (isAxisParallel(theLine.h)) {
1425     // Fix one point and a line length
1426     Slvs_hConstraint aFixed;
1427     if (!isPointFixed(theLine.point[0], aFixed, true) &&
1428         !isPointFixed(theLine.point[1], aFixed, true))
1429       fixPoint(aPoint[0], theCreated);
1430     if (!isUsedInEqual(theLine.h, anEqual)) {
1431       // Check the distance is not set yet
1432       std::vector<Slvs_Constraint>::const_iterator aDistIt = myConstraints.begin();
1433       for (; aDistIt != myConstraints.end(); ++aDistIt)
1434         if ((aDistIt->type == SLVS_C_PT_PT_DISTANCE) &&
1435            ((aDistIt->ptA == theLine.point[0] && aDistIt->ptB == theLine.point[1]) ||
1436             (aDistIt->ptA == theLine.point[1] && aDistIt->ptB == theLine.point[0])))
1437           return;
1438       // Calculate distance between points on the line
1439       double aCoords[4];
1440       for (int i = 0; i < 2; i++)
1441         for (int j = 0; j < 2; j++) {
1442           Slvs_Param aParam = getParameter(aPoint[i].param[j]);
1443           aCoords[2*i+j] = aParam.val;
1444         }
1445
1446       double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) + 
1447                             (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
1448       // fix line length
1449       Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group,
1450           SLVS_C_PT_PT_DISTANCE, theLine.wrkpl, aLength,
1451           theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
1452       aDistance.h = addConstraint(aDistance);
1453       theCreated.push_back(aDistance.h);
1454     }
1455     return;
1456   }
1457   else if (isUsedInEqual(theLine.h, anEqual)) {
1458     // Check another entity of Equal is already fixed
1459     Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
1460     if (isEntityFixed(anOtherEntID, true)) {
1461       // Fix start point of the line (if end point is not fixed yet) ...
1462       Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
1463       bool isFixed = isPointFixed(theLine.point[1], anEndFixedID, true);
1464       if (isFixed == SLVS_E_UNKNOWN)
1465         fixPoint(aPoint[0], theCreated);
1466       // ...  and create fixed point lying on this line
1467       Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
1468       // Firstly, search already fixed point on line
1469       bool isPonLineFixed = false;
1470       Slvs_hEntity aFixedPoint;
1471       std::vector<Slvs_Constraint>::const_iterator aPLIter = myConstraints.begin();
1472       for (; aPLIter != myConstraints.end() && !isPonLineFixed; ++aPLIter)
1473         if (aPLIter->type == SLVS_C_PT_ON_LINE && aPLIter->entityA == theLine.h) {
1474           isPonLineFixed = isPointFixed(aPLIter->ptA, anEndFixedID);
1475           aFixedPoint = aPLIter->ptA;
1476         }
1477
1478       if (isPonLineFixed) { // update existent constraint
1479         copyEntity(aPointToCopy, aFixedPoint);
1480       } else { // create new constraint
1481         Slvs_hEntity aCopied = copyEntity(aPointToCopy);
1482         Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group, SLVS_C_PT_ON_LINE,
1483             theLine.wrkpl, 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
1484         aPonLine.h = addConstraint(aPonLine);
1485         theCreated.push_back(aPonLine.h);
1486         fixPoint(getEntity(aCopied), theCreated);
1487       }
1488       return;
1489     }
1490   }
1491
1492   // Fix both points
1493   for (int i = 0; i < 2; i++)
1494     fixPoint(aPoint[i], theCreated);
1495 }
1496
1497 void SolveSpaceSolver_Storage::fixCircle(const Slvs_Entity& theCircle,
1498     std::vector<Slvs_hConstraint>& theCreated)
1499 {
1500   bool isFixRadius = true;
1501   // Verify the arc is under Equal constraint
1502   Slvs_Constraint anEqual;
1503   if (isUsedInEqual(theCircle.h, anEqual)) {
1504     // Check another entity of Equal is already fixed
1505     Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
1506     if (isEntityFixed(anOtherEntID, true))
1507       isFixRadius = false;
1508   }
1509
1510   fixPoint(getEntity(theCircle.point[0]), theCreated);
1511
1512   if (isFixRadius) {
1513     // Search the radius is already fixed
1514     std::vector<Slvs_Constraint>::const_iterator aDiamIter = myConstraints.begin();
1515     for (; aDiamIter != myConstraints.end(); ++aDiamIter)
1516       if (aDiamIter->type == SLVS_C_DIAMETER && aDiamIter->entityA == theCircle.h)
1517         return;
1518
1519     // Fix radius of a circle
1520     const Slvs_Entity& aRadEnt = getEntity(theCircle.distance);
1521     double aRadius = getParameter(aRadEnt.param[0]).val;
1522     Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theCircle.group, SLVS_C_DIAMETER,
1523         theCircle.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theCircle.h, SLVS_E_UNKNOWN);
1524     aFixedR.h = addConstraint(aFixedR);
1525     theCreated.push_back(aFixedR.h);
1526   }
1527 }
1528
1529 void SolveSpaceSolver_Storage::fixArc(const Slvs_Entity& theArc,
1530     std::vector<Slvs_hConstraint>& theCreated)
1531 {
1532   Slvs_Entity aPoint[3] = {
1533       getEntity(theArc.point[0]),
1534       getEntity(theArc.point[1]),
1535       getEntity(theArc.point[2])
1536   };
1537
1538   bool isFixRadius = true;
1539   std::list<Slvs_Entity> aPointsToFix;
1540   aPointsToFix.push_back(aPoint[1]);
1541   aPointsToFix.push_back(aPoint[2]);
1542
1543   // Verify the arc is under Equal constraint
1544   Slvs_Constraint anEqual;
1545   if (isUsedInEqual(theArc.h, anEqual)) {
1546     // Check another entity of Equal is already fixed
1547     Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
1548     if (isEntityFixed(anOtherEntID, true)) {
1549       isFixRadius = false;
1550       Slvs_Entity anOtherEntity = getEntity(anOtherEntID);
1551       if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
1552         aPointsToFix.pop_back();
1553         aPointsToFix.push_back(aPoint[0]);
1554       }
1555     }
1556   }
1557
1558   Slvs_hConstraint aConstrID;
1559   int aNbPointsToFix = 2; // number of fixed points for the arc
1560   if (isPointFixed(theArc.point[0], aConstrID, true))
1561     aNbPointsToFix--;
1562
1563   double anArcPoints[3][2];
1564   for (int i = 0; i < 3; i++) {
1565     const Slvs_Entity& aPointOnArc = getEntity(theArc.point[i]);
1566     for (int j = 0; j < 2; j++)
1567       anArcPoints[i][j] = getParameter(aPointOnArc.param[j]).val;
1568   }
1569
1570   // Radius of the arc
1571   std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(anArcPoints[0][0], anArcPoints[0][1]));
1572   std::shared_ptr<GeomAPI_Pnt2d> aStart(new GeomAPI_Pnt2d(anArcPoints[1][0], anArcPoints[1][1]));
1573   double aRadius = aCenter->distance(aStart);
1574
1575   // Update end point of the arc to be on a curve
1576   std::shared_ptr<GeomAPI_Pnt2d> anEnd(new GeomAPI_Pnt2d(anArcPoints[2][0], anArcPoints[2][1]));
1577   double aDistance = anEnd->distance(aCenter);
1578   std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
1579   if (aDistance < tolerance)
1580     aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
1581   else
1582     aDir = aDir->multiplied(aRadius / aDistance);
1583   double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
1584   const Slvs_Entity& aEndPoint = getEntity(theArc.point[2]);
1585   for (int i = 0; i < 2; i++) {
1586     Slvs_Param aParam = getParameter(aEndPoint.param[i]);
1587     aParam.val = xy[i];
1588     updateParameter(aParam);
1589   }
1590
1591   std::list<Slvs_Entity>::iterator aPtIt = aPointsToFix.begin();
1592   for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
1593     fixPoint(*aPtIt, theCreated);
1594
1595   if (isFixRadius) {
1596     // Fix radius of the arc
1597     bool isExists = false;
1598     std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
1599     for (; anIt != myConstraints.end() && !isExists; ++anIt)
1600       if (anIt->type == SLVS_C_DIAMETER && anIt->entityA == theArc.h)
1601         isExists = true;
1602     if (!isExists) {
1603       Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theArc.group, SLVS_C_DIAMETER,
1604           theArc.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theArc.h, SLVS_E_UNKNOWN);
1605       aFixedR.h = addConstraint(aFixedR);
1606       theCreated.push_back(aFixedR.h);
1607     }
1608   }
1609 }
1610
1611
1612 bool SolveSpaceSolver_Storage::isAxisParallel(const Slvs_hEntity& theEntity) const
1613 {
1614   std::vector<Slvs_Constraint>::const_iterator anIter = myConstraints.begin();
1615   for (; anIter != myConstraints.end(); anIter++)
1616     if ((anIter->type == SLVS_C_HORIZONTAL || anIter->type == SLVS_C_VERTICAL) && 
1617         anIter->entityA == theEntity)
1618       return true;
1619   return false;
1620 }
1621
1622 bool SolveSpaceSolver_Storage::isUsedInEqual(
1623     const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const
1624 {
1625   // Check the entity is used in Equal constraint
1626   std::vector<Slvs_Constraint>::const_iterator anEqIter = myConstraints.begin();
1627   for (; anEqIter != myConstraints.end(); anEqIter++)
1628     if ((anEqIter->type == SLVS_C_EQUAL_LENGTH_LINES ||
1629          anEqIter->type == SLVS_C_EQUAL_LINE_ARC_LEN ||
1630          anEqIter->type == SLVS_C_EQUAL_RADIUS) &&
1631        (anEqIter->entityA == theEntity || anEqIter->entityB == theEntity)) {
1632       theEqual = *anEqIter;
1633       return true;
1634     }
1635   return false;
1636 }
1637
1638 ////bool SolveSpaceSolver_Storage::isNeedToResolve()
1639 ////{
1640 ////  if (myConstraints.empty())
1641 ////    return false;
1642 ////
1643 ////  if (!myNeedToResolve) {
1644 ////    // Verify the updated parameters are used in constraints
1645 ////    std::set<Slvs_hEntity> aPoints;
1646 ////    std::vector<Slvs_Entity>::const_iterator anEntIt = myEntities.begin();
1647 ////    for (; anEntIt != myEntities.end(); ++anEntIt) {
1648 ////      for (int i = 0; i < 4 && anEntIt->param[i] != SLVS_E_UNKNOWN; ++i)
1649 ////        if (myUpdatedParameters.find(anEntIt->param[i]) != myUpdatedParameters.end()) {
1650 ////          aPoints.insert(anEntIt->h);
1651 ////          break;
1652 ////        }
1653 ////    }
1654 ////    std::set<Slvs_hEntity> anEntities = aPoints;
1655 ////    for (anEntIt = myEntities.begin(); anEntIt != myEntities.end(); ++anEntIt) {
1656 ////      for (int i = 0; i < 4 && anEntIt->point[i] != SLVS_E_UNKNOWN; ++i)
1657 ////        if (aPoints.find(anEntIt->point[i]) != aPoints.end()) {
1658 ////          anEntities.insert(anEntIt->h);
1659 ////          break;
1660 ////        }
1661 ////    }
1662 ////
1663 ////    std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
1664 ////    for (; aCIt != myConstraints.end() && !myNeedToResolve; ++aCIt) {
1665 ////      Slvs_hEntity anAttrs[6] =
1666 ////        {aCIt->ptA, aCIt->ptB, aCIt->entityA, aCIt->entityB, aCIt->entityC, aCIt->entityD};
1667 ////      for (int i = 0; i < 6; i++)
1668 ////        if (anAttrs[i] != SLVS_E_UNKNOWN && anEntities.find(anAttrs[i]) != anEntities.end()) {
1669 ////          myNeedToResolve = true;
1670 ////          break;
1671 ////        }
1672 ////    }
1673 ////  }
1674 ////
1675 ////  myUpdatedParameters.clear();
1676 ////  return myNeedToResolve;
1677 ////}
1678
1679 void SolveSpaceSolver_Storage::setTemporary(ConstraintPtr theConstraint)
1680 {
1681   // TODO
1682 }
1683
1684 bool SolveSpaceSolver_Storage::removeConstraint(ConstraintPtr theConstraint)
1685 {
1686   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::iterator
1687       aFound = myConstraintMap.find(theConstraint);
1688   if (aFound == myConstraintMap.end())
1689     return true; // no constraint, already deleted
1690
1691   // Remove constraint
1692   std::list<ConstraintWrapperPtr> aConstrList = aFound->second;
1693   myConstraintMap.erase(aFound);
1694   // Remove SolveSpace constraints
1695   bool isFullyRemoved = true;
1696   std::list<ConstraintWrapperPtr>::iterator anIt = aConstrList.begin();
1697   while (anIt != aConstrList.end()) {
1698     if (remove(*anIt)) {
1699       std::list<ConstraintWrapperPtr>::iterator aRemoveIt = anIt++;
1700       aConstrList.erase(aRemoveIt);
1701     } else {
1702       isFullyRemoved = false;
1703       ++anIt;
1704     }
1705   }
1706   if (!isFullyRemoved)
1707     myConstraintMap[theConstraint] = aConstrList;
1708   return isFullyRemoved;
1709 }
1710
1711 template <class ENT_TYPE>
1712 static bool isUsed(ConstraintWrapperPtr theConstraint, ENT_TYPE theEntity)
1713 {
1714   std::list<EntityWrapperPtr>::const_iterator anEntIt = theConstraint->entities().begin();
1715   for (; anEntIt != theConstraint->entities().end(); ++anEntIt)
1716     if (std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anEntIt)->isBase(theEntity))
1717       return true;
1718   return false;
1719 }
1720
1721 static bool isUsed(EntityWrapperPtr theFeature, AttributePtr theSubEntity)
1722 {
1723   std::list<EntityWrapperPtr>::const_iterator aSubIt = theFeature->subEntities().begin();
1724   for (; aSubIt != theFeature->subEntities().end(); ++aSubIt)
1725     if (std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*aSubIt)->isBase(theSubEntity))
1726       return true;
1727   return false;
1728 }
1729
1730 bool SolveSpaceSolver_Storage::isUsed(FeaturePtr theFeature) const
1731 {
1732   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
1733       aCIt = myConstraintMap.begin();
1734   std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
1735   for (; aCIt != myConstraintMap.end(); ++aCIt)
1736     for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
1737       if (::isUsed(*aCWIt, theFeature))
1738         return true;
1739   // check attributes
1740   std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
1741   std::list<AttributePtr>::const_iterator anIt = anAttrList.begin();
1742   for (; anIt != anAttrList.end(); ++anIt)
1743     if (isUsed(*anIt))
1744       return true;
1745   return false;
1746 }
1747
1748 bool SolveSpaceSolver_Storage::isUsed(AttributePtr theAttribute) const
1749 {
1750   AttributePtr anAttribute = theAttribute;
1751   AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
1752   if (aRefAttr) {
1753     if (aRefAttr->isObject())
1754       return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
1755     else
1756       anAttribute = aRefAttr->attr();
1757   }
1758
1759   std::map<ConstraintPtr, std::list<ConstraintWrapperPtr> >::const_iterator
1760       aCIt = myConstraintMap.begin();
1761   std::list<ConstraintWrapperPtr>::const_iterator aCWIt;
1762   for (; aCIt != myConstraintMap.end(); ++aCIt)
1763     for (aCWIt = aCIt->second.begin(); aCWIt != aCIt->second.end(); ++aCWIt)
1764       if (::isUsed(*aCWIt, anAttribute))
1765         return true;
1766   return false;
1767 }
1768
1769
1770 bool SolveSpaceSolver_Storage::removeEntity(FeaturePtr theFeature)
1771 {
1772   std::map<FeaturePtr, EntityWrapperPtr>::iterator aFound = myFeatureMap.find(theFeature);
1773   if (aFound == myFeatureMap.end())
1774     return false; // feature not found, nothing to delete
1775
1776   // Check the feature is not used by constraints
1777   if (isUsed(theFeature))
1778     return false; // the feature is used, don't remove it
1779
1780   // Remove feature
1781   EntityWrapperPtr anEntity = aFound->second;
1782   myFeatureMap.erase(aFound);
1783   if (remove(anEntity))
1784     return true;
1785   // feature is not removed, revert operation
1786   myFeatureMap[theFeature] = anEntity;
1787   return false;
1788 }
1789
1790 bool SolveSpaceSolver_Storage::removeEntity(AttributePtr theAttribute)
1791 {
1792   std::map<AttributePtr, EntityWrapperPtr>::iterator aFound = myAttributeMap.find(theAttribute);
1793   if (aFound == myAttributeMap.end())
1794     return false; // attribute not found, nothing to delete
1795
1796   // Check the attribute is not used by constraints
1797   if (isUsed(theAttribute))
1798     return false; // the attribute is used, don't remove it
1799   // Check the attribute is not used by other features
1800   std::map<FeaturePtr, EntityWrapperPtr>::const_iterator aFIt = myFeatureMap.begin();
1801   for (; aFIt != myFeatureMap.end(); ++aFIt)
1802     if (::isUsed(aFIt->second, theAttribute)) // the attribute is used, don't remove it
1803       return false;
1804
1805   // Remove attribute
1806   EntityWrapperPtr anEntity = aFound->second;
1807   myAttributeMap.erase(aFound);
1808   if (remove(anEntity))
1809     return true;
1810   // attribute is not removed, revert operation
1811   myAttributeMap[theAttribute] = anEntity;
1812   return false;
1813 }
1814
1815
1816 bool SolveSpaceSolver_Storage::remove(ConstraintWrapperPtr theConstraint)
1817 {
1818   std::shared_ptr<SolveSpaceSolver_ConstraintWrapper> aConstraint =
1819       std::dynamic_pointer_cast<SolveSpaceSolver_ConstraintWrapper>(theConstraint);
1820
1821   // verify whether the constraint has duplicated
1822   SameConstraintMap::iterator anEqIt = myEqualConstraints.begin();
1823   for (; anEqIt != myEqualConstraints.end(); ++anEqIt)
1824     if (anEqIt->find(aConstraint) != anEqIt->end()) {
1825       anEqIt->erase(aConstraint);
1826       break;
1827     }
1828   if (anEqIt != myEqualConstraints.end())
1829     return true;
1830
1831   bool isFullyRemoved = removeConstraint((Slvs_hConstraint)aConstraint->id());
1832
1833   std::list<EntityWrapperPtr>::const_iterator anIt = aConstraint->entities().begin();
1834   for (; anIt != aConstraint->entities().end(); ++anIt) {
1835     std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity = 
1836         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anIt);
1837     FeaturePtr aBaseFeature = anEntity->baseFeature();
1838     if (aBaseFeature)
1839       isFullyRemoved = removeEntity(aBaseFeature) && isFullyRemoved;
1840     else
1841       isFullyRemoved = removeEntity(anEntity->baseAttribute()) && isFullyRemoved;
1842   }
1843
1844   return isFullyRemoved;
1845 }
1846
1847 bool SolveSpaceSolver_Storage::remove(EntityWrapperPtr theEntity)
1848 {
1849   std::shared_ptr<SolveSpaceSolver_EntityWrapper> anEntity = 
1850         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(theEntity);
1851   bool isFullyRemoved = removeEntity((Slvs_hEntity)anEntity->id());
1852
1853   std::list<EntityWrapperPtr>::const_iterator anEntIt = anEntity->subEntities().begin();
1854   for (; anEntIt != anEntity->subEntities().end(); ++anEntIt) {
1855     std::shared_ptr<SolveSpaceSolver_EntityWrapper> aSubEntity = 
1856         std::dynamic_pointer_cast<SolveSpaceSolver_EntityWrapper>(*anEntIt);
1857     FeaturePtr aBaseFeature = aSubEntity->baseFeature();
1858     if (aBaseFeature)
1859       isFullyRemoved = removeEntity(aBaseFeature) && isFullyRemoved;
1860     else
1861       isFullyRemoved = removeEntity(aSubEntity->baseAttribute()) && isFullyRemoved;
1862   }
1863
1864   std::list<ParameterWrapperPtr>::const_iterator aParIt = anEntity->parameters().begin();
1865   for (; aParIt != anEntity->parameters().end(); ++aParIt)
1866     isFullyRemoved = remove(*aParIt) && isFullyRemoved;
1867   return isFullyRemoved;
1868 }
1869
1870 bool SolveSpaceSolver_Storage::remove(ParameterWrapperPtr theParameter)
1871 {
1872   return removeParameter((Slvs_hParam)theParameter->id());
1873 }
1874
1875
1876 void SolveSpaceSolver_Storage::refresh(bool theFixedOnly) const
1877 {
1878   blockEvents(true);
1879
1880   std::map<AttributePtr, EntityWrapperPtr>::const_iterator anIt = myAttributeMap.begin();
1881   std::list<ParameterWrapperPtr> aParams;
1882   std::list<ParameterWrapperPtr>::const_iterator aParIt;
1883   for (; anIt != myAttributeMap.end(); ++anIt) {
1884     // update parameter wrappers and obtain values of attributes
1885     aParams = anIt->second->parameters();
1886     double aCoords[3];
1887     bool isUpd[3] = {false};
1888     int i = 0;
1889     for (aParIt = aParams.begin(); i < 3 && aParIt != aParams.end(); ++aParIt, ++i) {
1890       std::shared_ptr<SolveSpaceSolver_ParameterWrapper> aWrapper = 
1891           std::dynamic_pointer_cast<SolveSpaceSolver_ParameterWrapper>(*aParIt);
1892       if (!theFixedOnly || aWrapper->group() == GID_OUTOFGROUP || aWrapper->isParametric()) {
1893         aWrapper->changeParameter().val = getParameter((Slvs_hParam)aWrapper->id()).val;
1894         aCoords[i] = aWrapper->value();
1895         isUpd[i] = true;
1896       }
1897     }
1898     if (!isUpd[0] && !isUpd[1] && !isUpd[2])
1899       continue; // nothing is updated
1900
1901     std::shared_ptr<GeomDataAPI_Point2D> aPoint2D =
1902         std::dynamic_pointer_cast<GeomDataAPI_Point2D>(anIt->first);
1903     if (aPoint2D) {
1904       if ((isUpd[0] && fabs(aPoint2D->x() - aCoords[0]) > tolerance) ||
1905           (isUpd[1] && fabs(aPoint2D->y() - aCoords[1]) > tolerance)) {
1906         if (!isUpd[0]) aCoords[0] = aPoint2D->x();
1907         if (!isUpd[1]) aCoords[1] = aPoint2D->y();
1908         aPoint2D->setValue(aCoords[0], aCoords[1]);
1909       }
1910       continue;
1911     }
1912     AttributeDoublePtr aScalar = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(anIt->first);
1913     if (aScalar) {
1914       if (isUpd[0] && fabs(aScalar->value() - aCoords[0]) > tolerance)
1915         aScalar->setValue(aCoords[0]);
1916       continue;
1917     }
1918     std::shared_ptr<GeomDataAPI_Point> aPoint =
1919         std::dynamic_pointer_cast<GeomDataAPI_Point>(anIt->first);
1920     if (aPoint) {
1921       if ((isUpd[0] && fabs(aPoint->x() - aCoords[0]) > tolerance) ||
1922           (isUpd[1] && fabs(aPoint->y() - aCoords[1]) > tolerance) ||
1923           (isUpd[2] && fabs(aPoint->z() - aCoords[2]) > tolerance))
1924         if (!isUpd[0]) aCoords[0] = aPoint->x();
1925         if (!isUpd[1]) aCoords[1] = aPoint->y();
1926         if (!isUpd[2]) aCoords[2] = aPoint->z();
1927         aPoint->setValue(aCoords[0], aCoords[1], aCoords[2]);
1928       continue;
1929     }
1930   }
1931
1932   blockEvents(false);
1933 }
1934
1935 void SolveSpaceSolver_Storage::verifyFixed()
1936 {
1937   std::map<AttributePtr, EntityWrapperPtr>::iterator anAttrIt = myAttributeMap.begin();
1938   for (; anAttrIt != myAttributeMap.end(); ++anAttrIt) {
1939     const std::list<ParameterWrapperPtr>& aParameters = anAttrIt->second->parameters();
1940     std::list<ParameterWrapperPtr>::const_iterator aParIt = aParameters.begin();
1941     for (; aParIt != aParameters.end(); ++aParIt)
1942       if ((*aParIt)->group() == GID_OUTOFGROUP) {
1943         Slvs_Param aParam = getParameter((Slvs_hParam)(*aParIt)->id());
1944         if (aParam.group != (Slvs_hParam)GID_OUTOFGROUP) {
1945           aParam.group = (Slvs_hParam)GID_OUTOFGROUP;
1946           updateParameter(aParam);
1947         }
1948       }
1949   }
1950 }
1951
1952
1953 void SolveSpaceSolver_Storage::adjustArc(const Slvs_Entity& theArc)
1954 {
1955   double anArcPoints[3][2];
1956   double aDist[3] = {0.0};
1957   bool isFixed[3] = {false};
1958   for (int i = 0; i < 3; ++i) {
1959     Slvs_Entity aPoint = getEntity(theArc.point[i]);
1960     isFixed[i] = (aPoint.group != (Slvs_hGroup)myGroupID);
1961     anArcPoints[i][0] = getParameter(aPoint.param[0]).val;
1962     anArcPoints[i][1] = getParameter(aPoint.param[1]).val;
1963     if (i > 0) {
1964       anArcPoints[i][0] -= anArcPoints[0][0];
1965       anArcPoints[i][1] -= anArcPoints[0][1];
1966       aDist[i] = sqrt(anArcPoints[i][0] * anArcPoints[i][0] + 
1967                       anArcPoints[i][1] * anArcPoints[i][1]);
1968     }
1969   }
1970
1971   if (fabs(aDist[1] - aDist[2]) < tolerance)
1972     return;
1973
1974   int anInd = 2;
1975   while (anInd > 0 && isFixed[anInd])
1976     --anInd;
1977   if (anInd < 1)
1978     return; // adjust only start or end point of the arc
1979
1980   anArcPoints[anInd][0] /= aDist[anInd];
1981   anArcPoints[anInd][1] /= aDist[anInd];
1982
1983   Slvs_Entity aPoint = getEntity(theArc.point[anInd]);
1984   for (int i = 0; i < 2; ++i) {
1985     Slvs_Param aParam = getParameter(aPoint.param[i]);
1986     aParam.val = anArcPoints[0][i] + aDist[3-anInd] * anArcPoints[anInd][i];
1987     updateParameter(aParam);
1988   }
1989 }
1990
1991
1992
1993
1994
1995
1996
1997 // ========================================================
1998 // =========      Auxiliary functions       ===============
1999 // ========================================================
2000
2001 template<typename T>
2002 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
2003 {
2004   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
2005   int aVecSize = theEntities.size();
2006   if (theEntities.empty())
2007     return 1;
2008   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
2009     aResIndex--;
2010   while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
2011     aResIndex++;
2012   if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
2013     aResIndex = aVecSize;
2014   return aResIndex;
2015 }
2016
2017 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
2018 {
2019   return fabs(theParam1.val - theParam2.val) > tolerance;
2020 }
2021
2022 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
2023 {
2024   int i = 0;
2025   for (; theEntity1.param[i] != 0 && i < 4; i++)
2026     if (theEntity1.param[i] != theEntity2.param[i])
2027       return true;
2028   i = 0;
2029   for (; theEntity1.point[i] != 0 && i < 4; i++)
2030     if (theEntity1.point[i] != theEntity2.point[i])
2031       return true;
2032   return false;
2033 }
2034
2035 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
2036 {
2037   return theConstraint1.ptA != theConstraint2.ptA ||
2038          theConstraint1.ptB != theConstraint2.ptB ||
2039          theConstraint1.entityA != theConstraint2.entityA ||
2040          theConstraint1.entityB != theConstraint2.entityB ||
2041          theConstraint1.entityC != theConstraint2.entityC ||
2042          theConstraint1.entityD != theConstraint2.entityD ||
2043          fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;
2044 }