]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/SketchSolver_Storage.cpp
Salome HOME
9dbf54f0c6aa5bbdd166a615460cc2f63c9e201c
[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_hConstraint SketchSolver_Storage::isPointFixed(const Slvs_hEntity& thePointID) const
196 {
197   // Search the set of coincident points
198   std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
199   for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
200     if (aCPIter->find(thePointID) != aCPIter->end())
201       break;
202   if (aCPIter == myCoincidentPoints.end()) {
203     std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
204     for (; aConstrIter != myConstraints.end(); aConstrIter++)
205       if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
206           aConstrIter->ptA == thePointID)
207         return aConstrIter->h;
208     return SLVS_E_UNKNOWN;
209   }
210
211   // Search the Rigid constraint
212   std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
213   for (; aConstrIter != myConstraints.end(); aConstrIter++)
214     if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
215         aCPIter->find(aConstrIter->ptA) != aCPIter->end())
216       return aConstrIter->h;
217   return SLVS_E_UNKNOWN;
218 }
219
220
221 Slvs_hConstraint SketchSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
222 {
223   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
224     // Constraint is already used, rewrite it
225     return updateConstraint(theConstraint);
226   }
227
228   Slvs_Constraint aConstraint = theConstraint;
229   if (aConstraint.h > myConstrMaxID)
230     myConstrMaxID = aConstraint.h;
231   else
232     aConstraint.h = ++myConstrMaxID;
233   myConstraints.push_back(aConstraint);
234   myNeedToResolve = true;
235   if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
236     addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
237   return aConstraint.h;
238 }
239
240 Slvs_hConstraint SketchSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
241 {
242   if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
243     // Constraint already used, rewrite it
244     int aPos = Search(theConstraint.h, myConstraints);
245     if (aPos >= 0 && aPos < (int)myConstraints.size()) {
246       myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
247       myConstraints[aPos] = theConstraint;
248       if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
249         addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
250       return theConstraint.h;
251     }
252   }
253
254   // Constraint is not found, add new one
255   Slvs_Constraint aConstraint = theConstraint;
256   aConstraint.h = 0;
257   return addConstraint(aConstraint);
258 }
259
260 bool SketchSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
261 {
262   bool aResult = true;
263   int aPos = Search(theConstraintID, myConstraints);
264   if (aPos >= 0 && aPos < (int)myConstraints.size()) {
265     Slvs_Constraint aConstraint = myConstraints[aPos];
266     myConstraints.erase(myConstraints.begin() + aPos);
267     myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
268     myNeedToResolve = true;
269     myRemovedConstraints.insert(theConstraintID);
270     // Remove all entities
271     Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
272         aConstraint.entityA, aConstraint.entityB,
273         aConstraint.entityC, aConstraint.entityD};
274     for (int i = 0; i < 6; i++)
275       if (anEntities[i] != SLVS_E_UNKNOWN)
276         aResult = removeEntity(anEntities[i]) && aResult;
277     // remove temporary fixed point, if available
278     if (myFixed == theConstraintID)
279       myFixed = SLVS_E_UNKNOWN;
280   }
281   return aResult;
282 }
283
284 const Slvs_Constraint& SketchSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
285 {
286   int aPos = Search(theConstraintID, myConstraints);
287   if (aPos >= 0 && aPos < (int)myConstraints.size())
288     return myConstraints[aPos];
289
290   // Constraint is not found, return empty object
291   static Slvs_Constraint aDummy;
292   aDummy.h = 0;
293   return aDummy;
294 }
295
296 void SketchSolver_Storage::addTemporaryConstraint(const Slvs_hConstraint& theConstraintID)
297 {
298   if (myFixed != SLVS_E_UNKNOWN)
299     return; // the point is already fixed
300   int aPos = Search(theConstraintID, myConstraints);
301   if (aPos >= 0 && aPos < (int)myConstraints.size())
302     myFixed = theConstraintID;
303 }
304
305 void SketchSolver_Storage::getRemoved(
306     std::set<Slvs_hParam>& theParameters,
307     std::set<Slvs_hEntity>& theEntities,
308     std::set<Slvs_hConstraint>& theConstraints)
309 {
310   theParameters = myRemovedParameters;
311   theEntities = myRemovedEntities;
312   theConstraints = myRemovedConstraints;
313
314   myRemovedParameters.clear();
315   myRemovedEntities.clear();
316   myRemovedConstraints.clear();
317 }
318
319 void SketchSolver_Storage::initializeSolver(SketchSolver_Solver& theSolver)
320 {
321   theSolver.setParameters(myParameters.data(), (int)myParameters.size());
322   theSolver.setEntities(myEntities.data(), (int)myEntities.size());
323
324   // Copy constraints excluding the fixed one
325   std::vector<Slvs_Constraint> aConstraints = myConstraints;
326   if (myFixed != SLVS_E_UNKNOWN) {
327     Slvs_hEntity aFixedPoint = SLVS_E_UNKNOWN;
328     std::vector<Slvs_Constraint>::iterator anIt = aConstraints.begin();
329     for (; anIt != aConstraints.end(); anIt++)
330       if (anIt->h == myFixed) {
331         aFixedPoint = anIt->ptA;
332         aConstraints.erase(anIt);
333         break;
334       }
335     // set dragged parameters
336     int aPos = Search(aFixedPoint, myEntities);
337     theSolver.setDraggedParameters(myEntities[aPos].param);
338   }
339   theSolver.setConstraints(aConstraints.data(), (int)aConstraints.size());
340 }
341
342 void SketchSolver_Storage::addCoincidentPoints(
343     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
344 {
345   std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
346   std::vector< std::set<Slvs_hEntity> >::iterator aFoundIter = myCoincidentPoints.end(); // already found coincidence
347   bool isFound = false;
348   for (; aCIter != myCoincidentPoints.end(); aCIter++) {
349     bool isFirstFound = aCIter->find(thePoint1) != aCIter->end();
350     bool isSecondFound = aCIter->find(thePoint2) != aCIter->end();
351     isFound = isFound || isFirstFound || isSecondFound;
352     if (isFirstFound && isSecondFound)
353       break; // already coincident
354     else if (isFirstFound || isSecondFound) {
355       if (aFoundIter != myCoincidentPoints.end()) {
356         // merge two sets
357         aFoundIter->insert(aCIter->begin(), aCIter->end());
358         myCoincidentPoints.erase(aCIter);
359         break;
360       }
361       aCIter->insert(thePoint1);
362       aCIter->insert(thePoint2);
363     }
364   }
365   // coincident points not found
366   if (!isFound) {
367     std::set<Slvs_hEntity> aNewSet;
368     aNewSet.insert(thePoint1);
369     aNewSet.insert(thePoint2);
370     myCoincidentPoints.push_back(aNewSet);
371   }
372 }
373
374 void SketchSolver_Storage::removeCoincidentPoint(const Slvs_hEntity& thePoint)
375 {
376   std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
377   for (; aCIter != myCoincidentPoints.end(); aCIter++)
378     if (aCIter->find(thePoint) != aCIter->end()) {
379       aCIter->erase(thePoint);
380       if (aCIter->size() <= 1)
381         myCoincidentPoints.erase(aCIter);
382       break;
383     }
384 }
385
386 bool SketchSolver_Storage::isCoincident(
387     const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
388 {
389   std::vector< std::set<Slvs_hEntity> >::const_iterator aCIter = myCoincidentPoints.begin();
390   for (; aCIter != myCoincidentPoints.end(); aCIter++)
391     if (aCIter->find(thePoint1) != aCIter->end() && aCIter->find(thePoint2) != aCIter->end())
392       return true;
393   return false;
394 }
395
396
397
398
399 // ========================================================
400 // =========      Auxiliary functions       ===============
401 // ========================================================
402
403 template<typename T>
404 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
405 {
406   int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
407   int aVecSize = theEntities.size();
408   if (theEntities.empty())
409     return 1;
410   while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
411     aResIndex--;
412   while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
413     aResIndex++;
414   if (aResIndex == -1)
415     aResIndex = aVecSize;
416   return aResIndex;
417 }
418
419 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
420 {
421   return fabs(theParam1.val - theParam2.val) > tolerance;
422 }
423
424 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
425 {
426   int i = 0;
427   for (; theEntity1.param[i] != 0 && i < 4; i++)
428     if (theEntity1.param[i] != theEntity2.param[i])
429       return true;
430   i = 0;
431   for (; theEntity1.point[i] != 0 && i < 4; i++)
432     if (theEntity1.point[i] != theEntity2.point[i])
433       return true;
434   return false;
435 }
436
437 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
438 {
439   return theConstraint1.ptA != theConstraint2.ptA ||
440          theConstraint1.ptB != theConstraint2.ptB ||
441          theConstraint1.entityA != theConstraint2.entityA ||
442          theConstraint1.entityB != theConstraint2.entityB ||
443          theConstraint1.entityC != theConstraint2.entityC ||
444          theConstraint1.entityD != theConstraint2.entityD ||
445          fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;
446 }