Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into azv/SketchSolver_Refactoring
[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 bool PlaneGCSSolver_UpdateCoincidence::checkCoincidence(
43     const EntityWrapperPtr& theEntity1,
44     const EntityWrapperPtr& theEntity2)
45 {
46   bool isAccepted = true;
47
48   std::list<std::set<EntityWrapperPtr> >::iterator anIt = myCoincident.begin();
49   std::list<std::set<EntityWrapperPtr> >::iterator aFound1 = myCoincident.end();
50   std::list<std::set<EntityWrapperPtr> >::iterator aFound2 = myCoincident.end();
51   for (; anIt != myCoincident.end(); ++anIt) {
52     if (aFound1 == myCoincident.end() && anIt->find(theEntity1) != anIt->end())
53       aFound1 = anIt;
54     if (aFound2 == myCoincident.end() && anIt->find(theEntity2) != anIt->end())
55       aFound2 = anIt;
56     if (aFound1 != myCoincident.end() && aFound2 != myCoincident.end())
57       break;
58   }
59
60   if (aFound1 == myCoincident.end() && aFound2 == myCoincident.end()) {
61     // new group of coincidence
62     std::set<EntityWrapperPtr> aNewCoinc;
63     aNewCoinc.insert(theEntity1);
64     aNewCoinc.insert(theEntity2);
65     myCoincident.push_back(aNewCoinc);
66   } else if (aFound1 == aFound2) // same group => already coincident
67     isAccepted = false;
68   else if (aFound1 == myCoincident.end())
69     aFound2->insert(theEntity1);
70   else if (aFound2 == myCoincident.end())
71     aFound1->insert(theEntity2);
72   else { // merge two groups
73     aFound1->insert(aFound2->begin(), aFound2->end());
74     myCoincident.erase(aFound2);
75   }
76
77   return isAccepted;
78 }