Salome HOME
Add tools
[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   theFixed = SLVS_C_UNKNOWN;
376   std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
377   for (; aConstrIter != myConstraints.end(); aConstrIter++)
378     if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
379         aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
380       theFixed = aConstrIter->h;
381       if (aConstrIter->ptA == thePointID)
382         return true;
383     }
384   if (theFixed != SLVS_C_UNKNOWN)
385     return true;
386
387   if (theAccurate) {
388     // Try to find the fixed entity which uses such point or its coincidence
389     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
390     for (; anEntIter != myEntities.end(); anEntIter++) {
391       for (int i = 0; i < 4; i++) {
392         Slvs_hEntity aPt = anEntIter->point[i];
393         if (aPt != SLVS_E_UNKNOWN &&
394             (aPt == thePointID || aCoincident.find(aPt) != aCoincident.end())) {
395           if (isEntityFixed(anEntIter->h, true))
396             return true;
397         }
398       }
399     }
400   }
401   return SLVS_E_UNKNOWN;
402 }
403
404 bool SketchSolver_Storage::isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate) const
405 {
406   int aPos = Search(theEntityID, myEntities);
407   if (aPos < 0 || aPos >= (int)myEntities.size())
408     return false;
409
410   // Firstly, find how many points are under Rigid constraint
411   int aNbFixed = 0;
412   for (int i = 0; i < 4; i++) {
413     Slvs_hEntity aPoint = myEntities[aPos].point[i];
414     if (aPoint == SLVS_E_UNKNOWN)
415       continue;
416
417     std::set<Slvs_hEntity> aCoincident;
418     aCoincident.insert(aPoint);
419     std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
420     for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
421       if (aCPIter->find(aPoint) != aCPIter->end()) {
422         aCoincident = *aCPIter;
423         break;
424       }
425
426     // Search the Rigid constraint
427     std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
428     for (; aConstrIter != myConstraints.end(); aConstrIter++)
429       if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
430           aCoincident.find(aConstrIter->ptA) != aCoincident.end())
431         aNbFixed++;
432   }
433
434   std::list<Slvs_Constraint> aList;
435   std::list<Slvs_Constraint>::iterator anIt;
436   Slvs_hConstraint aTempID; // used in isPointFixed() method
437
438   if (myEntities[aPos].type == SLVS_E_LINE_SEGMENT) {
439     if (aNbFixed == 2)
440       return true;
441     else if (aNbFixed == 0 || !theAccurate)
442       return false;
443     // Additional check (the line may be fixed if it is used by different constraints):
444     // 1. The line is used in Equal constraint, another entity is fixed and there is a fixed point on line
445     aList = getConstraintsByType(SLVS_C_PT_ON_LINE);
446     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
447       if (anIt->entityA == theEntityID && isPointFixed(anIt->ptA, aTempID))
448         break;
449     if (anIt != aList.end()) {
450       aList = getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
451       aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
452       for (anIt = aList.begin(); anIt != aList.end(); anIt++)
453         if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
454           Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
455           if (isEntityFixed(anOther, false))
456             return true;
457         }
458     }
459     // 2. The line is used in Parallel/Perpendicular/Vertical/Horizontal and Length constraints
460     aList = getConstraintsByType(SLVS_C_PARALLEL);
461     aList.splice(aList.end(), getConstraintsByType(SLVS_C_PERPENDICULAR));
462     aList.splice(aList.end(), getConstraintsByType(SLVS_C_VERTICAL));
463     aList.splice(aList.end(), getConstraintsByType(SLVS_C_HORIZONTAL));
464     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
465       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
466         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
467         if (isEntityFixed(anOther, false))
468           break;
469       }
470     if (anIt != aList.end()) {
471       aList = getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
472       for (anIt = aList.begin(); anIt != aList.end(); anIt++)
473         if ((anIt->ptA == myEntities[aPos].point[0] && anIt->ptB == myEntities[aPos].point[1]) ||
474             (anIt->ptA == myEntities[aPos].point[1] && anIt->ptB == myEntities[aPos].point[0]))
475           return true;
476     }
477     // 3. Another verifiers ...
478   } else if (myEntities[aPos].type == SLVS_E_CIRCLE) {
479     if (aNbFixed == 0)
480       return false;
481     // Search for Diameter constraint
482     aList = getConstraintsByType(SLVS_C_DIAMETER);
483     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
484       if (anIt->entityA == theEntityID)
485         return true;
486     if (!theAccurate)
487       return false;
488     // Additional check (the circle may be fixed if it is used by different constraints):
489     // 1. The circle is used in Equal constraint and another entity is fixed
490     aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
491     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
492       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
493         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
494         if (isEntityFixed(anOther, false))
495           return true;
496       }
497     // 2. Another verifiers ...
498   } else if (myEntities[aPos].type == SLVS_E_ARC_OF_CIRCLE) {
499     if (aNbFixed > 2)
500       return true;
501     else if (aNbFixed <= 1)
502       return false;
503     // Search for Diameter constraint
504     aList = getConstraintsByType(SLVS_C_DIAMETER);
505     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
506       if (anIt->entityA == theEntityID)
507         return true;
508     if (!theAccurate)
509       return false;
510     // Additional check (the arc may be fixed if it is used by different constraints):
511     // 1. The arc is used in Equal constraint and another entity is fixed
512     aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
513     aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
514     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
515       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
516         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
517         if (isEntityFixed(anOther, false))
518           return true;
519       }
520     // 2. Another verifiers ...
521   }
522   return false;
523 }
524
525
526 Slvs_hConstraint SketchSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
527 {
528   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
529     // Constraint is already used, rewrite it
530     return updateConstraint(theConstraint);
531   }
532
533   Slvs_Constraint aConstraint = theConstraint;
534
535   // Find a constraint with same type uses same arguments to show user overconstraint situation
536   std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
537   for (; aCIt != myConstraints.end(); aCIt++) {
538     if (aConstraint.type != aCIt->type)
539       continue;
540     if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
541         aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
542         aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD)
543       myDuplicatedConstraint = true;
544   }
545
546   if (aConstraint.h > myConstrMaxID)
547     myConstrMaxID = aConstraint.h;
548   else
549     aConstraint.h = ++myConstrMaxID;
550   myConstraints.push_back(aConstraint);
551   myNeedToResolve = true;
552   if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
553     addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
554   return aConstraint.h;
555 }
556
557 Slvs_hConstraint SketchSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
558 {
559   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
560     // Constraint already used, rewrite it
561     int aPos = Search(theConstraint.h, myConstraints);
562     if (aPos >= 0 && aPos < (int)myConstraints.size()) {
563       myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
564       myConstraints[aPos] = theConstraint;
565       if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
566         addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
567       return theConstraint.h;
568     }
569   }
570
571   // Constraint is not found, add new one
572   Slvs_Constraint aConstraint = theConstraint;
573   aConstraint.h = 0;
574   return addConstraint(aConstraint);
575 }
576
577 bool SketchSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
578 {
579   bool aResult = true;
580   int aPos = Search(theConstraintID, myConstraints);
581   if (aPos >= 0 && aPos < (int)myConstraints.size()) {
582     Slvs_Constraint aConstraint = myConstraints[aPos];
583     myConstraints.erase(myConstraints.begin() + aPos);
584     myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
585     myNeedToResolve = true;
586     myRemovedConstraints.insert(theConstraintID);
587     if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
588       removeCoincidence(aConstraint);
589
590     // Remove all entities
591     Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
592         aConstraint.entityA, aConstraint.entityB,
593         aConstraint.entityC, aConstraint.entityD};
594     for (int i = 0; i < 6; i++)
595       if (anEntities[i] != SLVS_E_UNKNOWN)
596         aResult = removeEntity(anEntities[i]) && aResult;
597     // remove temporary fixed point, if available
598     if (myFixed == theConstraintID)
599       myFixed = SLVS_E_UNKNOWN;
600     if (myDuplicatedConstraint) {
601       // Check the duplicated constraints are still available
602       myDuplicatedConstraint = false;
603       std::vector<Slvs_Constraint>::const_iterator anIt1 = myConstraints.begin();
604       std::vector<Slvs_Constraint>::const_iterator anIt2 = myConstraints.begin();
605       for (; anIt1 != myConstraints.end() && !myDuplicatedConstraint; anIt1++)
606         for (anIt2 = anIt1+1; anIt2 != myConstraints.end() && !myDuplicatedConstraint; anIt2++) {
607           if (anIt1->type != anIt2->type)
608             continue;
609           if (anIt1->ptA == anIt2->ptA && anIt1->ptB == anIt2->ptB &&
610               anIt1->entityA == anIt2->entityA && anIt1->entityB == anIt2->entityB &&
611               anIt1->entityC == anIt2->entityC && anIt1->entityD == anIt2->entityD)
612             myDuplicatedConstraint = true;
613         }
614     }
615   }
616   return aResult;
617 }
618
619 const Slvs_Constraint& SketchSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
620 {
621   int aPos = Search(theConstraintID, myConstraints);
622   if (aPos >= 0 && aPos < (int)myConstraints.size())
623     return myConstraints[aPos];
624
625   // Constraint is not found, return empty object
626   static Slvs_Constraint aDummy;
627   aDummy.h = 0;
628   return aDummy;
629 }
630
631 std::list<Slvs_Constraint> SketchSolver_Storage::getConstraintsByType(int theConstraintType) const
632 {
633   std::list<Slvs_Constraint> aResult;
634   std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
635   for (; aCIter != myConstraints.end(); aCIter++)
636     if (aCIter->type == theConstraintType)
637       aResult.push_back(*aCIter);
638   return aResult;
639 }
640
641
642 void SketchSolver_Storage::addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID)
643 {
644   if (myFixed != SLVS_E_UNKNOWN)
645     return; // the point is already fixed
646   int aPos = Search(theConstraintID, myConstraints);
647   if (aPos >= 0 && aPos < (int)myConstraints.size())
648     myFixed = theConstraintID;
649 }
650
651 void SketchSolver_Storage::addTemporaryConstraint(const Slvs_hConstraint& theConstraintID)
652 {
653   myTemporaryConstraints.insert(theConstraintID);
654 }
655
656 void SketchSolver_Storage::removeTemporaryConstraints()
657 {
658   myTemporaryConstraints.clear();
659 }
660
661 int SketchSolver_Storage::deleteTemporaryConstraint()
662 {
663   if (myTemporaryConstraints.empty())
664     return 0;
665   // Search the point-on-line or a non-rigid constraint
666   std::set<Slvs_hConstraint>::iterator aCIt = myTemporaryConstraints.begin();
667   for (; aCIt != myTemporaryConstraints.end(); aCIt++) {
668     int aPos = Search(*aCIt, myConstraints);
669     if (aPos >= (int)myConstraints.size() || myConstraints[aPos].type != SLVS_C_WHERE_DRAGGED)
670       break;
671     std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
672     for (; anIt != myConstraints.end(); anIt++)
673       if (anIt->type == SLVS_C_PT_ON_LINE && anIt->ptA == myConstraints[aPos].ptA)
674         break;
675     if (anIt != myConstraints.end())
676       break;
677   }
678   if (aCIt == myTemporaryConstraints.end())
679     aCIt = myTemporaryConstraints.begin();
680   bool aNewFixed = (*aCIt == myFixed);
681   removeConstraint(*aCIt);
682   myTemporaryConstraints.erase(aCIt);
683   if (aNewFixed) {
684     for (aCIt = myTemporaryConstraints.begin(); aCIt != myTemporaryConstraints.end(); aCIt++) {
685       int aPos = Search(*aCIt, myConstraints);
686       if (myConstraints[aPos].type == SLVS_C_WHERE_DRAGGED) {
687         myFixed = *aCIt;
688         break;
689       }
690     }
691   }
692   return (int)myTemporaryConstraints.size();
693 }
694
695 bool SketchSolver_Storage::isTemporary(const Slvs_hConstraint& theConstraintID) const
696 {
697   return myTemporaryConstraints.find(theConstraintID) != myTemporaryConstraints.end();
698 }
699
700
701 void SketchSolver_Storage::getRemoved(
702     std::set<Slvs_hParam>& theParameters,
703     std::set<Slvs_hEntity>& theEntities,
704     std::set<Slvs_hConstraint>& theConstraints)
705 {
706   theParameters = myRemovedParameters;
707   theEntities = myRemovedEntities;
708   theConstraints = myRemovedConstraints;
709
710   myRemovedParameters.clear();
711   myRemovedEntities.clear();
712   myRemovedConstraints.clear();
713 }
714
715 void SketchSolver_Storage::initializeSolver(SketchSolver_Solver& theSolver)
716 {
717   theSolver.setParameters(myParameters.data(), (int)myParameters.size());
718   theSolver.setEntities(myEntities.data(), (int)myEntities.size());
719
720   // Copy constraints excluding the fixed one
721   std::vector<Slvs_Constraint> aConstraints = myConstraints;
722   if (myFixed != SLVS_E_UNKNOWN) {
723     Slvs_hEntity aFixedPoint = SLVS_E_UNKNOWN;
724     std::vector<Slvs_Constraint>::iterator anIt = aConstraints.begin();
725     for (; anIt != aConstraints.end(); anIt++)
726       if (anIt->h == myFixed) {
727         aFixedPoint = anIt->ptA;
728         aConstraints.erase(anIt);
729         break;
730       }
731     // set dragged parameters
732     int aPos = Search(aFixedPoint, myEntities);
733     theSolver.setDraggedParameters(myEntities[aPos].param);
734   }
735   theSolver.setConstraints(aConstraints.data(), (int)aConstraints.size());
736 }
737
738 void SketchSolver_Storage::addCoincidentPoints(
739     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
740 {
741   std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
742   std::vector< std::set<Slvs_hEntity> >::iterator aFoundIter = myCoincidentPoints.end(); // already found coincidence
743   bool isFound = false;
744   for (; aCIter != myCoincidentPoints.end(); aCIter++) {
745     bool isFirstFound = aCIter->find(thePoint1) != aCIter->end();
746     bool isSecondFound = aCIter->find(thePoint2) != aCIter->end();
747     isFound = isFound || isFirstFound || isSecondFound;
748     if (isFirstFound && isSecondFound)
749       break; // already coincident
750     else if (isFirstFound || isSecondFound) {
751       if (aFoundIter != myCoincidentPoints.end()) {
752         // merge two sets
753         aFoundIter->insert(aCIter->begin(), aCIter->end());
754         myCoincidentPoints.erase(aCIter);
755         break;
756       } else
757         aFoundIter = aCIter;
758       aCIter->insert(thePoint1);
759       aCIter->insert(thePoint2);
760     }
761   }
762   // coincident points not found
763   if (!isFound) {
764     std::set<Slvs_hEntity> aNewSet;
765     aNewSet.insert(thePoint1);
766     aNewSet.insert(thePoint2);
767     myCoincidentPoints.push_back(aNewSet);
768   }
769 }
770
771 void SketchSolver_Storage::removeCoincidentPoint(const Slvs_hEntity& thePoint)
772 {
773   std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
774   for (; aCIter != myCoincidentPoints.end(); aCIter++)
775     if (aCIter->find(thePoint) != aCIter->end()) {
776       aCIter->erase(thePoint);
777       if (aCIter->size() <= 1)
778         myCoincidentPoints.erase(aCIter);
779       break;
780     }
781 }
782
783 void SketchSolver_Storage::removeCoincidence(const Slvs_Constraint& theCoincidence)
784 {
785   // Find set of coincident points
786   std::vector< std::set<Slvs_hEntity> >::iterator aCIt = myCoincidentPoints.begin();
787   for (; aCIt != myCoincidentPoints.end(); ++aCIt)
788     if (aCIt->find(theCoincidence.ptA) != aCIt->end() ||
789         aCIt->find(theCoincidence.ptB) != aCIt->end())
790       break;
791   if (aCIt == myCoincidentPoints.end())
792     return;
793
794   // Leave only the points which are still coincident
795   std::set<Slvs_hEntity> aRemainCoincidence;
796   std::vector<Slvs_Constraint>::const_iterator aConstrIt = myConstraints.begin();
797   for (; aConstrIt != myConstraints.end(); ++aConstrIt) {
798     if (aConstrIt->type != SLVS_C_POINTS_COINCIDENT)
799       continue;
800     if (aCIt->find(aConstrIt->ptA) != aCIt->end() ||
801         aCIt->find(aConstrIt->ptB) != aCIt->end()) {
802       aRemainCoincidence.insert(aConstrIt->ptA);
803       aRemainCoincidence.insert(aConstrIt->ptB);
804     }
805   }
806   if (aRemainCoincidence.size() <= 1)
807     myCoincidentPoints.erase(aCIt);
808   else
809     aCIt->swap(aRemainCoincidence);
810 }
811
812 bool SketchSolver_Storage::isCoincident(
813     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
814 {
815   std::vector< std::set<Slvs_hEntity> >::const_iterator aCIter = myCoincidentPoints.begin();
816   for (; aCIter != myCoincidentPoints.end(); aCIter++)
817     if (aCIter->find(thePoint1) != aCIter->end() && aCIter->find(thePoint2) != aCIter->end())
818       return true;
819   return false;
820 }
821
822 bool SketchSolver_Storage::isEqual(
823     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
824 {
825   if (isCoincident(thePoint1, thePoint2))
826     return true;
827
828   // Precise checking of coincidence: verify that points have equal coordinates
829   int aEnt1Pos = Search(thePoint1, myEntities);
830   int aEnt2Pos = Search(thePoint2, myEntities);
831   if (aEnt1Pos >= 0 && aEnt1Pos < (int)myEntities.size() &&
832       aEnt2Pos >= 0 && aEnt2Pos < (int)myEntities.size()) {
833     double aDist[2];
834     int aParamPos;
835     for (int i = 0; i < 2; i++) {
836       aParamPos = Search(myEntities[aEnt1Pos].param[i], myParameters);
837       aDist[i] = myParameters[aParamPos].val;
838       aParamPos = Search(myEntities[aEnt2Pos].param[i], myParameters);
839       aDist[i] -= myParameters[aParamPos].val;
840     }
841     if (aDist[0] * aDist[0] + aDist[1] * aDist[1] < tolerance * tolerance)
842       return true;
843   }
844   return false;
845 }
846
847
848 std::vector<Slvs_hConstraint> SketchSolver_Storage::fixEntity(const Slvs_hEntity& theEntity)
849 {
850   std::vector<Slvs_hConstraint> aNewConstraints;
851
852   int aPos = Search(theEntity, myEntities);
853   if (aPos >= 0 && aPos < (int)myEntities.size()) {
854     switch (myEntities[aPos].type) {
855     case SLVS_E_POINT_IN_2D:
856     case SLVS_E_POINT_IN_3D:
857       fixPoint(myEntities[aPos], aNewConstraints);
858       break;
859     case SLVS_E_LINE_SEGMENT:
860       fixLine(myEntities[aPos], aNewConstraints);
861       break;
862     case SLVS_E_CIRCLE:
863       fixCircle(myEntities[aPos], aNewConstraints);
864       break;
865     case SLVS_E_ARC_OF_CIRCLE:
866       fixArc(myEntities[aPos], aNewConstraints);
867       break;
868     default:
869       break;
870     }
871   }
872
873   return aNewConstraints;
874 }
875
876 void SketchSolver_Storage::fixPoint(const Slvs_Entity& thePoint,
877     std::vector<Slvs_hConstraint>& theCreated)
878 {
879   Slvs_Constraint aConstraint;
880   Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
881   bool isFixed = isPointFixed(thePoint.h, aConstrID, true);
882   bool isForceUpdate = (isFixed && isTemporary(aConstrID));
883   if (!isForceUpdate) { // create new constraint
884     if (isFixed) return;
885     aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, thePoint.group, SLVS_C_WHERE_DRAGGED, thePoint.wrkpl,
886         0.0, thePoint.h, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
887     aConstraint.h = addConstraint(aConstraint);
888     theCreated.push_back(aConstraint.h);
889   } else { // update already existent constraint
890     if (!isFixed || aConstrID == SLVS_E_UNKNOWN)
891       return;
892     int aPos = Search(aConstrID, myConstraints);
893     if (aPos >= 0 && aPos < (int)myConstraints.size())
894       myConstraints[aPos].ptA = thePoint.h;
895   }
896 }
897
898 void SketchSolver_Storage::fixLine(const Slvs_Entity& theLine,
899     std::vector<Slvs_hConstraint>& theCreated)
900 {
901   Slvs_Entity aPoint[2] = {
902       getEntity(theLine.point[0]),
903       getEntity(theLine.point[1])
904   };
905
906   Slvs_Constraint anEqual;
907   if (isAxisParallel(theLine.h)) {
908     // Fix one point and a line length
909     Slvs_hConstraint aFixed;
910     if (!isPointFixed(theLine.point[0], aFixed, true) &&
911         !isPointFixed(theLine.point[1], aFixed, true))
912       fixPoint(aPoint[0], theCreated);
913     if (!isUsedInEqual(theLine.h, anEqual)) {
914       // Check the distance is not set yet
915       std::vector<Slvs_Constraint>::const_iterator aDistIt = myConstraints.begin();
916       for (; aDistIt != myConstraints.end(); ++aDistIt)
917         if ((aDistIt->type == SLVS_C_PT_PT_DISTANCE) &&
918            ((aDistIt->ptA == theLine.point[0] && aDistIt->ptB == theLine.point[1]) ||
919             (aDistIt->ptA == theLine.point[1] && aDistIt->ptB == theLine.point[0])))
920           return;
921       // Calculate distance between points on the line
922       double aCoords[4];
923       for (int i = 0; i < 2; i++)
924         for (int j = 0; j < 2; j++) {
925           Slvs_Param aParam = getParameter(aPoint[i].param[j]);
926           aCoords[2*i+j] = aParam.val;
927         }
928
929       double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) + 
930                             (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
931       // fix line length
932       Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group,
933           SLVS_C_PT_PT_DISTANCE, theLine.wrkpl, aLength,
934           theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
935       aDistance.h = addConstraint(aDistance);
936       theCreated.push_back(aDistance.h);
937     }
938     return;
939   }
940   else if (isUsedInEqual(theLine.h, anEqual)) {
941     // Check another entity of Equal is already fixed
942     Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
943     if (isEntityFixed(anOtherEntID, true)) {
944       // Fix start point of the line (if end point is not fixed yet) ...
945       Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
946       bool isFixed = isPointFixed(theLine.point[1], anEndFixedID, true);
947       if (isFixed == SLVS_E_UNKNOWN)
948         fixPoint(aPoint[0], theCreated);
949       // ...  and create fixed point lying on this line
950       Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
951       // Firstly, search already fixed point on line
952       bool isPonLineFixed = false;
953       Slvs_hEntity aFixedPoint;
954       std::vector<Slvs_Constraint>::const_iterator aPLIter = myConstraints.begin();
955       for (; aPLIter != myConstraints.end() && !isPonLineFixed; ++aPLIter)
956         if (aPLIter->type == SLVS_C_PT_ON_LINE && aPLIter->entityA == theLine.h) {
957           isPonLineFixed = isPointFixed(aPLIter->ptA, anEndFixedID);
958           aFixedPoint = aPLIter->ptA;
959         }
960
961       if (isPonLineFixed) { // update existent constraint
962         copyEntity(aPointToCopy, aFixedPoint);
963       } else { // create new constraint
964         Slvs_hEntity aCopied = copyEntity(aPointToCopy);
965         Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group, SLVS_C_PT_ON_LINE,
966             theLine.wrkpl, 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
967         aPonLine.h = addConstraint(aPonLine);
968         theCreated.push_back(aPonLine.h);
969         fixPoint(getEntity(aCopied), theCreated);
970       }
971       return;
972     }
973   }
974
975   // Fix both points
976   for (int i = 0; i < 2; i++)
977     fixPoint(aPoint[i], theCreated);
978 }
979
980 void SketchSolver_Storage::fixCircle(const Slvs_Entity& theCircle,
981     std::vector<Slvs_hConstraint>& theCreated)
982 {
983   bool isFixRadius = true;
984   // Verify the arc is under Equal constraint
985   Slvs_Constraint anEqual;
986   if (isUsedInEqual(theCircle.h, anEqual)) {
987     // Check another entity of Equal is already fixed
988     Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
989     if (isEntityFixed(anOtherEntID, true))
990       isFixRadius = false;
991   }
992
993   fixPoint(getEntity(theCircle.point[0]), theCreated);
994
995   if (isFixRadius) {
996     // Search the radius is already fixed
997     std::vector<Slvs_Constraint>::const_iterator aDiamIter = myConstraints.begin();
998     for (; aDiamIter != myConstraints.end(); ++aDiamIter)
999       if (aDiamIter->type == SLVS_C_DIAMETER && aDiamIter->entityA == theCircle.h)
1000         return;
1001
1002     // Fix radius of a circle
1003     const Slvs_Entity& aRadEnt = getEntity(theCircle.distance);
1004     double aRadius = getParameter(aRadEnt.param[0]).val;
1005     Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theCircle.group, SLVS_C_DIAMETER,
1006         theCircle.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theCircle.h, SLVS_E_UNKNOWN);
1007     aFixedR.h = addConstraint(aFixedR);
1008     theCreated.push_back(aFixedR.h);
1009   }
1010 }
1011
1012 void SketchSolver_Storage::fixArc(const Slvs_Entity& theArc,
1013     std::vector<Slvs_hConstraint>& theCreated)
1014 {
1015   Slvs_Entity aPoint[3] = {
1016       getEntity(theArc.point[0]),
1017       getEntity(theArc.point[1]),
1018       getEntity(theArc.point[2])
1019   };
1020
1021   bool isFixRadius = true;
1022   std::list<Slvs_Entity> aPointsToFix;
1023   aPointsToFix.push_back(aPoint[1]);
1024   aPointsToFix.push_back(aPoint[2]);
1025
1026   // Verify the arc is under Equal constraint
1027   Slvs_Constraint anEqual;
1028   if (isUsedInEqual(theArc.h, anEqual)) {
1029     // Check another entity of Equal is already fixed
1030     Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
1031     if (isEntityFixed(anOtherEntID, true)) {
1032       isFixRadius = false;
1033       Slvs_Entity anOtherEntity = getEntity(anOtherEntID);
1034       if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
1035         aPointsToFix.pop_back();
1036         aPointsToFix.push_back(aPoint[0]);
1037       }
1038     }
1039   }
1040
1041   Slvs_hConstraint aConstrID;
1042   int aNbPointsToFix = 2; // number of fixed points for the arc
1043   if (isPointFixed(theArc.point[0], aConstrID, true))
1044     aNbPointsToFix--;
1045
1046   double anArcPoints[3][2];
1047   for (int i = 0; i < 3; i++) {
1048     const Slvs_Entity& aPointOnArc = getEntity(theArc.point[i]);
1049     for (int j = 0; j < 2; j++)
1050       anArcPoints[i][j] = getParameter(aPointOnArc.param[j]).val;
1051   }
1052
1053   // Radius of the arc
1054   std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(anArcPoints[0][0], anArcPoints[0][1]));
1055   std::shared_ptr<GeomAPI_Pnt2d> aStart(new GeomAPI_Pnt2d(anArcPoints[1][0], anArcPoints[1][1]));
1056   double aRadius = aCenter->distance(aStart);
1057
1058   // Update end point of the arc to be on a curve
1059   std::shared_ptr<GeomAPI_Pnt2d> anEnd(new GeomAPI_Pnt2d(anArcPoints[2][0], anArcPoints[2][1]));
1060   double aDistance = anEnd->distance(aCenter);
1061   std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
1062   if (aDistance < tolerance)
1063     aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
1064   else
1065     aDir = aDir->multiplied(aRadius / aDistance);
1066   double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
1067   const Slvs_Entity& aEndPoint = getEntity(theArc.point[2]);
1068   for (int i = 0; i < 2; i++) {
1069     Slvs_Param aParam = getParameter(aEndPoint.param[i]);
1070     aParam.val = xy[i];
1071     updateParameter(aParam);
1072   }
1073
1074   std::list<Slvs_Entity>::iterator aPtIt = aPointsToFix.begin();
1075   for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
1076     fixPoint(*aPtIt, theCreated);
1077
1078   if (isFixRadius) {
1079     // Fix radius of the arc
1080     bool isExists = false;
1081     std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
1082     for (; anIt != myConstraints.end() && !isExists; ++anIt)
1083       if (anIt->type == SLVS_C_DIAMETER && anIt->entityA == theArc.h)
1084         isExists = true;
1085     if (!isExists) {
1086       Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theArc.group, SLVS_C_DIAMETER,
1087           theArc.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theArc.h, SLVS_E_UNKNOWN);
1088       aFixedR.h = addConstraint(aFixedR);
1089       theCreated.push_back(aFixedR.h);
1090     }
1091   }
1092 }
1093
1094
1095 bool SketchSolver_Storage::isAxisParallel(const Slvs_hEntity& theEntity) const
1096 {
1097   std::vector<Slvs_Constraint>::const_iterator anIter = myConstraints.begin();
1098   for (; anIter != myConstraints.end(); anIter++)
1099     if ((anIter->type == SLVS_C_HORIZONTAL || anIter->type == SLVS_C_VERTICAL) && 
1100         anIter->entityA == theEntity)
1101       return true;
1102   return false;
1103 }
1104
1105 bool SketchSolver_Storage::isUsedInEqual(
1106     const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const
1107 {
1108   // Check the entity is used in Equal constraint
1109   std::vector<Slvs_Constraint>::const_iterator anEqIter = myConstraints.begin();
1110   for (; anEqIter != myConstraints.end(); anEqIter++)
1111     if ((anEqIter->type == SLVS_C_EQUAL_LENGTH_LINES ||
1112          anEqIter->type == SLVS_C_EQUAL_LINE_ARC_LEN ||
1113          anEqIter->type == SLVS_C_EQUAL_RADIUS) &&
1114        (anEqIter->entityA == theEntity || anEqIter->entityB == theEntity)) {
1115       theEqual = *anEqIter;
1116       return true;
1117     }
1118   return false;
1119 }
1120
1121 bool SketchSolver_Storage::isNeedToResolve()
1122 {
1123   if (myConstraints.empty())
1124     return false;
1125
1126   if (!myNeedToResolve) {
1127     // Verify the updated parameters are used in constraints
1128     std::set<Slvs_hEntity> aPoints;
1129     std::vector<Slvs_Entity>::const_iterator anEntIt = myEntities.begin();
1130     for (; anEntIt != myEntities.end(); ++anEntIt) {
1131       for (int i = 0; i < 4 && anEntIt->param[i] != SLVS_E_UNKNOWN; ++i)
1132         if (myUpdatedParameters.find(anEntIt->param[i]) != myUpdatedParameters.end()) {
1133           aPoints.insert(anEntIt->h);
1134           break;
1135         }
1136     }
1137     std::set<Slvs_hEntity> anEntities = aPoints;
1138     for (anEntIt = myEntities.begin(); anEntIt != myEntities.end(); ++anEntIt) {
1139       for (int i = 0; i < 4 && anEntIt->point[i] != SLVS_E_UNKNOWN; ++i)
1140         if (aPoints.find(anEntIt->point[i]) != aPoints.end()) {
1141           anEntities.insert(anEntIt->h);
1142           break;
1143         }
1144     }
1145
1146     std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
1147     for (; aCIt != myConstraints.end() && !myNeedToResolve; ++aCIt) {
1148       Slvs_hEntity anAttrs[6] =
1149         {aCIt->ptA, aCIt->ptB, aCIt->entityA, aCIt->entityB, aCIt->entityC, aCIt->entityD};
1150       for (int i = 0; i < 6; i++)
1151         if (anAttrs[i] != SLVS_E_UNKNOWN && anEntities.find(anAttrs[i]) != anEntities.end()) {
1152           myNeedToResolve = true;
1153           break;
1154         }
1155     }
1156   }
1157
1158   myUpdatedParameters.clear();
1159   return myNeedToResolve;
1160 }
1161
1162
1163
1164
1165
1166
1167 // ========================================================
1168 // =========      Auxiliary functions       ===============
1169 // ========================================================
1170
1171 template<typename T>
1172 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1173 {
1174   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1175   int aVecSize = theEntities.size();
1176   if (theEntities.empty())
1177     return 1;
1178   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1179     aResIndex--;
1180   while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1181     aResIndex++;
1182   if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1183     aResIndex = aVecSize;
1184   return aResIndex;
1185 }
1186
1187 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1188 {
1189   return fabs(theParam1.val - theParam2.val) > tolerance;
1190 }
1191
1192 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1193 {
1194   int i = 0;
1195   for (; theEntity1.param[i] != 0 && i < 4; i++)
1196     if (theEntity1.param[i] != theEntity2.param[i])
1197       return true;
1198   i = 0;
1199   for (; theEntity1.point[i] != 0 && i < 4; i++)
1200     if (theEntity1.point[i] != theEntity2.point[i])
1201       return true;
1202   return false;
1203 }
1204
1205 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1206 {
1207   return theConstraint1.ptA != theConstraint2.ptA ||
1208          theConstraint1.ptB != theConstraint2.ptB ||
1209          theConstraint1.entityA != theConstraint2.entityA ||
1210          theConstraint1.entityB != theConstraint2.entityB ||
1211          theConstraint1.entityC != theConstraint2.entityC ||
1212          theConstraint1.entityD != theConstraint2.entityD ||
1213          fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;
1214 }