1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include <PlaneGCSSolver_UpdateCoincidence.h>
22 #include <PlaneGCSSolver_EdgeWrapper.h>
23 #include <PlaneGCSSolver_PointWrapper.h>
24 #include <SketchSolver_Constraint.h>
26 #include <SketchPlugin_ConstraintCoincidence.h>
27 #include <SketchPlugin_ConstraintCollinear.h>
28 #include <SketchPlugin_ConstraintMiddle.h>
30 static bool hasSamePoint(const std::set<EntityWrapperPtr>& theList,
31 const EntityWrapperPtr& thePoint);
34 void PlaneGCSSolver_UpdateCoincidence::attach(SketchSolver_Constraint* theObserver,
35 const std::string& theType)
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())
44 myObservers.insert(aPlaceToAdd, theObserver);
46 myNext->attach(theObserver, theType);
49 void PlaneGCSSolver_UpdateCoincidence::update(const FeaturePtr& theFeature)
51 if (theFeature->getKind() == SketchPlugin_ConstraintCoincidence::ID() ||
52 theFeature->getKind() == SketchPlugin_ConstraintMiddle::ID() ||
53 theFeature->getKind() == SketchPlugin_ConstraintCollinear::ID()) {
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);
60 myNext->update(theFeature);
63 static bool hasAnotherExternalPoint(const std::set<EntityWrapperPtr>& theCoincidences,
64 const EntityWrapperPtr& thePoint)
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)
73 bool PlaneGCSSolver_UpdateCoincidence::addCoincidence(
74 const EntityWrapperPtr& theEntity1,
75 const EntityWrapperPtr& theEntity2)
77 bool isAccepted = true;
78 std::list<CoincidentEntities>::iterator aFound[2] = {
79 findGroupOfCoincidence(theEntity1),
80 findGroupOfCoincidence(theEntity2)
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
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
107 aFound[0]->merge(*aFound[1]);
108 myCoincident.erase(aFound[1]);
114 bool PlaneGCSSolver_UpdateCoincidence::isPointOnEntity(
115 const EntityWrapperPtr& thePoint,
116 const EntityWrapperPtr& theEntity)
118 std::list<CoincidentEntities>::iterator anIt = findGroupOfCoincidence(thePoint);
119 if (anIt == myCoincident.end())
122 if (anIt->isExist(theEntity))
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);
133 std::list<PlaneGCSSolver_UpdateCoincidence::CoincidentEntities>::iterator
134 PlaneGCSSolver_UpdateCoincidence::findGroupOfCoincidence(const EntityWrapperPtr& theEntity)
136 if (theEntity->type() != ENTITY_POINT)
137 return myCoincident.end();
139 std::list<CoincidentEntities>::iterator aFound = myCoincident.begin();
140 for (; aFound != myCoincident.end(); ++aFound)
141 if (aFound->isExist(theEntity))
146 bool PlaneGCSSolver_UpdateCoincidence::addToGroupOfCoincidence(
147 CoincidentEntities& theGroup, const EntityWrapperPtr& theEntity)
149 if (theGroup.isExist(theEntity))
151 return theGroup.add(theEntity);
158 static const GCS::Point& toPoint(const EntityWrapperPtr& theEntity)
160 PointWrapperPtr aPoint = std::dynamic_pointer_cast<PlaneGCSSolver_PointWrapper>(theEntity);
161 return *(aPoint->point());
164 static double squareDistance(const GCS::Point& thePoint1, const GCS::Point& thePoint2)
166 double dx = *thePoint1.x - *thePoint2.x;
167 double dy = *thePoint1.y - *thePoint2.y;
168 return dx * dx + dy * dy;
171 static bool hasSamePoint(const std::set<EntityWrapperPtr>& theList, const GCS::Point& thePoint)
173 std::set<EntityWrapperPtr>::const_iterator anIt = theList.begin();
174 for (; anIt != theList.end(); ++anIt)
175 if (squareDistance(thePoint, toPoint(*anIt)) < 1.e-14)
180 bool hasSamePoint(const std::set<EntityWrapperPtr>& theList,
181 const EntityWrapperPtr& thePoint)
183 return hasSamePoint(theList, toPoint(thePoint));
187 PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::CoincidentEntities(
188 const EntityWrapperPtr& theEntity1,
189 const EntityWrapperPtr& theEntity2)
195 bool PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::add(
196 const EntityWrapperPtr& theEntity)
199 if (theEntity->type() == ENTITY_POINT) {
200 if (theEntity->isExternal()) {
201 isAdded = !hasSamePoint(myExternalPoints, theEntity);
202 myExternalPoints.insert(theEntity);
204 myPoints.insert(theEntity);
206 myFeatures.insert(theEntity);
210 void PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::remove(
211 const EntityWrapperPtr& theEntity)
213 if (theEntity->type() == ENTITY_POINT) {
214 if (theEntity->isExternal())
215 myExternalPoints.erase(theEntity);
217 myPoints.erase(theEntity);
219 myFeatures.erase(theEntity);
222 void PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::merge(
223 const CoincidentEntities& theOther)
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());
230 bool PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::isExist(
231 const EntityWrapperPtr& theEntity) const
233 if (theEntity->type() == ENTITY_POINT) {
234 if (theEntity->isExternal())
235 return myExternalPoints.find(theEntity) != myExternalPoints.end();
237 return myPoints.find(theEntity) != myPoints.end();
240 return myFeatures.find(theEntity) != myFeatures.end();
243 bool PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::isExist(
244 const GCS::Point& thePoint) const
246 return hasSamePoint(myExternalPoints, thePoint) || hasSamePoint(myPoints, thePoint);
249 EntityWrapperPtr PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::externalPoint() const
251 return myExternalPoints.empty() ? EntityWrapperPtr() : *myExternalPoints.begin();