- // Search the Rigid constraint
- std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
- for (; aConstrIter != myConstraints.end(); aConstrIter++)
- if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
- aCoincident.find(aConstrIter->ptA) != aCoincident.end()) {
- theFixed = aConstrIter->h;
- return true;
- }
-
- if (theAccurate) {
- // Try to find the fixed entity which uses such point or its coincidence
- std::vector<Slvs_Entity>::const_iterator anEntIter = myEntities.begin();
- for (; anEntIter != myEntities.end(); anEntIter++) {
- for (int i = 0; i < 4; i++) {
- Slvs_hEntity aPt = anEntIter->point[i];
- if (aPt != SLVS_E_UNKNOWN &&
- (aPt == thePointID || aCoincident.find(aPt) != aCoincident.end())) {
- if (isEntityFixed(anEntIter->h, true))
- return true;
- }
- }
- }
- }
- return SLVS_E_UNKNOWN;
-}
-
-bool SketchSolver_Storage::isEntityFixed(const Slvs_hEntity& theEntityID, bool theAccurate) const
-{
- int aPos = Search(theEntityID, myEntities);
- if (aPos < 0 || aPos >= (int)myEntities.size())
- return false;
-
- // Firstly, find how many points are under Rigid constraint
- int aNbFixed = 0;
- for (int i = 0; i < 4; i++) {
- Slvs_hEntity aPoint = myEntities[aPos].point[i];
- if (aPoint == SLVS_E_UNKNOWN)
- continue;
-
- std::set<Slvs_hEntity> aCoincident;
- aCoincident.insert(aPoint);
- std::vector< std::set<Slvs_hEntity> >::const_iterator aCPIter = myCoincidentPoints.begin();
- for (; aCPIter != myCoincidentPoints.end(); aCPIter++)
- if (aCPIter->find(aPoint) != aCPIter->end()) {
- aCoincident = *aCPIter;
- break;
- }
-
- // Search the Rigid constraint
- std::vector<Slvs_Constraint>::const_iterator aConstrIter = myConstraints.begin();
- for (; aConstrIter != myConstraints.end(); aConstrIter++)
- if (aConstrIter->type == SLVS_C_WHERE_DRAGGED &&
- aCoincident.find(aConstrIter->ptA) != aCoincident.end())
- aNbFixed++;
- }
-
- std::list<Slvs_Constraint> aList;
- std::list<Slvs_Constraint>::iterator anIt;
- Slvs_hConstraint aTempID; // used in isPointFixed() method
-
- if (myEntities[aPos].type == SLVS_E_LINE_SEGMENT) {
- if (aNbFixed == 2)
- return true;
- else if (aNbFixed == 0 || !theAccurate)
- return false;
- // Additional check (the line may be fixed if it is used by different constraints):
- // 1. The line is used in Equal constraint, another entity is fixed and there is a fixed point on line
- aList = getConstraintsByType(SLVS_C_PT_ON_LINE);
- for (anIt = aList.begin(); anIt != aList.end(); anIt++)
- if (anIt->entityA == theEntityID && isPointFixed(anIt->ptA, aTempID))
- break;
- if (anIt != aList.end()) {
- aList = getConstraintsByType(SLVS_C_EQUAL_LENGTH_LINES);
- aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
- for (anIt = aList.begin(); anIt != aList.end(); anIt++)
- if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
- Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
- if (isEntityFixed(anOther, false))
- return true;
- }
- }
- // 2. The line is used in Parallel/Perpendicular/Vertical/Horizontal and Length constraints
- aList = getConstraintsByType(SLVS_C_PARALLEL);
- aList.splice(aList.end(), getConstraintsByType(SLVS_C_PERPENDICULAR));
- aList.splice(aList.end(), getConstraintsByType(SLVS_C_VERTICAL));
- aList.splice(aList.end(), getConstraintsByType(SLVS_C_HORIZONTAL));
- for (anIt = aList.begin(); anIt != aList.end(); anIt++)
- if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
- Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
- if (isEntityFixed(anOther, false))
- break;
- }
- if (anIt != aList.end()) {
- aList = getConstraintsByType(SLVS_C_PT_PT_DISTANCE);
- for (anIt = aList.begin(); anIt != aList.end(); anIt++)
- if ((anIt->ptA == myEntities[aPos].point[0] && anIt->ptB == myEntities[aPos].point[1]) ||
- (anIt->ptA == myEntities[aPos].point[1] && anIt->ptB == myEntities[aPos].point[0]))
- return true;
- }
- // 3. Another verifiers ...
- } else if (myEntities[aPos].type == SLVS_E_CIRCLE) {
- if (aNbFixed == 0)
- return false;
- // Search for Diameter constraint
- aList = getConstraintsByType(SLVS_C_DIAMETER);
- for (anIt = aList.begin(); anIt != aList.end(); anIt++)
- if (anIt->entityA == theEntityID)
- return true;
- if (!theAccurate)
- return false;
- // Additional check (the circle may be fixed if it is used by different constraints):
- // 1. The circle is used in Equal constraint and another entity is fixed
- aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
- for (anIt = aList.begin(); anIt != aList.end(); anIt++)
- if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
- Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
- if (isEntityFixed(anOther, false))
- return true;
- }
- // 2. Another verifiers ...
- } else if (myEntities[aPos].type == SLVS_E_ARC_OF_CIRCLE) {
- if (aNbFixed > 2)
- return true;
- else if (aNbFixed <= 1)
- return false;
- // Search for Diameter constraint
- aList = getConstraintsByType(SLVS_C_DIAMETER);
- for (anIt = aList.begin(); anIt != aList.end(); anIt++)
- if (anIt->entityA == theEntityID)
- return true;
- if (!theAccurate)
- return false;
- // Additional check (the arc may be fixed if it is used by different constraints):
- // 1. The arc is used in Equal constraint and another entity is fixed
- aList = getConstraintsByType(SLVS_C_EQUAL_RADIUS);
- aList.splice(aList.end(), getConstraintsByType(SLVS_C_EQUAL_LINE_ARC_LEN));
- for (anIt = aList.begin(); anIt != aList.end(); anIt++)
- if (anIt->entityA == theEntityID || anIt->entityB == theEntityID) {
- Slvs_hEntity anOther = anIt->entityA == theEntityID ? anIt->entityB : anIt->entityA;
- if (isEntityFixed(anOther, false))
- return true;
- }
- // 2. Another verifiers ...
- }
- return false;
-}
-
-
-Slvs_hConstraint SketchSolver_Storage::addConstraint(const Slvs_Constraint& theConstraint)
-{
- if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
- // Constraint is already used, rewrite it
- return updateConstraint(theConstraint);
- }
-
- Slvs_Constraint aConstraint = theConstraint;
-
- // Find a constraint with same type uses same arguments to show user overconstraint situation
- std::vector<Slvs_Constraint>::iterator aCIt = myConstraints.begin();
- for (; aCIt != myConstraints.end(); aCIt++) {
- if (aConstraint.type != aCIt->type)
- continue;
- if (aConstraint.ptA == aCIt->ptA && aConstraint.ptB == aCIt->ptB &&
- aConstraint.entityA == aCIt->entityA && aConstraint.entityB == aCIt->entityB &&
- aConstraint.entityC == aCIt->entityC && aConstraint.entityD == aCIt->entityD)
- myDuplicatedConstraint = true;
- }
-
- if (aConstraint.h > myConstrMaxID)
- myConstrMaxID = aConstraint.h;
- else
- aConstraint.h = ++myConstrMaxID;
- myConstraints.push_back(aConstraint);
- myNeedToResolve = true;
- if (aConstraint.type == SLVS_C_POINTS_COINCIDENT)
- addCoincidentPoints(aConstraint.ptA, aConstraint.ptB);
- return aConstraint.h;
-}
-
-Slvs_hConstraint SketchSolver_Storage::updateConstraint(const Slvs_Constraint& theConstraint)
-{
- if (theConstraint.h > 0 && theConstraint.h <= myConstrMaxID) {
- // Constraint already used, rewrite it
- int aPos = Search(theConstraint.h, myConstraints);
- if (aPos >= 0 && aPos < (int)myConstraints.size()) {
- myNeedToResolve = myNeedToResolve || IsNotEqual(myConstraints[aPos], theConstraint);
- myConstraints[aPos] = theConstraint;
- if (theConstraint.type == SLVS_C_POINTS_COINCIDENT)
- addCoincidentPoints(theConstraint.ptA, theConstraint.ptB);
- return theConstraint.h;
- }
- }
-
- // Constraint is not found, add new one
- Slvs_Constraint aConstraint = theConstraint;
- aConstraint.h = 0;
- return addConstraint(aConstraint);
-}
-
-bool SketchSolver_Storage::removeConstraint(const Slvs_hConstraint& theConstraintID)
-{
- bool aResult = true;
- int aPos = Search(theConstraintID, myConstraints);
- if (aPos >= 0 && aPos < (int)myConstraints.size()) {
- Slvs_Constraint aConstraint = myConstraints[aPos];
- myConstraints.erase(myConstraints.begin() + aPos);
- myConstrMaxID = myConstraints.empty() ? SLVS_E_UNKNOWN : myConstraints.back().h;
- myNeedToResolve = true;
- myRemovedConstraints.insert(theConstraintID);
- // Remove all entities
- Slvs_hEntity anEntities[6] = {aConstraint.ptA, aConstraint.ptB,
- aConstraint.entityA, aConstraint.entityB,
- aConstraint.entityC, aConstraint.entityD};
- for (int i = 0; i < 6; i++)
- if (anEntities[i] != SLVS_E_UNKNOWN)
- aResult = removeEntity(anEntities[i]) && aResult;
- // remove temporary fixed point, if available
- if (myFixed == theConstraintID)
- myFixed = SLVS_E_UNKNOWN;
- if (myDuplicatedConstraint) {
- // Check the duplicated constraints are still available
- myDuplicatedConstraint = false;
- std::vector<Slvs_Constraint>::const_iterator anIt1 = myConstraints.begin();
- std::vector<Slvs_Constraint>::const_iterator anIt2 = myConstraints.begin();
- for (; anIt1 != myConstraints.end() && !myDuplicatedConstraint; anIt1++)
- for (anIt2 = anIt1+1; anIt2 != myConstraints.end() && !myDuplicatedConstraint; anIt2++) {
- if (anIt1->type != anIt2->type)
- continue;
- if (anIt1->ptA == anIt2->ptA && anIt1->ptB == anIt2->ptB &&
- anIt1->entityA == anIt2->entityA && anIt1->entityB == anIt2->entityB &&
- anIt1->entityC == anIt2->entityC && anIt1->entityD == anIt2->entityD)
- myDuplicatedConstraint = true;
- }
- }
- }
- return aResult;
-}
-
-const Slvs_Constraint& SketchSolver_Storage::getConstraint(const Slvs_hConstraint& theConstraintID) const
-{
- int aPos = Search(theConstraintID, myConstraints);
- if (aPos >= 0 && aPos < (int)myConstraints.size())
- return myConstraints[aPos];
-
- // Constraint is not found, return empty object
- static Slvs_Constraint aDummy;
- aDummy.h = 0;