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