Salome HOME
Process redundant constraints reported by PlaneGCS as conflicting.
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_UpdateCoincidence.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:    PlaneGCSSolver_UpdateCoincidence.cpp
4 // Created: 17 Feb 2017
5 // Author:  Artem ZHIDKOV
6
7 #include <PlaneGCSSolver_UpdateCoincidence.h>
8 #include <SketchSolver_Constraint.h>
9
10 #include <SketchPlugin_ConstraintCoincidence.h>
11 #include <SketchPlugin_ConstraintMiddle.h>
12
13 void PlaneGCSSolver_UpdateCoincidence::attach(SketchSolver_Constraint* theObserver,
14                                               const std::string& theType)
15 {
16   if (theType == GROUP()) {
17     std::list<SketchSolver_Constraint*>::iterator aPlaceToAdd = myObservers.end();
18     // point-point coincidence is placed first
19     if (theObserver->getType() == CONSTRAINT_PT_PT_COINCIDENT) {
20       for (aPlaceToAdd = myObservers.begin(); aPlaceToAdd != myObservers.end(); ++aPlaceToAdd)
21         if ((*aPlaceToAdd)->getType() != CONSTRAINT_PT_PT_COINCIDENT)
22           break;
23     }
24     myObservers.insert(aPlaceToAdd, theObserver);
25   } else
26     myNext->attach(theObserver, theType);
27 }
28
29 void PlaneGCSSolver_UpdateCoincidence::update(const FeaturePtr& theFeature)
30 {
31   if (theFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID() ||
32       theFeature->getKind() == SketchPlugin_ConstraintMiddle::ID()) {
33     myCoincident.clear();
34     // notify listeners and stop procesing
35     std::list<SketchSolver_Constraint*>::iterator anIt = myObservers.begin();
36     for (; anIt != myObservers.end(); ++anIt)
37       (*anIt)->notify(theFeature, this);
38   } else
39     myNext->update(theFeature);
40 }
41
42 static bool hasExternalPoint(const std::set<EntityWrapperPtr>& theCoincidences)
43 {
44   std::set<EntityWrapperPtr>::const_iterator anIt = theCoincidences.begin();
45   for (; anIt != theCoincidences.end(); ++anIt)
46     if ((*anIt)->type() == ENTITY_POINT && (*anIt)->isExternal())
47       return true;
48   return false;
49 }
50
51 bool PlaneGCSSolver_UpdateCoincidence::checkCoincidence(
52     const EntityWrapperPtr& theEntity1,
53     const EntityWrapperPtr& theEntity2)
54 {
55   bool isAccepted = true;
56
57   std::list<std::set<EntityWrapperPtr> >::iterator anIt = myCoincident.begin();
58   std::list<std::set<EntityWrapperPtr> >::iterator aFound1 = myCoincident.end();
59   std::list<std::set<EntityWrapperPtr> >::iterator aFound2 = myCoincident.end();
60   for (; anIt != myCoincident.end(); ++anIt) {
61     if (aFound1 == myCoincident.end() && anIt->find(theEntity1) != anIt->end())
62       aFound1 = anIt;
63     if (aFound2 == myCoincident.end() && anIt->find(theEntity2) != anIt->end())
64       aFound2 = anIt;
65     if (aFound1 != myCoincident.end() && aFound2 != myCoincident.end())
66       break;
67   }
68
69   if (aFound1 == myCoincident.end() && aFound2 == myCoincident.end()) {
70     // new group of coincidence
71     std::set<EntityWrapperPtr> aNewCoinc;
72     aNewCoinc.insert(theEntity1);
73     aNewCoinc.insert(theEntity2);
74     myCoincident.push_back(aNewCoinc);
75   } else if (aFound1 == aFound2) // same group => already coincident
76     isAccepted = false;
77   else {
78     if (theEntity1->type() == ENTITY_POINT && theEntity2->type() == ENTITY_POINT &&
79        (theEntity1->isExternal() || theEntity2->isExternal())) {
80       bool hasExternal = aFound1 != myCoincident.end() && hasExternalPoint(*aFound1);
81       hasExternal = hasExternal || (aFound2 != myCoincident.end() && hasExternalPoint(*aFound2));
82       if (hasExternal)
83         isAccepted = false;
84     }
85
86     if (aFound1 == myCoincident.end())
87       aFound2->insert(theEntity1);
88     else if (aFound2 == myCoincident.end())
89       aFound1->insert(theEntity2);
90     else { // merge two groups
91       aFound1->insert(aFound2->begin(), aFound2->end());
92       myCoincident.erase(aFound2);
93     }
94   }
95
96   return isAccepted;
97 }