Salome HOME
f13ad6b6f861d5aa026a06407c1fa94337686509
[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 Slvs_hConstraint SketchSolver_Storage::isPointFixed(const Slvs_hEntity& thePointID) const
250 {
251   // Search the set of coincident points
252   std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
253   for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
254     if (aCPIter->find(thePointID) != aCPIter->end())
255       break;
256   if (aCPIter == myCoincidentPoints.end()) {
257     std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
258     for (; aConstrIter != myConstraints.end(); aConstrIter++)
259       if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
260           aConstrIter->ptA == thePointID)
261         return aConstrIter->h;
262     return SLVS_E_UNKNOWN;
263   }
264
265   // Search the Rigid constraint
266   std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
267   for (; aConstrIter != myConstraints.end(); aConstrIter++)
268     if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
269         aCPIter->find(aConstrIter->ptA) != aCPIter->end())
270       return aConstrIter->h;
271   return SLVS_E_UNKNOWN;
272 }
273
274
275 Slvs_hConstraint SketchSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
276 {
277   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
278     // Constraint is already used, rewrite it
279     return updateConstraint(theConstraint);
280   }
281
282   Slvs_Constraint aConstraint = theConstraint;
283
284   // Find a constraint with same type uses same arguments
285   std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
286   for (; aCIt != myConstraints.end(); aCIt++) {
287     if (aConstraint.type != aCIt->type)
288       continue;
289     if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
290         aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
291         aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD) {
292       aConstraint.h = aCIt->h;
293       return updateConstraint(aConstraint);
294     }
295   }
296
297   if (aConstraint.h > myConstrMaxID)
298     myConstrMaxID = aConstraint.h;
299   else
300     aConstraint.h = ++myConstrMaxID;
301   myConstraints.push_back(aConstraint);
302   myNeedToResolve = true;
303   if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
304     addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
305   return aConstraint.h;
306 }
307
308 Slvs_hConstraint SketchSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
309 {
310   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
311     // Constraint already used, rewrite it
312     int aPos = Search(theConstraint.h, myConstraints);
313     if (aPos >= 0 && aPos < (int)myConstraints.size()) {
314       myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
315       myConstraints[aPos] = theConstraint;
316       if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
317         addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
318       return theConstraint.h;
319     }
320   }
321
322   // Constraint is not found, add new one
323   Slvs_Constraint aConstraint = theConstraint;
324   aConstraint.h = 0;
325   return addConstraint(aConstraint);
326 }
327
328 bool SketchSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
329 {
330   bool aResult = true;
331   int aPos = Search(theConstraintID, myConstraints);
332   if (aPos >= 0 && aPos < (int)myConstraints.size()) {
333     Slvs_Constraint aConstraint = myConstraints[aPos];
334     myConstraints.erase(myConstraints.begin() + aPos);
335     myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
336     myNeedToResolve = true;
337     myRemovedConstraints.insert(theConstraintID);
338     // Remove all entities
339     Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
340         aConstraint.entityA, aConstraint.entityB,
341         aConstraint.entityC, aConstraint.entityD};
342     for (int i = 0; i < 6; i++)
343       if (anEntities[i] != SLVS_E_UNKNOWN)
344         aResult = removeEntity(anEntities[i]) && aResult;
345     // remove temporary fixed point, if available
346     if (myFixed == theConstraintID)
347       myFixed = SLVS_E_UNKNOWN;
348   }
349   return aResult;
350 }
351
352 const Slvs_Constraint& SketchSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
353 {
354   int aPos = Search(theConstraintID, myConstraints);
355   if (aPos >= 0 && aPos < (int)myConstraints.size())
356     return myConstraints[aPos];
357
358   // Constraint is not found, return empty object
359   static Slvs_Constraint aDummy;
360   aDummy.h = 0;
361   return aDummy;
362 }
363
364 std::list<Slvs_Constraint> SketchSolver_Storage::getConstraintsByType(int theConstraintType) const
365 {
366   std::list<Slvs_Constraint> aResult;
367   std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
368   for (; aCIter != myConstraints.end(); aCIter++)
369     if (aCIter->type == theConstraintType)
370       aResult.push_back(*aCIter);
371   return aResult;
372 }
373
374
375 void SketchSolver_Storage::addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID)
376 {
377   if (myFixed != SLVS_E_UNKNOWN)
378     return; // the point is already fixed
379   int aPos = Search(theConstraintID, myConstraints);
380   if (aPos >= 0 && aPos < (int)myConstraints.size())
381     myFixed = theConstraintID;
382 }
383
384 void SketchSolver_Storage::addTemporaryConstraint(const Slvs_hConstraint& theConstraintID)
385 {
386   myTemporaryConstraints.insert(theConstraintID);
387 }
388
389 void SketchSolver_Storage::removeTemporaryConstraints()
390 {
391   myTemporaryConstraints.clear();
392 }
393
394 bool SketchSolver_Storage::isTemporary(const Slvs_hConstraint& theConstraintID) const
395 {
396   return myTemporaryConstraints.find(theConstraintID) != myTemporaryConstraints.end();
397 }
398
399
400 void SketchSolver_Storage::getRemoved(
401     std::set<Slvs_hParam>& theParameters,
402     std::set<Slvs_hEntity>& theEntities,
403     std::set<Slvs_hConstraint>& theConstraints)
404 {
405   theParameters = myRemovedParameters;
406   theEntities = myRemovedEntities;
407   theConstraints = myRemovedConstraints;
408
409   myRemovedParameters.clear();
410   myRemovedEntities.clear();
411   myRemovedConstraints.clear();
412 }
413
414 void SketchSolver_Storage::initializeSolver(SketchSolver_Solver& theSolver)
415 {
416   theSolver.setParameters(myParameters.data(), (int)myParameters.size());
417   theSolver.setEntities(myEntities.data(), (int)myEntities.size());
418
419   // Copy constraints excluding the fixed one
420   std::vector<Slvs_Constraint> aConstraints = myConstraints;
421   if (myFixed != SLVS_E_UNKNOWN) {
422     Slvs_hEntity aFixedPoint = SLVS_E_UNKNOWN;
423     std::vector<Slvs_Constraint>::iterator anIt = aConstraints.begin();
424     for (; anIt != aConstraints.end(); anIt++)
425       if (anIt->h == myFixed) {
426         aFixedPoint = anIt->ptA;
427         aConstraints.erase(anIt);
428         break;
429       }
430     // set dragged parameters
431     int aPos = Search(aFixedPoint, myEntities);
432     theSolver.setDraggedParameters(myEntities[aPos].param);
433   }
434   theSolver.setConstraints(aConstraints.data(), (int)aConstraints.size());
435 }
436
437 void SketchSolver_Storage::addCoincidentPoints(
438     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
439 {
440   std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
441   std::vector< std::set<Slvs_hEntity> >::iterator aFoundIter = myCoincidentPoints.end(); // already found coincidence
442   bool isFound = false;
443   for (; aCIter != myCoincidentPoints.end(); aCIter++) {
444     bool isFirstFound = aCIter->find(thePoint1) != aCIter->end();
445     bool isSecondFound = aCIter->find(thePoint2) != aCIter->end();
446     isFound = isFound || isFirstFound || isSecondFound;
447     if (isFirstFound && isSecondFound)
448       break; // already coincident
449     else if (isFirstFound || isSecondFound) {
450       if (aFoundIter != myCoincidentPoints.end()) {
451         // merge two sets
452         aFoundIter->insert(aCIter->begin(), aCIter->end());
453         myCoincidentPoints.erase(aCIter);
454         break;
455       }
456       aCIter->insert(thePoint1);
457       aCIter->insert(thePoint2);
458     }
459   }
460   // coincident points not found
461   if (!isFound) {
462     std::set<Slvs_hEntity> aNewSet;
463     aNewSet.insert(thePoint1);
464     aNewSet.insert(thePoint2);
465     myCoincidentPoints.push_back(aNewSet);
466   }
467 }
468
469 void SketchSolver_Storage::removeCoincidentPoint(const Slvs_hEntity& thePoint)
470 {
471   std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
472   for (; aCIter != myCoincidentPoints.end(); aCIter++)
473     if (aCIter->find(thePoint) != aCIter->end()) {
474       aCIter->erase(thePoint);
475       if (aCIter->size() <= 1)
476         myCoincidentPoints.erase(aCIter);
477       break;
478     }
479 }
480
481 bool SketchSolver_Storage::isCoincident(
482     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
483 {
484   std::vector< std::set<Slvs_hEntity> >::const_iterator aCIter = myCoincidentPoints.begin();
485   for (; aCIter != myCoincidentPoints.end(); aCIter++)
486     if (aCIter->find(thePoint1) != aCIter->end() && aCIter->find(thePoint2) != aCIter->end())
487       return true;
488   return false;
489 }
490
491
492
493
494 // ========================================================
495 // =========      Auxiliary functions       ===============
496 // ========================================================
497
498 template<typename T>
499 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
500 {
501   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
502   int aVecSize = theEntities.size();
503   if (theEntities.empty())
504     return 1;
505   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
506     aResIndex--;
507   while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
508     aResIndex++;
509   if (aResIndex == -1)
510     aResIndex = aVecSize;
511   return aResIndex;
512 }
513
514 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
515 {
516   return fabs(theParam1.val - theParam2.val) > tolerance;
517 }
518
519 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
520 {
521   int i = 0;
522   for (; theEntity1.param[i] != 0 && i < 4; i++)
523     if (theEntity1.param[i] != theEntity2.param[i])
524       return true;
525   i = 0;
526   for (; theEntity1.point[i] != 0 && i < 4; i++)
527     if (theEntity1.point[i] != theEntity2.point[i])
528       return true;
529   return false;
530 }
531
532 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
533 {
534   return theConstraint1.ptA != theConstraint2.ptA ||
535          theConstraint1.ptB != theConstraint2.ptB ||
536          theConstraint1.entityA != theConstraint2.entityA ||
537          theConstraint1.entityB != theConstraint2.entityB ||
538          theConstraint1.entityC != theConstraint2.entityC ||
539          theConstraint1.entityD != theConstraint2.entityD ||
540          fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;
541 }