Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_Solver.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <PlaneGCSSolver_Solver.h>
22 #include <Events_LongOp.h>
23
24
25 PlaneGCSSolver_Solver::PlaneGCSSolver_Solver()
26   : myEquationSystem(new GCS::System),
27     myDiagnoseBeforeSolve(false),
28     myInitilized(false),
29     myConfCollected(false),
30     myDOF(0),
31     myFictiveConstraint(0)
32 {
33 }
34
35 PlaneGCSSolver_Solver::~PlaneGCSSolver_Solver()
36 {
37   clear();
38 }
39
40 void PlaneGCSSolver_Solver::clear()
41 {
42   myEquationSystem->clear();
43   myParameters.clear();
44   myConstraints.clear();
45   myConflictingIDs.clear();
46   myDOF = 0;
47
48   removeFictiveConstraint();
49 }
50
51 void PlaneGCSSolver_Solver::addConstraint(GCSConstraintPtr theConstraint)
52 {
53   myEquationSystem->addConstraint(theConstraint.get());
54   myConstraints[theConstraint->getTag()].insert(theConstraint);
55   if (theConstraint->getTag() >= 0)
56     myDOF = -1;
57   myInitilized = false;
58 }
59
60 void PlaneGCSSolver_Solver::removeConstraint(ConstraintID theID)
61 {
62   myConstraints.erase(theID);
63   if (myConstraints.empty()) {
64     myEquationSystem->clear();
65     myDOF = (int)myParameters.size();
66   } else {
67     myEquationSystem->clearByTag(theID);
68     if (theID >= 0)
69       myDOF = -1;
70   }
71   myInitilized = false;
72 }
73
74 double* PlaneGCSSolver_Solver::createParameter()
75 {
76   double* aResult = new double(0);
77   myParameters.push_back(aResult);
78   if (myConstraints.empty() && myDOF >= 0)
79     ++myDOF; // calculate DoF by hand if and only if there is no constraints yet
80   else
81     myDiagnoseBeforeSolve = true;
82   return aResult;
83 }
84
85 void PlaneGCSSolver_Solver::addParameters(const GCS::SET_pD& theParams)
86 {
87   GCS::SET_pD aParams(theParams);
88   // leave new parameters only
89   GCS::VEC_pD::iterator anIt = myParameters.begin();
90   for (; anIt != myParameters.end(); ++anIt)
91     if (aParams.find(*anIt) != aParams.end())
92       aParams.erase(*anIt);
93
94   myParameters.insert(myParameters.end(), aParams.begin(), aParams.end());
95   if (myConstraints.empty() && myDOF >=0)
96     myDOF += (int)aParams.size(); // calculate DoF by hand only if there is no constraints yet
97   else
98     myDiagnoseBeforeSolve = true;
99 }
100
101 void PlaneGCSSolver_Solver::removeParameters(const GCS::SET_pD& theParams)
102 {
103   for (int i = (int)myParameters.size() - 1; i >= 0; --i)
104     if (theParams.find(myParameters[i]) != theParams.end()) {
105       myParameters.erase(myParameters.begin() + i);
106       --myDOF;
107     }
108   if (!myConstraints.empty())
109     myDiagnoseBeforeSolve = true;
110 }
111
112 void PlaneGCSSolver_Solver::initialize()
113 {
114   Events_LongOp::start(this);
115   addFictiveConstraintIfNecessary();
116   if (myDiagnoseBeforeSolve)
117     diagnose();
118   myEquationSystem->declareUnknowns(myParameters);
119   myEquationSystem->initSolution();
120   Events_LongOp::end(this);
121
122   myInitilized = true;
123 }
124
125 PlaneGCSSolver_Solver::SolveStatus PlaneGCSSolver_Solver::solve()
126 {
127   // clear list of conflicting constraints
128   if (myConfCollected) {
129     myConflictingIDs.clear();
130     myConfCollected = false;
131   }
132
133   if (myParameters.empty())
134     return STATUS_INCONSISTENT;
135
136   GCS::SolveStatus aResult = GCS::Success;
137   Events_LongOp::start(this);
138   if (myInitilized) {
139     aResult = (GCS::SolveStatus)myEquationSystem->solve();
140   } else {
141     addFictiveConstraintIfNecessary();
142
143     if (myDiagnoseBeforeSolve)
144       diagnose();
145     aResult = (GCS::SolveStatus)myEquationSystem->solve(myParameters);
146   }
147   Events_LongOp::end(this);
148
149   // collect information about conflicting constraints every time,
150   // sometimes solver reports about succeeded recalculation but has conflicting constraints
151   // (for example, apply horizontal constraint for a copied feature)
152   collectConflicting();
153   if (!myConflictingIDs.empty())
154     aResult = GCS::Failed;
155
156   SolveStatus aStatus;
157   if (aResult == GCS::Failed)
158     aStatus = STATUS_FAILED;
159   else {
160     myEquationSystem->applySolution();
161     if (myDOF < 0)
162       myDOF = myEquationSystem->dofsNumber();
163     aStatus = STATUS_OK;
164   }
165
166   removeFictiveConstraint();
167   myInitilized = false;
168   return aStatus;
169 }
170
171 void PlaneGCSSolver_Solver::undo()
172 {
173   myEquationSystem->undoSolution();
174 }
175
176 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
177 {
178   if (!myConfCollected)
179     const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
180   return myConflictingIDs.find((int)theConstraint) != myConflictingIDs.end();
181 }
182
183 void PlaneGCSSolver_Solver::collectConflicting()
184 {
185   GCS::VEC_I aConflict;
186   myEquationSystem->getConflicting(aConflict);
187   myConflictingIDs.insert(aConflict.begin(), aConflict.end());
188
189   myEquationSystem->getRedundant(aConflict);
190   myConflictingIDs.insert(aConflict.begin(), aConflict.end());
191
192   myConfCollected = true;
193 }
194
195 int PlaneGCSSolver_Solver::dof()
196 {
197   if (myDOF < 0 && !myConstraints.empty())
198     diagnose();
199   return myDOF;
200 }
201
202 void PlaneGCSSolver_Solver::diagnose()
203 {
204   myEquationSystem->declareUnknowns(myParameters);
205   myDOF = myEquationSystem->diagnose();
206   myDiagnoseBeforeSolve = false;
207 }
208
209 void PlaneGCSSolver_Solver::addFictiveConstraintIfNecessary()
210 {
211   if (!myConstraints.empty() &&
212       myConstraints.find(CID_MOVEMENT) == myConstraints.end())
213     return;
214
215   if (myFictiveConstraint)
216     return; // no need several fictive constraints
217
218   double* aParam = createParameter();
219   double* aFictiveParameter = new double(0.0);
220
221   myFictiveConstraint = new GCS::ConstraintEqual(aFictiveParameter, aParam);
222   myFictiveConstraint->setTag(CID_FICTIVE);
223   myEquationSystem->addConstraint(myFictiveConstraint);
224 }
225
226 void PlaneGCSSolver_Solver::removeFictiveConstraint()
227 {
228   if (myFictiveConstraint) {
229     myEquationSystem->removeConstraint(myFictiveConstraint);
230     myParameters.pop_back();
231
232     GCS::VEC_pD aParams = myFictiveConstraint->params();
233     for (GCS::VEC_pD::iterator anIt = aParams.begin(); anIt != aParams.end(); ++ anIt)
234       delete *anIt;
235     delete myFictiveConstraint;
236     myFictiveConstraint = 0;
237   }
238 }