]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/PlaneGCSSolver/PlaneGCSSolver_Solver.cpp
Salome HOME
PlaneGCS: Fix the problem regarding update of a distance during moving of a line...
[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   std::set<GCS::Constraint*>::const_iterator anIt = myConstraints.begin();
19   for (; anIt != myConstraints.end(); ++anIt)
20     myEquationSystem.removeConstraint(*anIt);
21   myConstraints.clear();
22   myParameters.clear();
23 }
24
25 void PlaneGCSSolver_Solver::addConstraint(GCSConstraintPtr theConstraint)
26 {
27   GCS::Constraint* aConstraint = theConstraint.get();
28   if (myConstraints.find(aConstraint) != myConstraints.end())
29     return; // constraint already exists, no need to add it again
30
31   myEquationSystem.addConstraint(aConstraint);
32   myConstraints.insert(aConstraint);
33 }
34
35 void PlaneGCSSolver_Solver::removeConstraint(GCSConstraintPtr theConstraint)
36 {
37   GCS::Constraint* aConstraint = theConstraint.get();
38   if (myConstraints.find(aConstraint) == myConstraints.end())
39     return; // no constraint, no need to remove it
40
41   myEquationSystem.removeConstraint(aConstraint);
42   myConstraints.erase(aConstraint);
43 }
44
45 SketchSolver_SolveStatus PlaneGCSSolver_Solver::solve()
46 {
47   // clear list of conflicting constraints
48   if (myConfCollected) {
49     myConflictingIDs.clear();
50     myConfCollected = false;
51   }
52
53   if (myConstraints.empty())
54     return STATUS_EMPTYSET;
55   if (myParameters.empty())
56     return STATUS_INCONSISTENT;
57
58   Events_LongOp::start(this);
59   GCS::SolveStatus aResult = GCS::Success;
60   // if there is a constraint with all attributes constant, set fail status
61   GCS::SET_pD aParameters;
62   aParameters.insert(myParameters.begin(), myParameters.end());
63   std::set<GCS::Constraint*>::const_iterator aConstrIt = myConstraints.begin();
64   for (; aConstrIt != myConstraints.end(); ++aConstrIt) {
65     GCS::VEC_pD aParams = (*aConstrIt)->params();
66     GCS::VEC_pD::const_iterator aPIt = aParams.begin();
67     for (; aPIt != aParams.end(); ++aPIt)
68       if (aParameters.find(*aPIt) != aParameters.end())
69         break;
70     if (aPIt == aParams.end()) {
71       aResult = GCS::Failed;
72     }
73   }
74   // solve equations
75   if (aResult == GCS::Success)
76     aResult = (GCS::SolveStatus)myEquationSystem.solve(myParameters);
77   Events_LongOp::end(this);
78
79   SketchSolver_SolveStatus aStatus;
80   if (aResult == GCS::Success) {
81     myEquationSystem.applySolution();
82     aStatus = STATUS_OK;
83   } else
84     aStatus = STATUS_FAILED;
85
86   return aStatus;
87 }
88
89 void PlaneGCSSolver_Solver::undo()
90 {
91   myEquationSystem.undoSolution();
92 }
93
94 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
95 {
96   if (!myConfCollected)
97     const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
98
99   GCS::VEC_I::const_iterator anIt = myConflictingIDs.begin();
100   for (; anIt != myConflictingIDs.end(); ++anIt)
101     if (*anIt == (int)theConstraint)
102       return true;
103   return false;
104 }
105
106 void PlaneGCSSolver_Solver::collectConflicting()
107 {
108   myEquationSystem.getConflicting(myConflictingIDs);
109
110   GCS::VEC_I aRedundantID;
111   myEquationSystem.getRedundant(aRedundantID);
112   myConflictingIDs.insert(myConflictingIDs.end(), aRedundantID.begin(), aRedundantID.end());
113
114   myConfCollected = true;
115 }
116
117 int PlaneGCSSolver_Solver::dof() const
118 {
119   return const_cast<PlaneGCSSolver_Solver*>(this)->myEquationSystem.dofsNumber();
120 }