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()
12 : myEquationSystem(new GCS::System),
13 myDiagnoseBeforeSolve(false),
15 myConfCollected(false),
20 PlaneGCSSolver_Solver::~PlaneGCSSolver_Solver()
25 void PlaneGCSSolver_Solver::clear()
27 myEquationSystem->clear();
29 myConstraints.clear();
30 myConflictingIDs.clear();
34 void PlaneGCSSolver_Solver::addConstraint(GCSConstraintPtr theConstraint)
36 myEquationSystem->addConstraint(theConstraint.get());
37 myConstraints[theConstraint->getTag()].insert(theConstraint);
38 if (theConstraint->getTag() >= 0)
43 void PlaneGCSSolver_Solver::removeConstraint(ConstraintID theID)
45 myConstraints.erase(theID);
46 if (myConstraints.empty()) {
47 myEquationSystem->clear();
48 myDOF = (int)myParameters.size();
50 myEquationSystem->clearByTag(theID);
57 double* PlaneGCSSolver_Solver::createParameter()
59 double* aResult = new double(0);
60 myParameters.push_back(aResult);
61 if (myConstraints.empty() && myDOF >= 0)
62 ++myDOF; // calculate DoF by hand if and only if there is no constraints yet
64 myDiagnoseBeforeSolve = true;
68 void PlaneGCSSolver_Solver::addParameters(const GCS::SET_pD& theParams)
70 GCS::SET_pD aParams(theParams);
71 // leave new parameters only
72 GCS::VEC_pD::iterator anIt = myParameters.begin();
73 for (; anIt != myParameters.end(); ++anIt)
74 if (aParams.find(*anIt) != aParams.end())
77 myParameters.insert(myParameters.end(), aParams.begin(), aParams.end());
78 if (myConstraints.empty() && myDOF >=0)
79 myDOF += (int)aParams.size(); // calculate DoF by hand only if there is no constraints yet
81 myDiagnoseBeforeSolve = true;
84 void PlaneGCSSolver_Solver::removeParameters(const GCS::SET_pD& theParams)
86 for (int i = (int)myParameters.size() - 1; i >= 0; --i)
87 if (theParams.find(myParameters[i]) != theParams.end()) {
88 myParameters.erase(myParameters.begin() + i);
91 if (!myConstraints.empty())
92 myDiagnoseBeforeSolve = true;
95 void PlaneGCSSolver_Solver::initialize()
97 Events_LongOp::start(this);
98 if (myDiagnoseBeforeSolve)
100 myEquationSystem->declareUnknowns(myParameters);
101 myEquationSystem->initSolution();
102 Events_LongOp::end(this);
107 PlaneGCSSolver_Solver::SolveStatus PlaneGCSSolver_Solver::solve()
109 // clear list of conflicting constraints
110 if (myConfCollected) {
111 myConflictingIDs.clear();
112 myConfCollected = false;
115 if (myParameters.empty())
116 return STATUS_INCONSISTENT;
118 GCS::SolveStatus aResult = GCS::Success;
119 Events_LongOp::start(this);
121 aResult = (GCS::SolveStatus)myEquationSystem->solve();
123 if (myDiagnoseBeforeSolve)
125 aResult = (GCS::SolveStatus)myEquationSystem->solve(myParameters);
127 Events_LongOp::end(this);
129 // collect information about conflicting constraints every time,
130 // sometimes solver reports about succeeded recalculation but has conflicting constraints
131 // (for example, apply horizontal constraint for a copied feature)
132 collectConflicting();
133 if (!myConflictingIDs.empty())
134 aResult = GCS::Failed;
137 if (aResult == GCS::Failed)
138 aStatus = STATUS_FAILED;
140 myEquationSystem->applySolution();
142 myDOF = myEquationSystem->dofsNumber();
146 myInitilized = false;
150 void PlaneGCSSolver_Solver::undo()
152 myEquationSystem->undoSolution();
155 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
157 if (!myConfCollected)
158 const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
159 return myConflictingIDs.find((int)theConstraint) != myConflictingIDs.end();
162 void PlaneGCSSolver_Solver::collectConflicting()
164 GCS::VEC_I aConflict;
165 myEquationSystem->getConflicting(aConflict);
166 myConflictingIDs.insert(aConflict.begin(), aConflict.end());
168 myEquationSystem->getRedundant(aConflict);
169 myConflictingIDs.insert(aConflict.begin(), aConflict.end());
171 myConfCollected = true;
174 int PlaneGCSSolver_Solver::dof()
176 if (myDOF < 0 && !myConstraints.empty())
181 void PlaneGCSSolver_Solver::diagnose()
183 myEquationSystem->declareUnknowns(myParameters);
184 myDOF = myEquationSystem->diagnose();
185 myDiagnoseBeforeSolve = false;