Salome HOME
Merge branch 'Pre_2.8.0_development'
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_UpdateCoincidence.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include <PlaneGCSSolver_UpdateCoincidence.h>
22 #include <PlaneGCSSolver_EdgeWrapper.h>
23 #include <PlaneGCSSolver_PointWrapper.h>
24 #include <SketchSolver_Constraint.h>
25
26 #include <SketchPlugin_ConstraintCoincidence.h>
27 #include <SketchPlugin_ConstraintCollinear.h>
28 #include <SketchPlugin_ConstraintMiddle.h>
29
30 static bool hasSamePoint(const std::set<EntityWrapperPtr>& theList,
31                          const EntityWrapperPtr& thePoint);
32
33
34 void PlaneGCSSolver_UpdateCoincidence::attach(SketchSolver_Constraint* theObserver,
35                                               const std::string& theType)
36 {
37   if (theType == GROUP()) {
38     std::list<SketchSolver_Constraint*>::iterator aPlaceToAdd = myObservers.end();
39     // point-point coincidence is placed first,
40     // other constraints are sorted by their type
41     for (aPlaceToAdd = myObservers.begin(); aPlaceToAdd != myObservers.end(); ++aPlaceToAdd)
42       if ((*aPlaceToAdd)->getType() > theObserver->getType())
43         break;
44     myObservers.insert(aPlaceToAdd, theObserver);
45   } else
46     myNext->attach(theObserver, theType);
47 }
48
49 void PlaneGCSSolver_UpdateCoincidence::update(const FeaturePtr& theFeature)
50 {
51   if (theFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID() ||
52       theFeature->getKind() == SketchPlugin_ConstraintMiddle::ID() ||
53       theFeature->getKind() == SketchPlugin_ConstraintCollinear::ID()) {
54     myCoincident.clear();
55     // notify listeners and stop procesing
56     std::list<SketchSolver_Constraint*>::iterator anIt = myObservers.begin();
57     for (; anIt != myObservers.end(); ++anIt)
58       (*anIt)->notify(theFeature, this);
59   } else
60     myNext->update(theFeature);
61 }
62
63 static bool hasAnotherExternalPoint(const std::set<EntityWrapperPtr>& theCoincidences,
64                                     const EntityWrapperPtr& thePoint)
65 {
66   std::set<EntityWrapperPtr>::const_iterator anIt = theCoincidences.begin();
67   for (; anIt != theCoincidences.end(); ++anIt)
68     if ((*anIt)->type() == ENTITY_POINT && (*anIt)->isExternal() && *anIt != thePoint)
69       return true;
70   return false;
71 }
72
73 bool PlaneGCSSolver_UpdateCoincidence::addCoincidence(
74     const EntityWrapperPtr& theEntity1,
75     const EntityWrapperPtr& theEntity2)
76 {
77   bool isAccepted = true;
78   std::list<CoincidentEntities>::iterator aFound[2] = {
79     findGroupOfCoincidence(theEntity1),
80     findGroupOfCoincidence(theEntity2)
81   };
82
83   if (aFound[0] == myCoincident.end() && aFound[1] == myCoincident.end()) {
84     // new group of coincidence
85     myCoincident.push_back(CoincidentEntities(theEntity1, theEntity2));
86   } else if (aFound[0] == myCoincident.end()) {
87     isAccepted = addToGroupOfCoincidence(*aFound[1], theEntity1);
88   } else if (aFound[1] == myCoincident.end()) {
89     isAccepted = addToGroupOfCoincidence(*aFound[0], theEntity2);
90   } else if (aFound[0] == aFound[1]) { // same group => already coincident
91     isAccepted = false;
92   } else { // merge two groups
93     // first check the external points are equal
94     EntityWrapperPtr anExternal0 = aFound[0]->externalPoint();
95     EntityWrapperPtr anExternal1 = aFound[1]->externalPoint();
96     if (anExternal0 && anExternal1) {
97       std::set<EntityWrapperPtr> anExtList;
98       anExtList.insert(anExternal0);
99       if (hasSamePoint(anExtList, anExternal1)) {
100         // no need to add coincidence, because all points are
101         // already coincident to correct external points
102         isAccepted = false;
103       }
104     }
105
106     // merge
107     aFound[0]->merge(*aFound[1]);
108     myCoincident.erase(aFound[1]);
109   }
110
111   return isAccepted;
112 }
113
114 bool PlaneGCSSolver_UpdateCoincidence::isPointOnEntity(
115     const EntityWrapperPtr& thePoint,
116     const EntityWrapperPtr& theEntity)
117 {
118   std::list<CoincidentEntities>::iterator anIt = findGroupOfCoincidence(thePoint);
119   if (anIt == myCoincident.end())
120     return false;
121
122   if (anIt->isExist(theEntity))
123     return true;
124
125   if (theEntity->type() == ENTITY_LINE) {
126     std::shared_ptr<GCS::Line> aLine = std::dynamic_pointer_cast<GCS::Line>(
127         std::dynamic_pointer_cast<PlaneGCSSolver_EdgeWrapper>(theEntity)->entity());
128     return anIt->isExist(aLine->p1) || anIt->isExist(aLine->p2);
129   }
130   return false;
131 }
132
133 std::list<PlaneGCSSolver_UpdateCoincidence::CoincidentEntities>::iterator
134   PlaneGCSSolver_UpdateCoincidence::findGroupOfCoincidence(const EntityWrapperPtr& theEntity)
135 {
136   if (theEntity->type() != ENTITY_POINT)
137     return myCoincident.end();
138
139   std::list<CoincidentEntities>::iterator aFound = myCoincident.begin();
140   for (; aFound != myCoincident.end(); ++aFound)
141     if (aFound->isExist(theEntity))
142       break;
143   return aFound;
144 }
145
146 bool PlaneGCSSolver_UpdateCoincidence::addToGroupOfCoincidence(
147     CoincidentEntities& theGroup, const EntityWrapperPtr& theEntity)
148 {
149   if (theGroup.isExist(theEntity))
150     return false;
151   return theGroup.add(theEntity);
152 }
153
154
155
156
157
158 static const GCS::Point& toPoint(const EntityWrapperPtr& theEntity)
159 {
160   PointWrapperPtr aPoint = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
161   return *(aPoint->point());
162 }
163
164 static double squareDistance(const GCS::Point& thePoint1, const GCS::Point& thePoint2)
165 {
166   double dx = *thePoint1.x - *thePoint2.x;
167   double dy = *thePoint1.y - *thePoint2.y;
168   return dx * dx + dy * dy;
169 }
170
171 static bool hasSamePoint(const std::set<EntityWrapperPtr>& theList, const GCS::Point& thePoint)
172 {
173   std::set<EntityWrapperPtr>::const_iterator anIt = theList.begin();
174   for (; anIt != theList.end(); ++anIt)
175     if (squareDistance(thePoint, toPoint(*anIt)) < 1.e-14)
176       return true;
177   return false;
178 }
179
180 bool hasSamePoint(const std::set<EntityWrapperPtr>& theList,
181                   const EntityWrapperPtr& thePoint)
182 {
183   return hasSamePoint(theList, toPoint(thePoint));
184 }
185
186
187 PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::CoincidentEntities(
188     const EntityWrapperPtr& theEntity1,
189     const EntityWrapperPtr& theEntity2)
190 {
191   add(theEntity1);
192   add(theEntity2);
193 }
194
195 bool PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::add(
196     const EntityWrapperPtr& theEntity)
197 {
198   bool isAdded = true;
199   if (theEntity->type() == ENTITY_POINT) {
200     if (theEntity->isExternal()) {
201       isAdded = !hasSamePoint(myExternalPoints, theEntity);
202       myExternalPoints.insert(theEntity);
203     } else
204       myPoints.insert(theEntity);
205   } else
206     myFeatures.insert(theEntity);
207   return isAdded;
208 }
209
210 void PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::remove(
211     const EntityWrapperPtr& theEntity)
212 {
213   if (theEntity->type() == ENTITY_POINT) {
214     if (theEntity->isExternal())
215       myExternalPoints.erase(theEntity);
216     else
217       myPoints.erase(theEntity);
218   } else
219     myFeatures.erase(theEntity);
220 }
221
222 void PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::merge(
223     const CoincidentEntities& theOther)
224 {
225   myExternalPoints.insert(theOther.myExternalPoints.begin(), theOther.myExternalPoints.end());
226   myPoints.insert(theOther.myPoints.begin(), theOther.myPoints.end());
227   myFeatures.insert(theOther.myFeatures.begin(), theOther.myFeatures.end());
228 }
229
230 bool PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::isExist(
231     const EntityWrapperPtr& theEntity) const
232 {
233   if (theEntity->type() == ENTITY_POINT) {
234     if (theEntity->isExternal())
235       return myExternalPoints.find(theEntity) != myExternalPoints.end();
236     else
237       return myPoints.find(theEntity) != myPoints.end();
238   }
239
240   return myFeatures.find(theEntity) != myFeatures.end();
241 }
242
243 bool PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::isExist(
244     const GCS::Point& thePoint) const
245 {
246   return hasSamePoint(myExternalPoints, thePoint) || hasSamePoint(myPoints, thePoint);
247 }
248
249 EntityWrapperPtr PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::externalPoint() const
250 {
251   return myExternalPoints.empty() ? EntityWrapperPtr() : *myExternalPoints.begin();
252 }