]> SALOME platform Git repositories - modules/shaper.git/blob - src/SketchSolver/PlaneGCSSolver/PlaneGCSSolver_Solver.cpp
Salome HOME
Fix crashes caused by PlaneGCSSolver connector (issue #1538)
[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   Events_LongOp::end(this);
76
77   SketchSolver_SolveStatus aStatus;
78   if (aResult == GCS::Success) {
79     myEquationSystem.applySolution();
80     aStatus = STATUS_OK;
81   } else {
82     undo();
83     aStatus = STATUS_FAILED;
84   }
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 }