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