Salome HOME
Merge branch 'BR_internationalization'
[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   if (myConstraints.find(aConstraint) == myConstraints.end())
37     return; // no constraint, no need to remove it
38
39   myEquationSystem.removeConstraint(aConstraint);
40   myConstraints.erase(aConstraint);
41 }
42
43 SketchSolver_SolveStatus PlaneGCSSolver_Solver::solve()
44 {
45   // clear list of conflicting constraints
46   if (myConfCollected) {
47     myConflictingIDs.clear();
48     myConfCollected = false;
49   }
50
51   if (myConstraints.empty())
52     return STATUS_EMPTYSET;
53   if (myParameters.empty())
54     return STATUS_INCONSISTENT;
55
56   Events_LongOp::start(this);
57   GCS::SolveStatus aResult = GCS::Success;
58   // if there is a constraint with all attributes constant, set fail status
59   GCS::SET_pD aParameters;
60   aParameters.insert(myParameters.begin(), myParameters.end());
61   std::set<GCS::Constraint*>::const_iterator aConstrIt = myConstraints.begin();
62   for (; aConstrIt != myConstraints.end(); ++aConstrIt) {
63     GCS::VEC_pD aParams = (*aConstrIt)->params();
64     GCS::VEC_pD::const_iterator aPIt = aParams.begin();
65     for (; aPIt != aParams.end(); ++aPIt)
66       if (aParameters.find(*aPIt) != aParameters.end())
67         break;
68     if (aPIt == aParams.end()) {
69       aResult = GCS::Failed;
70     }
71   }
72   // solve equations
73   if (aResult == GCS::Success)
74     aResult = (GCS::SolveStatus)myEquationSystem.solve(myParameters);
75   if (aResult == GCS::Success) {
76     // additionally check redundant constraints
77     GCS::VEC_I aRedundantID;
78     myEquationSystem.getRedundant(aRedundantID);
79     if (!aRedundantID.empty())
80       aResult = GCS::Failed;
81   }
82   Events_LongOp::end(this);
83
84   SketchSolver_SolveStatus aStatus;
85   if (aResult == GCS::Success) {
86     myEquationSystem.applySolution();
87     aStatus = STATUS_OK;
88   } else {
89     undo();
90     aStatus = STATUS_FAILED;
91   }
92
93   return aStatus;
94 }
95
96 void PlaneGCSSolver_Solver::undo()
97 {
98   myEquationSystem.undoSolution();
99 }
100
101 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
102 {
103   if (!myConfCollected)
104     const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
105
106   GCS::VEC_I::const_iterator anIt = myConflictingIDs.begin();
107   for (; anIt != myConflictingIDs.end(); ++anIt)
108     if (*anIt == (int)theConstraint)
109       return true;
110   return false;
111 }
112
113 void PlaneGCSSolver_Solver::collectConflicting()
114 {
115   myEquationSystem.getConflicting(myConflictingIDs);
116
117   GCS::VEC_I aRedundantID;
118   myEquationSystem.getRedundant(aRedundantID);
119   myConflictingIDs.insert(myConflictingIDs.end(), aRedundantID.begin(), aRedundantID.end());
120
121   myConfCollected = true;
122 }
123
124 int PlaneGCSSolver_Solver::dof() const
125 {
126   return const_cast<PlaneGCSSolver_Solver*>(this)->myEquationSystem.dofsNumber();
127 }