Salome HOME
Merge remote-tracking branch 'remotes/origin/HigherLevelObjectsHistory'
[modules/shaper.git] / src / SketchSolver / PlaneGCSSolver / PlaneGCSSolver_UpdateCoincidence.cpp
index 4ea868a110ee423e1700f9a5dac6c12509db3ee2..4ed6b81e992743639bcd0e6c310473ba7e71a9c5 100644 (file)
@@ -1,8 +1,21 @@
-// Copyright (C) 2014-20xx CEA/DEN, EDF R&D
-
-// File:    PlaneGCSSolver_UpdateCoincidence.cpp
-// Created: 17 Feb 2017
-// Author:  Artem ZHIDKOV
+// Copyright (C) 2014-2019  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
 
 #include <PlaneGCSSolver_UpdateCoincidence.h>
 #include <PlaneGCSSolver_EdgeWrapper.h>
 #include <SketchPlugin_ConstraintCollinear.h>
 #include <SketchPlugin_ConstraintMiddle.h>
 
+static bool hasSamePoint(const std::set<EntityWrapperPtr>& theList,
+                         const EntityWrapperPtr& thePoint);
+
+
 void PlaneGCSSolver_UpdateCoincidence::attach(SketchSolver_Constraint* theObserver,
                                               const std::string& theType)
 {
@@ -72,17 +89,21 @@ bool PlaneGCSSolver_UpdateCoincidence::addCoincidence(
   } else if (aFound[0] == aFound[1]) { // same group => already coincident
     isAccepted = false;
   } else { // merge two groups
-    EntityWrapperPtr anEntityToAdd = theEntity1;
-    if (theEntity1->isExternal()) { // swap found groups;
-      anEntityToAdd = theEntity2;
-      std::list<CoincidentEntities>::iterator aTempIt = aFound[0];
-      aFound[0] = aFound[1];
-      aFound[1] = aTempIt;
+    // first check the external points are equal
+    EntityWrapperPtr anExternal0 = aFound[0]->externalPoint();
+    EntityWrapperPtr anExternal1 = aFound[1]->externalPoint();
+    if (anExternal0 && anExternal1) {
+      std::set<EntityWrapperPtr> anExtList;
+      anExtList.insert(anExternal0);
+      if (hasSamePoint(anExtList, anExternal1)) {
+        // no need to add coincidence, because all points are
+        // already coincident to correct external points
+        isAccepted = false;
+      }
     }
 
-    aFound[1]->remove(anEntityToAdd);
+    // merge
     aFound[0]->merge(*aFound[1]);
-    isAccepted = aFound[0]->add(anEntityToAdd);
     myCoincident.erase(aFound[1]);
   }
 
@@ -126,9 +147,7 @@ bool PlaneGCSSolver_UpdateCoincidence::addToGroupOfCoincidence(
 {
   if (theGroup.isExist(theEntity))
     return false;
-
-  theGroup.add(theEntity);
-  return true;
+  return theGroup.add(theEntity);
 }
 
 
@@ -157,7 +176,8 @@ static bool hasSamePoint(const std::set<EntityWrapperPtr>& theList, const GCS::P
   return false;
 }
 
-static bool hasSamePoint(const std::set<EntityWrapperPtr>& theList, const EntityWrapperPtr& thePoint)
+bool hasSamePoint(const std::set<EntityWrapperPtr>& theList,
+                  const EntityWrapperPtr& thePoint)
 {
   return hasSamePoint(theList, toPoint(thePoint));
 }
@@ -224,3 +244,8 @@ bool PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::isExist(
 {
   return hasSamePoint(myExternalPoints, thePoint) || hasSamePoint(myPoints, thePoint);
 }
+
+EntityWrapperPtr PlaneGCSSolver_UpdateCoincidence::CoincidentEntities::externalPoint() const
+{
+  return myExternalPoints.empty() ? EntityWrapperPtr() : *myExternalPoints.begin();
+}