Salome HOME
Issue #532 - 4.13. Partition with splitting-arguments making solids with shared faces
[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   return false;
742 }
743
744 bool SketchSolver_Storage::isEqual(
745     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
746 {
747   if (isCoincident(thePoint1, thePoint2))
748     return true;
749
750   // Precise checking of coincidence: verify that points have equal coordinates
751   int aEnt1Pos = Search(thePoint1, myEntities);
752   int aEnt2Pos = Search(thePoint2, myEntities);
753   if (aEnt1Pos >= 0 && aEnt1Pos < (int)myEntities.size() &&
754       aEnt2Pos >= 0 && aEnt2Pos < (int)myEntities.size()) {
755     double aDist[2];
756     int aParamPos;
757     for (int i = 0; i < 2; i++) {
758       aParamPos = Search(myEntities[aEnt1Pos].param[i], myParameters);
759       aDist[i] = myParameters[aParamPos].val;
760       aParamPos = Search(myEntities[aEnt2Pos].param[i], myParameters);
761       aDist[i] -= myParameters[aParamPos].val;
762     }
763     if (aDist[0] * aDist[0] + aDist[1] * aDist[1] < tolerance * tolerance)
764       return true;
765   }
766   return false;
767 }
768
769
770 std::vector<Slvs_hConstraint> SketchSolver_Storage::fixEntity(const Slvs_hEntity& theEntity)
771 {
772   std::vector<Slvs_hConstraint> aNewConstraints;
773
774   int aPos = Search(theEntity, myEntities);
775   if (aPos >= 0 && aPos < (int)myEntities.size()) {
776     switch (myEntities[aPos].type) {
777     case SLVS_E_POINT_IN_2D:
778     case SLVS_E_POINT_IN_3D:
779       fixPoint(myEntities[aPos], aNewConstraints);
780       break;
781     case SLVS_E_LINE_SEGMENT:
782       fixLine(myEntities[aPos], aNewConstraints);
783       break;
784     case SLVS_E_CIRCLE:
785       fixCircle(myEntities[aPos], aNewConstraints);
786       break;
787     case SLVS_E_ARC_OF_CIRCLE:
788       fixArc(myEntities[aPos], aNewConstraints);
789       break;
790     default:
791       break;
792     }
793   }
794
795   return aNewConstraints;
796 }
797
798 void SketchSolver_Storage::fixPoint(const Slvs_Entity& thePoint,
799     std::vector<Slvs_hConstraint>& theCreated)
800 {
801   Slvs_Constraint aConstraint;
802   Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
803   bool isFixed = isPointFixed(thePoint.h, aConstrID, true);
804   bool isForceUpdate = (isFixed && isTemporary(aConstrID));
805   if (!isForceUpdate) { // create new constraint
806     if (isFixed) return;
807     aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, thePoint.group, SLVS_C_WHERE_DRAGGED, thePoint.wrkpl,
808         0.0, thePoint.h, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
809     aConstraint.h = addConstraint(aConstraint);
810     theCreated.push_back(aConstraint.h);
811   } else { // update already existent constraint
812     if (!isFixed || aConstrID == SLVS_E_UNKNOWN)
813       return;
814     int aPos = Search(aConstrID, myConstraints);
815     if (aPos >= 0 && aPos < (int)myConstraints.size())
816       myConstraints[aPos].ptA = thePoint.h;
817   }
818 }
819
820 void SketchSolver_Storage::fixLine(const Slvs_Entity& theLine,
821     std::vector<Slvs_hConstraint>& theCreated)
822 {
823   Slvs_Entity aPoint[2] = {
824       getEntity(theLine.point[0]),
825       getEntity(theLine.point[1])
826   };
827
828   Slvs_Constraint anEqual;
829   if (isAxisParallel(theLine.h)) {
830     // Fix one point and a line length
831     Slvs_hConstraint aFixed;
832     if (!isPointFixed(theLine.point[0], aFixed, true) &&
833         !isPointFixed(theLine.point[1], aFixed, true))
834       fixPoint(aPoint[0], theCreated);
835     if (!isUsedInEqual(theLine.h, anEqual)) {
836       // Check the distance is not set yet
837       std::vector<Slvs_Constraint>::const_iterator aDistIt = myConstraints.begin();
838       for (; aDistIt != myConstraints.end(); ++aDistIt)
839         if ((aDistIt->type == SLVS_C_PT_PT_DISTANCE) &&
840            ((aDistIt->ptA == theLine.point[0] && aDistIt->ptB == theLine.point[1]) ||
841             (aDistIt->ptA == theLine.point[1] && aDistIt->ptB == theLine.point[0])))
842           return;
843       // Calculate distance between points on the line
844       double aCoords[4];
845       for (int i = 0; i < 2; i++)
846         for (int j = 0; j < 2; j++) {
847           Slvs_Param aParam = getParameter(aPoint[i].param[j]);
848           aCoords[2*i+j] = aParam.val;
849         }
850
851       double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) + 
852                             (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
853       // fix line length
854       Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group,
855           SLVS_C_PT_PT_DISTANCE, theLine.wrkpl, aLength,
856           theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
857       aDistance.h = addConstraint(aDistance);
858       theCreated.push_back(aDistance.h);
859     }
860     return;
861   }
862   else if (isUsedInEqual(theLine.h, anEqual)) {
863     // Check another entity of Equal is already fixed
864     Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
865     if (isEntityFixed(anOtherEntID, true)) {
866       // Fix start point of the line (if end point is not fixed yet) ...
867       Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
868       bool isFixed = isPointFixed(theLine.point[1], anEndFixedID, true);
869       if (isFixed == SLVS_E_UNKNOWN)
870         fixPoint(aPoint[0], theCreated);
871       // ...  and create fixed point lying on this line
872       Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
873       // Firstly, search already fixed point on line
874       bool isPonLineFixed = false;
875       Slvs_hEntity aFixedPoint;
876       std::vector<Slvs_Constraint>::const_iterator aPLIter = myConstraints.begin();
877       for (; aPLIter != myConstraints.end() && !isPonLineFixed; ++aPLIter)
878         if (aPLIter->type == SLVS_C_PT_ON_LINE && aPLIter->entityA == theLine.h) {
879           isPonLineFixed = isPointFixed(aPLIter->ptA, anEndFixedID);
880           aFixedPoint = aPLIter->ptA;
881         }
882
883       if (isPonLineFixed) { // update existent constraint
884         copyEntity(aPointToCopy, aFixedPoint);
885       } else { // create new constraint
886         Slvs_hEntity aCopied = copyEntity(aPointToCopy);
887         Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group, SLVS_C_PT_ON_LINE,
888             theLine.wrkpl, 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
889         aPonLine.h = addConstraint(aPonLine);
890         theCreated.push_back(aPonLine.h);
891         fixPoint(getEntity(aCopied), theCreated);
892       }
893       return;
894     }
895   }
896
897   // Fix both points
898   for (int i = 0; i < 2; i++)
899     fixPoint(aPoint[i], theCreated);
900 }
901
902 void SketchSolver_Storage::fixCircle(const Slvs_Entity& theCircle,
903     std::vector<Slvs_hConstraint>& theCreated)
904 {
905   bool isFixRadius = true;
906   // Verify the arc is under Equal constraint
907   Slvs_Constraint anEqual;
908   if (isUsedInEqual(theCircle.h, anEqual)) {
909     // Check another entity of Equal is already fixed
910     Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
911     if (isEntityFixed(anOtherEntID, true))
912       isFixRadius = false;
913   }
914
915   fixPoint(getEntity(theCircle.point[0]), theCreated);
916
917   if (isFixRadius) {
918     // Search the radius is already fixed
919     std::vector<Slvs_Constraint>::const_iterator aDiamIter = myConstraints.begin();
920     for (; aDiamIter != myConstraints.end(); ++aDiamIter)
921       if (aDiamIter->type == SLVS_C_DIAMETER && aDiamIter->entityA == theCircle.h)
922         return;
923
924     // Fix radius of a circle
925     const Slvs_Entity& aRadEnt = getEntity(theCircle.distance);
926     double aRadius = getParameter(aRadEnt.param[0]).val;
927     Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theCircle.group, SLVS_C_DIAMETER,
928         theCircle.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theCircle.h, SLVS_E_UNKNOWN);
929     aFixedR.h = addConstraint(aFixedR);
930     theCreated.push_back(aFixedR.h);
931   }
932 }
933
934 void SketchSolver_Storage::fixArc(const Slvs_Entity& theArc,
935     std::vector<Slvs_hConstraint>& theCreated)
936 {
937   Slvs_Entity aPoint[3] = {
938       getEntity(theArc.point[0]),
939       getEntity(theArc.point[1]),
940       getEntity(theArc.point[2])
941   };
942
943   bool isFixRadius = true;
944   std::list<Slvs_Entity> aPointsToFix;
945   aPointsToFix.push_back(aPoint[1]);
946   aPointsToFix.push_back(aPoint[2]);
947
948   // Verify the arc is under Equal constraint
949   Slvs_Constraint anEqual;
950   if (isUsedInEqual(theArc.h, anEqual)) {
951     // Check another entity of Equal is already fixed
952     Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
953     if (isEntityFixed(anOtherEntID, true)) {
954       isFixRadius = false;
955       Slvs_Entity anOtherEntity = getEntity(anOtherEntID);
956       if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
957         aPointsToFix.pop_back();
958         aPointsToFix.push_back(aPoint[0]);
959       }
960     }
961   }
962
963   Slvs_hConstraint aConstrID;
964   int aNbPointsToFix = 2; // number of fixed points for the arc
965   if (isPointFixed(theArc.point[0], aConstrID, true))
966     aNbPointsToFix--;
967
968   double anArcPoints[3][2];
969   for (int i = 0; i < 3; i++) {
970     const Slvs_Entity& aPointOnArc = getEntity(theArc.point[i]);
971     for (int j = 0; j < 2; j++)
972       anArcPoints[i][j] = getParameter(aPointOnArc.param[j]).val;
973   }
974
975   // Radius of the arc
976   std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(anArcPoints[0][0], anArcPoints[0][1]));
977   std::shared_ptr<GeomAPI_Pnt2d> aStart(new GeomAPI_Pnt2d(anArcPoints[1][0], anArcPoints[1][1]));
978   double aRadius = aCenter->distance(aStart);
979
980   // Update end point of the arc to be on a curve
981   std::shared_ptr<GeomAPI_Pnt2d> anEnd(new GeomAPI_Pnt2d(anArcPoints[2][0], anArcPoints[2][1]));
982   double aDistance = anEnd->distance(aCenter);
983   std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
984   if (aDistance < tolerance)
985     aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
986   else
987     aDir = aDir->multiplied(aRadius / aDistance);
988   double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
989   const Slvs_Entity& aEndPoint = getEntity(theArc.point[2]);
990   for (int i = 0; i < 2; i++) {
991     Slvs_Param aParam = getParameter(aEndPoint.param[i]);
992     aParam.val = xy[i];
993     updateParameter(aParam);
994   }
995
996   std::list<Slvs_Entity>::iterator aPtIt = aPointsToFix.begin();
997   for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
998     fixPoint(*aPtIt, theCreated);
999
1000   if (isFixRadius) {
1001     // Fix radius of the arc
1002     bool isExists = false;
1003     std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
1004     for (; anIt != myConstraints.end() && !isExists; ++anIt)
1005       if (anIt->type == SLVS_C_DIAMETER && anIt->entityA == theArc.h)
1006         isExists = true;
1007     if (!isExists) {
1008       Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theArc.group, SLVS_C_DIAMETER,
1009           theArc.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theArc.h, SLVS_E_UNKNOWN);
1010       aFixedR.h = addConstraint(aFixedR);
1011       theCreated.push_back(aFixedR.h);
1012     }
1013   }
1014 }
1015
1016
1017 bool SketchSolver_Storage::isAxisParallel(const Slvs_hEntity& theEntity) const
1018 {
1019   std::vector<Slvs_Constraint>::const_iterator anIter = myConstraints.begin();
1020   for (; anIter != myConstraints.end(); anIter++)
1021     if ((anIter->type == SLVS_C_HORIZONTAL || anIter->type == SLVS_C_VERTICAL) && 
1022         anIter->entityA == theEntity)
1023       return true;
1024   return false;
1025 }
1026
1027 bool SketchSolver_Storage::isUsedInEqual(
1028     const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const
1029 {
1030   // Check the entity is used in Equal constraint
1031   std::vector<Slvs_Constraint>::const_iterator anEqIter = myConstraints.begin();
1032   for (; anEqIter != myConstraints.end(); anEqIter++)
1033     if ((anEqIter->type == SLVS_C_EQUAL_LENGTH_LINES ||
1034          anEqIter->type == SLVS_C_EQUAL_LINE_ARC_LEN ||
1035          anEqIter->type == SLVS_C_EQUAL_RADIUS) &&
1036        (anEqIter->entityA == theEntity || anEqIter->entityB == theEntity)) {
1037       theEqual = *anEqIter;
1038       return true;
1039     }
1040   return false;
1041 }
1042
1043
1044
1045
1046
1047 // ========================================================
1048 // =========      Auxiliary functions       ===============
1049 // ========================================================
1050
1051 template<typename T>
1052 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1053 {
1054   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1055   int aVecSize = theEntities.size();
1056   if (theEntities.empty())
1057     return 1;
1058   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1059     aResIndex--;
1060   while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1061     aResIndex++;
1062   if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1063     aResIndex = aVecSize;
1064   return aResIndex;
1065 }
1066
1067 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1068 {
1069   return fabs(theParam1.val - theParam2.val) > tolerance;
1070 }
1071
1072 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1073 {
1074   int i = 0;
1075   for (; theEntity1.param[i] != 0 && i < 4; i++)
1076     if (theEntity1.param[i] != theEntity2.param[i])
1077       return true;
1078   i = 0;
1079   for (; theEntity1.point[i] != 0 && i < 4; i++)
1080     if (theEntity1.point[i] != theEntity2.point[i])
1081       return true;
1082   return false;
1083 }
1084
1085 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1086 {
1087   return theConstraint1.ptA != theConstraint2.ptA ||
1088          theConstraint1.ptB != theConstraint2.ptB ||
1089          theConstraint1.entityA != theConstraint2.entityA ||
1090          theConstraint1.entityB != theConstraint2.entityB ||
1091          theConstraint1.entityC != theConstraint2.entityC ||
1092          theConstraint1.entityD != theConstraint2.entityD ||
1093          fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;
1094 }