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>
13 PlaneGCSSolver_Solver::PlaneGCSSolver_Solver()
14 : myEquationSystem(new GCS::System),
15 myConfCollected(false)
19 PlaneGCSSolver_Solver::~PlaneGCSSolver_Solver()
24 void PlaneGCSSolver_Solver::clear()
26 myEquationSystem->clear();
27 myConstraints.clear();
31 void PlaneGCSSolver_Solver::addConstraint(GCSConstraintPtr theConstraint,
32 const SketchSolver_ConstraintType theType)
34 GCS::Constraint* aConstraint = theConstraint.get();
35 if (myConstraints.find(aConstraint) != myConstraints.end())
36 return; // constraint already exists, no need to add it again
38 myEquationSystem->addConstraint(aConstraint);
39 myConstraints[aConstraint] = theType;
42 void PlaneGCSSolver_Solver::removeConstraint(GCSConstraintPtr theConstraint)
44 GCS::Constraint* aConstraint = theConstraint.get();
45 removeConstraint(aConstraint);
48 void PlaneGCSSolver_Solver::removeConstraint(GCS::Constraint* theConstraint)
50 if (myConstraints.find(theConstraint) == myConstraints.end())
51 return; // no constraint, no need to remove it
53 myEquationSystem->removeConstraint(theConstraint);
54 myConstraints.erase(theConstraint);
57 SketchSolver_SolveStatus PlaneGCSSolver_Solver::solve()
59 // clear list of conflicting constraints
60 if (myConfCollected) {
61 myConflictingIDs.clear();
62 myConfCollected = false;
65 if (myConstraints.empty())
66 return STATUS_EMPTYSET;
67 if (myParameters.empty())
68 return STATUS_INCONSISTENT;
70 Events_LongOp::start(this);
71 GCS::SolveStatus aResult = GCS::Success;
72 // if there is a constraint with all attributes constant, set fail status
73 GCS::SET_pD aParameters;
74 aParameters.insert(myParameters.begin(), myParameters.end());
75 ConstraintMap::const_iterator aConstrIt = myConstraints.begin();
76 for (; aConstrIt != myConstraints.end(); ++aConstrIt) {
77 GCS::VEC_pD aParams = aConstrIt->first->params();
78 GCS::VEC_pD::const_iterator aPIt = aParams.begin();
79 for (; aPIt != aParams.end(); ++aPIt)
80 if (aParameters.find(*aPIt) != aParameters.end())
82 if (aPIt == aParams.end() && aConstrIt->first->getTag() > 0) {
83 myConflictingIDs.insert(aConstrIt->first->getTag());
84 myConfCollected = true;
85 aResult = GCS::Failed;
89 if (aResult == GCS::Success)
90 aResult = (GCS::SolveStatus)myEquationSystem->solve(myParameters);
92 GCS::VEC_I aRedundantID;
94 // Workaround: the system with tangent constraint may fail if the tangent entities are connected smoothly.
95 // Investigate this situation and move constraints to redundant list
96 if (aResult == GCS::Failed && !myTangent.empty()) {
97 GCS::VEC_I aConflictingID;
98 myEquationSystem->getConflicting(aConflictingID);
99 GCS::VEC_I::iterator aCIt = aConflictingID.begin();
100 for (; aCIt != aConflictingID.end(); ++ aCIt) {
101 if (myTangent.find(*aCIt) == myTangent.end())
103 if (isTangentTruth(*aCIt))
104 aRedundantID.push_back(*aCIt);
107 if (!aRedundantID.empty())
108 aResult = GCS::Success; // check redundant constraints
111 // Additionally check redundant constraints
112 if (aResult == GCS::Success || aResult == GCS::Converged) {
113 GCS::VEC_I aRedundantLocal;
114 myEquationSystem->getRedundant(aRedundantLocal);
115 aRedundantID.insert(aRedundantID.end(), aRedundantLocal.begin(), aRedundantLocal.end());
116 // Workaround: remove all point-point coincidences from list of redundant
117 if (!aRedundantID.empty()) {
118 ConstraintMap::const_iterator aCIt = myConstraints.begin();
119 for (; aCIt != myConstraints.end(); ++aCIt) {
120 if (aCIt->second != CONSTRAINT_PT_PT_COINCIDENT)
122 GCS::VEC_I::iterator aRIt = aRedundantID.begin();
123 for (; aRIt != aRedundantID.end(); ++aRIt)
124 if (aCIt->first->getTag() == *aRIt) {
125 aRedundantID.erase(aRIt);
130 // The system with tangent constraints may show redundant constraints if the entities are coupled smoothly.
131 // Sometimes tangent constraints are fall to both conflicting and redundant constraints.
132 // Need to check if there are redundant constraints without these tangencies.
133 if (!aRedundantID.empty())
134 aResult = myTangent.empty() ? GCS::Failed : solveWithoutTangent();
136 aResult = GCS::Success;
138 Events_LongOp::end(this);
140 SketchSolver_SolveStatus aStatus;
141 if (aResult == GCS::Success) {
142 myEquationSystem->applySolution();
145 aStatus = STATUS_FAILED;
150 GCS::SolveStatus PlaneGCSSolver_Solver::solveWithoutTangent()
152 std::shared_ptr<GCS::System> aSystemWithoutTangent(new GCS::System);
154 // Remove tangency which leads to redundant or conflicting constraints
155 GCS::VEC_I aConflicting, aRedundant;
156 myEquationSystem->getRedundant(aRedundant);
157 size_t aNbRemove = myTangent.size(); // number of tangent constraints which can be removed
158 myEquationSystem->getConflicting(aConflicting);
159 aRedundant.insert(aRedundant.end(), aConflicting.begin(), aConflicting.end());
161 GCS::SET_I aTangentToRemove;
162 GCS::VEC_I::iterator aCIt = aRedundant.begin();
163 for (; aCIt != aRedundant.end() && aNbRemove > 0; ++aCIt)
164 if (myTangent.find(*aCIt) != myTangent.end()) {
165 aTangentToRemove.insert(*aCIt);
169 std::set<GCS::Constraint*> aRemovedTangent;
170 ConstraintMap::const_iterator aConstrIt = myConstraints.begin();
171 while (aConstrIt != myConstraints.end()) {
172 GCS::Constraint* aConstraint = aConstrIt->first;
173 int anID = aConstraint->getTag();
175 if (aTangentToRemove.find(anID) == aTangentToRemove.end())
176 aSystemWithoutTangent->addConstraint(aConstraint);
178 aRemovedTangent.insert(aConstraint);
182 GCS::SolveStatus aResult = (GCS::SolveStatus)aSystemWithoutTangent->solve(myParameters);
183 if (aResult == GCS::Success) {
184 GCS::VEC_I aRedundant;
185 aSystemWithoutTangent->getRedundant(aRedundant);
186 if (aRedundant.empty())
187 myEquationSystem = aSystemWithoutTangent;
189 aResult = GCS::Failed;
192 // additional check that removed constraints are still correct
193 if (aResult == GCS::Success) {
194 std::set<GCS::Constraint*>::const_iterator aRemIt = aRemovedTangent.begin();
195 for (; aRemIt != aRemovedTangent.end(); ++aRemIt)
196 if (!isTangentTruth(*aRemIt))
198 if (aRemIt != aRemovedTangent.end())
199 aResult = GCS::Failed;
202 // Add IDs of removed tangent to the list of conflicting constraints
203 if (aResult == GCS::Failed) {
204 std::set<GCS::Constraint*>::const_iterator aRemIt = aRemovedTangent.begin();
205 for (; aRemIt != aRemovedTangent.end(); ++aRemIt)
206 myConflictingIDs.insert((*aRemIt)->getTag());
212 bool PlaneGCSSolver_Solver::isTangentTruth(GCS::Constraint* theTangent) const
214 static const double aTol = 1e-5;
215 static const double aTol2 = aTol *aTol;
217 if (theTangent->getTypeId() == GCS::TangentCircumf) {
218 GCS::VEC_pD aParams = theTangent->params();
219 double dx = *(aParams[2]) - *(aParams[0]);
220 double dy = *(aParams[3]) - *(aParams[1]);
221 double aDist2 = dx * dx + dy * dy;
222 double aRadSum = *(aParams[4]) + *(aParams[5]);
223 double aRadDiff = *(aParams[4]) - *(aParams[5]);
224 return fabs(aDist2 - aRadSum * aRadSum) <= aTol2 * aDist2 ||
225 fabs(aDist2 - aRadDiff * aRadDiff) <= aTol2 * aDist2;
227 if (theTangent->getTypeId() == GCS::P2LDistance) {
228 GCS::VEC_pD aParams = theTangent->params();
229 double aDist2 = *(aParams[6]) * *(aParams[6]);
230 // orthogonal line direction
231 double aDirX = *(aParams[5]) - *(aParams[3]);
232 double aDirY = *(aParams[2]) - *(aParams[4]);
233 double aLen2 = aDirX * aDirX + aDirY * aDirY;
234 // vector from line's start to point
235 double aVecX = *(aParams[0]) - *(aParams[2]);
236 double aVecY = *(aParams[1]) - *(aParams[3]);
238 double aDot = aVecX * aDirX + aVecY * aDirY;
239 return fabs(aDot * aDot - aDist2 * aLen2) <= aTol2 * aLen2;
244 bool PlaneGCSSolver_Solver::isTangentTruth(int theTagID) const
246 ConstraintMap::const_iterator anIt = myConstraints.begin();
247 for (; anIt != myConstraints.end(); ++anIt)
248 if (anIt->first->getTag() == theTagID)
249 return isTangentTruth(anIt->first);
253 void PlaneGCSSolver_Solver::undo()
255 myEquationSystem->undoSolution();
258 bool PlaneGCSSolver_Solver::isConflicting(const ConstraintID& theConstraint) const
260 if (!myConfCollected)
261 const_cast<PlaneGCSSolver_Solver*>(this)->collectConflicting();
262 return myConflictingIDs.find((int)theConstraint) != myConflictingIDs.end();
265 void PlaneGCSSolver_Solver::collectConflicting()
267 GCS::VEC_I aConflict;
268 myEquationSystem->getConflicting(aConflict);
269 myConflictingIDs.insert(aConflict.begin(), aConflict.end());
271 myEquationSystem->getRedundant(aConflict);
272 myConflictingIDs.insert(aConflict.begin(), aConflict.end());
274 myConfCollected = true;
277 int PlaneGCSSolver_Solver::dof() const
279 return const_cast<PlaneGCSSolver_Solver*>(this)->myEquationSystem->dofsNumber();