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