]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_Storage.cpp
Salome HOME
Avoid conflicting constraints when mirror a group of entities with Equal constraint
[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 <math.h>
10
11 /** \brief Search the entity/parameter with specified ID in the list of elements
12  *  \param[in] theEntityID unique ID of the element
13  *  \param[in] theEntities list of elements
14  *  \return position of the found element or -1 if the element is not found
15  */
16 template<typename T>
17 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
18
19 /// \brief Compare two parameters to be different
20 static bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2);
21 /// \brief Compare two entities to be different
22 static bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2);
23 /// \brief Compare two constriants to be different
24 static bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2);
25
26
27 SketchSolver_Storage::SketchSolver_Storage()
28   : myParamMaxID(SLVS_E_UNKNOWN),
29     myEntityMaxID(SLVS_E_UNKNOWN),
30     myConstrMaxID(SLVS_C_UNKNOWN),
31     myFixed(SLVS_E_UNKNOWN),
32     myNeedToResolve(false)
33 {
34 }
35
36 Slvs_hParam SketchSolver_Storage::addParameter(const Slvs_Param& theParam)
37 {
38   if (theParam.h > 0 && theParam.h <= myParamMaxID) {
39     // parameter is already used, rewrite it
40     return updateParameter(theParam);
41   }
42
43   Slvs_Param aParam = theParam;
44   if (aParam.h > myParamMaxID)
45     myParamMaxID = aParam.h;
46   else
47     aParam.h = ++myParamMaxID;
48   myParameters.push_back(aParam);
49   myNeedToResolve = true;
50   return aParam.h;
51 }
52
53 Slvs_hParam SketchSolver_Storage::updateParameter(const Slvs_Param& theParam)
54 {
55   if (theParam.h > 0 && theParam.h <= myParamMaxID) {
56     // parameter already used, rewrite it
57     int aPos = Search(theParam.h, myParameters);
58     if (aPos >= 0 && aPos < (int)myParameters.size()) {
59       myNeedToResolve = myNeedToResolve || IsNotEqual(myParameters[aPos], theParam);
60       myParameters[aPos] = theParam;
61       return theParam.h;
62     }
63   }
64
65   // Parameter is not found, add new one
66   Slvs_Param aParam = theParam;
67   aParam.h = 0;
68   return addParameter(aParam);
69 }
70
71 bool SketchSolver_Storage::removeParameter(const Slvs_hParam& theParamID)
72 {
73   int aPos = Search(theParamID, myParameters);
74   if (aPos >= 0 && aPos < (int)myParameters.size()) {
75     // Firstly, search the parametes is not used elsewhere
76     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
77     for (; anEntIter != myEntities.end(); anEntIter++) {
78       for (int i = 0; i < 4; i++)
79         if (anEntIter->param[i] == theParamID)
80           return false;
81     }
82     // Remove parameter
83     myParameters.erase(myParameters.begin() + aPos);
84     myParamMaxID = myParameters.empty() ? SLVS_E_UNKNOWN : myParameters.back().h;
85     myNeedToResolve = true;
86     myRemovedParameters.insert(theParamID);
87     return true;
88   }
89   return false;
90 }
91
92 const Slvs_Param& SketchSolver_Storage::getParameter(const Slvs_hParam& theParamID) const
93 {
94   int aPos = Search(theParamID, myParameters);
95   if (aPos >= 0 && aPos < (int)myParameters.size())
96     return myParameters[aPos];
97
98   // Parameter is not found, return empty object
99   static Slvs_Param aDummy;
100   aDummy.h = 0;
101   return aDummy;
102 }
103
104
105 Slvs_hEntity SketchSolver_Storage::addEntity(const Slvs_Entity& theEntity)
106 {
107   if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
108     // Entity is already used, rewrite it
109     return updateEntity(theEntity);
110   }
111
112   Slvs_Entity aEntity = theEntity;
113   if (aEntity.h > myEntityMaxID)
114     myEntityMaxID = aEntity.h;
115   else
116     aEntity.h = ++myEntityMaxID;
117   myEntities.push_back(aEntity);
118   myNeedToResolve = true;
119   return aEntity.h;
120 }
121
122 Slvs_hEntity SketchSolver_Storage::updateEntity(const Slvs_Entity& theEntity)
123 {
124   if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
125     // Entity already used, rewrite it
126     int aPos = Search(theEntity.h, myEntities);
127     if (aPos >= 0 && aPos < (int)myEntities.size()) {
128       myNeedToResolve = myNeedToResolve || IsNotEqual(myEntities[aPos], theEntity);
129       myEntities[aPos] = theEntity;
130       return theEntity.h;
131     }
132   }
133
134   // Entity is not found, add new one
135   Slvs_Entity aEntity = theEntity;
136   aEntity.h = 0;
137   return addEntity(aEntity);
138 }
139
140 bool SketchSolver_Storage::removeEntity(const Slvs_hEntity& theEntityID)
141 {
142   bool aResult = true;
143   int aPos = Search(theEntityID, myEntities);
144   if (aPos >= 0 && aPos < (int)myEntities.size()) {
145     // Firstly, check the entity is not used elsewhere
146     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
147     for (; anEntIter != myEntities.end(); anEntIter++) {
148       for (int i = 0; i < 4; i++)
149         if (anEntIter->point[i] == theEntityID)
150           return false;
151       if (anEntIter->distance == theEntityID)
152         return false;
153     }
154     std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
155     for (; aConstrIter != myConstraints.end(); aConstrIter++) {
156       Slvs_hEntity anEntIDs[6] = {aConstrIter->ptA, aConstrIter->ptB,
157           aConstrIter->entityA, aConstrIter->entityB,
158           aConstrIter->entityC, aConstrIter->entityD};
159       for (int i = 0; i < 6; i++)
160         if (anEntIDs[i] == theEntityID)
161           return false;
162     }
163     // The entity is not used, remove it and its parameters
164     Slvs_Entity anEntity = myEntities[aPos];
165     myEntities.erase(myEntities.begin() + aPos);
166     myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
167     if (anEntity.distance != SLVS_E_UNKNOWN)
168       aResult = aResult && removeParameter(anEntity.distance);
169     for (int i = 0; i < 4; i++)
170       if (anEntity.param[i] != SLVS_E_UNKNOWN)
171         aResult = removeParameter(anEntity.param[i]) && aResult;
172     for (int i = 0; i < 4; i++)
173       if (anEntity.point[i] != SLVS_E_UNKNOWN)
174         aResult = removeEntity(anEntity.point[i]) && aResult;
175     myNeedToResolve = true;
176     myRemovedEntities.insert(theEntityID);
177     if (anEntity.type == SLVS_E_POINT_IN_2D || anEntity.type == SLVS_E_POINT_IN_3D)
178       removeCoincidentPoint(theEntityID);
179   }
180   return aResult;
181 }
182
183 const Slvs_Entity& SketchSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
184 {
185   int aPos = Search(theEntityID, myEntities);
186   if (aPos >= 0 && aPos < (int)myEntities.size())
187     return myEntities[aPos];
188
189   // Entity is not found, return empty object
190   static Slvs_Entity aDummy;
191   aDummy.h = 0;
192   return aDummy;
193 }
194
195 Slvs_hEntity SketchSolver_Storage::copyEntity(const Slvs_hEntity& theCopied)
196 {
197   int aPos = Search(theCopied, myEntities);
198   if (aPos < 0 || aPos >= (int)myEntities.size())
199     return SLVS_E_UNKNOWN;
200
201   Slvs_Entity aCopy = myEntities[aPos];
202   aCopy.h = SLVS_E_UNKNOWN;
203   int i = 0;
204   while (aCopy.point[i] != SLVS_E_UNKNOWN) {
205     aCopy.point[i] = copyEntity(aCopy.point[i]);
206     i++;
207   }
208   if (aCopy.param[0] != SLVS_E_UNKNOWN) {
209     aPos = Search(aCopy.param[0], myParameters);
210     i = 0;
211     while (aCopy.param[i] != SLVS_E_UNKNOWN) {
212       Slvs_Param aNewParam = myParameters[aPos];
213       aNewParam.h = SLVS_E_UNKNOWN;
214       aCopy.param[i] = addParameter(aNewParam);
215       i++;
216       aPos++;
217     }
218   }
219   return addEntity(aCopy);
220 }
221
222 void SketchSolver_Storage::copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo)
223 {
224   int aPosFrom = Search(theFrom, myEntities);
225   int aPosTo = Search(theTo, myEntities);
226   if (aPosFrom < 0 || aPosFrom >= (int)myEntities.size() || 
227       aPosTo < 0 || aPosTo >= (int)myEntities.size())
228     return;
229
230   Slvs_Entity aEntFrom = myEntities[aPosFrom];
231   Slvs_Entity aEntTo = myEntities[aPosTo];
232   int i = 0;
233   while (aEntFrom.point[i] != SLVS_E_UNKNOWN) {
234     copyEntity(aEntFrom.point[i], aEntTo.point[i]);
235     i++;
236   }
237   if (aEntFrom.param[0] != SLVS_E_UNKNOWN) {
238     aPosFrom = Search(aEntFrom.param[0], myParameters);
239     aPosTo = Search(aEntTo.param[0], myParameters);
240     i = 0;
241     while (aEntFrom.param[i] != SLVS_E_UNKNOWN) {
242       myParameters[aPosTo++].val = myParameters[aPosFrom++].val;
243       i++;
244     }
245   }
246 }
247
248
249 bool SketchSolver_Storage::isPointFixed(
250     const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate) const
251 {
252   // Search the set of coincident points
253   std::set<Slvs_hEntity> aCoincident;
254   aCoincident.insert(thePointID);
255   std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
256   for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
257     if (aCPIter->find(thePointID) != aCPIter->end()) {
258       aCoincident = *aCPIter;
259       break;
260     }
261
262   // Search the Rigid constraint
263   std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
264   for (; aConstrIter != myConstraints.end(); aConstrIter++)
265     if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
266         aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
267       theFixed = aConstrIter->h;
268       return true;
269     }
270
271   if (theAccurate) {
272     // Try to find the fixed entity which uses such point or its coincidence
273     std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
274     for (; anEntIter != myEntities.end(); anEntIter++) {
275       for (int i = 0; i < 4; i++) {
276         Slvs_hEntity aPt = anEntIter->point[i];
277         if (aPt != SLVS_E_UNKNOWN &&
278             (aPt == thePointID || aCoincident.find(aPt) != aCoincident.end())) {
279           if (isEntityFixed(anEntIter->h, true))
280             return true;
281         }
282       }
283     }
284   }
285   return SLVS_E_UNKNOWN;
286 }
287
288 bool SketchSolver_Storage::isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate) const
289 {
290   int aPos = Search(theEntityID, myEntities);
291   if (aPos < 0 || aPos >= (int)myEntities.size())
292     return false;
293
294   // Firstly, find how many points are under Rigid constraint
295   int aNbFixed = 0;
296   for (int i = 0; i < 4; i++) {
297     Slvs_hEntity aPoint = myEntities[aPos].point[i];
298     if (aPoint == SLVS_E_UNKNOWN)
299       continue;
300
301     std::set<Slvs_hEntity> aCoincident;
302     aCoincident.insert(aPoint);
303     std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
304     for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
305       if (aCPIter->find(aPoint) != aCPIter->end()) {
306         aCoincident = *aCPIter;
307         break;
308       }
309
310     // Search the Rigid constraint
311     std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
312     for (; aConstrIter != myConstraints.end(); aConstrIter++)
313       if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
314           aCoincident.find(aConstrIter->ptA) != aCoincident.end())
315         aNbFixed++;
316   }
317
318   std::list<Slvs_Constraint> aList;
319   std::list<Slvs_Constraint>::iterator anIt;
320   Slvs_hConstraint aTempID; // used in isPointFixed() method
321
322   if (myEntities[aPos].type == SLVS_E_LINE_SEGMENT) {
323     if (aNbFixed == 2)
324       return true;
325     else if (aNbFixed == 0 || !theAccurate)
326       return false;
327     // Additional check (the line may be fixed if it is used by different constraints):
328     // 1. The line is used in Equal constraint, another entity is fixed and there is a fixed point on line
329     aList = getConstraintsByType(SLVS_C_PT_ON_LINE);
330     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
331       if (anIt->entityA == theEntityID && isPointFixed(anIt->ptA, aTempID))
332         break;
333     if (anIt != aList.end()) {
334       aList = getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
335       aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
336       for (anIt = aList.begin(); anIt != aList.end(); anIt++)
337         if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
338           Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
339           if (isEntityFixed(anOther, false))
340             return true;
341         }
342     }
343     // 2. The line is used in Parallel/Perpendicular and Length constraints
344     aList = getConstraintsByType(SLVS_C_PARALLEL);
345     aList.splice(aList.end(), getConstraintsByType(SLVS_C_PERPENDICULAR));
346     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
347       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
348         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
349         if (isEntityFixed(anOther, false))
350           break;
351       }
352     if (anIt != aList.end()) {
353       aList = getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
354       for (anIt = aList.begin(); anIt != aList.end(); anIt++)
355         if ((anIt->ptA == myEntities[aPos].point[0] && anIt->ptB == myEntities[aPos].point[1]) ||
356             (anIt->ptA == myEntities[aPos].point[1] && anIt->ptB == myEntities[aPos].point[0]))
357           return true;
358     }
359     // 3. Another verifiers ...
360   } else if (myEntities[aPos].type == SLVS_E_CIRCLE) {
361     if (aNbFixed == 0)
362       return false;
363     // Search for Diameter constraint
364     aList = getConstraintsByType(SLVS_C_DIAMETER);
365     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
366       if (anIt->entityA == theEntityID)
367         return true;
368     if (!theAccurate)
369       return false;
370     // Additional check (the circle may be fixed if it is used by different constraints):
371     // 1. The circle is used in Equal constraint and another entity is fixed
372     aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
373     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
374       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
375         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
376         if (isEntityFixed(anOther, false))
377           return true;
378       }
379     // 2. Another verifiers ...
380   } else if (myEntities[aPos].type == SLVS_E_ARC_OF_CIRCLE) {
381     if (aNbFixed > 2)
382       return true;
383     else if (aNbFixed <= 1)
384       return false;
385     // Search for Diameter constraint
386     aList = getConstraintsByType(SLVS_C_DIAMETER);
387     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
388       if (anIt->entityA == theEntityID)
389         return true;
390     if (!theAccurate)
391       return false;
392     // Additional check (the arc may be fixed if it is used by different constraints):
393     // 1. The arc is used in Equal constraint and another entity is fixed
394     aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
395     aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
396     for (anIt = aList.begin(); anIt != aList.end(); anIt++)
397       if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
398         Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
399         if (isEntityFixed(anOther, false))
400           return true;
401       }
402     // 2. Another verifiers ...
403   }
404   return false;
405 }
406
407
408 Slvs_hConstraint SketchSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
409 {
410   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
411     // Constraint is already used, rewrite it
412     return updateConstraint(theConstraint);
413   }
414
415   Slvs_Constraint aConstraint = theConstraint;
416
417   // Find a constraint with same type uses same arguments
418   std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
419   for (; aCIt != myConstraints.end(); aCIt++) {
420     if (aConstraint.type != aCIt->type)
421       continue;
422     if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
423         aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
424         aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
425       aConstraint.h = aCIt->h;
426       return updateConstraint(aConstraint);
427     }
428   }
429
430   if (aConstraint.h > myConstrMaxID)
431     myConstrMaxID = aConstraint.h;
432   else
433     aConstraint.h = ++myConstrMaxID;
434   myConstraints.push_back(aConstraint);
435   myNeedToResolve = true;
436   if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
437     addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
438   return aConstraint.h;
439 }
440
441 Slvs_hConstraint SketchSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
442 {
443   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
444     // Constraint already used, rewrite it
445     int aPos = Search(theConstraint.h, myConstraints);
446     if (aPos >= 0 && aPos < (int)myConstraints.size()) {
447       myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
448       myConstraints[aPos] = theConstraint;
449       if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
450         addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
451       return theConstraint.h;
452     }
453   }
454
455   // Constraint is not found, add new one
456   Slvs_Constraint aConstraint = theConstraint;
457   aConstraint.h = 0;
458   return addConstraint(aConstraint);
459 }
460
461 bool SketchSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
462 {
463   bool aResult = true;
464   int aPos = Search(theConstraintID, myConstraints);
465   if (aPos >= 0 && aPos < (int)myConstraints.size()) {
466     Slvs_Constraint aConstraint = myConstraints[aPos];
467     myConstraints.erase(myConstraints.begin() + aPos);
468     myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
469     myNeedToResolve = true;
470     myRemovedConstraints.insert(theConstraintID);
471     // Remove all entities
472     Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
473         aConstraint.entityA, aConstraint.entityB,
474         aConstraint.entityC, aConstraint.entityD};
475     for (int i = 0; i < 6; i++)
476       if (anEntities[i] != SLVS_E_UNKNOWN)
477         aResult = removeEntity(anEntities[i]) && aResult;
478     // remove temporary fixed point, if available
479     if (myFixed == theConstraintID)
480       myFixed = SLVS_E_UNKNOWN;
481   }
482   return aResult;
483 }
484
485 const Slvs_Constraint& SketchSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
486 {
487   int aPos = Search(theConstraintID, myConstraints);
488   if (aPos >= 0 && aPos < (int)myConstraints.size())
489     return myConstraints[aPos];
490
491   // Constraint is not found, return empty object
492   static Slvs_Constraint aDummy;
493   aDummy.h = 0;
494   return aDummy;
495 }
496
497 std::list<Slvs_Constraint> SketchSolver_Storage::getConstraintsByType(int theConstraintType) const
498 {
499   std::list<Slvs_Constraint> aResult;
500   std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
501   for (; aCIter != myConstraints.end(); aCIter++)
502     if (aCIter->type == theConstraintType)
503       aResult.push_back(*aCIter);
504   return aResult;
505 }
506
507
508 void SketchSolver_Storage::addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID)
509 {
510   if (myFixed != SLVS_E_UNKNOWN)
511     return; // the point is already fixed
512   int aPos = Search(theConstraintID, myConstraints);
513   if (aPos >= 0 && aPos < (int)myConstraints.size())
514     myFixed = theConstraintID;
515 }
516
517 void SketchSolver_Storage::addTemporaryConstraint(const Slvs_hConstraint& theConstraintID)
518 {
519   myTemporaryConstraints.insert(theConstraintID);
520 }
521
522 void SketchSolver_Storage::removeTemporaryConstraints()
523 {
524   myTemporaryConstraints.clear();
525 }
526
527 bool SketchSolver_Storage::isTemporary(const Slvs_hConstraint& theConstraintID) const
528 {
529   return myTemporaryConstraints.find(theConstraintID) != myTemporaryConstraints.end();
530 }
531
532
533 void SketchSolver_Storage::getRemoved(
534     std::set<Slvs_hParam>& theParameters,
535     std::set<Slvs_hEntity>& theEntities,
536     std::set<Slvs_hConstraint>& theConstraints)
537 {
538   theParameters = myRemovedParameters;
539   theEntities = myRemovedEntities;
540   theConstraints = myRemovedConstraints;
541
542   myRemovedParameters.clear();
543   myRemovedEntities.clear();
544   myRemovedConstraints.clear();
545 }
546
547 void SketchSolver_Storage::initializeSolver(SketchSolver_Solver& theSolver)
548 {
549   theSolver.setParameters(myParameters.data(), (int)myParameters.size());
550   theSolver.setEntities(myEntities.data(), (int)myEntities.size());
551
552   // Copy constraints excluding the fixed one
553   std::vector<Slvs_Constraint> aConstraints = myConstraints;
554   if (myFixed != SLVS_E_UNKNOWN) {
555     Slvs_hEntity aFixedPoint = SLVS_E_UNKNOWN;
556     std::vector<Slvs_Constraint>::iterator anIt = aConstraints.begin();
557     for (; anIt != aConstraints.end(); anIt++)
558       if (anIt->h == myFixed) {
559         aFixedPoint = anIt->ptA;
560         aConstraints.erase(anIt);
561         break;
562       }
563     // set dragged parameters
564     int aPos = Search(aFixedPoint, myEntities);
565     theSolver.setDraggedParameters(myEntities[aPos].param);
566   }
567   theSolver.setConstraints(aConstraints.data(), (int)aConstraints.size());
568 }
569
570 void SketchSolver_Storage::addCoincidentPoints(
571     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
572 {
573   std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
574   std::vector< std::set<Slvs_hEntity> >::iterator aFoundIter = myCoincidentPoints.end(); // already found coincidence
575   bool isFound = false;
576   for (; aCIter != myCoincidentPoints.end(); aCIter++) {
577     bool isFirstFound = aCIter->find(thePoint1) != aCIter->end();
578     bool isSecondFound = aCIter->find(thePoint2) != aCIter->end();
579     isFound = isFound || isFirstFound || isSecondFound;
580     if (isFirstFound && isSecondFound)
581       break; // already coincident
582     else if (isFirstFound || isSecondFound) {
583       if (aFoundIter != myCoincidentPoints.end()) {
584         // merge two sets
585         aFoundIter->insert(aCIter->begin(), aCIter->end());
586         myCoincidentPoints.erase(aCIter);
587         break;
588       }
589       aCIter->insert(thePoint1);
590       aCIter->insert(thePoint2);
591     }
592   }
593   // coincident points not found
594   if (!isFound) {
595     std::set<Slvs_hEntity> aNewSet;
596     aNewSet.insert(thePoint1);
597     aNewSet.insert(thePoint2);
598     myCoincidentPoints.push_back(aNewSet);
599   }
600 }
601
602 void SketchSolver_Storage::removeCoincidentPoint(const Slvs_hEntity& thePoint)
603 {
604   std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
605   for (; aCIter != myCoincidentPoints.end(); aCIter++)
606     if (aCIter->find(thePoint) != aCIter->end()) {
607       aCIter->erase(thePoint);
608       if (aCIter->size() <= 1)
609         myCoincidentPoints.erase(aCIter);
610       break;
611     }
612 }
613
614 bool SketchSolver_Storage::isCoincident(
615     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
616 {
617   std::vector< std::set<Slvs_hEntity> >::const_iterator aCIter = myCoincidentPoints.begin();
618   for (; aCIter != myCoincidentPoints.end(); aCIter++)
619     if (aCIter->find(thePoint1) != aCIter->end() && aCIter->find(thePoint2) != aCIter->end())
620       return true;
621   return false;
622 }
623
624
625
626
627 // ========================================================
628 // =========      Auxiliary functions       ===============
629 // ========================================================
630
631 template<typename T>
632 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
633 {
634   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
635   int aVecSize = theEntities.size();
636   if (theEntities.empty())
637     return 1;
638   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
639     aResIndex--;
640   while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
641     aResIndex++;
642   if (aResIndex == -1)
643     aResIndex = aVecSize;
644   return aResIndex;
645 }
646
647 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
648 {
649   return fabs(theParam1.val - theParam2.val) > tolerance;
650 }
651
652 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
653 {
654   int i = 0;
655   for (; theEntity1.param[i] != 0 && i < 4; i++)
656     if (theEntity1.param[i] != theEntity2.param[i])
657       return true;
658   i = 0;
659   for (; theEntity1.point[i] != 0 && i < 4; i++)
660     if (theEntity1.point[i] != theEntity2.point[i])
661       return true;
662   return false;
663 }
664
665 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
666 {
667   return theConstraint1.ptA != theConstraint2.ptA ||
668          theConstraint1.ptB != theConstraint2.ptB ||
669          theConstraint1.entityA != theConstraint2.entityA ||
670          theConstraint1.entityB != theConstraint2.entityB ||
671          theConstraint1.entityC != theConstraint2.entityC ||
672          theConstraint1.entityD != theConstraint2.entityD ||
673          fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;
674 }