Salome HOME
Update copyrights
[modules/shaper.git] / src / SketchSolver / SketchSolver_ConstraintCoincidence.cpp
index 1172988cba921d4ba5e83eab6c9d0627ca88789f..9903b850efdb4ab8bee4b67503465c0bd7419456 100644 (file)
+// 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 <SketchSolver_ConstraintCoincidence.h>
 #include <SketchSolver_Error.h>
-#include <SketchSolver_Group.h>
+#include <PlaneGCSSolver_Tools.h>
+#include <PlaneGCSSolver_UpdateCoincidence.h>
 
-#include <map>
+#include <SketchPlugin_Arc.h>
+#include <SketchPlugin_Line.h>
 
-bool SketchSolver_ConstraintCoincidence::hasConstraint(ConstraintPtr theConstraint) const
+static void getCoincidentFeatureExtremities(const ConstraintPtr& theConstraint,
+                                            const StoragePtr& theStorage,
+                                            EntityWrapperPtr theExtremities[2])
 {
-  if (myBaseConstraint == theConstraint)
-    return true;
-  std::map<Slvs_hConstraint, ConstraintPtr>::const_iterator anIt = myExtraCoincidence.begin();
-  for (; anIt != myExtraCoincidence.end(); anIt++)
-    if (anIt->second == theConstraint)
-      return true;
-  return false;
-}
+  for (int i = 0; i < CONSTRAINT_ATTR_SIZE; ++i) {
+    AttributeRefAttrPtr aRefAttr = theConstraint->refattr(SketchPlugin_Constraint::ATTRIBUTE(i));
+    if (!aRefAttr || !aRefAttr->isObject())
+      continue;
 
-std::list<ConstraintPtr> SketchSolver_ConstraintCoincidence::constraints() const
-{
-  std::list<ConstraintPtr> aConstraints;
-  aConstraints.push_back(myBaseConstraint);
-  std::map<Slvs_hConstraint, ConstraintPtr>::const_iterator anIt = myExtraCoincidence.begin();
-  for (; anIt != myExtraCoincidence.end(); anIt++)
-    aConstraints.push_back(anIt->second);
-  return aConstraints;
-}
+    FeaturePtr aFeature = ModelAPI_Feature::feature(aRefAttr->object());
+    if (!aFeature)
+      continue;
 
-bool SketchSolver_ConstraintCoincidence::isCoincide(
-    std::shared_ptr<SketchSolver_ConstraintCoincidence> theConstraint) const
-{
-  std::map<FeaturePtr, Slvs_hEntity>::const_iterator aFeatIter = myFeatureMap.begin();
-  for (; aFeatIter != myFeatureMap.end(); aFeatIter++)
-    if (theConstraint->myFeatureMap.find(aFeatIter->first) != theConstraint->myFeatureMap.end())
-      return true;
-  std::map<AttributePtr, Slvs_hEntity>::const_iterator anAttrIter = myAttributeMap.begin();
-  for (; anAttrIter != myAttributeMap.end(); anAttrIter++)
-    if (theConstraint->myAttributeMap.find(anAttrIter->first) != theConstraint->myAttributeMap.end())
-      return true;
-  return false;
+    if (aFeature->getKind() == SketchPlugin_Line::ID()) {
+      theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::START_ID()));
+      theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Line::END_ID()));
+    } else if (aFeature->getKind() == SketchPlugin_Arc::ID()) {
+      theExtremities[0] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::START_ID()));
+      theExtremities[1] = theStorage->entity(aFeature->attribute(SketchPlugin_Arc::END_ID()));
+    }
+  }
 }
 
