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)
42 void PlaneGCSSolver_Solver::removeConstraint(ConstraintID theID)
44 myConstraints.erase(theID);
45 if (myConstraints.empty()) {
46 myEquationSystem->clear();
47 myDOF = (int)myParameters.size();
49 myEquationSystem->clearByTag(theID);
55 double* PlaneGCSSolver_Solver::createParameter()
57 double* aResult = new double(0);
58 myParameters.push_back(aResult);
59 if (myConstraints.empty() && myDOF >= 0)
60 ++myDOF; // calculate DoF by hand if and only if there is no constraints yet
62 myDiagnoseBeforeSolve = true;
66 void PlaneGCSSolver_Solver::removeParameters(const GCS::SET_pD& theParams)
68 for (int i = (int)myParameters.size() - 1; i >= 0; --i)
69 if (theParams.find(myParameters[i]) != theParams.end()) {
70 myParameters.erase(myParameters.begin() + i);
75 void PlaneGCSSolver_Solver::initialize()
77 Events_LongOp::start(this);
78 if (myDiagnoseBeforeSolve)
80 myEquationSystem->declareUnknowns(myParameters);
81 myEquationSystem->initSolution();
82 Events_LongOp::end(this);
87 PlaneGCSSolver_Solver::SolveStatus PlaneGCSSolver_Solver::solve()
89 // clear list of conflicting constraints
90 if (myConfCollected) {
91 myConflictingIDs.clear();
92 myConfCollected = false;
95 if (myParameters.empty())
96 return STATUS_INCONSISTENT;
98 GCS::SolveStatus aResult = GCS::Success;
99 Events_LongOp::start(this);
101 aResult = (GCS::SolveStatus)myEquationSystem->solve();
103 if (myDiagnoseBeforeSolve)
105 aResult = (GCS::SolveStatus)myEquationSystem->solve(myParameters);
107 Events_LongOp::end(this);
109 // collect information about conflicting constraints every time,
110 // sometimes solver reports about succeeded recalculation but has conflicting constraints
111 // (for example, apply horizontal constraint for a copied feature)
112 collectConflicting();
113 if (!myConflictingIDs.empty())
114 aResult = GCS::Failed;
117 if (aResult == GCS::Failed)
118 aStatus = STATUS_FAILED;
120 myEquationSystem->applySolution();
122 myDOF = myEquationSystem->dofsNumber();
126 myInitilized = false;
130 void PlaneGCSSolver_Solver::undo()
132 myEquationSystem->undoSolution();
135 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
137 if (!myConfCollected)
138 const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
139 return myConflictingIDs.find((int)theConstraint) != myConflictingIDs.end();
142 void PlaneGCSSolver_Solver::collectConflicting()
144 GCS::VEC_I aConflict;
145 myEquationSystem->getConflicting(aConflict);
146 myConflictingIDs.insert(aConflict.begin(), aConflict.end());
148 myEquationSystem->getRedundant(aConflict);
149 myConflictingIDs.insert(aConflict.begin(), aConflict.end());
151 myConfCollected = true;
154 int PlaneGCSSolver_Solver::dof()
156 if (myDOF < 0 && !myConstraints.empty())
161 void PlaneGCSSolver_Solver::diagnose()
163 myEquationSystem->declareUnknowns(myParameters);
164 myDOF = myEquationSystem->diagnose();
165 myDiagnoseBeforeSolve = false;