Salome HOME
Issue #1299, Issue #1393 Angle constraint: support of additional and complementary...
[modules/shaper.git] / src / SketchSolver / SketchSolver_Constraint.cpp
index caccc5223b1303460ed3fd6dd265e4cd728c4c9c..fcb2a1fc138371fb94e349c6886f9cd1b60249a1 100644 (file)
-// File:    SketchSolver_Constraint.cpp
-// Created: 27 May 2014
-// Author:  Artem ZHIDKOV
-
-#include "SketchSolver_Constraint.h"
-#include <SketchSolver_Solver.h>
+#include <SketchSolver_Constraint.h>
+#include <SketchSolver_Group.h>
+#include <SketchSolver_Error.h>
+#include <SketchSolver_Manager.h>
 
+#include <SketchPlugin_Arc.h>
+#include <SketchPlugin_Circle.h>
 #include <SketchPlugin_Line.h>
 #include <SketchPlugin_Point.h>
-#include <SketchPlugin_Circle.h>
-#include <SketchPlugin_Arc.h>
+
+#include <SketchPlugin_ConstraintAngle.h>
 #include <SketchPlugin_ConstraintCoincidence.h>
+#include <SketchPlugin_ConstraintCollinear.h>
 #include <SketchPlugin_ConstraintDistance.h>
+#include <SketchPlugin_ConstraintEqual.h>
+#include <SketchPlugin_ConstraintHorizontal.h>
 #include <SketchPlugin_ConstraintLength.h>
+#include <SketchPlugin_ConstraintMiddle.h>
+#include <SketchPlugin_ConstraintMirror.h>
 #include <SketchPlugin_ConstraintParallel.h>
 #include <SketchPlugin_ConstraintPerpendicular.h>
 #include <SketchPlugin_ConstraintRadius.h>
 #include <SketchPlugin_ConstraintRigid.h>
+#include <SketchPlugin_ConstraintTangent.h>
+#include <SketchPlugin_ConstraintVertical.h>
 
-#include <ModelAPI_AttributeRefAttr.h>
-#include <ModelAPI_Data.h>
-#include <ModelAPI_Document.h>
-#include <ModelAPI_Object.h>
-#include <ModelAPI_ResultConstruction.h>
-
+#include <GeomAPI_Dir2d.h>
 #include <GeomDataAPI_Point.h>
 #include <GeomDataAPI_Point2D.h>
-#include <GeomAPI_Edge.h>
-#include <GeomAPI_Shape.h>
+#include <ModelAPI_AttributeDouble.h>
+#include <ModelAPI_ResultConstruction.h>
 
-/// Possible types of attributes (used to determine constraint type)
-enum AttrType
+#include <math.h>
+
+SketchSolver_Constraint::SketchSolver_Constraint(
+    ConstraintPtr  theConstraint)
+  : myBaseConstraint(theConstraint),
+    myGroupID(GID_UNKNOWN),
+    myType(CONSTRAINT_UNKNOWN)
 {
-  UNKNOWN,  // Something wrong during type determination
-  POINT2D,
-  POINT3D,
-  LINE,
-  CIRCLE,
-  ARC
-};
-
-/// Calculate type of the attribute
-static AttrType typeOfAttribute(std::shared_ptr<ModelAPI_Attribute> theAttribute);
-
-SketchSolver_Constraint::SketchSolver_Constraint()
-    : myConstraint(std::shared_ptr<SketchPlugin_Constraint>()),
-      myType(SLVS_C_UNKNOWN),
-      myAttributesList()
+}
+
+void SketchSolver_Constraint::process(StoragePtr theStorage,
+                                      const GroupID& theGroupID,
+                                      const EntityID& theSketchID)
 {
+  myStorage = theStorage;
+  myGroupID = theGroupID;
+  mySketchID = theSketchID;
+  // Process constraint according to its type
+  process();
 }
 