-void SketchSolver_ConstraintCoincidence::attach(
-    std::shared_ptr<SketchSolver_ConstraintCoincidence> theConstraint)
+
+void SketchSolver_ConstraintCoincidence::process()
 {
   cleanErrorMsg();
-  Slvs_Constraint aBaseCoincidence = myStorage->getConstraint(mySlvsConstraints.front());
-  // Remove constraints from theConstraint
-  std::vector<Slvs_hConstraint>::iterator aCIter = theConstraint->mySlvsConstraints.begin();
-  for (; aCIter != theConstraint->mySlvsConstraints.end(); aCIter++)
-    theConstraint->myStorage->removeConstraint(*aCIter);
-
-  if (myStorage == theConstraint->myStorage) {
-    // Clean removed items
-    std::set<Slvs_hParam> aRemParams;
-    std::set<Slvs_hEntity> aRemEnts;
-    std::set<Slvs_hConstraint> aRemConstr;
-    theConstraint->myStorage->getRemoved(aRemParams, aRemEnts, aRemConstr);
+  if (!myBaseConstraint || !myStorage) {
+    // Not enough parameters are assigned
+    return;
   }
 
-  // Copy data.
-  addConstraint(theConstraint->myBaseConstraint);
-  std::map<Slvs_hConstraint, ConstraintPtr>::iterator aConstrIter =
-      theConstraint->myExtraCoincidence.begin();
-  for (; aConstrIter != theConstraint->myExtraCoincidence.end(); aConstrIter++)
-    addConstraint(aConstrIter->second);
-  // Clear the lists to not remove the entities on destruction
-  theConstraint->mySlvsConstraints.clear();
-  theConstraint->myFeatureMap.clear();
-  theConstraint->myAttributeMap.clear();
+  EntityWrapperPtr aValue;
+  std::vector<EntityWrapperPtr> anAttributes;
+  getAttributes(aValue, anAttributes);
+  if (!myErrorMsg.empty())
+    return;
+  if (anAttributes.empty()) {
+    myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
+    return;
+  }
+
+  mySolverConstraint = PlaneGCSSolver_Tools::createConstraint(
+      myBaseConstraint, getType(),
+      aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
+
+  myStorage->subscribeUpdates(this, PlaneGCSSolver_UpdateCoincidence::GROUP());
+  myStorage->notify(myBaseConstraint);
 }
 
-Slvs_hConstraint SketchSolver_ConstraintCoincidence::addConstraint(
-    Slvs_hEntity thePoint1, Slvs_hEntity thePoint2)
+bool SketchSolver_ConstraintCoincidence::remove()
 {
-  Slvs_Constraint aNewConstraint = Slvs_MakeConstraint(SLVS_E_UNKNOWN, myGroup->getId(),
-      SLVS_C_POINTS_COINCIDENT, myGroup->getWorkplaneId(), 0.0, thePoint1, thePoint2, 
-      SLVS_E_UNKNOWN, SLVS_E_UNKNOWN);
-  Slvs_hConstraint aNewID = myStorage->addConstraint(aNewConstraint);
-  mySlvsConstraints.push_back(aNewID);
-  return aNewID;
+  myInSolver = false;
+  myFeatureExtremities[0] = EntityWrapperPtr();
+  myFeatureExtremities[1] = EntityWrapperPtr();
+  return SketchSolver_Constraint::remove();
 }
 
-void SketchSolver_ConstraintCoincidence::addConstraint(ConstraintPtr theConstraint)
+void SketchSolver_ConstraintCoincidence::getAttributes(
+    EntityWrapperPtr& theValue,
+    std::vector<EntityWrapperPtr>& theAttributes)
 {
-  std::list<AttributePtr> anAttrList =
-      theConstraint->data()->attributes(ModelAPI_AttributeRefAttr::typeId());
-  std::list<AttributePtr>::iterator anIter = anAttrList.begin();
-  std::vector<Slvs_hEntity> anEntities;
-  for (; anIter != anAttrList.end(); anIter++) {
-    AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
-    if (!aRefAttr || aRefAttr->isObject() ||
-        myAttributeMap.find(aRefAttr->attr()) != myAttributeMap.end())
-      continue;
-    int aType;
-    Slvs_hEntity anEntityID = myGroup->getAttributeId(aRefAttr->attr());
-    if (anEntityID == SLVS_E_UNKNOWN)
-      anEntityID = changeEntity(aRefAttr->attr(), aType);
-    anEntities.push_back(anEntityID);
+  SketchSolver_Constraint::getAttributes(theValue, theAttributes);
+  if (!myErrorMsg.empty() || !theAttributes[0]) {
+    theAttributes.clear();
+    return;
   }
 
-  Slvs_Constraint aBaseCoincidence = myStorage->getConstraint(mySlvsConstraints.front());
-  Slvs_hConstraint aNewConstr = SLVS_E_UNKNOWN;
-  std::vector<Slvs_hEntity>::iterator anEntIter = anEntities.begin();
-  for (; anEntIter != anEntities.end(); anEntIter++)
-    aNewConstr = addConstraint(aBaseCoincidence.ptA, *anEntIter);
-  myExtraCoincidence[aNewConstr] = theConstraint;
+  if (theAttributes[1])
+    myType = CONSTRAINT_PT_PT_COINCIDENT;
+  else if (theAttributes[2]) {
+    // check the type of entity (line or circle)
+    SketchSolver_EntityType anEntType = theAttributes[2]->type();
+    if (anEntType == ENTITY_LINE)
+      myType = CONSTRAINT_PT_ON_LINE;
+    else if (anEntType == ENTITY_CIRCLE || anEntType == ENTITY_ARC)
+      myType = CONSTRAINT_PT_ON_CIRCLE;
+    else
+      myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
+
+    // obtain extremity points of the coincident feature for further checking of multi-coincidence
+    getCoincidentFeatureExtremities(myBaseConstraint, myStorage, myFeatureExtremities);
+  } else
+    myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
 }
 
-bool SketchSolver_ConstraintCoincidence::remove(ConstraintPtr theConstraint)
+void SketchSolver_ConstraintCoincidence::notify(const FeaturePtr&      theFeature,
+                                                PlaneGCSSolver_Update* theUpdater)
 {
-  cleanErrorMsg();
-  if (mySlvsConstraints.empty())
-    return true;
-  ConstraintPtr aConstraint = theConstraint ? theConstraint : myBaseConstraint;
-  int aPos = -1; // position of constraint in the list (-1 for base constraint)
-  std::map<Slvs_hConstraint, ConstraintPtr>::iterator anExtraIt;
-  if (aConstraint != myBaseConstraint) {
-    anExtraIt = myExtraCoincidence.begin();
-    for (aPos = 0; anExtraIt != myExtraCoincidence.end(); anExtraIt++, aPos++)
-      if (anExtraIt->second == aConstraint)
-        break;
-    if (aPos >= (int)myExtraCoincidence.size())
-      return false; // there is no constraint, which is specified to remove
-    else {
-      bool isEmpty = anExtraIt->first == SLVS_E_UNKNOWN;
-      myExtraCoincidence.erase(anExtraIt);
-      if (isEmpty)
-        return false;
-    }
-  }
+  PlaneGCSSolver_UpdateCoincidence* anUpdater =
+      static_cast<PlaneGCSSolver_UpdateCoincidence*>(theUpdater);
+  bool isAccepted = anUpdater->addCoincidence(myAttributes.front(), myAttributes.back());
 
-  bool isFullyRemoved = myStorage->removeConstraint(mySlvsConstraints[aPos+1]);
-  mySlvsConstraints.erase(mySlvsConstraints.begin() + (1+aPos));
-  if (aPos < 0 && !myExtraCoincidence.empty()) {
-    anExtraIt = myExtraCoincidence.begin();
-    // Remove invalid constraints
-    while (anExtraIt != myExtraCoincidence.end()) {
-      if (!anExtraIt->second->data() || !anExtraIt->second->data()->isValid()) {
-        std::map<Slvs_hConstraint, ConstraintPtr>::iterator aTempIt = anExtraIt++;
-        if (aTempIt->first != SLVS_E_UNKNOWN) {
-          myStorage->removeConstraint(aTempIt->first);
-          std::vector<Slvs_hConstraint>::iterator anIt = mySlvsConstraints.begin();
-          for (; anIt != mySlvsConstraints.end(); anIt++)
-            if (*anIt == aTempIt->first) {
-              mySlvsConstraints.erase(anIt);
-              break;
-            }
-        }
-        myExtraCoincidence.erase(aTempIt);
-        continue;
-      }
-      anExtraIt++;
-    }
-    // Find first non-extra conststraint
-    while (anExtraIt != myExtraCoincidence.end() && anExtraIt->first == SLVS_E_UNKNOWN)
-      anExtraIt++;
-    if (anExtraIt != myExtraCoincidence.end()) {
-      // Need to specify another base coincidence constraint
-      myBaseConstraint = anExtraIt->second;
-      myExtraCoincidence.erase(anExtraIt);
-      std::vector<Slvs_hConstraint>::iterator aCIter = mySlvsConstraints.begin();
-      Slvs_Constraint aBase = myStorage->getConstraint(*aCIter);
-      for (++aCIter; aCIter != mySlvsConstraints.end(); aCIter++) {
-        Slvs_Constraint aConstr = myStorage->getConstraint(*aCIter);
-        aConstr.ptA = aBase.ptA;
-        myStorage->updateConstraint(aConstr);
-      }
-    }
-  }
-  // Clear removed attributes
-  std::set<Slvs_hParam> aParamRemoved;
-  std::set<Slvs_hEntity> anEntRemoved;
-  std::set<Slvs_hConstraint> aConstrRemoved;
-  myStorage->getRemoved(aParamRemoved, anEntRemoved, aConstrRemoved);
-  std::map<AttributePtr, Slvs_hEntity>::iterator anAttrIter = myAttributeMap.begin();
-  while (anAttrIter != myAttributeMap.end()) {
-    if (anEntRemoved.find(anAttrIter->second) != anEntRemoved.end()) {
-      std::map<AttributePtr, Slvs_hEntity>::iterator aTempIt = anAttrIter++;
-      myAttributeMap.erase(aTempIt);
-      continue;
-    }
-    anAttrIter++;
+  // additionally check the point is coincident to extremity of coincident feature
+  if (myFeatureExtremities[0] && myFeatureExtremities[1]) {
+    EntityWrapperPtr aPoint =
+        myAttributes.front()->type() == ENTITY_POINT ? myAttributes.front() : myAttributes.back();
+
+    for (int i = 0; i < 2; ++i)
+      isAccepted = isAccepted && !anUpdater->isPointOnEntity(aPoint, myFeatureExtremities[i]);
   }
 
-  // Go through remaining extra coincidence and try to add or remove them
-  anExtraIt = myExtraCoincidence.begin();
-  while (anExtraIt != myExtraCoincidence.end()) {
-    if (anExtraIt->first == SLVS_E_UNKNOWN) {
-      if (!anExtraIt->second->data() || !anExtraIt->second->data()->isValid()) {
-        std::map<Slvs_hConstraint, ConstraintPtr>::iterator aTempIt = anExtraIt++;
-        myExtraCoincidence.erase(aTempIt);
-        continue;
-      }
-      if (mySlvsConstraints.empty()) {
-        myBaseConstraint = anExtraIt->second;
-        std::map<Slvs_hConstraint, ConstraintPtr>::iterator aTempIt = anExtraIt++;
-        myExtraCoincidence.erase(aTempIt);
-        process();
-        continue;
-      } else
-        addConstraint(anExtraIt->second);
+  if (isAccepted) {
+    if (!myInSolver) {
+      myInSolver = true;
+      myStorage->addConstraint(myBaseConstraint, mySolverConstraint);
+    }
+  } else {
+    if (myInSolver) {
+      myInSolver = false;
+      myStorage->removeConstraint(myBaseConstraint);
     }
-    anExtraIt++;
   }
-  return isFullyRemoved;
 }
-