Salome HOME
Improve sketch performance while moving entities
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_Solver.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    PlaneGCSSolver_Solver.cpp
4 // Created: 14 Dec 2014
5 // Author:  Artem ZHIDKOV
6
7 #include <PlaneGCSSolver_Solver.h>
8 #include <Events_LongOp.h>
9
10
11 PlaneGCSSolver_Solver::PlaneGCSSolver_Solver()
12   : myEquationSystem(new GCS::System),
13     myDiagnoseBeforeSolve(false),
14     myInitilized(false),
15     myConfCollected(false),
16     myDOF(0)
17 {
18 }
19
20 PlaneGCSSolver_Solver::~PlaneGCSSolver_Solver()
21 {
22   clear();
23 }
24
25 void PlaneGCSSolver_Solver::clear()
26 {
27   myEquationSystem->clear();
28   myParameters.clear();
29   myConstraints.clear();
30   myConflictingIDs.clear();
31   myDOF = 0;
32 }
33
34 void PlaneGCSSolver_Solver::addConstraint(GCSConstraintPtr theConstraint)
35 {
36   myEquationSystem->addConstraint(theConstraint.get());
37   myConstraints[theConstraint->getTag()].insert(theConstraint);
38   if (theConstraint->getTag() >= 0)
39     myDOF = -1;
40 }
41
42 void PlaneGCSSolver_Solver::removeConstraint(ConstraintID theID)
43 {
44   myConstraints.erase(theID);
45   if (myConstraints.empty()) {
46     myEquationSystem->clear();
47     myDOF = (int)myParameters.size();
48   } else {
49     myEquationSystem->clearByTag(theID);
50     if (theID >= 0)
51       myDOF = -1;
52   }
53 }
54
55 double* PlaneGCSSolver_Solver::createParameter()
56 {
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
61   else
62     myDiagnoseBeforeSolve = true;
63   return aResult;
64 }
65
66 void PlaneGCSSolver_Solver::removeParameters(const GCS::SET_pD& theParams)
67 {
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);
71       --myDOF;
72     }
73 }
74
75 void PlaneGCSSolver_Solver::initialize()
76 {
77   Events_LongOp::start(this);
78   if (myDiagnoseBeforeSolve)
79     diagnose();
80   myEquationSystem->declareUnknowns(myParameters);
81   myEquationSystem->initSolution();
82   Events_LongOp::end(this);
83
84   myInitilized = true;
85 }
86
87 PlaneGCSSolver_Solver::SolveStatus PlaneGCSSolver_Solver::solve()
88 {
89   // clear list of conflicting constraints
90   if (myConfCollected) {
91     myConflictingIDs.clear();
92     myConfCollected = false;
93   }
94
95   if (myParameters.empty())
96     return STATUS_INCONSISTENT;
97
98   GCS::SolveStatus aResult = GCS::Success;
99   Events_LongOp::start(this);
100   if (myInitilized) {
101     aResult = (GCS::SolveStatus)myEquationSystem->solve();
102   } else {
103     if (myDiagnoseBeforeSolve)
104       diagnose();
105     aResult = (GCS::SolveStatus)myEquationSystem->solve(myParameters);
106   }
107   Events_LongOp::end(this);
108
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;
115
116   SolveStatus aStatus;
117   if (aResult == GCS::Failed)
118     aStatus = STATUS_FAILED;
119   else {
120     myEquationSystem->applySolution();
121     if (myDOF < 0)
122       myDOF = myEquationSystem->dofsNumber();
123     aStatus = STATUS_OK;
124   }
125
126   myInitilized = false;
127   return aStatus;
128 }
129
130 void PlaneGCSSolver_Solver::undo()
131 {
132   myEquationSystem->undoSolution();
133 }
134
135 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
136 {
137   if (!myConfCollected)
138     const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
139   return myConflictingIDs.find((int)theConstraint) != myConflictingIDs.end();
140 }
141
142 void PlaneGCSSolver_Solver::collectConflicting()
143 {
144   GCS::VEC_I aConflict;
145   myEquationSystem->getConflicting(aConflict);
146   myConflictingIDs.insert(aConflict.begin(), aConflict.end());
147
148   myEquationSystem->getRedundant(aConflict);
149   myConflictingIDs.insert(aConflict.begin(), aConflict.end());
150
151   myConfCollected = true;
152 }
153
154 int PlaneGCSSolver_Solver::dof()
155 {
156   if (myDOF < 0 && !myConstraints.empty())
157     diagnose();
158   return myDOF;
159 }
160
161 void PlaneGCSSolver_Solver::diagnose()
162 {
163   myEquationSystem->declareUnknowns(myParameters);
164   myDOF = myEquationSystem->diagnose();
165   myDiagnoseBeforeSolve = false;
166 }