-SketchSolver_Constraint::SketchSolver_Constraint(
-    std::shared_ptr<SketchPlugin_Constraint> theConstraint)
-    : myConstraint(theConstraint),
-      myAttributesList()
+
+SketchSolver_ConstraintType SketchSolver_Constraint::TYPE(ConstraintPtr theConstraint)
 {
-  myType = getType(myConstraint);
+  const std::string& aType = theConstraint->getKind();
+  if (aType == SketchPlugin_ConstraintCoincidence::ID())
+    return CONSTRAINT_COINCIDENCE;
+  else if (aType == SketchPlugin_ConstraintRigid::ID())
+    return CONSTRAINT_FIXED;
+  else if (aType == SketchPlugin_ConstraintHorizontal::ID())
+    return CONSTRAINT_HORIZONTAL;
+  else if (aType == SketchPlugin_ConstraintVertical::ID())
+    return CONSTRAINT_VERTICAL;
+  else if (aType == SketchPlugin_ConstraintAngle::ID())
+    return CONSTRAINT_ANGLE;
+  else if (aType == SketchPlugin_ConstraintDistance::ID())
+    return CONSTRAINT_DISTANCE;
+  else if (aType == SketchPlugin_ConstraintEqual::ID())
+    return CONSTRAINT_EQUAL;
+  else if (aType == SketchPlugin_ConstraintLength::ID())
+    return CONSTRAINT_PT_PT_DISTANCE;
+  else if (aType == SketchPlugin_ConstraintMirror::ID())
+    return CONSTRAINT_SYMMETRIC;
+  else if (aType == SketchPlugin_ConstraintParallel::ID())
+    return CONSTRAINT_PARALLEL;
+  else if (aType == SketchPlugin_ConstraintPerpendicular::ID())
+    return CONSTRAINT_PERPENDICULAR;
+  else if (aType == SketchPlugin_ConstraintRadius::ID())
+    return CONSTRAINT_RADIUS;
+  else if (aType == SketchPlugin_ConstraintTangent::ID())
+    return CONSTRAINT_TANGENT;
+  else if (aType == SketchPlugin_ConstraintCollinear::ID())
+    return CONSTRAINT_COLLINEAR;
+  else if (aType == SketchPlugin_ConstraintMiddle::ID())
+    return CONSTRAINT_MIDDLE_POINT;
+  return CONSTRAINT_UNKNOWN;
 }
 
