1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PlaneGCSSolver_Solver.cpp
4 // Created: 14 Dec 2014
5 // Author: Artem ZHIDKOV
7 #include "PlaneGCSSolver_Solver.h"
8 #include <Events_LongOp.h>
11 PlaneGCSSolver_Solver::~PlaneGCSSolver_Solver()
16 void PlaneGCSSolver_Solver::clear()
18 myEquationSystem.clear();
19 myConstraints.clear();
23 void PlaneGCSSolver_Solver::addConstraint(GCSConstraintPtr theConstraint)
25 GCS::Constraint* aConstraint = theConstraint.get();
26 if (myConstraints.find(aConstraint) != myConstraints.end())
27 return; // constraint already exists, no need to add it again
29 myEquationSystem.addConstraint(aConstraint);
30 myConstraints.insert(aConstraint);
33 void PlaneGCSSolver_Solver::removeConstraint(GCSConstraintPtr theConstraint)
35 GCS::Constraint* aConstraint = theConstraint.get();
36 if (myConstraints.find(aConstraint) == myConstraints.end())
37 return; // no constraint, no need to remove it
39 myEquationSystem.removeConstraint(aConstraint);
40 myConstraints.erase(aConstraint);
43 static void removeTangent(GCS::VEC_I& theRedundant, const GCS::SET_I& theTangent)
46 while (i < theRedundant.size()) {
47 if (theTangent.find(theRedundant[i]) == theTangent.end())
50 theRedundant.erase(theRedundant.begin() + i);
54 SketchSolver_SolveStatus PlaneGCSSolver_Solver::solve()
56 // clear list of conflicting constraints
57 if (myConfCollected) {
58 myConflictingIDs.clear();
59 myConfCollected = false;
62 if (myConstraints.empty())
63 return STATUS_EMPTYSET;
64 if (myParameters.empty())
65 return STATUS_INCONSISTENT;
67 Events_LongOp::start(this);
68 GCS::SolveStatus aResult = GCS::Success;
69 // if there is a constraint with all attributes constant, set fail status
70 GCS::SET_pD aParameters;
71 aParameters.insert(myParameters.begin(), myParameters.end());
72 std::set<GCS::Constraint*>::const_iterator aConstrIt = myConstraints.begin();
73 for (; aConstrIt != myConstraints.end(); ++aConstrIt) {
74 GCS::VEC_pD aParams = (*aConstrIt)->params();
75 GCS::VEC_pD::const_iterator aPIt = aParams.begin();
76 for (; aPIt != aParams.end(); ++aPIt)
77 if (aParameters.find(*aPIt) != aParameters.end())
79 if (aPIt == aParams.end()) {
80 aResult = GCS::Failed;
84 if (aResult == GCS::Success)
85 aResult = (GCS::SolveStatus)myEquationSystem.solve(myParameters);
86 if (aResult == GCS::Success) {
87 // additionally check redundant constraints
88 GCS::VEC_I aRedundantID;
89 myEquationSystem.getRedundant(aRedundantID);
90 // remove redundant constraints relative to tangency
91 removeTangent(aRedundantID, myTangent);
92 if (!aRedundantID.empty())
93 aResult = GCS::Failed;
95 Events_LongOp::end(this);
97 SketchSolver_SolveStatus aStatus;
98 if (aResult == GCS::Success) {
99 myEquationSystem.applySolution();
102 aStatus = STATUS_FAILED;
107 void PlaneGCSSolver_Solver::undo()
109 myEquationSystem.undoSolution();
112 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
114 if (!myConfCollected)
115 const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
117 GCS::VEC_I::const_iterator anIt = myConflictingIDs.begin();
118 for (; anIt != myConflictingIDs.end(); ++anIt)
119 if (*anIt == (int)theConstraint)
124 void PlaneGCSSolver_Solver::collectConflicting()
126 myEquationSystem.getConflicting(myConflictingIDs);
128 GCS::VEC_I aRedundantID;
129 myEquationSystem.getRedundant(aRedundantID);
130 myConflictingIDs.insert(myConflictingIDs.end(), aRedundantID.begin(), aRedundantID.end());
132 myConfCollected = true;
135 int PlaneGCSSolver_Solver::dof() const
137 return const_cast<PlaneGCSSolver_Solver*>(this)->myEquationSystem.dofsNumber();