Salome HOME
ff8d151505007369c73cda968a2d676cbf6cfca9
[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 {
13   clear();
14 }
15
16 void PlaneGCSSolver_Solver::clear()
17 {
18   myEquationSystem.clear();
19   myConstraints.clear();
20   myParameters.clear();
21 }
22
23 void PlaneGCSSolver_Solver::addConstraint(GCSConstraintPtr theConstraint)
24 {
25   GCS::Constraint* aConstraint = theConstraint.get();
26   if (myConstraints.find(aConstraint) != myConstraints.end())
27     return; // constraint already exists, no need to add it again
28
29   myEquationSystem.addConstraint(aConstraint);
30   myConstraints.insert(aConstraint);
31 }
32
33 void PlaneGCSSolver_Solver::removeConstraint(GCSConstraintPtr theConstraint)
34 {
35   GCS::Constraint* aConstraint = theConstraint.get();
36   removeConstraint(aConstraint);
37 }
38
39 void PlaneGCSSolver_Solver::removeConstraint(GCS::Constraint* theConstraint)
40 {
41   if (myConstraints.find(theConstraint) == myConstraints.end())
42     return; // no constraint, no need to remove it
43
44   myEquationSystem.removeConstraint(theConstraint);
45   myConstraints.erase(theConstraint);
46 }
47
48 SketchSolver_SolveStatus PlaneGCSSolver_Solver::solve()
49 {
50   // clear list of conflicting constraints
51   if (myConfCollected) {
52     myConflictingIDs.clear();
53     myConfCollected = false;
54   }
55
56   if (myConstraints.empty())
57     return STATUS_EMPTYSET;
58   if (myParameters.empty())
59     return STATUS_INCONSISTENT;
60
61   Events_LongOp::start(this);
62   GCS::SolveStatus aResult = GCS::Success;
63   // if there is a constraint with all attributes constant, set fail status
64   GCS::SET_pD aParameters;
65   aParameters.insert(myParameters.begin(), myParameters.end());
66   std::set<GCS::Constraint*>::const_iterator aConstrIt = myConstraints.begin();
67   for (; aConstrIt != myConstraints.end(); ++aConstrIt) {
68     GCS::VEC_pD aParams = (*aConstrIt)->params();
69     GCS::VEC_pD::const_iterator aPIt = aParams.begin();
70     for (; aPIt != aParams.end(); ++aPIt)
71       if (aParameters.find(*aPIt) != aParameters.end())
72         break;
73     if (aPIt == aParams.end() && (*aConstrIt)->getTag() > 0) {
74       myConflictingIDs.push_back((*aConstrIt)->getTag());
75       myConfCollected = true;
76       aResult = GCS::Failed;
77     }
78   }
79   // solve equations
80   if (aResult == GCS::Success)
81     aResult = (GCS::SolveStatus)myEquationSystem.solve(myParameters);
82   if (aResult == GCS::Success) {
83     // additionally check redundant constraints
84     GCS::VEC_I aRedundantID;
85     myEquationSystem.getRedundant(aRedundantID);
86     // Workaround: remove all constraints "Equal"
87     if (!aRedundantID.empty()) {
88       std::set<GCS::Constraint*>::const_iterator aCIt = myConstraints.begin();
89       for (; aCIt != myConstraints.end(); ++aCIt) {
90         GCS::VEC_I::iterator aRIt = aRedundantID.begin();
91         for (; aRIt != aRedundantID.end(); ++aRIt)
92           if ((*aCIt)->getTag() == *aRIt) {
93             aRedundantID.erase(aRIt);
94             break;
95           }
96       }
97     }
98     // The system with tangent constraints may show redundant constraints if the entities are coupled smoothly.
99     // Sometimes tangent constraints are fall to both conflicting and redundant constraints.
100     // Need to check if there are redundant constraints without these tangencies.
101     if (!aRedundantID.empty())
102       aResult = myTangent.empty() ? GCS::Failed : (GCS::SolveStatus)solveWithoutTangent();
103   }
104   Events_LongOp::end(this);
105
106   SketchSolver_SolveStatus aStatus;
107   if (aResult == GCS::Success) {
108     myEquationSystem.applySolution();
109     aStatus = STATUS_OK;
110   } else
111     aStatus = STATUS_FAILED;
112
113   return aStatus;
114 }
115
116 SketchSolver_SolveStatus PlaneGCSSolver_Solver::solveWithoutTangent()
117 {
118   // Remove tangency which leads to redundant or conflicting constraints
119   GCS::VEC_I aConflicting, aRedundant;
120   myEquationSystem.getRedundant(aRedundant);
121   size_t aNbRemove = myTangent.size(); // number of tangent constraints which can be removed
122   myEquationSystem.getConflicting(aConflicting);
123   aRedundant.insert(aRedundant.end(), aConflicting.begin(), aConflicting.end());
124
125   GCS::SET_I aTangentToRemove;
126   GCS::VEC_I::iterator aCIt = aRedundant.begin();
127   for (; aCIt != aRedundant.end() && aNbRemove > 0; ++aCIt)
128     if (myTangent.find(*aCIt) != myTangent.end()) {
129       aTangentToRemove.insert(*aCIt);
130       --aNbRemove;
131     }
132
133   std::set<GCS::Constraint*>::const_iterator aConstrIt = myConstraints.begin();
134   while (aConstrIt != myConstraints.end()) {
135     GCS::Constraint* aConstraint = *aConstrIt;
136     int anID = aConstraint->getTag();
137     ++aConstrIt;
138     if (aTangentToRemove.find(anID) != aTangentToRemove.end())
139       removeConstraint(aConstraint);
140   }
141
142   myTangent.clear();
143   return solve();
144 }
145
146 void PlaneGCSSolver_Solver::undo()
147 {
148   myEquationSystem.undoSolution();
149 }
150
151 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
152 {
153   if (!myConfCollected)
154     const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
155
156   GCS::VEC_I::const_iterator anIt = myConflictingIDs.begin();
157   for (; anIt != myConflictingIDs.end(); ++anIt)
158     if (*anIt == (int)theConstraint)
159       return true;
160   return false;
161 }
162
163 void PlaneGCSSolver_Solver::collectConflicting()
164 {
165   GCS::VEC_I aConflict;
166   myEquationSystem.getConflicting(myConflictingIDs);
167   myConflictingIDs.insert(myConflictingIDs.end(), aConflict.begin(), aConflict.end());
168
169   myEquationSystem.getRedundant(aConflict);
170   myConflictingIDs.insert(myConflictingIDs.end(), aConflict.begin(), aConflict.end());
171
172   myConfCollected = true;
173 }
174
175 int PlaneGCSSolver_Solver::dof() const
176 {
177   return const_cast<PlaneGCSSolver_Solver*>(this)->myEquationSystem.dofsNumber();
178 }