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 // Check whether one of coincident points is out-of-group
375 std::set<Slvs_hEntity>::const_iterator aCoincIt = aCoincident.begin();
376 for (; aCoincIt != aCoincident.end(); ++aCoincIt) {
377 Slvs_Entity aPoint = getEntity(*aCoincIt);
378 if (aPoint.group == SLVS_G_OUTOFGROUP)
382 // Search the Rigid constraint
383 theFixed = SLVS_C_UNKNOWN;
384 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
385 for (; aConstrIter != myConstraints.end(); aConstrIter++)
386 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
387 aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
388 theFixed = aConstrIter->h;
389 if (aConstrIter->ptA == thePointID)
392 if (theFixed != SLVS_C_UNKNOWN)
396 // Try to find the fixed entity which uses such point or its coincidence
397 std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
398 for (; anEntIter != myEntities.end(); anEntIter++) {
399 for (int i = 0; i < 4; i++) {
400 Slvs_hEntity aPt = anEntIter->point[i];
401 if (aPt != SLVS_E_UNKNOWN &&
402 (aPt == thePointID || aCoincident.find(aPt) != aCoincident.end())) {
403 if (isEntityFixed(anEntIter->h, true))
409 return SLVS_E_UNKNOWN;
412 bool SketchSolver_Storage::isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate) const
414 int aPos = Search(theEntityID, myEntities);
415 if (aPos < 0 || aPos >= (int)myEntities.size())
418 // Firstly, find how many points are under Rigid constraint
420 for (int i = 0; i < 4; i++) {
421 Slvs_hEntity aPoint = myEntities[aPos].point[i];
422 if (aPoint == SLVS_E_UNKNOWN)
425 std::set<Slvs_hEntity> aCoincident;
426 aCoincident.insert(aPoint);
427 std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
428 for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
429 if (aCPIter->find(aPoint) != aCPIter->end()) {
430 aCoincident = *aCPIter;
434 // Search the Rigid constraint
435 std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
436 for (; aConstrIter != myConstraints.end(); aConstrIter++)
437 if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
438 aCoincident.find(aConstrIter->ptA) != aCoincident.end())
442 std::list<Slvs_Constraint> aList;
443 std::list<Slvs_Constraint>::iterator anIt;
444 Slvs_hConstraint aTempID; // used in isPointFixed() method
446 if (myEntities[aPos].type == SLVS_E_LINE_SEGMENT) {
449 else if (aNbFixed == 0 || !theAccurate)
451 // Additional check (the line may be fixed if it is used by different constraints):
452 // 1. The line is used in Equal constraint, another entity is fixed and there is a fixed point on line
453 aList = getConstraintsByType(SLVS_C_PT_ON_LINE);
454 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
455 if (anIt->entityA == theEntityID && isPointFixed(anIt->ptA, aTempID))
457 if (anIt != aList.end()) {
458 aList = getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
459 aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
460 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
461 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
462 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
463 if (isEntityFixed(anOther, false))
467 // 2. The line is used in Parallel/Perpendicular/Vertical/Horizontal and Length constraints
468 aList = getConstraintsByType(SLVS_C_PARALLEL);
469 aList.splice(aList.end(), getConstraintsByType(SLVS_C_PERPENDICULAR));
470 aList.splice(aList.end(), getConstraintsByType(SLVS_C_VERTICAL));
471 aList.splice(aList.end(), getConstraintsByType(SLVS_C_HORIZONTAL));
472 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
473 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
474 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
475 if (isEntityFixed(anOther, false))
478 if (anIt != aList.end()) {
479 aList = getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
480 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
481 if ((anIt->ptA == myEntities[aPos].point[0] && anIt->ptB == myEntities[aPos].point[1]) ||
482 (anIt->ptA == myEntities[aPos].point[1] && anIt->ptB == myEntities[aPos].point[0]))
485 // 3. Another verifiers ...
486 } else if (myEntities[aPos].type == SLVS_E_CIRCLE) {
489 // Search for Diameter constraint
490 aList = getConstraintsByType(SLVS_C_DIAMETER);
491 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
492 if (anIt->entityA == theEntityID)
496 // Additional check (the circle may be fixed if it is used by different constraints):
497 // 1. The circle is used in Equal constraint and another entity is fixed
498 aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
499 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
500 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
501 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
502 if (isEntityFixed(anOther, false))
505 // 2. Another verifiers ...
506 } else if (myEntities[aPos].type == SLVS_E_ARC_OF_CIRCLE) {
509 else if (aNbFixed <= 1)
511 // Search for Diameter constraint
512 aList = getConstraintsByType(SLVS_C_DIAMETER);
513 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
514 if (anIt->entityA == theEntityID)
518 // Additional check (the arc may be fixed if it is used by different constraints):
519 // 1. The arc is used in Equal constraint and another entity is fixed
520 aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
521 aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
522 for (anIt = aList.begin(); anIt != aList.end(); anIt++)
523 if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
524 Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
525 if (isEntityFixed(anOther, false))
528 // 2. Another verifiers ...
534 Slvs_hConstraint SketchSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
536 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
537 // Constraint is already used, rewrite it
538 return updateConstraint(theConstraint);
541 Slvs_Constraint aConstraint = theConstraint;
543 // Find a constraint with same type uses same arguments to show user overconstraint situation
544 std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
545 for (; aCIt != myConstraints.end(); aCIt++) {
546 if (aConstraint.type != aCIt->type)
548 if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
549 aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
550 aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD)
551 myDuplicatedConstraint = true;
554 if (aConstraint.h > myConstrMaxID)
555 myConstrMaxID = aConstraint.h;
557 aConstraint.h = ++myConstrMaxID;
558 myConstraints.push_back(aConstraint);
559 myNeedToResolve = true;
560 if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
561 addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
562 return aConstraint.h;
565 Slvs_hConstraint SketchSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
567 if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
568 // Constraint already used, rewrite it
569 int aPos = Search(theConstraint.h, myConstraints);
570 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
571 myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
572 myConstraints[aPos] = theConstraint;
573 if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
574 addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
575 return theConstraint.h;
579 // Constraint is not found, add new one
580 Slvs_Constraint aConstraint = theConstraint;
582 return addConstraint(aConstraint);
585 bool SketchSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
588 int aPos = Search(theConstraintID, myConstraints);
589 if (aPos >= 0 && aPos < (int)myConstraints.size()) {
590 Slvs_Constraint aConstraint = myConstraints[aPos];
591 myConstraints.erase(myConstraints.begin() + aPos);
592 myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
593 myNeedToResolve = true;
594 myRemovedConstraints.insert(theConstraintID);
595 if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
596 removeCoincidence(aConstraint);
598 // Remove all entities
599 Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
600 aConstraint.entityA, aConstraint.entityB,
601 aConstraint.entityC, aConstraint.entityD};
602 for (int i = 0; i < 6; i++)
603 if (anEntities[i] != SLVS_E_UNKNOWN)
604 aResult = removeEntity(anEntities[i]) && aResult;
605 // remove temporary fixed point, if available
606 if (myFixed == theConstraintID)
607 myFixed = SLVS_E_UNKNOWN;
608 if (myDuplicatedConstraint) {
609 // Check the duplicated constraints are still available
610 myDuplicatedConstraint = false;
611 std::vector<Slvs_Constraint>::const_iterator anIt1 = myConstraints.begin();
612 std::vector<Slvs_Constraint>::const_iterator anIt2 = myConstraints.begin();
613 for (; anIt1 != myConstraints.end() && !myDuplicatedConstraint; anIt1++)
614 for (anIt2 = anIt1+1; anIt2 != myConstraints.end() && !myDuplicatedConstraint; anIt2++) {
615 if (anIt1->type != anIt2->type)
617 if (anIt1->ptA == anIt2->ptA && anIt1->ptB == anIt2->ptB &&
618 anIt1->entityA == anIt2->entityA && anIt1->entityB == anIt2->entityB &&
619 anIt1->entityC == anIt2->entityC && anIt1->entityD == anIt2->entityD)
620 myDuplicatedConstraint = true;
627 const Slvs_Constraint& SketchSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
629 int aPos = Search(theConstraintID, myConstraints);
630 if (aPos >= 0 && aPos < (int)myConstraints.size())
631 return myConstraints[aPos];
633 // Constraint is not found, return empty object
634 static Slvs_Constraint aDummy;
639 std::list<Slvs_Constraint> SketchSolver_Storage::getConstraintsByType(int theConstraintType) const
641 std::list<Slvs_Constraint> aResult;
642 std::vector<Slvs_Constraint>::const_iterator aCIter = myConstraints.begin();
643 for (; aCIter != myConstraints.end(); aCIter++)
644 if (aCIter->type == theConstraintType)
645 aResult.push_back(*aCIter);
650 void SketchSolver_Storage::addConstraintWhereDragged(const Slvs_hConstraint& theConstraintID)
652 if (myFixed != SLVS_E_UNKNOWN)
653 return; // the point is already fixed
654 int aPos = Search(theConstraintID, myConstraints);
655 if (aPos >= 0 && aPos < (int)myConstraints.size())
656 myFixed = theConstraintID;
659 void SketchSolver_Storage::addTemporaryConstraint(const Slvs_hConstraint& theConstraintID)
661 myTemporaryConstraints.insert(theConstraintID);
664 void SketchSolver_Storage::removeTemporaryConstraints()
666 myTemporaryConstraints.clear();
669 int SketchSolver_Storage::deleteTemporaryConstraint()
671 if (myTemporaryConstraints.empty())
673 // Search the point-on-line or a non-rigid constraint
674 std::set<Slvs_hConstraint>::iterator aCIt = myTemporaryConstraints.begin();
675 for (; aCIt != myTemporaryConstraints.end(); aCIt++) {
676 int aPos = Search(*aCIt, myConstraints);
677 if (aPos >= (int)myConstraints.size() || myConstraints[aPos].type != SLVS_C_WHERE_DRAGGED)
679 std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
680 for (; anIt != myConstraints.end(); anIt++)
681 if (anIt->type == SLVS_C_PT_ON_LINE && anIt->ptA == myConstraints[aPos].ptA)
683 if (anIt != myConstraints.end())
686 if (aCIt == myTemporaryConstraints.end())
687 aCIt = myTemporaryConstraints.begin();
688 bool aNewFixed = (*aCIt == myFixed);
689 removeConstraint(*aCIt);
690 myTemporaryConstraints.erase(aCIt);
692 for (aCIt = myTemporaryConstraints.begin(); aCIt != myTemporaryConstraints.end(); aCIt++) {
693 int aPos = Search(*aCIt, myConstraints);
694 if (myConstraints[aPos].type == SLVS_C_WHERE_DRAGGED) {
700 return (int)myTemporaryConstraints.size();
703 bool SketchSolver_Storage::isTemporary(const Slvs_hConstraint& theConstraintID) const
705 return myTemporaryConstraints.find(theConstraintID) != myTemporaryConstraints.end();
709 void SketchSolver_Storage::getRemoved(
710 std::set<Slvs_hParam>& theParameters,
711 std::set<Slvs_hEntity>& theEntities,
712 std::set<Slvs_hConstraint>& theConstraints)
714 theParameters = myRemovedParameters;
715 theEntities = myRemovedEntities;
716 theConstraints = myRemovedConstraints;
718 myRemovedParameters.clear();
719 myRemovedEntities.clear();
720 myRemovedConstraints.clear();
723 void SketchSolver_Storage::initializeSolver(SketchSolver_Solver& theSolver)
725 theSolver.setParameters(myParameters.data(), (int)myParameters.size());
726 theSolver.setEntities(myEntities.data(), (int)myEntities.size());
728 // Copy constraints excluding the fixed one
729 std::vector<Slvs_Constraint> aConstraints = myConstraints;
730 if (myFixed != SLVS_E_UNKNOWN) {
731 Slvs_hEntity aFixedPoint = SLVS_E_UNKNOWN;
732 std::vector<Slvs_Constraint>::iterator anIt = aConstraints.begin();
733 for (; anIt != aConstraints.end(); anIt++)
734 if (anIt->h == myFixed) {
735 aFixedPoint = anIt->ptA;
736 aConstraints.erase(anIt);
739 // set dragged parameters
740 int aPos = Search(aFixedPoint, myEntities);
741 theSolver.setDraggedParameters(myEntities[aPos].param);
743 theSolver.setConstraints(aConstraints.data(), (int)aConstraints.size());
746 void SketchSolver_Storage::addCoincidentPoints(
747 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2)
749 std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
750 std::vector< std::set<Slvs_hEntity> >::iterator aFoundIter = myCoincidentPoints.end(); // already found coincidence
751 bool isFound = false;
752 for (; aCIter != myCoincidentPoints.end(); aCIter++) {
753 bool isFirstFound = aCIter->find(thePoint1) != aCIter->end();
754 bool isSecondFound = aCIter->find(thePoint2) != aCIter->end();
755 isFound = isFound || isFirstFound || isSecondFound;
756 if (isFirstFound && isSecondFound)
757 break; // already coincident
758 else if (isFirstFound || isSecondFound) {
759 if (aFoundIter != myCoincidentPoints.end()) {
761 aFoundIter->insert(aCIter->begin(), aCIter->end());
762 myCoincidentPoints.erase(aCIter);
766 aCIter->insert(thePoint1);
767 aCIter->insert(thePoint2);
770 // coincident points not found
772 std::set<Slvs_hEntity> aNewSet;
773 aNewSet.insert(thePoint1);
774 aNewSet.insert(thePoint2);
775 myCoincidentPoints.push_back(aNewSet);
779 void SketchSolver_Storage::removeCoincidentPoint(const Slvs_hEntity& thePoint)
781 std::vector< std::set<Slvs_hEntity> >::iterator aCIter = myCoincidentPoints.begin();
782 for (; aCIter != myCoincidentPoints.end(); aCIter++)
783 if (aCIter->find(thePoint) != aCIter->end()) {
784 aCIter->erase(thePoint);
785 if (aCIter->size() <= 1)
786 myCoincidentPoints.erase(aCIter);
791 void SketchSolver_Storage::removeCoincidence(const Slvs_Constraint& theCoincidence)
793 // Find set of coincident points
794 std::vector< std::set<Slvs_hEntity> >::iterator aCIt = myCoincidentPoints.begin();
795 for (; aCIt != myCoincidentPoints.end(); ++aCIt)
796 if (aCIt->find(theCoincidence.ptA) != aCIt->end() ||
797 aCIt->find(theCoincidence.ptB) != aCIt->end())
799 if (aCIt == myCoincidentPoints.end())
802 // Leave only the points which are still coincident
803 std::set<Slvs_hEntity> aRemainCoincidence;
804 std::vector<Slvs_Constraint>::const_iterator aConstrIt = myConstraints.begin();
805 for (; aConstrIt != myConstraints.end(); ++aConstrIt) {
806 if (aConstrIt->type != SLVS_C_POINTS_COINCIDENT)
808 if (aCIt->find(aConstrIt->ptA) != aCIt->end() ||
809 aCIt->find(aConstrIt->ptB) != aCIt->end()) {
810 aRemainCoincidence.insert(aConstrIt->ptA);
811 aRemainCoincidence.insert(aConstrIt->ptB);
814 if (aRemainCoincidence.size() <= 1)
815 myCoincidentPoints.erase(aCIt);
817 aCIt->swap(aRemainCoincidence);
820 bool SketchSolver_Storage::isCoincident(
821 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
823 std::vector< std::set<Slvs_hEntity> >::const_iterator aCIter = myCoincidentPoints.begin();
824 for (; aCIter != myCoincidentPoints.end(); aCIter++)
825 if (aCIter->find(thePoint1) != aCIter->end() && aCIter->find(thePoint2) != aCIter->end())
830 bool SketchSolver_Storage::isEqual(
831 const Slvs_hEntity& thePoint1, const Slvs_hEntity& thePoint2) const
833 if (isCoincident(thePoint1, thePoint2))
836 // Precise checking of coincidence: verify that points have equal coordinates
837 int aEnt1Pos = Search(thePoint1, myEntities);
838 int aEnt2Pos = Search(thePoint2, myEntities);
839 if (aEnt1Pos >= 0 && aEnt1Pos < (int)myEntities.size() &&
840 aEnt2Pos >= 0 && aEnt2Pos < (int)myEntities.size()) {
843 for (int i = 0; i < 2; i++) {
844 aParamPos = Search(myEntities[aEnt1Pos].param[i], myParameters);
845 aDist[i] = myParameters[aParamPos].val;
846 aParamPos = Search(myEntities[aEnt2Pos].param[i], myParameters);
847 aDist[i] -= myParameters[aParamPos].val;
849 if (aDist[0] * aDist[0] + aDist[1] * aDist[1] < tolerance * tolerance)
856 std::vector<Slvs_hConstraint> SketchSolver_Storage::fixEntity(const Slvs_hEntity& theEntity)
858 std::vector<Slvs_hConstraint> aNewConstraints;
860 int aPos = Search(theEntity, myEntities);
861 if (aPos >= 0 && aPos < (int)myEntities.size()) {
862 switch (myEntities[aPos].type) {
863 case SLVS_E_POINT_IN_2D:
864 case SLVS_E_POINT_IN_3D:
865 fixPoint(myEntities[aPos], aNewConstraints);
867 case SLVS_E_LINE_SEGMENT:
868 fixLine(myEntities[aPos], aNewConstraints);
871 fixCircle(myEntities[aPos], aNewConstraints);
873 case SLVS_E_ARC_OF_CIRCLE:
874 fixArc(myEntities[aPos], aNewConstraints);
881 return aNewConstraints;
884 void SketchSolver_Storage::fixPoint(const Slvs_Entity& thePoint,
885 std::vector<Slvs_hConstraint>& theCreated)
887 Slvs_Constraint aConstraint;
888 Slvs_hConstraint aConstrID = SLVS_E_UNKNOWN;
889 bool isFixed = isPointFixed(thePoint.h, aConstrID, true);
890 bool isForceUpdate = (isFixed && isTemporary(aConstrID));
891 if (!isForceUpdate) { // create new constraint
893 aConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, thePoint.group, SLVS_C_WHERE_DRAGGED, thePoint.wrkpl,
894 0.0, thePoint.h, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
895 aConstraint.h = addConstraint(aConstraint);
896 theCreated.push_back(aConstraint.h);
897 } else { // update already existent constraint
898 if (!isFixed || aConstrID == SLVS_E_UNKNOWN)
900 int aPos = Search(aConstrID, myConstraints);
901 if (aPos >= 0 && aPos < (int)myConstraints.size())
902 myConstraints[aPos].ptA = thePoint.h;
906 void SketchSolver_Storage::fixLine(const Slvs_Entity& theLine,
907 std::vector<Slvs_hConstraint>& theCreated)
909 Slvs_Entity aPoint[2] = {
910 getEntity(theLine.point[0]),
911 getEntity(theLine.point[1])
914 Slvs_Constraint anEqual;
915 if (isAxisParallel(theLine.h)) {
916 // Fix one point and a line length
917 Slvs_hConstraint aFixed;
918 if (!isPointFixed(theLine.point[0], aFixed, true) &&
919 !isPointFixed(theLine.point[1], aFixed, true))
920 fixPoint(aPoint[0], theCreated);
921 if (!isUsedInEqual(theLine.h, anEqual)) {
922 // Check the distance is not set yet
923 std::vector<Slvs_Constraint>::const_iterator aDistIt = myConstraints.begin();
924 for (; aDistIt != myConstraints.end(); ++aDistIt)
925 if ((aDistIt->type == SLVS_C_PT_PT_DISTANCE) &&
926 ((aDistIt->ptA == theLine.point[0] && aDistIt->ptB == theLine.point[1]) ||
927 (aDistIt->ptA == theLine.point[1] && aDistIt->ptB == theLine.point[0])))
929 // Calculate distance between points on the line
931 for (int i = 0; i < 2; i++)
932 for (int j = 0; j < 2; j++) {
933 Slvs_Param aParam = getParameter(aPoint[i].param[j]);
934 aCoords[2*i+j] = aParam.val;
937 double aLength = sqrt((aCoords[2] - aCoords[0]) * (aCoords[2] - aCoords[0]) +
938 (aCoords[3] - aCoords[1]) * (aCoords[3] - aCoords[1]));
940 Slvs_Constraint aDistance = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group,
941 SLVS_C_PT_PT_DISTANCE, theLine.wrkpl, aLength,
942 theLine.point[0], theLine.point[1], SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
943 aDistance.h = addConstraint(aDistance);
944 theCreated.push_back(aDistance.h);
948 else if (isUsedInEqual(theLine.h, anEqual)) {
949 // Check another entity of Equal is already fixed
950 Slvs_hEntity anOtherEntID = anEqual.entityA == theLine.h ? anEqual.entityB : anEqual.entityA;
951 if (isEntityFixed(anOtherEntID, true)) {
952 // Fix start point of the line (if end point is not fixed yet) ...
953 Slvs_hConstraint anEndFixedID = SLVS_E_UNKNOWN;
954 bool isFixed = isPointFixed(theLine.point[1], anEndFixedID, true);
955 if (isFixed == SLVS_E_UNKNOWN)
956 fixPoint(aPoint[0], theCreated);
957 // ... and create fixed point lying on this line
958 Slvs_hEntity aPointToCopy = anEndFixedID == SLVS_E_UNKNOWN ? theLine.point[1] : theLine.point[0];
959 // Firstly, search already fixed point on line
960 bool isPonLineFixed = false;
961 Slvs_hEntity aFixedPoint;
962 std::vector<Slvs_Constraint>::const_iterator aPLIter = myConstraints.begin();
963 for (; aPLIter != myConstraints.end() && !isPonLineFixed; ++aPLIter)
964 if (aPLIter->type == SLVS_C_PT_ON_LINE && aPLIter->entityA == theLine.h) {
965 isPonLineFixed = isPointFixed(aPLIter->ptA, anEndFixedID);
966 aFixedPoint = aPLIter->ptA;
969 if (isPonLineFixed) { // update existent constraint
970 copyEntity(aPointToCopy, aFixedPoint);
971 } else { // create new constraint
972 Slvs_hEntity aCopied = copyEntity(aPointToCopy);
973 Slvs_Constraint aPonLine = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theLine.group, SLVS_C_PT_ON_LINE,
974 theLine.wrkpl, 0.0, aCopied, SLVS_E_UNKNOWN, theLine.h, SLVS_E_UNKNOWN);
975 aPonLine.h = addConstraint(aPonLine);
976 theCreated.push_back(aPonLine.h);
977 fixPoint(getEntity(aCopied), theCreated);
984 for (int i = 0; i < 2; i++)
985 fixPoint(aPoint[i], theCreated);
988 void SketchSolver_Storage::fixCircle(const Slvs_Entity& theCircle,
989 std::vector<Slvs_hConstraint>& theCreated)
991 bool isFixRadius = true;
992 // Verify the arc is under Equal constraint
993 Slvs_Constraint anEqual;
994 if (isUsedInEqual(theCircle.h, anEqual)) {
995 // Check another entity of Equal is already fixed
996 Slvs_hEntity anOtherEntID = anEqual.entityA == theCircle.h ? anEqual.entityB : anEqual.entityA;
997 if (isEntityFixed(anOtherEntID, true))
1001 fixPoint(getEntity(theCircle.point[0]), theCreated);
1004 // Search the radius is already fixed
1005 std::vector<Slvs_Constraint>::const_iterator aDiamIter = myConstraints.begin();
1006 for (; aDiamIter != myConstraints.end(); ++aDiamIter)
1007 if (aDiamIter->type == SLVS_C_DIAMETER && aDiamIter->entityA == theCircle.h)
1010 // Fix radius of a circle
1011 const Slvs_Entity& aRadEnt = getEntity(theCircle.distance);
1012 double aRadius = getParameter(aRadEnt.param[0]).val;
1013 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theCircle.group, SLVS_C_DIAMETER,
1014 theCircle.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theCircle.h, SLVS_E_UNKNOWN);
1015 aFixedR.h = addConstraint(aFixedR);
1016 theCreated.push_back(aFixedR.h);
1020 void SketchSolver_Storage::fixArc(const Slvs_Entity& theArc,
1021 std::vector<Slvs_hConstraint>& theCreated)
1023 Slvs_Entity aPoint[3] = {
1024 getEntity(theArc.point[0]),
1025 getEntity(theArc.point[1]),
1026 getEntity(theArc.point[2])
1029 bool isFixRadius = true;
1030 std::list<Slvs_Entity> aPointsToFix;
1031 aPointsToFix.push_back(aPoint[1]);
1032 aPointsToFix.push_back(aPoint[2]);
1034 // Verify the arc is under Equal constraint
1035 Slvs_Constraint anEqual;
1036 if (isUsedInEqual(theArc.h, anEqual)) {
1037 // Check another entity of Equal is already fixed
1038 Slvs_hEntity anOtherEntID = anEqual.entityA == theArc.h ? anEqual.entityB : anEqual.entityA;
1039 if (isEntityFixed(anOtherEntID, true)) {
1040 isFixRadius = false;
1041 Slvs_Entity anOtherEntity = getEntity(anOtherEntID);
1042 if (anOtherEntity.type == SLVS_E_LINE_SEGMENT) {
1043 aPointsToFix.pop_back();
1044 aPointsToFix.push_back(aPoint[0]);
1049 Slvs_hConstraint aConstrID;
1050 int aNbPointsToFix = 2; // number of fixed points for the arc
1051 if (isPointFixed(theArc.point[0], aConstrID, true))
1054 double anArcPoints[3][2];
1055 for (int i = 0; i < 3; i++) {
1056 const Slvs_Entity& aPointOnArc = getEntity(theArc.point[i]);
1057 for (int j = 0; j < 2; j++)
1058 anArcPoints[i][j] = getParameter(aPointOnArc.param[j]).val;
1061 // Radius of the arc
1062 std::shared_ptr<GeomAPI_Pnt2d> aCenter(new GeomAPI_Pnt2d(anArcPoints[0][0], anArcPoints[0][1]));
1063 std::shared_ptr<GeomAPI_Pnt2d> aStart(new GeomAPI_Pnt2d(anArcPoints[1][0], anArcPoints[1][1]));
1064 double aRadius = aCenter->distance(aStart);
1066 // Update end point of the arc to be on a curve
1067 std::shared_ptr<GeomAPI_Pnt2d> anEnd(new GeomAPI_Pnt2d(anArcPoints[2][0], anArcPoints[2][1]));
1068 double aDistance = anEnd->distance(aCenter);
1069 std::shared_ptr<GeomAPI_XY> aDir = anEnd->xy()->decreased(aCenter->xy());
1070 if (aDistance < tolerance)
1071 aDir = aStart->xy()->decreased(aCenter->xy())->multiplied(-1.0);
1073 aDir = aDir->multiplied(aRadius / aDistance);
1074 double xy[2] = {aCenter->x() + aDir->x(), aCenter->y() + aDir->y()};
1075 const Slvs_Entity& aEndPoint = getEntity(theArc.point[2]);
1076 for (int i = 0; i < 2; i++) {
1077 Slvs_Param aParam = getParameter(aEndPoint.param[i]);
1079 updateParameter(aParam);
1082 std::list<Slvs_Entity>::iterator aPtIt = aPointsToFix.begin();
1083 for (; aNbPointsToFix > 0; aPtIt++, aNbPointsToFix--)
1084 fixPoint(*aPtIt, theCreated);
1087 // Fix radius of the arc
1088 bool isExists = false;
1089 std::vector<Slvs_Constraint>::iterator anIt = myConstraints.begin();
1090 for (; anIt != myConstraints.end() && !isExists; ++anIt)
1091 if (anIt->type == SLVS_C_DIAMETER && anIt->entityA == theArc.h)
1094 Slvs_Constraint aFixedR = Slvs_MakeConstraint(SLVS_E_UNKNOWN, theArc.group, SLVS_C_DIAMETER,
1095 theArc.wrkpl, aRadius * 2.0, SLVS_E_UNKNOWN, SLVS_E_UNKNOWN, theArc.h, SLVS_E_UNKNOWN);
1096 aFixedR.h = addConstraint(aFixedR);
1097 theCreated.push_back(aFixedR.h);
1103 bool SketchSolver_Storage::isAxisParallel(const Slvs_hEntity& theEntity) const
1105 std::vector<Slvs_Constraint>::const_iterator anIter = myConstraints.begin();
1106 for (; anIter != myConstraints.end(); anIter++)
1107 if ((anIter->type == SLVS_C_HORIZONTAL || anIter->type == SLVS_C_VERTICAL) &&
1108 anIter->entityA == theEntity)
1113 bool SketchSolver_Storage::isUsedInEqual(
1114 const Slvs_hEntity& theEntity, Slvs_Constraint& theEqual) const
1116 // Check the entity is used in Equal constraint
1117 std::vector<Slvs_Constraint>::const_iterator anEqIter = myConstraints.begin();
1118 for (; anEqIter != myConstraints.end(); anEqIter++)
1119 if ((anEqIter->type == SLVS_C_EQUAL_LENGTH_LINES ||
1120 anEqIter->type == SLVS_C_EQUAL_LINE_ARC_LEN ||
1121 anEqIter->type == SLVS_C_EQUAL_RADIUS) &&
1122 (anEqIter->entityA == theEntity || anEqIter->entityB == theEntity)) {
1123 theEqual = *anEqIter;
1129 bool SketchSolver_Storage::isNeedToResolve()
1131 if (myConstraints.empty())
1134 if (!myNeedToResolve) {
1135 // Verify the updated parameters are used in constraints
1136 std::set<Slvs_hEntity> aPoints;
1137 std::vector<Slvs_Entity>::const_iterator anEntIt = myEntities.begin();
1138 for (; anEntIt != myEntities.end(); ++anEntIt) {
1139 for (int i = 0; i < 4 && anEntIt->param[i] != SLVS_E_UNKNOWN; ++i)
1140 if (myUpdatedParameters.find(anEntIt->param[i]) != myUpdatedParameters.end()) {
1141 aPoints.insert(anEntIt->h);
1145 std::set<Slvs_hEntity> anEntities = aPoints;
1146 for (anEntIt = myEntities.begin(); anEntIt != myEntities.end(); ++anEntIt) {
1147 for (int i = 0; i < 4 && anEntIt->point[i] != SLVS_E_UNKNOWN; ++i)
1148 if (aPoints.find(anEntIt->point[i]) != aPoints.end()) {
1149 anEntities.insert(anEntIt->h);
1154 std::vector<Slvs_Constraint>::const_iterator aCIt = myConstraints.begin();
1155 for (; aCIt != myConstraints.end() && !myNeedToResolve; ++aCIt) {
1156 Slvs_hEntity anAttrs[6] =
1157 {aCIt->ptA, aCIt->ptB, aCIt->entityA, aCIt->entityB, aCIt->entityC, aCIt->entityD};
1158 for (int i = 0; i < 6; i++)
1159 if (anAttrs[i] != SLVS_E_UNKNOWN && anEntities.find(anAttrs[i]) != anEntities.end()) {
1160 myNeedToResolve = true;
1166 myUpdatedParameters.clear();
1167 return myNeedToResolve;
1175 // ========================================================
1176 // ========= Auxiliary functions ===============
1177 // ========================================================
1179 template<typename T>
1180 int Search(const uint32_t& theEntityID, const std::vector<T>& theEntities)
1182 int aResIndex = theEntityID <= theEntities.size() ? theEntityID - 1 : 0;
1183 int aVecSize = theEntities.size();
1184 if (theEntities.empty())
1186 while (aResIndex >= 0 && theEntities[aResIndex].h > theEntityID)
1188 while (aResIndex < aVecSize && aResIndex >= 0 && theEntities[aResIndex].h < theEntityID)
1190 if (aResIndex == -1 || (aResIndex < aVecSize && theEntities[aResIndex].h != theEntityID))
1191 aResIndex = aVecSize;
1195 bool IsNotEqual(const Slvs_Param& theParam1, const Slvs_Param& theParam2)
1197 return fabs(theParam1.val - theParam2.val) > tolerance;
1200 bool IsNotEqual(const Slvs_Entity& theEntity1, const Slvs_Entity& theEntity2)
1203 for (; theEntity1.param[i] != 0 && i < 4; i++)
1204 if (theEntity1.param[i] != theEntity2.param[i])
1207 for (; theEntity1.point[i] != 0 && i < 4; i++)
1208 if (theEntity1.point[i] != theEntity2.point[i])
1213 bool IsNotEqual(const Slvs_Constraint& theConstraint1, const Slvs_Constraint& theConstraint2)
1215 return theConstraint1.ptA != theConstraint2.ptA ||
1216 theConstraint1.ptB != theConstraint2.ptB ||
1217 theConstraint1.entityA != theConstraint2.entityA ||
1218 theConstraint1.entityB != theConstraint2.entityB ||
1219 theConstraint1.entityC != theConstraint2.entityC ||
1220 theConstraint1.entityD != theConstraint2.entityD ||
1221 fabs(theConstraint1.valA - theConstraint2.valA) > tolerance;