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