1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PlaneGCSSolver_Solver.cpp
4 // Created: 14 Dec 2014
5 // Author: Artem ZHIDKOV
7 #include "PlaneGCSSolver_Solver.h"
8 #include <Events_LongOp.h>
11 PlaneGCSSolver_Solver::~PlaneGCSSolver_Solver()
16 void PlaneGCSSolver_Solver::clear()
18 std::set<GCS::Constraint*>::const_iterator anIt = myConstraints.begin();
19 for (; anIt != myConstraints.end(); ++anIt)
20 myEquationSystem.removeConstraint(*anIt);
21 myConstraints.clear();
25 void PlaneGCSSolver_Solver::addConstraint(GCSConstraintPtr theConstraint)
27 GCS::Constraint* aConstraint = theConstraint.get();
28 if (myConstraints.find(aConstraint) != myConstraints.end())
29 return; // constraint already exists, no need to add it again
31 myEquationSystem.addConstraint(aConstraint);
32 myConstraints.insert(aConstraint);
35 void PlaneGCSSolver_Solver::removeConstraint(GCSConstraintPtr theConstraint)
37 GCS::Constraint* aConstraint = theConstraint.get();
38 if (myConstraints.find(aConstraint) == myConstraints.end())
39 return; // no constraint, no need to remove it
41 myEquationSystem.removeConstraint(aConstraint);
42 myConstraints.erase(aConstraint);
45 SketchSolver_SolveStatus PlaneGCSSolver_Solver::solve()
47 // clear list of conflicting constraints
48 if (myConfCollected) {
49 myConflictingIDs.clear();
50 myConfCollected = false;
53 if (myConstraints.empty())
54 return STATUS_EMPTYSET;
55 if (myParameters.empty())
56 return STATUS_INCONSISTENT;
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())
70 if (aPIt == aParams.end()) {
71 aResult = GCS::Failed;
75 if (aResult == GCS::Success)
76 aResult = (GCS::SolveStatus)myEquationSystem.solve(myParameters);
77 Events_LongOp::end(this);
79 SketchSolver_SolveStatus aStatus;
80 if (aResult == GCS::Success) {
81 myEquationSystem.applySolution();
84 aStatus = STATUS_FAILED;
89 void PlaneGCSSolver_Solver::undo()
91 myEquationSystem.undoSolution();
94 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
97 const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
99 GCS::VEC_I::const_iterator anIt = myConflictingIDs.begin();
100 for (; anIt != myConflictingIDs.end(); ++anIt)
101 if (*anIt == (int)theConstraint)
106 void PlaneGCSSolver_Solver::collectConflicting()
108 myEquationSystem.getConflicting(myConflictingIDs);
110 GCS::VEC_I aRedundantID;
111 myEquationSystem.getRedundant(aRedundantID);
112 myConflictingIDs.insert(myConflictingIDs.end(), aRedundantID.begin(), aRedundantID.end());
114 myConfCollected = true;
117 int PlaneGCSSolver_Solver::dof() const
119 return const_cast<PlaneGCSSolver_Solver*>(this)->myEquationSystem.dofsNumber();