Salome HOME
Revert "Make the copies fixed in multi-rotation, multi-translation (issue #1471)"
[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::SolveStatus)myEquationSystem.solve(myParameters);
60   Events_LongOp::end(this);
61
62   SketchSolver_SolveStatus aStatus;
63   if (aResult == GCS::Success) {
64     myEquationSystem.applySolution();
65     aStatus = STATUS_OK;
66   } else
67     aStatus = STATUS_FAILED;
68
69   return aStatus;
70 }
71
72 void PlaneGCSSolver_Solver::undo()
73 {
74   myEquationSystem.undoSolution();
75 }
76
77 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
78 {
79   if (!myConfCollected)
80     const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
81
82   GCS::VEC_I::const_iterator anIt = myConflictingIDs.begin();
83   for (; anIt != myConflictingIDs.end(); ++anIt)
84     if (*anIt == (int)theConstraint)
85       return true;
86   return false;
87 }
88
89 void PlaneGCSSolver_Solver::collectConflicting()
90 {
91   myEquationSystem.getConflicting(myConflictingIDs);
92
93   GCS::VEC_I aRedundantID;
94   myEquationSystem.getRedundant(aRedundantID);
95   myConflictingIDs.insert(myConflictingIDs.end(), aRedundantID.begin(), aRedundantID.end());
96
97   myConfCollected = true;
98 }
99
100 int PlaneGCSSolver_Solver::dof() const
101 {
102   return const_cast<PlaneGCSSolver_Solver*>(this)->myEquationSystem.dofsNumber();
103 }