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 myEquationSystem.clear();
19 myConstraints.clear();
23 void PlaneGCSSolver_Solver::addConstraint(GCSConstraintPtr theConstraint)
25 GCS::Constraint* aConstraint = theConstraint.get();
26 if (myConstraints.find(aConstraint) != myConstraints.end())
27 return; // constraint already exists, no need to add it again
29 myEquationSystem.addConstraint(aConstraint);
30 myConstraints.insert(aConstraint);
33 void PlaneGCSSolver_Solver::removeConstraint(GCSConstraintPtr theConstraint)
35 GCS::Constraint* aConstraint = theConstraint.get();
36 removeConstraint(aConstraint);
39 void PlaneGCSSolver_Solver::removeConstraint(GCS::Constraint* theConstraint)
41 if (myConstraints.find(theConstraint) == myConstraints.end())
42 return; // no constraint, no need to remove it
44 myEquationSystem.removeConstraint(theConstraint);
45 myConstraints.erase(theConstraint);
48 SketchSolver_SolveStatus PlaneGCSSolver_Solver::solve()
50 // clear list of conflicting constraints
51 if (myConfCollected) {
52 myConflictingIDs.clear();
53 myConfCollected = false;
56 if (myConstraints.empty())
57 return STATUS_EMPTYSET;
58 if (myParameters.empty())
59 return STATUS_INCONSISTENT;
61 Events_LongOp::start(this);
62 GCS::SolveStatus aResult = GCS::Success;
63 // if there is a constraint with all attributes constant, set fail status
64 GCS::SET_pD aParameters;
65 aParameters.insert(myParameters.begin(), myParameters.end());
66 std::set<GCS::Constraint*>::const_iterator aConstrIt = myConstraints.begin();
67 for (; aConstrIt != myConstraints.end(); ++aConstrIt) {
68 GCS::VEC_pD aParams = (*aConstrIt)->params();
69 GCS::VEC_pD::const_iterator aPIt = aParams.begin();
70 for (; aPIt != aParams.end(); ++aPIt)
71 if (aParameters.find(*aPIt) != aParameters.end())
73 if (aPIt == aParams.end() && (*aConstrIt)->getTag() > 0) {
74 myConflictingIDs.push_back((*aConstrIt)->getTag());
75 myConfCollected = true;
76 aResult = GCS::Failed;
80 if (aResult == GCS::Success)
81 aResult = (GCS::SolveStatus)myEquationSystem.solve(myParameters);
82 if (aResult == GCS::Success) {
83 // additionally check redundant constraints
84 GCS::VEC_I aRedundantID;
85 myEquationSystem.getRedundant(aRedundantID);
86 // The system with tangent constraints may show redundant constraints if the entities are coupled smoothly.
87 // Sometimes tangent constraints are fall to both conflicting and redundant constraints.
88 // Need to check if there are redundant constraints without these tangencies.
89 if (!aRedundantID.empty())
90 aResult = myTangent.empty() ? GCS::Failed : (GCS::SolveStatus)solveWithoutTangent();
92 Events_LongOp::end(this);
94 SketchSolver_SolveStatus aStatus;
95 if (aResult == GCS::Success) {
96 myEquationSystem.applySolution();
99 aStatus = STATUS_FAILED;
104 SketchSolver_SolveStatus PlaneGCSSolver_Solver::solveWithoutTangent()
106 // Remove tangency which leads to redundant or conflicting constraints
107 GCS::VEC_I aConflicting, aRedundant;
108 myEquationSystem.getRedundant(aRedundant);
109 size_t aNbRemove = myTangent.size(); // number of tangent constraints which can be removed
110 myEquationSystem.getConflicting(aConflicting);
111 aRedundant.insert(aRedundant.end(), aConflicting.begin(), aConflicting.end());
113 GCS::SET_I aTangentToRemove;
114 GCS::VEC_I::iterator aCIt = aRedundant.begin();
115 for (; aCIt != aRedundant.end() && aNbRemove > 0; ++aCIt)
116 if (myTangent.find(*aCIt) != myTangent.end()) {
117 aTangentToRemove.insert(*aCIt);
121 std::set<GCS::Constraint*>::const_iterator aConstrIt = myConstraints.begin();
122 while (aConstrIt != myConstraints.end()) {
123 GCS::Constraint* aConstraint = *aConstrIt;
124 int anID = aConstraint->getTag();
126 if (aTangentToRemove.find(anID) != aTangentToRemove.end())
127 removeConstraint(aConstraint);
134 void PlaneGCSSolver_Solver::undo()
136 myEquationSystem.undoSolution();
139 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
141 if (!myConfCollected)
142 const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
144 GCS::VEC_I::const_iterator anIt = myConflictingIDs.begin();
145 for (; anIt != myConflictingIDs.end(); ++anIt)
146 if (*anIt == (int)theConstraint)
151 void PlaneGCSSolver_Solver::collectConflicting()
153 GCS::VEC_I aConflict;
154 myEquationSystem.getConflicting(myConflictingIDs);
155 myConflictingIDs.insert(myConflictingIDs.end(), aConflict.begin(), aConflict.end());
157 myEquationSystem.getRedundant(aConflict);
158 myConflictingIDs.insert(myConflictingIDs.end(), aConflict.begin(), aConflict.end());
160 myConfCollected = true;
163 int PlaneGCSSolver_Solver::dof() const
165 return const_cast<PlaneGCSSolver_Solver*>(this)->myEquationSystem.dofsNumber();