-const int& SketchSolver_Constraint::getType(
-    std::shared_ptr<SketchPlugin_Constraint> theConstraint)
+void SketchSolver_Constraint::process()
 {
-  myType = SLVS_C_UNKNOWN;
-  if (!theConstraint)
-    return getType();
-
-  DataPtr aConstrData = theConstraint->data();
-  if (!aConstrData || !aConstrData->isValid())
-    return getType();
-
-  // Assign empty names of attributes
-  myAttributesList.clear();
-  for (int i = 0; i < CONSTRAINT_ATTR_SIZE; i++)
-    myAttributesList.push_back(std::string());
-
-  const std::string& aConstraintKind = theConstraint->getKind();
-  // Constraint for coincidence of two points
-  if (aConstraintKind.compare(SketchPlugin_ConstraintCoincidence::ID()) == 0) {
-    int anAttrPos = 0;
-    // Verify the constraint has only two attributes and they are points
-    int aPt2d = 0;  // bit-mapped field, each bit indicates whether the attribute is 2D point
-    int aPt3d = 0;  // bit-mapped field, the same information for 3D points
-    for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
-      std::shared_ptr<ModelAPI_Attribute> anAttr = 
-          aConstrData->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
-      if (!anAttr)
-        continue;
-      switch (typeOfAttribute(anAttr)) {
-        case POINT2D:  // the attribute is a 2D point
-          aPt2d |= (1 << indAttr);
-          myAttributesList[anAttrPos++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
-          break;
-        case POINT3D:  // the attribute is a 3D point
-          aPt3d |= (1 << indAttr);
-          myAttributesList[anAttrPos++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
-          break;
-        default:
-          // Attribute neither 2D nor 3D point is not supported by this type of constraint
-          return getType();
-      }
-    }
-    // The constrained points should be in first and second positions,
-    // so the expected value of aPt2d or aPt3d is 3
-    if ((aPt2d == 3 && aPt3d == 0) || (aPt2d == 0 && aPt3d == 3))
-      myType = SLVS_C_POINTS_COINCIDENT;
-    // Constraint parameters are wrong
-    return getType();
+  cleanErrorMsg();
+  if (!myBaseConstraint || !myStorage || myGroupID == GID_UNKNOWN) {
+    // Not enough parameters are assigned
+    return;
   }
 
-  // Constraint for distance between point and another entity
-  if (aConstraintKind.compare(SketchPlugin_ConstraintDistance::ID()) == 0) {
-    int aNbPoints = 0;
-    int aNbEntities = 0;
-    for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
-      std::shared_ptr<ModelAPI_Attribute> anAttr = 
-          aConstrData->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
-      switch (typeOfAttribute(anAttr)) {
-        case POINT2D:
-        case POINT3D:
-          myAttributesList[aNbPoints++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
-          break;
-        case LINE:
-          // entities are placed starting from SketchPlugin_Constraint::ENTITY_C() attribute
-          myAttributesList[2 + aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
-          myType = SLVS_C_PT_LINE_DISTANCE;
-          break;
-      }
-    }
-    // Verify the correctness of constraint arguments
-    if (aNbPoints == 2 && aNbEntities == 0)
-      myType = SLVS_C_PT_PT_DISTANCE;
-    else if (aNbPoints != 1 || aNbEntities != 1)
-      myType = SLVS_C_UNKNOWN;
-    return getType();
+  SketchSolver_ConstraintType aConstrType = getType();
+  double aValue;
+  std::vector<EntityWrapperPtr> anAttributes;
+  getAttributes(aValue, anAttributes);
+  if (!myErrorMsg.empty())
+    return;
+  if (anAttributes.empty()) {
+    myErrorMsg = SketchSolver_Error::INCORRECT_ATTRIBUTE();
+    return;
   }
+  if (aConstrType == CONSTRAINT_UNKNOWN)
+    aConstrType = getType();
+
+  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
+  std::list<ConstraintWrapperPtr> aNewConstraints = aBuilder->createConstraint(
+      myBaseConstraint, myGroupID, mySketchID, aConstrType,
+      aValue, anAttributes[0], anAttributes[1], anAttributes[2], anAttributes[3]);
+  myStorage->addConstraint(myBaseConstraint, aNewConstraints);
+
+  adjustConstraint();
+}
+
+void SketchSolver_Constraint::update()
+{
+  cleanErrorMsg();
+  std::list<ConstraintWrapperPtr> aWrapper = myStorage->constraint(myBaseConstraint);
+  std::list<ConstraintWrapperPtr>::iterator aWIt = aWrapper.begin();
 
-  // Constraint for the given length of a line
-  if (aConstraintKind.compare(SketchPlugin_ConstraintLength::ID()) == 0) {
-    int aNbLines = 0;
-    for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
-      std::shared_ptr<ModelAPI_Attribute> anAttr = 
-          aConstrData->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
-      if (typeOfAttribute(anAttr) == LINE)
-        myAttributesList[aNbLines++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
+  // Check if attributes of constraint are changed, rebuild constraint
+  std::set<AttributePtr> anAttributes;
+  std::set<AttributePtr>::iterator aFoundAttr;
+  std::set<FeaturePtr> aFeatures;
+  std::set<FeaturePtr>::iterator aFoundFeat;
+  for (int anEntIndex = 0; anEntIndex < 4; ++anEntIndex) {
+    AttributePtr anAttr =
+        myBaseConstraint->attribute(SketchPlugin_Constraint::ATTRIBUTE(anEntIndex));
+    if (!anAttr)
+      continue;
+
+    AttributeRefAttrPtr aRefAttr = std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttr);
+    if (aRefAttr) {
+      if (aRefAttr->isObject()) {
+        FeaturePtr aFeat = ModelAPI_Feature::feature(aRefAttr->object());
+        if (myBaseConstraint->getKind() != SketchPlugin_ConstraintLength::ID())
+          aFeatures.insert(aFeat);
+        else {
+          // Workaround for the Length constraint: add points of line, not line itself
+          anAttributes.insert(aFeat->attribute(SketchPlugin_Line::START_ID()));
+          anAttributes.insert(aFeat->attribute(SketchPlugin_Line::END_ID()));
+        }
+      } else
+        anAttributes.insert(aRefAttr->attr());
+    } else
+      anAttributes.insert(anAttr);
+  }
+  bool hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
+  for (; hasNewAttr && aWIt != aWrapper.end(); ++ aWIt) {
+    const std::list<EntityWrapperPtr>& aSubs = (*aWIt)->entities();
+    std::list<EntityWrapperPtr>::const_iterator aSIt = aSubs.begin();
+    for (; hasNewAttr && aSIt != aSubs.end(); ++aSIt) {
+      if ((*aSIt)->baseAttribute()) {
+        aFoundAttr = anAttributes.find((*aSIt)->baseAttribute());
+        if (aFoundAttr != anAttributes.end())
+          anAttributes.erase(aFoundAttr);
+      } else {
+        aFoundFeat = aFeatures.find((*aSIt)->baseFeature());
+        if (aFoundFeat != aFeatures.end())
+          aFeatures.erase(aFoundFeat);
+      }
+      hasNewAttr = !(anAttributes.empty() && aFeatures.empty());
     }
-    if (aNbLines == 1)
-      myType = SLVS_C_PT_PT_DISTANCE;
-    return getType();
+  }
+  if (hasNewAttr) {
+    remove();
+    process();
+    return;
   }
 
-  // Constraint for two parallel/perpendicular lines
-  bool isParallel = (aConstraintKind.compare(SketchPlugin_ConstraintParallel::ID()) == 0);
-  bool isPerpendicular = (aConstraintKind.compare(SketchPlugin_ConstraintPerpendicular::ID()) == 0);
-  if (isParallel || isPerpendicular) {
-    int aNbEntities = 2;  // lines in SolveSpace constraints should start from SketchPlugin_Constraint::ENTITY_C() attribute
-    for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
-      std::shared_ptr<ModelAPI_Attribute> anAttr = 
-          aConstrData->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
-      if (typeOfAttribute(anAttr) == LINE)
-        myAttributesList[aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
-    }
-    if (aNbEntities == 4)
-      myType = isParallel ? SLVS_C_PARALLEL : SLVS_C_PERPENDICULAR;
-    return getType();
+  AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+      myBaseConstraint->attribute(SketchPlugin_Constraint::VALUE()));
+  if (aValueAttr) {
+    for (aWIt = aWrapper.begin(); aWIt != aWrapper.end(); ++aWIt)
+      if (fabs((*aWIt)->value() - aValueAttr->value()) > tolerance) {
+        (*aWIt)->setValue(aValueAttr->value());
+        myStorage->setNeedToResolve(true);
+      }
   }
+  myStorage->addConstraint(myBaseConstraint, aWrapper);
 
-  // Constraint for radius of a circle or an arc of circle
-  if (aConstraintKind.compare(SketchPlugin_ConstraintRadius::ID()) == 0) {
-    int aNbEntities = 2;  // lines in SolveSpace constraints should started from SketchPlugin_Constraint::ENTITY_C() attribute
-    for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
-      std::shared_ptr<ModelAPI_Attribute> anAttr = 
-          aConstrData->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
-      AttrType aType = typeOfAttribute(anAttr);
-      if (aType == CIRCLE || aType == ARC)
-        myAttributesList[aNbEntities++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
+  adjustConstraint();
+}
+
+bool SketchSolver_Constraint::remove()
+{
+  cleanErrorMsg();
+  myType = CONSTRAINT_UNKNOWN;
+  return myStorage->removeConstraint(myBaseConstraint);
+}
+
+void SketchSolver_Constraint::getAttributes(
+    double& theValue,
+    std::vector<EntityWrapperPtr>& theAttributes)
+{
+  static const int anInitNbOfAttr = 4;
+  theAttributes.assign(anInitNbOfAttr, EntityWrapperPtr());
+  myAttributes.clear();
+
+  DataPtr aData = myBaseConstraint->data();
+  BuilderPtr aBuilder = SketchSolver_Manager::instance()->builder();
+
+  myType = TYPE(myBaseConstraint);
+
+  AttributeDoublePtr aValueAttr = std::dynamic_pointer_cast<ModelAPI_AttributeDouble>(
+      aData->attribute(SketchPlugin_Constraint::VALUE()));
+  theValue = aValueAttr ? aValueAttr->value() : 0.0;
+
+  int aPtInd = 0; // index of first point in the list of attributes
+  int aEntInd = 2; // index of first entity in the list of attributes
+  std::list<AttributePtr> aConstrAttrs = aData->attributes(ModelAPI_AttributeRefAttr::typeId());
+  std::list<AttributePtr>::iterator anIter = aConstrAttrs.begin();
+  for (; anIter != aConstrAttrs.end(); anIter++) {
+    AttributeRefAttrPtr aRefAttr =
+        std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(*anIter);
+    if (!aRefAttr || !aRefAttr->isInitialized()) {
+      myErrorMsg = SketchSolver_Error::NOT_INITIALIZED();
+      return;
     }
-    if (aNbEntities == 3)
-      myType = SLVS_C_DIAMETER;
-    return getType();
-  }
 
-  // Constraint for fixed entity
-  if (aConstraintKind.compare(SketchPlugin_ConstraintRigid::ID()) == 0) {
-    // Verify that only one entity is filled
-    int aNbAttrs = 0;
-    for (unsigned int indAttr = 0; indAttr < CONSTRAINT_ATTR_SIZE; indAttr++) {
-      std::shared_ptr<ModelAPI_Attribute> anAttr = 
-          aConstrData->attribute(SketchPlugin_Constraint::ATTRIBUTE(indAttr));
-      AttrType aType = typeOfAttribute(anAttr);
-      if (aType != UNKNOWN)
-        myAttributesList[aNbAttrs++] = SketchPlugin_Constraint::ATTRIBUTE(indAttr);
+    myStorage->update(*anIter/*, myGroupID*/);
+    EntityWrapperPtr anEntity = myStorage->entity(*anIter);
+    myAttributes.push_back(anEntity);
+
+    SketchSolver_EntityType aType = anEntity->type();
+    if (aType == ENTITY_UNKNOWN)
+      continue;
+    else if (aType == ENTITY_POINT)
+      theAttributes[aPtInd++] = anEntity; // the point is created
+    else { // another entity (not a point) is created
+      if (aEntInd < anInitNbOfAttr)
+        theAttributes[aEntInd] = anEntity;
+      else
+        theAttributes.push_back(anEntity);
+      aEntInd++;
     }
-    if (aNbAttrs == 1)
-      myType = SLVS_C_WHERE_DRAGGED;
-    return getType();
   }
+}
+
+bool SketchSolver_Constraint::isUsed(FeaturePtr theFeature) const
+{
+  const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
+  std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
+  for (; aCIt != aCList.end(); ++aCIt)
+    if ((*aCIt)->isUsed(theFeature))
+      return true;
 
-  /// \todo Implement other kind of constraints
+  std::list<AttributePtr> anAttrList = theFeature->data()->attributes(GeomDataAPI_Point2D::typeId());
+  std::list<AttributePtr>::const_iterator anAttrIt = anAttrList.begin();
+  for (; anAttrIt != anAttrList.end(); ++ anAttrIt)
+    if (isUsed(*anAttrIt))
+      return true;
 
-  return getType();
+  return false;
 }
 
-// ================= Auxiliary functions ==============================
-AttrType typeOfAttribute(std::shared_ptr<ModelAPI_Attribute> theAttribute)
+bool SketchSolver_Constraint::isUsed(AttributePtr theAttribute) const
 {
-  std::shared_ptr<ModelAPI_AttributeRefAttr> anAttrRef = std::dynamic_pointer_cast<
-      ModelAPI_AttributeRefAttr>(theAttribute);
-  if (!anAttrRef)
-    return UNKNOWN;
-
-  if (anAttrRef->isObject()) {
-    ResultConstructionPtr aRC = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(
-        anAttrRef->object());
-    if (!aRC || !aRC->shape())
-      return UNKNOWN;
-
-    if (aRC->shape()->isVertex())
-      return POINT3D;
-    else if (aRC->shape()->isEdge()) {
-      std::shared_ptr<GeomAPI_Edge> anEdge = std::dynamic_pointer_cast<GeomAPI_Edge>(
-          aRC->shape());
-      if (anEdge->isLine())
-        return LINE;
-      else if (anEdge->isCircle())
-        return CIRCLE;
-      else if (anEdge->isArc())
-        return ARC;
-    }
-  } else {
-    const std::string aType = anAttrRef->attr()->attributeType();
-    if (aType == GeomDataAPI_Point2D::type())
-      return POINT2D;
-    if (aType == GeomDataAPI_Point2D::type())
-      return POINT2D;
+  AttributePtr anAttribute = theAttribute;
+  AttributeRefAttrPtr aRefAttr =
+      std::dynamic_pointer_cast<ModelAPI_AttributeRefAttr>(anAttribute);
+  if (aRefAttr) {
+    if (aRefAttr->isObject())
+      return isUsed(ModelAPI_Feature::feature(aRefAttr->object()));
+    else
+      anAttribute = aRefAttr->attr();
   }
 
-  return UNKNOWN;
+  const std::list<ConstraintWrapperPtr>& aCList = myStorage->constraint(myBaseConstraint);
+  std::list<ConstraintWrapperPtr>::const_iterator aCIt = aCList.begin();
+  for (; aCIt != aCList.end(); ++aCIt)
+    if ((*aCIt)->isUsed(theAttribute))
+      return true;
+  return false;
 }
-