1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: SketchSolver_Storage.cpp
4 // Created: 18 Mar 2015
5 // Author: Artem ZHIDKOV
7 #include <SketchSolver_Storage.h>
9 #include <GeomAPI_Pnt2d.h>
10 #include <GeomAPI_XY.h>
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
19 static int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities);
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);
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)
39 Slvs_hParam SketchSolver_Storage::addParameter(const Slvs_Param& theParam)
41 if (theParam.h > 0 && theParam.h <= myParamMaxID) {
42 // parameter is already used, rewrite it
43 return updateParameter(theParam);
46 Slvs_Param aParam = theParam;
47 if (aParam.h > myParamMaxID)
48 myParamMaxID = aParam.h;
50 aParam.h = ++myParamMaxID;
51 myParameters.push_back(aParam);
52 myNeedToResolve = true;
56 Slvs_hParam SketchSolver_Storage::updateParameter(const Slvs_Param& theParam)
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;
68 // Parameter is not found, add new one
69 Slvs_Param aParam = theParam;
71 return addParameter(aParam);
74 bool SketchSolver_Storage::removeParameter(const Slvs_hParam& theParamID)
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)
86 myParameters.erase(myParameters.begin() + aPos);
87 myParamMaxID = myParameters.empty() ? SLVS_E_UNKNOWN : myParameters.back().h;
88 myNeedToResolve = true;
89 myRemovedParameters.insert(theParamID);
95 const Slvs_Param& SketchSolver_Storage::getParameter(const Slvs_hParam& theParamID) const
97 int aPos = Search(theParamID, myParameters);
98 if (aPos >= 0 && aPos < (int)myParameters.size())
99 return myParameters[aPos];
101 // Parameter is not found, return empty object
102 static Slvs_Param aDummy;
108 Slvs_hEntity SketchSolver_Storage::addEntity(const Slvs_Entity& theEntity)
110 if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
111 // Entity is already used, rewrite it
112 return updateEntity(theEntity);
115 Slvs_Entity aEntity = theEntity;
116 if (aEntity.h > myEntityMaxID)
117 myEntityMaxID = aEntity.h;
119 aEntity.h = ++myEntityMaxID;
120 myEntities.push_back(aEntity);
121 myNeedToResolve = true;
125 Slvs_hEntity SketchSolver_Storage::updateEntity(const Slvs_Entity& theEntity)
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;
137 // Entity is not found, add new one
138 Slvs_Entity aEntity = theEntity;
140 return addEntity(aEntity);
143 bool SketchSolver_Storage::removeEntity(const Slvs_hEntity& theEntityID)
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]);
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())
160 if (anEntAndSubs.find(anEntIter->distance) != anEntAndSubs.end())
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())
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);
192 void SketchSolver_Storage::removeUnusedEntities()
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);
203 anUnusedEntities.insert(aEIt->h);
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);
227 std::set<Slvs_hEntity>::const_iterator anEntIt = anUnusedEntities.begin();
228 while (anEntIt != anUnusedEntities.end()) {
229 int aPos = Search(*anEntIt, myEntities);
230 if (aPos < 0 && aPos >= (int)myEntities.size())
232 Slvs_Entity anEntity = myEntities[aPos];
233 // Remove entity if and only if all its parameters unused
235 if (anEntity.distance != SLVS_E_UNKNOWN &&
236 anUnusedEntities.find(anEntity.distance) == anUnusedEntities.end())
238 for (int i = 0; i < 4 && !isUsed; i++)
239 if (anEntity.point[i] != SLVS_E_UNKNOWN &&
240 anUnusedEntities.find(anEntity.point[i]) == anUnusedEntities.end())
243 anUnusedEntities.erase(anEntity.distance);
244 for (int i = 0; i < 4; i++)
245 if (anEntity.point[i] != SLVS_E_UNKNOWN)
246 anUnusedEntities.erase(anEntity.point[i]);
247 std::set<Slvs_hEntity>::iterator aRemoveIt = anEntIt++;
248 anUnusedEntities.erase(aRemoveIt);
254 for (anEntIt = anUnusedEntities.begin(); anEntIt != anUnusedEntities.end(); ++anEntIt) {
255 int aPos = Search(*anEntIt, myEntities);
256 if (aPos >= 0 && aPos < (int)myEntities.size()) {
257 // Remove entity and its parameters
258 Slvs_Entity anEntity = myEntities[aPos];
259 myEntities.erase(myEntities.begin() + aPos);
260 myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
261 if (anEntity.distance != SLVS_E_UNKNOWN)
262 removeParameter(anEntity.distance);
263 for (int i = 0; i < 4; i++)
264 if (anEntity.param[i] != SLVS_E_UNKNOWN)
265 removeParameter(anEntity.param[i]);
266 for (int i = 0; i < 4; i++)
267 if (anEntity.point[i] != SLVS_E_UNKNOWN)
268 removeEntity(anEntity.point[i]);
269 myRemovedEntities.insert(*anEntIt);
270 if (anEntity.type == SLVS_E_POINT_IN_2D || anEntity.type == SLVS_E_POINT_IN_3D)
271 removeCoincidentPoint(*anEntIt);
275 if (!anUnusedEntities.empty())
276 myNeedToResolve = true;
279 const Slvs_Entity& SketchSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
281 int aPos = Search(theEntityID, myEntities);
282 if (aPos >= 0 && aPos < (int)myEntities.size())
283 return myEntities[aPos];
285 // Entity is not found, return empty object
286 static Slvs_Entity aDummy;
287 aDummy.h = SLVS_E_UNKNOWN;
291 Slvs_hEntity SketchSolver_Storage::copyEntity(const Slvs_hEntity& theCopied)
293 int aPos = Search(theCopied, myEntities);
294 if (aPos < 0 || aPos >= (int)myEntities.size())
295 return SLVS_E_UNKNOWN;
297 Slvs_Entity aCopy = myEntities[aPos];
298 aCopy.h = SLVS_E_UNKNOWN;
300 while (aCopy.point[i] != SLVS_E_UNKNOWN) {
301 aCopy.point[i] = copyEntity(aCopy.point[i]);
304 if (aCopy.param[0] != SLVS_E_UNKNOWN) {
305 aPos = Search(aCopy.param[0], myParameters);
307 while (aCopy.param[i] != SLVS_E_UNKNOWN) {
308 Slvs_Param aNewParam = myParameters[aPos];
309 aNewParam.h = SLVS_E_UNKNOWN;
310 aCopy.param[i] = addParameter(aNewParam);
315 return addEntity(aCopy);
318 void SketchSolver_Storage::copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo)
320 int aPosFrom = Search(theFrom, myEntities);
321 int aPosTo = Search(theTo, myEntities);
322 if (aPosFrom < 0 || aPosFrom >= (int)myEntities.size() ||
323 aPosTo < 0 || aPosTo >= (int)myEntities.size())
326 Slvs_Entity aEntFrom = myEntities[aPosFrom];
327 Slvs_Entity aEntTo = myEntities[aPosTo];
329 while (aEntFrom.point[i] != SLVS_E_UNKNOWN) {
330 copyEntity(aEntFrom.point[i], aEntTo.point[i]);
333 if (aEntFrom.param[0] != SLVS_E_UNKNOWN) {
334 aPosFrom = Search(aEntFrom.param[0], myParameters);
335 aPosTo = Search(aEntTo.param[0], myParameters);
337 while (aEntFrom.param[i] != SLVS_E_UNKNOWN) {
338 myParameters[aPosTo++].val = myParameters[aPosFrom++].val;
345 bool SketchSolver_Storage::isPointFixed(
346 const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate) const
348 // Search the set of coincident points
349 std::set<Slvs_hEntity> aCoincident;
350 aCoincident.insert(thePointID);
351 std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
352 for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
353 if (aCPIter->find(thePointID) != aCPIter->end()) {
354 aCoincident = *aCPIter;
358 // Search the Rigid constraint
359 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
360 for (; aConstrIter != myConstraints.end(); aConstrIter++)
361 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
362 aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
363 theFixed = aConstrIter->h;
368 // Try to find the fixed entity which uses such point or its coincidence
369 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
370 for (; anEntIter != myEntities.end(); anEntIter++) {
371 for (int i = 0; i < 4; i++) {
372 Slvs_hEntity aPt = anEntIter->point[i];
373 if (aPt != SLVS_E_UNKNOWN &&
374 (aPt == thePointID || aCoincident.find(aPt) != aCoincident.end())) {
375 if (isEntityFixed(anEntIter->h, true))
381 return SLVS_E_UNKNOWN;
384 bool SketchSolver_Storage::isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate) const
386 int aPos = Search(theEntityID, myEntities);
387 if (aPos < 0 || aPos >= (int)myEntities.size())
390 // Firstly, find how many points are under Rigid constraint
392 for (int i = 0; i < 4; i++) {
393 Slvs_hEntity aPoint = myEntities[aPos].point[i];
394 if (aPoint == SLVS_E_UNKNOWN)
397 std::set<Slvs_hEntity> aCoincident;
398 aCoincident.insert(aPoint);
399 std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
400 for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
401 if (aCPIter->find(aPoint) != aCPIter->end()) {
402 aCoincident = *aCPIter;
406 // Search the Rigid constraint
407 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
408 for (; aConstrIter != myConstraints.end(); aConstrIter++)
409 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
410 aCoincident.find(aConstrIter->ptA) != aCoincident.end())
414 std::list<Slvs_Constraint> aList;
415 std::list<Slvs_Constraint>::iterator anIt;
416 Slvs_hConstraint aTempID; // used in isPointFixed() method
418 if (myEntities[aPos].type == SLVS_E_LINE_SEGMENT) {
421 else if (aNbFixed == 0 || !theAccurate)
423 // Additional check (the line may be fixed if it is used by different constraints):
424 // 1. The line is used in Equal constraint, another entity is fixed and there is a fixed point on line
425 aList = getConstraintsByType(SLVS_C_PT_ON_LINE);
426 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
427 if (anIt->entityA == theEntityID && isPointFixed(anIt->ptA, aTempID))
429 if (anIt != aList.end()) {
430 aList = getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
431 aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
432 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
433 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
434 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
435 if (isEntityFixed(anOther, false))
439 // 2. The line is used in Parallel/Perpendicular/Vertical/Horizontal and Length constraints
440 aList = getConstraintsByType(SLVS_C_PARALLEL);
441 aList.splice(aList.end(), getConstraintsByType(SLVS_C_PERPENDICULAR));
442 aList.splice(aList.end(), getConstraintsByType(SLVS_C_VERTICAL));
443 aList.splice(aList.end(), getConstraintsByType(SLVS_C_HORIZONTAL));
444 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
445 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
446 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
447 if (isEntityFixed(anOther, false))
450 if (anIt != aList.end()) {
451 aList = getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
452 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
453 if ((anIt->ptA == myEntities[aPos].point[0] && anIt->ptB == myEntities[aPos].point[1]) ||
454 (anIt->ptA == myEntities[aPos].point[1] && anIt->ptB == myEntities[aPos].point[0]))
457 // 3. Another verifiers ...
458 } else if (myEntities[aPos].type == SLVS_E_CIRCLE) {
461 // Search for Diameter constraint
462 aList = getConstraintsByType(SLVS_C_DIAMETER);
463 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
464 if (anIt->entityA == theEntityID)
468 // Additional check (the circle may be fixed if it is used by different constraints):
469 // 1. The circle is used in Equal constraint and another entity is fixed
470 aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
471 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
472 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
473 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
474 if (isEntityFixed(anOther, false))
477 // 2. Another verifiers ...
478 } else if (myEntities[aPos].type == SLVS_E_ARC_OF_CIRCLE) {
481 else if (aNbFixed <= 1)
483 // Search for Diameter constraint
484 aList = getConstraintsByType(SLVS_C_DIAMETER);
485 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
486 if (anIt->entityA == theEntityID)
490 // Additional check (the arc may be fixed if it is used by different constraints):
491 // 1. The arc is used in Equal constraint and another entity is fixed
492 aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
493 aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
494 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
495 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
496 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
497 if (isEntityFixed(anOther, false))
500 // 2. Another verifiers ...
506 Slvs_hConstraint SketchSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
508 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
509 // Constraint is already used, rewrite it
510 return updateConstraint(theConstraint);
513 Slvs_Constraint aConstraint = theConstraint;
515 // Find a constraint with same type uses same arguments to show user overconstraint situation
516 std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
517 for (; aCIt != myConstraints.end(); aCIt++) {
518 if (aConstraint.type != aCIt->type)
520 if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
521 aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
522 aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD)
523 myDuplicatedConstraint = true;
526 if (aConstraint.h > myConstrMaxID)
527 myConstrMaxID = aConstraint.h;
529 aConstraint.h = ++myConstrMaxID;
530 myConstraints.push_back(aConstraint);
531 myNeedToResolve = true;
532 if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
533 addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
534 return aConstraint.h;
537 Slvs_hConstraint SketchSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
539 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
540 // Constraint already used, rewrite it
541 int aPos = Search(theConstraint.h, myConstraints);
542 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
543 myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
544 myConstraints[aPos] = theConstraint;
545 if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
546 addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
547 return theConstraint.h;
551 // Constraint is not found, add new one
552 Slvs_Constraint aConstraint = theConstraint;
554 return addConstraint(aConstraint);
557 bool SketchSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
560 int aPos = Search(theConstraintID, myConstraints);
561 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
562 Slvs_Constraint aConstraint = myConstraints[aPos];
563 myConstraints.erase(myConstraints.begin() + aPos);
564 myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
565 myNeedToResolve = true;
566 myRemovedConstraints.insert(theConstraintID);
567 // Remove all entities
568 Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
569 aConstraint.entityA, aConstraint.entityB,
570 aConstraint.entityC, aConstraint.entityD};
571 for (int i = 0; i < 6; i++)
572 if (anEntities[i] != SLVS_E_UNKNOWN)
573 aResult = removeEntity(anEntities[i]) && aResult;
574 // remove temporary fixed point, if available
575 if (myFixed == theConstraintID)
576 myFixed = SLVS_E_UNKNOWN;
577 if (myDuplicatedConstraint) {
578 // Check the duplicated constraints are still available
579 myDuplicatedConstraint = false;
580 std::vector<Slvs_Constraint>::const_iterator anIt1 = myConstraints.begin();
581 std::vector<Slvs_Constraint>::const_iterator anIt2 = myConstraints.begin();
582 for (; anIt1 != myConstraints.end() && !myDuplicatedConstraint; anIt1++)
583 for (anIt2 = anIt1+1; anIt2 != myConstraints.end() && !myDuplicatedConstraint; anIt2++) {
584 if (anIt1->type != anIt2->type)
586 if (anIt1->ptA == anIt2->ptA && anIt1->ptB == anIt2->ptB &&
587 anIt1->entityA == anIt2->entityA && anIt1->entityB == anIt2->entityB &&
588 anIt1->entityC == anIt2->entityC && anIt1->entityD == anIt2->entityD)
589 myDuplicatedConstraint = true;
596 const Slvs_Constraint& SketchSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
598 int aPos = Search(theConstraintID, myConstraints);
599 if (aPos >= 0 && aPos < (int)myConstraints.size())
600 return myConstraints[aPos];
602 // Constraint is not found, return empty object
603 static Slvs_Constraint aDummy;
608 std::list<Slvs_Constraint> SketchSolver_Storage::getConstraintsByType(int theConstraintType) const
610 std::list<Slvs_Constraint> aResult;
611 std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
612 for (; aCIter != myConstraints.end(); aCIter++)
613 if (aCIter->type == theConstraintType)
614 aResult.push_back(*aCIter);
619 void SketchSolver_Storage::addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID)
621 if (myFixed != SLVS_E_UNKNOWN)
622 return; // the point is already fixed
623 int aPos = Search(theConstraintID, myConstraints);
624 if (aPos >= 0 && aPos < (int)myConstraints.size())
625 myFixed = theConstraintID;
628 void SketchSolver_Storage::addTemporaryConstraint(const Slvs_hConstraint& theConstraintID)
630 myTemporaryConstraints.insert(theConstraintID);
633 void SketchSolver_Storage::removeTemporaryConstraints()
635 myTemporaryConstraints.clear();
638 int SketchSolver_Storage::deleteTemporaryConstraint()
640 if (myTemporaryConstraints.empty())
642 // Search the point-on-line or a non-rigid constraint
643 std::set<Slvs_hConstraint>::iterator aCIt = myTemporaryConstraints.begin();
644 for (; aCIt != myTemporaryConstraints.end(); aCIt++) {
645 int aPos = Search(*aCIt, myConstraints);
646 if (aPos >= (int)myConstraints.size() || myConstraints[aPos].type != SLVS_C_WHERE_DRAGGED)
648 std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
649 for (; anIt != myConstraints.end(); anIt++)
650 if (anIt->type == SLVS_C_PT_ON_LINE && anIt->ptA == myConstraints[aPos].ptA)
652 if (anIt != myConstraints.end())
655 if (aCIt == myTemporaryConstraints.end())
656 aCIt = myTemporaryConstraints.begin();
657 bool aNewFixed = (*aCIt == myFixed);
658 removeConstraint(*aCIt);
659 myTemporaryConstraints.erase(aCIt);
661 for (aCIt = myTemporaryConstraints.begin(); aCIt != myTemporaryConstraints.end(); aCIt++) {
662 int aPos = Search(*aCIt, myConstraints);
663 if (myConstraints[aPos].type == SLVS_C_WHERE_DRAGGED) {
669 return (int)myTemporaryConstraints.size();
672 bool SketchSolver_Storage::isTemporary(const Slvs_hConstraint& theConstraintID) const
674 return myTemporaryConstraints.find(theConstraintID) != myTemporaryConstraints.end();
678 void SketchSolver_Storage::getRemoved(
679 std::set<Slvs_hParam>& theParameters,
680 std::set<Slvs_hEntity>& theEntities,
681 std::set<Slvs_hConstraint>& theConstraints)
683 theParameters = myRemovedParameters;
684 theEntities = myRemovedEntities;
685 theConstraints = myRemovedConstraints;
687 myRemovedParameters.clear();
688 myRemovedEntities.clear();
689 myRemovedConstraints.clear();
692 void SketchSolver_Storage::initializeSolver(SketchSolver_Solver& theSolver)
694 theSolver.setParameters(myParameters.data(), (int)myParameters.size());
695 theSolver.setEntities(myEntities.data(), (int)myEntities.size());
697 // Copy constraints excluding the fixed one
698 std::vector<Slvs_Constraint> aConstraints = myConstraints;
699 if (myFixed != SLVS_E_UNKNOWN) {
700 Slvs_hEntity aFixedPoint = SLVS_E_UNKNOWN;
701 std::vector<Slvs_Constraint>::iterator anIt = aConstraints.begin();
702 for (; anIt != aConstraints.end(); anIt++)
703 if (anIt->h == myFixed) {
704 aFixedPoint = anIt->ptA;
705 aConstraints.erase(anIt);
708 // set dragged parameters
709 int aPos = Search(aFixedPoint, myEntities);
710 theSolver.setDraggedParameters(myEntities[aPos].param);
712 theSolver.setConstraints(aConstraints.data(), (int)aConstraints.size());
715 void SketchSolver_Storage::addCoincidentPoints(
716 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
718 std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
719 std::vector< std::set<Slvs_hEntity> >::iterator aFoundIter = myCoincidentPoints.end(); // already found coincidence
720 bool isFound = false;
721 for (; aCIter != myCoincidentPoints.end(); aCIter++) {
722 bool isFirstFound = aCIter->find(thePoint1) != aCIter->end();
723 bool isSecondFound = aCIter->find(thePoint2) != aCIter->end();
724 isFound = isFound || isFirstFound || isSecondFound;
725 if (isFirstFound && isSecondFound)
726 break; // already coincident
727 else if (isFirstFound || isSecondFound) {
728 if (aFoundIter != myCoincidentPoints.end()) {
730 aFoundIter->insert(aCIter->begin(), aCIter->end());
731 myCoincidentPoints.erase(aCIter);
735 aCIter->insert(thePoint1);
736 aCIter->insert(thePoint2);
739 // coincident points not found
741 std::set<Slvs_hEntity> aNewSet;
742 aNewSet.insert(thePoint1);
743 aNewSet.insert(thePoint2);
744 myCoincidentPoints.push_back(aNewSet);
748 void SketchSolver_Storage::removeCoincidentPoint(const Slvs_hEntity& thePoint)
750 std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
751 for (; aCIter != myCoincidentPoints.end(); aCIter++)
752 if (aCIter->find(thePoint) != aCIter->end()) {
753 aCIter->erase(thePoint);
754 if (aCIter->size() <= 1)
755 myCoincidentPoints.erase(aCIter);
760 bool SketchSolver_Storage::isCoincident(
761 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
763 std::vector< std::set<Slvs_hEntity> >::const_iterator aCIter = myCoincidentPoints.begin();
764 for (; aCIter != myCoincidentPoints.end(); aCIter++)
765 if (aCIter->find(thePoint1) != aCIter->end() && aCIter->find(thePoint2) != aCIter->end())
770 bool SketchSolver_Storage::isEqual(
771 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
773 if (isCoincident(thePoint1, thePoint2))
776 // Precise checking of coincidence: verify that points have equal coordinates
777 int aEnt1Pos = Search(thePoint1, myEntities);
778 int aEnt2Pos = Search(thePoint2, myEntities);
779 if (aEnt1Pos >= 0 && aEnt1Pos < (int)myEntities.size() &&
780 aEnt2Pos >= 0 && aEnt2Pos < (int)myEntities.size()) {
783 for (int i = 0; i < 2; i++) {
784 aParamPos = Search(myEntities[aEnt1Pos].param[i], myParameters);
785 aDist[i] = myParameters[aParamPos].val;
786 aParamPos = Search(myEntities[aEnt2Pos].param[i], myParameters);
787 aDist[i] -= myParameters[aParamPos].val;
789 if (aDist[0] * aDist[0] + aDist[1] * aDist[1] < tolerance * tolerance)
796 std::vector<Slvs_hConstraint> SketchSolver_Storage::fixEntity(const Slvs_hEntity& theEntity)
798 std::vector<Slvs_hConstraint> aNewConstraints;
800 int aPos = Search(theEntity, myEntities);
801 if (aPos >= 0 && aPos < (int)myEntities.size()) {
802 switch (myEntities[aPos].type) {
803 case SLVS_E_POINT_IN_2D:
804 case SLVS_E_POINT_IN_3D:
805 fixPoint(myEntities[aPos], aNewConstraints);
807 case SLVS_E_LINE_SEGMENT:
808 fixLine(myEntities[aPos], aNewConstraints);
811 fixCircle(myEntities[aPos], aNewConstraints);
813 case SLVS_E_ARC_OF_CIRCLE:
814 fixArc(myEntities[aPos], aNewConstraints);
821 return aNewConstraints;
824 void SketchSolver_Storage::fixPoint(const Slvs_Entity& thePoint,
825 std::vector<Slvs_hConstraint>& theCreated)
827 Slvs_Constraint aConstraint;
828 Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
829 bool isFixed = isPointFixed(thePoint.h, aConstrID, true);
830 bool isForceUpdate = (isFixed && isTemporary(aConstrID));
831 if (!isForceUpdate) { // create new constraint
833 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, thePoint.group, SLVS_C_WHERE_DRAGGED, thePoint.wrkpl,
834 0.0, thePoint.h, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
835 aConstraint.h = addConstraint(aConstraint);
836 theCreated.push_back(aConstraint.h);
837 } else { // update already existent constraint
838 if (!isFixed || aConstrID == SLVS_E_UNKNOWN)
840 int aPos = Search(aConstrID, myConstraints);
841 if (aPos >= 0 && aPos < (int)myConstraints.size())
842 myConstraints[aPos].ptA = thePoint.h;
846 void SketchSolver_Storage::fixLine(const Slvs_Entity& theLine,
847 std::vector<Slvs_hConstraint>& theCreated)
849 Slvs_Entity aPoint[2] = {
850 getEntity(theLine.point[0]),
851 getEntity(theLine.point[1])
854 Slvs_Constraint anEqual;
855 if (isAxisParallel(theLine.h)) {
856 // Fix one point and a line length
857 Slvs_hConstraint aFixed;
858 if (!isPointFixed(theLine.point[0], aFixed, true) &&
859 !isPointFixed(theLine.point[1], aFixed, true))
860 fixPoint(aPoint[0], theCreated);
861 if (!isUsedInEqual(theLine.h, anEqual)) {
862 // Check the distance is not set yet
863 std::vector<Slvs_Constraint>::const_iterator aDistIt = myConstraints.begin();
864 for (; aDistIt != myConstraints.end(); ++aDistIt)
865 if ((aDistIt->type == SLVS_C_PT_PT_DISTANCE) &&
866 ((aDistIt->ptA == theLine.point[0] && aDistIt->ptB == theLine.point[1]) ||
867 (aDistIt->ptA == theLine.point[1] && aDistIt->ptB == theLine.point[0])))
869 // Calculate distance between points on the line
871 for (int i = 0; i < 2; i++)
872 for (int j = 0; j < 2; j++) {
873 Slvs_Param aParam = getParameter(aPoint[i].param[j]);
874 aCoords[2*i+j] = aParam.val;
877 double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) +
878 (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
880 Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group,
881 SLVS_C_PT_PT_DISTANCE, theLine.wrkpl, aLength,
882 theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
883 aDistance.h = addConstraint(aDistance);
884 theCreated.push_back(aDistance.h);
888 else if (isUsedInEqual(theLine.h, anEqual)) {
889 // Check another entity of Equal is already fixed
890 Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
891 if (isEntityFixed(anOtherEntID, true)) {
892 // Fix start point of the line (if end point is not fixed yet) ...
893 Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
894 bool isFixed = isPointFixed(theLine.point[1], anEndFixedID, true);
895 if (isFixed == SLVS_E_UNKNOWN)
896 fixPoint(aPoint[0], theCreated);
897 // ... and create fixed point lying on this line
898 Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
899 // Firstly, search already fixed point on line
900 bool isPonLineFixed = false;
901 Slvs_hEntity aFixedPoint;
902 std::vector<Slvs_Constraint>::const_iterator aPLIter = myConstraints.begin();
903 for (; aPLIter != myConstraints.end() && !isPonLineFixed; ++aPLIter)
904 if (aPLIter->type == SLVS_C_PT_ON_LINE && aPLIter->entityA == theLine.h) {
905 isPonLineFixed = isPointFixed(aPLIter->ptA, anEndFixedID);
906 aFixedPoint = aPLIter->ptA;
909 if (isPonLineFixed) { // update existent constraint
910 copyEntity(aPointToCopy, aFixedPoint);
911 } else { // create new constraint
912 Slvs_hEntity aCopied = copyEntity(aPointToCopy);
913 Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group, SLVS_C_PT_ON_LINE,
914 theLine.wrkpl, 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
915 aPonLine.h = addConstraint(aPonLine);
916 theCreated.push_back(aPonLine.h);
917 fixPoint(getEntity(aCopied), theCreated);
924 for (int i = 0; i < 2; i++)
925 fixPoint(aPoint[i], theCreated);
928 void SketchSolver_Storage::fixCircle(const Slvs_Entity& theCircle,
929 std::vector<Slvs_hConstraint>& theCreated)
931 bool isFixRadius = true;
932 // Verify the arc is under Equal constraint
933 Slvs_Constraint anEqual;
934 if (isUsedInEqual(theCircle.h, anEqual)) {
935 // Check another entity of Equal is already fixed
936 Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
937 if (isEntityFixed(anOtherEntID, true))
941 fixPoint(getEntity(theCircle.point[0]), theCreated);
944 // Search the radius is already fixed
945 std::vector<Slvs_Constraint>::const_iterator aDiamIter = myConstraints.begin();
946 for (; aDiamIter != myConstraints.end(); ++aDiamIter)
947 if (aDiamIter->type == SLVS_C_DIAMETER && aDiamIter->entityA == theCircle.h)
950 // Fix radius of a circle
951 const Slvs_Entity& aRadEnt = getEntity(theCircle.distance);
952 double aRadius = getParameter(aRadEnt.param[0]).val;
953 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theCircle.group, SLVS_C_DIAMETER,
954 theCircle.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theCircle.h, SLVS_E_UNKNOWN);
955 aFixedR.h = addConstraint(aFixedR);
956 theCreated.push_back(aFixedR.h);
960 void SketchSolver_Storage::fixArc(const Slvs_Entity& theArc,
961 std::vector<Slvs_hConstraint>& theCreated)
963 Slvs_Entity aPoint[3] = {
964 getEntity(theArc.point[0]),
965 getEntity(theArc.point[1]),
966 getEntity(theArc.point[2])
969 bool isFixRadius = true;
970 std::list<Slvs_Entity> aPointsToFix;
971 aPointsToFix.push_back(aPoint[1]);
972 aPointsToFix.push_back(aPoint[2]);
974 // Verify the arc is under Equal constraint
975 Slvs_Constraint anEqual;
976 if (isUsedInEqual(theArc.h, anEqual)) {
977 // Check another entity of Equal is already fixed
978 Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
979 if (isEntityFixed(anOtherEntID, true)) {
981 Slvs_Entity anOtherEntity = getEntity(anOtherEntID);
982 if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
983 aPointsToFix.pop_back();
984 aPointsToFix.push_back(aPoint[0]);
989 Slvs_hConstraint aConstrID;
990 int aNbPointsToFix = 2; // number of fixed points for the arc
991 if (isPointFixed(theArc.point[0], aConstrID, true))
994 double anArcPoints[3][2];
995 for (int i = 0; i < 3; i++) {
996 const Slvs_Entity& aPointOnArc = getEntity(theArc.point[i]);
997 for (int j = 0; j < 2; j++)
998 anArcPoints[i][j] = getParameter(aPointOnArc.param[j]).val;
1001 // Radius of the arc
1002 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(anArcPoints[0][0], anArcPoints[0][1]));
1003 std::shared_ptr<GeomAPI_Pnt2d> aStart(new GeomAPI_Pnt2d(anArcPoints[1][0], anArcPoints[1][1]));
1004 double aRadius = aCenter->distance(aStart);
1006 // Update end point of the arc to be on a curve
1007 std::shared_ptr<GeomAPI_Pnt2d> anEnd(new GeomAPI_Pnt2d(anArcPoints[2][0], anArcPoints[2][1]));
1008 double aDistance = anEnd->distance(aCenter);
1009 std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
1010 if (aDistance < tolerance)
1011 aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
1013 aDir = aDir->multiplied(aRadius / aDistance);
1014 double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
1015 const Slvs_Entity& aEndPoint = getEntity(theArc.point[2]);
1016 for (int i = 0; i < 2; i++) {
1017 Slvs_Param aParam = getParameter(aEndPoint.param[i]);
1019 updateParameter(aParam);
1022 std::list<Slvs_Entity>::iterator aPtIt = aPointsToFix.begin();
1023 for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
1024 fixPoint(*aPtIt, theCreated);
1027 // Fix radius of the arc
1028 bool isExists = false;
1029 std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
1030 for (; anIt != myConstraints.end() && !isExists; ++anIt)
1031 if (anIt->type == SLVS_C_DIAMETER && anIt->entityA == theArc.h)
1034 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theArc.group, SLVS_C_DIAMETER,
1035 theArc.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theArc.h, SLVS_E_UNKNOWN);
1036 aFixedR.h = addConstraint(aFixedR);
1037 theCreated.push_back(aFixedR.h);
1043 bool SketchSolver_Storage::isAxisParallel(const Slvs_hEntity& theEntity) const
1045 std::vector<Slvs_Constraint>::const_iterator anIter = myConstraints.begin();
1046 for (; anIter != myConstraints.end(); anIter++)
1047 if ((anIter->type == SLVS_C_HORIZONTAL || anIter->type == SLVS_C_VERTICAL) &&
1048 anIter->entityA == theEntity)
1053 bool SketchSolver_Storage::isUsedInEqual(
1054 const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const
1056 // Check the entity is used in Equal constraint
1057 std::vector<Slvs_Constraint>::const_iterator anEqIter = myConstraints.begin();
1058 for (; anEqIter != myConstraints.end(); anEqIter++)
1059 if ((anEqIter->type == SLVS_C_EQUAL_LENGTH_LINES ||
1060 anEqIter->type == SLVS_C_EQUAL_LINE_ARC_LEN ||
1061 anEqIter->type == SLVS_C_EQUAL_RADIUS) &&
1062 (anEqIter->entityA == theEntity || anEqIter->entityB == theEntity)) {
1063 theEqual = *anEqIter;
1073 // ========================================================
1074 // ========= Auxiliary functions ===============
1075 // ========================================================
1077 template<typename T>
1078 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1080 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1081 int aVecSize = theEntities.size();
1082 if (theEntities.empty())
1084 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1086 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1088 if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1089 aResIndex = aVecSize;
1093 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1095 return fabs(theParam1.val - theParam2.val) > tolerance;
1098 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1101 for (; theEntity1.param[i] != 0 && i < 4; i++)
1102 if (theEntity1.param[i] != theEntity2.param[i])
1105 for (; theEntity1.point[i] != 0 && i < 4; i++)
1106 if (theEntity1.point[i] != theEntity2.point[i])
1111 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1113 return theConstraint1.ptA != theConstraint2.ptA ||
1114 theConstraint1.ptB != theConstraint2.ptB ||
1115 theConstraint1.entityA != theConstraint2.entityA ||
1116 theConstraint1.entityB != theConstraint2.entityB ||
1117 theConstraint1.entityC != theConstraint2.entityC ||
1118 theConstraint1.entityD != theConstraint2.entityD ||
1119 fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;