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] != 0)
219 anUnusedEntities.erase(myEntities[aPos].point[j]);
225 std::set<Slvs_hEntity>::const_iterator anEntIt = anUnusedEntities.begin();
226 for (; anEntIt != anUnusedEntities.end(); ++anEntIt) {
227 int aPos = Search(*anEntIt, myEntities);
228 if (aPos >= 0 && aPos < (int)myEntities.size()) {
229 // Remove entity and its parameters
230 Slvs_Entity anEntity = myEntities[aPos];
231 myEntities.erase(myEntities.begin() + aPos);
232 myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
233 if (anEntity.distance != SLVS_E_UNKNOWN)
234 removeParameter(anEntity.distance);
235 for (int i = 0; i < 4; i++)
236 if (anEntity.param[i] != SLVS_E_UNKNOWN)
237 removeParameter(anEntity.param[i]);
238 for (int i = 0; i < 4; i++)
239 if (anEntity.point[i] != SLVS_E_UNKNOWN)
240 removeEntity(anEntity.point[i]);
241 myRemovedEntities.insert(*anEntIt);
242 if (anEntity.type == SLVS_E_POINT_IN_2D || anEntity.type == SLVS_E_POINT_IN_3D)
243 removeCoincidentPoint(*anEntIt);
247 if (!anUnusedEntities.empty())
248 myNeedToResolve = true;
251 const Slvs_Entity& SketchSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
253 int aPos = Search(theEntityID, myEntities);
254 if (aPos >= 0 && aPos < (int)myEntities.size())
255 return myEntities[aPos];
257 // Entity is not found, return empty object
258 static Slvs_Entity aDummy;
259 aDummy.h = SLVS_E_UNKNOWN;
263 Slvs_hEntity SketchSolver_Storage::copyEntity(const Slvs_hEntity& theCopied)
265 int aPos = Search(theCopied, myEntities);
266 if (aPos < 0 || aPos >= (int)myEntities.size())
267 return SLVS_E_UNKNOWN;
269 Slvs_Entity aCopy = myEntities[aPos];
270 aCopy.h = SLVS_E_UNKNOWN;
272 while (aCopy.point[i] != SLVS_E_UNKNOWN) {
273 aCopy.point[i] = copyEntity(aCopy.point[i]);
276 if (aCopy.param[0] != SLVS_E_UNKNOWN) {
277 aPos = Search(aCopy.param[0], myParameters);
279 while (aCopy.param[i] != SLVS_E_UNKNOWN) {
280 Slvs_Param aNewParam = myParameters[aPos];
281 aNewParam.h = SLVS_E_UNKNOWN;
282 aCopy.param[i] = addParameter(aNewParam);
287 return addEntity(aCopy);
290 void SketchSolver_Storage::copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo)
292 int aPosFrom = Search(theFrom, myEntities);
293 int aPosTo = Search(theTo, myEntities);
294 if (aPosFrom < 0 || aPosFrom >= (int)myEntities.size() ||
295 aPosTo < 0 || aPosTo >= (int)myEntities.size())
298 Slvs_Entity aEntFrom = myEntities[aPosFrom];
299 Slvs_Entity aEntTo = myEntities[aPosTo];
301 while (aEntFrom.point[i] != SLVS_E_UNKNOWN) {
302 copyEntity(aEntFrom.point[i], aEntTo.point[i]);
305 if (aEntFrom.param[0] != SLVS_E_UNKNOWN) {
306 aPosFrom = Search(aEntFrom.param[0], myParameters);
307 aPosTo = Search(aEntTo.param[0], myParameters);
309 while (aEntFrom.param[i] != SLVS_E_UNKNOWN) {
310 myParameters[aPosTo++].val = myParameters[aPosFrom++].val;
317 bool SketchSolver_Storage::isPointFixed(
318 const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate) const
320 // Search the set of coincident points
321 std::set<Slvs_hEntity> aCoincident;
322 aCoincident.insert(thePointID);
323 std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
324 for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
325 if (aCPIter->find(thePointID) != aCPIter->end()) {
326 aCoincident = *aCPIter;
330 // Search the Rigid constraint
331 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
332 for (; aConstrIter != myConstraints.end(); aConstrIter++)
333 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
334 aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
335 theFixed = aConstrIter->h;
340 // Try to find the fixed entity which uses such point or its coincidence
341 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
342 for (; anEntIter != myEntities.end(); anEntIter++) {
343 for (int i = 0; i < 4; i++) {
344 Slvs_hEntity aPt = anEntIter->point[i];
345 if (aPt != SLVS_E_UNKNOWN &&
346 (aPt == thePointID || aCoincident.find(aPt) != aCoincident.end())) {
347 if (isEntityFixed(anEntIter->h, true))
353 return SLVS_E_UNKNOWN;
356 bool SketchSolver_Storage::isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate) const
358 int aPos = Search(theEntityID, myEntities);
359 if (aPos < 0 || aPos >= (int)myEntities.size())
362 // Firstly, find how many points are under Rigid constraint
364 for (int i = 0; i < 4; i++) {
365 Slvs_hEntity aPoint = myEntities[aPos].point[i];
366 if (aPoint == SLVS_E_UNKNOWN)
369 std::set<Slvs_hEntity> aCoincident;
370 aCoincident.insert(aPoint);
371 std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
372 for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
373 if (aCPIter->find(aPoint) != aCPIter->end()) {
374 aCoincident = *aCPIter;
378 // Search the Rigid constraint
379 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
380 for (; aConstrIter != myConstraints.end(); aConstrIter++)
381 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
382 aCoincident.find(aConstrIter->ptA) != aCoincident.end())
386 std::list<Slvs_Constraint> aList;
387 std::list<Slvs_Constraint>::iterator anIt;
388 Slvs_hConstraint aTempID; // used in isPointFixed() method
390 if (myEntities[aPos].type == SLVS_E_LINE_SEGMENT) {
393 else if (aNbFixed == 0 || !theAccurate)
395 // Additional check (the line may be fixed if it is used by different constraints):
396 // 1. The line is used in Equal constraint, another entity is fixed and there is a fixed point on line
397 aList = getConstraintsByType(SLVS_C_PT_ON_LINE);
398 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
399 if (anIt->entityA == theEntityID && isPointFixed(anIt->ptA, aTempID))
401 if (anIt != aList.end()) {
402 aList = getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
403 aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
404 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
405 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
406 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
407 if (isEntityFixed(anOther, false))
411 // 2. The line is used in Parallel/Perpendicular/Vertical/Horizontal and Length constraints
412 aList = getConstraintsByType(SLVS_C_PARALLEL);
413 aList.splice(aList.end(), getConstraintsByType(SLVS_C_PERPENDICULAR));
414 aList.splice(aList.end(), getConstraintsByType(SLVS_C_VERTICAL));
415 aList.splice(aList.end(), getConstraintsByType(SLVS_C_HORIZONTAL));
416 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
417 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
418 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
419 if (isEntityFixed(anOther, false))
422 if (anIt != aList.end()) {
423 aList = getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
424 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
425 if ((anIt->ptA == myEntities[aPos].point[0] && anIt->ptB == myEntities[aPos].point[1]) ||
426 (anIt->ptA == myEntities[aPos].point[1] && anIt->ptB == myEntities[aPos].point[0]))
429 // 3. Another verifiers ...
430 } else if (myEntities[aPos].type == SLVS_E_CIRCLE) {
433 // Search for Diameter constraint
434 aList = getConstraintsByType(SLVS_C_DIAMETER);
435 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
436 if (anIt->entityA == theEntityID)
440 // Additional check (the circle may be fixed if it is used by different constraints):
441 // 1. The circle is used in Equal constraint and another entity is fixed
442 aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
443 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
444 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
445 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
446 if (isEntityFixed(anOther, false))
449 // 2. Another verifiers ...
450 } else if (myEntities[aPos].type == SLVS_E_ARC_OF_CIRCLE) {
453 else if (aNbFixed <= 1)
455 // Search for Diameter constraint
456 aList = getConstraintsByType(SLVS_C_DIAMETER);
457 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
458 if (anIt->entityA == theEntityID)
462 // Additional check (the arc may be fixed if it is used by different constraints):
463 // 1. The arc is used in Equal constraint and another entity is fixed
464 aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
465 aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
466 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
467 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
468 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
469 if (isEntityFixed(anOther, false))
472 // 2. Another verifiers ...
478 Slvs_hConstraint SketchSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
480 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
481 // Constraint is already used, rewrite it
482 return updateConstraint(theConstraint);
485 Slvs_Constraint aConstraint = theConstraint;
487 // Find a constraint with same type uses same arguments to show user overconstraint situation
488 std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
489 for (; aCIt != myConstraints.end(); aCIt++) {
490 if (aConstraint.type != aCIt->type)
492 if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
493 aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
494 aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD)
495 myDuplicatedConstraint = true;
498 if (aConstraint.h > myConstrMaxID)
499 myConstrMaxID = aConstraint.h;
501 aConstraint.h = ++myConstrMaxID;
502 myConstraints.push_back(aConstraint);
503 myNeedToResolve = true;
504 if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
505 addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
506 return aConstraint.h;
509 Slvs_hConstraint SketchSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
511 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
512 // Constraint already used, rewrite it
513 int aPos = Search(theConstraint.h, myConstraints);
514 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
515 myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
516 myConstraints[aPos] = theConstraint;
517 if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
518 addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
519 return theConstraint.h;
523 // Constraint is not found, add new one
524 Slvs_Constraint aConstraint = theConstraint;
526 return addConstraint(aConstraint);
529 bool SketchSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
532 int aPos = Search(theConstraintID, myConstraints);
533 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
534 Slvs_Constraint aConstraint = myConstraints[aPos];
535 myConstraints.erase(myConstraints.begin() + aPos);
536 myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
537 myNeedToResolve = true;
538 myRemovedConstraints.insert(theConstraintID);
539 // Remove all entities
540 Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
541 aConstraint.entityA, aConstraint.entityB,
542 aConstraint.entityC, aConstraint.entityD};
543 for (int i = 0; i < 6; i++)
544 if (anEntities[i] != SLVS_E_UNKNOWN)
545 aResult = removeEntity(anEntities[i]) && aResult;
546 // remove temporary fixed point, if available
547 if (myFixed == theConstraintID)
548 myFixed = SLVS_E_UNKNOWN;
549 if (myDuplicatedConstraint) {
550 // Check the duplicated constraints are still available
551 myDuplicatedConstraint = false;
552 std::vector<Slvs_Constraint>::const_iterator anIt1 = myConstraints.begin();
553 std::vector<Slvs_Constraint>::const_iterator anIt2 = myConstraints.begin();
554 for (; anIt1 != myConstraints.end() && !myDuplicatedConstraint; anIt1++)
555 for (anIt2 = anIt1+1; anIt2 != myConstraints.end() && !myDuplicatedConstraint; anIt2++) {
556 if (anIt1->type != anIt2->type)
558 if (anIt1->ptA == anIt2->ptA && anIt1->ptB == anIt2->ptB &&
559 anIt1->entityA == anIt2->entityA && anIt1->entityB == anIt2->entityB &&
560 anIt1->entityC == anIt2->entityC && anIt1->entityD == anIt2->entityD)
561 myDuplicatedConstraint = true;
568 const Slvs_Constraint& SketchSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
570 int aPos = Search(theConstraintID, myConstraints);
571 if (aPos >= 0 && aPos < (int)myConstraints.size())
572 return myConstraints[aPos];
574 // Constraint is not found, return empty object
575 static Slvs_Constraint aDummy;
580 std::list<Slvs_Constraint> SketchSolver_Storage::getConstraintsByType(int theConstraintType) const
582 std::list<Slvs_Constraint> aResult;
583 std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
584 for (; aCIter != myConstraints.end(); aCIter++)
585 if (aCIter->type == theConstraintType)
586 aResult.push_back(*aCIter);
591 void SketchSolver_Storage::addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID)
593 if (myFixed != SLVS_E_UNKNOWN)
594 return; // the point is already fixed
595 int aPos = Search(theConstraintID, myConstraints);
596 if (aPos >= 0 && aPos < (int)myConstraints.size())
597 myFixed = theConstraintID;
600 void SketchSolver_Storage::addTemporaryConstraint(const Slvs_hConstraint& theConstraintID)
602 myTemporaryConstraints.insert(theConstraintID);
605 void SketchSolver_Storage::removeTemporaryConstraints()
607 myTemporaryConstraints.clear();
610 int SketchSolver_Storage::deleteTemporaryConstraint()
612 if (myTemporaryConstraints.empty())
614 // Search the point-on-line or a non-rigid constraint
615 std::set<Slvs_hConstraint>::iterator aCIt = myTemporaryConstraints.begin();
616 for (; aCIt != myTemporaryConstraints.end(); aCIt++) {
617 int aPos = Search(*aCIt, myConstraints);
618 if (aPos >= (int)myConstraints.size() || myConstraints[aPos].type != SLVS_C_WHERE_DRAGGED)
620 std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
621 for (; anIt != myConstraints.end(); anIt++)
622 if (anIt->type == SLVS_C_PT_ON_LINE && anIt->ptA == myConstraints[aPos].ptA)
624 if (anIt != myConstraints.end())
627 if (aCIt == myTemporaryConstraints.end())
628 aCIt = myTemporaryConstraints.begin();
629 bool aNewFixed = (*aCIt == myFixed);
630 removeConstraint(*aCIt);
631 myTemporaryConstraints.erase(aCIt);
633 for (aCIt = myTemporaryConstraints.begin(); aCIt != myTemporaryConstraints.end(); aCIt++) {
634 int aPos = Search(*aCIt, myConstraints);
635 if (myConstraints[aPos].type == SLVS_C_WHERE_DRAGGED) {
641 return (int)myTemporaryConstraints.size();
644 bool SketchSolver_Storage::isTemporary(const Slvs_hConstraint& theConstraintID) const
646 return myTemporaryConstraints.find(theConstraintID) != myTemporaryConstraints.end();
650 void SketchSolver_Storage::getRemoved(
651 std::set<Slvs_hParam>& theParameters,
652 std::set<Slvs_hEntity>& theEntities,
653 std::set<Slvs_hConstraint>& theConstraints)
655 theParameters = myRemovedParameters;
656 theEntities = myRemovedEntities;
657 theConstraints = myRemovedConstraints;
659 myRemovedParameters.clear();
660 myRemovedEntities.clear();
661 myRemovedConstraints.clear();
664 void SketchSolver_Storage::initializeSolver(SketchSolver_Solver& theSolver)
666 theSolver.setParameters(myParameters.data(), (int)myParameters.size());
667 theSolver.setEntities(myEntities.data(), (int)myEntities.size());
669 // Copy constraints excluding the fixed one
670 std::vector<Slvs_Constraint> aConstraints = myConstraints;
671 if (myFixed != SLVS_E_UNKNOWN) {
672 Slvs_hEntity aFixedPoint = SLVS_E_UNKNOWN;
673 std::vector<Slvs_Constraint>::iterator anIt = aConstraints.begin();
674 for (; anIt != aConstraints.end(); anIt++)
675 if (anIt->h == myFixed) {
676 aFixedPoint = anIt->ptA;
677 aConstraints.erase(anIt);
680 // set dragged parameters
681 int aPos = Search(aFixedPoint, myEntities);
682 theSolver.setDraggedParameters(myEntities[aPos].param);
684 theSolver.setConstraints(aConstraints.data(), (int)aConstraints.size());
687 void SketchSolver_Storage::addCoincidentPoints(
688 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
690 std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
691 std::vector< std::set<Slvs_hEntity> >::iterator aFoundIter = myCoincidentPoints.end(); // already found coincidence
692 bool isFound = false;
693 for (; aCIter != myCoincidentPoints.end(); aCIter++) {
694 bool isFirstFound = aCIter->find(thePoint1) != aCIter->end();
695 bool isSecondFound = aCIter->find(thePoint2) != aCIter->end();
696 isFound = isFound || isFirstFound || isSecondFound;
697 if (isFirstFound && isSecondFound)
698 break; // already coincident
699 else if (isFirstFound || isSecondFound) {
700 if (aFoundIter != myCoincidentPoints.end()) {
702 aFoundIter->insert(aCIter->begin(), aCIter->end());
703 myCoincidentPoints.erase(aCIter);
706 aCIter->insert(thePoint1);
707 aCIter->insert(thePoint2);
710 // coincident points not found
712 std::set<Slvs_hEntity> aNewSet;
713 aNewSet.insert(thePoint1);
714 aNewSet.insert(thePoint2);
715 myCoincidentPoints.push_back(aNewSet);
719 void SketchSolver_Storage::removeCoincidentPoint(const Slvs_hEntity& thePoint)
721 std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
722 for (; aCIter != myCoincidentPoints.end(); aCIter++)
723 if (aCIter->find(thePoint) != aCIter->end()) {
724 aCIter->erase(thePoint);
725 if (aCIter->size() <= 1)
726 myCoincidentPoints.erase(aCIter);
731 bool SketchSolver_Storage::isCoincident(
732 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
734 std::vector< std::set<Slvs_hEntity> >::const_iterator aCIter = myCoincidentPoints.begin();
735 for (; aCIter != myCoincidentPoints.end(); aCIter++)
736 if (aCIter->find(thePoint1) != aCIter->end() && aCIter->find(thePoint2) != aCIter->end())
738 // precise checking of coincidence
739 int aEnt1Pos = Search(thePoint1, myEntities);
740 int aEnt2Pos = Search(thePoint2, myEntities);
741 if (aEnt1Pos >= 0 && aEnt1Pos < (int)myEntities.size() &&
742 aEnt2Pos >= 0 && aEnt2Pos < (int)myEntities.size()) {
745 for (int i = 0; i < 2; i++) {
746 aParamPos = Search(myEntities[aEnt1Pos].param[i], myParameters);
747 aDist[i] = myParameters[aParamPos].val;
748 aParamPos = Search(myEntities[aEnt2Pos].param[i], myParameters);
749 aDist[i] -= myParameters[aParamPos].val;
751 if (aDist[0] * aDist[0] + aDist[1] * aDist[1] < tolerance * tolerance)
758 std::vector<Slvs_hConstraint> SketchSolver_Storage::fixEntity(const Slvs_hEntity& theEntity)
760 std::vector<Slvs_hConstraint> aNewConstraints;
762 int aPos = Search(theEntity, myEntities);
763 if (aPos >= 0 && aPos < (int)myEntities.size()) {
764 switch (myEntities[aPos].type) {
765 case SLVS_E_POINT_IN_2D:
766 case SLVS_E_POINT_IN_3D:
767 fixPoint(myEntities[aPos], aNewConstraints);
769 case SLVS_E_LINE_SEGMENT:
770 fixLine(myEntities[aPos], aNewConstraints);
773 fixCircle(myEntities[aPos], aNewConstraints);
775 case SLVS_E_ARC_OF_CIRCLE:
776 fixArc(myEntities[aPos], aNewConstraints);
783 return aNewConstraints;
786 void SketchSolver_Storage::fixPoint(const Slvs_Entity& thePoint,
787 std::vector<Slvs_hConstraint>& theCreated)
789 Slvs_Constraint aConstraint;
790 Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
791 bool isFixed = isPointFixed(thePoint.h, aConstrID, true);
792 bool isForceUpdate = (isFixed && isTemporary(aConstrID));
793 if (!isForceUpdate) { // create new constraint
795 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, thePoint.group, SLVS_C_WHERE_DRAGGED, thePoint.wrkpl,
796 0.0, thePoint.h, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
797 aConstraint.h = addConstraint(aConstraint);
798 theCreated.push_back(aConstraint.h);
799 } else { // update already existent constraint
800 if (!isFixed || aConstrID == SLVS_E_UNKNOWN)
802 int aPos = Search(aConstrID, myConstraints);
803 if (aPos >= 0 && aPos < (int)myConstraints.size())
804 myConstraints[aPos].ptA = thePoint.h;
808 void SketchSolver_Storage::fixLine(const Slvs_Entity& theLine,
809 std::vector<Slvs_hConstraint>& theCreated)
811 Slvs_Entity aPoint[2] = {
812 getEntity(theLine.point[0]),
813 getEntity(theLine.point[1])
816 Slvs_Constraint anEqual;
817 if (isAxisParallel(theLine.h)) {
818 // Fix one point and a line length
819 Slvs_hConstraint aFixed;
820 if (!isPointFixed(theLine.point[0], aFixed, true) &&
821 !isPointFixed(theLine.point[1], aFixed, true))
822 fixPoint(aPoint[0], theCreated);
823 if (!isUsedInEqual(theLine.h, anEqual)) {
824 // Check the distance is not set yet
825 std::vector<Slvs_Constraint>::const_iterator aDistIt = myConstraints.begin();
826 for (; aDistIt != myConstraints.end(); ++aDistIt)
827 if ((aDistIt->type == SLVS_C_PT_PT_DISTANCE) &&
828 ((aDistIt->ptA == theLine.point[0] && aDistIt->ptB == theLine.point[1]) ||
829 (aDistIt->ptA == theLine.point[1] && aDistIt->ptB == theLine.point[0])))
831 // Calculate distance between points on the line
833 for (int i = 0; i < 2; i++)
834 for (int j = 0; j < 2; j++) {
835 Slvs_Param aParam = getParameter(aPoint[i].param[j]);
836 aCoords[2*i+j] = aParam.val;
839 double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) +
840 (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
842 Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group,
843 SLVS_C_PT_PT_DISTANCE, theLine.wrkpl, aLength,
844 theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
845 aDistance.h = addConstraint(aDistance);
846 theCreated.push_back(aDistance.h);
850 else if (isUsedInEqual(theLine.h, anEqual)) {
851 // Check another entity of Equal is already fixed
852 Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
853 if (isEntityFixed(anOtherEntID, true)) {
854 // Fix start point of the line (if end point is not fixed yet) ...
855 Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
856 bool isFixed = isPointFixed(theLine.point[1], anEndFixedID, true);
857 if (isFixed == SLVS_E_UNKNOWN)
858 fixPoint(aPoint[0], theCreated);
859 // ... and create fixed point lying on this line
860 Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
861 // Firstly, search already fixed point on line
862 bool isPonLineFixed = false;
863 Slvs_hEntity aFixedPoint;
864 std::vector<Slvs_Constraint>::const_iterator aPLIter = myConstraints.begin();
865 for (; aPLIter != myConstraints.end() && !isPonLineFixed; ++aPLIter)
866 if (aPLIter->type == SLVS_C_PT_ON_LINE && aPLIter->entityA == theLine.h) {
867 isPonLineFixed = isPointFixed(aPLIter->ptA, anEndFixedID);
868 aFixedPoint = aPLIter->ptA;
871 if (isPonLineFixed) { // update existent constraint
872 copyEntity(aPointToCopy, aFixedPoint);
873 } else { // create new constraint
874 Slvs_hEntity aCopied = copyEntity(aPointToCopy);
875 Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group, SLVS_C_PT_ON_LINE,
876 theLine.wrkpl, 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
877 aPonLine.h = addConstraint(aPonLine);
878 theCreated.push_back(aPonLine.h);
879 fixPoint(getEntity(aCopied), theCreated);
886 for (int i = 0; i < 2; i++)
887 fixPoint(aPoint[i], theCreated);
890 void SketchSolver_Storage::fixCircle(const Slvs_Entity& theCircle,
891 std::vector<Slvs_hConstraint>& theCreated)
893 bool isFixRadius = true;
894 // Verify the arc is under Equal constraint
895 Slvs_Constraint anEqual;
896 if (isUsedInEqual(theCircle.h, anEqual)) {
897 // Check another entity of Equal is already fixed
898 Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
899 if (isEntityFixed(anOtherEntID, true))
903 fixPoint(getEntity(theCircle.point[0]), theCreated);
906 // Search the radius is already fixed
907 std::vector<Slvs_Constraint>::const_iterator aDiamIter = myConstraints.begin();
908 for (; aDiamIter != myConstraints.end(); ++aDiamIter)
909 if (aDiamIter->type == SLVS_C_DIAMETER && aDiamIter->entityA == theCircle.h)
912 // Fix radius of a circle
913 const Slvs_Entity& aRadEnt = getEntity(theCircle.distance);
914 double aRadius = getParameter(aRadEnt.param[0]).val;
915 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theCircle.group, SLVS_C_DIAMETER,
916 theCircle.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theCircle.h, SLVS_E_UNKNOWN);
917 aFixedR.h = addConstraint(aFixedR);
918 theCreated.push_back(aFixedR.h);
922 void SketchSolver_Storage::fixArc(const Slvs_Entity& theArc,
923 std::vector<Slvs_hConstraint>& theCreated)
925 Slvs_Entity aPoint[3] = {
926 getEntity(theArc.point[0]),
927 getEntity(theArc.point[1]),
928 getEntity(theArc.point[2])
931 bool isFixRadius = true;
932 std::list<Slvs_Entity> aPointsToFix;
933 aPointsToFix.push_back(aPoint[1]);
934 aPointsToFix.push_back(aPoint[2]);
936 // Verify the arc is under Equal constraint
937 Slvs_Constraint anEqual;
938 if (isUsedInEqual(theArc.h, anEqual)) {
939 // Check another entity of Equal is already fixed
940 Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
941 if (isEntityFixed(anOtherEntID, true)) {
943 Slvs_Entity anOtherEntity = getEntity(anOtherEntID);
944 if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
945 aPointsToFix.pop_back();
946 aPointsToFix.push_back(aPoint[0]);
951 Slvs_hConstraint aConstrID;
952 int aNbPointsToFix = 2; // number of fixed points for the arc
953 if (isPointFixed(theArc.point[0], aConstrID, true))
956 double anArcPoints[3][2];
957 for (int i = 0; i < 3; i++) {
958 const Slvs_Entity& aPointOnArc = getEntity(theArc.point[i]);
959 for (int j = 0; j < 2; j++)
960 anArcPoints[i][j] = getParameter(aPointOnArc.param[j]).val;
964 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(anArcPoints[0][0], anArcPoints[0][1]));
965 std::shared_ptr<GeomAPI_Pnt2d> aStart(new GeomAPI_Pnt2d(anArcPoints[1][0], anArcPoints[1][1]));
966 double aRadius = aCenter->distance(aStart);
968 // Update end point of the arc to be on a curve
969 std::shared_ptr<GeomAPI_Pnt2d> anEnd(new GeomAPI_Pnt2d(anArcPoints[2][0], anArcPoints[2][1]));
970 double aDistance = anEnd->distance(aCenter);
971 std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
972 if (aDistance < tolerance)
973 aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
975 aDir = aDir->multiplied(aRadius / aDistance);
976 double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
977 const Slvs_Entity& aEndPoint = getEntity(theArc.point[2]);
978 for (int i = 0; i < 2; i++) {
979 Slvs_Param aParam = getParameter(aEndPoint.param[i]);
981 updateParameter(aParam);
984 std::list<Slvs_Entity>::iterator aPtIt = aPointsToFix.begin();
985 for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
986 fixPoint(*aPtIt, theCreated);
989 // Fix radius of the arc
990 bool isExists = false;
991 std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
992 for (; anIt != myConstraints.end() && !isExists; ++anIt)
993 if (anIt->type == SLVS_C_DIAMETER && anIt->entityA == theArc.h)
996 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theArc.group, SLVS_C_DIAMETER,
997 theArc.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theArc.h, SLVS_E_UNKNOWN);
998 aFixedR.h = addConstraint(aFixedR);
999 theCreated.push_back(aFixedR.h);
1005 bool SketchSolver_Storage::isAxisParallel(const Slvs_hEntity& theEntity) const
1007 std::vector<Slvs_Constraint>::const_iterator anIter = myConstraints.begin();
1008 for (; anIter != myConstraints.end(); anIter++)
1009 if ((anIter->type == SLVS_C_HORIZONTAL || anIter->type == SLVS_C_VERTICAL) &&
1010 anIter->entityA == theEntity)
1015 bool SketchSolver_Storage::isUsedInEqual(
1016 const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const
1018 // Check the entity is used in Equal constraint
1019 std::vector<Slvs_Constraint>::const_iterator anEqIter = myConstraints.begin();
1020 for (; anEqIter != myConstraints.end(); anEqIter++)
1021 if ((anEqIter->type == SLVS_C_EQUAL_LENGTH_LINES ||
1022 anEqIter->type == SLVS_C_EQUAL_LINE_ARC_LEN ||
1023 anEqIter->type == SLVS_C_EQUAL_RADIUS) &&
1024 (anEqIter->entityA == theEntity || anEqIter->entityB == theEntity)) {
1025 theEqual = *anEqIter;
1035 // ========================================================
1036 // ========= Auxiliary functions ===============
1037 // ========================================================
1039 template<typename T>
1040 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1042 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1043 int aVecSize = theEntities.size();
1044 if (theEntities.empty())
1046 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1048 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1050 if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1051 aResIndex = aVecSize;
1055 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1057 return fabs(theParam1.val - theParam2.val) > tolerance;
1060 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1063 for (; theEntity1.param[i] != 0 && i < 4; i++)
1064 if (theEntity1.param[i] != theEntity2.param[i])
1067 for (; theEntity1.point[i] != 0 && i < 4; i++)
1068 if (theEntity1.point[i] != theEntity2.point[i])
1073 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1075 return theConstraint1.ptA != theConstraint2.ptA ||
1076 theConstraint1.ptB != theConstraint2.ptB ||
1077 theConstraint1.entityA != theConstraint2.entityA ||
1078 theConstraint1.entityB != theConstraint2.entityB ||
1079 theConstraint1.entityC != theConstraint2.entityC ||
1080 theConstraint1.entityD != theConstraint2.entityD ||
1081 fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;