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 if (IsNotEqual(myParameters[aPos], theParam))
63 myUpdatedParameters.insert(theParam.h);
64 myParameters[aPos] = theParam;
69 // Parameter is not found, add new one
70 Slvs_Param aParam = theParam;
72 return addParameter(aParam);
75 bool SketchSolver_Storage::removeParameter(const Slvs_hParam& theParamID)
77 int aPos = Search(theParamID, myParameters);
78 if (aPos >= 0 && aPos < (int)myParameters.size()) {
79 // Firstly, search the parametes is not used elsewhere
80 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
81 for (; anEntIter != myEntities.end(); anEntIter++) {
82 for (int i = 0; i < 4; i++)
83 if (anEntIter->param[i] == theParamID)
87 myParameters.erase(myParameters.begin() + aPos);
88 myParamMaxID = myParameters.empty() ? SLVS_E_UNKNOWN : myParameters.back().h;
89 myNeedToResolve = true;
90 myRemovedParameters.insert(theParamID);
96 const Slvs_Param& SketchSolver_Storage::getParameter(const Slvs_hParam& theParamID) const
98 int aPos = Search(theParamID, myParameters);
99 if (aPos >= 0 && aPos < (int)myParameters.size())
100 return myParameters[aPos];
102 // Parameter is not found, return empty object
103 static Slvs_Param aDummy;
109 Slvs_hEntity SketchSolver_Storage::addEntity(const Slvs_Entity& theEntity)
111 if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
112 // Entity is already used, rewrite it
113 return updateEntity(theEntity);
116 Slvs_Entity aEntity = theEntity;
117 if (aEntity.h > myEntityMaxID)
118 myEntityMaxID = aEntity.h;
120 aEntity.h = ++myEntityMaxID;
121 myEntities.push_back(aEntity);
122 myNeedToResolve = true;
126 Slvs_hEntity SketchSolver_Storage::updateEntity(const Slvs_Entity& theEntity)
128 if (theEntity.h > 0 && theEntity.h <= myEntityMaxID) {
129 // Entity already used, rewrite it
130 int aPos = Search(theEntity.h, myEntities);
131 if (aPos >= 0 && aPos < (int)myEntities.size()) {
132 myNeedToResolve = myNeedToResolve || IsNotEqual(myEntities[aPos], theEntity);
133 myEntities[aPos] = theEntity;
138 // Entity is not found, add new one
139 Slvs_Entity aEntity = theEntity;
141 return addEntity(aEntity);
144 bool SketchSolver_Storage::removeEntity(const Slvs_hEntity& theEntityID)
147 int aPos = Search(theEntityID, myEntities);
148 if (aPos >= 0 && aPos < (int)myEntities.size()) {
149 // Firstly, check the entity and its attributes is not used elsewhere
150 std::set<Slvs_hEntity> anEntAndSubs;
151 anEntAndSubs.insert(theEntityID);
152 for (int i = 0; i < 4; i++)
153 if (myEntities[aPos].point[i] != SLVS_E_UNKNOWN)
154 anEntAndSubs.insert(myEntities[aPos].point[i]);
156 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
157 for (; anEntIter != myEntities.end(); anEntIter++) {
158 for (int i = 0; i < 4; i++)
159 if (anEntAndSubs.find(anEntIter->point[i]) != anEntAndSubs.end())
161 if (anEntAndSubs.find(anEntIter->distance) != anEntAndSubs.end())
164 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
165 for (; aConstrIter != myConstraints.end(); aConstrIter++) {
166 Slvs_hEntity anEntIDs[6] = {aConstrIter->ptA, aConstrIter->ptB,
167 aConstrIter->entityA, aConstrIter->entityB,
168 aConstrIter->entityC, aConstrIter->entityD};
169 for (int i = 0; i < 6; i++)
170 if (anEntAndSubs.find(anEntIDs[i]) != anEntAndSubs.end())
173 // The entity is not used, remove it and its parameters
174 Slvs_Entity anEntity = myEntities[aPos];
175 myEntities.erase(myEntities.begin() + aPos);
176 myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
177 if (anEntity.distance != SLVS_E_UNKNOWN)
178 aResult = aResult && removeParameter(anEntity.distance);
179 for (int i = 0; i < 4; i++)
180 if (anEntity.param[i] != SLVS_E_UNKNOWN)
181 aResult = removeParameter(anEntity.param[i]) && aResult;
182 for (int i = 0; i < 4; i++)
183 if (anEntity.point[i] != SLVS_E_UNKNOWN)
184 aResult = removeEntity(anEntity.point[i]) && aResult;
185 myNeedToResolve = true;
186 myRemovedEntities.insert(theEntityID);
187 if (anEntity.type == SLVS_E_POINT_IN_2D || anEntity.type == SLVS_E_POINT_IN_3D)
188 removeCoincidentPoint(theEntityID);
193 void SketchSolver_Storage::removeUnusedEntities()
195 std::set<Slvs_hEntity> anUnusedEntities;
196 std::vector<Slvs_Entity>::const_iterator aEIt = myEntities.begin();
197 for (; aEIt != myEntities.end(); ++aEIt) {
198 if (aEIt->h == aEIt->wrkpl) {
199 // don't remove workplane
200 anUnusedEntities.erase(aEIt->point[0]);
201 anUnusedEntities.erase(aEIt->normal);
204 anUnusedEntities.insert(aEIt->h);
207 std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
208 for (; aCIt != myConstraints.end(); ++aCIt) {
209 Slvs_hEntity aSubs[6] = {
210 aCIt->entityA, aCIt->entityB,
211 aCIt->entityC, aCIt->entityD,
212 aCIt->ptA, aCIt->ptB};
213 for (int i = 0; i < 6; i++) {
214 if (aSubs[i] != SLVS_E_UNKNOWN) {
215 anUnusedEntities.erase(aSubs[i]);
216 int aPos = Search(aSubs[i], myEntities);
217 if (aPos >= 0 && aPos < (int)myEntities.size()) {
218 for (int j = 0; j < 4; j++)
219 if (myEntities[aPos].point[j] != SLVS_E_UNKNOWN)
220 anUnusedEntities.erase(myEntities[aPos].point[j]);
221 if (myEntities[aPos].distance != SLVS_E_UNKNOWN)
222 anUnusedEntities.erase(myEntities[aPos].distance);
228 std::set<Slvs_hEntity>::const_iterator anEntIt = anUnusedEntities.begin();
229 while (anEntIt != anUnusedEntities.end()) {
230 int aPos = Search(*anEntIt, myEntities);
231 if (aPos < 0 && aPos >= (int)myEntities.size())
233 Slvs_Entity anEntity = myEntities[aPos];
234 // Remove entity if and only if all its parameters unused
236 if (anEntity.distance != SLVS_E_UNKNOWN &&
237 anUnusedEntities.find(anEntity.distance) == anUnusedEntities.end())
239 for (int i = 0; i < 4 && !isUsed; i++)
240 if (anEntity.point[i] != SLVS_E_UNKNOWN &&
241 anUnusedEntities.find(anEntity.point[i]) == anUnusedEntities.end())
244 anUnusedEntities.erase(anEntity.distance);
245 for (int i = 0; i < 4; i++)
246 if (anEntity.point[i] != SLVS_E_UNKNOWN)
247 anUnusedEntities.erase(anEntity.point[i]);
248 std::set<Slvs_hEntity>::iterator aRemoveIt = anEntIt++;
249 anUnusedEntities.erase(aRemoveIt);
255 for (anEntIt = anUnusedEntities.begin(); anEntIt != anUnusedEntities.end(); ++anEntIt) {
256 int aPos = Search(*anEntIt, myEntities);
257 if (aPos >= 0 && aPos < (int)myEntities.size()) {
258 // Remove entity and its parameters
259 Slvs_Entity anEntity = myEntities[aPos];
260 myEntities.erase(myEntities.begin() + aPos);
261 myEntityMaxID = myEntities.empty() ? SLVS_E_UNKNOWN : myEntities.back().h;
262 if (anEntity.distance != SLVS_E_UNKNOWN)
263 removeParameter(anEntity.distance);
264 for (int i = 0; i < 4; i++)
265 if (anEntity.param[i] != SLVS_E_UNKNOWN)
266 removeParameter(anEntity.param[i]);
267 for (int i = 0; i < 4; i++)
268 if (anEntity.point[i] != SLVS_E_UNKNOWN)
269 removeEntity(anEntity.point[i]);
270 myRemovedEntities.insert(*anEntIt);
271 if (anEntity.type == SLVS_E_POINT_IN_2D || anEntity.type == SLVS_E_POINT_IN_3D)
272 removeCoincidentPoint(*anEntIt);
276 if (!anUnusedEntities.empty())
277 myNeedToResolve = true;
280 bool SketchSolver_Storage::isUsedByConstraints(const Slvs_hEntity& theEntityID) const
282 std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
283 for (; aCIt != myConstraints.end(); ++aCIt) {
284 Slvs_hEntity aSubs[6] = {
285 aCIt->entityA, aCIt->entityB,
286 aCIt->entityC, aCIt->entityD,
287 aCIt->ptA, aCIt->ptB};
288 for (int i = 0; i < 6; i++)
289 if (aSubs[i] != SLVS_E_UNKNOWN && aSubs[i] == theEntityID)
295 const Slvs_Entity& SketchSolver_Storage::getEntity(const Slvs_hEntity& theEntityID) const
297 int aPos = Search(theEntityID, myEntities);
298 if (aPos >= 0 && aPos < (int)myEntities.size())
299 return myEntities[aPos];
301 // Entity is not found, return empty object
302 static Slvs_Entity aDummy;
303 aDummy.h = SLVS_E_UNKNOWN;
307 Slvs_hEntity SketchSolver_Storage::copyEntity(const Slvs_hEntity& theCopied)
309 int aPos = Search(theCopied, myEntities);
310 if (aPos < 0 || aPos >= (int)myEntities.size())
311 return SLVS_E_UNKNOWN;
313 Slvs_Entity aCopy = myEntities[aPos];
314 aCopy.h = SLVS_E_UNKNOWN;
316 while (aCopy.point[i] != SLVS_E_UNKNOWN) {
317 aCopy.point[i] = copyEntity(aCopy.point[i]);
320 if (aCopy.param[0] != SLVS_E_UNKNOWN) {
321 aPos = Search(aCopy.param[0], myParameters);
323 while (aCopy.param[i] != SLVS_E_UNKNOWN) {
324 Slvs_Param aNewParam = myParameters[aPos];
325 aNewParam.h = SLVS_E_UNKNOWN;
326 aCopy.param[i] = addParameter(aNewParam);
331 return addEntity(aCopy);
334 void SketchSolver_Storage::copyEntity(const Slvs_hEntity& theFrom, const Slvs_hEntity& theTo)
336 int aPosFrom = Search(theFrom, myEntities);
337 int aPosTo = Search(theTo, myEntities);
338 if (aPosFrom < 0 || aPosFrom >= (int)myEntities.size() ||
339 aPosTo < 0 || aPosTo >= (int)myEntities.size())
342 Slvs_Entity aEntFrom = myEntities[aPosFrom];
343 Slvs_Entity aEntTo = myEntities[aPosTo];
345 while (aEntFrom.point[i] != SLVS_E_UNKNOWN) {
346 copyEntity(aEntFrom.point[i], aEntTo.point[i]);
349 if (aEntFrom.param[0] != SLVS_E_UNKNOWN) {
350 aPosFrom = Search(aEntFrom.param[0], myParameters);
351 aPosTo = Search(aEntTo.param[0], myParameters);
353 while (aEntFrom.param[i] != SLVS_E_UNKNOWN) {
354 myParameters[aPosTo++].val = myParameters[aPosFrom++].val;
361 bool SketchSolver_Storage::isPointFixed(
362 const Slvs_hEntity& thePointID, Slvs_hConstraint& theFixed, bool theAccurate) const
364 // Search the set of coincident points
365 std::set<Slvs_hEntity> aCoincident;
366 aCoincident.insert(thePointID);
367 std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
368 for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
369 if (aCPIter->find(thePointID) != aCPIter->end()) {
370 aCoincident = *aCPIter;
374 // Search the Rigid constraint
375 theFixed = SLVS_C_UNKNOWN;
376 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
377 for (; aConstrIter != myConstraints.end(); aConstrIter++)
378 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
379 aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
380 theFixed = aConstrIter->h;
381 if (aConstrIter->ptA == thePointID)
384 if (theFixed != SLVS_C_UNKNOWN)
388 // Try to find the fixed entity which uses such point or its coincidence
389 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
390 for (; anEntIter != myEntities.end(); anEntIter++) {
391 for (int i = 0; i < 4; i++) {
392 Slvs_hEntity aPt = anEntIter->point[i];
393 if (aPt != SLVS_E_UNKNOWN &&
394 (aPt == thePointID || aCoincident.find(aPt) != aCoincident.end())) {
395 if (isEntityFixed(anEntIter->h, true))
401 return SLVS_E_UNKNOWN;
404 bool SketchSolver_Storage::isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate) const
406 int aPos = Search(theEntityID, myEntities);
407 if (aPos < 0 || aPos >= (int)myEntities.size())
410 // Firstly, find how many points are under Rigid constraint
412 for (int i = 0; i < 4; i++) {
413 Slvs_hEntity aPoint = myEntities[aPos].point[i];
414 if (aPoint == SLVS_E_UNKNOWN)
417 std::set<Slvs_hEntity> aCoincident;
418 aCoincident.insert(aPoint);
419 std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
420 for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
421 if (aCPIter->find(aPoint) != aCPIter->end()) {
422 aCoincident = *aCPIter;
426 // Search the Rigid constraint
427 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
428 for (; aConstrIter != myConstraints.end(); aConstrIter++)
429 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
430 aCoincident.find(aConstrIter->ptA) != aCoincident.end())
434 std::list<Slvs_Constraint> aList;
435 std::list<Slvs_Constraint>::iterator anIt;
436 Slvs_hConstraint aTempID; // used in isPointFixed() method
438 if (myEntities[aPos].type == SLVS_E_LINE_SEGMENT) {
441 else if (aNbFixed == 0 || !theAccurate)
443 // Additional check (the line may be fixed if it is used by different constraints):
444 // 1. The line is used in Equal constraint, another entity is fixed and there is a fixed point on line
445 aList = getConstraintsByType(SLVS_C_PT_ON_LINE);
446 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
447 if (anIt->entityA == theEntityID && isPointFixed(anIt->ptA, aTempID))
449 if (anIt != aList.end()) {
450 aList = getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
451 aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
452 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
453 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
454 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
455 if (isEntityFixed(anOther, false))
459 // 2. The line is used in Parallel/Perpendicular/Vertical/Horizontal and Length constraints
460 aList = getConstraintsByType(SLVS_C_PARALLEL);
461 aList.splice(aList.end(), getConstraintsByType(SLVS_C_PERPENDICULAR));
462 aList.splice(aList.end(), getConstraintsByType(SLVS_C_VERTICAL));
463 aList.splice(aList.end(), getConstraintsByType(SLVS_C_HORIZONTAL));
464 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
465 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
466 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
467 if (isEntityFixed(anOther, false))
470 if (anIt != aList.end()) {
471 aList = getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
472 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
473 if ((anIt->ptA == myEntities[aPos].point[0] && anIt->ptB == myEntities[aPos].point[1]) ||
474 (anIt->ptA == myEntities[aPos].point[1] && anIt->ptB == myEntities[aPos].point[0]))
477 // 3. Another verifiers ...
478 } else if (myEntities[aPos].type == SLVS_E_CIRCLE) {
481 // Search for Diameter constraint
482 aList = getConstraintsByType(SLVS_C_DIAMETER);
483 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
484 if (anIt->entityA == theEntityID)
488 // Additional check (the circle may be fixed if it is used by different constraints):
489 // 1. The circle is used in Equal constraint and another entity is fixed
490 aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
491 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
492 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
493 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
494 if (isEntityFixed(anOther, false))
497 // 2. Another verifiers ...
498 } else if (myEntities[aPos].type == SLVS_E_ARC_OF_CIRCLE) {
501 else if (aNbFixed <= 1)
503 // Search for Diameter constraint
504 aList = getConstraintsByType(SLVS_C_DIAMETER);
505 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
506 if (anIt->entityA == theEntityID)
510 // Additional check (the arc may be fixed if it is used by different constraints):
511 // 1. The arc is used in Equal constraint and another entity is fixed
512 aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
513 aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
514 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
515 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
516 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
517 if (isEntityFixed(anOther, false))
520 // 2. Another verifiers ...
526 Slvs_hConstraint SketchSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
528 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
529 // Constraint is already used, rewrite it
530 return updateConstraint(theConstraint);
533 Slvs_Constraint aConstraint = theConstraint;
535 // Find a constraint with same type uses same arguments to show user overconstraint situation
536 std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
537 for (; aCIt != myConstraints.end(); aCIt++) {
538 if (aConstraint.type != aCIt->type)
540 if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
541 aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
542 aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD)
543 myDuplicatedConstraint = true;
546 if (aConstraint.h > myConstrMaxID)
547 myConstrMaxID = aConstraint.h;
549 aConstraint.h = ++myConstrMaxID;
550 myConstraints.push_back(aConstraint);
551 myNeedToResolve = true;
552 if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
553 addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
554 return aConstraint.h;
557 Slvs_hConstraint SketchSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
559 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
560 // Constraint already used, rewrite it
561 int aPos = Search(theConstraint.h, myConstraints);
562 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
563 myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
564 myConstraints[aPos] = theConstraint;
565 if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
566 addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
567 return theConstraint.h;
571 // Constraint is not found, add new one
572 Slvs_Constraint aConstraint = theConstraint;
574 return addConstraint(aConstraint);
577 bool SketchSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
580 int aPos = Search(theConstraintID, myConstraints);
581 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
582 Slvs_Constraint aConstraint = myConstraints[aPos];
583 myConstraints.erase(myConstraints.begin() + aPos);
584 myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
585 myNeedToResolve = true;
586 myRemovedConstraints.insert(theConstraintID);
587 if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
588 removeCoincidence(aConstraint);
590 // Remove all entities
591 Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
592 aConstraint.entityA, aConstraint.entityB,
593 aConstraint.entityC, aConstraint.entityD};
594 for (int i = 0; i < 6; i++)
595 if (anEntities[i] != SLVS_E_UNKNOWN)
596 aResult = removeEntity(anEntities[i]) && aResult;
597 // remove temporary fixed point, if available
598 if (myFixed == theConstraintID)
599 myFixed = SLVS_E_UNKNOWN;
600 if (myDuplicatedConstraint) {
601 // Check the duplicated constraints are still available
602 myDuplicatedConstraint = false;
603 std::vector<Slvs_Constraint>::const_iterator anIt1 = myConstraints.begin();
604 std::vector<Slvs_Constraint>::const_iterator anIt2 = myConstraints.begin();
605 for (; anIt1 != myConstraints.end() && !myDuplicatedConstraint; anIt1++)
606 for (anIt2 = anIt1+1; anIt2 != myConstraints.end() && !myDuplicatedConstraint; anIt2++) {
607 if (anIt1->type != anIt2->type)
609 if (anIt1->ptA == anIt2->ptA && anIt1->ptB == anIt2->ptB &&
610 anIt1->entityA == anIt2->entityA && anIt1->entityB == anIt2->entityB &&
611 anIt1->entityC == anIt2->entityC && anIt1->entityD == anIt2->entityD)
612 myDuplicatedConstraint = true;
619 const Slvs_Constraint& SketchSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
621 int aPos = Search(theConstraintID, myConstraints);
622 if (aPos >= 0 && aPos < (int)myConstraints.size())
623 return myConstraints[aPos];
625 // Constraint is not found, return empty object
626 static Slvs_Constraint aDummy;
631 std::list<Slvs_Constraint> SketchSolver_Storage::getConstraintsByType(int theConstraintType) const
633 std::list<Slvs_Constraint> aResult;
634 std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
635 for (; aCIter != myConstraints.end(); aCIter++)
636 if (aCIter->type == theConstraintType)
637 aResult.push_back(*aCIter);
642 void SketchSolver_Storage::addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID)
644 if (myFixed != SLVS_E_UNKNOWN)
645 return; // the point is already fixed
646 int aPos = Search(theConstraintID, myConstraints);
647 if (aPos >= 0 && aPos < (int)myConstraints.size())
648 myFixed = theConstraintID;
651 void SketchSolver_Storage::addTemporaryConstraint(const Slvs_hConstraint& theConstraintID)
653 myTemporaryConstraints.insert(theConstraintID);
656 void SketchSolver_Storage::removeTemporaryConstraints()
658 myTemporaryConstraints.clear();
661 int SketchSolver_Storage::deleteTemporaryConstraint()
663 if (myTemporaryConstraints.empty())
665 // Search the point-on-line or a non-rigid constraint
666 std::set<Slvs_hConstraint>::iterator aCIt = myTemporaryConstraints.begin();
667 for (; aCIt != myTemporaryConstraints.end(); aCIt++) {
668 int aPos = Search(*aCIt, myConstraints);
669 if (aPos >= (int)myConstraints.size() || myConstraints[aPos].type != SLVS_C_WHERE_DRAGGED)
671 std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
672 for (; anIt != myConstraints.end(); anIt++)
673 if (anIt->type == SLVS_C_PT_ON_LINE && anIt->ptA == myConstraints[aPos].ptA)
675 if (anIt != myConstraints.end())
678 if (aCIt == myTemporaryConstraints.end())
679 aCIt = myTemporaryConstraints.begin();
680 bool aNewFixed = (*aCIt == myFixed);
681 removeConstraint(*aCIt);
682 myTemporaryConstraints.erase(aCIt);
684 for (aCIt = myTemporaryConstraints.begin(); aCIt != myTemporaryConstraints.end(); aCIt++) {
685 int aPos = Search(*aCIt, myConstraints);
686 if (myConstraints[aPos].type == SLVS_C_WHERE_DRAGGED) {
692 return (int)myTemporaryConstraints.size();
695 bool SketchSolver_Storage::isTemporary(const Slvs_hConstraint& theConstraintID) const
697 return myTemporaryConstraints.find(theConstraintID) != myTemporaryConstraints.end();
701 void SketchSolver_Storage::getRemoved(
702 std::set<Slvs_hParam>& theParameters,
703 std::set<Slvs_hEntity>& theEntities,
704 std::set<Slvs_hConstraint>& theConstraints)
706 theParameters = myRemovedParameters;
707 theEntities = myRemovedEntities;
708 theConstraints = myRemovedConstraints;
710 myRemovedParameters.clear();
711 myRemovedEntities.clear();
712 myRemovedConstraints.clear();
715 void SketchSolver_Storage::initializeSolver(SketchSolver_Solver& theSolver)
717 theSolver.setParameters(myParameters.data(), (int)myParameters.size());
718 theSolver.setEntities(myEntities.data(), (int)myEntities.size());
720 // Copy constraints excluding the fixed one
721 std::vector<Slvs_Constraint> aConstraints = myConstraints;
722 if (myFixed != SLVS_E_UNKNOWN) {
723 Slvs_hEntity aFixedPoint = SLVS_E_UNKNOWN;
724 std::vector<Slvs_Constraint>::iterator anIt = aConstraints.begin();
725 for (; anIt != aConstraints.end(); anIt++)
726 if (anIt->h == myFixed) {
727 aFixedPoint = anIt->ptA;
728 aConstraints.erase(anIt);
731 // set dragged parameters
732 int aPos = Search(aFixedPoint, myEntities);
733 theSolver.setDraggedParameters(myEntities[aPos].param);
735 theSolver.setConstraints(aConstraints.data(), (int)aConstraints.size());
738 void SketchSolver_Storage::addCoincidentPoints(
739 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
741 std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
742 std::vector< std::set<Slvs_hEntity> >::iterator aFoundIter = myCoincidentPoints.end(); // already found coincidence
743 bool isFound = false;
744 for (; aCIter != myCoincidentPoints.end(); aCIter++) {
745 bool isFirstFound = aCIter->find(thePoint1) != aCIter->end();
746 bool isSecondFound = aCIter->find(thePoint2) != aCIter->end();
747 isFound = isFound || isFirstFound || isSecondFound;
748 if (isFirstFound && isSecondFound)
749 break; // already coincident
750 else if (isFirstFound || isSecondFound) {
751 if (aFoundIter != myCoincidentPoints.end()) {
753 aFoundIter->insert(aCIter->begin(), aCIter->end());
754 myCoincidentPoints.erase(aCIter);
758 aCIter->insert(thePoint1);
759 aCIter->insert(thePoint2);
762 // coincident points not found
764 std::set<Slvs_hEntity> aNewSet;
765 aNewSet.insert(thePoint1);
766 aNewSet.insert(thePoint2);
767 myCoincidentPoints.push_back(aNewSet);
771 void SketchSolver_Storage::removeCoincidentPoint(const Slvs_hEntity& thePoint)
773 std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
774 for (; aCIter != myCoincidentPoints.end(); aCIter++)
775 if (aCIter->find(thePoint) != aCIter->end()) {
776 aCIter->erase(thePoint);
777 if (aCIter->size() <= 1)
778 myCoincidentPoints.erase(aCIter);
783 void SketchSolver_Storage::removeCoincidence(const Slvs_Constraint& theCoincidence)
785 // Find set of coincident points
786 std::vector< std::set<Slvs_hEntity> >::iterator aCIt = myCoincidentPoints.begin();
787 for (; aCIt != myCoincidentPoints.end(); ++aCIt)
788 if (aCIt->find(theCoincidence.ptA) != aCIt->end() ||
789 aCIt->find(theCoincidence.ptB) != aCIt->end())
791 if (aCIt == myCoincidentPoints.end())
794 // Leave only the points which are still coincident
795 std::set<Slvs_hEntity> aRemainCoincidence;
796 std::vector<Slvs_Constraint>::const_iterator aConstrIt = myConstraints.begin();
797 for (; aConstrIt != myConstraints.end(); ++aConstrIt) {
798 if (aConstrIt->type != SLVS_C_POINTS_COINCIDENT)
800 if (aCIt->find(aConstrIt->ptA) != aCIt->end() ||
801 aCIt->find(aConstrIt->ptB) != aCIt->end()) {
802 aRemainCoincidence.insert(aConstrIt->ptA);
803 aRemainCoincidence.insert(aConstrIt->ptB);
806 if (aRemainCoincidence.size() <= 1)
807 myCoincidentPoints.erase(aCIt);
809 aCIt->swap(aRemainCoincidence);
812 bool SketchSolver_Storage::isCoincident(
813 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
815 std::vector< std::set<Slvs_hEntity> >::const_iterator aCIter = myCoincidentPoints.begin();
816 for (; aCIter != myCoincidentPoints.end(); aCIter++)
817 if (aCIter->find(thePoint1) != aCIter->end() && aCIter->find(thePoint2) != aCIter->end())
822 bool SketchSolver_Storage::isEqual(
823 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
825 if (isCoincident(thePoint1, thePoint2))
828 // Precise checking of coincidence: verify that points have equal coordinates
829 int aEnt1Pos = Search(thePoint1, myEntities);
830 int aEnt2Pos = Search(thePoint2, myEntities);
831 if (aEnt1Pos >= 0 && aEnt1Pos < (int)myEntities.size() &&
832 aEnt2Pos >= 0 && aEnt2Pos < (int)myEntities.size()) {
835 for (int i = 0; i < 2; i++) {
836 aParamPos = Search(myEntities[aEnt1Pos].param[i], myParameters);
837 aDist[i] = myParameters[aParamPos].val;
838 aParamPos = Search(myEntities[aEnt2Pos].param[i], myParameters);
839 aDist[i] -= myParameters[aParamPos].val;
841 if (aDist[0] * aDist[0] + aDist[1] * aDist[1] < tolerance * tolerance)
848 std::vector<Slvs_hConstraint> SketchSolver_Storage::fixEntity(const Slvs_hEntity& theEntity)
850 std::vector<Slvs_hConstraint> aNewConstraints;
852 int aPos = Search(theEntity, myEntities);
853 if (aPos >= 0 && aPos < (int)myEntities.size()) {
854 switch (myEntities[aPos].type) {
855 case SLVS_E_POINT_IN_2D:
856 case SLVS_E_POINT_IN_3D:
857 fixPoint(myEntities[aPos], aNewConstraints);
859 case SLVS_E_LINE_SEGMENT:
860 fixLine(myEntities[aPos], aNewConstraints);
863 fixCircle(myEntities[aPos], aNewConstraints);
865 case SLVS_E_ARC_OF_CIRCLE:
866 fixArc(myEntities[aPos], aNewConstraints);
873 return aNewConstraints;
876 void SketchSolver_Storage::fixPoint(const Slvs_Entity& thePoint,
877 std::vector<Slvs_hConstraint>& theCreated)
879 Slvs_Constraint aConstraint;
880 Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
881 bool isFixed = isPointFixed(thePoint.h, aConstrID, true);
882 bool isForceUpdate = (isFixed && isTemporary(aConstrID));
883 if (!isForceUpdate) { // create new constraint
885 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, thePoint.group, SLVS_C_WHERE_DRAGGED, thePoint.wrkpl,
886 0.0, thePoint.h, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
887 aConstraint.h = addConstraint(aConstraint);
888 theCreated.push_back(aConstraint.h);
889 } else { // update already existent constraint
890 if (!isFixed || aConstrID == SLVS_E_UNKNOWN)
892 int aPos = Search(aConstrID, myConstraints);
893 if (aPos >= 0 && aPos < (int)myConstraints.size())
894 myConstraints[aPos].ptA = thePoint.h;
898 void SketchSolver_Storage::fixLine(const Slvs_Entity& theLine,
899 std::vector<Slvs_hConstraint>& theCreated)
901 Slvs_Entity aPoint[2] = {
902 getEntity(theLine.point[0]),
903 getEntity(theLine.point[1])
906 Slvs_Constraint anEqual;
907 if (isAxisParallel(theLine.h)) {
908 // Fix one point and a line length
909 Slvs_hConstraint aFixed;
910 if (!isPointFixed(theLine.point[0], aFixed, true) &&
911 !isPointFixed(theLine.point[1], aFixed, true))
912 fixPoint(aPoint[0], theCreated);
913 if (!isUsedInEqual(theLine.h, anEqual)) {
914 // Check the distance is not set yet
915 std::vector<Slvs_Constraint>::const_iterator aDistIt = myConstraints.begin();
916 for (; aDistIt != myConstraints.end(); ++aDistIt)
917 if ((aDistIt->type == SLVS_C_PT_PT_DISTANCE) &&
918 ((aDistIt->ptA == theLine.point[0] && aDistIt->ptB == theLine.point[1]) ||
919 (aDistIt->ptA == theLine.point[1] && aDistIt->ptB == theLine.point[0])))
921 // Calculate distance between points on the line
923 for (int i = 0; i < 2; i++)
924 for (int j = 0; j < 2; j++) {
925 Slvs_Param aParam = getParameter(aPoint[i].param[j]);
926 aCoords[2*i+j] = aParam.val;
929 double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) +
930 (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
932 Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group,
933 SLVS_C_PT_PT_DISTANCE, theLine.wrkpl, aLength,
934 theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
935 aDistance.h = addConstraint(aDistance);
936 theCreated.push_back(aDistance.h);
940 else if (isUsedInEqual(theLine.h, anEqual)) {
941 // Check another entity of Equal is already fixed
942 Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
943 if (isEntityFixed(anOtherEntID, true)) {
944 // Fix start point of the line (if end point is not fixed yet) ...
945 Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
946 bool isFixed = isPointFixed(theLine.point[1], anEndFixedID, true);
947 if (isFixed == SLVS_E_UNKNOWN)
948 fixPoint(aPoint[0], theCreated);
949 // ... and create fixed point lying on this line
950 Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
951 // Firstly, search already fixed point on line
952 bool isPonLineFixed = false;
953 Slvs_hEntity aFixedPoint;
954 std::vector<Slvs_Constraint>::const_iterator aPLIter = myConstraints.begin();
955 for (; aPLIter != myConstraints.end() && !isPonLineFixed; ++aPLIter)
956 if (aPLIter->type == SLVS_C_PT_ON_LINE && aPLIter->entityA == theLine.h) {
957 isPonLineFixed = isPointFixed(aPLIter->ptA, anEndFixedID);
958 aFixedPoint = aPLIter->ptA;
961 if (isPonLineFixed) { // update existent constraint
962 copyEntity(aPointToCopy, aFixedPoint);
963 } else { // create new constraint
964 Slvs_hEntity aCopied = copyEntity(aPointToCopy);
965 Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group, SLVS_C_PT_ON_LINE,
966 theLine.wrkpl, 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
967 aPonLine.h = addConstraint(aPonLine);
968 theCreated.push_back(aPonLine.h);
969 fixPoint(getEntity(aCopied), theCreated);
976 for (int i = 0; i < 2; i++)
977 fixPoint(aPoint[i], theCreated);
980 void SketchSolver_Storage::fixCircle(const Slvs_Entity& theCircle,
981 std::vector<Slvs_hConstraint>& theCreated)
983 bool isFixRadius = true;
984 // Verify the arc is under Equal constraint
985 Slvs_Constraint anEqual;
986 if (isUsedInEqual(theCircle.h, anEqual)) {
987 // Check another entity of Equal is already fixed
988 Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
989 if (isEntityFixed(anOtherEntID, true))
993 fixPoint(getEntity(theCircle.point[0]), theCreated);
996 // Search the radius is already fixed
997 std::vector<Slvs_Constraint>::const_iterator aDiamIter = myConstraints.begin();
998 for (; aDiamIter != myConstraints.end(); ++aDiamIter)
999 if (aDiamIter->type == SLVS_C_DIAMETER && aDiamIter->entityA == theCircle.h)
1002 // Fix radius of a circle
1003 const Slvs_Entity& aRadEnt = getEntity(theCircle.distance);
1004 double aRadius = getParameter(aRadEnt.param[0]).val;
1005 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theCircle.group, SLVS_C_DIAMETER,
1006 theCircle.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theCircle.h, SLVS_E_UNKNOWN);
1007 aFixedR.h = addConstraint(aFixedR);
1008 theCreated.push_back(aFixedR.h);
1012 void SketchSolver_Storage::fixArc(const Slvs_Entity& theArc,
1013 std::vector<Slvs_hConstraint>& theCreated)
1015 Slvs_Entity aPoint[3] = {
1016 getEntity(theArc.point[0]),
1017 getEntity(theArc.point[1]),
1018 getEntity(theArc.point[2])
1021 bool isFixRadius = true;
1022 std::list<Slvs_Entity> aPointsToFix;
1023 aPointsToFix.push_back(aPoint[1]);
1024 aPointsToFix.push_back(aPoint[2]);
1026 // Verify the arc is under Equal constraint
1027 Slvs_Constraint anEqual;
1028 if (isUsedInEqual(theArc.h, anEqual)) {
1029 // Check another entity of Equal is already fixed
1030 Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
1031 if (isEntityFixed(anOtherEntID, true)) {
1032 isFixRadius = false;
1033 Slvs_Entity anOtherEntity = getEntity(anOtherEntID);
1034 if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
1035 aPointsToFix.pop_back();
1036 aPointsToFix.push_back(aPoint[0]);
1041 Slvs_hConstraint aConstrID;
1042 int aNbPointsToFix = 2; // number of fixed points for the arc
1043 if (isPointFixed(theArc.point[0], aConstrID, true))
1046 double anArcPoints[3][2];
1047 for (int i = 0; i < 3; i++) {
1048 const Slvs_Entity& aPointOnArc = getEntity(theArc.point[i]);
1049 for (int j = 0; j < 2; j++)
1050 anArcPoints[i][j] = getParameter(aPointOnArc.param[j]).val;
1053 // Radius of the arc
1054 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(anArcPoints[0][0], anArcPoints[0][1]));
1055 std::shared_ptr<GeomAPI_Pnt2d> aStart(new GeomAPI_Pnt2d(anArcPoints[1][0], anArcPoints[1][1]));
1056 double aRadius = aCenter->distance(aStart);
1058 // Update end point of the arc to be on a curve
1059 std::shared_ptr<GeomAPI_Pnt2d> anEnd(new GeomAPI_Pnt2d(anArcPoints[2][0], anArcPoints[2][1]));
1060 double aDistance = anEnd->distance(aCenter);
1061 std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
1062 if (aDistance < tolerance)
1063 aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
1065 aDir = aDir->multiplied(aRadius / aDistance);
1066 double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
1067 const Slvs_Entity& aEndPoint = getEntity(theArc.point[2]);
1068 for (int i = 0; i < 2; i++) {
1069 Slvs_Param aParam = getParameter(aEndPoint.param[i]);
1071 updateParameter(aParam);
1074 std::list<Slvs_Entity>::iterator aPtIt = aPointsToFix.begin();
1075 for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
1076 fixPoint(*aPtIt, theCreated);
1079 // Fix radius of the arc
1080 bool isExists = false;
1081 std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
1082 for (; anIt != myConstraints.end() && !isExists; ++anIt)
1083 if (anIt->type == SLVS_C_DIAMETER && anIt->entityA == theArc.h)
1086 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theArc.group, SLVS_C_DIAMETER,
1087 theArc.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theArc.h, SLVS_E_UNKNOWN);
1088 aFixedR.h = addConstraint(aFixedR);
1089 theCreated.push_back(aFixedR.h);
1095 bool SketchSolver_Storage::isAxisParallel(const Slvs_hEntity& theEntity) const
1097 std::vector<Slvs_Constraint>::const_iterator anIter = myConstraints.begin();
1098 for (; anIter != myConstraints.end(); anIter++)
1099 if ((anIter->type == SLVS_C_HORIZONTAL || anIter->type == SLVS_C_VERTICAL) &&
1100 anIter->entityA == theEntity)
1105 bool SketchSolver_Storage::isUsedInEqual(
1106 const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const
1108 // Check the entity is used in Equal constraint
1109 std::vector<Slvs_Constraint>::const_iterator anEqIter = myConstraints.begin();
1110 for (; anEqIter != myConstraints.end(); anEqIter++)
1111 if ((anEqIter->type == SLVS_C_EQUAL_LENGTH_LINES ||
1112 anEqIter->type == SLVS_C_EQUAL_LINE_ARC_LEN ||
1113 anEqIter->type == SLVS_C_EQUAL_RADIUS) &&
1114 (anEqIter->entityA == theEntity || anEqIter->entityB == theEntity)) {
1115 theEqual = *anEqIter;
1121 bool SketchSolver_Storage::isNeedToResolve()
1123 if (myConstraints.empty())
1126 if (!myNeedToResolve) {
1127 // Verify the updated parameters are used in constraints
1128 std::set<Slvs_hEntity> aPoints;
1129 std::vector<Slvs_Entity>::const_iterator anEntIt = myEntities.begin();
1130 for (; anEntIt != myEntities.end(); ++anEntIt) {
1131 for (int i = 0; i < 4 && anEntIt->param[i] != SLVS_E_UNKNOWN; ++i)
1132 if (myUpdatedParameters.find(anEntIt->param[i]) != myUpdatedParameters.end()) {
1133 aPoints.insert(anEntIt->h);
1137 std::set<Slvs_hEntity> anEntities = aPoints;
1138 for (anEntIt = myEntities.begin(); anEntIt != myEntities.end(); ++anEntIt) {
1139 for (int i = 0; i < 4 && anEntIt->point[i] != SLVS_E_UNKNOWN; ++i)
1140 if (aPoints.find(anEntIt->point[i]) != aPoints.end()) {
1141 anEntities.insert(anEntIt->h);
1146 std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
1147 for (; aCIt != myConstraints.end() && !myNeedToResolve; ++aCIt) {
1148 Slvs_hEntity anAttrs[6] =
1149 {aCIt->ptA, aCIt->ptB, aCIt->entityA, aCIt->entityB, aCIt->entityC, aCIt->entityD};
1150 for (int i = 0; i < 6; i++)
1151 if (anAttrs[i] != SLVS_E_UNKNOWN && anEntities.find(anAttrs[i]) != anEntities.end()) {
1152 myNeedToResolve = true;
1158 myUpdatedParameters.clear();
1159 return myNeedToResolve;
1167 // ========================================================
1168 // ========= Auxiliary functions ===============
1169 // ========================================================
1171 template<typename T>
1172 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1174 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1175 int aVecSize = theEntities.size();
1176 if (theEntities.empty())
1178 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1180 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1182 if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1183 aResIndex = aVecSize;
1187 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1189 return fabs(theParam1.val - theParam2.val) > tolerance;
1192 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1195 for (; theEntity1.param[i] != 0 && i < 4; i++)
1196 if (theEntity1.param[i] != theEntity2.param[i])
1199 for (; theEntity1.point[i] != 0 && i < 4; i++)
1200 if (theEntity1.point[i] != theEntity2.point[i])
1205 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1207 return theConstraint1.ptA != theConstraint2.ptA ||
1208 theConstraint1.ptB != theConstraint2.ptB ||
1209 theConstraint1.entityA != theConstraint2.entityA ||
1210 theConstraint1.entityB != theConstraint2.entityB ||
1211 theConstraint1.entityC != theConstraint2.entityC ||
1212 theConstraint1.entityD != theConstraint2.entityD ||
1213 fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;