Salome HOME
c599c3fbba771667a9f06f28fb18be0afc3ade55
[modules/shaper.git] / src / SketchSolver / SketchSolver_Storage.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    SketchSolver_Storage.cpp
4 // Created: 18 Mar 2015
5 // Author:  Artem ZHIDKOV
6
7 #include <SketchSolver_Storage.h>
8
9 #include <GeomAPI_Pnt2d.h>
10 #include <GeomAPI_XY.h>
11 #include <math.h>
12
13 /** \brief Search the entity/parameter with specified ID in the list of elements
14  *  \param[in] theEntityID unique ID of the element
15  *  \param[in] theEntities list of elements
16  *  \return position of the found element or -1 if the element is not found
17  */
18 template<typename T>
19 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
20
21 /// \brief Compare two parameters to be different
22 static bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2);
23 /// \brief Compare two entities to be different
24 static bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2);
25 /// \brief Compare two constriants to be different
26 static bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2);
27
28
29 SketchSolver_Storage::SketchSolver_Storage()
30   : myParamMaxID(SLVS_E_UNKNOWN),
31     myEntityMaxID(SLVS_E_UNKNOWN),
32     myConstrMaxID(SLVS_C_UNKNOWN),
33     myFixed(SLVS_E_UNKNOWN),
34     myNeedToResolve(false),
35     myDuplicatedConstraint(false)
36 {
37 }
38
39 Slvs_hParam SketchSolver_Storage::addParameter(const Slvs_Param& theParam)
40 {
41   if (theParam.h > 0 && theParam.h <= myParamMaxID) {
42     // parameter is already used, rewrite it
43     return updateParameter(theParam);
44   }
45
46   Slvs_Param aParam = theParam;
47   if (aParam.h > myParamMaxID)
48     myParamMaxID = aParam.h;
49   else
50     aParam.h = ++myParamMaxID;
51   myParameters.push_back(aParam);
52   myNeedToResolve = true;
53   return aParam.h;
54 }
55
56 Slvs_hParam SketchSolver_Storage::updateParameter(const Slvs_Param& theParam)
57 {
58   if (theParam.h > 0 && theParam.h <= myParamMaxID) {
59     // parameter already used, rewrite it
60     int aPos = Search(theParam.h, myParameters);
61     if (aPos >= 0 && aPos < (int)myParameters.size()) {
62       if (IsNotEqual(myParameters[aPos], theParam))
63         myUpdatedParameters.insert(theParam.h);
64       myParameters[aPos] = theParam;
65       return theParam.h;
66     }
67   }
68
69   // Parameter is not found, add new one
70   Slvs_Param aParam = theParam;
71   aParam.h = 0;
72   return addParameter(aParam);
73 }
74
75 bool SketchSolver_Storage::removeParameter(const Slvs_hParam& theParamID)
76 {
77   int aPos = Search(theParamID, myParameters);
78   if (aPos >= 0 && aPos < (int)myParameters.size()) {
79     // Firstly, search the parametes is not used elsewhere
80     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
81     for (; anEntIter != myEntities.end(); anEntIter++) {
82       for (int i = 0; i < 4; i++)
83         if (anEntIter->param[i] == theParamID)
84           return false;
85     }
86     // Remove parameter
87     myParameters.erase(myParameters.begin() + aPos);
88     myParamMaxID = myParameters.empty() ? SLVS_E_UNKNOWN : myParameters.back().h;
89     myNeedToResolve = true;
90     myRemovedParameters.insert(theParamID);
91     return true;
92   }
93   return false;
94 }
95
96 const Slvs_Param& SketchSolver_Storage::getParameter(const Slvs_hParam& theParamID) const
97 {
98   int aPos = Search(theParamID, myParameters);
99   if (aPos >= 0 && aPos < (int)myParameters.size())
100     return myParameters[aPos];
101
102   // Parameter is not found, return empty object
103   static Slvs_Param aDummy;
104   aDummy.h = 0;
105   return aDummy;
106 }
107
108
109 Slvs_hEntity SketchSolver_Storage::addEntity(const Slvs_Entity& theEntity)
110 {
111   if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
112     // Entity is already used, rewrite it
113     return updateEntity(theEntity);
114   }
115
116   Slvs_Entity aEntity = theEntity;
117   if (aEntity.h > myEntityMaxID)
118     myEntityMaxID = aEntity.h;
119   else
120     aEntity.h = ++myEntityMaxID;
121   myEntities.push_back(aEntity);
122   myNeedToResolve = true;
123   return aEntity.h;
124 }
125
126 Slvs_hEntity SketchSolver_Storage::updateEntity(const Slvs_Entity& theEntity)
127 {
128   if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
129     // Entity already used, rewrite it
130     int aPos = Search(theEntity.h, myEntities);
131     if (aPos >= 0 && aPos < (int)myEntities.size()) {
132       myNeedToResolve = myNeedToResolve || IsNotEqual(myEntities[aPos], theEntity);
133       myEntities[aPos] = theEntity;
134       return theEntity.h;
135     }
136   }
137
138   // Entity is not found, add new one
139   Slvs_Entity aEntity = theEntity;
140   aEntity.h = 0;
141   return addEntity(aEntity);
142 }
143
144 bool SketchSolver_Storage::removeEntity(const Slvs_hEntity& theEntityID)
145 {
146   bool aResult = true;
147   int aPos = Search(theEntityID, myEntities);
148   if (aPos >= 0 && aPos < (int)myEntities.size()) {
149     // Firstly, check the entity and its attributes is not used elsewhere
150     std::set<Slvs_hEntity> anEntAndSubs;
151     anEntAndSubs.insert(theEntityID);
152     for (int i = 0; i < 4; i++)
153       if (myEntities[aPos].point[i] != SLVS_E_UNKNOWN)
154         anEntAndSubs.insert(myEntities[aPos].point[i]);
155
156     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
157     for (; anEntIter != myEntities.end(); anEntIter++) {
158       for (int i = 0; i < 4; i++)
159         if (anEntAndSubs.find(anEntIter->point[i]) != anEntAndSubs.end())
160           return false;
161       if (anEntAndSubs.find(anEntIter->distance) != anEntAndSubs.end())
162         return false;
163     }
164     std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
165     for (; aConstrIter != myConstraints.end(); aConstrIter++) {
166       Slvs_hEntity anEntIDs[6] = {aConstrIter->ptA, aConstrIter->ptB,
167           aConstrIter->entityA, aConstrIter->entityB,
168           aConstrIter->entityC, aConstrIter->entityD};
169       for (int i = 0; i < 6; i++)
170         if (anEntAndSubs.find(anEntIDs[i]) != anEntAndSubs.end())
171           return false;
172     }
173     // The entity is not used, remove it and its parameters
174     Slvs_Entity anEntity = myEntities[aPos];
175     myEntities.erase(myEntities.begin() + aPos);
176     myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
177     if (anEntity.distance != SLVS_E_UNKNOWN)
178       aResult = aResult && removeParameter(anEntity.distance);
179     for (int i = 0; i < 4; i++)
180       if (anEntity.param[i] != SLVS_E_UNKNOWN)
181         aResult = removeParameter(anEntity.param[i]) && aResult;
182     for (int i = 0; i < 4; i++)
183       if (anEntity.point[i] != SLVS_E_UNKNOWN)
184         aResult = removeEntity(anEntity.point[i]) && aResult;
185     myNeedToResolve = true;
186     myRemovedEntities.insert(theEntityID);
187     if (anEntity.type == SLVS_E_POINT_IN_2D || anEntity.type == SLVS_E_POINT_IN_3D)
188       removeCoincidentPoint(theEntityID);
189   }
190   return aResult;
191 }
192
193 void SketchSolver_Storage::removeUnusedEntities()
194 {
195   std::set<Slvs_hEntity> anUnusedEntities;
196   std::vector<Slvs_Entity>::const_iterator aEIt = myEntities.begin();
197   for (; aEIt != myEntities.end(); ++aEIt) {
198     if (aEIt->h == aEIt->wrkpl) {
199       // don't remove workplane
200       anUnusedEntities.erase(aEIt->point[0]);
201       anUnusedEntities.erase(aEIt->normal);
202       continue;
203     }
204     anUnusedEntities.insert(aEIt->h);
205   }
206
207   std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
208   for (; aCIt != myConstraints.end(); ++aCIt) {
209     Slvs_hEntity aSubs[6] = {
210         aCIt->entityA, aCIt->entityB,
211         aCIt->entityC, aCIt->entityD,
212         aCIt->ptA,     aCIt->ptB};
213     for (int i = 0; i < 6; i++) {
214       if (aSubs[i] != SLVS_E_UNKNOWN) {
215         anUnusedEntities.erase(aSubs[i]);
216         int aPos = Search(aSubs[i], myEntities);
217         if (aPos >= 0 && aPos < (int)myEntities.size()) {
218           for (int j = 0; j < 4; j++)
219             if (myEntities[aPos].point[j] != SLVS_E_UNKNOWN)
220               anUnusedEntities.erase(myEntities[aPos].point[j]);
221           if (myEntities[aPos].distance != SLVS_E_UNKNOWN)
222             anUnusedEntities.erase(myEntities[aPos].distance);
223         }
224       }
225     }
226   }
227
228   std::set<Slvs_hEntity>::const_iterator anEntIt = anUnusedEntities.begin();
229   while (anEntIt != anUnusedEntities.end()) {
230     int aPos = Search(*anEntIt, myEntities);
231     if (aPos < 0 && aPos >= (int)myEntities.size())
232       continue;
233     Slvs_Entity anEntity = myEntities[aPos];
234     // Remove entity if and only if all its parameters unused
235     bool isUsed = false;
236     if (anEntity.distance != SLVS_E_UNKNOWN && 
237       anUnusedEntities.find(anEntity.distance) == anUnusedEntities.end())
238       isUsed = true;
239     for (int i = 0; i < 4 && !isUsed; i++)
240       if (anEntity.point[i] != SLVS_E_UNKNOWN &&
241           anUnusedEntities.find(anEntity.point[i]) == anUnusedEntities.end())
242         isUsed = true;
243     if (isUsed) {
244       anUnusedEntities.erase(anEntity.distance);
245       for (int i = 0; i < 4; i++)
246         if (anEntity.point[i] != SLVS_E_UNKNOWN)
247           anUnusedEntities.erase(anEntity.point[i]);
248       std::set<Slvs_hEntity>::iterator aRemoveIt = anEntIt++;
249       anUnusedEntities.erase(aRemoveIt);
250       continue;
251     }
252     ++anEntIt;
253   }
254
255   for (anEntIt = anUnusedEntities.begin(); anEntIt != anUnusedEntities.end(); ++anEntIt) {
256     int aPos = Search(*anEntIt, myEntities);
257     if (aPos >= 0 && aPos < (int)myEntities.size()) {
258       // Remove entity and its parameters
259       Slvs_Entity anEntity = myEntities[aPos];
260       myEntities.erase(myEntities.begin() + aPos);
261       myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
262       if (anEntity.distance != SLVS_E_UNKNOWN)
263         removeParameter(anEntity.distance);
264       for (int i = 0; i < 4; i++)
265         if (anEntity.param[i] != SLVS_E_UNKNOWN)
266           removeParameter(anEntity.param[i]);
267       for (int i = 0; i < 4; i++)
268         if (anEntity.point[i] != SLVS_E_UNKNOWN)
269           removeEntity(anEntity.point[i]);
270       myRemovedEntities.insert(*anEntIt);
271       if (anEntity.type == SLVS_E_POINT_IN_2D || anEntity.type == SLVS_E_POINT_IN_3D)
272         removeCoincidentPoint(*anEntIt);
273     }
274   }
275
276   if (!anUnusedEntities.empty())
277     myNeedToResolve = true;
278 }
279
280 bool SketchSolver_Storage::isUsedByConstraints(const Slvs_hEntity& theEntityID) const
281 {
282   std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
283   for (; aCIt != myConstraints.end(); ++aCIt) {
284     Slvs_hEntity aSubs[6] = {
285         aCIt->entityA, aCIt->entityB,
286         aCIt->entityC, aCIt->entityD,
287         aCIt->ptA,     aCIt->ptB};
288     for (int i = 0; i < 6; i++)
289       if (aSubs[i] != SLVS_E_UNKNOWN && aSubs[i] == theEntityID)
290         return true;
291   }
292   return false;
293 }
294
295 const Slvs_Entity& SketchSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
296 {
297   int aPos = Search(theEntityID, myEntities);
298   if (aPos >= 0 && aPos < (int)myEntities.size())
299     return myEntities[aPos];
300
301   // Entity is not found, return empty object
302   static Slvs_Entity aDummy;
303   aDummy.h = SLVS_E_UNKNOWN;
304   return aDummy;
305 }
306
307 Slvs_hEntity SketchSolver_Storage::copyEntity(const Slvs_hEntity& theCopied)
308 {
309   int aPos = Search(theCopied, myEntities);
310   if (aPos < 0 || aPos >= (int)myEntities.size())
311     return SLVS_E_UNKNOWN;
312
313   Slvs_Entity aCopy = myEntities[aPos];
314   aCopy.h = SLVS_E_UNKNOWN;
315   int i = 0;
316   while (aCopy.point[i] != SLVS_E_UNKNOWN) {
317     aCopy.point[i] = copyEntity(aCopy.point[i]);
318     i++;
319   }
320   if (aCopy.param[0] != SLVS_E_UNKNOWN) {
321     aPos = Search(aCopy.param[0], myParameters);
322     i = 0;
323     while (aCopy.param[i] != SLVS_E_UNKNOWN) {
324       Slvs_Param aNewParam = myParameters[aPos];
325       aNewParam.h = SLVS_E_UNKNOWN;
326       aCopy.param[i] = addParameter(aNewParam);
327       i++;
328       aPos++;
329     }
330   }
331   return addEntity(aCopy);
332 }
333
334 void SketchSolver_Storage::copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo)
335 {
336   int aPosFrom = Search(theFrom, myEntities);
337   int aPosTo = Search(theTo, myEntities);
338   if (aPosFrom < 0 || aPosFrom >= (int)myEntities.size() || 
339       aPosTo < 0 || aPosTo >= (int)myEntities.size())
340     return;
341
342   Slvs_Entity aEntFrom = myEntities[aPosFrom];
343   Slvs_Entity aEntTo = myEntities[aPosTo];
344   int i = 0;
345   while (aEntFrom.point[i] != SLVS_E_UNKNOWN) {
346     copyEntity(aEntFrom.point[i], aEntTo.point[i]);
347     i++;
348   }
349   if (aEntFrom.param[0] != SLVS_E_UNKNOWN) {
350     aPosFrom = Search(aEntFrom.param[0], myParameters);
351     aPosTo = Search(aEntTo.param[0], myParameters);
352     i = 0;
353     while (aEntFrom.param[i] != SLVS_E_UNKNOWN) {
354       myParameters[aPosTo++].val = myParameters[aPosFrom++].val;
355       i++;
356     }
357   }
358 }
359
360
361 bool SketchSolver_Storage::isPointFixed(
362     const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate) const
363 {
364   // Search the set of coincident points
365   std::set<Slvs_hEntity> aCoincident;
366   aCoincident.insert(thePointID);
367   std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
368   for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
369     if (aCPIter->find(thePointID) != aCPIter->end()) {
370       aCoincident = *aCPIter;
371       break;
372     }
373
374   // Search the Rigid constraint
375   std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
376   for (; aConstrIter != myConstraints.end(); aConstrIter++)
377     if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
378         aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
379       theFixed = aConstrIter->h;
380       return true;
381     }
382
383   if (theAccurate) {
384     // Try to find the fixed entity which uses such point or its coincidence
385     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
386     for (; anEntIter != myEntities.end(); anEntIter++) {
387       for (int i = 0; i < 4; i++) {
388         Slvs_hEntity aPt = anEntIter->point[i];
389         if (aPt != SLVS_E_UNKNOWN &&
390             (aPt == thePointID || aCoincident.find(aPt) != aCoincident.end())) {
391           if (isEntityFixed(anEntIter->h, true))
392             return true;
393         }
394       }
395     }
396   }
397   return SLVS_E_UNKNOWN;
398 }
399
400 bool SketchSolver_Storage::isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate) const
401 {
402   int aPos = Search(theEntityID, myEntities);
403   if (aPos < 0 || aPos >= (int)myEntities.size())
404     return false;
405
406   // Firstly, find how many points are under Rigid constraint
407   int aNbFixed = 0;
408   for (int i = 0; i < 4; i++) {
409     Slvs_hEntity aPoint = myEntities[aPos].point[i];
410     if (aPoint == SLVS_E_UNKNOWN)
411       continue;
412
413     std::set<Slvs_hEntity> aCoincident;
414     aCoincident.insert(aPoint);
415     std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
416     for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
417       if (aCPIter->find(aPoint) != aCPIter->end()) {
418         aCoincident = *aCPIter;
419         break;
420       }
421
422     // Search the Rigid constraint
423     std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
424     for (; aConstrIter != myConstraints.end(); aConstrIter++)
425       if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
426           aCoincident.find(aConstrIter->ptA) != aCoincident.end())
427         aNbFixed++;
428   }
429
430   std::list<Slvs_Constraint> aList;
431   std::list<Slvs_Constraint>::iterator anIt;
432   Slvs_hConstraint aTempID; // used in isPointFixed() method
433
434   if (myEntities[aPos].type == SLVS_E_LINE_SEGMENT) {
435     if (aNbFixed == 2)
436       return true;
437     else if (aNbFixed == 0 || !theAccurate)
438       return false;
439     // Additional check (the line may be fixed if it is used by different constraints):
440     // 1. The line is used in Equal constraint, another entity is fixed and there is a fixed point on line
441     aList = getConstraintsByType(SLVS_C_PT_ON_LINE);
442     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
443       if (anIt->entityA == theEntityID && isPointFixed(anIt->ptA, aTempID))
444         break;
445     if (anIt != aList.end()) {
446       aList = getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
447       aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
448       for (anIt = aList.begin(); anIt != aList.end(); anIt++)
449         if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
450           Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
451           if (isEntityFixed(anOther, false))
452             return true;
453         }
454     }
455     // 2. The line is used in Parallel/Perpendicular/Vertical/Horizontal and Length constraints
456     aList = getConstraintsByType(SLVS_C_PARALLEL);
457     aList.splice(aList.end(), getConstraintsByType(SLVS_C_PERPENDICULAR));
458     aList.splice(aList.end(), getConstraintsByType(SLVS_C_VERTICAL));
459     aList.splice(aList.end(), getConstraintsByType(SLVS_C_HORIZONTAL));
460     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
461       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
462         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
463         if (isEntityFixed(anOther, false))
464           break;
465       }
466     if (anIt != aList.end()) {
467       aList = getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
468       for (anIt = aList.begin(); anIt != aList.end(); anIt++)
469         if ((anIt->ptA == myEntities[aPos].point[0] && anIt->ptB == myEntities[aPos].point[1]) ||
470             (anIt->ptA == myEntities[aPos].point[1] && anIt->ptB == myEntities[aPos].point[0]))
471           return true;
472     }
473     // 3. Another verifiers ...
474   } else if (myEntities[aPos].type == SLVS_E_CIRCLE) {
475     if (aNbFixed == 0)
476       return false;
477     // Search for Diameter constraint
478     aList = getConstraintsByType(SLVS_C_DIAMETER);
479     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
480       if (anIt->entityA == theEntityID)
481         return true;
482     if (!theAccurate)
483       return false;
484     // Additional check (the circle may be fixed if it is used by different constraints):
485     // 1. The circle is used in Equal constraint and another entity is fixed
486     aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
487     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
488       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
489         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
490         if (isEntityFixed(anOther, false))
491           return true;
492       }
493     // 2. Another verifiers ...
494   } else if (myEntities[aPos].type == SLVS_E_ARC_OF_CIRCLE) {
495     if (aNbFixed > 2)
496       return true;
497     else if (aNbFixed <= 1)
498       return false;
499     // Search for Diameter constraint
500     aList = getConstraintsByType(SLVS_C_DIAMETER);
501     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
502       if (anIt->entityA == theEntityID)
503         return true;
504     if (!theAccurate)
505       return false;
506     // Additional check (the arc may be fixed if it is used by different constraints):
507     // 1. The arc is used in Equal constraint and another entity is fixed
508     aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
509     aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
510     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
511       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
512         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
513         if (isEntityFixed(anOther, false))
514           return true;
515       }
516     // 2. Another verifiers ...
517   }
518   return false;
519 }
520
521
522 Slvs_hConstraint SketchSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
523 {
524   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
525     // Constraint is already used, rewrite it
526     return updateConstraint(theConstraint);
527   }
528
529   Slvs_Constraint aConstraint = theConstraint;
530
531   // Find a constraint with same type uses same arguments to show user overconstraint situation
532   std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
533   for (; aCIt != myConstraints.end(); aCIt++) {
534     if (aConstraint.type != aCIt->type)
535       continue;
536     if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
537         aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
538         aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD)
539       myDuplicatedConstraint = true;
540   }
541
542   if (aConstraint.h > myConstrMaxID)
543     myConstrMaxID = aConstraint.h;
544   else
545     aConstraint.h = ++myConstrMaxID;
546   myConstraints.push_back(aConstraint);
547   myNeedToResolve = true;
548   if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
549     addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
550   return aConstraint.h;
551 }
552
553 Slvs_hConstraint SketchSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
554 {
555   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
556     // Constraint already used, rewrite it
557     int aPos = Search(theConstraint.h, myConstraints);
558     if (aPos >= 0 && aPos < (int)myConstraints.size()) {
559       myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
560       myConstraints[aPos] = theConstraint;
561       if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
562         addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
563       return theConstraint.h;
564     }
565   }
566
567   // Constraint is not found, add new one
568   Slvs_Constraint aConstraint = theConstraint;
569   aConstraint.h = 0;
570   return addConstraint(aConstraint);
571 }
572
573 bool SketchSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
574 {
575   bool aResult = true;
576   int aPos = Search(theConstraintID, myConstraints);
577   if (aPos >= 0 && aPos < (int)myConstraints.size()) {
578     Slvs_Constraint aConstraint = myConstraints[aPos];
579     myConstraints.erase(myConstraints.begin() + aPos);
580     myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
581     myNeedToResolve = true;
582     myRemovedConstraints.insert(theConstraintID);
583     // Remove all entities
584     Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
585         aConstraint.entityA, aConstraint.entityB,
586         aConstraint.entityC, aConstraint.entityD};
587     for (int i = 0; i < 6; i++)
588       if (anEntities[i] != SLVS_E_UNKNOWN)
589         aResult = removeEntity(anEntities[i]) && aResult;
590     // remove temporary fixed point, if available
591     if (myFixed == theConstraintID)
592       myFixed = SLVS_E_UNKNOWN;
593     if (myDuplicatedConstraint) {
594       // Check the duplicated constraints are still available
595       myDuplicatedConstraint = false;
596       std::vector<Slvs_Constraint>::const_iterator anIt1 = myConstraints.begin();
597       std::vector<Slvs_Constraint>::const_iterator anIt2 = myConstraints.begin();
598       for (; anIt1 != myConstraints.end() && !myDuplicatedConstraint; anIt1++)
599         for (anIt2 = anIt1+1; anIt2 != myConstraints.end() && !myDuplicatedConstraint; anIt2++) {
600           if (anIt1->type != anIt2->type)
601             continue;
602           if (anIt1->ptA == anIt2->ptA && anIt1->ptB == anIt2->ptB &&
603               anIt1->entityA == anIt2->entityA && anIt1->entityB == anIt2->entityB &&
604               anIt1->entityC == anIt2->entityC && anIt1->entityD == anIt2->entityD)
605             myDuplicatedConstraint = true;
606         }
607     }
608   }
609   return aResult;
610 }
611
612 const Slvs_Constraint& SketchSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
613 {
614   int aPos = Search(theConstraintID, myConstraints);
615   if (aPos >= 0 && aPos < (int)myConstraints.size())
616     return myConstraints[aPos];
617
618   // Constraint is not found, return empty object
619   static Slvs_Constraint aDummy;
620   aDummy.h = 0;
621   return aDummy;
622 }
623
624 std::list<Slvs_Constraint> SketchSolver_Storage::getConstraintsByType(int theConstraintType) const
625 {
626   std::list<Slvs_Constraint> aResult;
627   std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
628   for (; aCIter != myConstraints.end(); aCIter++)
629     if (aCIter->type == theConstraintType)
630       aResult.push_back(*aCIter);
631   return aResult;
632 }
633
634
635 void SketchSolver_Storage::addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID)
636 {
637   if (myFixed != SLVS_E_UNKNOWN)
638     return; // the point is already fixed
639   int aPos = Search(theConstraintID, myConstraints);
640   if (aPos >= 0 && aPos < (int)myConstraints.size())
641     myFixed = theConstraintID;
642 }
643
644 void SketchSolver_Storage::addTemporaryConstraint(const Slvs_hConstraint& theConstraintID)
645 {
646   myTemporaryConstraints.insert(theConstraintID);
647 }
648
649 void SketchSolver_Storage::removeTemporaryConstraints()
650 {
651   myTemporaryConstraints.clear();
652 }
653
654 int SketchSolver_Storage::deleteTemporaryConstraint()
655 {
656   if (myTemporaryConstraints.empty())
657     return 0;
658   // Search the point-on-line or a non-rigid constraint
659   std::set<Slvs_hConstraint>::iterator aCIt = myTemporaryConstraints.begin();
660   for (; aCIt != myTemporaryConstraints.end(); aCIt++) {
661     int aPos = Search(*aCIt, myConstraints);
662     if (aPos >= (int)myConstraints.size() || myConstraints[aPos].type != SLVS_C_WHERE_DRAGGED)
663       break;
664     std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
665     for (; anIt != myConstraints.end(); anIt++)
666       if (anIt->type == SLVS_C_PT_ON_LINE && anIt->ptA == myConstraints[aPos].ptA)
667         break;
668     if (anIt != myConstraints.end())
669       break;
670   }
671   if (aCIt == myTemporaryConstraints.end())
672     aCIt = myTemporaryConstraints.begin();
673   bool aNewFixed = (*aCIt == myFixed);
674   removeConstraint(*aCIt);
675   myTemporaryConstraints.erase(aCIt);
676   if (aNewFixed) {
677     for (aCIt = myTemporaryConstraints.begin(); aCIt != myTemporaryConstraints.end(); aCIt++) {
678       int aPos = Search(*aCIt, myConstraints);
679       if (myConstraints[aPos].type == SLVS_C_WHERE_DRAGGED) {
680         myFixed = *aCIt;
681         break;
682       }
683     }
684   }
685   return (int)myTemporaryConstraints.size();
686 }
687
688 bool SketchSolver_Storage::isTemporary(const Slvs_hConstraint& theConstraintID) const
689 {
690   return myTemporaryConstraints.find(theConstraintID) != myTemporaryConstraints.end();
691 }
692
693
694 void SketchSolver_Storage::getRemoved(
695     std::set<Slvs_hParam>& theParameters,
696     std::set<Slvs_hEntity>& theEntities,
697     std::set<Slvs_hConstraint>& theConstraints)
698 {
699   theParameters = myRemovedParameters;
700   theEntities = myRemovedEntities;
701   theConstraints = myRemovedConstraints;
702
703   myRemovedParameters.clear();
704   myRemovedEntities.clear();
705   myRemovedConstraints.clear();
706 }
707
708 void SketchSolver_Storage::initializeSolver(SketchSolver_Solver& theSolver)
709 {
710   theSolver.setParameters(myParameters.data(), (int)myParameters.size());
711   theSolver.setEntities(myEntities.data(), (int)myEntities.size());
712
713   // Copy constraints excluding the fixed one
714   std::vector<Slvs_Constraint> aConstraints = myConstraints;
715   if (myFixed != SLVS_E_UNKNOWN) {
716     Slvs_hEntity aFixedPoint = SLVS_E_UNKNOWN;
717     std::vector<Slvs_Constraint>::iterator anIt = aConstraints.begin();
718     for (; anIt != aConstraints.end(); anIt++)
719       if (anIt->h == myFixed) {
720         aFixedPoint = anIt->ptA;
721         aConstraints.erase(anIt);
722         break;
723       }
724     // set dragged parameters
725     int aPos = Search(aFixedPoint, myEntities);
726     theSolver.setDraggedParameters(myEntities[aPos].param);
727   }
728   theSolver.setConstraints(aConstraints.data(), (int)aConstraints.size());
729 }
730
731 void SketchSolver_Storage::addCoincidentPoints(
732     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
733 {
734   std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
735   std::vector< std::set<Slvs_hEntity> >::iterator aFoundIter = myCoincidentPoints.end(); // already found coincidence
736   bool isFound = false;
737   for (; aCIter != myCoincidentPoints.end(); aCIter++) {
738     bool isFirstFound = aCIter->find(thePoint1) != aCIter->end();
739     bool isSecondFound = aCIter->find(thePoint2) != aCIter->end();
740     isFound = isFound || isFirstFound || isSecondFound;
741     if (isFirstFound && isSecondFound)
742       break; // already coincident
743     else if (isFirstFound || isSecondFound) {
744       if (aFoundIter != myCoincidentPoints.end()) {
745         // merge two sets
746         aFoundIter->insert(aCIter->begin(), aCIter->end());
747         myCoincidentPoints.erase(aCIter);
748         break;
749       } else
750         aFoundIter = aCIter;
751       aCIter->insert(thePoint1);
752       aCIter->insert(thePoint2);
753     }
754   }
755   // coincident points not found
756   if (!isFound) {
757     std::set<Slvs_hEntity> aNewSet;
758     aNewSet.insert(thePoint1);
759     aNewSet.insert(thePoint2);
760     myCoincidentPoints.push_back(aNewSet);
761   }
762 }
763
764 void SketchSolver_Storage::removeCoincidentPoint(const Slvs_hEntity& thePoint)
765 {
766   std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
767   for (; aCIter != myCoincidentPoints.end(); aCIter++)
768     if (aCIter->find(thePoint) != aCIter->end()) {
769       aCIter->erase(thePoint);
770       if (aCIter->size() <= 1)
771         myCoincidentPoints.erase(aCIter);
772       break;
773     }
774 }
775
776 bool SketchSolver_Storage::isCoincident(
777     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
778 {
779   std::vector< std::set<Slvs_hEntity> >::const_iterator aCIter = myCoincidentPoints.begin();
780   for (; aCIter != myCoincidentPoints.end(); aCIter++)
781     if (aCIter->find(thePoint1) != aCIter->end() && aCIter->find(thePoint2) != aCIter->end())
782       return true;
783   return false;
784 }
785
786 bool SketchSolver_Storage::isEqual(
787     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
788 {
789   if (isCoincident(thePoint1, thePoint2))
790     return true;
791
792   // Precise checking of coincidence: verify that points have equal coordinates
793   int aEnt1Pos = Search(thePoint1, myEntities);
794   int aEnt2Pos = Search(thePoint2, myEntities);
795   if (aEnt1Pos >= 0 && aEnt1Pos < (int)myEntities.size() &&
796       aEnt2Pos >= 0 && aEnt2Pos < (int)myEntities.size()) {
797     double aDist[2];
798     int aParamPos;
799     for (int i = 0; i < 2; i++) {
800       aParamPos = Search(myEntities[aEnt1Pos].param[i], myParameters);
801       aDist[i] = myParameters[aParamPos].val;
802       aParamPos = Search(myEntities[aEnt2Pos].param[i], myParameters);
803       aDist[i] -= myParameters[aParamPos].val;
804     }
805     if (aDist[0] * aDist[0] + aDist[1] * aDist[1] < tolerance * tolerance)
806       return true;
807   }
808   return false;
809 }
810
811
812 std::vector<Slvs_hConstraint> SketchSolver_Storage::fixEntity(const Slvs_hEntity& theEntity)
813 {
814   std::vector<Slvs_hConstraint> aNewConstraints;
815
816   int aPos = Search(theEntity, myEntities);
817   if (aPos >= 0 && aPos < (int)myEntities.size()) {
818     switch (myEntities[aPos].type) {
819     case SLVS_E_POINT_IN_2D:
820     case SLVS_E_POINT_IN_3D:
821       fixPoint(myEntities[aPos], aNewConstraints);
822       break;
823     case SLVS_E_LINE_SEGMENT:
824       fixLine(myEntities[aPos], aNewConstraints);
825       break;
826     case SLVS_E_CIRCLE:
827       fixCircle(myEntities[aPos], aNewConstraints);
828       break;
829     case SLVS_E_ARC_OF_CIRCLE:
830       fixArc(myEntities[aPos], aNewConstraints);
831       break;
832     default:
833       break;
834     }
835   }
836
837   return aNewConstraints;
838 }
839
840 void SketchSolver_Storage::fixPoint(const Slvs_Entity& thePoint,
841     std::vector<Slvs_hConstraint>& theCreated)
842 {
843   Slvs_Constraint aConstraint;
844   Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
845   bool isFixed = isPointFixed(thePoint.h, aConstrID, true);
846   bool isForceUpdate = (isFixed && isTemporary(aConstrID));
847   if (!isForceUpdate) { // create new constraint
848     if (isFixed) return;
849     aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, thePoint.group, SLVS_C_WHERE_DRAGGED, thePoint.wrkpl,
850         0.0, thePoint.h, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
851     aConstraint.h = addConstraint(aConstraint);
852     theCreated.push_back(aConstraint.h);
853   } else { // update already existent constraint
854     if (!isFixed || aConstrID == SLVS_E_UNKNOWN)
855       return;
856     int aPos = Search(aConstrID, myConstraints);
857     if (aPos >= 0 && aPos < (int)myConstraints.size())
858       myConstraints[aPos].ptA = thePoint.h;
859   }
860 }
861
862 void SketchSolver_Storage::fixLine(const Slvs_Entity& theLine,
863     std::vector<Slvs_hConstraint>& theCreated)
864 {
865   Slvs_Entity aPoint[2] = {
866       getEntity(theLine.point[0]),
867       getEntity(theLine.point[1])
868   };
869
870   Slvs_Constraint anEqual;
871   if (isAxisParallel(theLine.h)) {
872     // Fix one point and a line length
873     Slvs_hConstraint aFixed;
874     if (!isPointFixed(theLine.point[0], aFixed, true) &&
875         !isPointFixed(theLine.point[1], aFixed, true))
876       fixPoint(aPoint[0], theCreated);
877     if (!isUsedInEqual(theLine.h, anEqual)) {
878       // Check the distance is not set yet
879       std::vector<Slvs_Constraint>::const_iterator aDistIt = myConstraints.begin();
880       for (; aDistIt != myConstraints.end(); ++aDistIt)
881         if ((aDistIt->type == SLVS_C_PT_PT_DISTANCE) &&
882            ((aDistIt->ptA == theLine.point[0] && aDistIt->ptB == theLine.point[1]) ||
883             (aDistIt->ptA == theLine.point[1] && aDistIt->ptB == theLine.point[0])))
884           return;
885       // Calculate distance between points on the line
886       double aCoords[4];
887       for (int i = 0; i < 2; i++)
888         for (int j = 0; j < 2; j++) {
889           Slvs_Param aParam = getParameter(aPoint[i].param[j]);
890           aCoords[2*i+j] = aParam.val;
891         }
892
893       double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) + 
894                             (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
895       // fix line length
896       Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group,
897           SLVS_C_PT_PT_DISTANCE, theLine.wrkpl, aLength,
898           theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
899       aDistance.h = addConstraint(aDistance);
900       theCreated.push_back(aDistance.h);
901     }
902     return;
903   }
904   else if (isUsedInEqual(theLine.h, anEqual)) {
905     // Check another entity of Equal is already fixed
906     Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
907     if (isEntityFixed(anOtherEntID, true)) {
908       // Fix start point of the line (if end point is not fixed yet) ...
909       Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
910       bool isFixed = isPointFixed(theLine.point[1], anEndFixedID, true);
911       if (isFixed == SLVS_E_UNKNOWN)
912         fixPoint(aPoint[0], theCreated);
913       // ...  and create fixed point lying on this line
914       Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
915       // Firstly, search already fixed point on line
916       bool isPonLineFixed = false;
917       Slvs_hEntity aFixedPoint;
918       std::vector<Slvs_Constraint>::const_iterator aPLIter = myConstraints.begin();
919       for (; aPLIter != myConstraints.end() && !isPonLineFixed; ++aPLIter)
920         if (aPLIter->type == SLVS_C_PT_ON_LINE && aPLIter->entityA == theLine.h) {
921           isPonLineFixed = isPointFixed(aPLIter->ptA, anEndFixedID);
922           aFixedPoint = aPLIter->ptA;
923         }
924
925       if (isPonLineFixed) { // update existent constraint
926         copyEntity(aPointToCopy, aFixedPoint);
927       } else { // create new constraint
928         Slvs_hEntity aCopied = copyEntity(aPointToCopy);
929         Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group, SLVS_C_PT_ON_LINE,
930             theLine.wrkpl, 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
931         aPonLine.h = addConstraint(aPonLine);
932         theCreated.push_back(aPonLine.h);
933         fixPoint(getEntity(aCopied), theCreated);
934       }
935       return;
936     }
937   }
938
939   // Fix both points
940   for (int i = 0; i < 2; i++)
941     fixPoint(aPoint[i], theCreated);
942 }
943
944 void SketchSolver_Storage::fixCircle(const Slvs_Entity& theCircle,
945     std::vector<Slvs_hConstraint>& theCreated)
946 {
947   bool isFixRadius = true;
948   // Verify the arc is under Equal constraint
949   Slvs_Constraint anEqual;
950   if (isUsedInEqual(theCircle.h, anEqual)) {
951     // Check another entity of Equal is already fixed
952     Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
953     if (isEntityFixed(anOtherEntID, true))
954       isFixRadius = false;
955   }
956
957   fixPoint(getEntity(theCircle.point[0]), theCreated);
958
959   if (isFixRadius) {
960     // Search the radius is already fixed
961     std::vector<Slvs_Constraint>::const_iterator aDiamIter = myConstraints.begin();
962     for (; aDiamIter != myConstraints.end(); ++aDiamIter)
963       if (aDiamIter->type == SLVS_C_DIAMETER && aDiamIter->entityA == theCircle.h)
964         return;
965
966     // Fix radius of a circle
967     const Slvs_Entity& aRadEnt = getEntity(theCircle.distance);
968     double aRadius = getParameter(aRadEnt.param[0]).val;
969     Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theCircle.group, SLVS_C_DIAMETER,
970         theCircle.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theCircle.h, SLVS_E_UNKNOWN);
971     aFixedR.h = addConstraint(aFixedR);
972     theCreated.push_back(aFixedR.h);
973   }
974 }
975
976 void SketchSolver_Storage::fixArc(const Slvs_Entity& theArc,
977     std::vector<Slvs_hConstraint>& theCreated)
978 {
979   Slvs_Entity aPoint[3] = {
980       getEntity(theArc.point[0]),
981       getEntity(theArc.point[1]),
982       getEntity(theArc.point[2])
983   };
984
985   bool isFixRadius = true;
986   std::list<Slvs_Entity> aPointsToFix;
987   aPointsToFix.push_back(aPoint[1]);
988   aPointsToFix.push_back(aPoint[2]);
989
990   // Verify the arc is under Equal constraint
991   Slvs_Constraint anEqual;
992   if (isUsedInEqual(theArc.h, anEqual)) {
993     // Check another entity of Equal is already fixed
994     Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
995     if (isEntityFixed(anOtherEntID, true)) {
996       isFixRadius = false;
997       Slvs_Entity anOtherEntity = getEntity(anOtherEntID);
998       if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
999         aPointsToFix.pop_back();
1000         aPointsToFix.push_back(aPoint[0]);
1001       }
1002     }
1003   }
1004
1005   Slvs_hConstraint aConstrID;
1006   int aNbPointsToFix = 2; // number of fixed points for the arc
1007   if (isPointFixed(theArc.point[0], aConstrID, true))
1008     aNbPointsToFix--;
1009
1010   double anArcPoints[3][2];
1011   for (int i = 0; i < 3; i++) {
1012     const Slvs_Entity& aPointOnArc = getEntity(theArc.point[i]);
1013     for (int j = 0; j < 2; j++)
1014       anArcPoints[i][j] = getParameter(aPointOnArc.param[j]).val;
1015   }
1016
1017   // Radius of the arc
1018   std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(anArcPoints[0][0], anArcPoints[0][1]));
1019   std::shared_ptr<GeomAPI_Pnt2d> aStart(new GeomAPI_Pnt2d(anArcPoints[1][0], anArcPoints[1][1]));
1020   double aRadius = aCenter->distance(aStart);
1021
1022   // Update end point of the arc to be on a curve
1023   std::shared_ptr<GeomAPI_Pnt2d> anEnd(new GeomAPI_Pnt2d(anArcPoints[2][0], anArcPoints[2][1]));
1024   double aDistance = anEnd->distance(aCenter);
1025   std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
1026   if (aDistance < tolerance)
1027     aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
1028   else
1029     aDir = aDir->multiplied(aRadius / aDistance);
1030   double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
1031   const Slvs_Entity& aEndPoint = getEntity(theArc.point[2]);
1032   for (int i = 0; i < 2; i++) {
1033     Slvs_Param aParam = getParameter(aEndPoint.param[i]);
1034     aParam.val = xy[i];
1035     updateParameter(aParam);
1036   }
1037
1038   std::list<Slvs_Entity>::iterator aPtIt = aPointsToFix.begin();
1039   for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
1040     fixPoint(*aPtIt, theCreated);
1041
1042   if (isFixRadius) {
1043     // Fix radius of the arc
1044     bool isExists = false;
1045     std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
1046     for (; anIt != myConstraints.end() && !isExists; ++anIt)
1047       if (anIt->type == SLVS_C_DIAMETER && anIt->entityA == theArc.h)
1048         isExists = true;
1049     if (!isExists) {
1050       Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theArc.group, SLVS_C_DIAMETER,
1051           theArc.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theArc.h, SLVS_E_UNKNOWN);
1052       aFixedR.h = addConstraint(aFixedR);
1053       theCreated.push_back(aFixedR.h);
1054     }
1055   }
1056 }
1057
1058
1059 bool SketchSolver_Storage::isAxisParallel(const Slvs_hEntity& theEntity) const
1060 {
1061   std::vector<Slvs_Constraint>::const_iterator anIter = myConstraints.begin();
1062   for (; anIter != myConstraints.end(); anIter++)
1063     if ((anIter->type == SLVS_C_HORIZONTAL || anIter->type == SLVS_C_VERTICAL) && 
1064         anIter->entityA == theEntity)
1065       return true;
1066   return false;
1067 }
1068
1069 bool SketchSolver_Storage::isUsedInEqual(
1070     const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const
1071 {
1072   // Check the entity is used in Equal constraint
1073   std::vector<Slvs_Constraint>::const_iterator anEqIter = myConstraints.begin();
1074   for (; anEqIter != myConstraints.end(); anEqIter++)
1075     if ((anEqIter->type == SLVS_C_EQUAL_LENGTH_LINES ||
1076          anEqIter->type == SLVS_C_EQUAL_LINE_ARC_LEN ||
1077          anEqIter->type == SLVS_C_EQUAL_RADIUS) &&
1078        (anEqIter->entityA == theEntity || anEqIter->entityB == theEntity)) {
1079       theEqual = *anEqIter;
1080       return true;
1081     }
1082   return false;
1083 }
1084
1085 bool SketchSolver_Storage::isNeedToResolve()
1086 {
1087   if (myConstraints.empty())
1088     return false;
1089
1090   if (!myNeedToResolve) {
1091     // Verify the updated parameters are used in constraints
1092     std::set<Slvs_hEntity> aPoints;
1093     std::vector<Slvs_Entity>::const_iterator anEntIt = myEntities.begin();
1094     for (; anEntIt != myEntities.end(); ++anEntIt) {
1095       for (int i = 0; i < 4 && anEntIt->param[i] != SLVS_E_UNKNOWN; ++i)
1096         if (myUpdatedParameters.find(anEntIt->param[i]) != myUpdatedParameters.end()) {
1097           aPoints.insert(anEntIt->h);
1098           break;
1099         }
1100     }
1101     std::set<Slvs_hEntity> anEntities = aPoints;
1102     for (anEntIt = myEntities.begin(); anEntIt != myEntities.end(); ++anEntIt) {
1103       for (int i = 0; i < 4 && anEntIt->point[i] != SLVS_E_UNKNOWN; ++i)
1104         if (aPoints.find(anEntIt->point[i]) != aPoints.end()) {
1105           anEntities.insert(anEntIt->h);
1106           break;
1107         }
1108     }
1109
1110     std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
1111     for (; aCIt != myConstraints.end() && !myNeedToResolve; ++aCIt) {
1112       Slvs_hEntity anAttrs[6] =
1113         {aCIt->ptA, aCIt->ptB, aCIt->entityA, aCIt->entityB, aCIt->entityC, aCIt->entityD};
1114       for (int i = 0; i < 6; i++)
1115         if (anAttrs[i] != SLVS_E_UNKNOWN && anEntities.find(anAttrs[i]) != anEntities.end()) {
1116           myNeedToResolve = true;
1117           break;
1118         }
1119     }
1120   }
1121
1122   myUpdatedParameters.clear();
1123   return myNeedToResolve;
1124 }
1125
1126
1127
1128
1129
1130
1131 // ========================================================
1132 // =========      Auxiliary functions       ===============
1133 // ========================================================
1134
1135 template<typename T>
1136 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1137 {
1138   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1139   int aVecSize = theEntities.size();
1140   if (theEntities.empty())
1141     return 1;
1142   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1143     aResIndex--;
1144   while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1145     aResIndex++;
1146   if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1147     aResIndex = aVecSize;
1148   return aResIndex;
1149 }
1150
1151 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1152 {
1153   return fabs(theParam1.val - theParam2.val) > tolerance;
1154 }
1155
1156 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1157 {
1158   int i = 0;
1159   for (; theEntity1.param[i] != 0 && i < 4; i++)
1160     if (theEntity1.param[i] != theEntity2.param[i])
1161       return true;
1162   i = 0;
1163   for (; theEntity1.point[i] != 0 && i < 4; i++)
1164     if (theEntity1.point[i] != theEntity2.point[i])
1165       return true;
1166   return false;
1167 }
1168
1169 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1170 {
1171   return theConstraint1.ptA != theConstraint2.ptA ||
1172          theConstraint1.ptB != theConstraint2.ptB ||
1173          theConstraint1.entityA != theConstraint2.entityA ||
1174          theConstraint1.entityB != theConstraint2.entityB ||
1175          theConstraint1.entityC != theConstraint2.entityC ||
1176          theConstraint1.entityD != theConstraint2.entityD ||
1177          fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;
1178 }