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 for (; anEntIt != anUnusedEntities.end(); ++anEntIt) {
229 int aPos = Search(*anEntIt, myEntities);
230 if (aPos >= 0 && aPos < (int)myEntities.size()) {
231 // Remove entity and its parameters
232 Slvs_Entity anEntity = myEntities[aPos];
233 myEntities.erase(myEntities.begin() + aPos);
234 myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
235 if (anEntity.distance != SLVS_E_UNKNOWN)
236 removeParameter(anEntity.distance);
237 for (int i = 0; i < 4; i++)
238 if (anEntity.param[i] != SLVS_E_UNKNOWN)
239 removeParameter(anEntity.param[i]);
240 for (int i = 0; i < 4; i++)
241 if (anEntity.point[i] != SLVS_E_UNKNOWN)
242 removeEntity(anEntity.point[i]);
243 myRemovedEntities.insert(*anEntIt);
244 if (anEntity.type == SLVS_E_POINT_IN_2D || anEntity.type == SLVS_E_POINT_IN_3D)
245 removeCoincidentPoint(*anEntIt);
249 if (!anUnusedEntities.empty())
250 myNeedToResolve = true;
253 const Slvs_Entity& SketchSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
255 int aPos = Search(theEntityID, myEntities);
256 if (aPos >= 0 && aPos < (int)myEntities.size())
257 return myEntities[aPos];
259 // Entity is not found, return empty object
260 static Slvs_Entity aDummy;
261 aDummy.h = SLVS_E_UNKNOWN;
265 Slvs_hEntity SketchSolver_Storage::copyEntity(const Slvs_hEntity& theCopied)
267 int aPos = Search(theCopied, myEntities);
268 if (aPos < 0 || aPos >= (int)myEntities.size())
269 return SLVS_E_UNKNOWN;
271 Slvs_Entity aCopy = myEntities[aPos];
272 aCopy.h = SLVS_E_UNKNOWN;
274 while (aCopy.point[i] != SLVS_E_UNKNOWN) {
275 aCopy.point[i] = copyEntity(aCopy.point[i]);
278 if (aCopy.param[0] != SLVS_E_UNKNOWN) {
279 aPos = Search(aCopy.param[0], myParameters);
281 while (aCopy.param[i] != SLVS_E_UNKNOWN) {
282 Slvs_Param aNewParam = myParameters[aPos];
283 aNewParam.h = SLVS_E_UNKNOWN;
284 aCopy.param[i] = addParameter(aNewParam);
289 return addEntity(aCopy);
292 void SketchSolver_Storage::copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo)
294 int aPosFrom = Search(theFrom, myEntities);
295 int aPosTo = Search(theTo, myEntities);
296 if (aPosFrom < 0 || aPosFrom >= (int)myEntities.size() ||
297 aPosTo < 0 || aPosTo >= (int)myEntities.size())
300 Slvs_Entity aEntFrom = myEntities[aPosFrom];
301 Slvs_Entity aEntTo = myEntities[aPosTo];
303 while (aEntFrom.point[i] != SLVS_E_UNKNOWN) {
304 copyEntity(aEntFrom.point[i], aEntTo.point[i]);
307 if (aEntFrom.param[0] != SLVS_E_UNKNOWN) {
308 aPosFrom = Search(aEntFrom.param[0], myParameters);
309 aPosTo = Search(aEntTo.param[0], myParameters);
311 while (aEntFrom.param[i] != SLVS_E_UNKNOWN) {
312 myParameters[aPosTo++].val = myParameters[aPosFrom++].val;
319 bool SketchSolver_Storage::isPointFixed(
320 const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate) const
322 // Search the set of coincident points
323 std::set<Slvs_hEntity> aCoincident;
324 aCoincident.insert(thePointID);
325 std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
326 for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
327 if (aCPIter->find(thePointID) != aCPIter->end()) {
328 aCoincident = *aCPIter;
332 // Search the Rigid constraint
333 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
334 for (; aConstrIter != myConstraints.end(); aConstrIter++)
335 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
336 aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
337 theFixed = aConstrIter->h;
342 // Try to find the fixed entity which uses such point or its coincidence
343 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
344 for (; anEntIter != myEntities.end(); anEntIter++) {
345 for (int i = 0; i < 4; i++) {
346 Slvs_hEntity aPt = anEntIter->point[i];
347 if (aPt != SLVS_E_UNKNOWN &&
348 (aPt == thePointID || aCoincident.find(aPt) != aCoincident.end())) {
349 if (isEntityFixed(anEntIter->h, true))
355 return SLVS_E_UNKNOWN;
358 bool SketchSolver_Storage::isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate) const
360 int aPos = Search(theEntityID, myEntities);
361 if (aPos < 0 || aPos >= (int)myEntities.size())
364 // Firstly, find how many points are under Rigid constraint
366 for (int i = 0; i < 4; i++) {
367 Slvs_hEntity aPoint = myEntities[aPos].point[i];
368 if (aPoint == SLVS_E_UNKNOWN)
371 std::set<Slvs_hEntity> aCoincident;
372 aCoincident.insert(aPoint);
373 std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
374 for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
375 if (aCPIter->find(aPoint) != aCPIter->end()) {
376 aCoincident = *aCPIter;
380 // Search the Rigid constraint
381 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
382 for (; aConstrIter != myConstraints.end(); aConstrIter++)
383 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
384 aCoincident.find(aConstrIter->ptA) != aCoincident.end())
388 std::list<Slvs_Constraint> aList;
389 std::list<Slvs_Constraint>::iterator anIt;
390 Slvs_hConstraint aTempID; // used in isPointFixed() method
392 if (myEntities[aPos].type == SLVS_E_LINE_SEGMENT) {
395 else if (aNbFixed == 0 || !theAccurate)
397 // Additional check (the line may be fixed if it is used by different constraints):
398 // 1. The line is used in Equal constraint, another entity is fixed and there is a fixed point on line
399 aList = getConstraintsByType(SLVS_C_PT_ON_LINE);
400 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
401 if (anIt->entityA == theEntityID && isPointFixed(anIt->ptA, aTempID))
403 if (anIt != aList.end()) {
404 aList = getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
405 aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
406 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
407 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
408 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
409 if (isEntityFixed(anOther, false))
413 // 2. The line is used in Parallel/Perpendicular/Vertical/Horizontal and Length constraints
414 aList = getConstraintsByType(SLVS_C_PARALLEL);
415 aList.splice(aList.end(), getConstraintsByType(SLVS_C_PERPENDICULAR));
416 aList.splice(aList.end(), getConstraintsByType(SLVS_C_VERTICAL));
417 aList.splice(aList.end(), getConstraintsByType(SLVS_C_HORIZONTAL));
418 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
419 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
420 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
421 if (isEntityFixed(anOther, false))
424 if (anIt != aList.end()) {
425 aList = getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
426 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
427 if ((anIt->ptA == myEntities[aPos].point[0] && anIt->ptB == myEntities[aPos].point[1]) ||
428 (anIt->ptA == myEntities[aPos].point[1] && anIt->ptB == myEntities[aPos].point[0]))
431 // 3. Another verifiers ...
432 } else if (myEntities[aPos].type == SLVS_E_CIRCLE) {
435 // Search for Diameter constraint
436 aList = getConstraintsByType(SLVS_C_DIAMETER);
437 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
438 if (anIt->entityA == theEntityID)
442 // Additional check (the circle may be fixed if it is used by different constraints):
443 // 1. The circle is used in Equal constraint and another entity is fixed
444 aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
445 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
446 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
447 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
448 if (isEntityFixed(anOther, false))
451 // 2. Another verifiers ...
452 } else if (myEntities[aPos].type == SLVS_E_ARC_OF_CIRCLE) {
455 else if (aNbFixed <= 1)
457 // Search for Diameter constraint
458 aList = getConstraintsByType(SLVS_C_DIAMETER);
459 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
460 if (anIt->entityA == theEntityID)
464 // Additional check (the arc may be fixed if it is used by different constraints):
465 // 1. The arc is used in Equal constraint and another entity is fixed
466 aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
467 aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
468 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
469 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
470 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
471 if (isEntityFixed(anOther, false))
474 // 2. Another verifiers ...
480 Slvs_hConstraint SketchSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
482 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
483 // Constraint is already used, rewrite it
484 return updateConstraint(theConstraint);
487 Slvs_Constraint aConstraint = theConstraint;
489 // Find a constraint with same type uses same arguments to show user overconstraint situation
490 std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
491 for (; aCIt != myConstraints.end(); aCIt++) {
492 if (aConstraint.type != aCIt->type)
494 if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
495 aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
496 aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD)
497 myDuplicatedConstraint = true;
500 if (aConstraint.h > myConstrMaxID)
501 myConstrMaxID = aConstraint.h;
503 aConstraint.h = ++myConstrMaxID;
504 myConstraints.push_back(aConstraint);
505 myNeedToResolve = true;
506 if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
507 addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
508 return aConstraint.h;
511 Slvs_hConstraint SketchSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
513 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
514 // Constraint already used, rewrite it
515 int aPos = Search(theConstraint.h, myConstraints);
516 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
517 myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
518 myConstraints[aPos] = theConstraint;
519 if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
520 addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
521 return theConstraint.h;
525 // Constraint is not found, add new one
526 Slvs_Constraint aConstraint = theConstraint;
528 return addConstraint(aConstraint);
531 bool SketchSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
534 int aPos = Search(theConstraintID, myConstraints);
535 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
536 Slvs_Constraint aConstraint = myConstraints[aPos];
537 myConstraints.erase(myConstraints.begin() + aPos);
538 myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
539 myNeedToResolve = true;
540 myRemovedConstraints.insert(theConstraintID);
541 // Remove all entities
542 Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
543 aConstraint.entityA, aConstraint.entityB,
544 aConstraint.entityC, aConstraint.entityD};
545 for (int i = 0; i < 6; i++)
546 if (anEntities[i] != SLVS_E_UNKNOWN)
547 aResult = removeEntity(anEntities[i]) && aResult;
548 // remove temporary fixed point, if available
549 if (myFixed == theConstraintID)
550 myFixed = SLVS_E_UNKNOWN;
551 if (myDuplicatedConstraint) {
552 // Check the duplicated constraints are still available
553 myDuplicatedConstraint = false;
554 std::vector<Slvs_Constraint>::const_iterator anIt1 = myConstraints.begin();
555 std::vector<Slvs_Constraint>::const_iterator anIt2 = myConstraints.begin();
556 for (; anIt1 != myConstraints.end() && !myDuplicatedConstraint; anIt1++)
557 for (anIt2 = anIt1+1; anIt2 != myConstraints.end() && !myDuplicatedConstraint; anIt2++) {
558 if (anIt1->type != anIt2->type)
560 if (anIt1->ptA == anIt2->ptA && anIt1->ptB == anIt2->ptB &&
561 anIt1->entityA == anIt2->entityA && anIt1->entityB == anIt2->entityB &&
562 anIt1->entityC == anIt2->entityC && anIt1->entityD == anIt2->entityD)
563 myDuplicatedConstraint = true;
570 const Slvs_Constraint& SketchSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
572 int aPos = Search(theConstraintID, myConstraints);
573 if (aPos >= 0 && aPos < (int)myConstraints.size())
574 return myConstraints[aPos];
576 // Constraint is not found, return empty object
577 static Slvs_Constraint aDummy;
582 std::list<Slvs_Constraint> SketchSolver_Storage::getConstraintsByType(int theConstraintType) const
584 std::list<Slvs_Constraint> aResult;
585 std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
586 for (; aCIter != myConstraints.end(); aCIter++)
587 if (aCIter->type == theConstraintType)
588 aResult.push_back(*aCIter);
593 void SketchSolver_Storage::addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID)
595 if (myFixed != SLVS_E_UNKNOWN)
596 return; // the point is already fixed
597 int aPos = Search(theConstraintID, myConstraints);
598 if (aPos >= 0 && aPos < (int)myConstraints.size())
599 myFixed = theConstraintID;
602 void SketchSolver_Storage::addTemporaryConstraint(const Slvs_hConstraint& theConstraintID)
604 myTemporaryConstraints.insert(theConstraintID);
607 void SketchSolver_Storage::removeTemporaryConstraints()
609 myTemporaryConstraints.clear();
612 int SketchSolver_Storage::deleteTemporaryConstraint()
614 if (myTemporaryConstraints.empty())
616 // Search the point-on-line or a non-rigid constraint
617 std::set<Slvs_hConstraint>::iterator aCIt = myTemporaryConstraints.begin();
618 for (; aCIt != myTemporaryConstraints.end(); aCIt++) {
619 int aPos = Search(*aCIt, myConstraints);
620 if (aPos >= (int)myConstraints.size() || myConstraints[aPos].type != SLVS_C_WHERE_DRAGGED)
622 std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
623 for (; anIt != myConstraints.end(); anIt++)
624 if (anIt->type == SLVS_C_PT_ON_LINE && anIt->ptA == myConstraints[aPos].ptA)
626 if (anIt != myConstraints.end())
629 if (aCIt == myTemporaryConstraints.end())
630 aCIt = myTemporaryConstraints.begin();
631 bool aNewFixed = (*aCIt == myFixed);
632 removeConstraint(*aCIt);
633 myTemporaryConstraints.erase(aCIt);
635 for (aCIt = myTemporaryConstraints.begin(); aCIt != myTemporaryConstraints.end(); aCIt++) {
636 int aPos = Search(*aCIt, myConstraints);
637 if (myConstraints[aPos].type == SLVS_C_WHERE_DRAGGED) {
643 return (int)myTemporaryConstraints.size();
646 bool SketchSolver_Storage::isTemporary(const Slvs_hConstraint& theConstraintID) const
648 return myTemporaryConstraints.find(theConstraintID) != myTemporaryConstraints.end();
652 void SketchSolver_Storage::getRemoved(
653 std::set<Slvs_hParam>& theParameters,
654 std::set<Slvs_hEntity>& theEntities,
655 std::set<Slvs_hConstraint>& theConstraints)
657 theParameters = myRemovedParameters;
658 theEntities = myRemovedEntities;
659 theConstraints = myRemovedConstraints;
661 myRemovedParameters.clear();
662 myRemovedEntities.clear();
663 myRemovedConstraints.clear();
666 void SketchSolver_Storage::initializeSolver(SketchSolver_Solver& theSolver)
668 theSolver.setParameters(myParameters.data(), (int)myParameters.size());
669 theSolver.setEntities(myEntities.data(), (int)myEntities.size());
671 // Copy constraints excluding the fixed one
672 std::vector<Slvs_Constraint> aConstraints = myConstraints;
673 if (myFixed != SLVS_E_UNKNOWN) {
674 Slvs_hEntity aFixedPoint = SLVS_E_UNKNOWN;
675 std::vector<Slvs_Constraint>::iterator anIt = aConstraints.begin();
676 for (; anIt != aConstraints.end(); anIt++)
677 if (anIt->h == myFixed) {
678 aFixedPoint = anIt->ptA;
679 aConstraints.erase(anIt);
682 // set dragged parameters
683 int aPos = Search(aFixedPoint, myEntities);
684 theSolver.setDraggedParameters(myEntities[aPos].param);
686 theSolver.setConstraints(aConstraints.data(), (int)aConstraints.size());
689 void SketchSolver_Storage::addCoincidentPoints(
690 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
692 std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
693 std::vector< std::set<Slvs_hEntity> >::iterator aFoundIter = myCoincidentPoints.end(); // already found coincidence
694 bool isFound = false;
695 for (; aCIter != myCoincidentPoints.end(); aCIter++) {
696 bool isFirstFound = aCIter->find(thePoint1) != aCIter->end();
697 bool isSecondFound = aCIter->find(thePoint2) != aCIter->end();
698 isFound = isFound || isFirstFound || isSecondFound;
699 if (isFirstFound && isSecondFound)
700 break; // already coincident
701 else if (isFirstFound || isSecondFound) {
702 if (aFoundIter != myCoincidentPoints.end()) {
704 aFoundIter->insert(aCIter->begin(), aCIter->end());
705 myCoincidentPoints.erase(aCIter);
708 aCIter->insert(thePoint1);
709 aCIter->insert(thePoint2);
712 // coincident points not found
714 std::set<Slvs_hEntity> aNewSet;
715 aNewSet.insert(thePoint1);
716 aNewSet.insert(thePoint2);
717 myCoincidentPoints.push_back(aNewSet);
721 void SketchSolver_Storage::removeCoincidentPoint(const Slvs_hEntity& thePoint)
723 std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
724 for (; aCIter != myCoincidentPoints.end(); aCIter++)
725 if (aCIter->find(thePoint) != aCIter->end()) {
726 aCIter->erase(thePoint);
727 if (aCIter->size() <= 1)
728 myCoincidentPoints.erase(aCIter);
733 bool SketchSolver_Storage::isCoincident(
734 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
736 std::vector< std::set<Slvs_hEntity> >::const_iterator aCIter = myCoincidentPoints.begin();
737 for (; aCIter != myCoincidentPoints.end(); aCIter++)
738 if (aCIter->find(thePoint1) != aCIter->end() && aCIter->find(thePoint2) != aCIter->end())
740 // precise checking of coincidence
741 int aEnt1Pos = Search(thePoint1, myEntities);
742 int aEnt2Pos = Search(thePoint2, myEntities);
743 if (aEnt1Pos >= 0 && aEnt1Pos < (int)myEntities.size() &&
744 aEnt2Pos >= 0 && aEnt2Pos < (int)myEntities.size()) {
747 for (int i = 0; i < 2; i++) {
748 aParamPos = Search(myEntities[aEnt1Pos].param[i], myParameters);
749 aDist[i] = myParameters[aParamPos].val;
750 aParamPos = Search(myEntities[aEnt2Pos].param[i], myParameters);
751 aDist[i] -= myParameters[aParamPos].val;
753 if (aDist[0] * aDist[0] + aDist[1] * aDist[1] < tolerance * tolerance)
760 std::vector<Slvs_hConstraint> SketchSolver_Storage::fixEntity(const Slvs_hEntity& theEntity)
762 std::vector<Slvs_hConstraint> aNewConstraints;
764 int aPos = Search(theEntity, myEntities);
765 if (aPos >= 0 && aPos < (int)myEntities.size()) {
766 switch (myEntities[aPos].type) {
767 case SLVS_E_POINT_IN_2D:
768 case SLVS_E_POINT_IN_3D:
769 fixPoint(myEntities[aPos], aNewConstraints);
771 case SLVS_E_LINE_SEGMENT:
772 fixLine(myEntities[aPos], aNewConstraints);
775 fixCircle(myEntities[aPos], aNewConstraints);
777 case SLVS_E_ARC_OF_CIRCLE:
778 fixArc(myEntities[aPos], aNewConstraints);
785 return aNewConstraints;
788 void SketchSolver_Storage::fixPoint(const Slvs_Entity& thePoint,
789 std::vector<Slvs_hConstraint>& theCreated)
791 Slvs_Constraint aConstraint;
792 Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
793 bool isFixed = isPointFixed(thePoint.h, aConstrID, true);
794 bool isForceUpdate = (isFixed && isTemporary(aConstrID));
795 if (!isForceUpdate) { // create new constraint
797 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, thePoint.group, SLVS_C_WHERE_DRAGGED, thePoint.wrkpl,
798 0.0, thePoint.h, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
799 aConstraint.h = addConstraint(aConstraint);
800 theCreated.push_back(aConstraint.h);
801 } else { // update already existent constraint
802 if (!isFixed || aConstrID == SLVS_E_UNKNOWN)
804 int aPos = Search(aConstrID, myConstraints);
805 if (aPos >= 0 && aPos < (int)myConstraints.size())
806 myConstraints[aPos].ptA = thePoint.h;
810 void SketchSolver_Storage::fixLine(const Slvs_Entity& theLine,
811 std::vector<Slvs_hConstraint>& theCreated)
813 Slvs_Entity aPoint[2] = {
814 getEntity(theLine.point[0]),
815 getEntity(theLine.point[1])
818 Slvs_Constraint anEqual;
819 if (isAxisParallel(theLine.h)) {
820 // Fix one point and a line length
821 Slvs_hConstraint aFixed;
822 if (!isPointFixed(theLine.point[0], aFixed, true) &&
823 !isPointFixed(theLine.point[1], aFixed, true))
824 fixPoint(aPoint[0], theCreated);
825 if (!isUsedInEqual(theLine.h, anEqual)) {
826 // Check the distance is not set yet
827 std::vector<Slvs_Constraint>::const_iterator aDistIt = myConstraints.begin();
828 for (; aDistIt != myConstraints.end(); ++aDistIt)
829 if ((aDistIt->type == SLVS_C_PT_PT_DISTANCE) &&
830 ((aDistIt->ptA == theLine.point[0] && aDistIt->ptB == theLine.point[1]) ||
831 (aDistIt->ptA == theLine.point[1] && aDistIt->ptB == theLine.point[0])))
833 // Calculate distance between points on the line
835 for (int i = 0; i < 2; i++)
836 for (int j = 0; j < 2; j++) {
837 Slvs_Param aParam = getParameter(aPoint[i].param[j]);
838 aCoords[2*i+j] = aParam.val;
841 double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) +
842 (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
844 Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group,
845 SLVS_C_PT_PT_DISTANCE, theLine.wrkpl, aLength,
846 theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
847 aDistance.h = addConstraint(aDistance);
848 theCreated.push_back(aDistance.h);
852 else if (isUsedInEqual(theLine.h, anEqual)) {
853 // Check another entity of Equal is already fixed
854 Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
855 if (isEntityFixed(anOtherEntID, true)) {
856 // Fix start point of the line (if end point is not fixed yet) ...
857 Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
858 bool isFixed = isPointFixed(theLine.point[1], anEndFixedID, true);
859 if (isFixed == SLVS_E_UNKNOWN)
860 fixPoint(aPoint[0], theCreated);
861 // ... and create fixed point lying on this line
862 Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
863 // Firstly, search already fixed point on line
864 bool isPonLineFixed = false;
865 Slvs_hEntity aFixedPoint;
866 std::vector<Slvs_Constraint>::const_iterator aPLIter = myConstraints.begin();
867 for (; aPLIter != myConstraints.end() && !isPonLineFixed; ++aPLIter)
868 if (aPLIter->type == SLVS_C_PT_ON_LINE && aPLIter->entityA == theLine.h) {
869 isPonLineFixed = isPointFixed(aPLIter->ptA, anEndFixedID);
870 aFixedPoint = aPLIter->ptA;
873 if (isPonLineFixed) { // update existent constraint
874 copyEntity(aPointToCopy, aFixedPoint);
875 } else { // create new constraint
876 Slvs_hEntity aCopied = copyEntity(aPointToCopy);
877 Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group, SLVS_C_PT_ON_LINE,
878 theLine.wrkpl, 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
879 aPonLine.h = addConstraint(aPonLine);
880 theCreated.push_back(aPonLine.h);
881 fixPoint(getEntity(aCopied), theCreated);
888 for (int i = 0; i < 2; i++)
889 fixPoint(aPoint[i], theCreated);
892 void SketchSolver_Storage::fixCircle(const Slvs_Entity& theCircle,
893 std::vector<Slvs_hConstraint>& theCreated)
895 bool isFixRadius = true;
896 // Verify the arc is under Equal constraint
897 Slvs_Constraint anEqual;
898 if (isUsedInEqual(theCircle.h, anEqual)) {
899 // Check another entity of Equal is already fixed
900 Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
901 if (isEntityFixed(anOtherEntID, true))
905 fixPoint(getEntity(theCircle.point[0]), theCreated);
908 // Search the radius is already fixed
909 std::vector<Slvs_Constraint>::const_iterator aDiamIter = myConstraints.begin();
910 for (; aDiamIter != myConstraints.end(); ++aDiamIter)
911 if (aDiamIter->type == SLVS_C_DIAMETER && aDiamIter->entityA == theCircle.h)
914 // Fix radius of a circle
915 const Slvs_Entity& aRadEnt = getEntity(theCircle.distance);
916 double aRadius = getParameter(aRadEnt.param[0]).val;
917 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theCircle.group, SLVS_C_DIAMETER,
918 theCircle.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theCircle.h, SLVS_E_UNKNOWN);
919 aFixedR.h = addConstraint(aFixedR);
920 theCreated.push_back(aFixedR.h);
924 void SketchSolver_Storage::fixArc(const Slvs_Entity& theArc,
925 std::vector<Slvs_hConstraint>& theCreated)
927 Slvs_Entity aPoint[3] = {
928 getEntity(theArc.point[0]),
929 getEntity(theArc.point[1]),
930 getEntity(theArc.point[2])
933 bool isFixRadius = true;
934 std::list<Slvs_Entity> aPointsToFix;
935 aPointsToFix.push_back(aPoint[1]);
936 aPointsToFix.push_back(aPoint[2]);
938 // Verify the arc is under Equal constraint
939 Slvs_Constraint anEqual;
940 if (isUsedInEqual(theArc.h, anEqual)) {
941 // Check another entity of Equal is already fixed
942 Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
943 if (isEntityFixed(anOtherEntID, true)) {
945 Slvs_Entity anOtherEntity = getEntity(anOtherEntID);
946 if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
947 aPointsToFix.pop_back();
948 aPointsToFix.push_back(aPoint[0]);
953 Slvs_hConstraint aConstrID;
954 int aNbPointsToFix = 2; // number of fixed points for the arc
955 if (isPointFixed(theArc.point[0], aConstrID, true))
958 double anArcPoints[3][2];
959 for (int i = 0; i < 3; i++) {
960 const Slvs_Entity& aPointOnArc = getEntity(theArc.point[i]);
961 for (int j = 0; j < 2; j++)
962 anArcPoints[i][j] = getParameter(aPointOnArc.param[j]).val;
966 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(anArcPoints[0][0], anArcPoints[0][1]));
967 std::shared_ptr<GeomAPI_Pnt2d> aStart(new GeomAPI_Pnt2d(anArcPoints[1][0], anArcPoints[1][1]));
968 double aRadius = aCenter->distance(aStart);
970 // Update end point of the arc to be on a curve
971 std::shared_ptr<GeomAPI_Pnt2d> anEnd(new GeomAPI_Pnt2d(anArcPoints[2][0], anArcPoints[2][1]));
972 double aDistance = anEnd->distance(aCenter);
973 std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
974 if (aDistance < tolerance)
975 aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
977 aDir = aDir->multiplied(aRadius / aDistance);
978 double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
979 const Slvs_Entity& aEndPoint = getEntity(theArc.point[2]);
980 for (int i = 0; i < 2; i++) {
981 Slvs_Param aParam = getParameter(aEndPoint.param[i]);
983 updateParameter(aParam);
986 std::list<Slvs_Entity>::iterator aPtIt = aPointsToFix.begin();
987 for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
988 fixPoint(*aPtIt, theCreated);
991 // Fix radius of the arc
992 bool isExists = false;
993 std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
994 for (; anIt != myConstraints.end() && !isExists; ++anIt)
995 if (anIt->type == SLVS_C_DIAMETER && anIt->entityA == theArc.h)
998 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theArc.group, SLVS_C_DIAMETER,
999 theArc.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theArc.h, SLVS_E_UNKNOWN);
1000 aFixedR.h = addConstraint(aFixedR);
1001 theCreated.push_back(aFixedR.h);
1007 bool SketchSolver_Storage::isAxisParallel(const Slvs_hEntity& theEntity) const
1009 std::vector<Slvs_Constraint>::const_iterator anIter = myConstraints.begin();
1010 for (; anIter != myConstraints.end(); anIter++)
1011 if ((anIter->type == SLVS_C_HORIZONTAL || anIter->type == SLVS_C_VERTICAL) &&
1012 anIter->entityA == theEntity)
1017 bool SketchSolver_Storage::isUsedInEqual(
1018 const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const
1020 // Check the entity is used in Equal constraint
1021 std::vector<Slvs_Constraint>::const_iterator anEqIter = myConstraints.begin();
1022 for (; anEqIter != myConstraints.end(); anEqIter++)
1023 if ((anEqIter->type == SLVS_C_EQUAL_LENGTH_LINES ||
1024 anEqIter->type == SLVS_C_EQUAL_LINE_ARC_LEN ||
1025 anEqIter->type == SLVS_C_EQUAL_RADIUS) &&
1026 (anEqIter->entityA == theEntity || anEqIter->entityB == theEntity)) {
1027 theEqual = *anEqIter;
1037 // ========================================================
1038 // ========= Auxiliary functions ===============
1039 // ========================================================
1041 template<typename T>
1042 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1044 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1045 int aVecSize = theEntities.size();
1046 if (theEntities.empty())
1048 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1050 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1052 if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1053 aResIndex = aVecSize;
1057 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1059 return fabs(theParam1.val - theParam2.val) > tolerance;
1062 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1065 for (; theEntity1.param[i] != 0 && i < 4; i++)
1066 if (theEntity1.param[i] != theEntity2.param[i])
1069 for (; theEntity1.point[i] != 0 && i < 4; i++)
1070 if (theEntity1.point[i] != theEntity2.point[i])
1075 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1077 return theConstraint1.ptA != theConstraint2.ptA ||
1078 theConstraint1.ptB != theConstraint2.ptB ||
1079 theConstraint1.entityA != theConstraint2.entityA ||
1080 theConstraint1.entityB != theConstraint2.entityB ||
1081 theConstraint1.entityC != theConstraint2.entityC ||
1082 theConstraint1.entityD != theConstraint2.entityD ||
1083 fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;